Skip to content

Commit c6a8274

Browse files
committed
Have importprivkey use CWallet's ImportPrivKeys, ImportScripts, and ImportScriptPubKeys
Behavior changes: * If we already have the key, it's wpkh script will still be added, although it should already be there
1 parent fae7a5b commit c6a8274

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/wallet/rpcdump.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,15 @@ UniValue importprivkey(const JSONRPCRequest& request)
187187
}
188188
}
189189

190-
// Don't throw error in case a key is already there
191-
if (pwallet->HaveKey(vchAddress)) {
192-
return NullUniValue;
190+
// Use timestamp of 1 to scan the whole chain
191+
if (!pwallet->ImportPrivKeys({{vchAddress, key}}, 1)) {
192+
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
193193
}
194194

195-
// whenever a key is imported, we need to scan the whole chain
196-
pwallet->UpdateTimeFirstKey(1);
197-
pwallet->mapKeyMetadata[vchAddress].nCreateTime = 1;
198-
199-
if (!pwallet->AddKeyPubKey(key, pubkey)) {
200-
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
195+
// Add the wpkh script for this key if possible
196+
if (pubkey.IsCompressed()) {
197+
pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))});
201198
}
202-
pwallet->LearnAllRelatedScripts(pubkey);
203199
}
204200
}
205201
if (fRescan) {

0 commit comments

Comments
 (0)