diff options
author | Matt Corallo <git@bluematt.me> | 2015-06-11 00:57:26 -0700 |
---|---|---|
committer | Matt Corallo <git@bluematt.me> | 2015-07-20 16:01:37 -0700 |
commit | 983d2d90af1b517bee51170d2ea059e68d09be35 (patch) | |
tree | b841edc61897ad53d9cd5b83cc6e2ed08fddf100 /src | |
parent | cfc3dd34284357262bcc7eef2714a210891276c0 (diff) | |
download | bitcoin-983d2d90af1b517bee51170d2ea059e68d09be35.tar.xz |
Split up importaddress into helper functions
Diffstat (limited to 'src')
-rw-r--r-- | src/wallet/rpcdump.cpp | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 5f800474a0..f56ff65f83 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -146,6 +146,26 @@ UniValue importprivkey(const UniValue& params, bool fHelp) return NullUniValue; } +void ImportScript(const CScript& script) +{ + if (::IsMine(*pwalletMain, script) == ISMINE_SPENDABLE) + throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script"); + + pwalletMain->MarkDirty(); + + if (!pwalletMain->HaveWatchOnly(script) && !pwalletMain->AddWatchOnly(script)) + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); +} + +void ImportAddress(const CBitcoinAddress& address, const string& strLabel) +{ + CScript script = GetScriptForDestination(address.Get()); + ImportScript(script, false); + // add to address book or update label + if (address.IsValid()) + pwalletMain->SetAddressBook(address.Get(), strLabel, "receive"); +} + UniValue importaddress(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) @@ -172,20 +192,6 @@ UniValue importaddress(const UniValue& params, bool fHelp) if (fPruneMode) throw JSONRPCError(RPC_WALLET_ERROR, "Importing addresses is disabled in pruned mode"); - LOCK2(cs_main, pwalletMain->cs_wallet); - - CScript script; - - CBitcoinAddress address(params[0].get_str()); - if (address.IsValid()) { - script = GetScriptForDestination(address.Get()); - } else if (IsHex(params[0].get_str())) { - std::vector<unsigned char> data(ParseHex(params[0].get_str())); - script = CScript(data.begin(), data.end()); - } else { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script"); - } - string strLabel = ""; if (params.size() > 1) strLabel = params[1].get_str(); @@ -195,28 +201,23 @@ UniValue importaddress(const UniValue& params, bool fHelp) if (params.size() > 2) fRescan = params[2].get_bool(); - { - if (::IsMine(*pwalletMain, script) == ISMINE_SPENDABLE) - throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script"); - // add to address book or update label - if (address.IsValid()) - pwalletMain->SetAddressBook(address.Get(), strLabel, "receive"); - - // Don't throw error in case an address is already there - if (pwalletMain->HaveWatchOnly(script)) - return NullUniValue; - - pwalletMain->MarkDirty(); + LOCK2(cs_main, pwalletMain->cs_wallet); - if (!pwalletMain->AddWatchOnly(script)) - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); + CBitcoinAddress address(params[0].get_str()); + if (address.IsValid()) { + ImportAddress(address, strLabel); + } else if (IsHex(params[0].get_str())) { + std::vector<unsigned char> data(ParseHex(params[0].get_str())); + ImportScript(CScript(data.begin(), data.end())); + } else { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script"); + } - if (fRescan) - { - pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); - pwalletMain->ReacceptWalletTransactions(); - } + if (fRescan) + { + pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); + pwalletMain->ReacceptWalletTransactions(); } return NullUniValue; |