Split up importaddress into helper functions
This commit is contained in:
parent
cfc3dd3428
commit
983d2d90af
1 changed files with 36 additions and 35 deletions
|
@ -146,6 +146,26 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
|
||||||
return NullUniValue;
|
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)
|
UniValue importaddress(const UniValue& params, bool fHelp)
|
||||||
{
|
{
|
||||||
if (!EnsureWalletIsAvailable(fHelp))
|
if (!EnsureWalletIsAvailable(fHelp))
|
||||||
|
@ -172,20 +192,6 @@ UniValue importaddress(const UniValue& params, bool fHelp)
|
||||||
if (fPruneMode)
|
if (fPruneMode)
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Importing addresses is disabled in pruned mode");
|
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 = "";
|
string strLabel = "";
|
||||||
if (params.size() > 1)
|
if (params.size() > 1)
|
||||||
strLabel = params[1].get_str();
|
strLabel = params[1].get_str();
|
||||||
|
@ -195,29 +201,24 @@ UniValue importaddress(const UniValue& params, bool fHelp)
|
||||||
if (params.size() > 2)
|
if (params.size() > 2)
|
||||||
fRescan = params[2].get_bool();
|
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
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||||
if (address.IsValid())
|
|
||||||
pwalletMain->SetAddressBook(address.Get(), strLabel, "receive");
|
|
||||||
|
|
||||||
// Don't throw error in case an address is already there
|
CBitcoinAddress address(params[0].get_str());
|
||||||
if (pwalletMain->HaveWatchOnly(script))
|
if (address.IsValid()) {
|
||||||
return NullUniValue;
|
ImportAddress(address, strLabel);
|
||||||
|
} else if (IsHex(params[0].get_str())) {
|
||||||
pwalletMain->MarkDirty();
|
std::vector<unsigned char> data(ParseHex(params[0].get_str()));
|
||||||
|
ImportScript(CScript(data.begin(), data.end()));
|
||||||
if (!pwalletMain->AddWatchOnly(script))
|
} else {
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
|
||||||
|
}
|
||||||
|
|
||||||
if (fRescan)
|
if (fRescan)
|
||||||
{
|
{
|
||||||
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
|
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
|
||||||
pwalletMain->ReacceptWalletTransactions();
|
pwalletMain->ReacceptWalletTransactions();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return NullUniValue;
|
return NullUniValue;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue