Skip to content

Commit

Permalink
fix tests for miniscript refactors
Browse files Browse the repository at this point in the history
and PSBT change detection for multisig
  • Loading branch information
odudex committed Dec 10, 2024
1 parent 5f60928 commit 213224e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/krux/pages/home_pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ def sign_psbt(self):
if not self.prompt(t("Proceed?"), BOTTOM_PROMPT_LINE):
return MENU_CONTINUE

# Show the policy for multisig and miniscript PSBTs in case the wallet descriptor is not loaded
# Show the policy for multisig and miniscript PSBTs
# in case the wallet descriptor is not loaded
if (
not self.ctx.wallet.is_loaded()
and not self.ctx.wallet.key.policy_type == TYPE_SINGLESIG
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/home_pages/wallet_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ...qr import FORMAT_NONE
from ...sd_card import DESCRIPTOR_FILE_EXTENSION, JSON_FILE_EXTENSION
from ...themes import theme
from ...key import FINGERPRINT_SYMBOL, TYPE_SINGLESIG
from ...key import FINGERPRINT_SYMBOL


class WalletDescriptor(Page):
Expand Down
22 changes: 11 additions & 11 deletions tests/pages/home_pages/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@pytest.fixture
def tdata(mocker):
from collections import namedtuple
from krux.key import Key, P2PKH, P2SH_P2WPKH, P2TR
from krux.key import Key, P2PKH, P2SH_P2WPKH, P2TR, TYPE_SINGLESIG, TYPE_MULTISIG
from embit.networks import NETWORKS

TEST_12_WORD_MNEMONIC = (
Expand All @@ -16,32 +16,32 @@ def tdata(mocker):
SIGNING_MNEMONIC = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
ACTION_MNEMONIC = "action action action action action action action action action action action action"

SINGLESIG_12_WORD_KEY = Key(TEST_12_WORD_MNEMONIC, False, NETWORKS["main"])
SINGLESIG_24_WORD_KEY = Key(TEST_24_WORD_MNEMONIC, False, NETWORKS["main"])
MULTISIG_12_WORD_KEY = Key(TEST_12_WORD_MNEMONIC, True, NETWORKS["main"])
SINGLESIG_SIGNING_KEY = Key(SIGNING_MNEMONIC, False, NETWORKS["main"])
MULTISIG_SIGNING_KEY = Key(SIGNING_MNEMONIC, True, NETWORKS["main"])
SINGLESIG_ACTION_KEY = Key(ACTION_MNEMONIC, False, NETWORKS["main"])
SINGLESIG_12_WORD_KEY = Key(TEST_12_WORD_MNEMONIC, TYPE_SINGLESIG, NETWORKS["main"])
SINGLESIG_24_WORD_KEY = Key(TEST_24_WORD_MNEMONIC, TYPE_SINGLESIG, NETWORKS["main"])
MULTISIG_12_WORD_KEY = Key(TEST_12_WORD_MNEMONIC, TYPE_MULTISIG, NETWORKS["main"])
SINGLESIG_SIGNING_KEY = Key(SIGNING_MNEMONIC, TYPE_SINGLESIG, NETWORKS["main"])
MULTISIG_SIGNING_KEY = Key(SIGNING_MNEMONIC, TYPE_MULTISIG, NETWORKS["main"])
SINGLESIG_ACTION_KEY = Key(ACTION_MNEMONIC, TYPE_SINGLESIG, NETWORKS["main"])
SINGLESIG_ACTION_KEY_TEST = Key(
ACTION_MNEMONIC, False, NETWORKS["test"], script_type=P2TR
ACTION_MNEMONIC, TYPE_SINGLESIG, NETWORKS["test"], script_type=P2TR
)
LEGACY1_KEY = Key(
TEST_12_WORD_MNEMONIC,
False,
TYPE_SINGLESIG,
NETWORKS["main"],
account_index=1,
script_type=P2PKH,
)
NESTEDSW1_KEY = Key(
TEST_12_WORD_MNEMONIC,
False,
TYPE_SINGLESIG,
NETWORKS["main"],
account_index=1,
script_type=P2SH_P2WPKH,
)
NATIVESW1_KEY = Key(
TEST_12_WORD_MNEMONIC,
False,
TYPE_SINGLESIG,
NETWORKS["main"],
account_index=1,
)
Expand Down
4 changes: 3 additions & 1 deletion tests/pages/home_pages/test_pub_key_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def test_public_key(mocker, m5stickv, tdata):
from krux.wallet import Wallet
from krux.input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV
from krux.qr import FORMAT_NONE
from krux.key import TYPE_MULTISIG

cases = [
# Case parameters: [Wallet, Printer, Button Sequence, Show XPUB, Show ZPUB]
Expand Down Expand Up @@ -77,7 +78,8 @@ def test_public_key(mocker, m5stickv, tdata):

pub_key_viewer.public_key()

version = "Zpub" if ctx.wallet.key.multisig else "zpub"
version = "Zpub" if ctx.wallet.key.policy_type == TYPE_MULTISIG else "zpub"
print(ctx.key.policy_type, version)
qr_view_calls = []
print_qr_calls = []

Expand Down
7 changes: 3 additions & 4 deletions tests/pages/test_settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ def test_settings_m5stickv(m5stickv, mocker, mocker_printer):
# Default Wallet
BUTTON_ENTER,
# Go to Network
BUTTON_PAGE,
BUTTON_ENTER,
# Change network
*([BUTTON_PAGE] * 2), # Cycle through 2 options
BUTTON_PAGE_PREV, # Go back to the second option - testnet
BUTTON_ENTER,
# Leave Default Wallet
*([BUTTON_PAGE] * 2),
*([BUTTON_PAGE] * 3),
BUTTON_ENTER,
# Leave Settings
BUTTON_PAGE_PREV,
Expand Down Expand Up @@ -150,7 +149,7 @@ def test_settings_m5stickv(m5stickv, mocker, mocker_printer):
assert case[1]()


@pytest.fixture(params=["m5stickv", "cube"])
@pytest.fixture(params=["m5stickv", "cube"]) # TODO:Add WonderMV to the list
def bkl_control_devices(request):
return request.getfixturevalue(request.param)

Expand Down Expand Up @@ -221,7 +220,7 @@ def test_settings_on_amigo_tft(amigo, mocker, mocker_printer):
# Enter Wallet
0,
# Go to Network
1,
0,
# Change network
NEXT_INDEX,
GO_INDEX,
Expand Down
34 changes: 17 additions & 17 deletions tests/pages/test_wallet_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ def test_change_multisig_changes(m5stickv, mocker, tdata):
ctx = create_ctx(mocker, BTN_SEQUENCE_1, Wallet(tdata.SINGLESIG_12_WORD_KEY))
mnemonic = ctx.wallet.key.mnemonic
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand All @@ -138,13 +138,13 @@ def test_change_multisig_changes(m5stickv, mocker, tdata):
]
ctx = create_ctx(mocker, BTN_SEQUENCE_2, ctx.wallet)
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand All @@ -163,7 +163,7 @@ def test_change_script_type(m5stickv, mocker, tdata):
BTN_SEQUENCE_1 = [
*([BUTTON_PAGE] * 2), # Go to "Script Type"
BUTTON_ENTER, # Enter "Script Type"
BUTTON_PAGE_PREV, # Move to Taproot
*([BUTTON_PAGE] * 3), # Move to Taproot
BUTTON_ENTER, # Confirm Taproot
BUTTON_PAGE_PREV, # Go to Back
BUTTON_ENTER, # Leave
Expand All @@ -172,13 +172,13 @@ def test_change_script_type(m5stickv, mocker, tdata):
ctx = create_ctx(mocker, BTN_SEQUENCE_1, Wallet(tdata.SINGLESIG_12_WORD_KEY))
mnemonic = ctx.wallet.key.mnemonic
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand All @@ -198,13 +198,13 @@ def test_change_script_type(m5stickv, mocker, tdata):
]
ctx = create_ctx(mocker, BTN_SEQUENCE_2, ctx.wallet)
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand Down Expand Up @@ -234,13 +234,13 @@ def test_change_account(m5stickv, mocker, tdata):
ctx = create_ctx(mocker, BTN_SEQUENCE_1, Wallet(tdata.SINGLESIG_12_WORD_KEY))
mnemonic = ctx.wallet.key.mnemonic
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand All @@ -265,13 +265,13 @@ def test_change_account(m5stickv, mocker, tdata):
]
ctx = create_ctx(mocker, BTN_SEQUENCE_2, ctx.wallet)
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand Down Expand Up @@ -300,13 +300,13 @@ def test_change_account_esc(m5stickv, mocker, tdata):
ctx = create_ctx(mocker, BTN_SEQUENCE_1, Wallet(tdata.SINGLESIG_12_WORD_KEY))
mnemonic = ctx.wallet.key.mnemonic
wallet_settings = WalletSettings(ctx)
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand Down Expand Up @@ -337,13 +337,13 @@ def test_account_out_of_range(m5stickv, mocker, tdata):
mnemonic = ctx.wallet.key.mnemonic
wallet_settings = WalletSettings(ctx)
wallet_settings.flash_error = mocker.MagicMock()
network, multisig, script_type, account = wallet_settings.customize_wallet(
network, policy_type, script_type, account = wallet_settings.customize_wallet(
ctx.wallet.key
)
ctx.wallet = Wallet(
Key(
mnemonic,
multisig,
policy_type,
network,
"", # passphrase
account,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def test_clear_clears_printer(mocker, m5stickv):


def test_is_logged_in(mocker, m5stickv):
from krux.key import TYPE_SINGLESIG

mock_modules(mocker)
from krux.context import Context
from krux.wallet import Wallet
Expand All @@ -52,5 +54,7 @@ def test_is_logged_in(mocker, m5stickv):
c.wallet = Wallet(None)
assert c.is_logged_in() == False

c.wallet = Wallet(Key(mnemonic="abandon " * 11 + "about", multisig=False))
c.wallet = Wallet(
Key(mnemonic="abandon " * 11 + "about", policy_type=TYPE_SINGLESIG)
)
assert c.is_logged_in() == True
20 changes: 10 additions & 10 deletions tests/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def mock_modules(mocker):
def test_init(mocker, m5stickv, tdata):
mock_modules(mocker)
from embit.networks import NETWORKS
from krux.key import Key
from krux.key import Key, TYPE_SINGLESIG, TYPE_MULTISIG

cases = [
(
[tdata.TEST_12_WORD_MNEMONIC, False],
{
"mnemonic": tdata.TEST_12_WORD_MNEMONIC,
"multisig": False,
"policy_type": TYPE_SINGLESIG,
"network": NETWORKS["test"],
"root key": "tprv8ZgxMBicQKsPfJtsjGcMm6f7ibxmy2LbqbePeCJnhE3tFNKfuWmNHUyMnAfgwQXDSAhfTLGvN4f8zjEFochGbnHiZcrGXnyHDKQaTRK5trx",
"derivation": "m/84h/1h/0h",
Expand All @@ -106,7 +106,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_12_WORD_MNEMONIC, True],
{
"mnemonic": tdata.TEST_12_WORD_MNEMONIC,
"multisig": True,
"policy_type": TYPE_MULTISIG,
"network": NETWORKS["test"],
"root key": "tprv8ZgxMBicQKsPfJtsjGcMm6f7ibxmy2LbqbePeCJnhE3tFNKfuWmNHUyMnAfgwQXDSAhfTLGvN4f8zjEFochGbnHiZcrGXnyHDKQaTRK5trx",
"derivation": "m/48h/1h/0h/2h",
Expand All @@ -117,7 +117,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_12_WORD_MNEMONIC, False, NETWORKS["main"]],
{
"mnemonic": tdata.TEST_12_WORD_MNEMONIC,
"multisig": False,
"policy_type": TYPE_SINGLESIG,
"network": NETWORKS["main"],
"root key": "xprv9s21ZrQH143K4VfM4hkrbT38QUYZjWJbW3jGmmtLDFZQTmaav9RcmjburzW2w38u4jAtTEfACi5LXsgWgQMKnj282ydxsSFEJDfA1o1TySf",
"derivation": "m/84h/0h/0h",
Expand All @@ -128,7 +128,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_12_WORD_MNEMONIC, True, NETWORKS["main"]],
{
"mnemonic": tdata.TEST_12_WORD_MNEMONIC,
"multisig": True,
"policy_type": TYPE_MULTISIG,
"network": NETWORKS["main"],
"root key": "xprv9s21ZrQH143K4VfM4hkrbT38QUYZjWJbW3jGmmtLDFZQTmaav9RcmjburzW2w38u4jAtTEfACi5LXsgWgQMKnj282ydxsSFEJDfA1o1TySf",
"derivation": "m/48h/0h/0h/2h",
Expand All @@ -139,7 +139,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_24_WORD_MNEMONIC, False],
{
"mnemonic": tdata.TEST_24_WORD_MNEMONIC,
"multisig": False,
"policy_type": TYPE_SINGLESIG,
"network": NETWORKS["test"],
"root key": "tprv8ZgxMBicQKsPe5ghS4VTSeC3XwXJqVcJo4pzkFpuqZzFxhjuMsF13r8avzU8nwnwng6PCZ5EcJuPuqWwvJVCMRj3G9ZZyJ884RcrjAQ52BG",
"derivation": "m/84h/1h/0h",
Expand All @@ -150,7 +150,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_24_WORD_MNEMONIC, True],
{
"mnemonic": tdata.TEST_24_WORD_MNEMONIC,
"multisig": True,
"policy_type": TYPE_MULTISIG,
"network": NETWORKS["test"],
"root key": "tprv8ZgxMBicQKsPe5ghS4VTSeC3XwXJqVcJo4pzkFpuqZzFxhjuMsF13r8avzU8nwnwng6PCZ5EcJuPuqWwvJVCMRj3G9ZZyJ884RcrjAQ52BG",
"derivation": "m/48h/1h/0h/2h",
Expand All @@ -161,7 +161,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_24_WORD_MNEMONIC, False, NETWORKS["main"]],
{
"mnemonic": tdata.TEST_24_WORD_MNEMONIC,
"multisig": False,
"policy_type": TYPE_SINGLESIG,
"network": NETWORKS["main"],
"root key": "xprv9s21ZrQH143K3GTAmVdxGza4Dp76byaJTWussqQTMbVnB6zpNVuFY6m91pJUnaQdREZcCTTUSxKbSyyCo69FYNTSjWMGJwQ59KsSHUeNNQd",
"derivation": "m/84h/0h/0h",
Expand All @@ -172,7 +172,7 @@ def test_init(mocker, m5stickv, tdata):
[tdata.TEST_24_WORD_MNEMONIC, True, NETWORKS["main"]],
{
"mnemonic": tdata.TEST_24_WORD_MNEMONIC,
"multisig": True,
"policy_type": TYPE_MULTISIG,
"network": NETWORKS["main"],
"root key": "xprv9s21ZrQH143K3GTAmVdxGza4Dp76byaJTWussqQTMbVnB6zpNVuFY6m91pJUnaQdREZcCTTUSxKbSyyCo69FYNTSjWMGJwQ59KsSHUeNNQd",
"derivation": "m/48h/0h/0h/2h",
Expand All @@ -186,7 +186,7 @@ def test_init(mocker, m5stickv, tdata):

assert isinstance(key, Key)
assert key.mnemonic == case[1]["mnemonic"]
assert key.multisig == case[1]["multisig"]
assert key.policy_type == case[1]["policy_type"]
assert key.network == case[1]["network"]
assert key.root.to_base58() == case[1]["root key"]
assert key.derivation == case[1]["derivation"]
Expand Down
5 changes: 3 additions & 2 deletions tests/test_psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,15 @@ def test_outputs_multisig(mocker, m5stickv, tdata):
[
"Inputs (2): ₿ 0.20 000 000\n\nSpend (1): ₿ 0.18 993 880\n\nSelf-transfer or Change (1): ₿ 0.01 000 000\n\nFee: ₿ 0.00 006 120 (0.1%) ~20.1 sat/vB",
"1. Spend: \n\ntb1q35pg2rdt3p0v27dmdh9st43q8vzl29cps6kt3yradnqmg55eahfqfgn83n\n\n₿ 0.18 993 880",
"1. Self-transfer: \n\ntb1q4xgr8suxvgenukgf4c7r6qaawxxmy9zelh24q8hg5pfxzn2ekn3qfw808t\n\n₿ 0.01 000 000",
"1. Change: \n\ntb1q4xgr8suxvgenukgf4c7r6qaawxxmy9zelh24q8hg5pfxzn2ekn3qfw808t\n\n₿ 0.01 000 000",
],
),
(
tdata.P2SH_P2WSH_PSBT,
[
"Inputs (1): ₿ 1.00 000 000\n\nSpend (1): ₿ 0.10 000 000\n\nSelf-transfer or Change (1): ₿ 0.89 995 740\n\nFee: ₿ 0.00 004 260 (0.1%) ~20.0 sat/vB",
"1. Spend: \n\ntb1que40al7rsw88ru9z0vr78vqwme4w3ctqj694kx\n\n₿ 0.10 000 000",
"1. Self-transfer: \n\n2N3vYfcg14Axr4NN33ADUorE2kEGEchFJpC\n\n₿ 0.89 995 740",
"1. Change: \n\n2N3vYfcg14Axr4NN33ADUorE2kEGEchFJpC\n\n₿ 0.89 995 740",
],
),
]
Expand All @@ -629,6 +629,7 @@ def test_outputs_multisig(mocker, m5stickv, tdata):
signer = PSBTSigner(wallet, case[0], FORMAT_NONE)
outputs, _ = signer.outputs()
assert outputs == case[1]
# TODO: Re-create wallets and check if addresses are change indeed outside these tests


def test_xpubs_fails_with_no_xpubs(mocker, m5stickv, tdata):
Expand Down

0 comments on commit 213224e

Please sign in to comment.