Skip to content

Commit

Permalink
loaded descriptors no longer forced to "xpub" version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Do committed Dec 10, 2024
1 parent 213224e commit f6bb3c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
24 changes: 12 additions & 12 deletions src/krux/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(xpub6C1dUaopHgps26SpkdwL28cS5WFH7Xyaut4HLKZq8Jg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Q68jGz2M6dfh2zJrUv))"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
),
(
Expand All @@ -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,
),
(
Expand Down

0 comments on commit f6bb3c2

Please sign in to comment.