Repository navigation
WCN-3934 Recognise bip44 wallet prefix in PSBT parsing, wallet prefix helpers #458
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
Merged
davidkaplanbitgo
merged 1 commit into
master
from
davidkaplan/wcn-3934-bitgowasm-wasm-utxo-per-wallet-derivation-prefix-path
Nov 18, 2026
+1,169
−58
Merged
Changes from all commits
Commits
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
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
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
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,70 @@ | ||
| import { BIP32, type BIP32Arg } from "../bip32.js"; | ||
| import type { Triple } from "../triple.js"; | ||
| import { FixedScriptWalletNamespace } from "../wasm/wasm_utxo.js"; | ||
| import { RootWalletKeys, assertCoinTypeU32, type WalletKeysArg } from "./RootWalletKeys.js"; | ||
|
|
||
| /** | ||
| * The three wallet root keys in `[user, backup, bitgo]` order. | ||
| * | ||
| * Accepts everything {@link RootWalletKeys.from} accepts, plus a triple of any {@link BIP32Arg} | ||
| * (xpub/xprv strings, `BIP32` instances, `BIP32Interface` objects). Private keys are neutered | ||
| * before use: the result is the same for an xprv and the matching xpub. | ||
| */ | ||
| export type WalletPrefixKeysArg = WalletKeysArg | Triple<BIP32Arg>; | ||
|
|
||
| function toBIP32Triple(keys: WalletPrefixKeysArg): Triple<BIP32> { | ||
| if (keys instanceof RootWalletKeys) { | ||
| return [keys.userKey(), keys.backupKey(), keys.bitgoKey()]; | ||
| } | ||
| if (Array.isArray(keys)) { | ||
| return (keys as Triple<BIP32Arg>).map((key) => BIP32.from(key)) as Triple<BIP32>; | ||
| } | ||
| return keys.triple.map((key) => BIP32.from(key)) as Triple<BIP32>; | ||
| } | ||
|
|
||
| /** | ||
| * Compute the `[h1, h2]` account indices of a wallet from its three root keys. | ||
| * | ||
| * ``` | ||
| * d = sha256("BitGo/utxo/wallet-prefix/v1" || K_user || K_backup || K_bitgo) | ||
| * K = compressed public key (33 bytes) || chain code (32 bytes) (of the neutered root key) | ||
| * h1 = u32be(d[0..4]) & 0x7fffffff | ||
| * h2 = u32be(d[4..8]) & 0x7fffffff | ||
| * ``` | ||
| * | ||
| * Both are valid non-hardened BIP32 indices (`0..=2^31-1`). The result depends on the key order | ||
| * `[user, backup, bitgo]`. These are indices, not amounts, hence `number`. | ||
| * | ||
| * @param keys - The three wallet root keys `[user, backup, bitgo]` | ||
| */ | ||
| export function deriveWalletAccount(keys: WalletPrefixKeysArg): [number, number] { | ||
| const [user, backup, bitgo] = toBIP32Triple(keys); | ||
| const account = FixedScriptWalletNamespace.derive_wallet_account( | ||
| user.wasm, | ||
| backup.wasm, | ||
| bitgo.wasm, | ||
| ); | ||
| return [account[0], account[1]]; | ||
| } | ||
|
|
||
| /** | ||
| * Compute the relative per-wallet derivation prefix `44/<coinType>/<h1>/<h2>` (no leading `/` or | ||
| * `m/`), see {@link deriveWalletAccount}. Addresses of a wallet using this prefix derive at | ||
| * `<prefix>/<chain>/<index>` from each of the three root keys. | ||
| * | ||
| * @param keys - The three wallet root keys `[user, backup, bitgo]` | ||
| * @param coinType - BIP44 coin type of the coin family, an integer in `0..=2^31-1`. wasm-utxo has | ||
| * no coin registry: the caller supplies it (e.g. `coin.bip44CoinType` from `@bitgo/statics`). | ||
| * @throws a `WasmUtxoError` with code `WalletPrefixError.CoinTypeOutOfRange` if `coinType` is | ||
| * larger than `2^31-1` | ||
| */ | ||
| export function deriveWalletPrefix(keys: WalletPrefixKeysArg, coinType: number): string { | ||
| assertCoinTypeU32(coinType); | ||
| const [user, backup, bitgo] = toBIP32Triple(keys); | ||
| return FixedScriptWalletNamespace.derive_wallet_prefix( | ||
| user.wasm, | ||
| backup.wasm, | ||
| bitgo.wasm, | ||
| coinType, | ||
| ); | ||
| } |
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
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.
🟡 nit (optional): Callers passing an out-of-range coin type get two different error types for the same mistake, which breaks the documented
@ throwscontract.assertCoinTypeU32allows values up to0xffffffff, so2^31..2^32-1reach Rust and fail with the typedWalletPrefixError.CoinTypeOutOfRange. Larger values (e.g.2**32,1e10) throw a plainRangeErrorinstead. That message says "0..=2^31-1", a range the check itself does not apply. Fix: give every value above0x7fffffffone error type and code, or document theRangeErrorcase in the@ throwstext ofwithBip44CoinType,deriveWalletPrefixandgetWalletKeysFromPsbt.Why this was flagged
The JSDoc of
withBip44CoinType(RootWalletKeys.ts:150-151) andderiveWalletPrefix(walletPrefix.ts:59-60) says each throws "aWasmUtxoErrorwith codeWalletPrefixError.CoinTypeOutOfRangeifcoinTypeis larger than2^31-1". Both first callassertCoinTypeU32(RootWalletKeys.ts:12-16), which passes any integer up to0xffffffffand throwsRangeErrorfor anything larger. SowithBip44CoinType(0x80000000)throws a WasmUtxoError that passesisWasmUtxoError, whilewithBip44CoinType(2 ** 32)throws a RangeError with nocode. A caller that branches one.code === "WalletPrefixError.CoinTypeOutOfRange", as the docs and test/fixedScript/bip44Prefix.ts:50 suggest, misses the second case. The RangeError message "coinType must be an integer in 0..=2^31-1" also states a bound the check does not enforce.getWalletKeysFromPsbt(BitGoPsbt.ts:1119-1121) uses the same helper and has the same split. All of this is new API, so no base behaviour exists to compare against.Verification: RootWalletKeys.ts:13-14 rejects only
coinType > 0xffffffff, throwing a plain RangeError naming 0..=2^31-1. Values in 2^31..2^32-1 reach Rust, where wallet_prefix.rs:86 and :98 returnWalletPrefixError::CoinTypeOutOfRangeas a typed WasmUtxoError. The JSDoc at RootWalletKeys.ts:150-151 and walletPrefix.ts:58-59 promises that code for all values above 2^31-1, sowithBip44CoinType(2**32)breaks it.