From f6bb3c21f3b2562c6773a54f09df4f8f467ae6e3 Mon Sep 17 00:00:00 2001 From: Jean Do Date: Tue, 10 Dec 2024 18:51:34 -0400 Subject: [PATCH] loaded descriptors no longer forced to "xpub" version --- src/krux/wallet.py | 24 ++++++++++++------------ tests/test_wallet.py | 14 ++++++++++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/krux/wallet.py b/src/krux/wallet.py index 42447901..33f46027 100644 --- a/src/krux/wallet.py +++ b/src/krux/wallet.py @@ -119,26 +119,28 @@ def load(self, wallet_data, qr_format, allow_assumption=None): """Loads the wallet from the given data""" descriptor, label = parse_wallet(wallet_data, allow_assumption) + # convert descriptor keys to 'xpub' on same network -- for comparison only + descriptor_xpubs = [] + for key in descriptor.keys: + network, _ = version_to_network_versiontype(key.key.version) + descriptor_xpubs.append(key.key.to_base58(version=NETWORKS[network]['xpub'])) + if self.key: if self.is_multisig(): if not descriptor.is_basic_multisig: raise ValueError("not multisig") - if self.key.xpub() not in [ - key.key.to_base58() for key in descriptor.keys - ]: + if self.key.xpub() not in descriptor_xpubs: raise ValueError("xpub not a multisig cosigner") elif self.is_miniscript(): if descriptor.miniscript is None or descriptor.is_basic_multisig: raise ValueError("not miniscript") - if self.key.xpub() not in [ - key.key.to_base58() for key in descriptor.keys - ]: + if self.key.xpub() not in descriptor_xpubs: raise ValueError("xpub not a miniscript cosigner") else: if not descriptor.key: if len(descriptor.keys) > 1: raise ValueError("not single-sig") - if self.key.xpub() != descriptor.key.key.to_base58(): + if self.key.xpub() != descriptor_xpubs[0]: raise ValueError("xpub does not match") self.wallet_data = wallet_data @@ -246,11 +248,9 @@ def parse_key_value_file(wallet_data): kv = key_vals[i] kv_prefix = kv[:4].lower() if kv_prefix[1:] == "pub" and kv_prefix[0] in ["x", "z", "t", "v"]: - xpub = Key.from_string(kv) - network, _ = version_to_network_versiontype(xpub.key.version) - xpub_as_xpub = xpub.key.to_base58(version=NETWORKS[network]["xpub"]) + xpub = Key.from_string(kv).key.to_base58() fingerprint = key_vals[i - 1] - keys.append((xpub_as_xpub, fingerprint)) + keys.append((xpub, fingerprint)) if len(keys) != n: raise ValueError("expected %d keys, found %d" % (n, len(keys))) @@ -347,7 +347,7 @@ def parse_wallet(wallet_data, allow_assumption=None): if pubkey.is_extended: network, versiontype = version_to_network_versiontype(pubkey.key.version) - xpub = pubkey.key.to_base58(version=NETWORKS[network]["xpub"]) + xpub = pubkey.key.to_base58() fmt = None if pubkey.origin is None: diff --git a/tests/test_wallet.py b/tests/test_wallet.py index c5b0c135..074b3779 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -37,10 +37,12 @@ def tdata(mocker): KRUX_NESTEDSW1_DESCRIPTOR = "sh(wpkh([55f8fc5d/49h/0h/1h]xpub6Ca1JGnSFNZ7jjwturEn944t8B9kBgiTKtmr3maTbryEyDyYY9xycVSQaFxeUPjbHyX7MUvLUbdoDVK7XZ7Fib9We4BQRRk8bZjW2UPRjHV))" KRUX_NESTEDSW1_XPUB = "[55f8fc5d/49h/0h/1h]xpub6Ca1JGnSFNZ7jjwturEn944t8B9kBgiTKtmr3maTbryEyDyYY9xycVSQaFxeUPjbHyX7MUvLUbdoDVK7XZ7Fib9We4BQRRk8bZjW2UPRjHV" KRUX_NESTEDSW1_YPUB = "[55f8fc5d/49h/0h/1h]ypub6XQGbwTMQ46bb391kD2QM9APJ9JC8JhxF1J4qAULysM82Knmnp8YEZ6YbTvEUJPWhcdv6xWtwFzM6mvgFFXGWpq7WPsq1LZcsHo9R97uuE4" + KRUX_NESTEDSW1_YPUB_DESCRIPTOR = "sh(wpkh(" + KRUX_NESTEDSW1_YPUB + "))" KRUX_NATIVESW1_DESCRIPTOR = "wpkh([55f8fc5d/84h/0h/1h]xpub6DPMTPxGMqdu43FvwYdC6eHCPJWckCkx1rLJ1HEG7259GyWQD5P17WB2oowP9SpQdC8ogrmXfwfoazvf6Te8svtxWh4UTwTqyRdG5G54FxW)" KRUX_NATIVESW1_XPUB = "[55f8fc5d/84h/0h/1h]xpub6DPMTPxGMqdu43FvwYdC6eHCPJWckCkx1rLJ1HEG7259GyWQD5P17WB2oowP9SpQdC8ogrmXfwfoazvf6Te8svtxWh4UTwTqyRdG5G54FxW" KRUX_NATIVESW1_ZPUB = "[55f8fc5d/84h/0h/1h]zpub6s3t4jJ6fCirkdeAcGCSWpUCjEoWdSjwr5Nja522s2puPB8riPi8MdVJrDrZ9G8FSUNRBoxebGNuMa9nXrUAUQGAFNTKdm6pWskYrMahu1i" + KRUX_NATIVESW1_ZPUB_DESCRIPTOR = "wpkh(" + KRUX_NATIVESW1_ZPUB + ")" KRUX_TAPROOT1_DESCRIPTOR = "tr([55f8fc5d/86h/0h/1h]xpub6CNGwJbVG9sQsJjtwLiemRFAfvDafL8zRthnHWNQbRz1PwAm28T1v5hLmJhFft71oEDCbA3xHemnScW5VWheP1BxXNVnoYboyw6t4wuKu5q)" KRUX_TAPROOT1_XPUB = "[55f8fc5d/86h/0h/1h]xpub6CNGwJbVG9sQsJjtwLiemRFAfvDafL8zRthnHWNQbRz1PwAm28T1v5hLmJhFft71oEDCbA3xHemnScW5VWheP1BxXNVnoYboyw6t4wuKu5q" @@ -51,13 +53,13 @@ def tdata(mocker): SPECTER_MULTISIG_DESCRIPTOR = "wsh(sortedmulti(2,[55f8fc5d/48h/0h/0h/2h]xpub6EKmKYGYc1WY6t9d3d9SksR8keSaPZbFa6tqsGiH4xVxx8d2YyxSX7WG6yXEX3CmG54dPCxaapDw1XsjwCmfoqP7tbsAeqMVfKvqSAu4ndy/0/*,[3e15470d/48h/0h/0h/2h]xpub6F2P6Pz5KLPgCc6pTBd2xxCunaSYWc8CdkL28W5z15pJrN3aCYY7mCUAkCMtqrgT2wdhAGgRnJxAkCCUpGKoXKxQ57yffEGmPwtYA3DEXwu/0/*,[d3a80c8b/48h/0h/0h/2h]xpub6FKYY6y3oVi7ihSCszFKRSeZj5SzrfSsUFXhKqjMV4iigrLhxwMX3mrjioNyLTZ5iD3u4wU9S3tyzpJGxhd5geaXoQ68jGz2M6dfh2zJrUv/0/*))" SPECTER_MULTISIG_WALLET_DATA = '{"label": "Specter Multisig Wallet", "blockheight": 0, "descriptor": "wsh(sortedmulti(2,[55f8fc5d/48h/0h/0h/2h]xpub6EKmKYGYc1WY6t9d3d9SksR8keSaPZbFa6tqsGiH4xVxx8d2YyxSX7WG6yXEX3CmG54dPCxaapDw1XsjwCmfoqP7tbsAeqMVfKvqSAu4ndy/0/*,[3e15470d/48h/0h/0h/2h]xpub6F2P6Pz5KLPgCc6pTBd2xxCunaSYWc8CdkL28W5z15pJrN3aCYY7mCUAkCMtqrgT2wdhAGgRnJxAkCCUpGKoXKxQ57yffEGmPwtYA3DEXwu/0/*,[d3a80c8b/48h/0h/0h/2h]xpub6FKYY6y3oVi7ihSCszFKRSeZj5SzrfSsUFXhKqjMV4iigrLhxwMX3mrjioNyLTZ5iD3u4wU9S3tyzpJGxhd5geaXoQ68jGz2M6dfh2zJrUv/0/*))#3nfc6jdy", "devices": [{"type": "other", "label": "Key1"}, {"type": "other", "label": "Key2"}, {"type": "other", "label": "Key3"}]}' - BLUEWALLET_SINGLESIG_DESCRIPTOR = "wpkh(xpub6DPMTPxGMqdtzMwpqT1dDQaVdyaEppEm2qYSaJ7ANsuES7HkNzrXJst1Ed8D7NAnijUdgSDUFgph1oj5LKKAD5gyxWNhNP2AuDqaKYqzphA)" + BLUEWALLET_SINGLESIG_DESCRIPTOR = "wpkh(zpub6s3t4jJ6fCirgxL4WAasdamVyus8i4Dks4at95tw8tezYJvCtKBeZ1CHH33P7BUdY1iFBPQbB1XnnNxCmi9BoZ4BhBmYYCf9Sfxs6jY8Ycw)" BLUEWALLET_SINGLESIG_WALLET_DATA = "zpub6s3t4jJ6fCirgxL4WAasdamVyus8i4Dks4at95tw8tezYJvCtKBeZ1CHH33P7BUdY1iFBPQbB1XnnNxCmi9BoZ4BhBmYYCf9Sfxs6jY8Ycw" BLUEWALLET_LEGACY_DESCRIPTOR = "pkh(xpub6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3mrjioNyLTZ5iD3u4wU9S3tyzpJGxhd5geaXoQ68jGz2M6dfh2zJrUv))" @@ -173,9 +175,11 @@ def tdata(mocker): "KRUX_NESTEDSW1_DESCRIPTOR", "KRUX_NESTEDSW1_XPUB", "KRUX_NESTEDSW1_YPUB", + "KRUX_NESTEDSW1_YPUB_DESCRIPTOR", "KRUX_NATIVESW1_DESCRIPTOR", "KRUX_NATIVESW1_XPUB", "KRUX_NATIVESW1_ZPUB", + "KRUX_NATIVESW1_ZPUB_DESCRIPTOR", "KRUX_TAPROOT1_DESCRIPTOR", "KRUX_TAPROOT1_XPUB", "SPECTER_SINGLESIG_DESCRIPTOR", @@ -221,9 +225,11 @@ def tdata(mocker): KRUX_NESTEDSW1_DESCRIPTOR, KRUX_NESTEDSW1_XPUB, KRUX_NESTEDSW1_YPUB, + KRUX_NESTEDSW1_YPUB_DESCRIPTOR, KRUX_NATIVESW1_DESCRIPTOR, KRUX_NATIVESW1_XPUB, KRUX_NATIVESW1_ZPUB, + KRUX_NATIVESW1_ZPUB_DESCRIPTOR, KRUX_TAPROOT1_DESCRIPTOR, KRUX_TAPROOT1_XPUB, SPECTER_SINGLESIG_DESCRIPTOR, @@ -615,7 +621,7 @@ def test_parse_wallet(mocker, m5stickv, tdata): ), ( tdata.KRUX_NESTEDSW1_YPUB, - tdata.KRUX_NESTEDSW1_DESCRIPTOR, + tdata.KRUX_NESTEDSW1_YPUB_DESCRIPTOR, None, ), ( @@ -625,7 +631,7 @@ def test_parse_wallet(mocker, m5stickv, tdata): ), ( tdata.KRUX_NATIVESW1_ZPUB, - tdata.KRUX_NATIVESW1_DESCRIPTOR, + tdata.KRUX_NATIVESW1_ZPUB_DESCRIPTOR, None, ), (