Skip to content

Commit 94bf156

Browse files
committed
Have importaddress use ImportScripts and ImportScriptPubKeys
Also removes the now unused ImportAddress and ImportScript from rpcdump.cpp Behavior changes: * No errors will be thrown when the script or key already exists in the wallet. * If the key or script is already in the wallet, their labels will be updated.
1 parent a00d1e5 commit 94bf156

File tree

1 file changed

+14
-38
lines changed

1 file changed

+14
-38
lines changed

src/wallet/rpcdump.cpp

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -235,42 +235,6 @@ UniValue abortrescan(const JSONRPCRequest& request)
235235
return true;
236236
}
237237

238-
static void ImportAddress(CWallet*, const CTxDestination& dest, const std::string& strLabel);
239-
static void ImportScript(CWallet* const pwallet, const CScript& script, const std::string& strLabel, bool isRedeemScript) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
240-
{
241-
if (!isRedeemScript && ::IsMine(*pwallet, script) == ISMINE_SPENDABLE) {
242-
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
243-
}
244-
245-
pwallet->MarkDirty();
246-
247-
if (!pwallet->HaveWatchOnly(script) && !pwallet->AddWatchOnly(script, 0 /* nCreateTime */)) {
248-
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
249-
}
250-
251-
if (isRedeemScript) {
252-
const CScriptID id(script);
253-
if (!pwallet->HaveCScript(id) && !pwallet->AddCScript(script)) {
254-
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding p2sh redeemScript to wallet");
255-
}
256-
ImportAddress(pwallet, ScriptHash(id), strLabel);
257-
} else {
258-
CTxDestination destination;
259-
if (ExtractDestination(script, destination)) {
260-
pwallet->SetAddressBook(destination, strLabel, "receive");
261-
}
262-
}
263-
}
264-
265-
static void ImportAddress(CWallet* const pwallet, const CTxDestination& dest, const std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
266-
{
267-
CScript script = GetScriptForDestination(dest);
268-
ImportScript(pwallet, script, strLabel, false);
269-
// add to address book or update label
270-
if (IsValidDestination(dest))
271-
pwallet->SetAddressBook(dest, strLabel, "receive");
272-
}
273-
274238
UniValue importaddress(const JSONRPCRequest& request)
275239
{
276240
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -343,10 +307,22 @@ UniValue importaddress(const JSONRPCRequest& request)
343307
if (fP2SH) {
344308
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead");
345309
}
346-
ImportAddress(pwallet, dest, strLabel);
310+
311+
pwallet->MarkDirty();
312+
313+
pwallet->ImportScriptPubKeys(strLabel, {GetScriptForDestination(dest)}, false /* have_solving_data */, true /* apply_label */, 1 /* timestamp */);
347314
} else if (IsHex(request.params[0].get_str())) {
348315
std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
349-
ImportScript(pwallet, CScript(data.begin(), data.end()), strLabel, fP2SH);
316+
CScript redeem_script(data.begin(), data.end());
317+
318+
std::set<CScript> scripts = {redeem_script};
319+
pwallet->ImportScripts(scripts);
320+
321+
if (fP2SH) {
322+
scripts.insert(GetScriptForDestination(ScriptHash(CScriptID(redeem_script))));
323+
}
324+
325+
pwallet->ImportScriptPubKeys(strLabel, scripts, false /* have_solving_data */, true /* apply_label */, 1 /* timestamp */);
350326
} else {
351327
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
352328
}

0 commit comments

Comments
 (0)