Skip to content

Commit a7274db

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): restructure fixedScriptWallet into directory
Reorganize fixedScriptWallet into a proper directory structure with separate files for distinct functionality. Move RootWalletKeys and address functions into their own modules while keeping the same API through the index file. Issue: BTC-2786 Co-authored-by: llm-git <[email protected]>
1 parent 878d8f1 commit a7274db

File tree

8 files changed

+76
-65
lines changed

8 files changed

+76
-65
lines changed

packages/wasm-utxo/js/fixedScriptWallet.ts renamed to packages/wasm-utxo/js/fixedScriptWallet/BitGoPsbt.ts

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
1-
import { FixedScriptWalletNamespace } from "./wasm/wasm_utxo.js";
2-
import { type WalletKeysArg, RootWalletKeys } from "./WalletKeys.js";
3-
import { type BIP32Arg, BIP32 } from "./bip32.js";
4-
import { type ECPairArg, ECPair } from "./ecpair.js";
5-
import type { UtxolibName, UtxolibNetwork } from "./utxolibCompat.js";
6-
import type { CoinName } from "./coinName.js";
7-
import { AddressFormat } from "./address.js";
1+
import { BitGoPsbt as WasmBitGoPsbt } from "../wasm/wasm_utxo.js";
2+
import { type WalletKeysArg, RootWalletKeys } from "./RootWalletKeys.js";
3+
import { type BIP32Arg, BIP32 } from "../bip32.js";
4+
import { type ECPairArg, ECPair } from "../ecpair.js";
5+
import type { UtxolibName } from "../utxolibCompat.js";
6+
import type { CoinName } from "../coinName.js";
87

98
export type NetworkName = UtxolibName | CoinName;
109

11-
/**
12-
* Create the output script for a given wallet keys and chain and index
13-
*/
14-
export function outputScript(
15-
keys: WalletKeysArg,
16-
chain: number,
17-
index: number,
18-
network: UtxolibNetwork,
19-
): Uint8Array {
20-
const walletKeys = RootWalletKeys.from(keys);
21-
return FixedScriptWalletNamespace.output_script(walletKeys.wasm, chain, index, network);
22-
}
23-
24-
/**
25-
* Create the address for a given wallet keys and chain and index and network.
26-
* 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.
35-
*/
36-
export function address(
37-
keys: WalletKeysArg,
38-
chain: number,
39-
index: number,
40-
network: UtxolibNetwork,
41-
addressFormat?: AddressFormat,
42-
): string {
43-
const walletKeys = RootWalletKeys.from(keys);
44-
return FixedScriptWalletNamespace.address(walletKeys.wasm, chain, index, network, addressFormat);
45-
}
46-
4710
type ReplayProtection =
4811
| {
4912
outputScripts: Uint8Array[];
@@ -86,8 +49,6 @@ export type ParsedTransaction = {
8649
virtualSize: number;
8750
};
8851

89-
import { BitGoPsbt as WasmBitGoPsbt } from "./wasm/wasm_utxo.js";
90-
9152
export class BitGoPsbt {
9253
private constructor(private wasm: WasmBitGoPsbt) {}
9354

packages/wasm-utxo/js/WalletKeys.ts renamed to packages/wasm-utxo/js/fixedScriptWallet/RootWalletKeys.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { BIP32Interface } from "./bip32.js";
2-
import { BIP32 } from "./bip32.js";
3-
import { Triple } from "./triple.js";
4-
import { WasmRootWalletKeys, WasmBIP32 } from "./wasm/wasm_utxo.js";
1+
import type { BIP32Interface } from "../bip32.js";
2+
import { BIP32 } from "../bip32.js";
3+
import { Triple } from "../triple.js";
4+
import { WasmRootWalletKeys, WasmBIP32 } from "../wasm/wasm_utxo.js";
55

66
/**
77
* IWalletKeys represents the various forms that wallet keys can take
@@ -21,21 +21,21 @@ export type WalletKeysArg =
2121
| RootWalletKeys;
2222

2323
/**
24-
* Convert WalletKeysArg to a triple of WasmBIP32 instances
24+
* Convert WalletKeysArg to a triple of BIP32 instances
2525
*/
26-
function toBIP32Triple(keys: WalletKeysArg): Triple<WasmBIP32> {
26+
function toBIP32Triple(keys: WalletKeysArg): Triple<BIP32> {
2727
if (keys instanceof RootWalletKeys) {
28-
return [keys.userKey().wasm, keys.backupKey().wasm, keys.bitgoKey().wasm];
28+
return [keys.userKey(), keys.backupKey(), keys.bitgoKey()];
2929
}
3030

3131
// Check if it's an IWalletKeys object
3232
if (typeof keys === "object" && "triple" in keys) {
3333
// Extract BIP32 keys from the triple
34-
return keys.triple.map((key) => BIP32.from(key).wasm) as Triple<WasmBIP32>;
34+
return keys.triple.map((key) => BIP32.from(key)) as Triple<BIP32>;
3535
}
3636

3737
// Otherwise it's a triple of strings (xpubs)
38-
return keys.map((xpub) => WasmBIP32.from_xpub(xpub)) as Triple<WasmBIP32>;
38+
return keys.map((xpub) => BIP32.fromWasm(WasmBIP32.from_xpub(xpub))) as Triple<BIP32>;
3939
}
4040

4141
/**
@@ -69,14 +69,14 @@ export class RootWalletKeys {
6969

7070
const wasm = derivationPrefixes
7171
? WasmRootWalletKeys.with_derivation_prefixes(
72-
user,
73-
backup,
74-
bitgo,
72+
user.wasm,
73+
backup.wasm,
74+
bitgo.wasm,
7575
derivationPrefixes[0],
7676
derivationPrefixes[1],
7777
derivationPrefixes[2],
7878
)
79-
: new WasmRootWalletKeys(user, backup, bitgo);
79+
: new WasmRootWalletKeys(user.wasm, backup.wasm, bitgo.wasm);
8080

8181
return new RootWalletKeys(wasm);
8282
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { FixedScriptWalletNamespace } from "../wasm/wasm_utxo.js";
2+
import { type WalletKeysArg, RootWalletKeys } from "./RootWalletKeys.js";
3+
import type { UtxolibNetwork } from "../utxolibCompat.js";
4+
import { AddressFormat } from "../address.js";
5+
6+
/**
7+
* Create the output script for a given wallet keys and chain and index
8+
*/
9+
export function outputScript(
10+
keys: WalletKeysArg,
11+
chain: number,
12+
index: number,
13+
network: UtxolibNetwork,
14+
): Uint8Array {
15+
const walletKeys = RootWalletKeys.from(keys);
16+
return FixedScriptWalletNamespace.output_script(walletKeys.wasm, chain, index, network);
17+
}
18+
19+
/**
20+
* Create the address for a given wallet keys and chain and index and network.
21+
* Wrapper for outputScript that also encodes the script to an address.
22+
* @param keys - The wallet keys to use.
23+
* @param chain - The chain to use.
24+
* @param index - The index to use.
25+
* @param network - The network to use.
26+
* @param addressFormat - The address format to use.
27+
* Only relevant for Bitcoin Cash and eCash networks, where:
28+
* - "default" means base58check,
29+
* - "cashaddr" means cashaddr.
30+
*/
31+
export function address(
32+
keys: WalletKeysArg,
33+
chain: number,
34+
index: number,
35+
network: UtxolibNetwork,
36+
addressFormat?: AddressFormat,
37+
): string {
38+
const walletKeys = RootWalletKeys.from(keys);
39+
return FixedScriptWalletNamespace.address(walletKeys.wasm, chain, index, network, addressFormat);
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export { RootWalletKeys, type WalletKeysArg, type IWalletKeys } from "./RootWalletKeys.js";
2+
export { outputScript, address } from "./address.js";
3+
export {
4+
BitGoPsbt,
5+
type NetworkName,
6+
type ScriptId,
7+
type InputScriptType,
8+
type ParsedInput,
9+
type ParsedOutput,
10+
type ParsedTransaction,
11+
} from "./BitGoPsbt.js";

packages/wasm-utxo/js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ void wasm;
77
export * as address from "./address.js";
88
export * as ast from "./ast/index.js";
99
export * as utxolibCompat from "./utxolibCompat.js";
10-
export * as fixedScriptWallet from "./fixedScriptWallet.js";
10+
export * as fixedScriptWallet from "./fixedScriptWallet/index.js";
1111
export * as bip32 from "./bip32.js";
1212
export * as ecpair from "./ecpair.js";
1313

packages/wasm-utxo/test/fixedScript/fixtureUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as fs from "node:fs";
22
import * as path from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { dirname } from "node:path";
5-
import type { IWalletKeys } from "../../js/WalletKeys.js";
5+
import type { IWalletKeys } from "../../js/fixedScriptWallet/RootWalletKeys.js";
66
import { BIP32, type BIP32Interface } from "../../js/bip32.js";
7-
import { RootWalletKeys } from "../../js/WalletKeys.js";
7+
import { RootWalletKeys } from "../../js/fixedScriptWallet/RootWalletKeys.js";
88

99
const __filename = fileURLToPath(import.meta.url);
1010
const __dirname = dirname(__filename);

packages/wasm-utxo/test/fixedScript/parseTransactionWithWalletKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from "node:assert";
22
import * as utxolib from "@bitgo/utxo-lib";
33
import { fixedScriptWallet } from "../../js/index.js";
4-
import { BitGoPsbt, InputScriptType } from "../../js/fixedScriptWallet.js";
5-
import type { RootWalletKeys } from "../../js/WalletKeys.js";
4+
import { BitGoPsbt, InputScriptType } from "../../js/fixedScriptWallet/index.js";
5+
import type { RootWalletKeys } from "../../js/fixedScriptWallet/RootWalletKeys.js";
66
import {
77
loadPsbtFixture,
88
loadWalletKeysFromFixture,

packages/wasm-utxo/test/fixedScript/verifySignature.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import assert from "node:assert";
22
import * as utxolib from "@bitgo/utxo-lib";
33
import { fixedScriptWallet, BIP32 } from "../../js/index.js";
4-
import { BitGoPsbt } from "../../js/fixedScriptWallet.js";
5-
import type { RootWalletKeys } from "../../js/WalletKeys.js";
4+
import { BitGoPsbt, RootWalletKeys } from "../../js/fixedScriptWallet/index.js";
65
import {
76
loadPsbtFixture,
87
loadWalletKeysFromFixture,

0 commit comments

Comments
 (0)