Skip to content

Commit 843c6de

Browse files
refactor: move descriptor utility functions to a separate file
Issue: BTC-1348
1 parent 7153476 commit 843c6de

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as utxolib from "@bitgo/utxo-lib";
2+
3+
/** Expand a template with the given root wallet keys and chain code */
4+
function expand(template: string, rootWalletKeys: utxolib.bitgo.RootWalletKeys, chainCode: number) {
5+
return template.replace(/\$([0-9])/g, (_, i) => {
6+
const keyIndex = parseInt(i, 10);
7+
if (keyIndex !== 0 && keyIndex !== 1 && keyIndex !== 2) {
8+
throw new Error("Invalid key index");
9+
}
10+
const xpub = rootWalletKeys.triple[keyIndex].neutered().toBase58();
11+
const prefix = rootWalletKeys.derivationPrefixes[keyIndex];
12+
return xpub + "/" + prefix + "/" + chainCode + "/*";
13+
});
14+
}
15+
16+
/**
17+
* Get a standard output descriptor that corresponds to the proprietary HD wallet setup
18+
* used in BitGo wallets.
19+
* Only supports a subset of script types.
20+
*/
21+
export function getDescriptorForScriptType(
22+
rootWalletKeys: utxolib.bitgo.RootWalletKeys,
23+
scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3,
24+
scope: "internal" | "external",
25+
): string {
26+
const chain =
27+
scope === "external"
28+
? utxolib.bitgo.getExternalChainCode(scriptType)
29+
: utxolib.bitgo.getInternalChainCode(scriptType);
30+
switch (scriptType) {
31+
case "p2sh":
32+
return expand("sh(multi(2,$0,$1,$2))", rootWalletKeys, chain);
33+
case "p2shP2wsh":
34+
return expand("sh(wsh(multi(2,$0,$1,$2)))", rootWalletKeys, chain);
35+
case "p2wsh":
36+
return expand("wsh(multi(2,$0,$1,$2))", rootWalletKeys, chain);
37+
default:
38+
throw new Error(`Unsupported script type ${scriptType}`);
39+
}
40+
}

packages/wasm-miniscript/test/fixedScriptToDescriptor.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,7 @@
11
import * as assert from "assert";
22
import * as utxolib from "@bitgo/utxo-lib";
33
import { Descriptor } from "../js";
4-
5-
/** Expand a template with the given root wallet keys and chain code */
6-
function expand(template: string, rootWalletKeys: utxolib.bitgo.RootWalletKeys, chainCode: number) {
7-
return template.replace(/\$([0-9])/g, (_, i) => {
8-
const keyIndex = parseInt(i, 10);
9-
if (keyIndex !== 0 && keyIndex !== 1 && keyIndex !== 2) {
10-
throw new Error("Invalid key index");
11-
}
12-
const xpub = rootWalletKeys.triple[keyIndex].neutered().toBase58();
13-
const prefix = rootWalletKeys.derivationPrefixes[keyIndex];
14-
return xpub + "/" + prefix + "/" + chainCode + "/*";
15-
});
16-
}
17-
18-
/**
19-
* Get a standard output descriptor that corresponds to the proprietary HD wallet setup
20-
* used in BitGo wallets.
21-
* Only supports a subset of script types.
22-
*/
23-
function getDescriptorForScriptType(
24-
rootWalletKeys: utxolib.bitgo.RootWalletKeys,
25-
scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3,
26-
scope: "internal" | "external",
27-
): string {
28-
const chain =
29-
scope === "external"
30-
? utxolib.bitgo.getExternalChainCode(scriptType)
31-
: utxolib.bitgo.getInternalChainCode(scriptType);
32-
switch (scriptType) {
33-
case "p2sh":
34-
return expand("sh(multi(2,$0,$1,$2))", rootWalletKeys, chain);
35-
case "p2shP2wsh":
36-
return expand("sh(wsh(multi(2,$0,$1,$2)))", rootWalletKeys, chain);
37-
case "p2wsh":
38-
return expand("wsh(multi(2,$0,$1,$2))", rootWalletKeys, chain);
39-
default:
40-
throw new Error(`Unsupported script type ${scriptType}`);
41-
}
42-
}
4+
import { getDescriptorForScriptType } from "./descriptorUtil";
435

446
const rootWalletKeys = new utxolib.bitgo.RootWalletKeys(utxolib.testutil.getKeyTriple("wasm"));
457
const scriptTypes = ["p2sh", "p2shP2wsh", "p2wsh"] as const;

0 commit comments

Comments
 (0)