TODO: enable rescan wallet.
| 170 | |
| 171 | // TODO: enable rescan wallet. |
| 172 | Value importprivkey(const Array& params, bool fHelp) { |
| 173 | if (fHelp || (params.size() != 1 && params.size() != 2)) |
| 174 | throw runtime_error( |
| 175 | "importprivkey \"privkey\"\n" |
| 176 | "\nAdds a private key (as returned by dumpprivkey) to your wallet.\n" |
| 177 | "\nArguments:\n" |
| 178 | "1.\"privkey\" (string, required) The private key, which can be the mining key when address is supplied (also refer to dumpprivkey)\n" |
| 179 | "2.\"address\" (string, optional) Set the miner's saving account address when importing the mining privkey in front\n" |
| 180 | "\nExamples:\n" |
| 181 | "\nDump privkey first\n" + |
| 182 | HelpExampleCli("dumpprivkey", "\"address\"") + "\nImport privkey\n" + |
| 183 | HelpExampleCli("importprivkey", "\"privkey\"") + "\nAs a json rpc call\n" + |
| 184 | HelpExampleRpc("importprivkey", "\"privkey\"")); |
| 185 | |
| 186 | EnsureWalletIsUnlocked(); |
| 187 | |
| 188 | string strSecret = params[0].get_str(); |
| 189 | CCoinSecret vchSecret; |
| 190 | bool fGood = vchSecret.SetString(strSecret); |
| 191 | |
| 192 | if (!fGood) |
| 193 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding."); |
| 194 | |
| 195 | CKey key = vchSecret.GetKey(); |
| 196 | if (!key.IsValid()) |
| 197 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range."); |
| 198 | |
| 199 | CPubKey pubkey = key.GetPubKey(); |
| 200 | { |
| 201 | LOCK2(cs_main, pWalletMain->cs_wallet); |
| 202 | |
| 203 | if (params.size() == 1) { |
| 204 | if (!pWalletMain->AddKey(key)) |
| 205 | throw JSONRPCError(RPC_WALLET_ERROR, "Failed to add key into wallet."); |
| 206 | } else { |
| 207 | CKeyCombi keyCombi; |
| 208 | CKey emptyMainKey; |
| 209 | keyCombi.SetMainKey(emptyMainKey); |
| 210 | keyCombi.SetMinerKey(key); |
| 211 | |
| 212 | CKeyID keyid = RPC_PARAM::GetKeyId(params[1]); |
| 213 | |
| 214 | if (!pWalletMain->AddKey(keyid, keyCombi)) |
| 215 | throw JSONRPCError(RPC_WALLET_ERROR, "Failed to add key into wallet."); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | Object ret; |
| 220 | ret.push_back(Pair("imported_key_address", pubkey.GetKeyId().ToAddress())); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | Value dropminermainkeys(const Array& params, bool fHelp) { |
| 225 | if (fHelp || params.size() != 0) { |
nothing calls this directly
no test coverage detected