Skip to content

Commit 9e1551b

Browse files
committed
Test pubkey import to keypool
1 parent 513719c commit 9e1551b

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

test/functional/wallet_importmulti.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ def run_test(self):
625625
ismine=False,
626626
iswatchonly=False)
627627

628-
629628
# Import pubkeys with key origin info
630629
self.log.info("Addresses should have hd keypath and master key id after import with key origin")
631630
pub_addr = self.nodes[1].getnewaddress()
@@ -691,5 +690,92 @@ def run_test(self):
691690
assert 'hdmasterfingerprint' not in pub_import_info
692691
assert 'hdkeypath' not in pub_import_info
693692

693+
# Import some public keys to the keypool of a no privkey wallet
694+
self.log.info("Adding pubkey to keypool of disableprivkey wallet")
695+
self.nodes[1].createwallet(wallet_name="noprivkeys", disable_private_keys=True)
696+
wrpc = self.nodes[1].get_wallet_rpc("noprivkeys")
697+
698+
addr1 = self.nodes[0].getnewaddress()
699+
addr2 = self.nodes[0].getnewaddress()
700+
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey']
701+
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey']
702+
result = wrpc.importmulti(
703+
[{
704+
'desc': 'wpkh(' + pub1 + ')',
705+
'keypool': True,
706+
"timestamp": "now",
707+
},
708+
{
709+
'desc': 'wpkh(' + pub2 + ')',
710+
'keypool': True,
711+
"timestamp": "now",
712+
}]
713+
)
714+
assert result[0]['success']
715+
assert result[1]['success']
716+
assert_equal(wrpc.getwalletinfo()["keypoolsize"], 2)
717+
newaddr1 = wrpc.getnewaddress()
718+
assert_equal(addr1, newaddr1)
719+
newaddr2 = wrpc.getnewaddress()
720+
assert_equal(addr2, newaddr2)
721+
722+
# Import some public keys to the internal keypool of a no privkey wallet
723+
self.log.info("Adding pubkey to internal keypool of disableprivkey wallet")
724+
addr1 = self.nodes[0].getnewaddress()
725+
addr2 = self.nodes[0].getnewaddress()
726+
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey']
727+
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey']
728+
result = wrpc.importmulti(
729+
[{
730+
'desc': 'wpkh(' + pub1 + ')',
731+
'keypool': True,
732+
'internal': True,
733+
"timestamp": "now",
734+
},
735+
{
736+
'desc': 'wpkh(' + pub2 + ')',
737+
'keypool': True,
738+
'internal': True,
739+
"timestamp": "now",
740+
}]
741+
)
742+
assert result[0]['success']
743+
assert result[1]['success']
744+
assert_equal(wrpc.getwalletinfo()["keypoolsize_hd_internal"], 2)
745+
newaddr1 = wrpc.getrawchangeaddress()
746+
assert_equal(addr1, newaddr1)
747+
newaddr2 = wrpc.getrawchangeaddress()
748+
assert_equal(addr2, newaddr2)
749+
750+
# Import a multisig and make sure the keys don't go into the keypool
751+
self.log.info('Imported scripts with pubkeys shoud not have their pubkeys go into the keypool')
752+
addr1 = self.nodes[0].getnewaddress()
753+
addr2 = self.nodes[0].getnewaddress()
754+
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey']
755+
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey']
756+
result = wrpc.importmulti(
757+
[{
758+
'desc': 'wsh(multi(2,' + pub1 + ',' + pub2 + '))',
759+
'keypool': True,
760+
"timestamp": "now",
761+
}]
762+
)
763+
assert result[0]['success']
764+
assert_equal(wrpc.getwalletinfo()["keypoolsize"], 0)
765+
766+
# Cannot import those pubkeys to keypool of wallet with privkeys
767+
self.log.info("Pubkeys cannot be added to the keypool of a wallet with private keys")
768+
wrpc = self.nodes[1].get_wallet_rpc("")
769+
assert wrpc.getwalletinfo()['private_keys_enabled']
770+
result = wrpc.importmulti(
771+
[{
772+
'desc': 'wpkh(' + pub1 + ')',
773+
'keypool': True,
774+
"timestamp": "now",
775+
}]
776+
)
777+
assert_equal(result[0]['error']['code'], -8)
778+
assert_equal(result[0]['error']['message'], "Keys can only be imported to the keypool when private keys are disabled")
779+
694780
if __name__ == '__main__':
695781
ImportMultiTest().main()

0 commit comments

Comments
 (0)