Skip to content

Commit

Permalink
Use importdescriptors when in descriptor wallet mode in wallet_create…
Browse files Browse the repository at this point in the history
…wallet.py

Summary:
sethdseed and importmulti are not available for descriptor wallets, so when doing descriptor wallet tests, use importdescriptors instead

The keypool size when using descriptor wallets is unaffected for Bitcoin ABC, because our descriptors wallets have only the p2pkh keys.

This is a backport of [[bitcoin/bitcoin#18788 | core#18788]] [7/15]
bitcoin/bitcoin@25bc5dc

Test Plan: `test/functional/test_runner.py  wallet_createwallet`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10669
  • Loading branch information
achow101 authored and PiRK committed Dec 15, 2021
1 parent e250429 commit 6cabd8d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"wallet_avoidreuse.py": [["--descriptors"]],
"wallet_balance.py": [["--descriptors"]],
# FIXME: "wallet_basic.py": [["--descriptors"]],
"wallet_createwallet.py": [["--usecli"]],
"wallet_createwallet.py": [["--usecli"], ["--descriptors"]],
"wallet_encryption.py": [["--descriptors"]],
"wallet_hd.py": [["--descriptors"]],
"wallet_importprunedfunds.py": [["--descriptors"]],
Expand Down
64 changes: 53 additions & 11 deletions test/functional/wallet_createwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
"""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):
Expand Down Expand Up @@ -36,12 +40,20 @@ def run_test(self):
w1.importpubkey(w0.getaddressinfo(address1)['pubkey'])

self.log.info('Test that private keys cannot be imported')
addr = w0.getnewaddress('', 'legacy')
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]}])
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)
Expand All @@ -68,13 +80,28 @@ def run_test(self):
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.sethdseed()
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.sethdseed()
assert_equal(w3.getwalletinfo()['keypoolsize'], 1)
w3.getnewaddress()
w3.getrawchangeaddress()
Expand All @@ -97,7 +124,22 @@ def run_test(self):
w4.getrawchangeaddress)
# Now set a seed and it should work. Wallet should also be encrypted
w4.walletpassphrase('pass', 2)
w4.sethdseed()
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.sethdseed()
w4.getnewaddress()
w4.getrawchangeaddress()

Expand Down Expand Up @@ -160,7 +202,7 @@ def run_test(self):
w6.walletpassphrase('thisisapassphrase', 10)
w6.signmessage(w6.getnewaddress('', 'legacy'), "test")
w6.keypoolrefill(1)
# There should only be 1 key
# There should only be 1 key for legacy and for descriptors
walletinfo = w6.getwalletinfo()
assert_equal(walletinfo['keypoolsize'], 1)
assert_equal(walletinfo['keypoolsize_hd_internal'], 1)
Expand All @@ -170,9 +212,9 @@ def run_test(self):
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,
Expand Down

0 comments on commit 6cabd8d

Please sign in to comment.