Merge #15108: [tests] tidy up wallet_importmulti.py

2d5f1ea2e3 [tests] move wallet util functions to wallet_util.py (John Newbery)
6be64ef02c [tests] tidy up wallet_importmulti.py (John Newbery)

Pull request description:

  Cherry picks un-merged commits from #14952, which "fixes review comments from @ryanofsky here: https://github.com/bitcoin/bitcoin/pull/14886#pullrequestreview-183772779"

Tree-SHA512: 5f389196b0140d013a533d500f1812786a3a5cfb65980e13eaeacc459fddb55f43d05da3ab5e7cc8c997f26c0b667eed081ab6de2d125e631c70a7dd4c06e350
This commit is contained in:
MarcoFalke 2019-01-09 16:58:40 -05:00
commit 1cfbb16b5d
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25
3 changed files with 359 additions and 334 deletions

View file

@ -0,0 +1,99 @@
#!/usr/bin/env python3
# Copyright (c) 2018 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Useful util functions for testing the wallet"""
from collections import namedtuple
from test_framework.address import (
key_to_p2pkh,
key_to_p2sh_p2wpkh,
key_to_p2wpkh,
script_to_p2sh,
script_to_p2sh_p2wsh,
script_to_p2wsh,
)
from test_framework.script import (
CScript,
OP_0,
OP_2,
OP_3,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_DUP,
OP_EQUAL,
OP_EQUALVERIFY,
OP_HASH160,
hash160,
sha256,
)
from test_framework.util import hex_str_to_bytes
Key = namedtuple('Key', ['privkey',
'pubkey',
'p2pkh_script',
'p2pkh_addr',
'p2wpkh_script',
'p2wpkh_addr',
'p2sh_p2wpkh_script',
'p2sh_p2wpkh_redeem_script',
'p2sh_p2wpkh_addr'])
Multisig = namedtuple('Multisig', ['privkeys',
'pubkeys',
'p2sh_script',
'p2sh_addr',
'redeem_script',
'p2wsh_script',
'p2wsh_addr',
'p2sh_p2wsh_script',
'p2sh_p2wsh_addr'])
def get_key(node):
"""Generate a fresh key on node
Returns a named tuple of privkey, pubkey and all address and scripts."""
addr = node.getnewaddress()
pubkey = node.getaddressinfo(addr)['pubkey']
pkh = hash160(hex_str_to_bytes(pubkey))
return Key(privkey=node.dumpprivkey(addr),
pubkey=pubkey,
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
p2pkh_addr=key_to_p2pkh(pubkey),
p2wpkh_script=CScript([OP_0, pkh]).hex(),
p2wpkh_addr=key_to_p2wpkh(pubkey),
p2sh_p2wpkh_script=CScript([OP_HASH160, hash160(CScript([OP_0, pkh])), OP_EQUAL]).hex(),
p2sh_p2wpkh_redeem_script=CScript([OP_0, pkh]).hex(),
p2sh_p2wpkh_addr=key_to_p2sh_p2wpkh(pubkey))
def get_multisig(node):
"""Generate a fresh 2-of-3 multisig on node
Returns a named tuple of privkeys, pubkeys and all address and scripts."""
addrs = []
pubkeys = []
for _ in range(3):
addr = node.getaddressinfo(node.getnewaddress())
addrs.append(addr['address'])
pubkeys.append(addr['pubkey'])
script_code = CScript([OP_2] + [hex_str_to_bytes(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
witness_script = CScript([OP_0, sha256(script_code)])
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
pubkeys=pubkeys,
p2sh_script=CScript([OP_HASH160, hash160(script_code), OP_EQUAL]).hex(),
p2sh_addr=script_to_p2sh(script_code),
redeem_script=script_code.hex(),
p2wsh_script=witness_script.hex(),
p2wsh_addr=script_to_p2wsh(script_code),
p2sh_p2wsh_script=CScript([OP_HASH160, witness_script, OP_EQUAL]).hex(),
p2sh_p2wsh_addr=script_to_p2sh_p2wsh(script_code))
def test_address(node, address, **kwargs):
"""Get address info for `address` and test whether the returned values are as expected."""
addr_info = node.getaddressinfo(address)
for key, value in kwargs.items():
if value is None:
if key in addr_info.keys():
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
elif addr_info[key] != value:
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))

View file

@ -11,7 +11,7 @@ with and without a label.
""" """
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.wallet_util import test_address
class ImportWithLabel(BitcoinTestFramework): class ImportWithLabel(BitcoinTestFramework):
@ -32,11 +32,11 @@ class ImportWithLabel(BitcoinTestFramework):
address = self.nodes[0].getnewaddress() address = self.nodes[0].getnewaddress()
label = "Test Label" label = "Test Label"
self.nodes[1].importaddress(address, label) self.nodes[1].importaddress(address, label)
address_assert = self.nodes[1].getaddressinfo(address) test_address(self.nodes[1],
address,
assert_equal(address_assert["iswatchonly"], True) iswatchonly=True,
assert_equal(address_assert["ismine"], False) ismine=False,
assert_equal(address_assert["label"], label) label=label)
self.log.info( self.log.info(
"Import the watch-only address's private key without a " "Import the watch-only address's private key without a "
@ -45,7 +45,9 @@ class ImportWithLabel(BitcoinTestFramework):
priv_key = self.nodes[0].dumpprivkey(address) priv_key = self.nodes[0].dumpprivkey(address)
self.nodes[1].importprivkey(priv_key) self.nodes[1].importprivkey(priv_key)
assert_equal(label, self.nodes[1].getaddressinfo(address)["label"]) test_address(self.nodes[1],
address,
label=label)
self.log.info( self.log.info(
"Test importaddress without label and importprivkey with label." "Test importaddress without label and importprivkey with label."
@ -53,11 +55,11 @@ class ImportWithLabel(BitcoinTestFramework):
self.log.info("Import a watch-only address without a label.") self.log.info("Import a watch-only address without a label.")
address2 = self.nodes[0].getnewaddress() address2 = self.nodes[0].getnewaddress()
self.nodes[1].importaddress(address2) self.nodes[1].importaddress(address2)
address_assert2 = self.nodes[1].getaddressinfo(address2) test_address(self.nodes[1],
address2,
assert_equal(address_assert2["iswatchonly"], True) iswatchonly=True,
assert_equal(address_assert2["ismine"], False) ismine=False,
assert_equal(address_assert2["label"], "") label="")
self.log.info( self.log.info(
"Import the watch-only address's private key with a " "Import the watch-only address's private key with a "
@ -67,18 +69,20 @@ class ImportWithLabel(BitcoinTestFramework):
label2 = "Test Label 2" label2 = "Test Label 2"
self.nodes[1].importprivkey(priv_key2, label2) self.nodes[1].importprivkey(priv_key2, label2)
assert_equal(label2, self.nodes[1].getaddressinfo(address2)["label"]) test_address(self.nodes[1],
address2,
label=label2)
self.log.info("Test importaddress with label and importprivkey with label.") self.log.info("Test importaddress with label and importprivkey with label.")
self.log.info("Import a watch-only address with a label.") self.log.info("Import a watch-only address with a label.")
address3 = self.nodes[0].getnewaddress() address3 = self.nodes[0].getnewaddress()
label3_addr = "Test Label 3 for importaddress" label3_addr = "Test Label 3 for importaddress"
self.nodes[1].importaddress(address3, label3_addr) self.nodes[1].importaddress(address3, label3_addr)
address_assert3 = self.nodes[1].getaddressinfo(address3) test_address(self.nodes[1],
address3,
assert_equal(address_assert3["iswatchonly"], True) iswatchonly=True,
assert_equal(address_assert3["ismine"], False) ismine=False,
assert_equal(address_assert3["label"], label3_addr) label=label3_addr)
self.log.info( self.log.info(
"Import the watch-only address's private key with a " "Import the watch-only address's private key with a "
@ -88,7 +92,9 @@ class ImportWithLabel(BitcoinTestFramework):
label3_priv = "Test Label 3 for importprivkey" label3_priv = "Test Label 3 for importprivkey"
self.nodes[1].importprivkey(priv_key3, label3_priv) self.nodes[1].importprivkey(priv_key3, label3_priv)
assert_equal(label3_priv, self.nodes[1].getaddressinfo(address3)["label"]) test_address(self.nodes[1],
address3,
label=label3_priv)
self.log.info( self.log.info(
"Test importprivkey won't label new dests with the same " "Test importprivkey won't label new dests with the same "
@ -98,15 +104,12 @@ class ImportWithLabel(BitcoinTestFramework):
address4 = self.nodes[0].getnewaddress() address4 = self.nodes[0].getnewaddress()
label4_addr = "Test Label 4 for importaddress" label4_addr = "Test Label 4 for importaddress"
self.nodes[1].importaddress(address4, label4_addr) self.nodes[1].importaddress(address4, label4_addr)
address_assert4 = self.nodes[1].getaddressinfo(address4) test_address(self.nodes[1],
address4,
assert_equal(address_assert4["iswatchonly"], True) iswatchonly=True,
assert_equal(address_assert4["ismine"], False) ismine=False,
assert_equal(address_assert4["label"], label4_addr) label=label4_addr,
embedded=None)
self.log.info("Asserts address has no embedded field with dests.")
assert_equal(address_assert4.get("embedded"), None)
self.log.info( self.log.info(
"Import the watch-only address's private key without a " "Import the watch-only address's private key without a "
@ -116,16 +119,14 @@ class ImportWithLabel(BitcoinTestFramework):
) )
priv_key4 = self.nodes[0].dumpprivkey(address4) priv_key4 = self.nodes[0].dumpprivkey(address4)
self.nodes[1].importprivkey(priv_key4) self.nodes[1].importprivkey(priv_key4)
address_assert4 = self.nodes[1].getaddressinfo(address4) embedded_addr = self.nodes[1].getaddressinfo(address4)['embedded']['address']
assert address_assert4.get("embedded") test_address(self.nodes[1],
embedded_addr,
bcaddress_assert = self.nodes[1].getaddressinfo( label="")
address_assert4["embedded"]["address"] test_address(self.nodes[1],
) address4,
label=label4_addr)
assert_equal(address_assert4["label"], label4_addr)
assert_equal(bcaddress_assert["label"], "")
self.stop_nodes() self.stop_nodes()

View file

@ -14,30 +14,10 @@ variants.
success, and (if unsuccessful) test the error code and error message returned. success, and (if unsuccessful) test the error code and error message returned.
- `test_address()` is called to call getaddressinfo for an address on node1 - `test_address()` is called to call getaddressinfo for an address on node1
and test the values returned.""" and test the values returned."""
from collections import namedtuple
from test_framework.address import (
key_to_p2pkh,
key_to_p2sh_p2wpkh,
key_to_p2wpkh,
script_to_p2sh,
script_to_p2sh_p2wsh,
script_to_p2wsh,
)
from test_framework.script import ( from test_framework.script import (
CScript, CScript,
OP_0,
OP_2,
OP_3,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_DUP,
OP_EQUAL,
OP_EQUALVERIFY,
OP_HASH160,
OP_NOP, OP_NOP,
hash160,
sha256,
) )
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
@ -45,28 +25,12 @@ from test_framework.util import (
assert_greater_than, assert_greater_than,
assert_raises_rpc_error, assert_raises_rpc_error,
bytes_to_hex_str, bytes_to_hex_str,
hex_str_to_bytes
) )
from test_framework.wallet_util import (
Key = namedtuple('Key', ['privkey', get_key,
'pubkey', get_multisig,
'p2pkh_script', test_address,
'p2pkh_addr', )
'p2wpkh_script',
'p2wpkh_addr',
'p2sh_p2wpkh_script',
'p2sh_p2wpkh_redeem_script',
'p2sh_p2wpkh_addr'])
Multisig = namedtuple('Multisig', ['privkeys',
'pubkeys',
'p2sh_script',
'p2sh_addr',
'redeem_script',
'p2wsh_script',
'p2wsh_addr',
'p2sh_p2wsh_script',
'p2sh_p2wsh_addr'])
class ImportMultiTest(BitcoinTestFramework): class ImportMultiTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -80,45 +44,6 @@ class ImportMultiTest(BitcoinTestFramework):
def setup_network(self): def setup_network(self):
self.setup_nodes() self.setup_nodes()
def get_key(self):
"""Generate a fresh key on node0
Returns a named tuple of privkey, pubkey and all address and scripts."""
addr = self.nodes[0].getnewaddress()
pubkey = self.nodes[0].getaddressinfo(addr)['pubkey']
pkh = hash160(hex_str_to_bytes(pubkey))
return Key(self.nodes[0].dumpprivkey(addr),
pubkey,
CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(), # p2pkh
key_to_p2pkh(pubkey), # p2pkh addr
CScript([OP_0, pkh]).hex(), # p2wpkh
key_to_p2wpkh(pubkey), # p2wpkh addr
CScript([OP_HASH160, hash160(CScript([OP_0, pkh])), OP_EQUAL]).hex(), # p2sh-p2wpkh
CScript([OP_0, pkh]).hex(), # p2sh-p2wpkh redeem script
key_to_p2sh_p2wpkh(pubkey)) # p2sh-p2wpkh addr
def get_multisig(self):
"""Generate a fresh multisig on node0
Returns a named tuple of privkeys, pubkeys and all address and scripts."""
addrs = []
pubkeys = []
for _ in range(3):
addr = self.nodes[0].getaddressinfo(self.nodes[0].getnewaddress())
addrs.append(addr['address'])
pubkeys.append(addr['pubkey'])
script_code = CScript([OP_2] + [hex_str_to_bytes(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
witness_script = CScript([OP_0, sha256(script_code)])
return Multisig([self.nodes[0].dumpprivkey(addr) for addr in addrs],
pubkeys,
CScript([OP_HASH160, hash160(script_code), OP_EQUAL]).hex(), # p2sh
script_to_p2sh(script_code), # p2sh addr
script_code.hex(), # redeem script
witness_script.hex(), # p2wsh
script_to_p2wsh(script_code), # p2wsh addr
CScript([OP_HASH160, witness_script, OP_EQUAL]).hex(), # p2sh-p2wsh
script_to_p2sh_p2wsh(script_code)) # p2sh-p2wsh addr
def test_importmulti(self, req, success, error_code=None, error_message=None, warnings=[]): def test_importmulti(self, req, success, error_code=None, error_message=None, warnings=[]):
"""Run importmulti and assert success""" """Run importmulti and assert success"""
result = self.nodes[1].importmulti([req]) result = self.nodes[1].importmulti([req])
@ -131,16 +56,6 @@ class ImportMultiTest(BitcoinTestFramework):
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 test_address(self, address, **kwargs):
"""Get address info for `address` and test whether the returned values are as expected."""
addr_info = self.nodes[1].getaddressinfo(address)
for key, value in kwargs.items():
if value is None:
if key in addr_info.keys():
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
elif addr_info[key] != value:
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))
def run_test(self): def run_test(self):
self.log.info("Mining blocks...") self.log.info("Mining blocks...")
self.nodes[0].generate(1) self.nodes[0].generate(1)
@ -164,34 +79,35 @@ class ImportMultiTest(BitcoinTestFramework):
# 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 = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
timestamp=timestamp, timestamp=timestamp,
ischange=False) ischange=False)
watchonly_address = address 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({"scriptPubKey": {"address": "not valid address"},
"timestamp": "now"}, "timestamp": "now"},
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 = self.get_key() 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},
True) success=True)
self.test_address(key.p2pkh_addr, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
timestamp=timestamp, timestamp=timestamp,
@ -199,142 +115,142 @@ class ImportMultiTest(BitcoinTestFramework):
# ScriptPubKey + internal + label # ScriptPubKey + internal + label
self.log.info("Should not allow a label to be specified when internal is true") self.log.info("Should not allow a label to be specified when internal is true")
key = self.get_key() 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": "Example label"}, "label": "Example label"},
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("Should not import a nonstandard scriptPubKey without internal flag") self.log.info("Should not import a nonstandard scriptPubKey without internal flag")
nonstandardScriptPubKey = key.p2pkh_script + bytes_to_hex_str(CScript([OP_NOP])) nonstandardScriptPubKey = key.p2pkh_script + bytes_to_hex_str(CScript([OP_NOP]))
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey,
"timestamp": "now"}, "timestamp": "now"},
False, success=False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
self.test_address(address, test_address(self.nodes[1],
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 = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"pubkeys": [key.pubkey], "pubkeys": [key.pubkey],
"internal": False}, "internal": False},
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.test_address(address, test_address(self.nodes[1],
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("Should import a scriptPubKey with internal and with public key") self.log.info("Should import a scriptPubKey with internal and with public key")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr
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},
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.test_address(address, test_address(self.nodes[1],
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("Should not import a nonstandard scriptPubKey without internal and with public key") self.log.info("Should not import a nonstandard scriptPubKey without internal and with public key")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey,
"timestamp": "now", "timestamp": "now",
"pubkeys": [key.pubkey]}, "pubkeys": [key.pubkey]},
False, success=False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
self.test_address(address, test_address(self.nodes[1],
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 = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=False, iswatchonly=False,
ismine=True, ismine=True,
timestamp=timestamp) timestamp=timestamp)
self.log.info("Should not import an address with private key if is already imported") self.log.info("Should not import an address with private key if is already imported")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
False, success=False,
error_code=-4, error_code=-4,
error_message='The wallet already contains the private key for this address or script') error_message='The wallet already contains the private key for this address or script')
# Address + Private key + watchonly # Address + Private key + watchonly
self.log.info("Should import an address with private key and with watchonly") self.log.info("Should import an address with private key and with watchonly")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey], "keys": [key.privkey],
"watchonly": True}, "watchonly": True},
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=["All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag."])
self.test_address(address, test_address(self.nodes[1],
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("Should import a scriptPubKey with internal and with private key") self.log.info("Should import a scriptPubKey with internal and with private key")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr
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},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
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("Should not import a nonstandard scriptPubKey without internal and with private key") self.log.info("Should not import a nonstandard scriptPubKey without internal and with private key")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr
self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey, self.test_importmulti({"scriptPubKey": nonstandardScriptPubKey,
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
False, success=False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=False, iswatchonly=False,
ismine=False, ismine=False,
timestamp=None) timestamp=None)
# P2SH address # P2SH address
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
self.nodes[1].generate(100) self.nodes[1].generate(100)
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
self.nodes[1].generate(1) self.nodes[1].generate(1)
@ -343,8 +259,9 @@ class ImportMultiTest(BitcoinTestFramework):
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({"scriptPubKey": {"address": multisig.p2sh_addr},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(multisig.p2sh_addr, test_address(self.nodes[1],
multisig.p2sh_addr,
isscript=True, isscript=True,
iswatchonly=True, iswatchonly=True,
timestamp=timestamp) timestamp=timestamp)
@ -353,7 +270,7 @@ class ImportMultiTest(BitcoinTestFramework):
assert_equal(p2shunspent['solvable'], False) assert_equal(p2shunspent['solvable'], False)
# P2SH + Redeem script # P2SH + Redeem script
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
self.nodes[1].generate(100) self.nodes[1].generate(100)
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
self.nodes[1].generate(1) self.nodes[1].generate(1)
@ -363,16 +280,17 @@ class ImportMultiTest(BitcoinTestFramework):
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},
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.test_address(multisig.p2sh_addr, timestamp=timestamp, iswatchonly=True, ismine=False, solvable=True) test_address(self.nodes[1],
multisig.p2sh_addr, timestamp=timestamp, iswatchonly=True, ismine=False, solvable=True)
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'], True) assert_equal(p2shunspent['solvable'], True)
# P2SH + Redeem script + Private Keys + !Watchonly # P2SH + Redeem script + Private Keys + !Watchonly
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
self.nodes[1].generate(100) self.nodes[1].generate(100)
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
self.nodes[1].generate(1) self.nodes[1].generate(1)
@ -383,9 +301,10 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"redeemscript": multisig.redeem_script, "redeemscript": multisig.redeem_script,
"keys": multisig.privkeys[0:2]}, "keys": multisig.privkeys[0:2]},
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.test_address(multisig.p2sh_addr, test_address(self.nodes[1],
multisig.p2sh_addr,
timestamp=timestamp, timestamp=timestamp,
ismine=False, ismine=False,
iswatchonly=True, iswatchonly=True,
@ -396,7 +315,7 @@ class ImportMultiTest(BitcoinTestFramework):
assert_equal(p2shunspent['solvable'], True) assert_equal(p2shunspent['solvable'], True)
# P2SH + Redeem script + Private Keys + Watchonly # P2SH + Redeem script + Private Keys + Watchonly
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
self.nodes[1].generate(100) self.nodes[1].generate(100)
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00) self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
self.nodes[1].generate(1) self.nodes[1].generate(1)
@ -408,8 +327,9 @@ class ImportMultiTest(BitcoinTestFramework):
"redeemscript": multisig.redeem_script, "redeemscript": multisig.redeem_script,
"keys": multisig.privkeys[0:2], "keys": multisig.privkeys[0:2],
"watchonly": True}, "watchonly": True},
True) success=True)
self.test_address(multisig.p2sh_addr, test_address(self.nodes[1],
multisig.p2sh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
solvable=True, solvable=True,
@ -417,15 +337,15 @@ class ImportMultiTest(BitcoinTestFramework):
# Address + Public key + !Internal + Wrong pubkey # Address + Public key + !Internal + Wrong pubkey
self.log.info("Should not import an address with the wrong public key as non-solvable") self.log.info("Should not import an address with the wrong public key as non-solvable")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr wrong_key = get_key(self.nodes[0]).pubkey
wrong_key = self.get_key().pubkey self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"pubkeys": [wrong_key]}, "pubkeys": [wrong_key]},
True, success=True,
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
solvable=False, solvable=False,
@ -433,16 +353,16 @@ class ImportMultiTest(BitcoinTestFramework):
# ScriptPubKey + Public key + internal + Wrong pubkey # ScriptPubKey + Public key + internal + Wrong pubkey
self.log.info("Should import a scriptPubKey with internal and with a wrong public key as non-solvable") self.log.info("Should import a scriptPubKey with internal and with a wrong public key as non-solvable")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr wrong_key = get_key(self.nodes[0]).pubkey
wrong_key = self.get_key().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},
True, success=True,
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
solvable=False, solvable=False,
@ -450,15 +370,15 @@ class ImportMultiTest(BitcoinTestFramework):
# Address + Private key + !watchonly + Wrong private key # Address + Private key + !watchonly + Wrong private key
self.log.info("Should import an address with a wrong private key as non-solvable") self.log.info("Should import an address with a wrong private key as non-solvable")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr wrong_privkey = get_key(self.nodes[0]).privkey
wrong_privkey = self.get_key().privkey self.test_importmulti({"scriptPubKey": {"address": key.p2pkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"keys": [wrong_privkey]}, "keys": [wrong_privkey]},
True, success=True,
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
solvable=False, solvable=False,
@ -466,16 +386,16 @@ class ImportMultiTest(BitcoinTestFramework):
# ScriptPubKey + Private key + internal + Wrong private key # ScriptPubKey + Private key + internal + Wrong private key
self.log.info("Should import a scriptPubKey with internal and with a wrong private key as non-solvable") self.log.info("Should import a scriptPubKey with internal and with a wrong private key as non-solvable")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2pkh_addr wrong_privkey = get_key(self.nodes[0]).privkey
wrong_privkey = self.get_key().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},
True, success=True,
warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."]) warnings=["Importing as non-solvable: some required keys are missing. If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.", "Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
self.test_address(address, test_address(self.nodes[1],
key.p2pkh_addr,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
solvable=False, solvable=False,
@ -486,8 +406,9 @@ class ImportMultiTest(BitcoinTestFramework):
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({"scriptPubKey": {"address": watchonly_address},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(watchonly_address, test_address(self.nodes[1],
watchonly_address,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
timestamp=timestamp) timestamp=timestamp)
@ -496,7 +417,8 @@ class ImportMultiTest(BitcoinTestFramework):
# restart nodes to check for proper serialization/deserialization of watch only address # restart nodes to check for proper serialization/deserialization of watch only address
self.stop_nodes() self.stop_nodes()
self.start_nodes() self.start_nodes()
self.test_address(watchonly_address, test_address(self.nodes[1],
watchonly_address,
iswatchonly=True, iswatchonly=True,
ismine=False, ismine=False,
timestamp=watchonly_timestamp) timestamp=watchonly_timestamp)
@ -513,47 +435,48 @@ class ImportMultiTest(BitcoinTestFramework):
# Import P2WPKH address as watch only # Import P2WPKH address as watch only
self.log.info("Should import a P2WPKH address as watch only") self.log.info("Should import a P2WPKH address as watch only")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2wpkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2wpkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2wpkh_addr,
iswatchonly=True, iswatchonly=True,
solvable=False) solvable=False)
# Import P2WPKH address with public key but no private key # Import P2WPKH address with public key but no private key
self.log.info("Should import a P2WPKH address and public key as solvable but not spendable") self.log.info("Should import a P2WPKH address and public key as solvable but not spendable")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2wpkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2wpkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"pubkeys": [key.pubkey]}, "pubkeys": [key.pubkey]},
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.test_address(address, test_address(self.nodes[1],
key.p2wpkh_addr,
ismine=False, ismine=False,
solvable=True) solvable=True)
# Import P2WPKH address with key and check it is spendable # Import P2WPKH address with key and check it is spendable
self.log.info("Should import a P2WPKH address with key") self.log.info("Should import a P2WPKH address with key")
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2wpkh_addr self.test_importmulti({"scriptPubKey": {"address": key.p2wpkh_addr},
self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2wpkh_addr,
iswatchonly=False, iswatchonly=False,
ismine=True) ismine=True)
# P2WSH multisig address without scripts or keys # P2WSH multisig address without scripts or keys
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
self.log.info("Should import a p2wsh multisig as watch only without respective redeem script and private keys") self.log.info("Should import a p2wsh multisig as watch only without respective redeem script and private keys")
self.test_importmulti({"scriptPubKey": {"address": multisig.p2wsh_addr}, self.test_importmulti({"scriptPubKey": {"address": multisig.p2wsh_addr},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(multisig.p2sh_addr, test_address(self.nodes[1],
multisig.p2sh_addr,
solvable=False) solvable=False)
# Same P2WSH multisig address as above, but now with witnessscript + private keys # Same P2WSH multisig address as above, but now with witnessscript + private keys
@ -562,59 +485,61 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"witnessscript": multisig.redeem_script, "witnessscript": multisig.redeem_script,
"keys": multisig.privkeys}, "keys": multisig.privkeys},
True) success=True)
self.test_address(multisig.p2sh_addr, test_address(self.nodes[1],
multisig.p2sh_addr,
solvable=True, solvable=True,
ismine=True, ismine=True,
sigsrequired=2) sigsrequired=2)
# P2SH-P2WPKH address with no redeemscript or public or private key # P2SH-P2WPKH address with no redeemscript or public or private key
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2sh_p2wpkh_addr
self.log.info("Should import a p2sh-p2wpkh without redeem script or keys") self.log.info("Should import a p2sh-p2wpkh without redeem script or keys")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": key.p2sh_p2wpkh_addr},
"timestamp": "now"}, "timestamp": "now"},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2sh_p2wpkh_addr,
solvable=False, solvable=False,
ismine=False) ismine=False)
# P2SH-P2WPKH address + redeemscript + public key with no private key # P2SH-P2WPKH address + redeemscript + public key with no private key
self.log.info("Should import a p2sh-p2wpkh with respective redeem script and pubkey as solvable") self.log.info("Should import a p2sh-p2wpkh with respective redeem script and pubkey as solvable")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": key.p2sh_p2wpkh_addr},
"timestamp": "now", "timestamp": "now",
"redeemscript": key.p2sh_p2wpkh_redeem_script, "redeemscript": key.p2sh_p2wpkh_redeem_script,
"pubkeys": [key.pubkey]}, "pubkeys": [key.pubkey]},
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.test_address(address, test_address(self.nodes[1],
key.p2sh_p2wpkh_addr,
solvable=True, solvable=True,
ismine=False) ismine=False)
# P2SH-P2WPKH address + redeemscript + private key # P2SH-P2WPKH address + redeemscript + private key
key = self.get_key() key = get_key(self.nodes[0])
address = key.p2sh_p2wpkh_addr
self.log.info("Should import a p2sh-p2wpkh with respective redeem script and private keys") self.log.info("Should import a p2sh-p2wpkh with respective redeem script and private keys")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": key.p2sh_p2wpkh_addr},
"timestamp": "now", "timestamp": "now",
"redeemscript": key.p2sh_p2wpkh_redeem_script, "redeemscript": key.p2sh_p2wpkh_redeem_script,
"keys": [key.privkey]}, "keys": [key.privkey]},
True) success=True)
self.test_address(address, test_address(self.nodes[1],
key.p2sh_p2wpkh_addr,
solvable=True, solvable=True,
ismine=True) ismine=True)
# P2SH-P2WSH multisig + redeemscript with no private key # P2SH-P2WSH multisig + redeemscript with no private key
multisig = self.get_multisig() multisig = get_multisig(self.nodes[0])
address = multisig.p2sh_p2wsh_addr
self.log.info("Should import a p2sh-p2wsh with respective redeem script but no private key") self.log.info("Should import a p2sh-p2wsh with respective redeem script but no private key")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_p2wsh_addr},
"timestamp": "now", "timestamp": "now",
"redeemscript": multisig.p2wsh_script, "redeemscript": multisig.p2wsh_script,
"witnessscript": multisig.redeem_script}, "witnessscript": multisig.redeem_script},
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.test_address(address, test_address(self.nodes[1],
multisig.p2sh_p2wsh_addr,
solvable=True, solvable=True,
ismine=False) ismine=False)