From 6668e21e1ff786e3cad9cdc683b3c5ac640a896d Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 3 Jun 2020 07:30:58 -0400 Subject: [PATCH 01/12] Merge #18974: test: Check that invalid witness destinations can not be imported facede18a42ccbf45cb5156bd4e5c9cabb359821 test: Check that invalid witness destinations can not be imported (MarcoFalke) Pull request description: ACKs for top commit: practicalswift: ACK facede18a42ccbf45cb5156bd4e5c9cabb359821 -- thanks for adding! Tree-SHA512: 87000606fac2e6f2780ca75cdeeb2dc1f0528d9b8f13e4156e8304ce7a6b1eb014781b6f0c59d11544bf360ba3dc5f99549470b0876132e189b9107f2c6bb38d --- test/functional/wallet_labels.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/functional/wallet_labels.py b/test/functional/wallet_labels.py index 6bcd9413ff08d..7891c86e6e646 100755 --- a/test/functional/wallet_labels.py +++ b/test/functional/wallet_labels.py @@ -137,6 +137,33 @@ def run_test(self): # in the label. This is a no-op. change_label(node, labels[2].addresses[0], labels[2], labels[2]) + self.log.info('Check watchonly labels') + node.createwallet(wallet_name='watch_only', disable_private_keys=True, descriptors=False) + wallet_watch_only = node.get_wallet_rpc('watch_only') + VALID = { + '✔️_a1': 'yMNJePdcKvXtWWQnFYHNeJ5u8TF2v1dfK4', + '✔️_a2': 'yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3', + '✔️_a3': '93bVhahvUKmQu8gu9g3QnPPa2cxFK98pMB', + } + INVALID = { + '❌_b1': 'y000000000000000000000000000000000', + '❌_b2': 'y000000000000000000000000000000001', + } + for l in VALID: + ad = VALID[l] + wallet_watch_only.importaddress(label=l, rescan=False, address=ad) + node.generatetoaddress(1, ad) + assert_equal(wallet_watch_only.getaddressesbylabel(label=l), {ad: {'purpose': 'receive'}}) + assert_equal(wallet_watch_only.getreceivedbylabel(label=l), 0) + for l in INVALID: + ad = INVALID[l] + assert_raises_rpc_error( + -5, + "Invalid Dash address or script", + lambda: wallet_watch_only.importaddress(label=l, rescan=False, address=ad), + ) + + class Label: def __init__(self, name): # Label name From eaf31ad2be268ba81c8be662d2102c1e898b1dc3 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 10 Jun 2020 10:23:46 -0400 Subject: [PATCH 02/12] Merge #19230: [TESTS] Move base58 to own module to break circular dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 [TESTS] Move base58 to own module to break circular dependency (Pieter Wuille) Pull request description: I encountered difficulties with the test framework in #17977. This fixes them, and I think the change is generally useful. ACKs for top commit: laanwj: Code review ACK c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 MarcoFalke: ACK c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 according to --color-moved=dimmed-zebra this is a move-only apart from the imports 👒 Tree-SHA512: 9e0493de3e279074f0c70e92c959b73ae30479ad6f2083a3c6bbf4b0191d65ef94854559a5b7c904f5dadc5e93129ed00f6dc0a8ccce6ba7921cd45f7119f74b --- test/functional/test_framework/address.py | 68 +--------------------- test/functional/test_framework/base58.py | 70 +++++++++++++++++++++++ test/functional/test_framework/key.py | 2 +- test/functional/test_runner.py | 2 +- 4 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 test/functional/test_framework/base58.py diff --git a/test/functional/test_framework/address.py b/test/functional/test_framework/address.py index 96bb1d6d5c98c..441482ca9e291 100644 --- a/test/functional/test_framework/address.py +++ b/test/functional/test_framework/address.py @@ -8,61 +8,16 @@ # # This file encodes and decodes BASE58 P2PKH and P2SH addresses # -import unittest -from .script import hash256, hash160, CScript +from .base58 import byte_to_base58 +from .script import hash160, CScript from .util import hex_str_to_bytes -from test_framework.util import assert_equal # Note unlike in bitcoin, this address isn't bech32 since we don't (at this time) support bech32. ADDRESS_BCRT1_UNSPENDABLE = 'yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9' ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk' ADDRESS_BCRT1_P2SH_OP_TRUE = '8zJctvfrzGZ5s1zQ3kagwyW1DsPYSQ4V2P' -chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' - - -def byte_to_base58(b, version): - result = '' - str = b.hex() - str = chr(version).encode('latin-1').hex() + str - checksum = hash256(hex_str_to_bytes(str)).hex() - str += checksum[:8] - value = int('0x'+str,0) - while value > 0: - result = chars[value % 58] + result - value //= 58 - while (str[:2] == '00'): - result = chars[0] + result - str = str[2:] - return result - - -def base58_to_byte(s, verify_checksum=True): - if not s: - return b'' - n = 0 - for c in s: - n *= 58 - assert c in chars - digit = chars.index(c) - n += digit - h = '%x' % n - if len(h) % 2: - h = '0' + h - res = n.to_bytes((n.bit_length() + 7) // 8, 'big') - pad = 0 - for c in s: - if c == chars[0]: - pad += 1 - else: - break - res = b'\x00' * pad + res - if verify_checksum: - assert_equal(hash256(res[:-4])[:4], res[-4:]) - - return res[1:-4], int(res[0]) - def keyhash_to_p2pkh(hash, main = False): assert len(hash) == 20 @@ -95,22 +50,3 @@ def check_script(script): if (type(script) is bytes or type(script) is CScript): return script assert False - - -class TestFrameworkScript(unittest.TestCase): - def test_base58encodedecode(self): - def check_base58(data, version): - self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version)) - - check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111) - check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111) - check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0) - check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0) - check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) diff --git a/test/functional/test_framework/base58.py b/test/functional/test_framework/base58.py new file mode 100644 index 0000000000000..3dab3569d0c1c --- /dev/null +++ b/test/functional/test_framework/base58.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# Copyright (c) 2016-2020 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Encode BASE58.""" + +import unittest + +from .messages import hash256 +from .util import hex_str_to_bytes, assert_equal + +chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + +def byte_to_base58(b, version): + result = '' + str = b.hex() + str = chr(version).encode('latin-1').hex() + str + checksum = hash256(hex_str_to_bytes(str)).hex() + str += checksum[:8] + value = int('0x'+str,0) + while value > 0: + result = chars[value % 58] + result + value //= 58 + while (str[:2] == '00'): + result = chars[0] + result + str = str[2:] + return result + +def base58_to_byte(s, verify_checksum=True): + if not s: + return b'' + n = 0 + for c in s: + n *= 58 + assert c in chars + digit = chars.index(c) + n += digit + h = '%x' % n + if len(h) % 2: + h = '0' + h + res = n.to_bytes((n.bit_length() + 7) // 8, 'big') + pad = 0 + for c in s: + if c == chars[0]: + pad += 1 + else: + break + res = b'\x00' * pad + res + if verify_checksum: + assert_equal(hash256(res[:-4])[:4], res[-4:]) + + return res[1:-4], int(res[0]) + +class TestFrameworkScript(unittest.TestCase): + def test_base58encodedecode(self): + def check_base58(data, version): + self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version)) + + check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111) + check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111) + check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0) + check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0) + check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) diff --git a/test/functional/test_framework/key.py b/test/functional/test_framework/key.py index 9529198809d0b..4f571d952df10 100644 --- a/test/functional/test_framework/key.py +++ b/test/functional/test_framework/key.py @@ -14,7 +14,7 @@ import unittest from test_framework.crypto import secp256k1 -from .address import byte_to_base58 +from .base58 import byte_to_base58 # Order of the secp256k1 curve ORDER = secp256k1.GE.ORDER diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 56f0faf9d6a28..9362afb88c388 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -70,7 +70,7 @@ # List of framework modules containing unit tests. Should be kept in sync with # the output of `git grep unittest.TestCase ./test/functional/test_framework` TEST_FRAMEWORK_MODULES = [ - "address", + "base58", "crypto.bip324_cipher", "blocktools", "crypto.chacha20", From a1e3885a682b52af331ffb979d319bef7a2f9d8b Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 11 Jun 2020 12:49:30 -0400 Subject: [PATCH 03/12] Merge #19239: tests: move generate_wif_key to wallet_util.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3a83a01694160f2e722e1bc90a328bd569b8e109 [tests] move generate_wif_key to wallet_util.py (John Newbery) b216b0b71f7853d747af8b712fc250c699f3c320 [tests] sort imports in rpc_createmultisig.py (John Newbery) e38081846d889fcbbbc6ca4a4d3bca26807fde2f Revert "[TESTS] Move base58 to own module to break circular dependency" (John Newbery) Pull request description: generate_wif_key is a wallet utility function. Move it from the EC key module to the wallet util module. This fixes the circular dependency issue in #17977 ACKs for top commit: MarcoFalke: ACK 3a83a01694160f2e722e1bc90a328bd569b8e109 🍪 Tree-SHA512: 24985dffb75202721ccc0c6c5b52f1fa5d1ce7963bccde24389feb913cab4dad0c265274ca67892c46c8b64e6a065a0f23263a89be4fb9134dfefbdbe5c7238a --- test/functional/rpc_createmultisig.py | 14 ++-- test/functional/test_framework/address.py | 71 ++++++++++++++++++- test/functional/test_framework/base58.py | 70 ------------------ test/functional/test_framework/key.py | 12 ---- test/functional/test_framework/wallet_util.py | 17 +++-- test/functional/test_runner.py | 2 +- 6 files changed, 90 insertions(+), 96 deletions(-) delete mode 100644 test/functional/test_framework/base58.py diff --git a/test/functional/rpc_createmultisig.py b/test/functional/rpc_createmultisig.py index 26a83d655f698..9fbf29f78b076 100755 --- a/test/functional/rpc_createmultisig.py +++ b/test/functional/rpc_createmultisig.py @@ -3,21 +3,21 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test multisig RPCs""" +import binascii +import decimal +import itertools +import json +import os from test_framework.authproxy import JSONRPCException from test_framework.descriptors import descsum_create, drop_origins +from test_framework.key import ECPubKey, ECKey from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_raises_rpc_error, assert_equal, ) -from test_framework.key import ECPubKey, ECKey, bytes_to_wif - -import binascii -import decimal -import itertools -import json -import os +from test_framework.wallet_util import bytes_to_wif class RpcCreateMultiSigTest(BitcoinTestFramework): def set_test_params(self): diff --git a/test/functional/test_framework/address.py b/test/functional/test_framework/address.py index 441482ca9e291..36220172881ea 100644 --- a/test/functional/test_framework/address.py +++ b/test/functional/test_framework/address.py @@ -9,16 +9,64 @@ # This file encodes and decodes BASE58 P2PKH and P2SH addresses # -from .base58 import byte_to_base58 -from .script import hash160, CScript +import unittest + +from .script import hash160, hash256, CScript from .util import hex_str_to_bytes +from test_framework.util import assert_equal + # Note unlike in bitcoin, this address isn't bech32 since we don't (at this time) support bech32. ADDRESS_BCRT1_UNSPENDABLE = 'yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9' ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk' ADDRESS_BCRT1_P2SH_OP_TRUE = '8zJctvfrzGZ5s1zQ3kagwyW1DsPYSQ4V2P' +chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + + +def byte_to_base58(b, version): + result = '' + str = b.hex() + str = chr(version).encode('latin-1').hex() + str + checksum = hash256(hex_str_to_bytes(str)).hex() + str += checksum[:8] + value = int('0x'+str,0) + while value > 0: + result = chars[value % 58] + result + value //= 58 + while (str[:2] == '00'): + result = chars[0] + result + str = str[2:] + return result + + +def base58_to_byte(s, verify_checksum=True): + if not s: + return b'' + n = 0 + for c in s: + n *= 58 + assert c in chars + digit = chars.index(c) + n += digit + h = '%x' % n + if len(h) % 2: + h = '0' + h + res = n.to_bytes((n.bit_length() + 7) // 8, 'big') + pad = 0 + for c in s: + if c == chars[0]: + pad += 1 + else: + break + res = b'\x00' * pad + res + if verify_checksum: + assert_equal(hash256(res[:-4])[:4], res[-4:]) + + return res[1:-4], int(res[0]) + + def keyhash_to_p2pkh(hash, main = False): assert len(hash) == 20 version = 76 if main else 140 @@ -50,3 +98,22 @@ def check_script(script): if (type(script) is bytes or type(script) is CScript): return script assert False + + +class TestFrameworkScript(unittest.TestCase): + def test_base58encodedecode(self): + def check_base58(data, version): + self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version)) + + check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111) + check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111) + check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) + check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0) + check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0) + check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) + check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) diff --git a/test/functional/test_framework/base58.py b/test/functional/test_framework/base58.py deleted file mode 100644 index 3dab3569d0c1c..0000000000000 --- a/test/functional/test_framework/base58.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) 2016-2020 The Bitcoin Core developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -"""Encode BASE58.""" - -import unittest - -from .messages import hash256 -from .util import hex_str_to_bytes, assert_equal - -chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' - -def byte_to_base58(b, version): - result = '' - str = b.hex() - str = chr(version).encode('latin-1').hex() + str - checksum = hash256(hex_str_to_bytes(str)).hex() - str += checksum[:8] - value = int('0x'+str,0) - while value > 0: - result = chars[value % 58] + result - value //= 58 - while (str[:2] == '00'): - result = chars[0] + result - str = str[2:] - return result - -def base58_to_byte(s, verify_checksum=True): - if not s: - return b'' - n = 0 - for c in s: - n *= 58 - assert c in chars - digit = chars.index(c) - n += digit - h = '%x' % n - if len(h) % 2: - h = '0' + h - res = n.to_bytes((n.bit_length() + 7) // 8, 'big') - pad = 0 - for c in s: - if c == chars[0]: - pad += 1 - else: - break - res = b'\x00' * pad + res - if verify_checksum: - assert_equal(hash256(res[:-4])[:4], res[-4:]) - - return res[1:-4], int(res[0]) - -class TestFrameworkScript(unittest.TestCase): - def test_base58encodedecode(self): - def check_base58(data, version): - self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version)) - - check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111) - check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111) - check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111) - check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0) - check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0) - check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) - check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) diff --git a/test/functional/test_framework/key.py b/test/functional/test_framework/key.py index 4f571d952df10..fc3ef17f7d1b8 100644 --- a/test/functional/test_framework/key.py +++ b/test/functional/test_framework/key.py @@ -14,7 +14,6 @@ import unittest from test_framework.crypto import secp256k1 -from .base58 import byte_to_base58 # Order of the secp256k1 curve ORDER = secp256k1.GE.ORDER @@ -185,17 +184,6 @@ def sign_ecdsa(self, msg, low_s=True, rfc6979=False): sb = s.to_bytes((s.bit_length() + 8) // 8, 'big') return b'\x30' + bytes([4 + len(rb) + len(sb), 2, len(rb)]) + rb + bytes([2, len(sb)]) + sb -def bytes_to_wif(b, compressed=True): - if compressed: - b += b'\x01' - return byte_to_base58(b, 239) - -def generate_wif_key(): - # Makes a WIF privkey for imports - k = ECKey() - k.generate() - return bytes_to_wif(k.get_bytes(), k.is_compressed) - def compute_xonly_pubkey(key): """Compute an x-only (32 byte) public key from a (32 byte) private key. diff --git a/test/functional/test_framework/wallet_util.py b/test/functional/test_framework/wallet_util.py index af8b0eca945f2..c3ad36bb7e4da 100755 --- a/test/functional/test_framework/wallet_util.py +++ b/test/functional/test_framework/wallet_util.py @@ -6,13 +6,11 @@ from collections import namedtuple from test_framework.address import ( + byte_to_base58, key_to_p2pkh, script_to_p2sh, ) -from test_framework.key import ( - bytes_to_wif, - ECKey, -) +from test_framework.key import ECKey from test_framework.script import ( CScript, OP_2, @@ -90,3 +88,14 @@ def test_address(node, address, **kwargs): 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 bytes_to_wif(b, compressed=True): + if compressed: + b += b'\x01' + return byte_to_base58(b, 239) + +def generate_wif_key(): + # Makes a WIF privkey for imports + k = ECKey() + k.generate() + return bytes_to_wif(k.get_bytes(), k.is_compressed) diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 9362afb88c388..56f0faf9d6a28 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -70,7 +70,7 @@ # List of framework modules containing unit tests. Should be kept in sync with # the output of `git grep unittest.TestCase ./test/functional/test_framework` TEST_FRAMEWORK_MODULES = [ - "base58", + "address", "crypto.bip324_cipher", "blocktools", "crypto.chacha20", From 9600f9ca350a190ccaafc52e6b9c7247151ede07 Mon Sep 17 00:00:00 2001 From: Samuel Dobson Date: Sun, 12 Jul 2020 00:04:53 +1200 Subject: [PATCH 04/12] Merge #19441: walletdb: don't reinitialize desc cache with multiple cache entries a66a7a1a7060bb422eba3b8c214852416c4280d1 walletdb: don't reinitialize desc cache with multiple cache entries (Andrew Chow) Pull request description: When loading descriptor caches, we would accidentally reinitialize the descriptor cache when seeing that one already exists. This should have only been initializing the cache when one does not exist. However this code itself is unnecessary as the act of looking up the cache to add to it will initialize it if it didn't already exist. This issue could be hit by trying to load a wallet that had imported a multisig descriptor. The wallet would fail to load. A test has been added to wallet_importdescriptors.py to catch this case. Another test case has also been added to check that loading a wallet with only single key descriptors works. ACKs for top commit: hugohn: tACK [a66a7a1](https://github.com/bitcoin/bitcoin/commit/a66a7a1a7060bb422eba3b8c214852416c4280d1) jonatack: ACK a66a7a1a706 meshcollider: Code review ACK a66a7a1a7060bb422eba3b8c214852416c4280d1 Tree-SHA512: 3df746421a008708eaa3bbbdd12b9ddd3e2ec111d54625a212dca7414b971cc1f6e2b1757b3232c31a2f637d1b1ef43bf3ffa4ac4216646cf1e92db5f79954f1 --- src/wallet/walletdb.cpp | 3 --- test/functional/wallet_importdescriptors.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 27499274c3075..3c9d2394bd25e 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -605,9 +605,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> ser_xpub; CExtPubKey xpub; xpub.Decode(ser_xpub.data()); - if (wss.m_descriptor_caches.count(desc_id)) { - wss.m_descriptor_caches[desc_id] = DescriptorCache(); - } if (parent) { wss.m_descriptor_caches[desc_id].CacheParentExtPubKey(key_exp_index, xpub); } else { diff --git a/test/functional/wallet_importdescriptors.py b/test/functional/wallet_importdescriptors.py index b5ca1db5a7f27..f3cc96db82879 100755 --- a/test/functional/wallet_importdescriptors.py +++ b/test/functional/wallet_importdescriptors.py @@ -98,6 +98,15 @@ def run_test(self): labels=["Descriptor import test"]) assert_equal(w1.getwalletinfo()['keypoolsize'], 0) + # Check persistence of data and that loading works correctly + w1.unloadwallet() + self.nodes[1].loadwallet('w1') + test_address(w1, + key.p2pkh_addr, + solvable=True, + ismine=True, + labels=["Descriptor import test"]) + self.log.info("Internal addresses cannot have labels") self.test_importdesc({"desc": descsum_create("pkh(" + key.pubkey + ")"), "timestamp": "now", @@ -365,6 +374,10 @@ def run_test(self): self.sync_all() assert_equal(wmulti_pub.getbalance(), wmulti_priv.getbalance()) + # Make sure that descriptor wallets containing multiple xpubs in a single descriptor load correctly + wmulti_pub.unloadwallet() + self.nodes[1].loadwallet('wmulti_pub') + self.log.info("Multisig with distributed keys") self.nodes[1].createwallet(wallet_name="wmulti_priv1", descriptors=True) wmulti_priv1 = self.nodes[1].get_wallet_rpc("wmulti_priv1") From 3575a6c4b10110818148fd23ec2f5942793c803a Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 14 Aug 2020 15:32:04 +0800 Subject: [PATCH 05/12] Merge #19568: Wallet should not override signing errors e7448d66803f42984018ef8dfa6699027cb9536d wallet: Don't override signing errors (Fabian Jahr) Pull request description: While reviewing #17204 I noticed that the errors in `input_errors` from `::SignTransaction` where being overridden by `CWallet::SignTransaction`. For example, a Script related error led to incomplete signature data which led to `CWallet::SignTransaction` reporting that keys were missing, which was a less precise error than the original one. Additionally, the error `"Input not found or already spent"` is [duplicated in `sign.cpp`](https://github.com/bitcoin/bitcoin/blob/c7b4968552c704f1e2e9a046911e1207f5af5fe0/src/script/sign.cpp#L481), so the error here is redundant at the moment. So technically the whole error block could be removed, I think. However, this code is affected by the ongoing work on the wallet so there might be a reason why these errors are here. But even if there is a reason to keep them, I don't think existing, potentially more precise errors should be overridden here unless we want to hide them from the users. I am looking for feedback if this is a work in progress state where these errors could be more useful in the future or if they can be removed. On testing: even though [the errors in `CWallet` are covered](https://marcofalke.github.io/btc_cov/total.coverage/src/wallet/wallet.cpp.gcov.html), all tests still pass after removing them. I am not sure if there is a desire to cover these specific error messages, tests in `test/functional/rpc_signrawtransaction.py` seem to aim for a more generic approach. ACKs for top commit: achow101: ACK e7448d66803f42984018ef8dfa6699027cb9536d meshcollider: Code review ACK e7448d66803f42984018ef8dfa6699027cb9536d Tree-SHA512: 3e2bc11d05379d2aef87b093a383d1b044787efc70e35955b2f8ecd028b6acef02f386180566af6a1a63193635f5d685466e2f6141c96326c49ffc5c81ca3e23 --- src/wallet/wallet.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d56ac2adf7426..7f794b74d5499 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2960,23 +2960,6 @@ bool CWallet::SignTransaction(CMutableTransaction& tx, const std::mapsecond.IsSpent()) { - input_errors[i] = "Input not found or already spent"; - continue; - } - - // Check if this input is complete - SignatureData sigdata = DataFromTransaction(tx, i, coin->second.out); - if (!sigdata.complete) { - input_errors[i] = "Unable to sign input, missing keys"; - continue; - } - } return false; } From 59c30e610bcc33d965f8fc007307bc9cc9905a4c Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Mon, 26 Feb 2024 02:29:23 +0700 Subject: [PATCH 06/12] fix: add missing dashification for bitcoin#18067 --- src/wallet/scriptpubkeyman.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 39c1446300f86..ffd4ed1887172 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -77,8 +77,8 @@ bool HaveKeys(const std::vector& pubkeys, const LegacyScriptPubKeyMan& //! //! @param keystore legacy key and script store //! @param script script to solve -//! @param sigversion script type (top-level / redeemscript / witnessscript) -//! @param recurse_scripthash whether to recurse into nested p2sh and p2wsh +//! @param sigversion script type (top-level / redeemscript) +//! @param recurse_scripthash whether to recurse into nested p2sh //! scripts or simply treat any script that has been //! stored in the keystore as spendable IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion, bool recurse_scripthash=true) From 56d9fe9510a7a86be368681b10378f50ed1b25a2 Mon Sep 17 00:00:00 2001 From: Samuel Dobson Date: Thu, 28 Jan 2021 13:16:02 +1300 Subject: [PATCH 07/12] Merge #20226: wallet, rpc: add listdescriptors command 647b81b70938dc4dbcf32399c56f78be395c721a wallet, rpc: add listdescriptors command (Ivan Metlushko) Pull request description: Looking for concept ACKs **Rationale**: allow users to inspect the contents of their newly created descriptor wallets. Currently the command only returns xpubs which is not very useful in itself, but there are multiples ways to extend it: * add an option to export xprv * with #19136 it'll be possible to return normalised descriptors suitable for a watch-only purposes The output is compatible with `importdescriptors` command so it could be easily used for backup/recover purposes. **Output example:** ```json [ { "desc": "wpkh(tpubD6NzVbkrYhZ4WW6E2ZETFyNfq2hfF23SKxqSGFvUpPAY58jmmuBybwqwFihAyQPk9KnwTt5516NDZRJ7k5QPeKjy7wuVd5WvXNxwwAs5tUD/*)#nhavpr5h", "timestamp": 1296688602, "active": false, "range": [ 0, 999 ], "next": 0 } ] ``` ACKs for top commit: jonatack: re-ACK 647b81b70938dc4dbcf32399c56f78be395c721a rebased to master, debug builds cleanly, reviewed diff since last review, tested with a descriptor wallet (and with a legacy wallet) achow101: re-ACK 647b81b Tree-SHA512: 51a3620bb17c836c52cecb066d4fa9d5ff418af56809046eaee0528c4dc240a4e90fff5711ba96e399c6664e00b9ee8194e33852b1b9e75af18061296e19a8a7 --- src/wallet/rpcdump.cpp | 69 +++++++++++++++++++++++ src/wallet/rpcwallet.cpp | 2 + test/functional/test_runner.py | 1 + test/functional/wallet_listdescriptors.py | 67 ++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100755 test/functional/wallet_listdescriptors.py diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index a21173e42c071..dfb1ec1139d8c 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1908,3 +1908,72 @@ UniValue importdescriptors(const JSONRPCRequest& main_request) { return response; } + +RPCHelpMan listdescriptors() +{ + return RPCHelpMan{ + "listdescriptors", + "\nList descriptors imported into a descriptor-enabled wallet.", + {}, + RPCResult{ + RPCResult::Type::ARR, "", "Response is an array of descriptor objects", + { + {RPCResult::Type::OBJ, "", "", { + {RPCResult::Type::STR, "desc", "Descriptor string representation"}, + {RPCResult::Type::NUM, "timestamp", "The creation time of the descriptor"}, + {RPCResult::Type::BOOL, "active", "Activeness flag"}, + {RPCResult::Type::BOOL, "internal", true, "Whether this is internal or external descriptor; defined only for active descriptors"}, + {RPCResult::Type::ARR_FIXED, "range", true, "Defined only for ranged descriptors", { + {RPCResult::Type::NUM, "", "Range start inclusive"}, + {RPCResult::Type::NUM, "", "Range end inclusive"}, + }}, + {RPCResult::Type::NUM, "next", true, "The next index to generate addresses from; defined only for ranged descriptors"}, + }}, + } + }, + RPCExamples{ + HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "") + }, + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue +{ + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + if (!wallet) return NullUniValue; + + if (!wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { + throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets"); + } + + LOCK(wallet->cs_wallet); + + UniValue response(UniValue::VARR); + const auto active_spk_mans = wallet->GetActiveScriptPubKeyMans(); + for (const auto& spk_man : wallet->GetAllScriptPubKeyMans()) { + const auto desc_spk_man = dynamic_cast(spk_man); + if (!desc_spk_man) { + throw JSONRPCError(RPC_WALLET_ERROR, "Unexpected ScriptPubKey manager type."); + } + UniValue spk(UniValue::VOBJ); + LOCK(desc_spk_man->cs_desc_man); + const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor(); + spk.pushKV("desc", wallet_descriptor.descriptor->ToString()); + spk.pushKV("timestamp", wallet_descriptor.creation_time); + const bool active = active_spk_mans.count(desc_spk_man) != 0; + spk.pushKV("active", active); + const auto& type = wallet_descriptor.descriptor->GetOutputType(); + if (active && type != std::nullopt) { + spk.pushKV("internal", wallet->GetScriptPubKeyMan(true) == desc_spk_man); + } + if (wallet_descriptor.descriptor->IsRange()) { + UniValue range(UniValue::VARR); + range.push_back(wallet_descriptor.range_start); + range.push_back(wallet_descriptor.range_end - 1); + spk.pushKV("range", range); + spk.pushKV("next", wallet_descriptor.next_index); + } + response.push_back(spk); + } + + return response; +}, + }; +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 727612554256a..7dfa1021678b2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4183,6 +4183,7 @@ UniValue importprunedfunds(const JSONRPCRequest& request); UniValue removeprunedfunds(const JSONRPCRequest& request); UniValue importmulti(const JSONRPCRequest& request); UniValue importdescriptors(const JSONRPCRequest& request); +RPCHelpMan listdescriptors(); UniValue dumphdinfo(const JSONRPCRequest& request); UniValue importelectrumwallet(const JSONRPCRequest& request); @@ -4443,6 +4444,7 @@ static const CRPCCommand commands[] = { "wallet", "keypoolrefill", &keypoolrefill, {"newsize"} }, { "wallet", "listaddressbalances", &listaddressbalances, {"minamount"} }, { "wallet", "listaddressgroupings", &listaddressgroupings, {} }, + { "wallet", "listdescriptors", &listdescriptors, {} }, { "wallet", "listlabels", &listlabels, {"purpose"} }, { "wallet", "listlockunspent", &listlockunspent, {} }, { "wallet", "listreceivedbyaddress", &listreceivedbyaddress, {"minconf","addlocked","include_empty","include_watchonly","address_filter"} }, diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 56f0faf9d6a28..09778e7aad255 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -245,6 +245,7 @@ 'mining_basic.py', 'rpc_named_arguments.py', 'wallet_listsinceblock.py', + 'wallet_listdescriptors.py --descriptors', 'p2p_leak.py', 'p2p_compactblocks.py', 'p2p_connect_to_devnet.py', diff --git a/test/functional/wallet_listdescriptors.py b/test/functional/wallet_listdescriptors.py new file mode 100755 index 0000000000000..c44c7ef5c229c --- /dev/null +++ b/test/functional/wallet_listdescriptors.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# Copyright (c) 2014-2021 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Test the listdescriptors RPC.""" + +from test_framework.descriptors import ( + descsum_create +) +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import ( + assert_equal, + assert_raises_rpc_error, +) + + +class ListDescriptorsTest(BitcoinTestFramework): + def set_test_params(self): + self.num_nodes = 1 + + def skip_test_if_missing_module(self): + self.skip_if_no_wallet() + self.skip_if_no_sqlite() + + # do not create any wallet by default + def init_wallet(self, i): + return + + def run_test(self): + node = self.nodes[0] + assert_raises_rpc_error(-18, 'No wallet is loaded.', node.listdescriptors) + + self.log.info('Test that the command is not available for legacy wallets.') + node.createwallet(wallet_name='w1', descriptors=False) + assert_raises_rpc_error(-4, 'listdescriptors is not available for non-descriptor wallets', node.listdescriptors) + + self.log.info('Test the command for empty descriptors wallet.') + node.createwallet(wallet_name='w2', blank=True, descriptors=True) + assert_equal(0, len(node.get_wallet_rpc('w2').listdescriptors())) + + self.log.info('Test the command for a default descriptors wallet.') + node.createwallet(wallet_name='w3', descriptors=True) + result = node.get_wallet_rpc('w3').listdescriptors() + assert_equal(2, len(result)) + assert_equal(2, len([d for d in result if d['active']])) + assert_equal(1, len([d for d in result if d['internal']])) + for item in result: + assert item['desc'] != '' + assert item['next'] == 0 + assert item['range'] == [0, 0] + assert item['timestamp'] is not None + + self.log.info('Test non-active non-range combo descriptor') + node.createwallet(wallet_name='w4', blank=True, descriptors=True) + wallet = node.get_wallet_rpc('w4') + wallet.importdescriptors([{ + 'desc': descsum_create('combo(' + node.get_deterministic_priv_key().key + ')'), + 'timestamp': 1296688602, + }]) + expected = [{'active': False, + 'desc': 'combo(038af19f35924e37ad7c3c8045d1e19b9b90b7310e08b892e620c253a102fe49f0)#2j2j0825', + 'timestamp': 1296688602}] + assert_equal(expected, wallet.listdescriptors()) + + +if __name__ == '__main__': + ListDescriptorsTest().main() From da2a7ed6d788d3da695b62e856457bd1554cc73a Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 4 Feb 2021 08:51:34 +0100 Subject: [PATCH 08/12] Merge #21049: Add release notes for listdescriptors RPC 51f3752fbeee09d025db33e154bb2efff9e20837 Add release notes for listdescriptors RPC (Ivan Metlushko) Pull request description: Original PR is #20226 ACKs for top commit: jonatack: ACK 51f3752 jonasschnelli: ACK 51f3752fbeee09d025db33e154bb2efff9e20837 Tree-SHA512: e8091d01b99a3effcd6c1738e7363a44858ba9bcf6bd99bf60f2025a25db83fc8d61354ab2002365b56071b9f3693c7d534153a259b5ebc91cbcf8d13f6555f1 --- doc/release-notes-21049.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 doc/release-notes-21049.md diff --git a/doc/release-notes-21049.md b/doc/release-notes-21049.md new file mode 100644 index 0000000000000..52c3618c05306 --- /dev/null +++ b/doc/release-notes-21049.md @@ -0,0 +1,6 @@ +Wallet +------ + +- A new `listdescriptors` RPC is available to inspect the contents of descriptor-enabled wallets. + The RPC returns public versions of all imported descriptors, including their timestamp and flags. + For ranged descriptors, it also returns the range boundaries and the next index to generate addresses from. (dash#5911) From 953b6706f4c2c7f27b6b259865ea52e56473e3de Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 2 Nov 2020 16:54:06 +0100 Subject: [PATCH 09/12] Merge #18788: tests: Update more tests to work with descriptor wallets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit c7b7e0a69265946aecc885be911c7650911ba2e3 tests: Make only desc wallets for wallet_multwallet.py --descriptors (Andrew Chow) d4b67ad214ada7645c4ce2d5ec336fe5c3f7f7ca Avoid creating legacy wallets in wallet_importdescriptors.py (Andrew Chow) 6c9c12bf87f95066acc28ea2270a00196eb77703 Update feature_backwards_compatibility for descriptor wallets (Andrew Chow) 9a4c631e1c00eb1661c000978b133d7aa0226290 Update wallet_labels.py to not require descriptors=False (Andrew Chow) 242aed7cc1d003e8fed574bbebd19c7e54e23402 tests: Add a --legacy-wallet that is mutually exclusive with --descriptors (Andrew Chow) 388053e1722632c2e485c56a444bc75cf0152188 Disable some tests for tool_wallet when descriptors (Andrew Chow) 47d3243160fdec7e464cfb8f869be7f5d4ee25fe Make raw multisig tests legacy wallet only in rpc_rawtransaction.py (Andrew Chow) 59d3da5bce4ebd9c2291d8f201a53ee087938b21 Do addmultisigaddress tests in legacy wallet mode in wallet_address_types.py (Andrew Chow) 25bc5dccbfd52691adca6edd418dd54290300c28 Use importdescriptors when in descriptor wallet mode in wallet_createwallet.py (Andrew Chow) 0bd1860300b13b12a25d330ba3a93ff2d13aa379 Avoid dumpprivkey and watchonly behavior in rpc_signrawtransaction.py (Andrew Chow) 08067aebfd7e838e6ce6b030c31a69422260fc6f Add script equivalent of functions in address.py (Andrew Chow) 86968882a8a26312a7af29c572313c4aff488c11 Add descriptor wallet output to tool_wallet.py (Andrew Chow) 3457679870e8eff2a7d14fe59a479692738c48b6 Use separate watchonly wallet for multisig in feature_nulldummy.py (Andrew Chow) a42652ec10c733a5bf37e418e45d4841f54331b4 Move import and watchonly tests to be legacy wallet only in wallet_balance.py (Andrew Chow) 4b871909d6e4a51888e062d322bf53263deda15e Use importdescriptors for descriptor wallets in wallet_bumpfee.py (Andrew Chow) c2711e4230d9a423ead24f6609691fb338b1d26b Avoid dumpprivkey in wallet_listsinceblock.py (Andrew Chow) 553dbf9af4dea96e6a3e79bba9607003342029bd Make import tests in wallet_listtransactions.py legacy wallet only (Andrew Chow) dc81418fd01021070f3f66bab5fee1484456691a Use a separate watchonly wallet in rpc_fundrawtransaction.py (Andrew Chow) a357111047411f18c156cd34a002a38430f2901c Update wallet_importprunedfunds to avoid dumpprivkey (Andrew Chow) Pull request description: I went through all the tests and checked whether they passed with descriptor wallets. This partially informed some changes in #16528. Some tests needed changes to work with descriptor wallets. These were primarily due to import and watchonly behavior. There are some tests and test cases that only test legacy wallet behavior so those tests won't be run with descriptor wallets. This PR updates more tests to have to the `--descriptors` switch in `test_runner.py`. Additionally a mutually exclusive `--legacy-wallet` option has been added to force legacy wallets. This does nothing currently but will be useful in the future when descriptor wallets are the default. For the tests that rely on legacy wallet behavior, this option is being set so that we don't forget in the future. Those tests are `feature_segwit.py`, `wallet_watchonly.py`, `wallet_implicitsegwit.py`, `wallet_import_with_label.py`, and `wallet_import_with_label.py`. If you invert the `--descriptors`/`--legacy-wallet` default so that descriptor wallets are the default, all tests (besides the legacy wallet specific ones) will pass. ACKs for top commit: MarcoFalke: review ACK c7b7e0a69265946aecc885be911c7650911ba2e3 🎿 laanwj: ACK c7b7e0a69265946aecc885be911c7650911ba2e3 Tree-SHA512: 2f4e87815005d1d0a2543ea7947f7cd7593d8cf5312228ef85f8e096f19739b225769961943049cb44f6f07a35b8de988e2246ab9aca5bb5a0b2e62694d5637d --- .../feature_backwards_compatibility.py | 171 +++++++------ test/functional/feature_nulldummy.py | 11 +- test/functional/rpc_fundrawtransaction.py | 89 +++++-- test/functional/rpc_rawtransaction.py | 240 ++++++++++-------- test/functional/test_framework/blocktools.py | 23 +- test/functional/test_framework/script_util.py | 33 ++- .../test_framework/test_framework.py | 8 +- test/functional/test_runner.py | 32 ++- test/functional/tool_wallet.py | 94 ++++--- test/functional/wallet_balance.py | 43 ++-- test/functional/wallet_createwallet.py | 60 ++++- test/functional/wallet_importdescriptors.py | 3 +- test/functional/wallet_importprunedfunds.py | 53 ++-- test/functional/wallet_labels.py | 4 +- test/functional/wallet_listsinceblock.py | 19 +- test/functional/wallet_listtransactions.py | 26 +- 16 files changed, 576 insertions(+), 333 deletions(-) diff --git a/test/functional/feature_backwards_compatibility.py b/test/functional/feature_backwards_compatibility.py index f786ed02e2b15..cfcb70e2aa31c 100755 --- a/test/functional/feature_backwards_compatibility.py +++ b/test/functional/feature_backwards_compatibility.py @@ -24,6 +24,7 @@ from test_framework.util import ( assert_equal, + assert_raises_rpc_error, ) @@ -82,7 +83,7 @@ def run_test(self): # w1: regular wallet, created on master: update this test when default # wallets can no longer be opened by older versions. - node_master.rpc.createwallet(wallet_name="w1") + node_master.createwallet(wallet_name="w1") wallet = node_master.get_wallet_rpc("w1") info = wallet.getwalletinfo() assert info['private_keys_enabled'] @@ -101,7 +102,7 @@ def run_test(self): assert info['private_keys_enabled'] assert info['keypoolsize'] > 0 # Use addmultisigaddress (see #18075) - address_18075 = wallet.rpc.addmultisigaddress(1, ["0296b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52", "037211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073"], "")["address"] + address_18075 = wallet.addmultisigaddress(1, ["0296b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52", "037211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073"], "")["address"] assert wallet.getaddressinfo(address_18075)["solvable"] # w1_v18: regular wallet, created with v0.18 @@ -217,82 +218,100 @@ def run_test(self): os.path.join(node_v20_wallets_dir, wallet) ) - # Open the wallets in v0.20 - node_v20.loadwallet("w1") - wallet = node_v20.get_wallet_rpc("w1") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] > 0 - txs = wallet.listtransactions() - assert_equal(len(txs), 1) - - node_v20.loadwallet("w2") - wallet = node_v20.get_wallet_rpc("w2") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] == False - assert info['keypoolsize'] == 0 - - node_v20.loadwallet("w3") - wallet = node_v20.get_wallet_rpc("w3") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] == 0 - - # Open the wallets in v0.19 - node_v19.loadwallet("w1") - wallet = node_v19.get_wallet_rpc("w1") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] > 0 - txs = wallet.listtransactions() - assert_equal(len(txs), 1) - - node_v19.loadwallet("w2") - wallet = node_v19.get_wallet_rpc("w2") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] == False - assert info['keypoolsize'] == 0 - - node_v19.loadwallet("w3") - wallet = node_v19.get_wallet_rpc("w3") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] == 0 - - # Open the wallets in v0.18 - node_v18.loadwallet("w1") - wallet = node_v18.get_wallet_rpc("w1") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] > 0 - txs = wallet.listtransactions() - assert_equal(len(txs), 1) - - node_v18.loadwallet("w2") - wallet = node_v18.get_wallet_rpc("w2") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] == False - assert info['keypoolsize'] == 0 - - node_v18.loadwallet("w3") - wallet = node_v18.get_wallet_rpc("w3") - info = wallet.getwalletinfo() - assert info['private_keys_enabled'] - assert info['keypoolsize'] == 0 + if not self.options.descriptors: + # Descriptor wallets break compatibility, only run this test for legacy wallet + # Open the wallets in v0.20 + node_v20.loadwallet("w1") + wallet = node_v20.get_wallet_rpc("w1") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] > 0 + txs = wallet.listtransactions() + assert_equal(len(txs), 1) + + node_v20.loadwallet("w2") + wallet = node_v20.get_wallet_rpc("w2") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] == False + assert info['keypoolsize'] == 0 + + node_v20.loadwallet("w3") + wallet = node_v20.get_wallet_rpc("w3") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] == 0 + + # Open the wallets in v0.19 + node_v19.loadwallet("w1") + wallet = node_v19.get_wallet_rpc("w1") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] > 0 + txs = wallet.listtransactions() + assert_equal(len(txs), 1) + + node_v19.loadwallet("w2") + wallet = node_v19.get_wallet_rpc("w2") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] == False + assert info['keypoolsize'] == 0 + + node_v19.loadwallet("w3") + wallet = node_v19.get_wallet_rpc("w3") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] == 0 + + # Open the wallets in v0.18 + node_v18.loadwallet("w1") + wallet = node_v18.get_wallet_rpc("w1") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] > 0 + txs = wallet.listtransactions() + assert_equal(len(txs), 1) + + node_v18.loadwallet("w2") + wallet = node_v18.get_wallet_rpc("w2") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] == False + assert info['keypoolsize'] == 0 + + node_v18.loadwallet("w3") + wallet = node_v18.get_wallet_rpc("w3") + info = wallet.getwalletinfo() + assert info['private_keys_enabled'] + assert info['keypoolsize'] == 0 + + node_v17.loadwallet("w1") + wallet = node_v17.get_wallet_rpc("w1") + info = wallet.getwalletinfo() + # doesn't have private_keys_enabled in v17 + #assert info['private_keys_enabled'] + assert info['keypoolsize'] > 0 + + node_v17.loadwallet("w2") + wallet = node_v17.get_wallet_rpc("w2") + info = wallet.getwalletinfo() + # doesn't have private_keys_enabled in v17 + # TODO enable back when HD wallets are created by default + # assert info['private_keys_enabled'] == False + # assert info['keypoolsize'] == 0 + else: + # Descriptor wallets appear to be corrupted wallets to old software + assert_raises_rpc_error(-4, "Wallet requires newer version of Dash Core", node_v19.loadwallet, "w1") + node_v19.loadwallet("w2") + node_v19.loadwallet("w3") + assert_raises_rpc_error(-18, "Data is not in recognized format", node_v18.loadwallet, "w1") + node_v18.loadwallet("w2") + node_v18.loadwallet("w3") # Open the wallets in v0.17 node_v17.loadwallet("w1_v18") wallet = node_v17.get_wallet_rpc("w1_v18") info = wallet.getwalletinfo() # doesn't have private_keys_enabled in v17 - #assert info['private_keys_enabled'] - assert info['keypoolsize'] > 0 - - node_v17.loadwallet("w1") - wallet = node_v17.get_wallet_rpc("w1") - info = wallet.getwalletinfo() - # doesn't have private_keys_enabled in v17 - #assert info['private_keys_enabled'] + # assert info['private_keys_enabled'] assert info['keypoolsize'] > 0 node_v17.loadwallet("w2_v18") @@ -300,14 +319,6 @@ def run_test(self): info = wallet.getwalletinfo() # doesn't have private_keys_enabled in v17 # TODO enable back when HD wallets are created by default - # assert info['private_keys_enabled'] == False - # assert info['keypoolsize'] == 0 - - node_v17.loadwallet("w2") - wallet = node_v17.get_wallet_rpc("w2") - info = wallet.getwalletinfo() - # doesn't have private_keys_enabled in v17 - # TODO enable back when HD wallets are created by default #assert info['private_keys_enabled'] == False #assert info['keypoolsize'] == 0 diff --git a/test/functional/feature_nulldummy.py b/test/functional/feature_nulldummy.py index acf3a1a87d632..47c2515c707ae 100755 --- a/test/functional/feature_nulldummy.py +++ b/test/functional/feature_nulldummy.py @@ -46,8 +46,15 @@ def skip_test_if_missing_module(self): self.skip_if_no_wallet() def run_test(self): - self.address = self.nodes[0].getnewaddress() - self.ms_address = self.nodes[0].addmultisigaddress(1, [self.address])['address'] + self.nodes[0].createwallet(wallet_name='wmulti', disable_private_keys=True) + wmulti = self.nodes[0].get_wallet_rpc('wmulti') + w0 = self.nodes[0].get_wallet_rpc(self.default_wallet_name) + self.address = w0.getnewaddress() + self.pubkey = w0.getaddressinfo(self.address)['pubkey'] + self.ms_address = wmulti.addmultisigaddress(1, [self.pubkey])['address'] + if not self.options.descriptors: + # Legacy wallets need to import these so that they are watched by the wallet. This is unnecssary (and does not need to be tested) for descriptor wallets + wmulti.importaddress(self.ms_address) self.coinbase_blocks = self.nodes[0].generate(2) # Block 2 coinbase_txid = [] diff --git a/test/functional/rpc_fundrawtransaction.py b/test/functional/rpc_fundrawtransaction.py index c4977efab0611..e0982ce81616e 100755 --- a/test/functional/rpc_fundrawtransaction.py +++ b/test/functional/rpc_fundrawtransaction.py @@ -5,6 +5,7 @@ """Test the fundrawtransaction RPC.""" from decimal import Decimal +from test_framework.descriptors import descsum_create from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -104,17 +105,19 @@ def test_change_position(self): rawmatch = self.nodes[2].fundrawtransaction(rawmatch, {"changePosition":1, "subtractFeeFromOutputs":[0]}) assert_equal(rawmatch["changepos"], -1) + self.nodes[3].createwallet(wallet_name="wwatch", disable_private_keys=True) + wwatch = self.nodes[3].get_wallet_rpc('wwatch') watchonly_address = self.nodes[0].getnewaddress() watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"] self.watchonly_amount = Decimal(2000) - self.nodes[3].importpubkey(watchonly_pubkey, "", True) + wwatch.importpubkey(watchonly_pubkey, "", True) self.watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, self.watchonly_amount) # Lock UTXO so nodes[0] doesn't accidentally spend it self.watchonly_vout = find_vout_for_address(self.nodes[0], self.watchonly_txid, watchonly_address) self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) - self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), self.watchonly_amount / 10) + self.nodes[0].sendtoaddress(self.nodes[3].get_wallet_rpc(self.default_wallet_name).getnewaddress(), self.watchonly_amount / 10) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 15) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10) @@ -123,6 +126,8 @@ def test_change_position(self): self.nodes[0].generate(1) self.sync_all() + wwatch.unloadwallet() + def test_simple(self): self.log.info("Test fundrawtxn") inputs = [ ] @@ -400,7 +405,7 @@ def test_fee_p2sh(self): addr1Obj = self.nodes[1].getaddressinfo(addr1) addr2Obj = self.nodes[1].getaddressinfo(addr2) - mSigObj = self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] + mSigObj = self.nodes[3].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] inputs = [] outputs = {mSigObj:11} @@ -432,7 +437,7 @@ def test_fee_4of5(self): addr4Obj = self.nodes[1].getaddressinfo(addr4) addr5Obj = self.nodes[1].getaddressinfo(addr5) - mSigObj = self.nodes[1].addmultisigaddress( + mSigObj = self.nodes[1].createmultisig( 4, [ addr1Obj['pubkey'], @@ -458,7 +463,7 @@ def test_fee_4of5(self): def test_spend_2of2(self): """Spend a 2-of-2 multisig transaction over fundraw.""" - self.log.info("Test fundrawtxn spending 2-of-2 multisig") + self.log.info("Test fundpsbt spending 2-of-2 multisig") # Create 2-of-2 addr. addr1 = self.nodes[2].getnewaddress() @@ -467,13 +472,18 @@ def test_spend_2of2(self): addr1Obj = self.nodes[2].getaddressinfo(addr1) addr2Obj = self.nodes[2].getaddressinfo(addr2) - mSigObj = self.nodes[2].addmultisigaddress( + self.nodes[2].createwallet(wallet_name='wmulti', disable_private_keys=True) + wmulti = self.nodes[2].get_wallet_rpc('wmulti') + w2 = self.nodes[2].get_wallet_rpc(self.default_wallet_name) + mSigObj = wmulti.addmultisigaddress( 2, [ addr1Obj['pubkey'], addr2Obj['pubkey'], ] )['address'] + if not self.options.descriptors: + wmulti.importaddress(mSigObj) # send 12 DASH to msig addr self.nodes[0].sendtoaddress(mSigObj, 12) @@ -483,22 +493,39 @@ def test_spend_2of2(self): oldBalance = self.nodes[1].getbalance() inputs = [] outputs = {self.nodes[1].getnewaddress():11} - rawtx = self.nodes[2].createrawtransaction(inputs, outputs) - fundedTx = self.nodes[2].fundrawtransaction(rawtx) + funded_psbt = wmulti.walletcreatefundedpsbt(inputs=inputs, outputs=outputs, options={'changeAddress': w2.getrawchangeaddress()})['psbt'] - signedTx = self.nodes[2].signrawtransactionwithwallet(fundedTx['hex']) - self.nodes[2].sendrawtransaction(signedTx['hex']) + signed_psbt = w2.walletprocesspsbt(funded_psbt) + final_psbt = w2.finalizepsbt(signed_psbt['psbt']) + self.nodes[2].sendrawtransaction(final_psbt['hex']) self.nodes[2].generate(1) self.sync_all() # Make sure funds are received at node1. assert_equal(oldBalance+Decimal('11.0000000'), self.nodes[1].getbalance()) + wmulti.unloadwallet() + def test_locked_wallet(self): - self.log.info("Test fundrawtxn with locked wallet") + self.log.info("Test fundrawtxn with locked wallet and hardened derivation") self.nodes[1].encryptwallet("test") + if self.options.descriptors: + self.nodes[1].walletpassphrase('test', 10) + self.nodes[1].importdescriptors([{ + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/0h/*h)'), + 'timestamp': 'now', + 'active': True + }, + { + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/1h/*h)'), + 'timestamp': 'now', + 'active': True, + 'internal': True + }]) + self.nodes[1].walletlock() + # Drain the keypool. self.nodes[1].getnewaddress() inputs = self.nodes[1].listunspent() @@ -616,7 +643,25 @@ def test_watchonly(self): outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2} rawtx = self.nodes[3].createrawtransaction(inputs, outputs) - result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True }) + self.nodes[3].loadwallet('wwatch') + wwatch = self.nodes[3].get_wallet_rpc('wwatch') + # Setup change addresses for the watchonly wallet + desc_import = [{ + "desc": descsum_create("pkh(tpubD6NzVbkrYhZ4YNXVQbNhMK1WqguFsUXceaVJKbmno2aZ3B6QfbMeraaYvnBSGpV3vxLyTTK9DYT1yoEck4XUScMzXoQ2U2oSmE2JyMedq3H/1/*)"), + "timestamp": "now", + "internal": True, + "active": True, + "keypool": True, + "range": [0, 100], + "watchonly": True, + }] + if self.options.descriptors: + wwatch.importdescriptors(desc_import) + else: + wwatch.importmulti(desc_import) + + # Backward compatibility test (2nd params is includeWatching) + result = wwatch.fundrawtransaction(rawtx, True) res_dec = self.nodes[0].decoderawtransaction(result["hex"]) assert_equal(len(res_dec["vin"]), 1) assert_equal(res_dec["vin"][0]["txid"], self.watchonly_txid) @@ -624,6 +669,8 @@ def test_watchonly(self): assert "fee" in result.keys() assert_greater_than(result["changepos"], -1) + wwatch.unloadwallet() + def test_all_watched_funds(self): self.log.info("Test fundrawtxn using entirety of watched funds") @@ -631,17 +678,19 @@ def test_all_watched_funds(self): outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount} rawtx = self.nodes[3].createrawtransaction(inputs, outputs) - # Backward compatibility test (2nd param is includeWatching). - result = self.nodes[3].fundrawtransaction(rawtx, True) + self.nodes[3].loadwallet('wwatch') + wwatch = self.nodes[3].get_wallet_rpc('wwatch') + w3 = self.nodes[3].get_wallet_rpc(self.default_wallet_name) + result = wwatch.fundrawtransaction(rawtx, {'includeWatching': True, 'changeAddress': w3.getrawchangeaddress(), 'subtractFeeFromOutputs': [0]}) res_dec = self.nodes[0].decoderawtransaction(result["hex"]) - assert_equal(len(res_dec["vin"]), 2) - assert res_dec["vin"][0]["txid"] == self.watchonly_txid or res_dec["vin"][1]["txid"] == self.watchonly_txid + assert_equal(len(res_dec["vin"]), 1) + assert res_dec["vin"][0]["txid"] == self.watchonly_txid assert_greater_than(result["fee"], 0) - assert_greater_than(result["changepos"], -1) - assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], self.watchonly_amount / 10) + assert_equal(result["changepos"], -1) + assert_equal(result["fee"] + res_dec["vout"][0]["value"], self.watchonly_amount) - signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"]) + signedtx = wwatch.signrawtransactionwithwallet(result["hex"]) assert not signedtx["complete"] signedtx = self.nodes[0].signrawtransactionwithwallet(signedtx["hex"]) assert signedtx["complete"] @@ -649,6 +698,8 @@ def test_all_watched_funds(self): self.nodes[0].generate(1) self.sync_all() + wwatch.unloadwallet() + def test_option_feerate(self): self.log.info("Test fundrawtxn feeRate option") diff --git a/test/functional/rpc_rawtransaction.py b/test/functional/rpc_rawtransaction.py index 11aa9a7782aef..c32a0c2e010ff 100755 --- a/test/functional/rpc_rawtransaction.py +++ b/test/functional/rpc_rawtransaction.py @@ -22,6 +22,7 @@ from test_framework.util import ( assert_equal, assert_raises_rpc_error, + find_vout_for_address, hex_str_to_bytes, ) @@ -186,125 +187,140 @@ def run_test(self): self.nodes[0].reconsiderblock(block1) assert_equal(self.nodes[0].getbestblockhash(), block2) - ######################### - # RAW TX MULTISIG TESTS # - ######################### - # 2of2 test - addr1 = self.nodes[2].getnewaddress() - addr2 = self.nodes[2].getnewaddress() - - addr1Obj = self.nodes[2].getaddressinfo(addr1) - addr2Obj = self.nodes[2].getaddressinfo(addr2) - - # Tests for createmultisig and addmultisigaddress - assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 1, ["01020304"]) - self.nodes[0].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']]) # createmultisig can only take public keys - assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 2, [addr1Obj['pubkey'], addr1]) # addmultisigaddress can take both pubkeys and addresses so long as they are in the wallet, which is tested here. - - mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr1])['address'] - - #use balance deltas instead of absolute values - bal = self.nodes[2].getbalance() - - # send 1.2 BTC to msig adr - txId = self.nodes[0].sendtoaddress(mSigObj, 1.2) - self.sync_all() - self.nodes[0].generate(1) - self.sync_all() - assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance - - - # 2of3 test from different nodes - bal = self.nodes[2].getbalance() - addr1 = self.nodes[1].getnewaddress() - addr2 = self.nodes[2].getnewaddress() - addr3 = self.nodes[2].getnewaddress() - - addr1Obj = self.nodes[1].getaddressinfo(addr1) - addr2Obj = self.nodes[2].getaddressinfo(addr2) - addr3Obj = self.nodes[2].getaddressinfo(addr3) - - mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])['address'] - - txId = self.nodes[0].sendtoaddress(mSigObj, 2.2) - decTx = self.nodes[0].gettransaction(txId) - rawTx = self.nodes[0].decoderawtransaction(decTx['hex']) - self.sync_all() + if not self.options.descriptors: + # The traditional multisig workflow does not work with descriptor wallets so these are legacy only. + # The multisig workflow with descriptor wallets uses PSBTs and is tested elsewhere, no need to do them here. + ######################### + # RAW TX MULTISIG TESTS # + ######################### + # 2of2 test + addr1 = self.nodes[2].getnewaddress() + addr2 = self.nodes[2].getnewaddress() + + addr1Obj = self.nodes[2].getaddressinfo(addr1) + addr2Obj = self.nodes[2].getaddressinfo(addr2) + + # Tests for createmultisig and addmultisigaddress + assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 1, ["01020304"]) + self.nodes[0].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']]) # createmultisig can only take public keys + assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 2, [addr1Obj['pubkey'], addr1]) # addmultisigaddress can take both pubkeys and addresses so long as they are in the wallet, which is tested here. + + mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr1])['address'] + + #use balance deltas instead of absolute values + bal = self.nodes[2].getbalance() + + # send 1.2 BTC to msig adr + txId = self.nodes[0].sendtoaddress(mSigObj, 1.2) + self.sync_all() + self.nodes[0].generate(1) + self.sync_all() + assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance + + + # 2of3 test from different nodes + bal = self.nodes[2].getbalance() + addr1 = self.nodes[1].getnewaddress() + addr2 = self.nodes[2].getnewaddress() + addr3 = self.nodes[2].getnewaddress() + + addr1Obj = self.nodes[1].getaddressinfo(addr1) + addr2Obj = self.nodes[2].getaddressinfo(addr2) + addr3Obj = self.nodes[2].getaddressinfo(addr3) + + mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])['address'] + + txId = self.nodes[0].sendtoaddress(mSigObj, 2.2) + decTx = self.nodes[0].gettransaction(txId) + rawTx = self.nodes[0].decoderawtransaction(decTx['hex']) + self.sync_all() + self.nodes[0].generate(1) + self.sync_all() + + #THIS IS AN INCOMPLETE FEATURE + #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION + assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable + + txDetails = self.nodes[0].gettransaction(txId, True) + rawTx = self.nodes[0].decoderawtransaction(txDetails['hex']) + vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('2.20000000')) + + bal = self.nodes[0].getbalance() + inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}] + outputs = { self.nodes[0].getnewaddress() : 2.19 } + rawTx = self.nodes[2].createrawtransaction(inputs, outputs) + rawTxPartialSigned = self.nodes[1].signrawtransactionwithwallet(rawTx, inputs) + assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx + + rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx, inputs) + assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys + self.nodes[2].sendrawtransaction(rawTxSigned['hex']) + rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex']) + self.sync_all() + self.nodes[0].generate(1) + self.sync_all() + assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx + + # 2of2 test for combining transactions + bal = self.nodes[2].getbalance() + addr1 = self.nodes[1].getnewaddress() + addr2 = self.nodes[2].getnewaddress() + + addr1Obj = self.nodes[1].getaddressinfo(addr1) + addr2Obj = self.nodes[2].getaddressinfo(addr2) + + self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] + mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] + mSigObjValid = self.nodes[2].getaddressinfo(mSigObj) + + txId = self.nodes[0].sendtoaddress(mSigObj, 2.2) + decTx = self.nodes[0].gettransaction(txId) + rawTx2 = self.nodes[0].decoderawtransaction(decTx['hex']) + self.sync_all() + self.nodes[0].generate(1) + self.sync_all() + + assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable + + txDetails = self.nodes[0].gettransaction(txId, True) + rawTx2 = self.nodes[0].decoderawtransaction(txDetails['hex']) + vout = next(o for o in rawTx2['vout'] if o['value'] == Decimal('2.20000000')) + + bal = self.nodes[0].getbalance() + inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "redeemScript" : mSigObjValid['hex']}] + outputs = { self.nodes[0].getnewaddress() : 2.19 } + rawTx2 = self.nodes[2].createrawtransaction(inputs, outputs) + rawTxPartialSigned1 = self.nodes[1].signrawtransactionwithwallet(rawTx2, inputs) + self.log.debug(rawTxPartialSigned1) + assert_equal(rawTxPartialSigned1['complete'], False) #node1 only has one key, can't comp. sign the tx + + rawTxPartialSigned2 = self.nodes[2].signrawtransactionwithwallet(rawTx2, inputs) + self.log.debug(rawTxPartialSigned2) + assert_equal(rawTxPartialSigned2['complete'], False) #node2 only has one key, can't comp. sign the tx + rawTxComb = self.nodes[2].combinerawtransaction([rawTxPartialSigned1['hex'], rawTxPartialSigned2['hex']]) + self.log.debug(rawTxComb) + self.nodes[2].sendrawtransaction(rawTxComb) + rawTx2 = self.nodes[0].decoderawtransaction(rawTxComb) + self.sync_all() + self.nodes[0].generate(1) + self.sync_all() + assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx + + + # Basic signrawtransaction test + addr = self.nodes[1].getnewaddress() + txid = self.nodes[0].sendtoaddress(addr, 10) self.nodes[0].generate(1) self.sync_all() - - #THIS IS AN INCOMPLETE FEATURE - #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION - assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable - - txDetails = self.nodes[0].gettransaction(txId, True) - rawTx = self.nodes[0].decoderawtransaction(txDetails['hex']) - vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('2.20000000')) - - bal = self.nodes[0].getbalance() - inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}] - outputs = { self.nodes[0].getnewaddress() : 2.19 } - rawTx = self.nodes[2].createrawtransaction(inputs, outputs) - rawTxPartialSigned = self.nodes[1].signrawtransactionwithwallet(rawTx, inputs) - assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx - - rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx, inputs) - assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys - self.nodes[2].sendrawtransaction(rawTxSigned['hex']) - rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex']) - self.sync_all() - self.nodes[0].generate(1) - self.sync_all() - assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx - - # 2of2 test for combining transactions - bal = self.nodes[2].getbalance() - addr1 = self.nodes[1].getnewaddress() - addr2 = self.nodes[2].getnewaddress() - - addr1Obj = self.nodes[1].getaddressinfo(addr1) - addr2Obj = self.nodes[2].getaddressinfo(addr2) - - self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] - mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] - mSigObjValid = self.nodes[2].getaddressinfo(mSigObj) - - txId = self.nodes[0].sendtoaddress(mSigObj, 2.2) - decTx = self.nodes[0].gettransaction(txId) - rawTx2 = self.nodes[0].decoderawtransaction(decTx['hex']) - self.sync_all() - self.nodes[0].generate(1) - self.sync_all() - - assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable - - txDetails = self.nodes[0].gettransaction(txId, True) - rawTx2 = self.nodes[0].decoderawtransaction(txDetails['hex']) - vout = next(o for o in rawTx2['vout'] if o['value'] == Decimal('2.20000000')) - - bal = self.nodes[0].getbalance() - inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "redeemScript" : mSigObjValid['hex']}] - outputs = { self.nodes[0].getnewaddress() : 2.19 } - rawTx2 = self.nodes[2].createrawtransaction(inputs, outputs) - rawTxPartialSigned1 = self.nodes[1].signrawtransactionwithwallet(rawTx2, inputs) - self.log.debug(rawTxPartialSigned1) - assert_equal(rawTxPartialSigned1['complete'], False) #node1 only has one key, can't comp. sign the tx - - rawTxPartialSigned2 = self.nodes[2].signrawtransactionwithwallet(rawTx2, inputs) - self.log.debug(rawTxPartialSigned2) - assert_equal(rawTxPartialSigned2['complete'], False) #node2 only has one key, can't comp. sign the tx - rawTxComb = self.nodes[2].combinerawtransaction([rawTxPartialSigned1['hex'], rawTxPartialSigned2['hex']]) - self.log.debug(rawTxComb) - self.nodes[2].sendrawtransaction(rawTxComb) - rawTx2 = self.nodes[0].decoderawtransaction(rawTxComb) - self.sync_all() + vout = find_vout_for_address(self.nodes[1], txid, addr) + rawTx = self.nodes[1].createrawtransaction([{'txid': txid, 'vout': vout}], {self.nodes[1].getnewaddress(): 9.999}) + rawTxSigned = self.nodes[1].signrawtransactionwithwallet(rawTx) + txId = self.nodes[1].sendrawtransaction(rawTxSigned['hex']) self.nodes[0].generate(1) self.sync_all() - assert_equal(self.nodes[0].getbalance(), bal+Decimal('500.00000000')+Decimal('2.19000000')) #block reward + tx # getrawtransaction tests # 1. valid parameters - only supply txid - txId = rawTx["txid"] assert_equal(self.nodes[0].getrawtransaction(txId), rawTxSigned['hex']) assert_equal(self.nodes[0].getrawtransactionmulti({"0":[txId]})[txId], rawTxSigned['hex']) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index 7e68fe9a635cb..e8202f5e7d293 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -202,9 +202,8 @@ def create_tx_with_script(prevtx, n, script_sig=b"", *, amount, script_pub_key=C def create_transaction(node, txid, to_address, *, amount): """ Return signed transaction spending the first output of the - input txid. Note that the node must be able to sign for the - output that is being spent, and the node must not be running - multiple wallets. + input txid. Note that the node must have a wallet that can + sign for the output that is being spent. """ raw_tx = create_raw_transaction(node, txid, to_address, amount=amount) tx = CTransaction() @@ -213,14 +212,18 @@ def create_transaction(node, txid, to_address, *, amount): def create_raw_transaction(node, txid, to_address, *, amount): """ Return raw signed transaction spending the first output of the - input txid. Note that the node must be able to sign for the - output that is being spent, and the node must not be running - multiple wallets. + input txid. Note that the node must have a wallet that can sign + for the output that is being spent. """ - rawtx = node.createrawtransaction(inputs=[{"txid": txid, "vout": 0}], outputs={to_address: amount}) - signresult = node.signrawtransactionwithwallet(rawtx) - assert_equal(signresult["complete"], True) - return signresult['hex'] + psbt = node.createpsbt(inputs=[{"txid": txid, "vout": 0}], outputs={to_address: amount}) + for _ in range(2): + for w in node.listwallets(): + wrpc = node.get_wallet_rpc(w) + signed_psbt = wrpc.walletprocesspsbt(psbt) + psbt = signed_psbt['psbt'] + final_psbt = node.finalizepsbt(psbt) + assert_equal(final_psbt["complete"], True) + return final_psbt['hex'] def get_legacy_sigopcount_block(block, accurate=True): count = 0 diff --git a/test/functional/test_framework/script_util.py b/test/functional/test_framework/script_util.py index 18340b2bbbac7..40a448675528d 100755 --- a/test/functional/test_framework/script_util.py +++ b/test/functional/test_framework/script_util.py @@ -3,7 +3,8 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Useful Script constants and utils.""" -from test_framework.script import CScript +from test_framework.script import CScript, hash160, OP_DUP, OP_HASH160, OP_CHECKSIG, OP_EQUAL, OP_EQUALVERIFY +from test_framework.util import hex_str_to_bytes # To prevent a "tx-size-small" policy rule error, a transaction has to have a # size of at least 83 bytes (MIN_STANDARD_TX_SIZE in @@ -24,3 +25,33 @@ # met. DUMMY_P2SH_SCRIPT = CScript([b'a' * 22]) DUMMY_2_P2SH_SCRIPT = CScript([b'b' * 22]) + +def keyhash_to_p2pkh_script(hash, main = False): + assert len(hash) == 20 + return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG]) + +def scripthash_to_p2sh_script(hash, main = False): + assert len(hash) == 20 + return CScript([OP_HASH160, hash, OP_EQUAL]) + +def key_to_p2pkh_script(key, main = False): + key = check_key(key) + return keyhash_to_p2pkh_script(hash160(key), main) + +def script_to_p2sh_script(script, main = False): + script = check_script(script) + return scripthash_to_p2sh_script(hash160(script), main) + +def check_key(key): + if isinstance(key, str): + key = hex_str_to_bytes(key) # Assuming this is hex string + if isinstance(key, bytes) and (len(key) == 33 or len(key) == 65): + return key + assert False + +def check_script(script): + if isinstance(script, str): + script = hex_str_to_bytes(script) # Assuming this is hex string + if isinstance(script, bytes) or isinstance(script, CScript): + return script + assert False diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 24d2e8a0defc8..8f5034e866392 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -219,8 +219,12 @@ def parse_args(self): parser.add_argument("--randomseed", type=int, help="set a random seed for deterministically reproducing a previous test run") parser.add_argument('--timeout-factor', dest="timeout_factor", type=float, default=1.0, help='adjust test timeouts by a factor. Setting it to 0 disables all timeouts') - parser.add_argument("--descriptors", default=False, action="store_true", - help="Run test using a descriptor wallet") + + group = parser.add_mutually_exclusive_group() + group.add_argument("--descriptors", default=False, action="store_true", + help="Run test using a descriptor wallet", dest='descriptors') + group.add_argument("--legacy-wallet", default=False, action="store_false", + help="Run test using legacy wallets", dest='descriptors') self.add_options(parser) self.options = parser.parse_args() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 09778e7aad255..90cbb6004596a 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -104,6 +104,7 @@ 'feature_block.py', # NOTE: needs dash_hash to pass 'rpc_fundrawtransaction.py', 'rpc_fundrawtransaction.py --nohd', + 'rpc_fundrawtransaction.py --descriptors', 'wallet_multiwallet.py --usecli', 'p2p_quorum_data.py', # vv Tests less than 2m vv @@ -116,8 +117,9 @@ 'feature_bip68_sequence.py', 'mempool_updatefromblock.py', 'p2p_tx_download.py', - 'wallet_dump.py', + 'wallet_dump.py --legacy-wallet', 'wallet_listtransactions.py', + 'wallet_listtransactions.py --descriptors', 'feature_multikeysporks.py', 'feature_dip3_v19.py', 'feature_llmq_signing.py', # NOTE: needs dash_hash to pass @@ -136,13 +138,16 @@ # vv Tests less than 60s vv 'p2p_sendheaders.py', # NOTE: needs dash_hash to pass 'p2p_sendheaders_compressed.py', # NOTE: needs dash_hash to pass - 'wallet_importmulti.py', + 'wallet_importmulti.py --legacy-wallet', 'mempool_limit.py', 'rpc_txoutproof.py', 'wallet_listreceivedby.py', + 'wallet_listreceivedby.py --descriptors', 'wallet_abandonconflict.py', + 'wallet_abandonconflict.py --descriptors', 'feature_csv_activation.py', 'rpc_rawtransaction.py', + 'rpc_rawtransaction.py --descriptors', 'feature_reindex.py', 'feature_abortnode.py', # vv Tests less than 30s vv @@ -157,6 +162,7 @@ 'mempool_resurrect.py', 'wallet_txn_doublespend.py --mineblock', 'tool_wallet.py', + 'tool_wallet.py --descriptors', 'wallet_txn_clone.py', 'rpc_getchaintips.py', 'rpc_misc.py', @@ -170,9 +176,10 @@ 'wallet_multiwallet.py --descriptors', 'wallet_createwallet.py', 'wallet_createwallet.py --usecli', + 'wallet_createwallet.py --descriptors', 'wallet_reorgsrestore.py', - 'wallet_watchonly.py', - 'wallet_watchonly.py --usecli', + 'wallet_watchonly.py --legacy-wallet', + 'wallet_watchonly.py --usecli --legacy-wallet', 'interface_http.py', 'interface_rpc.py', 'rpc_psbt.py', @@ -181,8 +188,10 @@ 'rpc_whitelist.py', 'feature_proxy.py', 'rpc_signrawtransaction.py', + 'rpc_signrawtransaction.py --descriptors', 'p2p_addrv2_relay.py', 'wallet_groups.py', + 'wallet_groups.py --descriptors', 'p2p_disconnect_ban.py', 'feature_addressindex.py', 'feature_timestampindex.py', @@ -191,6 +200,7 @@ 'rpc_blockchain.py', 'rpc_deprecated.py', 'wallet_disable.py', + 'wallet_disable.py --descriptors', 'p2p_addr_relay.py', 'p2p_getaddr_caching.py', 'p2p_getdata.py', @@ -211,7 +221,9 @@ 'feature_assumevalid.py', 'example_test.py', 'wallet_txn_doublespend.py', + 'wallet_txn_doublespend.py --descriptors', 'feature_backwards_compatibility.py', + 'feature_backwards_compatibility.py --descriptors', 'wallet_txn_clone.py --mineblock', 'feature_notifications.py', 'rpc_getblockfilter.py', @@ -226,16 +238,19 @@ 'feature_versionbits_warning.py', 'rpc_preciousblock.py', 'wallet_importprunedfunds.py', + 'wallet_importprunedfunds.py --descriptors', 'p2p_leak_tx.py', 'p2p_eviction.py', 'rpc_signmessage.py', 'rpc_generateblock.py', 'wallet_balance.py', + 'wallet_balance.py --descriptors', 'feature_nulldummy.py', + 'feature_nulldummy.py --descriptors', 'mempool_accept.py', 'mempool_expiry.py', - 'wallet_import_rescan.py', - 'wallet_import_with_label.py', + 'wallet_import_rescan.py --legacy-wallet', + 'wallet_import_with_label.py --legacy-wallet', 'wallet_upgradewallet.py', 'wallet_importdescriptors.py --descriptors', 'wallet_mnemonicbits.py', @@ -245,6 +260,7 @@ 'mining_basic.py', 'rpc_named_arguments.py', 'wallet_listsinceblock.py', + 'wallet_listsinceblock.py --descriptors', 'wallet_listdescriptors.py --descriptors', 'p2p_leak.py', 'p2p_compactblocks.py', @@ -261,7 +277,9 @@ 'feature_governance.py', 'rpc_uptime.py', 'wallet_resendwallettransactions.py', + 'wallet_resendwallettransactions.py --descriptors', 'wallet_fallbackfee.py', + 'wallet_fallbackfee.py --descriptors', 'rpc_dumptxoutset.py', 'feature_minchainwork.py', 'rpc_estimatefee.py', @@ -274,12 +292,14 @@ 'rpc_verifychainlock.py', 'wallet_create_tx.py', 'wallet_send.py', + 'wallet_create_tx.py --descriptors', 'p2p_fingerprint.py', 'rpc_platform_filter.py', 'rpc_wipewallettxes.py', 'feature_dip0020_activation.py', 'feature_uacomment.py', 'wallet_coinbase_category.py', + 'wallet_coinbase_category.py --descriptors', 'feature_filelock.py', 'feature_loadblock.py', 'p2p_blockfilters.py', diff --git a/test/functional/tool_wallet.py b/test/functional/tool_wallet.py index f804a6c4b45f0..6fee23ea4df9b 100755 --- a/test/functional/tool_wallet.py +++ b/test/functional/tool_wallet.py @@ -70,9 +70,9 @@ def test_invalid_tool_commands_and_args(self): self.assert_raises_tool_error('Error: two methods provided (info and create). Only one method should be provided.', 'info', 'create') self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo') locked_dir = os.path.join(self.options.tmpdir, "node0", self.chain, "wallets") - error = "SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another dashd?" - if self.is_bdb_compiled(): - error = 'Error initializing wallet database environment "{}"!'.format(locked_dir) + error = 'Error initializing wallet database environment "{}"!'.format(locked_dir) + if self.options.descriptors: + error = "SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another dashd?" self.assert_raises_tool_error( error, '-wallet=' + self.default_wallet_name, @@ -97,19 +97,33 @@ def test_tool_wallet_info(self): # shasum_before = self.wallet_shasum() timestamp_before = self.wallet_timestamp() self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before)) - out = textwrap.dedent('''\ - Wallet info - =========== - Name: \ - - Format: bdb - Descriptors: no - Encrypted: no - HD (hd seed available): yes - Keypool Size: 2 - Transactions: 0 - Address Book: 1 - ''') + if self.options.descriptors: + out = textwrap.dedent('''\ + Wallet info + =========== + Name: default_wallet + Format: sqlite + Descriptors: yes + Encrypted: no + HD (hd seed available): yes + Keypool Size: 2 + Transactions: 0 + Address Book: 1 + ''') + else: + out = textwrap.dedent('''\ + Wallet info + =========== + Name: \ + + Format: bdb + Descriptors: no + Encrypted: no + HD (hd seed available): yes + Keypool Size: 2 + Transactions: 0 + Address Book: 1 + ''') self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info') timestamp_after = self.wallet_timestamp() @@ -141,19 +155,33 @@ def test_tool_wallet_info_after_transaction(self): shasum_before = self.wallet_shasum() timestamp_before = self.wallet_timestamp() self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before)) - out = textwrap.dedent('''\ - Wallet info - =========== - Name: \ - - Format: bdb - Descriptors: no - Encrypted: no - HD (hd seed available): yes - Keypool Size: 2 - Transactions: 1 - Address Book: 1 - ''') + if self.options.descriptors: + out = textwrap.dedent('''\ + Wallet info + =========== + Name: default_wallet + Format: sqlite + Descriptors: yes + Encrypted: no + HD (hd seed available): yes + Keypool Size: 2 + Transactions: 1 + Address Book: 1 + ''') + else: + out = textwrap.dedent('''\ + Wallet info + =========== + Name: \ + + Format: bdb + Descriptors: no + Encrypted: no + HD (hd seed available): yes + Keypool Size: 2 + Transactions: 1 + Address Book: 1 + ''') self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info') shasum_after = self.wallet_shasum() timestamp_after = self.wallet_timestamp() @@ -264,9 +292,11 @@ def run_test(self): # Warning: The following tests are order-dependent. self.test_tool_wallet_info() self.test_tool_wallet_info_after_transaction() - self.test_tool_wallet_create_on_existing_wallet() - self.test_getwalletinfo_on_different_wallet() - if self.is_bdb_compiled(): + if not self.options.descriptors: + # TODO: Wallet tool needs more create options at which point these can be enabled. + self.test_tool_wallet_create_on_existing_wallet() + self.test_getwalletinfo_on_different_wallet() + # Salvage is a legacy wallet only thing self.test_salvage() self.test_wipe() diff --git a/test/functional/wallet_balance.py b/test/functional/wallet_balance.py index ad5f3d11e981e..e1976568fb138 100755 --- a/test/functional/wallet_balance.py +++ b/test/functional/wallet_balance.py @@ -58,14 +58,16 @@ def skip_test_if_missing_module(self): self.skip_if_no_wallet() def run_test(self): - self.nodes[0].importaddress(ADDRESS_WATCHONLY) - # Check that nodes don't own any UTXOs - assert_equal(len(self.nodes[0].listunspent()), 0) - assert_equal(len(self.nodes[1].listunspent()), 0) + if not self.options.descriptors: + # Tests legacy watchonly behavior which is not present (and does not need to be tested) in descriptor wallets + self.nodes[0].importaddress(ADDRESS_WATCHONLY) + # Check that nodes don't own any UTXOs + assert_equal(len(self.nodes[0].listunspent()), 0) + assert_equal(len(self.nodes[1].listunspent()), 0) - self.log.info("Check that only node 0 is watching an address") - assert 'watchonly' in self.nodes[0].getbalances() - assert 'watchonly' not in self.nodes[1].getbalances() + self.log.info("Check that only node 0 is watching an address") + assert 'watchonly' in self.nodes[0].getbalances() + assert 'watchonly' not in self.nodes[1].getbalances() self.log.info("Mining blocks ...") self.nodes[0].generate(1) @@ -74,25 +76,30 @@ def run_test(self): self.nodes[1].generatetoaddress(COINBASE_MATURITY + 1, ADDRESS_WATCHONLY) self.sync_all() - assert_equal(self.nodes[0].getbalances()['mine']['trusted'], 500) - assert_equal(self.nodes[0].getwalletinfo()['balance'], 500) - assert_equal(self.nodes[1].getbalances()['mine']['trusted'], 500) + if not self.options.descriptors: + # Tests legacy watchonly behavior which is not present (and does not need to be tested) in descriptor wallets + assert_equal(self.nodes[0].getbalances()['mine']['trusted'], 500) + assert_equal(self.nodes[0].getwalletinfo()['balance'], 500) + assert_equal(self.nodes[1].getbalances()['mine']['trusted'], 500) - assert_equal(self.nodes[0].getbalances()['watchonly']['immature'], 50000) - assert 'watchonly' not in self.nodes[1].getbalances() + assert_equal(self.nodes[0].getbalances()['watchonly']['immature'], 50000) + assert 'watchonly' not in self.nodes[1].getbalances() - assert_equal(self.nodes[0].getbalance(), 500) - assert_equal(self.nodes[1].getbalance(), 500) + assert_equal(self.nodes[0].getbalance(), 500) + assert_equal(self.nodes[1].getbalance(), 500) self.log.info("Test getbalance with different arguments") assert_equal(self.nodes[0].getbalance("*"), 500) assert_equal(self.nodes[0].getbalance("*", 1), 500) - assert_equal(self.nodes[0].getbalance("*", 1, True), 500) - assert_equal(self.nodes[0].getbalance("*", 1, True, False), 500) assert_equal(self.nodes[0].getbalance(minconf=1, addlocked=True), 500) assert_equal(self.nodes[0].getbalance(minconf=1, avoid_reuse=False), 500) assert_equal(self.nodes[0].getbalance(minconf=1), 500) - assert_equal(self.nodes[0].getbalance(minconf=0, include_watchonly=True), 1000) + if not self.options.descriptors: + assert_equal(self.nodes[0].getbalance(minconf=0, include_watchonly=True), 1000) + assert_equal(self.nodes[0].getbalance("*", 1, True, True), 1000) + else: + assert_equal(self.nodes[0].getbalance(minconf=0, include_watchonly=True), 500) + assert_equal(self.nodes[0].getbalance("*", 1, True), 500) assert_equal(self.nodes[1].getbalance(minconf=0, include_watchonly=True), 500) # Send 490 BTC from 0 to 1 and 960 BTC from 1 to 0. @@ -162,6 +169,8 @@ def test_balances(*, fee_node_1=0): 'immature': Decimal('0E-8'), 'trusted': Decimal('0E-8'), # node 1's send had an unsafe input 'untrusted_pending': Decimal('30.0') - fee_node_1}} # Doesn't include output of node 0's send since it was spent + if self.options.descriptors: + del expected_balances_0["watchonly"] assert_equal(self.nodes[0].getbalances(), expected_balances_0) assert_equal(self.nodes[1].getbalances(), expected_balances_1) # getbalance without any arguments includes unconfirmed transactions, but not untrusted transactions diff --git a/test/functional/wallet_createwallet.py b/test/functional/wallet_createwallet.py index 61af66ee7fccf..d310b2d8a7530 100755 --- a/test/functional/wallet_createwallet.py +++ b/test/functional/wallet_createwallet.py @@ -5,11 +5,15 @@ """Test createwallet arguments. """ +from test_framework.address import key_to_p2pkh +from test_framework.descriptors import descsum_create +from test_framework.key import ECKey from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, ) +from test_framework.wallet_util import bytes_to_wif, generate_wif_key class CreateWalletTest(BitcoinTestFramework): def set_test_params(self): @@ -34,12 +38,16 @@ def run_test(self): w1.importpubkey(w0.getaddressinfo(address1)['pubkey']) self.log.info('Test that private keys cannot be imported') - addr = w0.getnewaddress() - privkey = w0.dumpprivkey(addr) + eckey = ECKey() + eckey.generate() + privkey = bytes_to_wif(eckey.get_bytes()) assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey) - result = w1.importmulti([{'scriptPubKey': {'address': addr}, 'timestamp': 'now', 'keys': [privkey]}]) - assert(not result[0]['success']) - assert('warning' not in result[0]) + if self.options.descriptors: + result = w1.importdescriptors([{'desc': descsum_create('pkh(' + privkey + ')'), 'timestamp': 'now'}]) + else: + result = w1.importmulti([{'scriptPubKey': {'address': key_to_p2pkh(eckey.get_pubkey().get_bytes())}, 'timestamp': 'now', 'keys': [privkey]}]) + assert not result[0]['success'] + assert 'warning' not in result[0] assert_equal(result[0]['error']['code'], -4) assert_equal(result[0]['error']['message'], 'Cannot import private keys to a wallet with private keys disabled') @@ -57,12 +65,25 @@ def run_test(self): assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress) assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getrawchangeaddress) # Import private key - w3.importprivkey(w0.dumpprivkey(address1)) + w3.importprivkey(generate_wif_key()) # Imported private keys are currently ignored by the keypool assert_equal(w3.getwalletinfo()['keypoolsize'], 0) assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress) # Set the seed - w3.upgradetohd() + if self.options.descriptors: + w3.importdescriptors([{ + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/0h/*)'), + 'timestamp': 'now', + 'active': True + }, + { + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/1h/*)'), + 'timestamp': 'now', + 'active': True, + 'internal': True + }]) + else: + w3.upgradetohd() assert_equal(w3.getwalletinfo()['keypoolsize'], 1) w3.getnewaddress() w3.getrawchangeaddress() @@ -79,7 +100,20 @@ def run_test(self): assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getrawchangeaddress) # Now set a seed and it should work. Wallet should also be encrypted w4.walletpassphrase('pass', 60) - w4.upgradetohd(walletpassphrase='pass') + if self.options.descriptors: + w4.importdescriptors([{ + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/0h/*)'), + 'timestamp': 'now', + 'active': True + }, + { + 'desc': descsum_create('pkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/1h/*)'), + 'timestamp': 'now', + 'active': True, + 'internal': True + }]) + else: + w4.upgradetohd(walletpassphrase='pass') w4.getnewaddress() w4.getrawchangeaddress() @@ -110,13 +144,15 @@ def run_test(self): w6.walletpassphrase('thisisapassphrase', 60) w6.signmessage(w6.getnewaddress(), "test") w6.keypoolrefill(1) - # There should only be 1 key + # There should only be 1 key for legacy, 1 for descriptors (dash has only one type of addresses) walletinfo = w6.getwalletinfo() - assert_equal(walletinfo['keypoolsize'], 1) - assert_equal(walletinfo['keypoolsize_hd_internal'], 1) + keys = 1 if self.options.descriptors else 1 + assert_equal(walletinfo['keypoolsize'], keys) + # hd_internals are not refilled by default for descriptor wallets atm + assert_equal(walletinfo['keypoolsize_hd_internal'], keys) # Allow empty passphrase, but there should be a warning resp = self.nodes[0].createwallet(wallet_name='w7', disable_private_keys=False, blank=False, passphrase='') - assert_equal(resp['warning'], 'Empty string given as passphrase, wallet will not be encrypted.') + assert 'Empty string given as passphrase, wallet will not be encrypted.' in resp['warning'] w7 = node.get_wallet_rpc('w7') assert_raises_rpc_error(-15, 'Error: running with an unencrypted wallet, but walletpassphrase was called.', w7.walletpassphrase, '', 60) diff --git a/test/functional/wallet_importdescriptors.py b/test/functional/wallet_importdescriptors.py index f3cc96db82879..35ab4e85ab5a8 100755 --- a/test/functional/wallet_importdescriptors.py +++ b/test/functional/wallet_importdescriptors.py @@ -35,6 +35,7 @@ def set_test_params(self): ["-keypool=5"] ] self.setup_clean_chain = True + self.wallet_names = [] def skip_test_if_missing_module(self): self.skip_if_no_wallet() @@ -60,7 +61,7 @@ def test_importdesc(self, req, success, error_code=None, error_message=None, war def run_test(self): self.log.info('Setting up wallets') - self.nodes[0].createwallet(wallet_name='w0', disable_private_keys=False) + self.nodes[0].createwallet(wallet_name='w0', disable_private_keys=False, descriptors=True) w0 = self.nodes[0].get_wallet_rpc('w0') self.nodes[1].createwallet(wallet_name='w1', disable_private_keys=True, blank=True, descriptors=True) diff --git a/test/functional/wallet_importprunedfunds.py b/test/functional/wallet_importprunedfunds.py index 50da2d13c622d..08f52cecba0fe 100755 --- a/test/functional/wallet_importprunedfunds.py +++ b/test/functional/wallet_importprunedfunds.py @@ -5,12 +5,15 @@ """Test the importprunedfunds and removeprunedfunds RPCs.""" from decimal import Decimal +from test_framework.address import key_to_p2pkh from test_framework.blocktools import COINBASE_MATURITY +from test_framework.key import ECKey from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, ) +from test_framework.wallet_util import bytes_to_wif class ImportPrunedFundsTest(BitcoinTestFramework): def set_test_params(self): @@ -31,8 +34,11 @@ def run_test(self): # pubkey address2 = self.nodes[0].getnewaddress() # privkey - address3 = self.nodes[0].getnewaddress() - address3_privkey = self.nodes[0].dumpprivkey(address3) # Using privkey + eckey = ECKey() + eckey.generate() + address3_privkey = bytes_to_wif(eckey.get_bytes()) + address3 = key_to_p2pkh(eckey.get_pubkey().get_bytes()) + self.nodes[0].importprivkey(address3_privkey) # Check only one address address_info = self.nodes[0].getaddressinfo(address1) @@ -81,37 +87,44 @@ def run_test(self): assert_equal(balance1, Decimal(0)) # Import with affiliated address with no rescan - self.nodes[1].importaddress(address=address2, rescan=False) - self.nodes[1].importprunedfunds(rawtransaction=rawtxn2, txoutproof=proof2) - assert [tx for tx in self.nodes[1].listtransactions(include_watchonly=True) if tx['txid'] == txnid2] + self.nodes[1].createwallet('wwatch', disable_private_keys=True) + wwatch = self.nodes[1].get_wallet_rpc('wwatch') + wwatch.importaddress(address=address2, rescan=False) + wwatch.importprunedfunds(rawtransaction=rawtxn2, txoutproof=proof2) + assert [tx for tx in wwatch.listtransactions(include_watchonly=True) if tx['txid'] == txnid2] # Import with private key with no rescan - self.nodes[1].importprivkey(privkey=address3_privkey, rescan=False) - self.nodes[1].importprunedfunds(rawtxn3, proof3) - assert [tx for tx in self.nodes[1].listtransactions() if tx['txid'] == txnid3] - balance3 = self.nodes[1].getbalance() + w1 = self.nodes[1].get_wallet_rpc(self.default_wallet_name) + w1.importprivkey(privkey=address3_privkey, rescan=False) + w1.importprunedfunds(rawtxn3, proof3) + assert [tx for tx in w1.listtransactions() if tx['txid'] == txnid3] + balance3 = w1.getbalance() assert_equal(balance3, Decimal('0.025')) # Addresses Test - after import - address_info = self.nodes[1].getaddressinfo(address1) + address_info = w1.getaddressinfo(address1) assert_equal(address_info['iswatchonly'], False) assert_equal(address_info['ismine'], False) - address_info = self.nodes[1].getaddressinfo(address2) - assert_equal(address_info['iswatchonly'], True) - assert_equal(address_info['ismine'], False) - address_info = self.nodes[1].getaddressinfo(address3) + address_info = wwatch.getaddressinfo(address2) + if self.options.descriptors: + assert_equal(address_info['iswatchonly'], False) + assert_equal(address_info['ismine'], True) + else: + assert_equal(address_info['iswatchonly'], True) + assert_equal(address_info['ismine'], False) + address_info = w1.getaddressinfo(address3) assert_equal(address_info['iswatchonly'], False) assert_equal(address_info['ismine'], True) # Remove transactions - assert_raises_rpc_error(-8, "Transaction does not exist in wallet.", self.nodes[1].removeprunedfunds, txnid1) - assert not [tx for tx in self.nodes[1].listtransactions(include_watchonly=True) if tx['txid'] == txnid1] + assert_raises_rpc_error(-8, "Transaction does not exist in wallet.", w1.removeprunedfunds, txnid1) + assert not [tx for tx in w1.listtransactions(include_watchonly=True) if tx['txid'] == txnid1] - self.nodes[1].removeprunedfunds(txnid2) - assert not [tx for tx in self.nodes[1].listtransactions(include_watchonly=True) if tx['txid'] == txnid2] + wwatch.removeprunedfunds(txnid2) + assert not [tx for tx in wwatch.listtransactions(include_watchonly=True) if tx['txid'] == txnid2] - self.nodes[1].removeprunedfunds(txnid3) - assert not [tx for tx in self.nodes[1].listtransactions(include_watchonly=True) if tx['txid'] == txnid3] + w1.removeprunedfunds(txnid3) + assert not [tx for tx in w1.listtransactions(include_watchonly=True) if tx['txid'] == txnid3] if __name__ == '__main__': ImportPrunedFundsTest().main() diff --git a/test/functional/wallet_labels.py b/test/functional/wallet_labels.py index 7891c86e6e646..5275abe80b409 100755 --- a/test/functional/wallet_labels.py +++ b/test/functional/wallet_labels.py @@ -138,7 +138,7 @@ def run_test(self): change_label(node, labels[2].addresses[0], labels[2], labels[2]) self.log.info('Check watchonly labels') - node.createwallet(wallet_name='watch_only', disable_private_keys=True, descriptors=False) + node.createwallet(wallet_name='watch_only', disable_private_keys=True) wallet_watch_only = node.get_wallet_rpc('watch_only') VALID = { '✔️_a1': 'yMNJePdcKvXtWWQnFYHNeJ5u8TF2v1dfK4', @@ -159,7 +159,7 @@ def run_test(self): ad = INVALID[l] assert_raises_rpc_error( -5, - "Invalid Dash address or script", + "Address is not valid" if self.options.descriptors else "Invalid Dash address or script", lambda: wallet_watch_only.importaddress(label=l, rescan=False, address=ad), ) diff --git a/test/functional/wallet_listsinceblock.py b/test/functional/wallet_listsinceblock.py index 8fceee3366c47..eb68ab5815bf8 100755 --- a/test/functional/wallet_listsinceblock.py +++ b/test/functional/wallet_listsinceblock.py @@ -4,7 +4,9 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the listsinceblock RPC.""" +from test_framework.address import key_to_p2pkh from test_framework.blocktools import COINBASE_MATURITY +from test_framework.key import ECKey from test_framework.test_framework import BitcoinTestFramework from test_framework.messages import BIP125_SEQUENCE_NUMBER from test_framework.util import ( @@ -12,6 +14,7 @@ assert_equal, assert_raises_rpc_error, ) +from test_framework.wallet_util import bytes_to_wif from decimal import Decimal @@ -182,15 +185,21 @@ def test_double_spend(self): self.sync_all() - # Split network into two - self.split_network() - # share utxo between nodes[1] and nodes[2] + eckey = ECKey() + eckey.generate() + privkey = bytes_to_wif(eckey.get_bytes()) + address = key_to_p2pkh(eckey.get_pubkey().get_bytes()) + self.nodes[2].sendtoaddress(address, 10) + self.nodes[2].generate(6) + self.nodes[2].importprivkey(privkey) utxos = self.nodes[2].listunspent() - utxo = utxos[0] - privkey = self.nodes[2].dumpprivkey(utxo['address']) + utxo = [u for u in utxos if u["address"] == address][0] self.nodes[1].importprivkey(privkey) + # Split network into two + self.split_network() + # send from nodes[1] using utxo to nodes[0] change = '%.8f' % (float(utxo['amount']) - 1.0003) recipient_dict = { diff --git a/test/functional/wallet_listtransactions.py b/test/functional/wallet_listtransactions.py index ccdf18606f81a..2e25d61df9149 100755 --- a/test/functional/wallet_listtransactions.py +++ b/test/functional/wallet_listtransactions.py @@ -77,18 +77,20 @@ def run_test(self): {"category": "receive", "amount": Decimal("0.44")}, {"txid": txid} ) - pubkey = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress())['pubkey'] - multisig = self.nodes[1].createmultisig(1, [pubkey]) - self.nodes[0].importaddress(multisig["redeemScript"], "watchonly", False, True) - txid = self.nodes[1].sendtoaddress(multisig["address"], 0.1) - self.nodes[1].generate(1) - self.sync_all() - assert_equal(len(self.nodes[0].listtransactions(label="watchonly", include_watchonly=True)), 1) - assert_equal(len(self.nodes[0].listtransactions(dummy="watchonly", include_watchonly=True)), 1) - assert len(self.nodes[0].listtransactions(label="watchonly", count=100, include_watchonly=False)) == 0 - assert_array_result(self.nodes[0].listtransactions(label="watchonly", count=100, include_watchonly=True), - {"category": "receive", "amount": Decimal("0.1")}, - {"txid": txid, "label": "watchonly"}) + if not self.options.descriptors: + # include_watchonly is a legacy wallet feature, so don't test it for descriptor wallets + pubkey = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress())['pubkey'] + multisig = self.nodes[1].createmultisig(1, [pubkey]) + self.nodes[0].importaddress(multisig["redeemScript"], "watchonly", False, True) + txid = self.nodes[1].sendtoaddress(multisig["address"], 0.1) + self.nodes[1].generate(1) + self.sync_all() + assert_equal(len(self.nodes[0].listtransactions(label="watchonly", include_watchonly=True)), 1) + assert_equal(len(self.nodes[0].listtransactions(dummy="watchonly", include_watchonly=True)), 1) + assert len(self.nodes[0].listtransactions(label="watchonly", count=100, include_watchonly=False)) == 0 + assert_array_result(self.nodes[0].listtransactions(label="watchonly", count=100, include_watchonly=True), + {"category": "receive", "amount": Decimal("0.1")}, + {"txid": txid, "label": "watchonly"}) if __name__ == '__main__': ListTransactionsTest().main() From d04c1a703e85a9635c585e5a149354a88a877fb3 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Fri, 8 Mar 2024 00:49:06 +0700 Subject: [PATCH 10/12] fix: unify ScriptPubKeyMan implementation with bitcoin's --- src/wallet/scriptpubkeyman.cpp | 14 +++++++++----- src/wallet/scriptpubkeyman.h | 2 -- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index ffd4ed1887172..f3e5ba35c43e6 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -299,6 +299,7 @@ bool LegacyScriptPubKeyMan::GetReservedDestination(bool internal, CTxDestination if (!ReserveKeyFromKeyPool(index, keypool, internal)) { return false; } + // TODO: unify with bitcoin and use here GetDestinationForKey even if we have no type address = PKHash(keypool.vchPubKey); return true; } @@ -1214,13 +1215,13 @@ bool LegacyScriptPubKeyMan::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& inf return true; } -bool LegacyScriptPubKeyMan::AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info) +bool LegacyScriptPubKeyMan::AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info) { LOCK(cs_KeyStore); std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint); mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path; mapKeyMetadata[pubkey.GetID()].has_key_origin = true; - return WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true); + return batch.WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true); } bool LegacyScriptPubKeyMan::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const @@ -1367,6 +1368,9 @@ void LegacyScriptPubKeyMan::LoadKeyPool(int64_t nIndex, const CKeyPool &keypool) bool LegacyScriptPubKeyMan::CanGenerateKeys() const { LOCK(cs_KeyStore); + // TODO : unify with bitcoin after backporting SetupGeneration + // return IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD); + if (m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) || m_storage.IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET)) { return false; } @@ -1713,6 +1717,9 @@ bool LegacyScriptPubKeyMan::ImportPrivKeys(const std::map& privkey bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector& ordered_pubkeys, const std::map& pubkey_map, const std::map>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) { WalletBatch batch(m_storage.GetDatabase()); + for (const auto& entry : key_origins) { + AddKeyOriginWithDB(batch, entry.second.first, entry.second.second); + } for (const CKeyID& id : ordered_pubkeys) { auto entry = pubkey_map.find(id); if (entry == pubkey_map.end()) { @@ -1735,9 +1742,6 @@ bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector& ordered_pub NotifyCanGetAddressesChanged(); } } - for (const auto& entry : key_origins) { - AddKeyOrigin(entry.second.first, entry.second.second); - } return true; } diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index c1396c201e7d2..c87eb9d7d847d 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -433,8 +433,6 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv bool AddCScript(const CScript& redeemScript) override; /** Implement lookup of key origin information through wallet key metadata. */ bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override; - /** Add a KeyOriginInfo to the wallet */ - bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info); void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool); bool NewKeyPool(); From 9a02f7da9471b9f95ee2a5ec6e658105be71909a Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Fri, 8 Mar 2024 00:50:39 +0700 Subject: [PATCH 11/12] fix: drop requirement of HD in CanGetAddresses for watch-only wallets --- src/wallet/scriptpubkeyman.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index f3e5ba35c43e6..2c9a3c00343f5 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -611,7 +611,7 @@ bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal) const LOCK(cs_KeyStore); // Check if the keypool has keys bool keypool_has_keys; - if (internal && m_storage.CanSupportFeature(FEATURE_HD)) { + if (internal) { keypool_has_keys = setInternalKeyPool.size() > 0; } else { keypool_has_keys = KeypoolCountExternalKeys() > 0; From 85fa6e4585c58e6b24a04ec2844cf671d80116b7 Mon Sep 17 00:00:00 2001 From: fanquake Date: Thu, 15 Jul 2021 13:36:40 +0800 Subject: [PATCH 12/12] Merge bitcoin/bitcoin#22446: test: Fix wallet_listdescriptors.py if bdb is not compiled 0c845e3f8995eb8dc543a63899e5633a46091b4e test: Fix wallet_listdescriptors.py if bdb is not compiled (Hennadii Stepanov) Pull request description: If build system is configured `--without-bdb`, the `wallet_listdescriptors.py` fails: ``` $ test/functional/wallet_listdescriptors.py --descriptors 2021-07-14T13:20:52.931000Z TestFramework (INFO): Initializing test directory /tmp/bitcoin_func_test_02p7o1c9 2021-07-14T13:21:23.377000Z TestFramework (INFO): Test that the command is not available for legacy wallets. 2021-07-14T13:21:23.381000Z TestFramework (ERROR): JSONRPC error Traceback (most recent call last): File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/test_framework.py", line 128, in main self.run_test() File "test/functional/wallet_listdescriptors.py", line 34, in run_test node.createwallet(wallet_name='w1', descriptors=False) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/test_node.py", line 685, in createwallet return self.__getattr__('createwallet')(wallet_name, disable_private_keys, blank, passphrase, avoid_reuse, descriptors, load_on_startup, external_signer) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/coverage.py", line 47, in __call__ return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/authproxy.py", line 146, in __call__ raise JSONRPCException(response['error'], status) test_framework.authproxy.JSONRPCException: Compiled without bdb support (required for legacy wallets) (-4) 2021-07-14T13:21:23.436000Z TestFramework (INFO): Stopping nodes 2021-07-14T13:21:24.092000Z TestFramework (WARNING): Not cleaning up dir /tmp/bitcoin_func_test_02p7o1c9 2021-07-14T13:21:24.092000Z TestFramework (ERROR): Test failed. Test logging available at /tmp/bitcoin_func_test_02p7o1c9/test_framework.log 2021-07-14T13:21:24.092000Z TestFramework (ERROR): 2021-07-14T13:21:24.092000Z TestFramework (ERROR): Hint: Call /home/hebasto/GitHub/bitcoin/test/functional/combine_logs.py '/tmp/bitcoin_func_test_02p7o1c9' to consolidate all logs 2021-07-14T13:21:24.092000Z TestFramework (ERROR): 2021-07-14T13:21:24.092000Z TestFramework (ERROR): If this failure happened unexpectedly or intermittently, please file a bug and provide a link or upload of the combined log. 2021-07-14T13:21:24.092000Z TestFramework (ERROR): https://github.com/bitcoin/bitcoin/issues 2021-07-14T13:21:24.092000Z TestFramework (ERROR): ``` This PR fixes this issue. Also see #20267. ACKs for top commit: achow101: ACK 0c845e3f8995eb8dc543a63899e5633a46091b4e Tree-SHA512: d7116a9ae30c7b7e3f55f55d2eea66f9e293c38d6757ed66d0477e4256ff5fedca5ddedafa0ef0c09f4dc1f7f973163e5a46090da26b067fdddbd9ea2ee76633 --- test/functional/wallet_listdescriptors.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/functional/wallet_listdescriptors.py b/test/functional/wallet_listdescriptors.py index c44c7ef5c229c..8ac5d5fa5b053 100755 --- a/test/functional/wallet_listdescriptors.py +++ b/test/functional/wallet_listdescriptors.py @@ -30,9 +30,10 @@ def run_test(self): node = self.nodes[0] assert_raises_rpc_error(-18, 'No wallet is loaded.', node.listdescriptors) - self.log.info('Test that the command is not available for legacy wallets.') - node.createwallet(wallet_name='w1', descriptors=False) - assert_raises_rpc_error(-4, 'listdescriptors is not available for non-descriptor wallets', node.listdescriptors) + if self.is_bdb_compiled(): + self.log.info('Test that the command is not available for legacy wallets.') + node.createwallet(wallet_name='w1', descriptors=False) + assert_raises_rpc_error(-4, 'listdescriptors is not available for non-descriptor wallets', node.listdescriptors) self.log.info('Test the command for empty descriptors wallet.') node.createwallet(wallet_name='w2', blank=True, descriptors=True)