Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcdump.cpp
Show First 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | CKeyID vchAddress = pubkey.GetID(); | ||||
if (fRescan) { | if (fRescan) { | ||||
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); | pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); | ||||
} | } | ||||
} | } | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
void ImportAddress(const CBitcoinAddress &address, const std::string &strLabel); | void ImportAddress(const CTxDestination &dest, const std::string &strLabel); | ||||
void ImportScript(const CScript &script, const std::string &strLabel, | void ImportScript(const CScript &script, const std::string &strLabel, | ||||
bool isRedeemScript) { | bool isRedeemScript) { | ||||
if (!isRedeemScript && ::IsMine(*pwalletMain, script) == ISMINE_SPENDABLE) | if (!isRedeemScript && ::IsMine(*pwalletMain, script) == ISMINE_SPENDABLE) | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the " | throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the " | ||||
"private key for this address or " | "private key for this address or " | ||||
"script"); | "script"); | ||||
pwalletMain->MarkDirty(); | pwalletMain->MarkDirty(); | ||||
if (!pwalletMain->HaveWatchOnly(script) && | if (!pwalletMain->HaveWatchOnly(script) && | ||||
!pwalletMain->AddWatchOnly(script, 0 /* nCreateTime */)) | !pwalletMain->AddWatchOnly(script, 0 /* nCreateTime */)) | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); | throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet"); | ||||
if (isRedeemScript) { | if (isRedeemScript) { | ||||
if (!pwalletMain->HaveCScript(script) && | if (!pwalletMain->HaveCScript(script) && | ||||
!pwalletMain->AddCScript(script)) | !pwalletMain->AddCScript(script)) | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Error adding p2sh redeemScript to wallet"); | "Error adding p2sh redeemScript to wallet"); | ||||
ImportAddress(CBitcoinAddress(CScriptID(script)), strLabel); | ImportAddress(CScriptID(script), strLabel); | ||||
} else { | } else { | ||||
CTxDestination destination; | CTxDestination destination; | ||||
if (ExtractDestination(script, destination)) { | if (ExtractDestination(script, destination)) { | ||||
pwalletMain->SetAddressBook(destination, strLabel, "receive"); | pwalletMain->SetAddressBook(destination, strLabel, "receive"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void ImportAddress(const CBitcoinAddress &address, | void ImportAddress(const CTxDestination &dest, const std::string &strLabel) { | ||||
const std::string &strLabel) { | CScript script = GetScriptForDestination(dest); | ||||
CScript script = GetScriptForDestination(address.Get()); | |||||
ImportScript(script, strLabel, false); | ImportScript(script, strLabel, false); | ||||
// add to address book or update label | // add to address book or update label | ||||
if (address.IsValid()) | if (IsValidDestination(dest)) | ||||
pwalletMain->SetAddressBook(address.Get(), strLabel, "receive"); | pwalletMain->SetAddressBook(dest, strLabel, "receive"); | ||||
} | } | ||||
UniValue importaddress(const Config &config, const JSONRPCRequest &request) { | UniValue importaddress(const Config &config, const JSONRPCRequest &request) { | ||||
if (!EnsureWalletIsAvailable(request.fHelp)) return NullUniValue; | if (!EnsureWalletIsAvailable(request.fHelp)) return NullUniValue; | ||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) | if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"importaddress \"address\" ( \"label\" rescan p2sh )\n" | "importaddress \"address\" ( \"label\" rescan p2sh )\n" | ||||
Show All 36 Lines | if (fRescan && fPruneMode) | ||||
"Rescan is disabled in pruned mode"); | "Rescan is disabled in pruned mode"); | ||||
// Whether to import a p2sh version, too | // Whether to import a p2sh version, too | ||||
bool fP2SH = false; | bool fP2SH = false; | ||||
if (request.params.size() > 3) fP2SH = request.params[3].get_bool(); | if (request.params.size() > 3) fP2SH = request.params[3].get_bool(); | ||||
LOCK2(cs_main, pwalletMain->cs_wallet); | LOCK2(cs_main, pwalletMain->cs_wallet); | ||||
CBitcoinAddress address(request.params[0].get_str()); | CTxDestination dest = DecodeDestination(request.params[0].get_str()); | ||||
if (address.IsValid()) { | if (IsValidDestination(dest)) { | ||||
if (fP2SH) | if (fP2SH) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Cannot use the p2sh flag with an address - use " | "Cannot use the p2sh flag with an address - use " | ||||
"a script instead"); | "a script instead"); | ||||
ImportAddress(address, strLabel); | } | ||||
ImportAddress(dest, strLabel); | |||||
} else if (IsHex(request.params[0].get_str())) { | } else if (IsHex(request.params[0].get_str())) { | ||||
std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | ||||
ImportScript(CScript(data.begin(), data.end()), strLabel, fP2SH); | ImportScript(CScript(data.begin(), data.end()), strLabel, fP2SH); | ||||
} else { | } else { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Invalid Bitcoin address or script"); | "Invalid Bitcoin address or script"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 166 Lines • ▼ Show 20 Lines | UniValue importpubkey(const Config &config, const JSONRPCRequest &request) { | ||||
std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | std::vector<uint8_t> data(ParseHex(request.params[0].get_str())); | ||||
CPubKey pubKey(data.begin(), data.end()); | CPubKey pubKey(data.begin(), data.end()); | ||||
if (!pubKey.IsFullyValid()) | if (!pubKey.IsFullyValid()) | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Pubkey is not a valid public key"); | "Pubkey is not a valid public key"); | ||||
LOCK2(cs_main, pwalletMain->cs_wallet); | LOCK2(cs_main, pwalletMain->cs_wallet); | ||||
ImportAddress(CBitcoinAddress(pubKey.GetID()), strLabel); | ImportAddress(pubKey.GetID(), strLabel); | ||||
ImportScript(GetScriptForRawPubKey(pubKey), strLabel, false); | ImportScript(GetScriptForRawPubKey(pubKey), strLabel, false); | ||||
if (fRescan) { | if (fRescan) { | ||||
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); | pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); | ||||
pwalletMain->ReacceptWalletTransactions(); | pwalletMain->ReacceptWalletTransactions(); | ||||
} | } | ||||
return NullUniValue; | return NullUniValue; | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | while (file.good()) { | ||||
CBitcoinSecret vchSecret; | CBitcoinSecret vchSecret; | ||||
if (!vchSecret.SetString(vstr[0])) continue; | if (!vchSecret.SetString(vstr[0])) continue; | ||||
CKey key = vchSecret.GetKey(); | CKey key = vchSecret.GetKey(); | ||||
CPubKey pubkey = key.GetPubKey(); | CPubKey pubkey = key.GetPubKey(); | ||||
assert(key.VerifyPubKey(pubkey)); | assert(key.VerifyPubKey(pubkey)); | ||||
CKeyID keyid = pubkey.GetID(); | CKeyID keyid = pubkey.GetID(); | ||||
if (pwalletMain->HaveKey(keyid)) { | if (pwalletMain->HaveKey(keyid)) { | ||||
LogPrintf("Skipping import of %s (key already present)\n", | LogPrintf("Skipping import of %s (key already present)\n", | ||||
CBitcoinAddress(keyid).ToString()); | EncodeDestination(keyid)); | ||||
continue; | continue; | ||||
} | } | ||||
int64_t nTime = DecodeDumpTime(vstr[1]); | int64_t nTime = DecodeDumpTime(vstr[1]); | ||||
std::string strLabel; | std::string strLabel; | ||||
bool fLabel = true; | bool fLabel = true; | ||||
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { | for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { | ||||
if (boost::algorithm::starts_with(vstr[nStr], "#")) break; | if (boost::algorithm::starts_with(vstr[nStr], "#")) break; | ||||
if (vstr[nStr] == "change=1") fLabel = false; | if (vstr[nStr] == "change=1") fLabel = false; | ||||
if (vstr[nStr] == "reserve=1") fLabel = false; | if (vstr[nStr] == "reserve=1") fLabel = false; | ||||
if (boost::algorithm::starts_with(vstr[nStr], "label=")) { | if (boost::algorithm::starts_with(vstr[nStr], "label=")) { | ||||
strLabel = DecodeDumpString(vstr[nStr].substr(6)); | strLabel = DecodeDumpString(vstr[nStr].substr(6)); | ||||
fLabel = true; | fLabel = true; | ||||
} | } | ||||
} | } | ||||
LogPrintf("Importing %s...\n", CBitcoinAddress(keyid).ToString()); | LogPrintf("Importing %s...\n", EncodeDestination(keyid)); | ||||
if (!pwalletMain->AddKeyPubKey(key, pubkey)) { | if (!pwalletMain->AddKeyPubKey(key, pubkey)) { | ||||
fGood = false; | fGood = false; | ||||
continue; | continue; | ||||
} | } | ||||
pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime; | pwalletMain->mapKeyMetadata[keyid].nCreateTime = nTime; | ||||
if (fLabel) pwalletMain->SetAddressBook(keyid, strLabel, "receive"); | if (fLabel) pwalletMain->SetAddressBook(keyid, strLabel, "receive"); | ||||
nTimeBegin = std::min(nTimeBegin, nTime); | nTimeBegin = std::min(nTimeBegin, nTime); | ||||
} | } | ||||
Show All 37 Lines | if (request.fHelp || request.params.size() != 1) | ||||
HelpExampleCli("importprivkey", "\"mykey\"") + | HelpExampleCli("importprivkey", "\"mykey\"") + | ||||
HelpExampleRpc("dumpprivkey", "\"myaddress\"")); | HelpExampleRpc("dumpprivkey", "\"myaddress\"")); | ||||
LOCK2(cs_main, pwalletMain->cs_wallet); | LOCK2(cs_main, pwalletMain->cs_wallet); | ||||
EnsureWalletIsUnlocked(); | EnsureWalletIsUnlocked(); | ||||
std::string strAddress = request.params[0].get_str(); | std::string strAddress = request.params[0].get_str(); | ||||
CBitcoinAddress address; | CTxDestination dest = DecodeDestination(strAddress); | ||||
if (!address.SetString(strAddress)) | if (!IsValidDestination(dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Invalid Bitcoin address"); | "Invalid Bitcoin address"); | ||||
CKeyID keyID; | } | ||||
if (!address.GetKeyID(keyID)) | const CKeyID *keyID = boost::get<CKeyID>(&dest); | ||||
if (!keyID) { | |||||
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); | throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key"); | ||||
} | |||||
CKey vchSecret; | CKey vchSecret; | ||||
if (!pwalletMain->GetKey(keyID, vchSecret)) | if (!pwalletMain->GetKey(*keyID, vchSecret)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + | throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + | ||||
strAddress + " is not known"); | strAddress + " is not known"); | ||||
} | |||||
return CBitcoinSecret(vchSecret).ToString(); | return CBitcoinSecret(vchSecret).ToString(); | ||||
} | } | ||||
UniValue dumpwallet(const Config &config, const JSONRPCRequest &request) { | UniValue dumpwallet(const Config &config, const JSONRPCRequest &request) { | ||||
if (!EnsureWalletIsAvailable(request.fHelp)) return NullUniValue; | if (!EnsureWalletIsAvailable(request.fHelp)) return NullUniValue; | ||||
if (request.fHelp || request.params.size() != 1) | if (request.fHelp || request.params.size() != 1) | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | if (!masterKeyID.IsNull()) { | ||||
<< "\n\n"; | << "\n\n"; | ||||
} | } | ||||
} | } | ||||
for (std::vector<std::pair<int64_t, CKeyID>>::const_iterator it = | for (std::vector<std::pair<int64_t, CKeyID>>::const_iterator it = | ||||
vKeyBirth.begin(); | vKeyBirth.begin(); | ||||
it != vKeyBirth.end(); it++) { | it != vKeyBirth.end(); it++) { | ||||
const CKeyID &keyid = it->second; | const CKeyID &keyid = it->second; | ||||
std::string strTime = EncodeDumpTime(it->first); | std::string strTime = EncodeDumpTime(it->first); | ||||
std::string strAddr = CBitcoinAddress(keyid).ToString(); | std::string strAddr = EncodeDestination(keyid); | ||||
CKey key; | CKey key; | ||||
if (pwalletMain->GetKey(keyid, key)) { | if (pwalletMain->GetKey(keyid, key)) { | ||||
file << strprintf("%s %s ", CBitcoinSecret(key).ToString(), | file << strprintf("%s %s ", CBitcoinSecret(key).ToString(), | ||||
strTime); | strTime); | ||||
if (pwalletMain->mapAddressBook.count(keyid)) { | if (pwalletMain->mapAddressBook.count(keyid)) { | ||||
file << strprintf( | file << strprintf( | ||||
"label=%s", | "label=%s", | ||||
EncodeDumpString(pwalletMain->mapAddressBook[keyid].name)); | EncodeDumpString(pwalletMain->mapAddressBook[keyid].name)); | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | try { | ||||
bool isScript = scriptPubKey.getType() == UniValue::VSTR; | bool isScript = scriptPubKey.getType() == UniValue::VSTR; | ||||
bool isP2SH = strRedeemScript.length() > 0; | bool isP2SH = strRedeemScript.length() > 0; | ||||
const std::string &output = isScript | const std::string &output = isScript | ||||
? scriptPubKey.get_str() | ? scriptPubKey.get_str() | ||||
: scriptPubKey["address"].get_str(); | : scriptPubKey["address"].get_str(); | ||||
// Parse the output. | // Parse the output. | ||||
CScript script; | CScript script; | ||||
CBitcoinAddress address; | CTxDestination dest; | ||||
if (!isScript) { | if (!isScript) { | ||||
address = CBitcoinAddress(output); | dest = DecodeDestination(output); | ||||
if (!address.IsValid()) { | if (!IsValidDestination(dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Invalid address"); | "Invalid address"); | ||||
} | } | ||||
script = GetScriptForDestination(address.Get()); | script = GetScriptForDestination(dest); | ||||
} else { | } else { | ||||
if (!IsHex(output)) { | if (!IsHex(output)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Invalid scriptPubKey"); | "Invalid scriptPubKey"); | ||||
} | } | ||||
std::vector<uint8_t> vData(ParseHex(output)); | std::vector<uint8_t> vData(ParseHex(output)); | ||||
script = CScript(vData.begin(), vData.end()); | script = CScript(vData.begin(), vData.end()); | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | try { | ||||
} | } | ||||
if (!pwalletMain->HaveCScript(redeemScript) && | if (!pwalletMain->HaveCScript(redeemScript) && | ||||
!pwalletMain->AddCScript(redeemScript)) { | !pwalletMain->AddCScript(redeemScript)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Error adding p2sh redeemScript to wallet"); | "Error adding p2sh redeemScript to wallet"); | ||||
} | } | ||||
CBitcoinAddress redeemAddress = | CTxDestination redeem_dest = CScriptID(redeemScript); | ||||
CBitcoinAddress(CScriptID(redeemScript)); | CScript redeemDestination = GetScriptForDestination(redeem_dest); | ||||
CScript redeemDestination = | |||||
GetScriptForDestination(redeemAddress.Get()); | |||||
if (::IsMine(*pwalletMain, redeemDestination) == ISMINE_SPENDABLE) { | if (::IsMine(*pwalletMain, redeemDestination) == ISMINE_SPENDABLE) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"The wallet already contains the private " | "The wallet already contains the private " | ||||
"key for this address or script"); | "key for this address or script"); | ||||
} | } | ||||
pwalletMain->MarkDirty(); | pwalletMain->MarkDirty(); | ||||
if (!pwalletMain->HaveWatchOnly(redeemDestination) && | if (!pwalletMain->HaveWatchOnly(redeemDestination) && | ||||
!pwalletMain->AddWatchOnly(redeemDestination, timestamp)) { | !pwalletMain->AddWatchOnly(redeemDestination, timestamp)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Error adding address to wallet"); | "Error adding address to wallet"); | ||||
} | } | ||||
// add to address book or update label | // add to address book or update label | ||||
if (address.IsValid()) { | if (IsValidDestination(dest)) { | ||||
pwalletMain->SetAddressBook(address.Get(), label, "receive"); | pwalletMain->SetAddressBook(dest, label, "receive"); | ||||
} | } | ||||
// Import private keys. | // Import private keys. | ||||
if (keys.size()) { | if (keys.size()) { | ||||
for (size_t i = 0; i < keys.size(); i++) { | for (size_t i = 0; i < keys.size(); i++) { | ||||
const std::string &privkey = keys[i].get_str(); | const std::string &privkey = keys[i].get_str(); | ||||
CBitcoinSecret vchSecret; | CBitcoinSecret vchSecret; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | try { | ||||
std::vector<uint8_t> vData(ParseHex(strPubKey)); | std::vector<uint8_t> vData(ParseHex(strPubKey)); | ||||
CPubKey pubKey(vData.begin(), vData.end()); | CPubKey pubKey(vData.begin(), vData.end()); | ||||
if (!pubKey.IsFullyValid()) { | if (!pubKey.IsFullyValid()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Pubkey is not a valid public key"); | "Pubkey is not a valid public key"); | ||||
} | } | ||||
CBitcoinAddress pubKeyAddress = CBitcoinAddress(pubKey.GetID()); | CTxDestination pubkey_dest = pubKey.GetID(); | ||||
// Consistency check. | // Consistency check. | ||||
if (!isScript && !(pubKeyAddress.Get() == address.Get())) { | if (!isScript && !(pubkey_dest == dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Consistency check failed"); | "Consistency check failed"); | ||||
} | } | ||||
// Consistency check. | // Consistency check. | ||||
if (isScript) { | if (isScript) { | ||||
CBitcoinAddress scriptAddress; | |||||
CTxDestination destination; | CTxDestination destination; | ||||
if (ExtractDestination(script, destination)) { | if (ExtractDestination(script, destination)) { | ||||
scriptAddress = CBitcoinAddress(destination); | if (!(destination == pubkey_dest)) { | ||||
if (!(scriptAddress.Get() == pubKeyAddress.Get())) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Consistency check failed"); | "Consistency check failed"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
CScript pubKeyScript = | CScript pubKeyScript = GetScriptForDestination(pubkey_dest); | ||||
GetScriptForDestination(pubKeyAddress.Get()); | |||||
if (::IsMine(*pwalletMain, pubKeyScript) == ISMINE_SPENDABLE) { | if (::IsMine(*pwalletMain, pubKeyScript) == ISMINE_SPENDABLE) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already " | throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already " | ||||
"contains the private " | "contains the private " | ||||
"key for this address " | "key for this address " | ||||
"or script"); | "or script"); | ||||
} | } | ||||
pwalletMain->MarkDirty(); | pwalletMain->MarkDirty(); | ||||
if (!pwalletMain->HaveWatchOnly(pubKeyScript) && | if (!pwalletMain->HaveWatchOnly(pubKeyScript) && | ||||
!pwalletMain->AddWatchOnly(pubKeyScript, timestamp)) { | !pwalletMain->AddWatchOnly(pubKeyScript, timestamp)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Error adding address to wallet"); | "Error adding address to wallet"); | ||||
} | } | ||||
// add to address book or update label | // add to address book or update label | ||||
if (pubKeyAddress.IsValid()) { | if (IsValidDestination(pubkey_dest)) { | ||||
pwalletMain->SetAddressBook(pubKeyAddress.Get(), label, | pwalletMain->SetAddressBook(pubkey_dest, label, "receive"); | ||||
"receive"); | |||||
} | } | ||||
// TODO Is this necessary? | // TODO Is this necessary? | ||||
CScript scriptRawPubKey = GetScriptForRawPubKey(pubKey); | CScript scriptRawPubKey = GetScriptForRawPubKey(pubKey); | ||||
if (::IsMine(*pwalletMain, scriptRawPubKey) == | if (::IsMine(*pwalletMain, scriptRawPubKey) == | ||||
ISMINE_SPENDABLE) { | ISMINE_SPENDABLE) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already " | throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already " | ||||
Show All 30 Lines | try { | ||||
if (!key.IsValid()) { | if (!key.IsValid()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Private key outside allowed range"); | "Private key outside allowed range"); | ||||
} | } | ||||
CPubKey pubKey = key.GetPubKey(); | CPubKey pubKey = key.GetPubKey(); | ||||
assert(key.VerifyPubKey(pubKey)); | assert(key.VerifyPubKey(pubKey)); | ||||
CBitcoinAddress pubKeyAddress = CBitcoinAddress(pubKey.GetID()); | CTxDestination pubkey_dest = pubKey.GetID(); | ||||
// Consistency check. | // Consistency check. | ||||
if (!isScript && !(pubKeyAddress.Get() == address.Get())) { | if (!isScript && !(pubkey_dest == dest)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Consistency check failed"); | "Consistency check failed"); | ||||
} | } | ||||
// Consistency check. | // Consistency check. | ||||
if (isScript) { | if (isScript) { | ||||
CBitcoinAddress scriptAddress; | |||||
CTxDestination destination; | CTxDestination destination; | ||||
if (ExtractDestination(script, destination)) { | if (ExtractDestination(script, destination)) { | ||||
scriptAddress = CBitcoinAddress(destination); | if (!(destination == pubkey_dest)) { | ||||
if (!(scriptAddress.Get() == pubKeyAddress.Get())) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Consistency check failed"); | "Consistency check failed"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
CKeyID vchAddress = pubKey.GetID(); | CKeyID vchAddress = pubKey.GetID(); | ||||
pwalletMain->MarkDirty(); | pwalletMain->MarkDirty(); | ||||
Show All 29 Lines | try { | ||||
if (!pwalletMain->HaveWatchOnly(script) && | if (!pwalletMain->HaveWatchOnly(script) && | ||||
!pwalletMain->AddWatchOnly(script, timestamp)) { | !pwalletMain->AddWatchOnly(script, timestamp)) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Error adding address to wallet"); | "Error adding address to wallet"); | ||||
} | } | ||||
if (scriptPubKey.getType() == UniValue::VOBJ) { | if (scriptPubKey.getType() == UniValue::VOBJ) { | ||||
// add to address book or update label | // add to address book or update label | ||||
if (address.IsValid()) { | if (IsValidDestination(dest)) { | ||||
pwalletMain->SetAddressBook(address.Get(), label, | pwalletMain->SetAddressBook(dest, label, "receive"); | ||||
"receive"); | |||||
} | } | ||||
} | } | ||||
success = true; | success = true; | ||||
} | } | ||||
} | } | ||||
UniValue result = UniValue(UniValue::VOBJ); | UniValue result = UniValue(UniValue::VOBJ); | ||||
▲ Show 20 Lines • Show All 202 Lines • Show Last 20 Lines |