@@ -185,19 +185,15 @@ UniValue importprivkey(const JSONRPCRequest& request)
185
185
}
186
186
}
187
187
188
- // Don't throw error in case a key is already there
189
- if (pwallet->HaveKey ( vchAddress)) {
190
- return NullUniValue ;
188
+ // Use timestamp of 1 to scan the whole chain
189
+ if (! pwallet->ImportPrivKeys ({{ vchAddress, key}}, 1 )) {
190
+ throw JSONRPCError (RPC_WALLET_ERROR, " Error adding key to wallet " ) ;
191
191
}
192
192
193
- // whenever a key is imported, we need to scan the whole chain
194
- pwallet->UpdateTimeFirstKey (1 );
195
- pwallet->mapKeyMetadata [vchAddress].nCreateTime = 1 ;
196
-
197
- if (!pwallet->AddKeyPubKey (key, pubkey)) {
198
- throw JSONRPCError (RPC_WALLET_ERROR, " Error adding key to wallet" );
193
+ // Add the wpkh script for this key if possible
194
+ if (pubkey.IsCompressed ()) {
195
+ pwallet->ImportScripts ({GetScriptForDestination (WitnessV0KeyHash (vchAddress))}, 0 /* timestamp */ );
199
196
}
200
- pwallet->LearnAllRelatedScripts (pubkey);
201
197
}
202
198
}
203
199
if (fRescan ) {
@@ -235,42 +231,6 @@ UniValue abortrescan(const JSONRPCRequest& request)
235
231
return true ;
236
232
}
237
233
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
-
274
234
UniValue importaddress (const JSONRPCRequest& request)
275
235
{
276
236
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -341,10 +301,22 @@ UniValue importaddress(const JSONRPCRequest& request)
341
301
if (fP2SH ) {
342
302
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Cannot use the p2sh flag with an address - use a script instead" );
343
303
}
344
- ImportAddress (pwallet, dest, strLabel);
304
+
305
+ pwallet->MarkDirty ();
306
+
307
+ pwallet->ImportScriptPubKeys (strLabel, {GetScriptForDestination (dest)}, false /* have_solving_data */ , true /* apply_label */ , 1 /* timestamp */ );
345
308
} else if (IsHex (request.params [0 ].get_str ())) {
346
309
std::vector<unsigned char > data (ParseHex (request.params [0 ].get_str ()));
347
- ImportScript (pwallet, CScript (data.begin (), data.end ()), strLabel, fP2SH );
310
+ CScript redeem_script (data.begin (), data.end ());
311
+
312
+ std::set<CScript> scripts = {redeem_script};
313
+ pwallet->ImportScripts (scripts, 0 /* timestamp */ );
314
+
315
+ if (fP2SH ) {
316
+ scripts.insert (GetScriptForDestination (ScriptHash (CScriptID (redeem_script))));
317
+ }
318
+
319
+ pwallet->ImportScriptPubKeys (strLabel, scripts, false /* have_solving_data */ , true /* apply_label */ , 1 /* timestamp */ );
348
320
} else {
349
321
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address or script" );
350
322
}
@@ -529,11 +501,16 @@ UniValue importpubkey(const JSONRPCRequest& request)
529
501
auto locked_chain = pwallet->chain ().lock ();
530
502
LOCK (pwallet->cs_wallet );
531
503
504
+ std::set<CScript> script_pub_keys;
532
505
for (const auto & dest : GetAllDestinationsForKey (pubKey)) {
533
- ImportAddress (pwallet, dest, strLabel );
506
+ script_pub_keys. insert ( GetScriptForDestination ( dest) );
534
507
}
535
- ImportScript (pwallet, GetScriptForRawPubKey (pubKey), strLabel, false );
536
- pwallet->LearnAllRelatedScripts (pubKey);
508
+
509
+ pwallet->MarkDirty ();
510
+
511
+ pwallet->ImportScriptPubKeys (strLabel, script_pub_keys, true /* have_solving_data */ , true /* apply_label */ , 1 /* timestamp */ );
512
+
513
+ pwallet->ImportPubKeys ({pubKey.GetID ()}, {{pubKey.GetID (), pubKey}} , {} /* key_origins */ , false /* add_keypool */ , false /* internal */ , 1 /* timestamp */ );
537
514
}
538
515
if (fRescan )
539
516
{
@@ -664,43 +641,38 @@ UniValue importwallet(const JSONRPCRequest& request)
664
641
CPubKey pubkey = key.GetPubKey ();
665
642
assert (key.VerifyPubKey (pubkey));
666
643
CKeyID keyid = pubkey.GetID ();
667
- if (pwallet->HaveKey (keyid)) {
668
- pwallet->WalletLogPrintf (" Skipping import of %s (key already present)\n " , EncodeDestination (PKHash (keyid)));
669
- continue ;
670
- }
644
+
671
645
pwallet->WalletLogPrintf (" Importing %s...\n " , EncodeDestination (PKHash (keyid)));
672
- if (!pwallet->AddKeyPubKey (key, pubkey)) {
646
+
647
+ if (!pwallet->ImportPrivKeys ({{keyid, key}}, time)) {
648
+ pwallet->WalletLogPrintf (" Error importing key for %s\n " , EncodeDestination (PKHash (keyid)));
673
649
fGood = false ;
674
650
continue ;
675
651
}
676
- pwallet-> mapKeyMetadata [keyid]. nCreateTime = time;
652
+
677
653
if (has_label)
678
654
pwallet->SetAddressBook (PKHash (keyid), label, " receive" );
655
+
679
656
nTimeBegin = std::min (nTimeBegin, time);
680
657
progress++;
681
658
}
682
659
for (const auto & script_pair : scripts) {
683
660
pwallet->chain ().showProgress (" " , std::max (50 , std::min (75 , (int )((progress / total) * 100 ) + 50 )), false );
684
661
const CScript& script = script_pair.first ;
685
662
int64_t time = script_pair.second ;
686
- CScriptID id (script);
687
- if (pwallet->HaveCScript (id)) {
688
- pwallet->WalletLogPrintf (" Skipping import of %s (script already present)\n " , HexStr (script));
689
- continue ;
690
- }
691
- if (!pwallet->AddCScript (script)) {
663
+
664
+ if (!pwallet->ImportScripts ({script}, time)) {
692
665
pwallet->WalletLogPrintf (" Error importing script %s\n " , HexStr (script));
693
666
fGood = false ;
694
667
continue ;
695
668
}
696
669
if (time > 0 ) {
697
- pwallet->m_script_metadata [id].nCreateTime = time;
698
670
nTimeBegin = std::min (nTimeBegin, time);
699
671
}
672
+
700
673
progress++;
701
674
}
702
675
pwallet->chain ().showProgress (" " , 100 , false ); // hide progress dialog in GUI
703
- pwallet->UpdateTimeFirstKey (nTimeBegin);
704
676
}
705
677
pwallet->chain ().showProgress (" " , 100 , false ); // hide progress dialog in GUI
706
678
RescanWallet (*pwallet, reserver, nTimeBegin, false /* update */ );
@@ -1255,7 +1227,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
1255
1227
1256
1228
// All good, time to import
1257
1229
pwallet->MarkDirty ();
1258
- if (!pwallet->ImportScripts (import_data.import_scripts )) {
1230
+ if (!pwallet->ImportScripts (import_data.import_scripts , timestamp )) {
1259
1231
throw JSONRPCError (RPC_WALLET_ERROR, " Error adding script to wallet" );
1260
1232
}
1261
1233
if (!pwallet->ImportPrivKeys (privkey_map, timestamp)) {
@@ -1264,7 +1236,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
1264
1236
if (!pwallet->ImportPubKeys (ordered_pubkeys, pubkey_map, import_data.key_origins , add_keypool, internal, timestamp)) {
1265
1237
throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
1266
1238
}
1267
- if (!pwallet->ImportScriptPubKeys (label, script_pub_keys, have_solving_data, internal, timestamp)) {
1239
+ if (!pwallet->ImportScriptPubKeys (label, script_pub_keys, have_solving_data, ! internal, timestamp)) {
1268
1240
throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
1269
1241
}
1270
1242
0 commit comments