@@ -235,42 +235,6 @@ UniValue abortrescan(const JSONRPCRequest& request)
235
235
return true ;
236
236
}
237
237
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
238
UniValue importaddress (const JSONRPCRequest& request)
275
239
{
276
240
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -343,10 +307,22 @@ UniValue importaddress(const JSONRPCRequest& request)
343
307
if (fP2SH ) {
344
308
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Cannot use the p2sh flag with an address - use a script instead" );
345
309
}
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 */ );
347
314
} else if (IsHex (request.params [0 ].get_str ())) {
348
315
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 */ );
350
326
} else {
351
327
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address or script" );
352
328
}
0 commit comments