Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] tidy up wallet_importmulti.py #15108

Merged
merged 2 commits into from Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 99 additions & 0 deletions test/functional/test_framework/wallet_util.py
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
# Copyright (c) 2018 The Bitcoin Core developers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2019

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was authored in 2018 (according to the git author date), so I guess it is fine.

# 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))
77 changes: 39 additions & 38 deletions test/functional/wallet_import_with_label.py
Expand Up @@ -11,7 +11,7 @@
"""

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):
Expand All @@ -32,11 +32,11 @@ def run_test(self):
address = self.nodes[0].getnewaddress()
label = "Test Label"
self.nodes[1].importaddress(address, label)
address_assert = self.nodes[1].getaddressinfo(address)

assert_equal(address_assert["iswatchonly"], True)
assert_equal(address_assert["ismine"], False)
assert_equal(address_assert["label"], label)
test_address(self.nodes[1],
address,
iswatchonly=True,
ismine=False,
label=label)

self.log.info(
"Import the watch-only address's private key without a "
Expand All @@ -45,19 +45,21 @@ def run_test(self):
priv_key = self.nodes[0].dumpprivkey(address)
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(
"Test importaddress without label and importprivkey with label."
)
self.log.info("Import a watch-only address without a label.")
address2 = self.nodes[0].getnewaddress()
self.nodes[1].importaddress(address2)
address_assert2 = self.nodes[1].getaddressinfo(address2)

assert_equal(address_assert2["iswatchonly"], True)
assert_equal(address_assert2["ismine"], False)
assert_equal(address_assert2["label"], "")
test_address(self.nodes[1],
address2,
iswatchonly=True,
ismine=False,
label="")

self.log.info(
"Import the watch-only address's private key with a "
Expand All @@ -67,18 +69,20 @@ def run_test(self):
label2 = "Test Label 2"
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("Import a watch-only address with a label.")
address3 = self.nodes[0].getnewaddress()
label3_addr = "Test Label 3 for importaddress"
self.nodes[1].importaddress(address3, label3_addr)
address_assert3 = self.nodes[1].getaddressinfo(address3)

assert_equal(address_assert3["iswatchonly"], True)
assert_equal(address_assert3["ismine"], False)
assert_equal(address_assert3["label"], label3_addr)
test_address(self.nodes[1],
address3,
iswatchonly=True,
ismine=False,
label=label3_addr)

self.log.info(
"Import the watch-only address's private key with a "
Expand All @@ -88,7 +92,9 @@ def run_test(self):
label3_priv = "Test Label 3 for importprivkey"
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(
"Test importprivkey won't label new dests with the same "
Expand All @@ -98,15 +104,12 @@ def run_test(self):
address4 = self.nodes[0].getnewaddress()
label4_addr = "Test Label 4 for importaddress"
self.nodes[1].importaddress(address4, label4_addr)
address_assert4 = self.nodes[1].getaddressinfo(address4)

assert_equal(address_assert4["iswatchonly"], True)
assert_equal(address_assert4["ismine"], False)
assert_equal(address_assert4["label"], label4_addr)

self.log.info("Asserts address has no embedded field with dests.")

assert_equal(address_assert4.get("embedded"), None)
test_address(self.nodes[1],
address4,
iswatchonly=True,
ismine=False,
label=label4_addr,
embedded=None)

self.log.info(
"Import the watch-only address's private key without a "
Expand All @@ -116,16 +119,14 @@ def run_test(self):
)
priv_key4 = self.nodes[0].dumpprivkey(address4)
self.nodes[1].importprivkey(priv_key4)
address_assert4 = self.nodes[1].getaddressinfo(address4)

assert address_assert4.get("embedded")

bcaddress_assert = self.nodes[1].getaddressinfo(
address_assert4["embedded"]["address"]
)

assert_equal(address_assert4["label"], label4_addr)
assert_equal(bcaddress_assert["label"], "")
embedded_addr = self.nodes[1].getaddressinfo(address4)['embedded']['address']

test_address(self.nodes[1],
embedded_addr,
label="")
test_address(self.nodes[1],
address4,
label=label4_addr)

self.stop_nodes()

Expand Down