@qntx/ur 2.1.0 → 4.0.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/README.md +1 -1
- package/dist/index-DuVMkXeG.d.mts +236 -0
- package/dist/{index-DTjNCWBI.d.mts → index-HtwyWL4y.d.mts} +7 -7
- package/dist/{index-CcMfgay6.d.mts → index-rxH9gNTp.d.mts} +10 -6
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/interop.d.mts +118 -0
- package/dist/interop.mjs +530 -0
- package/dist/registry-BGnkPqkV.mjs +1141 -0
- package/dist/registry.d.mts +4 -201
- package/dist/registry.mjs +4 -899
- package/dist/{typed-D-SyraAj.mjs → typed-C8j8iVYT.mjs} +2 -3
- package/dist/typed.d.mts +3 -3
- package/dist/typed.mjs +2 -2
- package/dist/{ur-7Ge3roVh.mjs → ur-ChGW72hQ.mjs} +10 -11
- package/package.json +13 -4
|
@@ -0,0 +1,1141 @@
|
|
|
1
|
+
import { o as toTagged, r as fromTagged } from "./typed-C8j8iVYT.mjs";
|
|
2
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
+
import { CborDate, CborError, CborMap, MajorType, Tag, bytesToHex, cbor, encodeCbor, expectArray, expectBoolean, expectBytes, expectInteger, expectMap, expectText, expectUnsigned, extractTaggedContent, hasTag, hexToBytes, isArray, isBytes, isMap, isTagged, isUnsigned, taggedValue } from "@blockchaincommons/dcbor";
|
|
4
|
+
//#region src/registry/bytes.ts
|
|
5
|
+
function copyBuf(bytes) {
|
|
6
|
+
return new Uint8Array(bytes);
|
|
7
|
+
}
|
|
8
|
+
function copyBytes(cbor) {
|
|
9
|
+
return copyBuf(expectBytes(cbor));
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/registry/map.ts
|
|
13
|
+
function expectClosedIntMap(cbor, allowed) {
|
|
14
|
+
const map = expectMap(cbor);
|
|
15
|
+
for (const [key] of map) {
|
|
16
|
+
const n = expectUnsigned(key);
|
|
17
|
+
if (typeof n !== "number" || !Number.isInteger(n) || !allowed.has(n)) throw CborError.wrongType();
|
|
18
|
+
}
|
|
19
|
+
return map;
|
|
20
|
+
}
|
|
21
|
+
function expectUint(cbor, max) {
|
|
22
|
+
const n = expectUnsigned(cbor);
|
|
23
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n < 0 || n > max) throw CborError.outOfRange();
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
const expectUint8 = (c) => expectUint(c, 255);
|
|
27
|
+
const expectUint31 = (c) => expectUint(c, 2147483647);
|
|
28
|
+
const expectUint32Ne0 = (c) => {
|
|
29
|
+
const n = expectUint(c, 4294967295);
|
|
30
|
+
if (n === 0) throw CborError.outOfRange();
|
|
31
|
+
return n;
|
|
32
|
+
};
|
|
33
|
+
/** CDDL `int` that must fit in JS number as int32. Used only for coin-info `network`. */
|
|
34
|
+
function expectInt32(cbor) {
|
|
35
|
+
const n = expectInteger(cbor);
|
|
36
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n < -2147483648 || n > 2147483647) throw CborError.outOfRange();
|
|
37
|
+
return n;
|
|
38
|
+
}
|
|
39
|
+
function expectBool(cbor) {
|
|
40
|
+
return expectBoolean(cbor);
|
|
41
|
+
}
|
|
42
|
+
/** Encode-side mirror of `expectText`: validates a caller-built field. */
|
|
43
|
+
function assertText(value) {
|
|
44
|
+
if (typeof value !== "string") throw CborError.wrongType();
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Codecs that exist in this version. The `crypto-*` entries are the deprecated v1 tags
|
|
49
|
+
* (BCR-2020-006); the v2 codecs accept them on read, and the separate `crypto*Codec` writers emit
|
|
50
|
+
* them. `SCRIPT_TAGS` are the BCR-2020-010 script-expression tags embedded inside v1 crypto-output
|
|
51
|
+
* bodies.
|
|
52
|
+
*/
|
|
53
|
+
const TAGS = {
|
|
54
|
+
envelope: Tag.from(200, "envelope"),
|
|
55
|
+
seed: Tag.from(40300, "seed"),
|
|
56
|
+
hdkey: Tag.from(40303, "hdkey"),
|
|
57
|
+
keypath: Tag.from(40304, "keypath"),
|
|
58
|
+
"coin-info": Tag.from(40305, "coin-info"),
|
|
59
|
+
eckey: Tag.from(40306, "eckey"),
|
|
60
|
+
address: Tag.from(40307, "address"),
|
|
61
|
+
"output-descriptor": Tag.from(40308, "output-descriptor"),
|
|
62
|
+
sskr: Tag.from(40309, "sskr"),
|
|
63
|
+
psbt: Tag.from(40310, "psbt"),
|
|
64
|
+
"account-descriptor": Tag.from(40311, "account-descriptor"),
|
|
65
|
+
"crypto-seed": Tag.from(300, "crypto-seed"),
|
|
66
|
+
"crypto-hdkey": Tag.from(303, "crypto-hdkey"),
|
|
67
|
+
"crypto-keypath": Tag.from(304, "crypto-keypath"),
|
|
68
|
+
"crypto-coin-info": Tag.from(305, "crypto-coin-info"),
|
|
69
|
+
"crypto-eckey": Tag.from(306, "crypto-eckey"),
|
|
70
|
+
"crypto-address": Tag.from(307, "crypto-address"),
|
|
71
|
+
"crypto-output": Tag.from(308, "crypto-output"),
|
|
72
|
+
"crypto-sskr": Tag.from(309, "crypto-sskr"),
|
|
73
|
+
"crypto-psbt": Tag.from(310, "crypto-psbt"),
|
|
74
|
+
"crypto-account": Tag.from(311, "crypto-account")
|
|
75
|
+
};
|
|
76
|
+
/** BCR-2020-010 script-expression tags by descriptor function name. */
|
|
77
|
+
const SCRIPT_TAGS = {
|
|
78
|
+
sh: Tag.from(400, "sh"),
|
|
79
|
+
wsh: Tag.from(401, "wsh"),
|
|
80
|
+
pk: Tag.from(402, "pk"),
|
|
81
|
+
pkh: Tag.from(403, "pkh"),
|
|
82
|
+
wpkh: Tag.from(404, "wpkh"),
|
|
83
|
+
combo: Tag.from(405, "combo"),
|
|
84
|
+
multi: Tag.from(406, "multi"),
|
|
85
|
+
sortedmulti: Tag.from(407, "sortedmulti"),
|
|
86
|
+
raw: Tag.from(408, "raw"),
|
|
87
|
+
tr: Tag.from(409, "tr"),
|
|
88
|
+
cosigner: Tag.from(410, "cosigner")
|
|
89
|
+
};
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/registry/seed.ts
|
|
92
|
+
const SEED_KEYS = /* @__PURE__ */ new Set([
|
|
93
|
+
1,
|
|
94
|
+
2,
|
|
95
|
+
3,
|
|
96
|
+
4
|
|
97
|
+
]);
|
|
98
|
+
const MAX_PAYLOAD = 64;
|
|
99
|
+
const TAG_EPOCH_DAYS = 100;
|
|
100
|
+
const SECONDS_PER_DAY = 86400;
|
|
101
|
+
function assertPayloadLen(bytes) {
|
|
102
|
+
if (bytes.length === 0 || bytes.length > MAX_PAYLOAD) throw CborError.outOfRange();
|
|
103
|
+
}
|
|
104
|
+
/** UR-ADR-019: read tag 1 (seconds) or tag 100 (RFC 8943 days); write is tag 1. */
|
|
105
|
+
function expectSeedDate(value) {
|
|
106
|
+
if (value.type === MajorType.Tagged && Number(value.tag) === TAG_EPOCH_DAYS) {
|
|
107
|
+
const days = expectInteger(value.value);
|
|
108
|
+
return CborDate.fromEpochSeconds(Number(days) * SECONDS_PER_DAY);
|
|
109
|
+
}
|
|
110
|
+
return CborDate.fromTaggedCbor(value);
|
|
111
|
+
}
|
|
112
|
+
const seedCodec = {
|
|
113
|
+
tags: [TAGS.seed, TAGS["crypto-seed"]],
|
|
114
|
+
encode(seed) {
|
|
115
|
+
assertPayloadLen(seed.payload);
|
|
116
|
+
const map = new CborMap();
|
|
117
|
+
map.set(1, cbor(copyBuf(seed.payload)));
|
|
118
|
+
if (seed.creationDate !== void 0) map.set(2, CborDate.fromDate(seed.creationDate));
|
|
119
|
+
if (seed.name !== void 0 && seed.name !== "") map.set(3, assertText(seed.name));
|
|
120
|
+
if (seed.note !== void 0 && seed.note !== "") map.set(4, assertText(seed.note));
|
|
121
|
+
return cbor(map);
|
|
122
|
+
},
|
|
123
|
+
decode(value) {
|
|
124
|
+
const map = expectClosedIntMap(value, SEED_KEYS);
|
|
125
|
+
const payload = copyBytes(map.getOrThrow(1));
|
|
126
|
+
assertPayloadLen(payload);
|
|
127
|
+
const date = map.get(2);
|
|
128
|
+
const name = map.get(3);
|
|
129
|
+
const note = map.get(4);
|
|
130
|
+
return Object.freeze({
|
|
131
|
+
payload,
|
|
132
|
+
...date === void 0 ? {} : { creationDate: expectSeedDate(date).toDate() },
|
|
133
|
+
...name === void 0 ? {} : { name: expectText(name) },
|
|
134
|
+
...note === void 0 ? {} : { note: expectText(note) }
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/registry/psbt.ts
|
|
140
|
+
const PSBT_MAGIC = [
|
|
141
|
+
112,
|
|
142
|
+
115,
|
|
143
|
+
98,
|
|
144
|
+
116,
|
|
145
|
+
255
|
|
146
|
+
];
|
|
147
|
+
function copyPsbt(bytes) {
|
|
148
|
+
if (bytes.length < PSBT_MAGIC.length) throw CborError.outOfRange();
|
|
149
|
+
for (let i = 0; i < PSBT_MAGIC.length; i++) if (bytes[i] !== PSBT_MAGIC[i]) throw CborError.wrongType();
|
|
150
|
+
return copyBuf(bytes);
|
|
151
|
+
}
|
|
152
|
+
const psbtCodec = {
|
|
153
|
+
tags: [TAGS.psbt, TAGS["crypto-psbt"]],
|
|
154
|
+
encode(psbt) {
|
|
155
|
+
return cbor(copyPsbt(psbt.bytes));
|
|
156
|
+
},
|
|
157
|
+
decode(value) {
|
|
158
|
+
return Object.freeze({ bytes: copyPsbt(copyBytes(value)) });
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Deprecated v1 writer (`ur:crypto-psbt`, tag 310). The body is identical to v2; only the tag
|
|
163
|
+
* differs.
|
|
164
|
+
*/
|
|
165
|
+
const cryptoPsbtCodec = {
|
|
166
|
+
tags: [TAGS["crypto-psbt"], TAGS.psbt],
|
|
167
|
+
encode: psbtCodec.encode,
|
|
168
|
+
decode: psbtCodec.decode
|
|
169
|
+
};
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/registry/keypath.ts
|
|
172
|
+
const KEYPATH_KEYS = /* @__PURE__ */ new Set([
|
|
173
|
+
1,
|
|
174
|
+
2,
|
|
175
|
+
3
|
|
176
|
+
]);
|
|
177
|
+
function assertUint31$1(n) {
|
|
178
|
+
if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
|
|
179
|
+
return n;
|
|
180
|
+
}
|
|
181
|
+
function assertUint8(n) {
|
|
182
|
+
if (!Number.isInteger(n) || n < 0 || n > 255) throw CborError.outOfRange();
|
|
183
|
+
return n;
|
|
184
|
+
}
|
|
185
|
+
function assertUint32Ne0$1(n) {
|
|
186
|
+
if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
|
|
187
|
+
return n;
|
|
188
|
+
}
|
|
189
|
+
function take(items, i) {
|
|
190
|
+
const item = items[i];
|
|
191
|
+
if (item === void 0) throw CborError.wrongType();
|
|
192
|
+
return item;
|
|
193
|
+
}
|
|
194
|
+
function encodeComponent(c) {
|
|
195
|
+
if (c.kind === "index") return [assertUint31$1(c.index), c.hardened];
|
|
196
|
+
if (c.kind === "wildcard") return [[], c.hardened];
|
|
197
|
+
if (c.kind === "range") {
|
|
198
|
+
if (c.low >= c.high) throw CborError.outOfRange();
|
|
199
|
+
return [[assertUint31$1(c.low), assertUint31$1(c.high)], c.hardened];
|
|
200
|
+
}
|
|
201
|
+
return [[
|
|
202
|
+
assertUint31$1(c.external.index),
|
|
203
|
+
c.external.hardened,
|
|
204
|
+
assertUint31$1(c.internal.index),
|
|
205
|
+
c.internal.hardened
|
|
206
|
+
]];
|
|
207
|
+
}
|
|
208
|
+
function decodeComponents(value) {
|
|
209
|
+
const items = expectArray(value);
|
|
210
|
+
const components = [];
|
|
211
|
+
let i = 0;
|
|
212
|
+
while (i < items.length) {
|
|
213
|
+
const head = take(items, i);
|
|
214
|
+
if (isUnsigned(head)) {
|
|
215
|
+
const hardened = expectBool(take(items, i + 1));
|
|
216
|
+
components.push({
|
|
217
|
+
kind: "index",
|
|
218
|
+
index: expectUint31(head),
|
|
219
|
+
hardened
|
|
220
|
+
});
|
|
221
|
+
i += 2;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (isArray(head)) {
|
|
225
|
+
const inner = head.value;
|
|
226
|
+
if (inner.length === 0) {
|
|
227
|
+
const hardened = expectBool(take(items, i + 1));
|
|
228
|
+
components.push({
|
|
229
|
+
kind: "wildcard",
|
|
230
|
+
hardened
|
|
231
|
+
});
|
|
232
|
+
i += 2;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (inner.length === 2) {
|
|
236
|
+
const low = expectUint31(take(inner, 0));
|
|
237
|
+
const high = expectUint31(take(inner, 1));
|
|
238
|
+
if (low >= high) throw CborError.outOfRange();
|
|
239
|
+
const hardened = expectBool(take(items, i + 1));
|
|
240
|
+
components.push({
|
|
241
|
+
kind: "range",
|
|
242
|
+
low,
|
|
243
|
+
high,
|
|
244
|
+
hardened
|
|
245
|
+
});
|
|
246
|
+
i += 2;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (inner.length === 4) {
|
|
250
|
+
components.push({
|
|
251
|
+
kind: "pair",
|
|
252
|
+
external: {
|
|
253
|
+
index: expectUint31(take(inner, 0)),
|
|
254
|
+
hardened: expectBool(take(inner, 1))
|
|
255
|
+
},
|
|
256
|
+
internal: {
|
|
257
|
+
index: expectUint31(take(inner, 2)),
|
|
258
|
+
hardened: expectBool(take(inner, 3))
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
i += 1;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
throw CborError.wrongType();
|
|
266
|
+
}
|
|
267
|
+
return components;
|
|
268
|
+
}
|
|
269
|
+
const keypathCodec = {
|
|
270
|
+
tags: [TAGS.keypath, TAGS["crypto-keypath"]],
|
|
271
|
+
encode(keypath) {
|
|
272
|
+
if (keypath.components.length === 0 && keypath.sourceFingerprint === void 0 && keypath.depth === void 0) throw CborError.wrongType();
|
|
273
|
+
const items = [];
|
|
274
|
+
for (const c of keypath.components) items.push(...encodeComponent(c));
|
|
275
|
+
const map = new CborMap();
|
|
276
|
+
map.set(1, items);
|
|
277
|
+
if (keypath.sourceFingerprint !== void 0) map.set(2, assertUint32Ne0$1(keypath.sourceFingerprint));
|
|
278
|
+
if (keypath.depth !== void 0) map.set(3, assertUint8(keypath.depth));
|
|
279
|
+
return cbor(map);
|
|
280
|
+
},
|
|
281
|
+
decode(value) {
|
|
282
|
+
const map = expectClosedIntMap(value, KEYPATH_KEYS);
|
|
283
|
+
const components = decodeComponents(map.getOrThrow(1));
|
|
284
|
+
const fingerprint = map.get(2);
|
|
285
|
+
const depth = map.get(3);
|
|
286
|
+
if (components.length === 0 && fingerprint === void 0 && depth === void 0) throw CborError.wrongType();
|
|
287
|
+
return Object.freeze({
|
|
288
|
+
components: Object.freeze(components),
|
|
289
|
+
...fingerprint === void 0 ? {} : { sourceFingerprint: expectUint32Ne0(fingerprint) },
|
|
290
|
+
...depth === void 0 ? {} : { depth: expectUint8(depth) }
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Deprecated v1 writer (`ur:crypto-keypath`, tag 304). The body is identical to v2; only the tag
|
|
296
|
+
* differs.
|
|
297
|
+
*/
|
|
298
|
+
const cryptoKeypathCodec = {
|
|
299
|
+
tags: [TAGS["crypto-keypath"], TAGS.keypath],
|
|
300
|
+
encode: keypathCodec.encode,
|
|
301
|
+
decode: keypathCodec.decode
|
|
302
|
+
};
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/registry/coin-info.ts
|
|
305
|
+
const COIN_INFO_KEYS = /* @__PURE__ */ new Set([1, 2]);
|
|
306
|
+
const CoinType = {
|
|
307
|
+
btc: 0,
|
|
308
|
+
eth: 60
|
|
309
|
+
};
|
|
310
|
+
const Network = {
|
|
311
|
+
mainnet: 0,
|
|
312
|
+
btcTestnet: 1
|
|
313
|
+
};
|
|
314
|
+
function assertUint31(n) {
|
|
315
|
+
if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
|
|
316
|
+
return n;
|
|
317
|
+
}
|
|
318
|
+
function assertInt32(n) {
|
|
319
|
+
if (!Number.isInteger(n) || n < -2147483648 || n > 2147483647) throw CborError.outOfRange();
|
|
320
|
+
return n;
|
|
321
|
+
}
|
|
322
|
+
function encodeBody(info, v1) {
|
|
323
|
+
const map = new CborMap();
|
|
324
|
+
if (info.type !== void 0 && (v1 || info.type !== 0)) map.set(1, assertUint31(info.type));
|
|
325
|
+
if (info.network !== void 0 && (v1 || info.network !== 0)) map.set(2, assertInt32(info.network));
|
|
326
|
+
return cbor(map);
|
|
327
|
+
}
|
|
328
|
+
const coinInfoCodec = {
|
|
329
|
+
tags: [TAGS["coin-info"], TAGS["crypto-coin-info"]],
|
|
330
|
+
encode(info) {
|
|
331
|
+
return encodeBody(info, false);
|
|
332
|
+
},
|
|
333
|
+
decode(value) {
|
|
334
|
+
const map = expectClosedIntMap(value, COIN_INFO_KEYS);
|
|
335
|
+
const type = map.get(1);
|
|
336
|
+
const network = map.get(2);
|
|
337
|
+
return Object.freeze({
|
|
338
|
+
...type === void 0 ? {} : { type: expectUint31(type) },
|
|
339
|
+
...network === void 0 ? {} : { network: expectInt32(network) }
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* Deprecated v1 writer (`ur:crypto-coin-info`, tag 305). Presence-preserving: `type` and `network`
|
|
345
|
+
* are written iff present, `0` included — unlike the v2 writer, which omits defaults.
|
|
346
|
+
*/
|
|
347
|
+
const cryptoCoinInfoCodec = {
|
|
348
|
+
tags: [TAGS["crypto-coin-info"], TAGS["coin-info"]],
|
|
349
|
+
encode(info) {
|
|
350
|
+
return encodeBody(info, true);
|
|
351
|
+
},
|
|
352
|
+
decode: coinInfoCodec.decode
|
|
353
|
+
};
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/registry/eckey.ts
|
|
356
|
+
const ECKEY_KEYS = /* @__PURE__ */ new Set([
|
|
357
|
+
1,
|
|
358
|
+
2,
|
|
359
|
+
3
|
|
360
|
+
]);
|
|
361
|
+
const CURVE_SECP256K1 = 0;
|
|
362
|
+
const PRIVATE_KEY_LEN = 32;
|
|
363
|
+
function assertDataLen$1(curve, isPrivate, data) {
|
|
364
|
+
if (curve !== CURVE_SECP256K1) {
|
|
365
|
+
if (data.length === 0) throw CborError.outOfRange();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (!(isPrivate ? data.length === PRIVATE_KEY_LEN : data.length === 33 || data.length === 65)) throw CborError.outOfRange();
|
|
369
|
+
}
|
|
370
|
+
function assertCurve(n) {
|
|
371
|
+
if (!Number.isSafeInteger(n) || n < 0) throw CborError.outOfRange();
|
|
372
|
+
return n;
|
|
373
|
+
}
|
|
374
|
+
function expectCurve(value) {
|
|
375
|
+
const n = expectUnsigned(value);
|
|
376
|
+
if (typeof n !== "number") throw CborError.outOfRange();
|
|
377
|
+
return assertCurve(n);
|
|
378
|
+
}
|
|
379
|
+
const ecKeyCodec = {
|
|
380
|
+
tags: [TAGS.eckey, TAGS["crypto-eckey"]],
|
|
381
|
+
encode(key) {
|
|
382
|
+
const curve = key.curve ?? CURVE_SECP256K1;
|
|
383
|
+
const isPrivate = key.isPrivate ?? false;
|
|
384
|
+
assertCurve(curve);
|
|
385
|
+
assertDataLen$1(curve, isPrivate, key.data);
|
|
386
|
+
const map = new CborMap();
|
|
387
|
+
if (curve !== CURVE_SECP256K1) map.set(1, curve);
|
|
388
|
+
if (isPrivate) map.set(2, true);
|
|
389
|
+
map.set(3, cbor(copyBuf(key.data)));
|
|
390
|
+
return cbor(map);
|
|
391
|
+
},
|
|
392
|
+
decode(value) {
|
|
393
|
+
const map = expectClosedIntMap(value, ECKEY_KEYS);
|
|
394
|
+
const curveV = map.get(1);
|
|
395
|
+
const isPrivateV = map.get(2);
|
|
396
|
+
const data = copyBytes(map.getOrThrow(3));
|
|
397
|
+
const curve = curveV === void 0 ? CURVE_SECP256K1 : expectCurve(curveV);
|
|
398
|
+
const isPrivate = isPrivateV === void 0 ? false : expectBool(isPrivateV);
|
|
399
|
+
assertDataLen$1(curve, isPrivate, data);
|
|
400
|
+
return Object.freeze({
|
|
401
|
+
...curveV === void 0 ? {} : { curve },
|
|
402
|
+
...isPrivateV === void 0 ? {} : { isPrivate },
|
|
403
|
+
data
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
//#endregion
|
|
408
|
+
//#region src/registry/address.ts
|
|
409
|
+
const ADDRESS_KEYS = /* @__PURE__ */ new Set([
|
|
410
|
+
1,
|
|
411
|
+
2,
|
|
412
|
+
3
|
|
413
|
+
]);
|
|
414
|
+
const TYPED_DATA_LEN = 20;
|
|
415
|
+
const ADDRESS_TYPES = [
|
|
416
|
+
"p2pkh",
|
|
417
|
+
"p2sh",
|
|
418
|
+
"p2wpkh"
|
|
419
|
+
];
|
|
420
|
+
function assertDataLen(type, data) {
|
|
421
|
+
if (type !== void 0) {
|
|
422
|
+
if (data.length !== TYPED_DATA_LEN) throw CborError.outOfRange();
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (data.length === 0) throw CborError.outOfRange();
|
|
426
|
+
}
|
|
427
|
+
function encodeAddressBody(address, coinInfo) {
|
|
428
|
+
assertDataLen(address.type, address.data);
|
|
429
|
+
const map = new CborMap();
|
|
430
|
+
if (address.info !== void 0) map.set(1, toTagged(address.info, coinInfo));
|
|
431
|
+
if (address.type !== void 0) {
|
|
432
|
+
const index = ADDRESS_TYPES.indexOf(address.type);
|
|
433
|
+
if (index === -1) throw CborError.wrongType();
|
|
434
|
+
map.set(2, index);
|
|
435
|
+
}
|
|
436
|
+
map.set(3, cbor(copyBuf(address.data)));
|
|
437
|
+
return cbor(map);
|
|
438
|
+
}
|
|
439
|
+
const addressCodec = {
|
|
440
|
+
tags: [TAGS.address, TAGS["crypto-address"]],
|
|
441
|
+
encode(address) {
|
|
442
|
+
return encodeAddressBody(address, coinInfoCodec);
|
|
443
|
+
},
|
|
444
|
+
decode(value) {
|
|
445
|
+
const map = expectClosedIntMap(value, ADDRESS_KEYS);
|
|
446
|
+
const info = map.get(1);
|
|
447
|
+
const typeV = map.get(2);
|
|
448
|
+
const data = copyBytes(map.getOrThrow(3));
|
|
449
|
+
const type = typeV === void 0 ? void 0 : ADDRESS_TYPES[expectUint(typeV, 2)];
|
|
450
|
+
assertDataLen(type, data);
|
|
451
|
+
return Object.freeze({
|
|
452
|
+
...info === void 0 ? {} : { info: fromTagged(info, coinInfoCodec) },
|
|
453
|
+
...type === void 0 ? {} : { type },
|
|
454
|
+
data
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/registry/hdkey.ts
|
|
460
|
+
const MASTER_KEYS = /* @__PURE__ */ new Set([
|
|
461
|
+
1,
|
|
462
|
+
3,
|
|
463
|
+
4
|
|
464
|
+
]);
|
|
465
|
+
const DERIVED_KEYS = /* @__PURE__ */ new Set([
|
|
466
|
+
2,
|
|
467
|
+
3,
|
|
468
|
+
4,
|
|
469
|
+
5,
|
|
470
|
+
6,
|
|
471
|
+
7,
|
|
472
|
+
8,
|
|
473
|
+
9,
|
|
474
|
+
10
|
|
475
|
+
]);
|
|
476
|
+
const KEY_DATA_LEN = 33;
|
|
477
|
+
const CHAIN_CODE_LEN = 32;
|
|
478
|
+
function copyLen(bytes, len) {
|
|
479
|
+
if (bytes.length !== len) throw CborError.outOfRange();
|
|
480
|
+
return copyBuf(bytes);
|
|
481
|
+
}
|
|
482
|
+
function bytesOfLen(value, len) {
|
|
483
|
+
const bytes = copyBytes(value);
|
|
484
|
+
if (bytes.length !== len) throw CborError.outOfRange();
|
|
485
|
+
return bytes;
|
|
486
|
+
}
|
|
487
|
+
function assertKeyData(isPrivate, data) {
|
|
488
|
+
if (data.length !== KEY_DATA_LEN || isPrivate && data[0] !== 0) throw CborError.outOfRange();
|
|
489
|
+
}
|
|
490
|
+
function assertUint32Ne0(n) {
|
|
491
|
+
if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
|
|
492
|
+
return n;
|
|
493
|
+
}
|
|
494
|
+
const V2_NESTED = {
|
|
495
|
+
coinInfo: coinInfoCodec,
|
|
496
|
+
keypath: keypathCodec
|
|
497
|
+
};
|
|
498
|
+
const V1_NESTED = {
|
|
499
|
+
coinInfo: cryptoCoinInfoCodec,
|
|
500
|
+
keypath: cryptoKeypathCodec
|
|
501
|
+
};
|
|
502
|
+
function writeDerivedMeta(map, meta, v1) {
|
|
503
|
+
const nested = v1 ? V1_NESTED : V2_NESTED;
|
|
504
|
+
if (v1 ? meta.isPrivate !== void 0 : meta.isPrivate === true) map.set(2, meta.isPrivate === true);
|
|
505
|
+
if (meta.chainCode !== void 0) map.set(4, cbor(copyLen(meta.chainCode, CHAIN_CODE_LEN)));
|
|
506
|
+
if (meta.useInfo !== void 0) map.set(5, toTagged(meta.useInfo, nested.coinInfo));
|
|
507
|
+
if (meta.origin !== void 0) map.set(6, toTagged(meta.origin, nested.keypath));
|
|
508
|
+
if (meta.children !== void 0) map.set(7, toTagged(meta.children, nested.keypath));
|
|
509
|
+
if (meta.parentFingerprint !== void 0) map.set(8, assertUint32Ne0(meta.parentFingerprint));
|
|
510
|
+
if (meta.name !== void 0 && meta.name !== "") map.set(9, assertText(meta.name));
|
|
511
|
+
if (meta.note !== void 0 && meta.note !== "") map.set(10, assertText(meta.note));
|
|
512
|
+
}
|
|
513
|
+
function readDerivedMeta(map, emptyChainCodeAbsent) {
|
|
514
|
+
const isPrivate = map.get(2);
|
|
515
|
+
const chainCode = map.get(4);
|
|
516
|
+
const useInfo = map.get(5);
|
|
517
|
+
const origin = map.get(6);
|
|
518
|
+
const children = map.get(7);
|
|
519
|
+
const parentFingerprint = map.get(8);
|
|
520
|
+
const name = map.get(9);
|
|
521
|
+
const note = map.get(10);
|
|
522
|
+
const chainCodeBytes = chainCode === void 0 ? void 0 : copyBytes(chainCode);
|
|
523
|
+
return {
|
|
524
|
+
...isPrivate === void 0 ? {} : { isPrivate: expectBool(isPrivate) },
|
|
525
|
+
...chainCodeBytes === void 0 ? {} : chainCodeBytes.length === 0 && emptyChainCodeAbsent ? {} : { chainCode: copyLen(chainCodeBytes, CHAIN_CODE_LEN) },
|
|
526
|
+
...useInfo === void 0 ? {} : { useInfo: fromTagged(useInfo, coinInfoCodec) },
|
|
527
|
+
...origin === void 0 ? {} : { origin: fromTagged(origin, keypathCodec) },
|
|
528
|
+
...children === void 0 ? {} : { children: fromTagged(children, keypathCodec) },
|
|
529
|
+
...parentFingerprint === void 0 ? {} : { parentFingerprint: expectUint32Ne0(parentFingerprint) },
|
|
530
|
+
...name === void 0 ? {} : { name: expectText(name) },
|
|
531
|
+
...note === void 0 ? {} : { note: expectText(note) }
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
function encodeHdKeyBody(key, v1) {
|
|
535
|
+
const map = new CborMap();
|
|
536
|
+
if (key.kind === "master") {
|
|
537
|
+
map.set(1, true);
|
|
538
|
+
assertKeyData(true, key.keyData);
|
|
539
|
+
map.set(3, cbor(copyBuf(key.keyData)));
|
|
540
|
+
map.set(4, cbor(copyLen(key.chainCode, CHAIN_CODE_LEN)));
|
|
541
|
+
return cbor(map);
|
|
542
|
+
}
|
|
543
|
+
assertKeyData(key.isPrivate === true, key.keyData);
|
|
544
|
+
map.set(3, cbor(copyBuf(key.keyData)));
|
|
545
|
+
writeDerivedMeta(map, key, v1);
|
|
546
|
+
return cbor(map);
|
|
547
|
+
}
|
|
548
|
+
function decodeDerivedHdKeyBody(value) {
|
|
549
|
+
const map = expectClosedIntMap(value, DERIVED_KEYS);
|
|
550
|
+
const meta = readDerivedMeta(map, false);
|
|
551
|
+
const keyData = bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN);
|
|
552
|
+
assertKeyData(meta.isPrivate === true, keyData);
|
|
553
|
+
return Object.freeze({
|
|
554
|
+
kind: "derived",
|
|
555
|
+
...meta,
|
|
556
|
+
keyData
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
const hdKeyCodec = {
|
|
560
|
+
tags: [TAGS.hdkey, TAGS["crypto-hdkey"]],
|
|
561
|
+
encode(key) {
|
|
562
|
+
return encodeHdKeyBody(key, false);
|
|
563
|
+
},
|
|
564
|
+
decode(value) {
|
|
565
|
+
const masterFlag = expectMap(value).get(1);
|
|
566
|
+
if (masterFlag !== void 0) {
|
|
567
|
+
if (!expectBool(masterFlag)) throw CborError.wrongType();
|
|
568
|
+
const map = expectClosedIntMap(value, MASTER_KEYS);
|
|
569
|
+
const keyData = bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN);
|
|
570
|
+
assertKeyData(true, keyData);
|
|
571
|
+
return Object.freeze({
|
|
572
|
+
kind: "master",
|
|
573
|
+
keyData,
|
|
574
|
+
chainCode: bytesOfLen(map.getOrThrow(4), CHAIN_CODE_LEN)
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return decodeDerivedHdKeyBody(value);
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* Deprecated v1 writer (`ur:crypto-hdkey`, tag 303). Presence-preserving: `is-private` is written
|
|
582
|
+
* iff `isPrivate` is present (`false` included), and the nested use-info / origin / children values
|
|
583
|
+
* carry the v1 tags (305/304) with v1 rules. Every other field matches v2.
|
|
584
|
+
*/
|
|
585
|
+
const cryptoHdKeyCodec = {
|
|
586
|
+
tags: [TAGS["crypto-hdkey"], TAGS.hdkey],
|
|
587
|
+
encode(key) {
|
|
588
|
+
return encodeHdKeyBody(key, true);
|
|
589
|
+
},
|
|
590
|
+
decode: hdKeyCodec.decode
|
|
591
|
+
};
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/registry/script-expression.ts
|
|
594
|
+
const MULTIKEY_KEYS = /* @__PURE__ */ new Set([1, 2]);
|
|
595
|
+
function tagNumber(cbor) {
|
|
596
|
+
if (cbor.type !== MajorType.Tagged) throw CborError.wrongType();
|
|
597
|
+
return Number(cbor.tag);
|
|
598
|
+
}
|
|
599
|
+
function bytesOf(cbor) {
|
|
600
|
+
if (cbor.type !== MajorType.ByteString || !(cbor.value instanceof Uint8Array)) throw CborError.wrongType();
|
|
601
|
+
return cbor.value;
|
|
602
|
+
}
|
|
603
|
+
function closedMultikeyMap(cbor) {
|
|
604
|
+
if (cbor.type !== MajorType.Map) throw CborError.wrongType();
|
|
605
|
+
const map = cbor.value;
|
|
606
|
+
for (const [k] of map) if (k.type !== MajorType.Unsigned || !MULTIKEY_KEYS.has(Number(k.value))) throw CborError.wrongType();
|
|
607
|
+
return map;
|
|
608
|
+
}
|
|
609
|
+
/** Key_exp = #6.306 / #6.303 / #6.307 (v2 tag equivalents accepted). */
|
|
610
|
+
function keyExp(cbor) {
|
|
611
|
+
const tag = tagNumber(cbor);
|
|
612
|
+
const content = extractTaggedContent(cbor);
|
|
613
|
+
if (tag === 40306 || tag === 306) return {
|
|
614
|
+
kind: "eckey",
|
|
615
|
+
key: ecKeyCodec.decode(content)
|
|
616
|
+
};
|
|
617
|
+
if (tag === 40303 || tag === 303) return {
|
|
618
|
+
kind: "hdkey",
|
|
619
|
+
key: hdKeyCodec.decode(content)
|
|
620
|
+
};
|
|
621
|
+
if (tag === 40307 || tag === 307) return {
|
|
622
|
+
kind: "address",
|
|
623
|
+
address: addressCodec.decode(content)
|
|
624
|
+
};
|
|
625
|
+
throw CborError.wrongType();
|
|
626
|
+
}
|
|
627
|
+
function isKeyExpTag(tag) {
|
|
628
|
+
return tag === 40306 || tag === 306 || tag === 40303 || tag === 303 || tag === 40307 || tag === 307;
|
|
629
|
+
}
|
|
630
|
+
const KEY_FN = {
|
|
631
|
+
[402]: "pk",
|
|
632
|
+
[403]: "pkh",
|
|
633
|
+
[404]: "wpkh",
|
|
634
|
+
[405]: "combo",
|
|
635
|
+
[410]: "cosigner"
|
|
636
|
+
};
|
|
637
|
+
const WRAPPER_FN = {
|
|
638
|
+
[400]: "sh",
|
|
639
|
+
[401]: "wsh"
|
|
640
|
+
};
|
|
641
|
+
/**
|
|
642
|
+
* BIP-380 text rendering of a v1 script-expression tree (BCR-2020-010 tags 400–410). Keys become
|
|
643
|
+
* `@0`, `@1`, … in order of appearance and are collected as v2 `DescriptorKey` values. The grammar
|
|
644
|
+
* gives every script function at most one child subtree (multikey's list is handled inline), so
|
|
645
|
+
* each node's local `@n` numbering is already the global order. `parent` is the enclosing wrapper
|
|
646
|
+
* tag, which bounds nesting to `sh(wsh(...))`. Unsupported combinations (e.g. a taproot script
|
|
647
|
+
* tree) throw `CborError` — surfaced as `CborType` by the codec layer.
|
|
648
|
+
*/
|
|
649
|
+
function scriptExp(cbor, parent) {
|
|
650
|
+
const tag = tagNumber(cbor);
|
|
651
|
+
const content = extractTaggedContent(cbor);
|
|
652
|
+
const keyFn = KEY_FN[tag];
|
|
653
|
+
if (keyFn !== void 0) return {
|
|
654
|
+
text: `${keyFn}(@0)`,
|
|
655
|
+
keys: [keyExp(content)]
|
|
656
|
+
};
|
|
657
|
+
const wrapper = WRAPPER_FN[tag];
|
|
658
|
+
if (wrapper !== void 0) {
|
|
659
|
+
if (!(parent === void 0 || parent === 400 && tag === 401)) throw CborError.wrongType();
|
|
660
|
+
if (content.type === MajorType.Tagged && isKeyExpTag(Number(content.tag))) {
|
|
661
|
+
const key = keyExp(content);
|
|
662
|
+
return {
|
|
663
|
+
text: `${wrapper}(cosigner(@0))`,
|
|
664
|
+
keys: [key]
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
const inner = scriptExp(content, tag);
|
|
668
|
+
return {
|
|
669
|
+
text: `${wrapper}(${inner.text})`,
|
|
670
|
+
keys: inner.keys
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
if (tag === 406 || tag === 407) {
|
|
674
|
+
const map = closedMultikeyMap(content);
|
|
675
|
+
const thresholdV = map.get(1);
|
|
676
|
+
const keysV = map.get(2);
|
|
677
|
+
if (thresholdV === void 0 || keysV === void 0) throw CborError.wrongType();
|
|
678
|
+
const threshold = expectUint31(thresholdV);
|
|
679
|
+
const keys = expectArray(keysV);
|
|
680
|
+
if (threshold < 1 || threshold > keys.length || keys.length === 0) throw CborError.outOfRange();
|
|
681
|
+
const parts = [];
|
|
682
|
+
const collected = [];
|
|
683
|
+
for (const k of keys) {
|
|
684
|
+
collected.push(keyExp(k));
|
|
685
|
+
parts.push(`@${collected.length - 1}`);
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
text: `${tag === 406 ? "multi" : "sortedmulti"}(${threshold},${parts.join(",")})`,
|
|
689
|
+
keys: collected
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
if (tag === 408) return {
|
|
693
|
+
text: `raw(${bytesToHex(bytesOf(content))})`,
|
|
694
|
+
keys: []
|
|
695
|
+
};
|
|
696
|
+
if (tag === 409) return {
|
|
697
|
+
text: "tr(@0)",
|
|
698
|
+
keys: [keyExp(content)]
|
|
699
|
+
};
|
|
700
|
+
throw CborError.wrongType();
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Convert a v1 `crypto-output` body (a script-expression tagged value) into the v2
|
|
704
|
+
* `OutputDescriptor` shape `{ source, keys }`.
|
|
705
|
+
*/
|
|
706
|
+
function scriptExpressionToDescriptor(cbor) {
|
|
707
|
+
const out = scriptExp(cbor);
|
|
708
|
+
return Object.freeze({
|
|
709
|
+
source: out.text,
|
|
710
|
+
keys: Object.freeze(out.keys)
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
const ecKeyV1Codec = {
|
|
714
|
+
tags: [TAGS["crypto-eckey"], TAGS.eckey],
|
|
715
|
+
encode: ecKeyCodec.encode,
|
|
716
|
+
decode: ecKeyCodec.decode
|
|
717
|
+
};
|
|
718
|
+
const addressV1Codec = {
|
|
719
|
+
tags: [TAGS["crypto-address"], TAGS.address],
|
|
720
|
+
encode(address) {
|
|
721
|
+
return encodeAddressBody(address, cryptoCoinInfoCodec);
|
|
722
|
+
},
|
|
723
|
+
decode: addressCodec.decode
|
|
724
|
+
};
|
|
725
|
+
function keyExpV1(key) {
|
|
726
|
+
if (key.kind === "hdkey") return toTagged(key.key, cryptoHdKeyCodec);
|
|
727
|
+
if (key.kind === "eckey") return toTagged(key.key, ecKeyV1Codec);
|
|
728
|
+
return toTagged(key.address, addressV1Codec);
|
|
729
|
+
}
|
|
730
|
+
function takeName(source, cur) {
|
|
731
|
+
const start = cur.pos;
|
|
732
|
+
while (cur.pos < source.length) {
|
|
733
|
+
const code = source.codePointAt(cur.pos) ?? -1;
|
|
734
|
+
if (code < 97 || code > 122) break;
|
|
735
|
+
cur.pos += 1;
|
|
736
|
+
}
|
|
737
|
+
if (cur.pos === start) throw CborError.wrongType();
|
|
738
|
+
return source.slice(start, cur.pos);
|
|
739
|
+
}
|
|
740
|
+
function takeChar(source, cur, ch) {
|
|
741
|
+
if (source[cur.pos] !== ch) throw CborError.wrongType();
|
|
742
|
+
cur.pos += 1;
|
|
743
|
+
}
|
|
744
|
+
function takeUint(source, cur) {
|
|
745
|
+
const start = cur.pos;
|
|
746
|
+
while (cur.pos < source.length) {
|
|
747
|
+
const code = source.codePointAt(cur.pos) ?? -1;
|
|
748
|
+
if (code < 48 || code > 57) break;
|
|
749
|
+
cur.pos += 1;
|
|
750
|
+
}
|
|
751
|
+
if (cur.pos === start) throw CborError.wrongType();
|
|
752
|
+
const n = Number(source.slice(start, cur.pos));
|
|
753
|
+
if (!Number.isSafeInteger(n)) throw CborError.outOfRange();
|
|
754
|
+
return n;
|
|
755
|
+
}
|
|
756
|
+
function takeKey(source, cur, keys) {
|
|
757
|
+
takeChar(source, cur, "@");
|
|
758
|
+
const key = keys[takeUint(source, cur)];
|
|
759
|
+
if (key === void 0) throw CborError.outOfRange();
|
|
760
|
+
return keyExpV1(key);
|
|
761
|
+
}
|
|
762
|
+
const KEY_FN_TAGS = {
|
|
763
|
+
pk: SCRIPT_TAGS.pk,
|
|
764
|
+
pkh: SCRIPT_TAGS.pkh,
|
|
765
|
+
wpkh: SCRIPT_TAGS.wpkh,
|
|
766
|
+
combo: SCRIPT_TAGS.combo,
|
|
767
|
+
tr: SCRIPT_TAGS.tr
|
|
768
|
+
};
|
|
769
|
+
function parseExp(source, cur, parent, keys) {
|
|
770
|
+
const name = takeName(source, cur);
|
|
771
|
+
takeChar(source, cur, "(");
|
|
772
|
+
if (name === "cosigner") {
|
|
773
|
+
const key = takeKey(source, cur, keys);
|
|
774
|
+
takeChar(source, cur, ")");
|
|
775
|
+
return parent === "top" ? taggedValue(SCRIPT_TAGS.cosigner, key) : key;
|
|
776
|
+
}
|
|
777
|
+
const keyTag = KEY_FN_TAGS[name];
|
|
778
|
+
if (keyTag !== void 0) {
|
|
779
|
+
const key = takeKey(source, cur, keys);
|
|
780
|
+
takeChar(source, cur, ")");
|
|
781
|
+
return taggedValue(keyTag, key);
|
|
782
|
+
}
|
|
783
|
+
if (name === "sh" || name === "wsh") {
|
|
784
|
+
if (name === "sh" ? parent !== "top" : parent === "wsh") throw CborError.wrongType();
|
|
785
|
+
const inner = parseExp(source, cur, name, keys);
|
|
786
|
+
takeChar(source, cur, ")");
|
|
787
|
+
return taggedValue(name === "sh" ? SCRIPT_TAGS.sh : SCRIPT_TAGS.wsh, inner);
|
|
788
|
+
}
|
|
789
|
+
if (name === "multi" || name === "sortedmulti") {
|
|
790
|
+
const threshold = takeUint(source, cur);
|
|
791
|
+
const list = [];
|
|
792
|
+
do {
|
|
793
|
+
takeChar(source, cur, ",");
|
|
794
|
+
list.push(takeKey(source, cur, keys));
|
|
795
|
+
} while (source[cur.pos] === ",");
|
|
796
|
+
takeChar(source, cur, ")");
|
|
797
|
+
if (threshold < 1 || threshold > list.length) throw CborError.outOfRange();
|
|
798
|
+
const map = new CborMap();
|
|
799
|
+
map.set(1, threshold);
|
|
800
|
+
map.set(2, list);
|
|
801
|
+
return taggedValue(name === "multi" ? SCRIPT_TAGS.multi : SCRIPT_TAGS.sortedmulti, cbor(map));
|
|
802
|
+
}
|
|
803
|
+
if (name === "raw") {
|
|
804
|
+
const start = cur.pos;
|
|
805
|
+
while (cur.pos < source.length) {
|
|
806
|
+
const code = source.codePointAt(cur.pos) ?? -1;
|
|
807
|
+
if (!(code >= 48 && code <= 57 || code >= 97 && code <= 102)) break;
|
|
808
|
+
cur.pos += 1;
|
|
809
|
+
}
|
|
810
|
+
const hex = source.slice(start, cur.pos);
|
|
811
|
+
takeChar(source, cur, ")");
|
|
812
|
+
return taggedValue(SCRIPT_TAGS.raw, cbor(hexToBytes(hex)));
|
|
813
|
+
}
|
|
814
|
+
throw CborError.wrongType();
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Inverse of {@link scriptExpressionToDescriptor}: render `source` (restricted to the reader's
|
|
818
|
+
* output grammar) back into a BCR-2020-010 script-expression tree, embedding `keys` with the v1
|
|
819
|
+
* tags. Anything outside the grammar throws `CborError` — surfaced as `CborType` by the codec
|
|
820
|
+
* layer.
|
|
821
|
+
*/
|
|
822
|
+
function descriptorToScriptExpression(source, keys) {
|
|
823
|
+
const cur = { pos: 0 };
|
|
824
|
+
const tree = parseExp(source, cur, "top", keys);
|
|
825
|
+
if (cur.pos !== source.length) throw CborError.wrongType();
|
|
826
|
+
return tree;
|
|
827
|
+
}
|
|
828
|
+
//#endregion
|
|
829
|
+
//#region src/registry/output-descriptor.ts
|
|
830
|
+
const DESCRIPTOR_KEYS = /* @__PURE__ */ new Set([
|
|
831
|
+
1,
|
|
832
|
+
2,
|
|
833
|
+
3,
|
|
834
|
+
4
|
|
835
|
+
]);
|
|
836
|
+
/** The `@n` placeholders in `source` must be exactly the set `0..keys.length-1`. */
|
|
837
|
+
function assertPlaceholders(source, keyCount) {
|
|
838
|
+
const found = /* @__PURE__ */ new Set();
|
|
839
|
+
for (const m of source.matchAll(/@(\d+)/g)) {
|
|
840
|
+
const n = Number(m[1]);
|
|
841
|
+
if (!Number.isSafeInteger(n) || n < 0 || n >= keyCount) throw CborError.outOfRange();
|
|
842
|
+
found.add(n);
|
|
843
|
+
}
|
|
844
|
+
if (found.size !== keyCount) throw CborError.outOfRange();
|
|
845
|
+
}
|
|
846
|
+
function encodeKey(key) {
|
|
847
|
+
if (key.kind === "hdkey") return toTagged(key.key, hdKeyCodec);
|
|
848
|
+
if (key.kind === "eckey") return toTagged(key.key, ecKeyCodec);
|
|
849
|
+
return toTagged(key.address, addressCodec);
|
|
850
|
+
}
|
|
851
|
+
function decodeKey(cbor) {
|
|
852
|
+
const tag = cbor.type === MajorType.Tagged ? Number(cbor.tag) : void 0;
|
|
853
|
+
if (tag === TAGS.hdkey.value || tag === TAGS["crypto-hdkey"].value) return {
|
|
854
|
+
kind: "hdkey",
|
|
855
|
+
key: fromTagged(cbor, hdKeyCodec)
|
|
856
|
+
};
|
|
857
|
+
if (tag === TAGS.eckey.value || tag === TAGS["crypto-eckey"].value) return {
|
|
858
|
+
kind: "eckey",
|
|
859
|
+
key: fromTagged(cbor, ecKeyCodec)
|
|
860
|
+
};
|
|
861
|
+
if (tag === TAGS.address.value || tag === TAGS["crypto-address"].value) return {
|
|
862
|
+
kind: "address",
|
|
863
|
+
address: fromTagged(cbor, addressCodec)
|
|
864
|
+
};
|
|
865
|
+
throw CborError.wrongType();
|
|
866
|
+
}
|
|
867
|
+
function encodeDescriptor(desc) {
|
|
868
|
+
assertPlaceholders(desc.source, desc.keys.length);
|
|
869
|
+
const map = new CborMap();
|
|
870
|
+
map.set(1, assertText(desc.source));
|
|
871
|
+
if (desc.keys.length > 0) map.set(2, desc.keys.map(encodeKey));
|
|
872
|
+
if (desc.name !== void 0 && desc.name !== "") map.set(3, assertText(desc.name));
|
|
873
|
+
if (desc.note !== void 0 && desc.note !== "") map.set(4, assertText(desc.note));
|
|
874
|
+
return cbor(map);
|
|
875
|
+
}
|
|
876
|
+
function decodeDescriptorMap(value) {
|
|
877
|
+
const map = expectClosedIntMap(value, DESCRIPTOR_KEYS);
|
|
878
|
+
const source = expectText(map.getOrThrow(1));
|
|
879
|
+
const keysV = map.get(2);
|
|
880
|
+
const nameV = map.get(3);
|
|
881
|
+
const noteV = map.get(4);
|
|
882
|
+
const keys = keysV === void 0 ? [] : expectArray(keysV).map(decodeKey);
|
|
883
|
+
assertPlaceholders(source, keys.length);
|
|
884
|
+
return Object.freeze({
|
|
885
|
+
source,
|
|
886
|
+
keys: Object.freeze(keys),
|
|
887
|
+
...nameV === void 0 ? {} : { name: expectText(nameV) },
|
|
888
|
+
...noteV === void 0 ? {} : { note: expectText(noteV) }
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
const outputDescriptorCodec = {
|
|
892
|
+
tags: [TAGS["output-descriptor"], TAGS["crypto-output"]],
|
|
893
|
+
encode(desc) {
|
|
894
|
+
return encodeDescriptor(desc);
|
|
895
|
+
},
|
|
896
|
+
decode(value) {
|
|
897
|
+
if (isMap(value)) return decodeDescriptorMap(value);
|
|
898
|
+
const { source, keys } = scriptExpressionToDescriptor(value);
|
|
899
|
+
return Object.freeze({
|
|
900
|
+
source,
|
|
901
|
+
keys
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
/**
|
|
906
|
+
* Write `desc` as a BCR-2020-010 script-expression tree — the v1 `crypto-output` body. The tree has
|
|
907
|
+
* no `name`/`note` slots, so a non-empty one is a `CborError` (empty strings normalize to absent,
|
|
908
|
+
* as on write). `source` must stay inside the reader's output grammar and its `@n` placeholders
|
|
909
|
+
* must be exactly `0..keys.length-1`.
|
|
910
|
+
*/
|
|
911
|
+
function encodeOutputV1(desc) {
|
|
912
|
+
if (desc.name !== void 0 && desc.name !== "" || desc.note !== void 0 && desc.note !== "") throw CborError.wrongType();
|
|
913
|
+
assertPlaceholders(desc.source, desc.keys.length);
|
|
914
|
+
return descriptorToScriptExpression(desc.source, desc.keys);
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Deprecated v1 writer (`ur:crypto-output`, tag 308): a script-expression tree whose embedded keys
|
|
918
|
+
* use the v1 tags (303/306/307). Decode accepts both the tree and a tagged v2 map, but only
|
|
919
|
+
* descriptors the v1 writer can re-encode — no `name`/`note`, `source` inside the script-expression
|
|
920
|
+
* grammar.
|
|
921
|
+
*/
|
|
922
|
+
const cryptoOutputCodec = {
|
|
923
|
+
tags: [TAGS["crypto-output"], TAGS["output-descriptor"]],
|
|
924
|
+
encode(desc) {
|
|
925
|
+
return encodeOutputV1(desc);
|
|
926
|
+
},
|
|
927
|
+
decode(value) {
|
|
928
|
+
const desc = isMap(value) ? decodeDescriptorMap(value) : scriptExpressionToDescriptor(value);
|
|
929
|
+
encodeOutputV1(desc);
|
|
930
|
+
return desc;
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
//#endregion
|
|
934
|
+
//#region src/registry/account-descriptor.ts
|
|
935
|
+
const ACCOUNT_KEYS = /* @__PURE__ */ new Set([1, 2]);
|
|
936
|
+
function assertFingerprint(n) {
|
|
937
|
+
if (!Number.isInteger(n) || n < 0 || n > 4294967295) throw CborError.outOfRange();
|
|
938
|
+
return n;
|
|
939
|
+
}
|
|
940
|
+
function assertDescriptors(descriptors) {
|
|
941
|
+
if (descriptors.length === 0) throw CborError.outOfRange();
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* One `output-descriptors` entry: a tagged `40308` map (v2), a tagged `308` crypto-output script
|
|
945
|
+
* expression (v1, per BCR-2020-015), or — leniently, as KeystoneHQ writes it — a bare `400…410`
|
|
946
|
+
* script expression.
|
|
947
|
+
*/
|
|
948
|
+
function decodeEntry(item) {
|
|
949
|
+
const tag = item.type === MajorType.Tagged ? Number(item.tag) : void 0;
|
|
950
|
+
if (tag !== void 0 && tag >= 400 && tag <= 410) {
|
|
951
|
+
const { source, keys } = scriptExpressionToDescriptor(item);
|
|
952
|
+
return Object.freeze({
|
|
953
|
+
source,
|
|
954
|
+
keys
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
return fromTagged(item, outputDescriptorCodec);
|
|
958
|
+
}
|
|
959
|
+
function decodeBody(value) {
|
|
960
|
+
const map = expectClosedIntMap(value, ACCOUNT_KEYS);
|
|
961
|
+
const descriptors = expectArray(map.getOrThrow(2)).map(decodeEntry);
|
|
962
|
+
assertDescriptors(descriptors);
|
|
963
|
+
return Object.freeze({
|
|
964
|
+
masterFingerprint: expectUint(map.getOrThrow(1), 4294967295),
|
|
965
|
+
outputDescriptors: Object.freeze(descriptors)
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
const accountDescriptorCodec = {
|
|
969
|
+
tags: [TAGS["account-descriptor"], TAGS["crypto-account"]],
|
|
970
|
+
encode(account) {
|
|
971
|
+
assertFingerprint(account.masterFingerprint);
|
|
972
|
+
assertDescriptors(account.outputDescriptors);
|
|
973
|
+
const map = new CborMap();
|
|
974
|
+
map.set(1, account.masterFingerprint);
|
|
975
|
+
map.set(2, account.outputDescriptors.map((d) => toTagged(d, outputDescriptorCodec)));
|
|
976
|
+
return cbor(map);
|
|
977
|
+
},
|
|
978
|
+
decode(value) {
|
|
979
|
+
return decodeBody(value);
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
/**
|
|
983
|
+
* Deprecated v1 writer (`ur:crypto-account`, tag 311): `{1: mfp, 2: entries}`. The entries are bare
|
|
984
|
+
* script-expression trees — Keystone's spelling, which deployed readers expect — not BCR-2020-015's
|
|
985
|
+
* tagged `308` outputs. Decode accepts v1/v2 entry forms but rejects anything the v1 writer cannot
|
|
986
|
+
* re-encode.
|
|
987
|
+
*/
|
|
988
|
+
const cryptoAccountCodec = {
|
|
989
|
+
tags: [TAGS["crypto-account"], TAGS["account-descriptor"]],
|
|
990
|
+
encode(account) {
|
|
991
|
+
assertFingerprint(account.masterFingerprint);
|
|
992
|
+
assertDescriptors(account.outputDescriptors);
|
|
993
|
+
const map = new CborMap();
|
|
994
|
+
map.set(1, account.masterFingerprint);
|
|
995
|
+
map.set(2, account.outputDescriptors.map(encodeOutputV1));
|
|
996
|
+
return cbor(map);
|
|
997
|
+
},
|
|
998
|
+
decode(value) {
|
|
999
|
+
const account = decodeBody(value);
|
|
1000
|
+
for (const desc of account.outputDescriptors) encodeOutputV1(desc);
|
|
1001
|
+
return account;
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
//#endregion
|
|
1005
|
+
//#region src/registry/sskr.ts
|
|
1006
|
+
const HEADER_LEN = 5;
|
|
1007
|
+
function assertUint(n, min, max) {
|
|
1008
|
+
if (!Number.isInteger(n) || n < min || n > max) throw CborError.outOfRange();
|
|
1009
|
+
return n;
|
|
1010
|
+
}
|
|
1011
|
+
function assertGroup(groupThreshold, groupCount, groupIndex) {
|
|
1012
|
+
if (groupThreshold > groupCount || groupIndex >= groupCount) throw CborError.outOfRange();
|
|
1013
|
+
}
|
|
1014
|
+
function pack(share) {
|
|
1015
|
+
const identifier = assertUint(share.identifier, 0, 65535);
|
|
1016
|
+
const groupThreshold = assertUint(share.groupThreshold, 1, 16);
|
|
1017
|
+
const groupCount = assertUint(share.groupCount, 1, 16);
|
|
1018
|
+
const groupIndex = assertUint(share.groupIndex, 0, 15);
|
|
1019
|
+
const memberThreshold = assertUint(share.memberThreshold, 1, 16);
|
|
1020
|
+
const memberIndex = assertUint(share.memberIndex, 0, 15);
|
|
1021
|
+
assertGroup(groupThreshold, groupCount, groupIndex);
|
|
1022
|
+
const value = copyBuf(share.shareValue);
|
|
1023
|
+
const bytes = new Uint8Array(HEADER_LEN + value.length);
|
|
1024
|
+
bytes[0] = identifier >> 8;
|
|
1025
|
+
bytes[1] = identifier & 255;
|
|
1026
|
+
bytes[2] = groupThreshold - 1 << 4 | groupCount - 1;
|
|
1027
|
+
bytes[3] = groupIndex << 4 | memberThreshold - 1;
|
|
1028
|
+
bytes[4] = memberIndex;
|
|
1029
|
+
bytes.set(value, HEADER_LEN);
|
|
1030
|
+
return bytes;
|
|
1031
|
+
}
|
|
1032
|
+
function unpack(bytes) {
|
|
1033
|
+
if (bytes.length < HEADER_LEN) throw CborError.outOfRange();
|
|
1034
|
+
const header = new DataView(bytes.buffer, bytes.byteOffset, HEADER_LEN);
|
|
1035
|
+
if (header.getUint8(4) >> 4 !== 0) throw CborError.wrongType();
|
|
1036
|
+
const identifier = header.getUint16(0);
|
|
1037
|
+
const groupThreshold = (header.getUint8(2) >> 4) + 1;
|
|
1038
|
+
const groupCount = (header.getUint8(2) & 15) + 1;
|
|
1039
|
+
const groupIndex = header.getUint8(3) >> 4;
|
|
1040
|
+
const memberThreshold = (header.getUint8(3) & 15) + 1;
|
|
1041
|
+
const memberIndex = header.getUint8(4) & 15;
|
|
1042
|
+
assertGroup(groupThreshold, groupCount, groupIndex);
|
|
1043
|
+
return Object.freeze({
|
|
1044
|
+
identifier,
|
|
1045
|
+
groupThreshold,
|
|
1046
|
+
groupCount,
|
|
1047
|
+
groupIndex,
|
|
1048
|
+
memberThreshold,
|
|
1049
|
+
memberIndex,
|
|
1050
|
+
shareValue: copyBuf(bytes.subarray(HEADER_LEN))
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
const sskrCodec = {
|
|
1054
|
+
tags: [TAGS.sskr, TAGS["crypto-sskr"]],
|
|
1055
|
+
encode(share) {
|
|
1056
|
+
return cbor(pack(share));
|
|
1057
|
+
},
|
|
1058
|
+
decode(value) {
|
|
1059
|
+
return unpack(copyBytes(value));
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
//#endregion
|
|
1063
|
+
//#region src/registry/envelope.ts
|
|
1064
|
+
/** Inclusive cap: depth 0..=64 accepted (65 frames). Predicate is `>`, not `>=`. */
|
|
1065
|
+
const ENVELOPE_MAX_DEPTH = 64;
|
|
1066
|
+
function isElided(cbor) {
|
|
1067
|
+
return isBytes(cbor) && cbor.value.length === 32;
|
|
1068
|
+
}
|
|
1069
|
+
function assertAssertion(cbor, depth) {
|
|
1070
|
+
if (!isMap(cbor) || cbor.value.size !== 1) throw CborError.wrongType();
|
|
1071
|
+
for (const [key, value] of cbor.value) {
|
|
1072
|
+
assertEnvelopeContent(key, depth);
|
|
1073
|
+
assertEnvelopeContent(value, depth);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
function assertEnvelopeContent(cbor, depth = 0) {
|
|
1077
|
+
if (depth > 64) throw CborError.outOfRange();
|
|
1078
|
+
if (isTagged(cbor)) {
|
|
1079
|
+
if (hasTag(cbor, 201)) return;
|
|
1080
|
+
if (hasTag(cbor, 200)) {
|
|
1081
|
+
assertEnvelopeContent(cbor.value, depth + 1);
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (hasTag(cbor, 4e4)) {
|
|
1085
|
+
expectUnsigned(cbor.value);
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
if (hasTag(cbor, 40002) || hasTag(cbor, 40003)) return;
|
|
1089
|
+
throw CborError.wrongType();
|
|
1090
|
+
}
|
|
1091
|
+
if (isElided(cbor)) return;
|
|
1092
|
+
if (isArray(cbor)) {
|
|
1093
|
+
const items = cbor.value;
|
|
1094
|
+
if (items.length < 2) throw CborError.wrongType();
|
|
1095
|
+
const [subject] = items;
|
|
1096
|
+
if (subject === void 0) throw CborError.wrongType();
|
|
1097
|
+
assertEnvelopeContent(subject, depth + 1);
|
|
1098
|
+
for (const element of items.slice(1)) {
|
|
1099
|
+
if (isElided(element)) continue;
|
|
1100
|
+
assertAssertion(element, depth + 1);
|
|
1101
|
+
}
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
1104
|
+
if (isMap(cbor) && cbor.value.size === 1) {
|
|
1105
|
+
assertAssertion(cbor, depth + 1);
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
throw CborError.wrongType();
|
|
1109
|
+
}
|
|
1110
|
+
function identity(value) {
|
|
1111
|
+
assertEnvelopeContent(value);
|
|
1112
|
+
return value;
|
|
1113
|
+
}
|
|
1114
|
+
const envelopeCodec = {
|
|
1115
|
+
tags: [TAGS.envelope],
|
|
1116
|
+
encode: identity,
|
|
1117
|
+
decode: identity
|
|
1118
|
+
};
|
|
1119
|
+
//#endregion
|
|
1120
|
+
//#region src/registry/digest.ts
|
|
1121
|
+
/** BCR-2021-002: SHA-256 of the raw seed payload, not the CBOR map. */
|
|
1122
|
+
function seedDigest(seed) {
|
|
1123
|
+
return new Uint8Array(sha256(seed.payload));
|
|
1124
|
+
}
|
|
1125
|
+
/** BCR-2021-002: dCBOR of [keyData, chainCode | null, coinType, network]. */
|
|
1126
|
+
function hdKeyDigestSource(key) {
|
|
1127
|
+
const chainCode = key.chainCode === void 0 ? null : copyBuf(key.chainCode);
|
|
1128
|
+
const coinType = key.kind === "master" ? 0 : key.useInfo?.type ?? 0;
|
|
1129
|
+
const network = key.kind === "master" ? 0 : key.useInfo?.network ?? 0;
|
|
1130
|
+
return new Uint8Array(encodeCbor(cbor([
|
|
1131
|
+
copyBuf(key.keyData),
|
|
1132
|
+
chainCode,
|
|
1133
|
+
coinType,
|
|
1134
|
+
network
|
|
1135
|
+
])));
|
|
1136
|
+
}
|
|
1137
|
+
function hdKeyDigest(key) {
|
|
1138
|
+
return new Uint8Array(sha256(hdKeyDigestSource(key)));
|
|
1139
|
+
}
|
|
1140
|
+
//#endregion
|
|
1141
|
+
export { assertText as A, cryptoKeypathCodec as C, seedCodec as D, psbtCodec as E, expectUint as M, copyBuf as N, SCRIPT_TAGS as O, copyBytes as P, cryptoCoinInfoCodec as S, cryptoPsbtCodec as T, addressCodec as _, assertEnvelopeContent as a, Network as b, accountDescriptorCodec as c, outputDescriptorCodec as d, cryptoHdKeyCodec as f, writeDerivedMeta as g, readDerivedMeta as h, ENVELOPE_MAX_DEPTH as i, expectClosedIntMap as j, TAGS as k, cryptoAccountCodec as l, hdKeyCodec as m, hdKeyDigestSource as n, envelopeCodec as o, decodeDerivedHdKeyBody as p, seedDigest as r, sskrCodec as s, hdKeyDigest as t, cryptoOutputCodec as u, ecKeyCodec as v, keypathCodec as w, coinInfoCodec as x, CoinType as y };
|