utxo-descriptors 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/index.cjs +1287 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +1269 -0
- package/dist/index.js.map +1 -0
- package/dist/parse.cjs +725 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.cts +10 -0
- package/dist/parse.d.ts +10 -0
- package/dist/parse.js +723 -0
- package/dist/parse.js.map +1 -0
- package/dist/types-Dr-JC_LD.d.cts +107 -0
- package/dist/types-Dr-JC_LD.d.ts +107 -0
- package/dist/weights-9_pPEdMA.d.cts +68 -0
- package/dist/weights-B0KrRWLe.d.ts +68 -0
- package/dist/weights.cjs +490 -0
- package/dist/weights.cjs.map +1 -0
- package/dist/weights.d.cts +2 -0
- package/dist/weights.d.ts +2 -0
- package/dist/weights.js +483 -0
- package/dist/weights.js.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 utxo-coinselect contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# utxo-descriptors
|
|
2
|
+
|
|
3
|
+
Dependency-free BIP-380 output descriptor parser for TypeScript, with weight estimation that agrees with [utxo-coinselect](https://github.com/0xDeny5/utxo-workspace/tree/main/packages/utxo-coinselect).
|
|
4
|
+
|
|
5
|
+
You pass a descriptor string; you get back a typed AST, plus the input and output weight (or a scriptPubKey/spending-script description) needed to price it. No keys, no PSBT, no WASM.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add utxo-descriptors
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Table of contents
|
|
12
|
+
|
|
13
|
+
- [Quick start](#quick-start)
|
|
14
|
+
- [Composing with `utxo-coinselect`](#composing-with-utxo-coinselect)
|
|
15
|
+
- [Supported grammar](#supported-grammar)
|
|
16
|
+
- [Multipath](#multipath)
|
|
17
|
+
- [Errors](#errors)
|
|
18
|
+
- [Subpath exports](#subpath-exports)
|
|
19
|
+
- [See also](#see-also)
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { inputWeight, outputWeight, parseDescriptor } from "utxo-descriptors";
|
|
25
|
+
|
|
26
|
+
const result = parseDescriptor("wsh(multi(2,03a34b...,02cb67...,024cfa...))#8k0h5xxx");
|
|
27
|
+
|
|
28
|
+
if (!result.ok) {
|
|
29
|
+
throw new Error(`${result.reason}: ${result.message}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const weight = inputWeight(result.descriptor.script);
|
|
33
|
+
|
|
34
|
+
if (!weight.ok) {
|
|
35
|
+
throw new Error(`${weight.reason}: ${weight.message}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(weight.weight); // weight units, ready for utxo-coinselect
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`outputWeight()` mirrors `inputWeight()` for the scriptPubKey side, and both accept any parsed `ScriptNode` directly, so you can build one by hand instead of parsing a string.
|
|
42
|
+
|
|
43
|
+
## Composing with `utxo-coinselect`
|
|
44
|
+
|
|
45
|
+
The whole point of pairing these two packages: descriptors in, `selectCoins()` inputs out.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { describeInput, inputWeight, outputWeight, parseDescriptor } from "utxo-descriptors";
|
|
49
|
+
import { selectCoins } from "utxo-coinselect";
|
|
50
|
+
|
|
51
|
+
const receiveDescriptor =
|
|
52
|
+
"wpkh(0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)";
|
|
53
|
+
|
|
54
|
+
function priceUtxo(txid: string, vout: number, value: bigint, descriptor: string) {
|
|
55
|
+
const parsed = parseDescriptor(descriptor);
|
|
56
|
+
|
|
57
|
+
if (!parsed.ok) throw new Error(parsed.message);
|
|
58
|
+
|
|
59
|
+
const weight = inputWeight(parsed.descriptor.script);
|
|
60
|
+
const description = describeInput(parsed.descriptor.script);
|
|
61
|
+
|
|
62
|
+
if (!weight.ok) throw new Error(weight.message);
|
|
63
|
+
if (!description.ok) throw new Error(description.message);
|
|
64
|
+
|
|
65
|
+
return { txid, vout, value, weight, scriptType: description.description };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const parsedTarget = parseDescriptor(receiveDescriptor);
|
|
69
|
+
|
|
70
|
+
if (!parsedTarget.ok) throw new Error(parsedTarget.message);
|
|
71
|
+
|
|
72
|
+
const result = selectCoins({
|
|
73
|
+
utxos: [
|
|
74
|
+
priceUtxo(
|
|
75
|
+
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
|
|
76
|
+
0,
|
|
77
|
+
100_000n,
|
|
78
|
+
receiveDescriptor,
|
|
79
|
+
),
|
|
80
|
+
],
|
|
81
|
+
targets: [{ value: 50_000n, weight: outputWeight(parsedTarget.descriptor.script) }],
|
|
82
|
+
feeRate: 5,
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`describeInput()`/`describeOutput()` return values that are structurally assignable to `utxo-coinselect`'s `InputType`/`OutputType`, verified by a repo-local cross-package test — see [the runnable version of this example](https://github.com/0xDeny5/utxo-workspace/blob/main/examples/node/descriptors.ts).
|
|
87
|
+
|
|
88
|
+
## Supported grammar
|
|
89
|
+
|
|
90
|
+
- Key expressions: raw hex (compressed, uncompressed, x-only), WIF, and extended `xpub`/`xprv` (Bitcoin) or `tpub`/`tprv` (testnet), each with an optional `[fingerprint/path]` origin and an optional derivation path, including a trailing ranged `/*`.
|
|
91
|
+
- Script expressions: `pk()`, `pkh()`, `wpkh()`, `combo()`, `sh()`, `wsh()`, `multi()`/`sortedmulti()`, `raw()`, `addr()`, and `tr()` — key-path only or with a script-path tree of `pk()` leaves.
|
|
92
|
+
- The BIP-380 checksum: `checksumCreate()` and `checksumVerify()`, exported directly for callers that only need checksum handling.
|
|
93
|
+
|
|
94
|
+
Every context and key-count rule from BIP-381 through BIP-386 (for example: compressed-only keys under `wsh()`, x-only-only keys under `tr()`, `sh()`/`combo()`/`raw()`/`addr()`/`tr()` valid only at the top level) is enforced by the parser, not left to weight computation. See [spec/descriptors.md](https://github.com/0xDeny5/utxo-workspace/blob/main/spec/descriptors.md) for the normative rules.
|
|
95
|
+
|
|
96
|
+
## Multipath
|
|
97
|
+
|
|
98
|
+
[BIP-389](https://github.com/bitcoin/bips/blob/master/bip-0389.mediawiki) multipath (`<0;1>`) expands into one descriptor per tuple position:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { expandMultipath } from "utxo-descriptors";
|
|
102
|
+
|
|
103
|
+
const parsed = parseDescriptor("wpkh([d34db33f/84h/0h/0h]xpub.../<0;1>/*)");
|
|
104
|
+
const expanded = expandMultipath(parsed.descriptor!.script);
|
|
105
|
+
// expanded.descriptors[0] -> the receive-chain (.../0/*) descriptor
|
|
106
|
+
// expanded.descriptors[1] -> the change-chain (.../1/*) descriptor
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Tuple elements may be hardened independently and in any order; duplicates within a tuple are rejected, matching the BIP rather than the stricter subset some implementations accept.
|
|
110
|
+
|
|
111
|
+
## Errors
|
|
112
|
+
|
|
113
|
+
Every function returns a discriminated `{ ok: true, ... } | { ok: false, reason, message }` result; nothing here throws on malformed _input_. Parse failures carry a stable `reason` (`invalid-character`, `invalid-checksum`, `unexpected-token`, `unknown-function`, `invalid-key-expression`, `invalid-context`, `key-count-exceeded`, `unsupported`) and, when known, a character `position`. Weight failures carry `opaque-script` (for `addr()`/`raw()` input pricing, where the spending script is genuinely unknown) or `ambiguous-script-type` (for `combo()`, which represents several candidate scripts at once) or `unsupported` (an out-of-scope nesting or miniscript fragment).
|
|
114
|
+
|
|
115
|
+
## Subpath exports
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { parseDescriptor } from "utxo-descriptors/parse";
|
|
119
|
+
import { inputWeight, outputWeight } from "utxo-descriptors/weights";
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Both subpaths are also re-exported from the package root; import from a subpath only if you want your bundler to see that you never touch the other half.
|
|
123
|
+
|
|
124
|
+
## See also
|
|
125
|
+
|
|
126
|
+
- [utxo-coinselect](https://github.com/0xDeny5/utxo-workspace/tree/main/packages/utxo-coinselect) — the coin-selection engine this package's weights feed.
|
|
127
|
+
- [spec/descriptors.md](https://github.com/0xDeny5/utxo-workspace/blob/main/spec/descriptors.md) — the normative grammar and weight-assumption contract.
|
|
128
|
+
- [Root README](https://github.com/0xDeny5/utxo-workspace#readme) — the family overview.
|