| 82 | } |
| 83 | |
| 84 | Value importwallet(const Array& params, bool fHelp) { |
| 85 | if (fHelp || params.size() != 1) |
| 86 | throw runtime_error( |
| 87 | "importwallet \"filename\"\n" |
| 88 | "\nImports keys from a wallet dump file (see dumpwallet).\n" |
| 89 | "\nArguments:\n" |
| 90 | "1. \"filename\" (string, required) The wallet file to be imported\n" |
| 91 | "\nExamples:\n" |
| 92 | "\nDump the wallet first\n" + |
| 93 | HelpExampleCli("dumpwallet", "\"target_dumpwallet_filepath\"") + "\nImport the wallet\n" + |
| 94 | HelpExampleCli("importwallet", "\"target_dumpwallet_filepath\"") + "\nAs a json rpc call\n" + |
| 95 | HelpExampleRpc("importwallet", "\"target_dumpwallet_filepath\"")); |
| 96 | |
| 97 | EnsureWalletIsUnlocked(); |
| 98 | |
| 99 | LOCK2(cs_main, pWalletMain->cs_wallet); |
| 100 | |
| 101 | ifstream file; |
| 102 | file.open(params[0].get_str().c_str(), ios::in | ios::ate); |
| 103 | if (!file.is_open()) |
| 104 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Fail to open dump file"); |
| 105 | |
| 106 | file.seekg(0, file.beg); |
| 107 | int importedKeySize = 0; |
| 108 | if (file.good()) { |
| 109 | Value reply; |
| 110 | json_spirit::read(file, reply); |
| 111 | const Value& keyObj = find_value(reply.get_obj(), "key"); |
| 112 | const Array& keyArray = keyObj.get_array(); |
| 113 | for (auto const& keyItem : keyArray) { |
| 114 | CKeyCombi keyCombi; |
| 115 | const Value& obj = find_value(keyItem.get_obj(), "keyid"); |
| 116 | if (obj.type() == null_type) |
| 117 | continue; |
| 118 | |
| 119 | string strKeyId = find_value(keyItem.get_obj(), "keyid").get_str(); |
| 120 | CKeyID keyId(uint160(ParseHex(strKeyId))); |
| 121 | keyCombi.UnSerializeFromJson(keyItem.get_obj()); |
| 122 | if (!keyCombi.HasMainKey() && !keyCombi.HaveMinerKey()) |
| 123 | continue; |
| 124 | |
| 125 | if (pWalletMain->AddKey(keyId, keyCombi)) |
| 126 | importedKeySize++; |
| 127 | } |
| 128 | } |
| 129 | file.close(); |
| 130 | |
| 131 | Object reply2; |
| 132 | reply2.push_back(Pair("info", "successfully imported wallet")); |
| 133 | reply2.push_back(Pair("count", importedKeySize)); |
| 134 | return reply2; |
| 135 | } |
| 136 | |
| 137 | Value dumpprivkey(const Array& params, bool fHelp) { |
| 138 | if (fHelp || params.size() != 1) |
nothing calls this directly
no test coverage detected