Skip to content

Commit bbc3098

Browse files
Merge pull request #17 from BitGo/BTC-2652.harden-output-script-creation
feat(wasm-utxo): improve address handling and network validation
2 parents f1c03fd + 20951a5 commit bbc3098

File tree

9 files changed

+618
-51
lines changed

9 files changed

+618
-51
lines changed

packages/wasm-utxo/js/address.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { AddressNamespace } from "./wasm/wasm_utxo";
22
import type { CoinName } from "./coinName";
33

4+
/**
5+
* Most coins only have one unambiguous address format (base58check and bech32/bech32m)
6+
* For Bitcoin Cash and eCash, we can select between base58check and cashaddr.
7+
*/
48
export type AddressFormat = "default" | "cashaddr";
59

610
export function toOutputScriptWithCoin(address: string, coin: CoinName): Uint8Array {
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FixedScriptWalletNamespace } from "./wasm/wasm_utxo";
22
import type { UtxolibNetwork, UtxolibRootWalletKeys } from "./utxolibCompat";
33
import { Triple } from "./triple";
4+
import { AddressFormat } from "./address";
45

56
export type WalletKeys =
67
/** Just an xpub triple, will assume default derivation prefixes */
@@ -11,19 +12,33 @@ export type WalletKeys =
1112
/**
1213
* Create the output script for a given wallet keys and chain and index
1314
*/
14-
export function outputScript(keys: WalletKeys, chain: number, index: number): Uint8Array {
15-
return FixedScriptWalletNamespace.output_script(keys, chain, index);
15+
export function outputScript(
16+
keys: WalletKeys,
17+
chain: number,
18+
index: number,
19+
network: UtxolibNetwork,
20+
): Uint8Array {
21+
return FixedScriptWalletNamespace.output_script(keys, chain, index, network);
1622
}
1723

1824
/**
1925
* Create the address for a given wallet keys and chain and index and network.
2026
* Wrapper for outputScript that also encodes the script to an address.
27+
* @param keys - The wallet keys to use.
28+
* @param chain - The chain to use.
29+
* @param index - The index to use.
30+
* @param network - The network to use.
31+
* @param addressFormat - The address format to use.
32+
* Only relevant for Bitcoin Cash and eCash networks, where:
33+
* - "default" means base58check,
34+
* - "cashaddr" means cashaddr.
2135
*/
2236
export function address(
2337
keys: WalletKeys,
2438
chain: number,
2539
index: number,
2640
network: UtxolibNetwork,
41+
addressFormat?: AddressFormat,
2742
): string {
28-
return FixedScriptWalletNamespace.address(keys, chain, index, network);
43+
return FixedScriptWalletNamespace.address(keys, chain, index, network, addressFormat);
2944
}

0 commit comments

Comments
 (0)