-
Notifications
You must be signed in to change notification settings - Fork 104
feat(wallets): add BC-UR xpub import support #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jbrauck-unchained
wants to merge
2
commits into
caravan-bitcoin:main
Choose a base branch
from
jbrauck-unchained:codex/passport-seedsigner-bcur-xpub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@caravan/wallets": minor | ||
| --- | ||
|
|
||
| Add BC-UR extended-public-key decoding for Passport Sparrow multisig exports | ||
| and SeedSigner keys on test-family networks. |
213 changes: 213 additions & 0 deletions
213
packages/caravan-wallets/src/bcur2/__tests__/extendedPublicKeyDecoder.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| import { ExtendedPublicKey, Network } from "@caravan/bitcoin"; | ||
| import { | ||
| Bytes, | ||
| CryptoHDKey, | ||
| CryptoKeypath, | ||
| CryptoPSBT, | ||
| PathComponent, | ||
| RegistryItem, | ||
| URRegistryDecoder, | ||
| } from "@keystonehq/bc-ur-registry"; | ||
|
|
||
| import { BCUR2Decoder } from "../decoder"; | ||
| import { | ||
| BCUR2ExtendedPublicKeyDecoder, | ||
| PASSPORT_MULTISIG_EXPORT_ERROR, | ||
| } from "../extendedPublicKeyDecoder"; | ||
|
|
||
| const PASSPORT_MULTISIG_EXPORT = { | ||
| p2sh_deriv: "m/45'", | ||
| p2sh: "tpubDA2HtQKGFGx9BPZQ3yemoxaH6tjBkKbwTc4mMqpvkvu2RSkmKgADtCVaCpV4iDhXnqb46iQ7PjMMVzU6MERq7tNoLJ8rEvaYSStJssFKfvb", | ||
| p2sh_p2wsh_deriv: "m/48'/1'/0'/1'", | ||
| p2sh_p2wsh: | ||
| "Upub5SRh9Zozi9attVLrU2hezfntv3kUNwbeRa3zJr6aV1pnmEUtfgZnLszfFKJyULFMbEcogARAsKosJBgaN8AmotgvbVgJ78srDmj59wzuTP7", | ||
| p2wsh_deriv: "m/48'/1'/0'/2'", | ||
| p2wsh: | ||
| "Vpub5mFxTEUurq8NoPJfC1T9dCYvSYrorqSRnWgCjbEBwZ2coBhBej9f3TK6tbz5m27sVn4TAY2KsbmN1k2oi2J2NcWJabKGSHdgXxJaJ4V8YMb", | ||
| xfp: "EFA5D916", | ||
| }; | ||
|
|
||
| // SeedSigner 0.8.7 testnet vector from tests/test_encodepsbtqr.py. | ||
| const SEEDSIGNER_TESTNET_CRYPTO_ACCOUNT = [ | ||
| "UR:CRYPTO-ACCOUNT/1-5/LPADAHCSKECYRTPEDKMOHDCFOEADCYSSMECPONAOLYTAADMETAADDLONAXHDCLAOKSRLNLKPUENSAHBTHS", | ||
| "UR:CRYPTO-ACCOUNT/2-5/LPAOAHCSKECYRTPEDKMOHDCFGYATHPMNSNKKGHZMLUZORPVDGUOTECSTTKTOLPCWPTNTLKZTTIZTNDJSCF", | ||
| "UR:CRYPTO-ACCOUNT/3-5/LPAXAHCSKECYRTPEDKMOHDCFZTBEAAHDCXVDTPMYRSTDSPZSBZSPGERLGDATUYNLPYBTGYIYYKBDFGWPKE", | ||
| "UR:CRYPTO-ACCOUNT/4-5/LPAAAHCSKECYRTPEDKMOHDCFBTWTAOSWKSVTSGCHBYDKYAVDAHTAADEHOYAOADAMTAADDYOTADGYBKBWFE", | ||
| "UR:CRYPTO-ACCOUNT/5-5/LPAHAHCSKECYRTPEDKMOHDCFLOCSDYYKADYKAEYKAOYKAOCYSSMECPONAXAAAYCYIOREKKJKAETODLFYWP", | ||
| ]; | ||
|
|
||
| const SEEDSIGNER_TESTNET_XPUB = | ||
| "tpubDEfkEY1bXf2FvRVCxiMRXWZPrEaxkMdwoVnjWhGnP42kk2ZPfkB86p5rLEjAVc7YgVGuUQWPPo6mbwTt9qXEW4YUyQXkkpQ5uJdppanC7rL"; | ||
|
|
||
| function makeHDKey(): CryptoHDKey { | ||
| const origin = new CryptoKeypath( | ||
| [new PathComponent({ index: 45, hardened: true })], | ||
| Buffer.from("efa5d916", "hex"), | ||
| 1, | ||
| ); | ||
|
|
||
| return new CryptoHDKey({ | ||
| isMaster: false, | ||
| key: Buffer.from( | ||
| "039b9ba1ad522fa2c4fc550c23626c2fb352373e22fbb8d59b984d058affe97e18", | ||
| "hex", | ||
| ), | ||
| chainCode: Buffer.from( | ||
| "6ff1bd910b424c55d269864c9f9ee9e3a5b20b034999ba83cb17f5635f56077c", | ||
| "hex", | ||
| ), | ||
| origin, | ||
| parentFingerprint: Buffer.from("efa5d916", "hex"), | ||
| }); | ||
| } | ||
|
|
||
| function scan( | ||
| item: RegistryItem, | ||
| network = Network.TESTNET, | ||
| ): BCUR2ExtendedPublicKeyDecoder { | ||
| const decoder = new BCUR2ExtendedPublicKeyDecoder({ network }); | ||
| item | ||
| .toUREncoder(100) | ||
| .encodeWhole() | ||
| .forEach((fragment) => decoder.receivePart(fragment)); | ||
| return decoder; | ||
| } | ||
|
|
||
| function scanPassport( | ||
| value: unknown, | ||
| network = Network.TESTNET, | ||
| ): BCUR2ExtendedPublicKeyDecoder { | ||
| return scan(new Bytes(Buffer.from(JSON.stringify(value))), network); | ||
| } | ||
|
|
||
| describe("BCUR2ExtendedPublicKeyDecoder", () => { | ||
| it.each([Network.TESTNET, Network.REGTEST, Network.SIGNET])( | ||
| "decodes SeedSigner's testnet account on %s", | ||
| (network) => { | ||
| const decoder = new BCUR2ExtendedPublicKeyDecoder({ network }); | ||
| SEEDSIGNER_TESTNET_CRYPTO_ACCOUNT.forEach((fragment) => | ||
| decoder.receivePart(fragment), | ||
| ); | ||
|
|
||
| expect(decoder.getDecodedData()).toEqual({ | ||
| type: "crypto-account", | ||
| xpub: SEEDSIGNER_TESTNET_XPUB, | ||
| rootFingerprint: "C49122A5", | ||
| bip32Path: "48'/1'/0'/2'", | ||
| }); | ||
| expect(decoder.getError()).toBeNull(); | ||
| }, | ||
| ); | ||
|
|
||
| it("uses test-family serialization for a standalone HD key on regtest", () => { | ||
| const decoder = scan(makeHDKey(), Network.REGTEST); | ||
|
|
||
| expect(decoder.getDecodedData()).toEqual({ | ||
| type: "crypto-hdkey", | ||
| xpub: PASSPORT_MULTISIG_EXPORT.p2sh, | ||
| rootFingerprint: "EFA5D916", | ||
| bip32Path: "45'", | ||
| }); | ||
| }); | ||
|
|
||
| it("decodes Passport's bytes export as its source key", () => { | ||
| const decoder = scanPassport(PASSPORT_MULTISIG_EXPORT); | ||
|
|
||
| expect(decoder.getDecodedData()).toEqual({ | ||
| type: "bytes", | ||
| xpub: PASSPORT_MULTISIG_EXPORT.p2sh, | ||
| rootFingerprint: "efa5d916", | ||
| bip32Path: "45'", | ||
| }); | ||
| expect(decoder.getError()).toBeNull(); | ||
| }); | ||
|
|
||
| it("accepts Passport's historical wrapped-SegWit field alias", () => { | ||
| const { p2sh_p2wsh_deriv, p2sh_p2wsh, ...rest } = PASSPORT_MULTISIG_EXPORT; | ||
| const decoder = scanPassport({ | ||
| ...rest, | ||
| p2wsh_p2sh_deriv: p2sh_p2wsh_deriv, | ||
| p2wsh_p2sh: p2sh_p2wsh, | ||
| }); | ||
|
|
||
| expect(decoder.getDecodedData()?.xpub).toBe(PASSPORT_MULTISIG_EXPORT.p2sh); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["arbitrary bytes", { hello: "world" }], | ||
| [ | ||
| "the wrong source path", | ||
| { ...PASSPORT_MULTISIG_EXPORT, p2sh_deriv: "m/84'/1'/0'" }, | ||
| ], | ||
| [ | ||
| "a mismatched fingerprint", | ||
| { ...PASSPORT_MULTISIG_EXPORT, xfp: "00000000" }, | ||
| ], | ||
| ])("rejects %s", (_label, value) => { | ||
| const decoder = scanPassport(value); | ||
|
|
||
| expect(decoder.getDecodedData()).toBeNull(); | ||
| expect(decoder.getError()).toBe(PASSPORT_MULTISIG_EXPORT_ERROR); | ||
| }); | ||
|
|
||
| it("rejects a key from the wrong serialization family", () => { | ||
| const decoder = scanPassport(PASSPORT_MULTISIG_EXPORT, Network.MAINNET); | ||
|
|
||
| expect(decoder.getDecodedData()).toBeNull(); | ||
| expect(decoder.getError()).toBe(PASSPORT_MULTISIG_EXPORT_ERROR); | ||
| }); | ||
|
|
||
| it("rejects a key that does not represent m/45'", () => { | ||
| const key = ExtendedPublicKey.fromBase58(PASSPORT_MULTISIG_EXPORT.p2sh); | ||
| key.index = 0x8000002e; | ||
| const decoder = scanPassport({ | ||
| ...PASSPORT_MULTISIG_EXPORT, | ||
| p2sh: key.toBase58(), | ||
| }); | ||
|
|
||
| expect(decoder.getDecodedData()).toBeNull(); | ||
| expect(decoder.getError()).toBe(PASSPORT_MULTISIG_EXPORT_ERROR); | ||
| }); | ||
|
|
||
| it("rejects PSBTs in the key-only decoder", () => { | ||
| const decoder = scan(new CryptoPSBT(Buffer.from([1, 2, 3]))); | ||
|
|
||
| expect(decoder.getDecodedData()).toBeNull(); | ||
| expect(decoder.getError()).toBe( | ||
| "QR code contains a PSBT, not an extended public key.", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps bytes unsupported in the generic decoder", () => { | ||
| const item = new Bytes( | ||
| Buffer.from(JSON.stringify(PASSPORT_MULTISIG_EXPORT)), | ||
| ); | ||
| const decoder = new BCUR2Decoder(); | ||
| item | ||
| .toUREncoder(100) | ||
| .encodeWhole() | ||
| .forEach((fragment) => decoder.receivePart(fragment)); | ||
|
|
||
| expect(decoder.getDecodedData(Network.TESTNET)).toBeNull(); | ||
| expect(decoder.getError()).toBe("Unsupported UR type: bytes"); | ||
| }); | ||
|
|
||
| it("decodes only the supplied CBOR view", () => { | ||
| const cbor = makeHDKey().toCBOR(); | ||
| const backing = Buffer.alloc(cbor.byteLength + 8, 0xff); | ||
| cbor.copy(backing, 4); | ||
| const registryDecoder = { | ||
| isComplete: () => true, | ||
| getProgress: () => 1, | ||
| resultUR: () => ({ | ||
| type: "crypto-hdkey", | ||
| cbor: backing.subarray(4, 4 + cbor.byteLength), | ||
| }), | ||
| } as unknown as URRegistryDecoder; | ||
| const decoder = new BCUR2Decoder(registryDecoder); | ||
|
|
||
| expect(decoder.getDecodedData(Network.TESTNET)?.xpub).toBe( | ||
| PASSPORT_MULTISIG_EXPORT.p2sh, | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,7 @@ export interface ExtendedPublicKeyData { | |
| * - crypto-hdkey: Contains hierarchical deterministic keys | ||
| */ | ||
| export class BCUR2Decoder { | ||
| private decoder: URRegistryDecoder; | ||
| protected decoder: URRegistryDecoder; | ||
|
|
||
| private error: string | null = null; | ||
|
|
||
|
|
@@ -254,7 +254,7 @@ export class BCUR2Decoder { | |
|
|
||
| const decodedResult = this.handleDecodedResult( | ||
| result.type, | ||
| Buffer.from(result.cbor.buffer), | ||
| Buffer.from(result.cbor), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's requiring this change? |
||
| network, | ||
| ); | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this actually be a constraint? If so, why?