Skip to content

Commit 6d57816

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): move fixed script wallet tests to separate file
Move the wallet key address derivation tests from utxolibCompat.ts to a new fixedScript/address.ts file for better organization and separation of concerns. Co-authored-by: llm-git <[email protected]>
1 parent 56cbcf9 commit 6d57816

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

packages/wasm-utxo/test/address/utxolibCompat.ts

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as utxolib from "@bitgo/utxo-lib";
55
import assert from "node:assert";
66
import {
77
utxolibCompat,
8-
FixedScriptWallet,
98
toOutputScriptWithCoin,
109
fromOutputScriptWithCoin,
1110
type CoinName,
@@ -16,37 +15,6 @@ type Triple<T> = [T, T, T];
1615

1716
type Fixture = [type: string, script: string, address: string];
1817

19-
function getAddressUtxoLib(
20-
keys: Triple<utxolib.BIP32Interface>,
21-
chain: number,
22-
index: number,
23-
network: utxolib.Network,
24-
): string {
25-
if (!utxolib.bitgo.isChainCode(chain)) {
26-
throw new Error(`Invalid chain code: ${chain}`);
27-
}
28-
29-
const walletKeys = new utxolib.bitgo.RootWalletKeys(keys);
30-
const derived = walletKeys.deriveForChainAndIndex(chain, index);
31-
const script = utxolib.bitgo.outputScripts.createOutputScript2of3(
32-
derived.publicKeys,
33-
utxolib.bitgo.outputScripts.scriptTypeForChain(chain),
34-
);
35-
const address = utxolib.address.fromOutputScript(script.scriptPubKey, network);
36-
return address;
37-
}
38-
39-
function getAddressWasm(
40-
keys: Triple<utxolib.BIP32Interface>,
41-
chain: number,
42-
index: number,
43-
network: utxolib.Network,
44-
): string {
45-
const xpubs = keys.map((key) => key.neutered().toBase58());
46-
const wasmAddress = FixedScriptWallet.address(xpubs, chain, index, network);
47-
return wasmAddress;
48-
}
49-
5018
function getCoinNameForNetwork(name: string): CoinName {
5119
switch (name) {
5220
case "bitcoin":
@@ -146,23 +114,6 @@ function runTest(network: utxolib.Network, addressFormat?: AddressFormat) {
146114
assert.deepStrictEqual(Buffer.from(scriptFromAddress), scriptBuf);
147115
}
148116
});
149-
150-
const keyTriple = utxolib.testutil.getKeyTriple("wasm");
151-
152-
const supportedChainCodes = utxolib.bitgo.chainCodes.filter((chainCode) => {
153-
const scriptType = utxolib.bitgo.outputScripts.scriptTypeForChain(chainCode);
154-
return utxolib.bitgo.outputScripts.isSupportedScriptType(network, scriptType);
155-
});
156-
157-
it(`can recreate address from wallet keys for chain codes ${supportedChainCodes.join(", ")}`, function () {
158-
for (const chainCode of supportedChainCodes) {
159-
for (let index = 0; index < 2; index++) {
160-
const utxolibAddress = getAddressUtxoLib(keyTriple, chainCode, index, network);
161-
const wasmAddress = getAddressWasm(keyTriple, chainCode, index, network);
162-
assert.strictEqual(utxolibAddress, wasmAddress);
163-
}
164-
}
165-
});
166117
});
167118
}
168119

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import assert from "node:assert";
2+
3+
import * as utxolib from "@bitgo/utxo-lib";
4+
5+
import { FixedScriptWallet } from "../../js";
6+
7+
type Triple<T> = [T, T, T];
8+
9+
function getAddressUtxoLib(
10+
keys: Triple<utxolib.BIP32Interface>,
11+
chain: number,
12+
index: number,
13+
network: utxolib.Network,
14+
): string {
15+
if (!utxolib.bitgo.isChainCode(chain)) {
16+
throw new Error(`Invalid chain code: ${chain}`);
17+
}
18+
19+
const walletKeys = new utxolib.bitgo.RootWalletKeys(keys);
20+
const derived = walletKeys.deriveForChainAndIndex(chain, index);
21+
const script = utxolib.bitgo.outputScripts.createOutputScript2of3(
22+
derived.publicKeys,
23+
utxolib.bitgo.outputScripts.scriptTypeForChain(chain),
24+
);
25+
const address = utxolib.address.fromOutputScript(script.scriptPubKey, network);
26+
return address;
27+
}
28+
29+
function getAddressWasm(
30+
keys: Triple<utxolib.BIP32Interface>,
31+
chain: number,
32+
index: number,
33+
network: utxolib.Network,
34+
): string {
35+
const xpubs = keys.map((key) => key.neutered().toBase58());
36+
const wasmAddress = FixedScriptWallet.address(xpubs, chain, index, network);
37+
return wasmAddress;
38+
}
39+
40+
function runTest(network: utxolib.Network) {
41+
describe(`address for network ${utxolib.getNetworkName(network)}`, function () {
42+
const keyTriple = utxolib.testutil.getKeyTriple("wasm");
43+
44+
const supportedChainCodes = utxolib.bitgo.chainCodes.filter((chainCode) => {
45+
const scriptType = utxolib.bitgo.outputScripts.scriptTypeForChain(chainCode);
46+
return utxolib.bitgo.outputScripts.isSupportedScriptType(network, scriptType);
47+
});
48+
49+
it(`can recreate address from wallet keys for chain codes ${supportedChainCodes.join(", ")}`, function () {
50+
for (const chainCode of supportedChainCodes) {
51+
for (let index = 0; index < 2; index++) {
52+
const utxolibAddress = getAddressUtxoLib(keyTriple, chainCode, index, network);
53+
const wasmAddress = getAddressWasm(keyTriple, chainCode, index, network);
54+
assert.strictEqual(utxolibAddress, wasmAddress);
55+
}
56+
}
57+
});
58+
});
59+
}
60+
61+
utxolib.getNetworkList().forEach((network) => {
62+
runTest(network);
63+
});

0 commit comments

Comments
 (0)