|
| 1 | +/* |
| 2 | +
|
| 3 | +This file contains type definitions for building an Abstract Syntax Tree for |
| 4 | +Bitcoin Descriptors and Miniscript expressions. |
| 5 | +
|
| 6 | +Currently, the types do not encode any validity or soundness checks, so it is |
| 7 | +possible to construct invalid descriptors. |
| 8 | +
|
| 9 | +*/ |
| 10 | +type Key = string; |
| 11 | + |
| 12 | +// https://bitcoin.sipa.be/miniscript/ |
| 13 | +// r is for custom bitgo extension OP_DROP |
| 14 | +type Identities = 'a' | 's' | 'c' | 't' | 'd' | 'v' | 'j' | 'n' | 'l' | 'u' | 'r'; |
| 15 | + |
| 16 | +// Union of all possible prefixes: { f: T } => { 'a:f': T } | { 's:f': T } | ... |
| 17 | +type PrefixWith<T, P extends string> = { |
| 18 | + [K in keyof T & string as `${P}:${K}`]: T[K]; |
| 19 | +}; |
| 20 | +type PrefixIdUnion<T> = { [P in Identities]: PrefixWith<T, P> }[Identities]; |
| 21 | + |
| 22 | +// Wrap a type with a union of all possible prefixes |
| 23 | +type Wrap<T> = T | PrefixIdUnion<T>; |
| 24 | + |
| 25 | +type Miniscript = |
| 26 | + | Wrap<{ pk: Key }> |
| 27 | + | Wrap<{ pkh: Key }> |
| 28 | + | Wrap<{ wpkh: Key }> |
| 29 | + | Wrap<{ multi: [number, ...Key[]] }> |
| 30 | + | Wrap<{ sortedmulti: [number, ...Key[]] }> |
| 31 | + | Wrap<{ multi_a: [number, ...Key[]] }> |
| 32 | + | Wrap<{ sortedmulti_a: [number, ...Key[]] }> |
| 33 | + | Wrap<{ tr: Key | [Key, Miniscript] }> |
| 34 | + | Wrap<{ sh: Miniscript }> |
| 35 | + | Wrap<{ wsh: Miniscript }> |
| 36 | + | Wrap<{ and_v: [Miniscript, Miniscript] }> |
| 37 | + | Wrap<{ and_b: [Miniscript, Miniscript] }> |
| 38 | + | Wrap<{ andor: [Miniscript, Miniscript, Miniscript] }> |
| 39 | + | Wrap<{ or_b: [Miniscript, Miniscript] }> |
| 40 | + | Wrap<{ or_c: [Miniscript, Miniscript] }> |
| 41 | + | Wrap<{ or_d: [Miniscript, Miniscript] }> |
| 42 | + | Wrap<{ or_i: [Miniscript, Miniscript] }> |
| 43 | + | Wrap<{ thresh: [number, ...Miniscript[]] }> |
| 44 | + | Wrap<{ sha256: string }> |
| 45 | + | Wrap<{ ripemd160: string }> |
| 46 | + | Wrap<{ hash256: string }> |
| 47 | + | Wrap<{ hash160: string }> |
| 48 | + | Wrap<{ older: number }> |
| 49 | + | Wrap<{ after: number }>; |
| 50 | + |
| 51 | +// Top level descriptor expressions |
| 52 | +// https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md#reference |
| 53 | +type Descriptor = |
| 54 | + | { sh: Miniscript | { wsh: Miniscript } } |
| 55 | + | { wsh: Miniscript } |
| 56 | + | { pk: Key } |
| 57 | + | { pkh: Key } |
| 58 | + | { wpkh: Key } |
| 59 | + | { combo: Key } |
| 60 | + | { tr: [Key, Miniscript] } |
| 61 | + | { addr: string } |
| 62 | + | { raw: string } |
| 63 | + | { rawtr: string }; |
| 64 | + |
| 65 | +type Node = Miniscript | Descriptor | number | string; |
| 66 | + |
| 67 | +function formatN(n: Node | Node[]): string { |
| 68 | + if (typeof n === 'string') { |
| 69 | + return n; |
| 70 | + } |
| 71 | + if (typeof n === 'number') { |
| 72 | + return String(n); |
| 73 | + } |
| 74 | + if (Array.isArray(n)) { |
| 75 | + return n.map(formatN).join(','); |
| 76 | + } |
| 77 | + if (n && typeof n === 'object') { |
| 78 | + const entries = Object.entries(n); |
| 79 | + if (entries.length !== 1) { |
| 80 | + throw new Error(`Invalid node: ${n}`); |
| 81 | + } |
| 82 | + const [name, value] = entries[0]; |
| 83 | + return `${name}(${formatN(value)})`; |
| 84 | + } |
| 85 | + throw new Error(`Invalid node: ${n}`); |
| 86 | +} |
| 87 | + |
| 88 | +export type MiniscriptNode = Miniscript; |
| 89 | +export type DescriptorNode = Descriptor; |
| 90 | + |
| 91 | +/** Format a Miniscript or Descriptor node as a descriptor string (without checksum) */ |
| 92 | +export function formatNode(n: MiniscriptNode | DescriptorNode): string { |
| 93 | + return formatN(n); |
| 94 | +} |
0 commit comments