Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_importmulti.py
Show All 31 Lines | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
def setup_network(self, split=False): | def setup_network(self, split=False): | ||||
self.setup_nodes() | self.setup_nodes() | ||||
def test_importmulti(self, req, success, error_code=None, | def test_importmulti( | ||||
error_message=None, warnings=None): | self, req, success, error_code=None, error_message=None, warnings=None | ||||
): | |||||
"""Run importmulti and assert success""" | """Run importmulti and assert success""" | ||||
if warnings is None: | if warnings is None: | ||||
warnings = [] | warnings = [] | ||||
result = self.nodes[1].importmulti([req]) | result = self.nodes[1].importmulti([req]) | ||||
observed_warnings = [] | observed_warnings = [] | ||||
if 'warnings' in result[0]: | if "warnings" in result[0]: | ||||
observed_warnings = result[0]['warnings'] | observed_warnings = result[0]["warnings"] | ||||
assert_equal( | assert_equal("\n".join(sorted(warnings)), "\n".join(sorted(observed_warnings))) | ||||
"\n".join( | assert_equal(result[0]["success"], success) | ||||
sorted(warnings)), "\n".join( | |||||
sorted(observed_warnings))) | |||||
assert_equal(result[0]['success'], success) | |||||
if error_code is not None: | if error_code is not None: | ||||
assert_equal(result[0]['error']['code'], error_code) | assert_equal(result[0]["error"]["code"], error_code) | ||||
assert_equal(result[0]['error']['message'], error_message) | assert_equal(result[0]["error"]["message"], error_message) | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info("Mining blocks...") | self.log.info("Mining blocks...") | ||||
self.generate(self.nodes[0], 1, sync_fun=self.no_op) | self.generate(self.nodes[0], 1, sync_fun=self.no_op) | ||||
self.generate(self.nodes[1], 1, sync_fun=self.no_op) | self.generate(self.nodes[1], 1, sync_fun=self.no_op) | ||||
timestamp = self.nodes[1].getblock( | timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())[ | ||||
self.nodes[1].getbestblockhash())['mediantime'] | "mediantime" | ||||
] | |||||
# Sync the timestamp to the wallet, so that importmulti works | # Sync the timestamp to the wallet, so that importmulti works | ||||
self.nodes[1].syncwithvalidationinterfacequeue() | self.nodes[1].syncwithvalidationinterfacequeue() | ||||
node0_address1 = self.nodes[0].getaddressinfo( | node0_address1 = self.nodes[0].getaddressinfo(self.nodes[0].getnewaddress()) | ||||
self.nodes[0].getnewaddress()) | |||||
# Check only one address | # Check only one address | ||||
assert_equal(node0_address1['ismine'], True) | assert_equal(node0_address1["ismine"], True) | ||||
# Node 1 sync test | # Node 1 sync test | ||||
assert_equal(self.nodes[1].getblockcount(), 1) | assert_equal(self.nodes[1].getblockcount(), 1) | ||||
# Address Test - before import | # Address Test - before import | ||||
address_info = self.nodes[1].getaddressinfo(node0_address1['address']) | address_info = self.nodes[1].getaddressinfo(node0_address1["address"]) | ||||
assert_equal(address_info['iswatchonly'], False) | assert_equal(address_info["iswatchonly"], False) | ||||
assert_equal(address_info['ismine'], False) | assert_equal(address_info["ismine"], False) | ||||
# RPC importmulti ----------------------------------------------- | # RPC importmulti ----------------------------------------------- | ||||
# Bitcoin Address (implicit non-internal) | # Bitcoin Address (implicit non-internal) | ||||
self.log.info("Should import an address") | self.log.info("Should import an address") | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
"timestamp": "now"}, | {"scriptPubKey": {"address": key.p2pkh_addr}, "timestamp": "now"}, | ||||
success=True) | success=True, | ||||
test_address(self.nodes[1], | ) | ||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
ischange=False) | ischange=False, | ||||
) | |||||
watchonly_address = key.p2pkh_addr | watchonly_address = key.p2pkh_addr | ||||
watchonly_timestamp = timestamp | watchonly_timestamp = timestamp | ||||
self.log.info("Should not import an invalid address") | self.log.info("Should not import an invalid address") | ||||
self.test_importmulti({"scriptPubKey": {"address": "not valid address"}, | self.test_importmulti( | ||||
"timestamp": "now"}, | {"scriptPubKey": {"address": "not valid address"}, "timestamp": "now"}, | ||||
success=False, | success=False, | ||||
error_code=-5, | error_code=-5, | ||||
error_message='Invalid address \"not valid address\"') | error_message='Invalid address "not valid address"', | ||||
) | |||||
# ScriptPubKey + internal | # ScriptPubKey + internal | ||||
self.log.info("Should import a scriptPubKey with internal flag") | self.log.info("Should import a scriptPubKey with internal flag") | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
"timestamp": "now", | {"scriptPubKey": key.p2pkh_script, "timestamp": "now", "internal": True}, | ||||
"internal": True}, | success=True, | ||||
success=True) | ) | ||||
test_address(self.nodes[1], | test_address( | ||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
ischange=True) | ischange=True, | ||||
) | |||||
# ScriptPubKey + internal + label | # ScriptPubKey + internal + label | ||||
self.log.info( | self.log.info("Should not allow a label to be specified when internal is true") | ||||
"Should not allow a label to be specified when internal is true") | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": key.p2pkh_script, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"internal": True, | "internal": True, | ||||
"label": "Unsuccessful labelling for internal addresses"}, | "label": "Unsuccessful labelling for internal addresses", | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Internal addresses should not have a label') | error_message="Internal addresses should not have a label", | ||||
) | |||||
# Nonstandard scriptPubKey + !internal | # Nonstandard scriptPubKey + !internal | ||||
self.log.info( | self.log.info( | ||||
"Should not import a nonstandard scriptPubKey without internal flag") | "Should not import a nonstandard scriptPubKey without internal flag" | ||||
) | |||||
nonstandardScriptPubKey = key.p2pkh_script + CScript([OP_NOP]).hex() | nonstandardScriptPubKey = key.p2pkh_script + CScript([OP_NOP]).hex() | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, | self.test_importmulti( | ||||
"timestamp": "now"}, | {"scriptPubKey": nonstandardScriptPubKey, "timestamp": "now"}, | ||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') | error_message=( | ||||
test_address(self.nodes[1], | "Internal must be set to true for nonstandard scriptPubKey imports." | ||||
), | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=False, | ismine=False, | ||||
timestamp=None) | timestamp=None, | ||||
) | |||||
# Address + Public key + !Internal(explicit) | # Address + Public key + !Internal(explicit) | ||||
self.log.info("Should import an address with public key") | self.log.info("Should import an address with public key") | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"pubkeys": [key.pubkey], | "pubkeys": [key.pubkey], | ||||
"internal": False}, | "internal": False, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | "Some private keys are missing, outputs will be considered watchonly." | ||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# ScriptPubKey + Public key + internal | # ScriptPubKey + Public key + internal | ||||
self.log.info( | self.log.info("Should import a scriptPubKey with internal and with public key") | ||||
"Should import a scriptPubKey with internal and with public key") | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": key.p2pkh_script, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"pubkeys": [key.pubkey], | "pubkeys": [key.pubkey], | ||||
"internal": True}, | "internal": True, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | "Some private keys are missing, outputs will be considered watchonly." | ||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# Nonstandard scriptPubKey + Public key + !internal | # Nonstandard scriptPubKey + Public key + !internal | ||||
self.log.info( | self.log.info( | ||||
"Should not import a nonstandard scriptPubKey without internal and with public key") | "Should not import a nonstandard scriptPubKey without internal and with" | ||||
" public key" | |||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": nonstandardScriptPubKey, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"pubkeys": [key.pubkey]}, | "pubkeys": [key.pubkey], | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') | error_message=( | ||||
test_address(self.nodes[1], | "Internal must be set to true for nonstandard scriptPubKey imports." | ||||
), | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=False, | ismine=False, | ||||
timestamp=None) | timestamp=None, | ||||
) | |||||
# Address + Private key + !watchonly | # Address + Private key + !watchonly | ||||
self.log.info("Should import an address with private key") | self.log.info("Should import an address with private key") | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [key.privkey]}, | "keys": [key.privkey], | ||||
success=True) | }, | ||||
test_address(self.nodes[1], | success=True, | ||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=True, | ismine=True, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
self.log.info( | self.log.info( | ||||
"Should not import an address with private key if is already imported") | "Should not import an address with private key if is already imported" | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | ) | ||||
self.test_importmulti( | |||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [key.privkey]}, | "keys": [key.privkey], | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-4, | error_code=-4, | ||||
error_message=f"The wallet already contains the private key for this address or script (\"{key.p2pkh_script}\")") | error_message=( | ||||
"The wallet already contains the private key for this address or" | |||||
f' script ("{key.p2pkh_script}")' | |||||
), | |||||
) | |||||
# Address + Private key + watchonly | # Address + Private key + watchonly | ||||
self.log.info( | self.log.info("Should import an address with private key and with watchonly") | ||||
"Should import an address with private key and with watchonly") | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [key.privkey], | "keys": [key.privkey], | ||||
"watchonly": True}, | "watchonly": True, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | "All private keys are provided, outputs will be considered spendable." | ||||
" If this is intentional, do not specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=True, | ismine=True, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# ScriptPubKey + Private key + internal | # ScriptPubKey + Private key + internal | ||||
self.log.info( | self.log.info("Should import a scriptPubKey with internal and with private key") | ||||
"Should import a scriptPubKey with internal and with private key") | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": key.p2pkh_script, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [key.privkey], | "keys": [key.privkey], | ||||
"internal": True}, | "internal": True, | ||||
success=True) | }, | ||||
test_address(self.nodes[1], | success=True, | ||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=True, | ismine=True, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# Nonstandard scriptPubKey + Private key + !internal | # Nonstandard scriptPubKey + Private key + !internal | ||||
self.log.info( | self.log.info( | ||||
"Should not import a nonstandard scriptPubKey without internal and with private key") | "Should not import a nonstandard scriptPubKey without internal and with" | ||||
" private key" | |||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": nonstandardScriptPubKey, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [key.privkey]}, | "keys": [key.privkey], | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') | error_message=( | ||||
test_address(self.nodes[1], | "Internal must be set to true for nonstandard scriptPubKey imports." | ||||
), | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=False, | iswatchonly=False, | ||||
ismine=False, | ismine=False, | ||||
timestamp=None) | timestamp=None, | ||||
) | |||||
# P2SH address | # P2SH address | ||||
multisig = get_multisig(self.nodes[0]) | multisig = get_multisig(self.nodes[0]) | ||||
self.generate(self.nodes[1], 100, sync_fun=self.no_op) | self.generate(self.nodes[1], 100, sync_fun=self.no_op) | ||||
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | ||||
self.generate(self.nodes[1], 1, sync_fun=self.no_op) | self.generate(self.nodes[1], 1, sync_fun=self.no_op) | ||||
timestamp = self.nodes[1].getblock( | timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())[ | ||||
self.nodes[1].getbestblockhash())['mediantime'] | "mediantime" | ||||
] | |||||
self.nodes[1].syncwithvalidationinterfacequeue() | self.nodes[1].syncwithvalidationinterfacequeue() | ||||
self.log.info("Should import a p2sh") | self.log.info("Should import a p2sh") | ||||
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, | self.test_importmulti( | ||||
"timestamp": "now"}, | {"scriptPubKey": {"address": multisig.p2sh_addr}, "timestamp": "now"}, | ||||
success=True) | success=True, | ||||
test_address(self.nodes[1], | ) | ||||
test_address( | |||||
self.nodes[1], | |||||
multisig.p2sh_addr, | multisig.p2sh_addr, | ||||
isscript=True, | isscript=True, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
p2shunspent = self.nodes[1].listunspent( | ) | ||||
0, 999999, [multisig.p2sh_addr])[0] | p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] | ||||
assert_equal(p2shunspent['spendable'], False) | assert_equal(p2shunspent["spendable"], False) | ||||
assert_equal(p2shunspent['solvable'], False) | assert_equal(p2shunspent["solvable"], False) | ||||
# P2SH + Redeem script | # P2SH + Redeem script | ||||
multisig = get_multisig(self.nodes[0]) | multisig = get_multisig(self.nodes[0]) | ||||
self.generate(self.nodes[1], 100, sync_fun=self.no_op) | self.generate(self.nodes[1], 100, sync_fun=self.no_op) | ||||
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | ||||
self.generate(self.nodes[1], 1, sync_fun=self.no_op) | self.generate(self.nodes[1], 1, sync_fun=self.no_op) | ||||
timestamp = self.nodes[1].getblock( | timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())[ | ||||
self.nodes[1].getbestblockhash())['mediantime'] | "mediantime" | ||||
] | |||||
self.nodes[1].syncwithvalidationinterfacequeue() | self.nodes[1].syncwithvalidationinterfacequeue() | ||||
self.log.info("Should import a p2sh with respective redeem script") | self.log.info("Should import a p2sh with respective redeem script") | ||||
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": multisig.p2sh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"redeemscript": multisig.redeem_script}, | "redeemscript": multisig.redeem_script, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
"Some private keys are missing, outputs will be considered watchonly." | |||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | test_address( | ||||
self.nodes[1], | self.nodes[1], | ||||
multisig.p2sh_addr, | multisig.p2sh_addr, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=True) | solvable=True, | ||||
) | |||||
p2shunspent = self.nodes[1].listunspent( | p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] | ||||
0, 999999, [multisig.p2sh_addr])[0] | assert_equal(p2shunspent["spendable"], False) | ||||
assert_equal(p2shunspent['spendable'], False) | assert_equal(p2shunspent["solvable"], True) | ||||
assert_equal(p2shunspent['solvable'], True) | |||||
# P2SH + Redeem script + Private Keys + !Watchonly | # P2SH + Redeem script + Private Keys + !Watchonly | ||||
multisig = get_multisig(self.nodes[0]) | multisig = get_multisig(self.nodes[0]) | ||||
self.generate(self.nodes[1], 100, sync_fun=self.no_op) | self.generate(self.nodes[1], 100, sync_fun=self.no_op) | ||||
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | ||||
self.generate(self.nodes[1], 1, sync_fun=self.no_op) | self.generate(self.nodes[1], 1, sync_fun=self.no_op) | ||||
timestamp = self.nodes[1].getblock( | timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())[ | ||||
self.nodes[1].getbestblockhash())['mediantime'] | "mediantime" | ||||
] | |||||
self.nodes[1].syncwithvalidationinterfacequeue() | self.nodes[1].syncwithvalidationinterfacequeue() | ||||
self.log.info( | self.log.info( | ||||
"Should import a p2sh with respective redeem script and private keys") | "Should import a p2sh with respective redeem script and private keys" | ||||
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, | ) | ||||
self.test_importmulti( | |||||
{ | |||||
"scriptPubKey": {"address": multisig.p2sh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"redeemscript": multisig.redeem_script, | "redeemscript": multisig.redeem_script, | ||||
"keys": multisig.privkeys[0:2]}, | "keys": multisig.privkeys[0:2], | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | "Some private keys are missing, outputs will be considered watchonly." | ||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
multisig.p2sh_addr, | multisig.p2sh_addr, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
ismine=False, | ismine=False, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
solvable=True) | solvable=True, | ||||
) | |||||
p2shunspent = self.nodes[1].listunspent( | p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] | ||||
0, 999999, [multisig.p2sh_addr])[0] | assert_equal(p2shunspent["spendable"], False) | ||||
assert_equal(p2shunspent['spendable'], False) | assert_equal(p2shunspent["solvable"], True) | ||||
assert_equal(p2shunspent['solvable'], True) | |||||
# P2SH + Redeem script + Private Keys + Watchonly | # P2SH + Redeem script + Private Keys + Watchonly | ||||
multisig = get_multisig(self.nodes[0]) | multisig = get_multisig(self.nodes[0]) | ||||
self.generate(self.nodes[1], 100, sync_fun=self.no_op) | self.generate(self.nodes[1], 100, sync_fun=self.no_op) | ||||
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) | ||||
self.generate(self.nodes[1], 1, sync_fun=self.no_op) | self.generate(self.nodes[1], 1, sync_fun=self.no_op) | ||||
timestamp = self.nodes[1].getblock( | timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())[ | ||||
self.nodes[1].getbestblockhash())['mediantime'] | "mediantime" | ||||
] | |||||
self.nodes[1].syncwithvalidationinterfacequeue() | self.nodes[1].syncwithvalidationinterfacequeue() | ||||
self.log.info( | self.log.info( | ||||
"Should import a p2sh with respective redeem script and private keys") | "Should import a p2sh with respective redeem script and private keys" | ||||
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, | ) | ||||
self.test_importmulti( | |||||
{ | |||||
"scriptPubKey": {"address": multisig.p2sh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"redeemscript": multisig.redeem_script, | "redeemscript": multisig.redeem_script, | ||||
"keys": multisig.privkeys[0:2], | "keys": multisig.privkeys[0:2], | ||||
"watchonly": True}, | "watchonly": True, | ||||
success=True) | }, | ||||
test_address(self.nodes[1], | success=True, | ||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
multisig.p2sh_addr, | multisig.p2sh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=True, | solvable=True, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# Address + Public key + !Internal + Wrong pubkey | # Address + Public key + !Internal + Wrong pubkey | ||||
self.log.info( | self.log.info( | ||||
"Should not import an address with the wrong public key as non-solvable") | "Should not import an address with the wrong public key as non-solvable" | ||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
wrong_key = get_key(self.nodes[0]).pubkey | wrong_key = get_key(self.nodes[0]).pubkey | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"pubkeys": [wrong_key]}, | "pubkeys": [wrong_key], | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | ( | ||||
"Importing as non-solvable: some required keys are missing. If this" | |||||
" is intentional, don't provide any keys, pubkeys or redeemscript." | |||||
), | |||||
( | |||||
"Some private keys are missing, outputs will be considered" | |||||
" watchonly. If this is intentional, specify the watchonly flag." | |||||
), | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=False, | solvable=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# ScriptPubKey + Public key + internal + Wrong pubkey | # ScriptPubKey + Public key + internal + Wrong pubkey | ||||
self.log.info( | self.log.info( | ||||
"Should import a scriptPubKey with internal and with a wrong public key as non-solvable") | "Should import a scriptPubKey with internal and with a wrong public key as" | ||||
" non-solvable" | |||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
wrong_key = get_key(self.nodes[0]).pubkey | wrong_key = get_key(self.nodes[0]).pubkey | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": key.p2pkh_script, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"pubkeys": [wrong_key], | "pubkeys": [wrong_key], | ||||
"internal": True}, | "internal": True, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | ( | ||||
"Importing as non-solvable: some required keys are missing. If this" | |||||
" is intentional, don't provide any keys, pubkeys or redeemscript." | |||||
), | |||||
( | |||||
"Some private keys are missing, outputs will be considered" | |||||
" watchonly. If this is intentional, specify the watchonly flag." | |||||
), | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=False, | solvable=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# Address + Private key + !watchonly + Wrong private key | # Address + Private key + !watchonly + Wrong private key | ||||
self.log.info( | self.log.info( | ||||
"Should import an address with a wrong private key as non-solvable") | "Should import an address with a wrong private key as non-solvable" | ||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
wrong_privkey = get_key(self.nodes[0]).privkey | wrong_privkey = get_key(self.nodes[0]).privkey | ||||
self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr}, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [wrong_privkey]}, | "keys": [wrong_privkey], | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | ( | ||||
"Importing as non-solvable: some required keys are missing. If this" | |||||
" is intentional, don't provide any keys, pubkeys or redeemscript." | |||||
), | |||||
( | |||||
"Some private keys are missing, outputs will be considered" | |||||
" watchonly. If this is intentional, specify the watchonly flag." | |||||
), | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=False, | solvable=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# ScriptPubKey + Private key + internal + Wrong private key | # ScriptPubKey + Private key + internal + Wrong private key | ||||
self.log.info( | self.log.info( | ||||
"Should import a scriptPubKey with internal and with a wrong private key as non-solvable") | "Should import a scriptPubKey with internal and with a wrong private key as" | ||||
" non-solvable" | |||||
) | |||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
wrong_privkey = get_key(self.nodes[0]).privkey | wrong_privkey = get_key(self.nodes[0]).privkey | ||||
self.test_importmulti({"scriptPubKey": key.p2pkh_script, | self.test_importmulti( | ||||
{ | |||||
"scriptPubKey": key.p2pkh_script, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"keys": [wrong_privkey], | "keys": [wrong_privkey], | ||||
"internal": True}, | "internal": True, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | ( | ||||
"Importing as non-solvable: some required keys are missing. If this" | |||||
" is intentional, don't provide any keys, pubkeys or redeemscript." | |||||
), | |||||
( | |||||
"Some private keys are missing, outputs will be considered" | |||||
" watchonly. If this is intentional, specify the watchonly flag." | |||||
), | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
solvable=False, | solvable=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
# Importing existing watch only address with new timestamp should | # Importing existing watch only address with new timestamp should | ||||
# replace saved timestamp. | # replace saved timestamp. | ||||
assert_greater_than(timestamp, watchonly_timestamp) | assert_greater_than(timestamp, watchonly_timestamp) | ||||
self.log.info("Should replace previously saved watch only timestamp.") | self.log.info("Should replace previously saved watch only timestamp.") | ||||
self.test_importmulti({"scriptPubKey": {"address": watchonly_address}, | self.test_importmulti( | ||||
"timestamp": "now"}, | {"scriptPubKey": {"address": watchonly_address}, "timestamp": "now"}, | ||||
success=True) | success=True, | ||||
test_address(self.nodes[1], | ) | ||||
test_address( | |||||
self.nodes[1], | |||||
watchonly_address, | watchonly_address, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=timestamp) | timestamp=timestamp, | ||||
) | |||||
watchonly_timestamp = timestamp | watchonly_timestamp = timestamp | ||||
# restart nodes to check for proper serialization/deserialization of | # restart nodes to check for proper serialization/deserialization of | ||||
# watch only address | # watch only address | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.start_nodes() | self.start_nodes() | ||||
test_address(self.nodes[1], | test_address( | ||||
self.nodes[1], | |||||
watchonly_address, | watchonly_address, | ||||
iswatchonly=True, | iswatchonly=True, | ||||
ismine=False, | ismine=False, | ||||
timestamp=watchonly_timestamp) | timestamp=watchonly_timestamp, | ||||
) | |||||
# Bad or missing timestamps | # Bad or missing timestamps | ||||
self.log.info("Should throw on invalid or missing timestamp values") | self.log.info("Should throw on invalid or missing timestamp values") | ||||
assert_raises_rpc_error(-3, 'Missing required timestamp field for key', | assert_raises_rpc_error( | ||||
self.nodes[1].importmulti, [{"scriptPubKey": key.p2pkh_script}]) | -3, | ||||
assert_raises_rpc_error(-3, 'Expected number or "now" timestamp value for key. got type string', | "Missing required timestamp field for key", | ||||
self.nodes[1].importmulti, [{ | self.nodes[1].importmulti, | ||||
"scriptPubKey": key.p2pkh_script, | [{"scriptPubKey": key.p2pkh_script}], | ||||
"timestamp": "" | ) | ||||
}]) | assert_raises_rpc_error( | ||||
-3, | |||||
'Expected number or "now" timestamp value for key. got type string', | |||||
self.nodes[1].importmulti, | |||||
[{"scriptPubKey": key.p2pkh_script, "timestamp": ""}], | |||||
) | |||||
# Test that importing of a P2PKH address via descriptor without | # Test that importing of a P2PKH address via descriptor without | ||||
# checksum fails | # checksum fails | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.log.info( | self.log.info( | ||||
"Should fail to import a p2pkh address from descriptor with no checksum") | "Should fail to import a p2pkh address from descriptor with no checksum" | ||||
self.test_importmulti({"desc": f"pkh({key.pubkey})", | ) | ||||
self.test_importmulti( | |||||
{ | |||||
"desc": f"pkh({key.pubkey})", | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"label": "Descriptor import test"}, | "label": "Descriptor import test", | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-5, | error_code=-5, | ||||
error_message='Missing checksum') | error_message="Missing checksum", | ||||
) | |||||
# Test ranged descriptor fails if range is not specified | # Test ranged descriptor fails if range is not specified | ||||
xpriv = "tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg" | xpriv = "tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg" | ||||
# hdkeypath=m/0'/0'/0' and 1' | # hdkeypath=m/0'/0'/0' and 1' | ||||
addresses = [ | addresses = [ | ||||
"ecregtest:prvn9ycvgr5atuyh49sua3mapskh2mnnzg7t9yp6dt", | "ecregtest:prvn9ycvgr5atuyh49sua3mapskh2mnnzg7t9yp6dt", | ||||
"ecregtest:pp3n087yx0njv2e5wcvltahfxqst7l66rutz8ceeat"] | "ecregtest:pp3n087yx0njv2e5wcvltahfxqst7l66rutz8ceeat", | ||||
] | |||||
# pkh subscripts corresponding to the above addresses | # pkh subscripts corresponding to the above addresses | ||||
addresses += [ | addresses += [ | ||||
"ecregtest:qqdkxd2xnzftq2p8wr3sqqyw8lntap7tncs546s6pr", | "ecregtest:qqdkxd2xnzftq2p8wr3sqqyw8lntap7tncs546s6pr", | ||||
"ecregtest:qpyryy83jfaec5u0gpzldk6teadsuq8zlyqh5l30uq", | "ecregtest:qpyryy83jfaec5u0gpzldk6teadsuq8zlyqh5l30uq", | ||||
] | ] | ||||
desc = f"sh(pkh({xpriv}/0'/0'/*'))" | desc = f"sh(pkh({xpriv}/0'/0'/*'))" | ||||
self.log.info( | self.log.info("Ranged descriptor import should fail without a specified range") | ||||
"Ranged descriptor import should fail without a specified range") | self.test_importmulti( | ||||
self.test_importmulti({"desc": descsum_create(desc), | {"desc": descsum_create(desc), "timestamp": "now"}, | ||||
"timestamp": "now"}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Descriptor is ranged, please specify the range') | error_message="Descriptor is ranged, please specify the range", | ||||
) | |||||
# Test importing of a ranged descriptor with xpriv | # Test importing of a ranged descriptor with xpriv | ||||
self.log.info( | self.log.info( | ||||
"Should import the ranged descriptor with specified range as solvable") | "Should import the ranged descriptor with specified range as solvable" | ||||
self.test_importmulti({"desc": descsum_create(desc), | ) | ||||
"timestamp": "now", | self.test_importmulti( | ||||
"range": 1}, | {"desc": descsum_create(desc), "timestamp": "now", "range": 1}, success=True | ||||
success=True) | ) | ||||
for address in addresses: | for address in addresses: | ||||
test_address(self.nodes[1], address, solvable=True, ismine=True) | test_address(self.nodes[1], address, solvable=True, ismine=True) | ||||
self.test_importmulti({"desc": descsum_create(desc), "timestamp": "now", "range": -1}, | self.test_importmulti( | ||||
success=False, error_code=-8, error_message='End of range is too high') | {"desc": descsum_create(desc), "timestamp": "now", "range": -1}, | ||||
success=False, | |||||
error_code=-8, | |||||
error_message="End of range is too high", | |||||
) | |||||
self.test_importmulti({"desc": descsum_create(desc), "timestamp": "now", "range": [-1, 10]}, | self.test_importmulti( | ||||
success=False, error_code=-8, error_message='Range should be greater or equal than 0') | {"desc": descsum_create(desc), "timestamp": "now", "range": [-1, 10]}, | ||||
success=False, | |||||
error_code=-8, | |||||
error_message="Range should be greater or equal than 0", | |||||
) | |||||
self.test_importmulti({"desc": descsum_create(desc), "timestamp": "now", "range": [(2 << 31 + 1) - 1000000, (2 << 31 + 1)]}, | self.test_importmulti( | ||||
success=False, error_code=-8, error_message='End of range is too high') | { | ||||
"desc": descsum_create(desc), | |||||
"timestamp": "now", | |||||
"range": [(2 << 31 + 1) - 1000000, (2 << 31 + 1)], | |||||
}, | |||||
success=False, | |||||
error_code=-8, | |||||
error_message="End of range is too high", | |||||
) | |||||
self.test_importmulti({"desc": descsum_create(desc), "timestamp": "now", "range": [2, 1]}, | self.test_importmulti( | ||||
success=False, error_code=-8, error_message='Range specified as [begin,end] must not have begin after end') | {"desc": descsum_create(desc), "timestamp": "now", "range": [2, 1]}, | ||||
success=False, | |||||
error_code=-8, | |||||
error_message=( | |||||
"Range specified as [begin,end] must not have begin after end" | |||||
), | |||||
) | |||||
self.test_importmulti({"desc": descsum_create(desc), "timestamp": "now", "range": [0, 1000001]}, | self.test_importmulti( | ||||
success=False, error_code=-8, error_message='Range is too large') | {"desc": descsum_create(desc), "timestamp": "now", "range": [0, 1000001]}, | ||||
success=False, | |||||
error_code=-8, | |||||
error_message="Range is too large", | |||||
) | |||||
# Test importing a descriptor containing a WIF private key | # Test importing a descriptor containing a WIF private key | ||||
wif_priv = "cTe1f5rdT8A8DFgVWTjyPwACsDPJM9ff4QngFxUixCSvvbg1x6sh" | wif_priv = "cTe1f5rdT8A8DFgVWTjyPwACsDPJM9ff4QngFxUixCSvvbg1x6sh" | ||||
# Note: in Core's test, this address refers to the sh(wpkh()) address. | # Note: in Core's test, this address refers to the sh(wpkh()) address. | ||||
# For a sh(pkh()) this does not refer to a key, so we use the subscript | # For a sh(pkh()) this does not refer to a key, so we use the subscript | ||||
# address instead, which returns the same privkey. | # address instead, which returns the same privkey. | ||||
address = "ecregtest:qzh6rch6st3wjvp0h2ud87gn7xnxvf6h8yrk8gcg8t" | address = "ecregtest:qzh6rch6st3wjvp0h2ud87gn7xnxvf6h8yrk8gcg8t" | ||||
desc = f"sh(pkh({wif_priv}))" | desc = f"sh(pkh({wif_priv}))" | ||||
self.log.info( | self.log.info("Should import a descriptor with a WIF private key as spendable") | ||||
"Should import a descriptor with a WIF private key as spendable") | self.test_importmulti( | ||||
self.test_importmulti({"desc": descsum_create(desc), | {"desc": descsum_create(desc), "timestamp": "now"}, success=True | ||||
"timestamp": "now"}, | ) | ||||
success=True) | test_address(self.nodes[1], address, solvable=True, ismine=True) | ||||
test_address(self.nodes[1], | |||||
address, | |||||
solvable=True, | |||||
ismine=True) | |||||
# dump the private key to ensure it matches what was imported | # dump the private key to ensure it matches what was imported | ||||
privkey = self.nodes[1].dumpprivkey(address) | privkey = self.nodes[1].dumpprivkey(address) | ||||
assert_equal(privkey, wif_priv) | assert_equal(privkey, wif_priv) | ||||
# Test importing of a P2PKH address via descriptor | # Test importing of a P2PKH address via descriptor | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
p2pkh_label = "P2PKH descriptor import" | p2pkh_label = "P2PKH descriptor import" | ||||
self.log.info("Should import a p2pkh address from descriptor") | self.log.info("Should import a p2pkh address from descriptor") | ||||
self.test_importmulti({"desc": descsum_create(f"pkh({key.pubkey})"), | self.test_importmulti( | ||||
{ | |||||
"desc": descsum_create(f"pkh({key.pubkey})"), | |||||
"timestamp": "now", | "timestamp": "now", | ||||
"label": p2pkh_label}, | "label": p2pkh_label, | ||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
test_address(self.nodes[1], | "Some private keys are missing, outputs will be considered watchonly." | ||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
test_address( | |||||
self.nodes[1], | |||||
key.p2pkh_addr, | key.p2pkh_addr, | ||||
solvable=True, | solvable=True, | ||||
ismine=False, | ismine=False, | ||||
labels=[p2pkh_label]) | labels=[p2pkh_label], | ||||
) | |||||
# Test import fails if both desc and scriptPubKey are provided | # Test import fails if both desc and scriptPubKey are provided | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.log.info( | self.log.info("Import should fail if both scriptPubKey and desc are provided") | ||||
"Import should fail if both scriptPubKey and desc are provided") | self.test_importmulti( | ||||
self.test_importmulti({"desc": descsum_create(f"pkh({key.pubkey})"), | { | ||||
"desc": descsum_create(f"pkh({key.pubkey})"), | |||||
"scriptPubKey": {"address": key.p2pkh_addr}, | "scriptPubKey": {"address": key.p2pkh_addr}, | ||||
"timestamp": "now"}, | "timestamp": "now", | ||||
}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Both a descriptor and a scriptPubKey should not be provided.') | error_message=( | ||||
"Both a descriptor and a scriptPubKey should not be provided." | |||||
), | |||||
) | |||||
# Test import fails if neither desc nor scriptPubKey are present | # Test import fails if neither desc nor scriptPubKey are present | ||||
key = get_key(self.nodes[0]) | key = get_key(self.nodes[0]) | ||||
self.log.info( | self.log.info( | ||||
"Import should fail if neither a descriptor nor a scriptPubKey are provided") | "Import should fail if neither a descriptor nor a scriptPubKey are provided" | ||||
self.test_importmulti({"timestamp": "now"}, | ) | ||||
self.test_importmulti( | |||||
{"timestamp": "now"}, | |||||
success=False, | success=False, | ||||
error_code=-8, | error_code=-8, | ||||
error_message='Either a descriptor or scriptPubKey must be provided.') | error_message="Either a descriptor or scriptPubKey must be provided.", | ||||
) | |||||
# Test importing of a multisig via descriptor | # Test importing of a multisig via descriptor | ||||
key1 = get_key(self.nodes[0]) | key1 = get_key(self.nodes[0]) | ||||
key2 = get_key(self.nodes[0]) | key2 = get_key(self.nodes[0]) | ||||
self.log.info("Should import a 1-of-2 bare multisig from descriptor") | self.log.info("Should import a 1-of-2 bare multisig from descriptor") | ||||
self.test_importmulti({"desc": descsum_create(f"multi(1,{key1.pubkey},{key2.pubkey})"), | self.test_importmulti( | ||||
"timestamp": "now"}, | { | ||||
"desc": descsum_create(f"multi(1,{key1.pubkey},{key2.pubkey})"), | |||||
"timestamp": "now", | |||||
}, | |||||
success=True, | success=True, | ||||
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) | warnings=[ | ||||
"Some private keys are missing, outputs will be considered watchonly." | |||||
" If this is intentional, specify the watchonly flag." | |||||
], | |||||
) | |||||
self.log.info( | self.log.info( | ||||
"Should not treat individual keys from the imported bare multisig as watchonly") | "Should not treat individual keys from the imported bare multisig as" | ||||
test_address(self.nodes[1], | " watchonly" | ||||
key1.p2pkh_addr, | ) | ||||
ismine=False, | test_address(self.nodes[1], key1.p2pkh_addr, ismine=False, iswatchonly=False) | ||||
iswatchonly=False) | |||||
# Import pubkeys with key origin info | # Import pubkeys with key origin info | ||||
self.log.info( | self.log.info( | ||||
"Addresses should have hd keypath and master key id after import with key origin") | "Addresses should have hd keypath and master key id after import with key" | ||||
" origin" | |||||
) | |||||
pub_addr = self.nodes[1].getnewaddress() | pub_addr = self.nodes[1].getnewaddress() | ||||
pub_addr = self.nodes[1].getnewaddress() | pub_addr = self.nodes[1].getnewaddress() | ||||
info = self.nodes[1].getaddressinfo(pub_addr) | info = self.nodes[1].getaddressinfo(pub_addr) | ||||
pub = info['pubkey'] | pub = info["pubkey"] | ||||
pub_keypath = info['hdkeypath'] | pub_keypath = info["hdkeypath"] | ||||
pub_fpr = info['hdmasterfingerprint'] | pub_fpr = info["hdmasterfingerprint"] | ||||
result = self.nodes[0].importmulti( | result = self.nodes[0].importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh([{pub_fpr}{pub_keypath[1:]}]{pub})"), | { | ||||
"desc": descsum_create(f"pkh([{pub_fpr}{pub_keypath[1:]}]{pub})"), | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | } | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
pub_import_info = self.nodes[0].getaddressinfo(pub_addr) | pub_import_info = self.nodes[0].getaddressinfo(pub_addr) | ||||
assert_equal(pub_import_info['hdmasterfingerprint'], pub_fpr) | assert_equal(pub_import_info["hdmasterfingerprint"], pub_fpr) | ||||
assert_equal(pub_import_info['pubkey'], pub) | assert_equal(pub_import_info["pubkey"], pub) | ||||
assert_equal(pub_import_info['hdkeypath'], pub_keypath) | assert_equal(pub_import_info["hdkeypath"], pub_keypath) | ||||
# Import privkeys with key origin info | # Import privkeys with key origin info | ||||
priv_addr = self.nodes[1].getnewaddress() | priv_addr = self.nodes[1].getnewaddress() | ||||
info = self.nodes[1].getaddressinfo(priv_addr) | info = self.nodes[1].getaddressinfo(priv_addr) | ||||
priv = self.nodes[1].dumpprivkey(priv_addr) | priv = self.nodes[1].dumpprivkey(priv_addr) | ||||
priv_keypath = info['hdkeypath'] | priv_keypath = info["hdkeypath"] | ||||
priv_fpr = info['hdmasterfingerprint'] | priv_fpr = info["hdmasterfingerprint"] | ||||
result = self.nodes[0].importmulti( | result = self.nodes[0].importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh([{priv_fpr}{priv_keypath[1:]}]{priv})"), | { | ||||
"desc": descsum_create( | |||||
f"pkh([{priv_fpr}{priv_keypath[1:]}]{priv})" | |||||
), | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | } | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
priv_import_info = self.nodes[0].getaddressinfo(priv_addr) | priv_import_info = self.nodes[0].getaddressinfo(priv_addr) | ||||
assert_equal(priv_import_info['hdmasterfingerprint'], priv_fpr) | assert_equal(priv_import_info["hdmasterfingerprint"], priv_fpr) | ||||
assert_equal(priv_import_info['hdkeypath'], priv_keypath) | assert_equal(priv_import_info["hdkeypath"], priv_keypath) | ||||
# Make sure the key origin info are still there after a restart | # Make sure the key origin info are still there after a restart | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.start_nodes() | self.start_nodes() | ||||
import_info = self.nodes[0].getaddressinfo(pub_addr) | import_info = self.nodes[0].getaddressinfo(pub_addr) | ||||
assert_equal(import_info['hdmasterfingerprint'], pub_fpr) | assert_equal(import_info["hdmasterfingerprint"], pub_fpr) | ||||
assert_equal(import_info['hdkeypath'], pub_keypath) | assert_equal(import_info["hdkeypath"], pub_keypath) | ||||
import_info = self.nodes[0].getaddressinfo(priv_addr) | import_info = self.nodes[0].getaddressinfo(priv_addr) | ||||
assert_equal(import_info['hdmasterfingerprint'], priv_fpr) | assert_equal(import_info["hdmasterfingerprint"], priv_fpr) | ||||
assert_equal(import_info['hdkeypath'], priv_keypath) | assert_equal(import_info["hdkeypath"], priv_keypath) | ||||
# Check legacy import does not import key origin info | # Check legacy import does not import key origin info | ||||
self.log.info("Legacy imports don't have key origin info") | self.log.info("Legacy imports don't have key origin info") | ||||
pub_addr = self.nodes[1].getnewaddress() | pub_addr = self.nodes[1].getnewaddress() | ||||
info = self.nodes[1].getaddressinfo(pub_addr) | info = self.nodes[1].getaddressinfo(pub_addr) | ||||
pub = info['pubkey'] | pub = info["pubkey"] | ||||
result = self.nodes[0].importmulti( | result = self.nodes[0].importmulti( | ||||
[{ | [ | ||||
'scriptPubKey': {'address': pub_addr}, | { | ||||
'pubkeys': [pub], | "scriptPubKey": {"address": pub_addr}, | ||||
"pubkeys": [pub], | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | } | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
pub_import_info = self.nodes[0].getaddressinfo(pub_addr) | pub_import_info = self.nodes[0].getaddressinfo(pub_addr) | ||||
assert_equal(pub_import_info['pubkey'], pub) | assert_equal(pub_import_info["pubkey"], pub) | ||||
assert 'hdmasterfingerprint' not in pub_import_info | assert "hdmasterfingerprint" not in pub_import_info | ||||
assert 'hdkeypath' not in pub_import_info | assert "hdkeypath" not in pub_import_info | ||||
# Import some public keys to the keypool of a no privkey wallet | # Import some public keys to the keypool of a no privkey wallet | ||||
self.log.info("Adding pubkey to keypool of disableprivkey wallet") | self.log.info("Adding pubkey to keypool of disableprivkey wallet") | ||||
self.nodes[1].createwallet( | self.nodes[1].createwallet(wallet_name="noprivkeys", disable_private_keys=True) | ||||
wallet_name="noprivkeys", | |||||
disable_private_keys=True) | |||||
wrpc = self.nodes[1].get_wallet_rpc("noprivkeys") | wrpc = self.nodes[1].get_wallet_rpc("noprivkeys") | ||||
addr1 = self.nodes[0].getnewaddress() | addr1 = self.nodes[0].getnewaddress() | ||||
addr2 = self.nodes[0].getnewaddress() | addr2 = self.nodes[0].getnewaddress() | ||||
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey'] | pub1 = self.nodes[0].getaddressinfo(addr1)["pubkey"] | ||||
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey'] | pub2 = self.nodes[0].getaddressinfo(addr2)["pubkey"] | ||||
result = wrpc.importmulti( | result = wrpc.importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh({pub1})"), | { | ||||
'keypool': True, | "desc": descsum_create(f"pkh({pub1})"), | ||||
"keypool": True, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}, | }, | ||||
{ | { | ||||
'desc': descsum_create(f"pkh({pub2})"), | "desc": descsum_create(f"pkh({pub2})"), | ||||
'keypool': True, | "keypool": True, | ||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | }, | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
assert result[1]['success'] | assert result[1]["success"] | ||||
assert_equal(wrpc.getwalletinfo()["keypoolsize"], 2) | assert_equal(wrpc.getwalletinfo()["keypoolsize"], 2) | ||||
newaddr1 = wrpc.getnewaddress() | newaddr1 = wrpc.getnewaddress() | ||||
assert_equal(addr1, newaddr1) | assert_equal(addr1, newaddr1) | ||||
newaddr2 = wrpc.getnewaddress() | newaddr2 = wrpc.getnewaddress() | ||||
assert_equal(addr2, newaddr2) | assert_equal(addr2, newaddr2) | ||||
# Import some public keys to the internal keypool of a no privkey | # Import some public keys to the internal keypool of a no privkey | ||||
# wallet | # wallet | ||||
self.log.info( | self.log.info("Adding pubkey to internal keypool of disableprivkey wallet") | ||||
"Adding pubkey to internal keypool of disableprivkey wallet") | |||||
addr1 = self.nodes[0].getnewaddress() | addr1 = self.nodes[0].getnewaddress() | ||||
addr2 = self.nodes[0].getnewaddress() | addr2 = self.nodes[0].getnewaddress() | ||||
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey'] | pub1 = self.nodes[0].getaddressinfo(addr1)["pubkey"] | ||||
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey'] | pub2 = self.nodes[0].getaddressinfo(addr2)["pubkey"] | ||||
result = wrpc.importmulti( | result = wrpc.importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh({pub1})"), | { | ||||
'keypool': True, | "desc": descsum_create(f"pkh({pub1})"), | ||||
'internal': True, | "keypool": True, | ||||
"internal": True, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}, | }, | ||||
{ | { | ||||
'desc': descsum_create(f"pkh({pub2})"), | "desc": descsum_create(f"pkh({pub2})"), | ||||
'keypool': True, | "keypool": True, | ||||
'internal': True, | "internal": True, | ||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | }, | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
assert result[1]['success'] | assert result[1]["success"] | ||||
assert_equal(wrpc.getwalletinfo()["keypoolsize_hd_internal"], 2) | assert_equal(wrpc.getwalletinfo()["keypoolsize_hd_internal"], 2) | ||||
newaddr1 = wrpc.getrawchangeaddress() | newaddr1 = wrpc.getrawchangeaddress() | ||||
assert_equal(addr1, newaddr1) | assert_equal(addr1, newaddr1) | ||||
newaddr2 = wrpc.getrawchangeaddress() | newaddr2 = wrpc.getrawchangeaddress() | ||||
assert_equal(addr2, newaddr2) | assert_equal(addr2, newaddr2) | ||||
# Import a multisig and make sure the keys don't go into the keypool | # Import a multisig and make sure the keys don't go into the keypool | ||||
self.log.info( | self.log.info( | ||||
'Imported scripts with pubkeys shoud not have their pubkeys go into the keypool') | "Imported scripts with pubkeys shoud not have their pubkeys go into the" | ||||
" keypool" | |||||
) | |||||
addr1 = self.nodes[0].getnewaddress() | addr1 = self.nodes[0].getnewaddress() | ||||
addr2 = self.nodes[0].getnewaddress() | addr2 = self.nodes[0].getnewaddress() | ||||
pub1 = self.nodes[0].getaddressinfo(addr1)['pubkey'] | pub1 = self.nodes[0].getaddressinfo(addr1)["pubkey"] | ||||
pub2 = self.nodes[0].getaddressinfo(addr2)['pubkey'] | pub2 = self.nodes[0].getaddressinfo(addr2)["pubkey"] | ||||
result = wrpc.importmulti( | result = wrpc.importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"sh(multi(2,{pub1},{pub2}))"), | { | ||||
'keypool': True, | "desc": descsum_create(f"sh(multi(2,{pub1},{pub2}))"), | ||||
"keypool": True, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | } | ||||
] | |||||
) | ) | ||||
assert result[0]['success'] | assert result[0]["success"] | ||||
assert_equal(wrpc.getwalletinfo()["keypoolsize"], 0) | assert_equal(wrpc.getwalletinfo()["keypoolsize"], 0) | ||||
# Cannot import those pubkeys to keypool of wallet with privkeys | # Cannot import those pubkeys to keypool of wallet with privkeys | ||||
self.log.info( | self.log.info( | ||||
"Pubkeys cannot be added to the keypool of a wallet with private keys") | "Pubkeys cannot be added to the keypool of a wallet with private keys" | ||||
) | |||||
wrpc = self.nodes[1].get_wallet_rpc(self.default_wallet_name) | wrpc = self.nodes[1].get_wallet_rpc(self.default_wallet_name) | ||||
assert wrpc.getwalletinfo()['private_keys_enabled'] | assert wrpc.getwalletinfo()["private_keys_enabled"] | ||||
result = wrpc.importmulti( | result = wrpc.importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh({pub1})"), | { | ||||
'keypool': True, | "desc": descsum_create(f"pkh({pub1})"), | ||||
"keypool": True, | |||||
"timestamp": "now", | "timestamp": "now", | ||||
}] | } | ||||
] | |||||
) | ) | ||||
assert_equal(result[0]['error']['code'], -8) | assert_equal(result[0]["error"]["code"], -8) | ||||
assert_equal( | assert_equal( | ||||
result[0]['error']['message'], | result[0]["error"]["message"], | ||||
"Keys can only be imported to the keypool when private keys are disabled") | "Keys can only be imported to the keypool when private keys are disabled", | ||||
) | |||||
# Make sure ranged imports import keys in order | # Make sure ranged imports import keys in order | ||||
self.log.info('Key ranges should be imported in order') | self.log.info("Key ranges should be imported in order") | ||||
wrpc = self.nodes[1].get_wallet_rpc("noprivkeys") | wrpc = self.nodes[1].get_wallet_rpc("noprivkeys") | ||||
assert_equal(wrpc.getwalletinfo()["keypoolsize"], 0) | assert_equal(wrpc.getwalletinfo()["keypoolsize"], 0) | ||||
assert_equal(wrpc.getwalletinfo()["private_keys_enabled"], False) | assert_equal(wrpc.getwalletinfo()["private_keys_enabled"], False) | ||||
xpub = "tpubDAXcJ7s7ZwicqjprRaEWdPoHKrCS215qxGYxpusRLLmJuT69ZSicuGdSfyvyKpvUNYBW1s2U3NSrT6vrCYB9e6nZUEvrqnwXPF8ArTCRXMY" | xpub = "tpubDAXcJ7s7ZwicqjprRaEWdPoHKrCS215qxGYxpusRLLmJuT69ZSicuGdSfyvyKpvUNYBW1s2U3NSrT6vrCYB9e6nZUEvrqnwXPF8ArTCRXMY" | ||||
addresses = [ | addresses = [ | ||||
'ecregtest:qp0v86h53rc92hjrlpwzpjtdlgzsxu25svv6g40fpl', # m/0'/0'/0 | "ecregtest:qp0v86h53rc92hjrlpwzpjtdlgzsxu25svv6g40fpl", # m/0'/0'/0 | ||||
'ecregtest:qqasy0zlkdleqt4pkn8fs4ehm5gnnz6qpgdcpt90fq', # m/0'/0'/1 | "ecregtest:qqasy0zlkdleqt4pkn8fs4ehm5gnnz6qpgdcpt90fq", # m/0'/0'/1 | ||||
'ecregtest:qp0sp4wlhctvprqvdt2dgvqcfdjssu04xgey0l3syw', # m/0'/0'/2 | "ecregtest:qp0sp4wlhctvprqvdt2dgvqcfdjssu04xgey0l3syw", # m/0'/0'/2 | ||||
'ecregtest:qrhn24tegn04cptfv4ldhtkduxq55zcwrycjfdj9vr', # m/0'/0'/3 | "ecregtest:qrhn24tegn04cptfv4ldhtkduxq55zcwrycjfdj9vr", # m/0'/0'/3 | ||||
'ecregtest:qzpqhett2uwltq803vrxv7zkqhft5vsnmcjeh50v0p', # m/0'/0'/4 | "ecregtest:qzpqhett2uwltq803vrxv7zkqhft5vsnmcjeh50v0p", # m/0'/0'/4 | ||||
] | ] | ||||
result = wrpc.importmulti( | result = wrpc.importmulti( | ||||
[{ | [ | ||||
'desc': descsum_create(f"pkh([80002067/0h/0h]{xpub}/*)"), | { | ||||
'keypool': True, | "desc": descsum_create(f"pkh([80002067/0h/0h]{xpub}/*)"), | ||||
'timestamp': 'now', | "keypool": True, | ||||
'range': [0, 4], | "timestamp": "now", | ||||
}] | "range": [0, 4], | ||||
} | |||||
] | |||||
) | ) | ||||
self.log.info(result) | self.log.info(result) | ||||
for i in range(0, 5): | for i in range(0, 5): | ||||
addr = wrpc.getnewaddress('') | addr = wrpc.getnewaddress("") | ||||
assert_equal(addr, addresses[i]) | assert_equal(addr, addresses[i]) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
ImportMultiTest().main() | ImportMultiTest().main() |