|
1 | 1 | import * as assert from "assert"; |
2 | 2 | import * as utxolib from "@bitgo/utxo-lib"; |
3 | 3 | 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"; |
43 | 5 |
|
44 | 6 | const rootWalletKeys = new utxolib.bitgo.RootWalletKeys(utxolib.testutil.getKeyTriple("wasm")); |
45 | 7 | const scriptTypes = ["p2sh", "p2shP2wsh", "p2wsh"] as const; |
|
0 commit comments