Skip to content

Commit ef0c730

Browse files
committed
Add scripts to importwallet RPC
1 parent b702ae8 commit ef0c730

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

src/wallet/rpcdump.cpp

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -500,40 +500,52 @@ UniValue importwallet(const JSONRPCRequest& request)
500500
if (vstr.size() < 2)
501501
continue;
502502
CBitcoinSecret vchSecret;
503-
if (!vchSecret.SetString(vstr[0]))
504-
continue;
505-
CKey key = vchSecret.GetKey();
506-
CPubKey pubkey = key.GetPubKey();
507-
assert(key.VerifyPubKey(pubkey));
508-
CKeyID keyid = pubkey.GetID();
509-
if (pwallet->HaveKey(keyid)) {
510-
LogPrintf("Skipping import of %s (key already present)\n", EncodeDestination(keyid));
511-
continue;
512-
}
513-
int64_t nTime = DecodeDumpTime(vstr[1]);
514-
std::string strLabel;
515-
bool fLabel = true;
516-
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
517-
if (boost::algorithm::starts_with(vstr[nStr], "#"))
518-
break;
519-
if (vstr[nStr] == "change=1")
520-
fLabel = false;
521-
if (vstr[nStr] == "reserve=1")
522-
fLabel = false;
523-
if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
524-
strLabel = DecodeDumpString(vstr[nStr].substr(6));
525-
fLabel = true;
503+
if (vchSecret.SetString(vstr[0])) {
504+
CKey key = vchSecret.GetKey();
505+
CPubKey pubkey = key.GetPubKey();
506+
assert(key.VerifyPubKey(pubkey));
507+
CKeyID keyid = pubkey.GetID();
508+
if (pwallet->HaveKey(keyid)) {
509+
LogPrintf("Skipping import of %s (key already present)\n", EncodeDestination(keyid));
510+
continue;
526511
}
512+
int64_t nTime = DecodeDumpTime(vstr[1]);
513+
std::string strLabel;
514+
bool fLabel = true;
515+
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
516+
if (boost::algorithm::starts_with(vstr[nStr], "#"))
517+
break;
518+
if (vstr[nStr] == "change=1")
519+
fLabel = false;
520+
if (vstr[nStr] == "reserve=1")
521+
fLabel = false;
522+
if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
523+
strLabel = DecodeDumpString(vstr[nStr].substr(6));
524+
fLabel = true;
525+
}
526+
}
527+
LogPrintf("Importing %s...\n", EncodeDestination(keyid));
528+
if (!pwallet->AddKeyPubKey(key, pubkey)) {
529+
fGood = false;
530+
continue;
531+
}
532+
pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
533+
if (fLabel)
534+
pwallet->SetAddressBook(keyid, strLabel, "receive");
535+
nTimeBegin = std::min(nTimeBegin, nTime);
536+
} else if(IsHex(vstr[0])) {
537+
std::vector<unsigned char> vData(ParseHex(vstr[0]));
538+
CScript script = CScript(vData.begin(), vData.end());
539+
if (pwallet->HaveCScript(script)) {
540+
LogPrintf("Skipping import of %s (script already present)\n", vstr[0]);
541+
continue;
542+
}
543+
if(!pwallet->AddCScript(script)) {
544+
LogPrintf("Error importing script %s\n", vstr[0]);
545+
fGood = false;
546+
continue;
547+
}
527548
}
528-
LogPrintf("Importing %s...\n", EncodeDestination(keyid));
529-
if (!pwallet->AddKeyPubKey(key, pubkey)) {
530-
fGood = false;
531-
continue;
532-
}
533-
pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
534-
if (fLabel)
535-
pwallet->SetAddressBook(keyid, strLabel, "receive");
536-
nTimeBegin = std::min(nTimeBegin, nTime);
537549
}
538550
file.close();
539551
pwallet->ShowProgress("", 100); // hide progress dialog in GUI
@@ -542,7 +554,7 @@ UniValue importwallet(const JSONRPCRequest& request)
542554
pwallet->MarkDirty();
543555

544556
if (!fGood)
545-
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
557+
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys/scripts to wallet");
546558

547559
return NullUniValue;
548560
}

0 commit comments

Comments
 (0)