Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions test/functional/wallet_mnemonicbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test -mnemonicbits wallet option."""

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
)

custom_mnemonic = "similar behave slot swim scissors throw planet view ghost laugh drift calm"
# this address belongs to custom mnemonic with no passphrase
custom_address_1 = "yLpq97zZUsFQ2rdMqhcPKkYT36MoPK4Hob"
# this address belongs to custom mnemonic with passphrase "custom-passphrase"
custom_address_2 = "yYBPeZQcqgQHu9dxA5pKBWtYbK2hwfFHxf"


class WalletMnemonicbitsTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
Expand Down Expand Up @@ -45,7 +53,6 @@ def run_test(self):

mnemonic_pre = self.get_mnemonic(self.nodes[0])


self.nodes[0].encryptwallet('pass')
self.nodes[0].walletpassphrase('pass', 100)
if self.options.descriptors:
Expand Down Expand Up @@ -88,22 +95,68 @@ def run_test(self):
self.nodes[0].loadwallet("wallet_160")
self.nodes[0].loadwallet("wallet_192")
self.nodes[0].loadwallet("wallet_224")
if self.options.descriptors:
self.nodes[0].createwallet("wallet_256", False, True, "", False, True) # blank Descriptors
self.nodes[0].get_wallet_rpc("wallet_256").upgradetohd()
assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc(self.default_wallet_name)).split()), 12) # 12 words by default
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_160").listdescriptors(True)["descriptors"][0]["mnemonic"].split()), 15) # 15 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_192").listdescriptors(True)["descriptors"][0]["mnemonic"].split()), 18) # 18 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_224").listdescriptors(True)["descriptors"][0]["mnemonic"].split()), 21) # 21 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_256").listdescriptors(True)["descriptors"][0]["mnemonic"].split()), 24) # 24 words
self.nodes[0].createwallet("wallet_256", blank=True, descriptors=self.options.descriptors) # blank wallet
self.nodes[0].get_wallet_rpc("wallet_256").upgradetohd()

assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc(self.default_wallet_name)).split()), 12) # 12 words by default
assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc("wallet_160")).split()), 15) # 15 words
assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc("wallet_192")).split()), 18) # 18 words
assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc("wallet_224")).split()), 21) # 21 words
assert_equal(len(self.get_mnemonic(self.nodes[0].get_wallet_rpc("wallet_256")).split()), 24) # 24 words


self.generate(self.nodes[0], COINBASE_MATURITY + 1)

self.nodes[0].get_wallet_rpc(self.default_wallet_name).sendtoaddress(custom_address_1, 11)
self.nodes[0].get_wallet_rpc(self.default_wallet_name).sendtoaddress(custom_address_2, 12)
self.generate(self.nodes[0], 1)


self.log.info("Test upgradetohd with user defined mnemonic")
self.test_upgradetohd_custom(False, False)

self.log.info("Test upgradetohd with user defined mnemonic, encrypt wallet after creation")
self.test_upgradetohd_custom(True, False)

self.log.info("Test upgradetohd with user defined mnemonic, encrypt wallet after upgradetohd")
self.test_upgradetohd_custom(False, True)
Comment on lines +115 to +122
Copy link

Choose a reason for hiding this comment

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

Why add these tests in wallet_mnemonicbits.py? Could we tweak wallet_upgradetohd.py to accept --legacy-wallet/--descriptors instead?

Copy link
Collaborator Author

@knst knst Jul 17, 2025

Choose a reason for hiding this comment

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

because descriptor wallets are not compatible with usehd=0, adding descriptor wallets to wallet_upgradetohd.py will require some re-work of this funtional test.

Let me try, maybe that's not so bad to tweak.


def test_upgradetohd_custom(self, to_encrypt_1, to_encrypt_2):
wname_1a = f"w-custom-1a-{to_encrypt_1}-{to_encrypt_2}"
wname_1b = f"w-custom-1b-{to_encrypt_1}-{to_encrypt_2}"
wname_2 = f"w-custom-2-{to_encrypt_1}-{to_encrypt_2}"
wnames = [wname_1a, wname_1b, wname_2]

for wname in wnames:
self.nodes[0].createwallet(wname, blank=True)
if to_encrypt_1:
self.nodes[0].get_wallet_rpc(wname).encryptwallet("123")
self.nodes[0].get_wallet_rpc(wname).walletpassphrase("123", 100)

if to_encrypt_1:
self.nodes[0].get_wallet_rpc(wname_1a).upgradetohd(custom_mnemonic, "", "123")
self.nodes[0].get_wallet_rpc(wname_1b).upgradetohd(custom_mnemonic, "", "123")
self.nodes[0].get_wallet_rpc(wname_2).upgradetohd(custom_mnemonic, "custom-passphrase", "123")
else:
self.nodes[0].createwallet("wallet_256", False, True) # blank HD legacy
self.nodes[0].get_wallet_rpc("wallet_256").upgradetohd()
assert_equal(len(self.nodes[0].get_wallet_rpc(self.default_wallet_name).dumphdinfo()["mnemonic"].split()), 12) # 12 words by default
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_160").dumphdinfo()["mnemonic"].split()), 15) # 15 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_192").dumphdinfo()["mnemonic"].split()), 18) # 18 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_224").dumphdinfo()["mnemonic"].split()), 21) # 21 words
assert_equal(len(self.nodes[0].get_wallet_rpc("wallet_256").dumphdinfo()["mnemonic"].split()), 24) # 24 words
self.nodes[0].get_wallet_rpc(wname_1a).upgradetohd(custom_mnemonic)
self.nodes[0].get_wallet_rpc(wname_1b).upgradetohd(custom_mnemonic, "")
self.nodes[0].get_wallet_rpc(wname_2).upgradetohd(custom_mnemonic, "custom-passphrase")


if to_encrypt_2:
for wname in wnames:
self.nodes[0].get_wallet_rpc(wname).encryptwallet("123")
self.nodes[0].get_wallet_rpc(wname).walletpassphrase("123", 100)

self.restart_node(0)
for wname in wnames:
self.nodes[0].loadwallet(wname)
if to_encrypt_1 or to_encrypt_2:
self.nodes[0].get_wallet_rpc(wname).walletpassphrase("123", 100)

assert_equal(11, self.nodes[0].get_wallet_rpc(wname_1a).getbalance())
assert_equal(11, self.nodes[0].get_wallet_rpc(wname_1b).getbalance())
assert_equal(12, self.nodes[0].get_wallet_rpc(wname_2).getbalance())


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions test/functional/wallet_upgradetohd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python3
# Copyright (c) 2016 The Bitcoin Core developers
# Copyright (c) 2016 The Dash Core developers
Copy link

Choose a reason for hiding this comment

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

Suggested change
# Copyright (c) 2016 The Dash Core developers
# Copyright (c) 2025 The Dash Core developers

# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
wallet_upgradetohd.py
Test upgrade to a Hierarchical Deterministic wallet via upgradetohd rpc
Test upgrade to a Hierarchical Deterministic wallet via upgradetohd rpc for legacy wallets
For tests of upgradetohd for descriptor wallets see wallet_mnemonicbits.py
"""

import shutil
Expand Down
Loading