Skip to content

Commit 7bcf2cb

Browse files
committed
Import pubkeys when importing p2sh with importmulti
1 parent 427253c commit 7bcf2cb

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/wallet/rpcdump.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,53 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
917917
pwallet->SetAddressBook(dest, label, "receive");
918918
}
919919

920+
// Import public keys
921+
for (size_t i = 0; i < pubKeys.size(); ++i) {
922+
const std::string& pubkey = pubKeys[i].get_str();
923+
924+
if (!IsHex(pubkey)) {
925+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
926+
}
927+
928+
std::vector<unsigned char> vData(ParseHex(pubkey));
929+
CPubKey pubKey(vData.begin(), vData.end());
930+
931+
if (!pubKey.IsFullyValid()) {
932+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
933+
}
934+
935+
CTxDestination pubkey_dest = pubKey.GetID();
936+
CScript pubKeyScript = GetScriptForDestination(pubkey_dest);
937+
938+
if (::IsMine(*pwallet, pubKeyScript) == ISMINE_SPENDABLE) {
939+
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
940+
}
941+
942+
pwallet->MarkDirty();
943+
944+
if (!pwallet->AddWatchOnly(pubKeyScript, timestamp)) {
945+
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
946+
}
947+
948+
// add to address book or update label
949+
if (IsValidDestination(pubkey_dest)) {
950+
pwallet->SetAddressBook(pubkey_dest, label, "receive");
951+
}
952+
953+
// TODO Is this necessary?
954+
CScript scriptRawPubKey = GetScriptForRawPubKey(pubKey);
955+
956+
if (::IsMine(*pwallet, scriptRawPubKey) == ISMINE_SPENDABLE) {
957+
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
958+
}
959+
960+
pwallet->MarkDirty();
961+
962+
if (!pwallet->AddWatchOnly(scriptRawPubKey, timestamp)) {
963+
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
964+
}
965+
}
966+
920967
// Import private keys.
921968
if (keys.size()) {
922969
for (size_t i = 0; i < keys.size(); i++) {

test/functional/wallet_importmulti.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ def run_test (self):
446446
"timestamp": "",
447447
}])
448448

449+
# Importing multiple public keys with 2-of-2 p2sh multisig
450+
self.log.info("Pubkeys should be available after importing 2-of-2 p2sh multisig with pubkeys")
451+
addr1 = self.nodes[1].getnewaddress()
452+
addr2 = self.nodes[1].getnewaddress()
453+
pub1 = self.nodes[1].getaddressinfo(addr1)['pubkey']
454+
pub2 = self.nodes[1].getaddressinfo(addr2)['pubkey']
455+
ms = self.nodes[1].createmultisig(2, [pub1, pub2])
456+
result = self.nodes[0].importmulti(
457+
[{
458+
'scriptPubKey' : { 'address' : ms['address']},
459+
'redeemscript' : ms['redeemScript'],
460+
'pubkeys' : [pub1, pub2],
461+
"timestamp": "now",
462+
}]
463+
)
464+
assert result[0]['success']
465+
import_pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey']
466+
assert_equal(pub1, import_pub1)
467+
import_pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey']
468+
assert_equal(pub2, import_pub2)
469+
449470

450471
if __name__ == '__main__':
451472
ImportMultiTest ().main ()

0 commit comments

Comments
 (0)