@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/dist/registry.mjs CHANGED
@@ -1,899 +1,4 @@
1
- import { o as isUrType, s as parseUrType, w as UrError } from "./ur-7Ge3roVh.mjs";
2
- import { a as fromUrWith, c as Ur, i as fromUr, n as codecUrTypes, o as toTagged, r as fromTagged, s as toUr, t as codecMap } from "./typed-D-SyraAj.mjs";
3
- import { sha256 } from "@noble/hashes/sha2.js";
4
- import { CborDate, CborError, CborMap, MajorType, Tag, bytesToHex, cbor, encodeCbor, expectArray, expectBoolean, expectBytes, expectInteger, expectMap, expectText, expectUnsigned, extractTaggedContent, hasTag, isArray, isBytes, isMap, isTagged, isUnsigned } from "@blockchaincommons/dcbor";
5
- //#region src/registry/bytes.ts
6
- function copyBuf(bytes) {
7
- return new Uint8Array(bytes);
8
- }
9
- function copyBytes(cbor) {
10
- return copyBuf(expectBytes(cbor));
11
- }
12
- //#endregion
13
- //#region src/registry/map.ts
14
- function expectClosedIntMap(cbor, allowed) {
15
- const map = expectMap(cbor);
16
- for (const [key] of map) {
17
- const n = expectUnsigned(key);
18
- if (typeof n !== "number" || !Number.isInteger(n) || !allowed.has(n)) throw CborError.wrongType();
19
- }
20
- return map;
21
- }
22
- function expectUint(cbor, max) {
23
- const n = expectUnsigned(cbor);
24
- if (typeof n !== "number" || !Number.isInteger(n) || n < 0 || n > max) throw CborError.outOfRange();
25
- return n;
26
- }
27
- const expectUint8 = (c) => expectUint(c, 255);
28
- const expectUint31 = (c) => expectUint(c, 2147483647);
29
- const expectUint32Ne0 = (c) => {
30
- const n = expectUint(c, 4294967295);
31
- if (n === 0) throw CborError.outOfRange();
32
- return n;
33
- };
34
- /** CDDL `int` that must fit in JS number as int32. Used only for coin-info `network`. */
35
- function expectInt32(cbor) {
36
- const n = expectInteger(cbor);
37
- if (typeof n !== "number" || !Number.isInteger(n) || n < -2147483648 || n > 2147483647) throw CborError.outOfRange();
38
- return n;
39
- }
40
- function expectBool(cbor) {
41
- return expectBoolean(cbor);
42
- }
43
- /** Encode-side mirror of `expectText`: validates a caller-built field. */
44
- function assertText(value) {
45
- if (typeof value !== "string") throw CborError.wrongType();
46
- return value;
47
- }
48
- /**
49
- * Codecs that exist in this version. The `crypto-*` entries are the deprecated v1 tags
50
- * (BCR-2020-006): read-only, never written. `SCRIPT_TAGS` are the BCR-2020-010 script-expression
51
- * tags embedded inside v1 crypto-output 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, 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) },
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
- //#endregion
162
- //#region src/registry/keypath.ts
163
- const KEYPATH_KEYS = /* @__PURE__ */ new Set([
164
- 1,
165
- 2,
166
- 3
167
- ]);
168
- function assertUint31$1(n) {
169
- if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
170
- return n;
171
- }
172
- function assertUint8(n) {
173
- if (!Number.isInteger(n) || n < 0 || n > 255) throw CborError.outOfRange();
174
- return n;
175
- }
176
- function assertUint32Ne0$1(n) {
177
- if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
178
- return n;
179
- }
180
- function take(items, i) {
181
- const item = items[i];
182
- if (item === void 0) throw CborError.wrongType();
183
- return item;
184
- }
185
- function encodeComponent(c) {
186
- if (c.kind === "index") return [assertUint31$1(c.index), c.hardened];
187
- if (c.kind === "wildcard") return [[], c.hardened];
188
- if (c.kind === "range") {
189
- if (c.low >= c.high) throw CborError.outOfRange();
190
- return [[assertUint31$1(c.low), assertUint31$1(c.high)], c.hardened];
191
- }
192
- return [[
193
- assertUint31$1(c.external.index),
194
- c.external.hardened,
195
- assertUint31$1(c.internal.index),
196
- c.internal.hardened
197
- ]];
198
- }
199
- function decodeComponents(value) {
200
- const items = expectArray(value);
201
- const components = [];
202
- let i = 0;
203
- while (i < items.length) {
204
- const head = take(items, i);
205
- if (isUnsigned(head)) {
206
- const hardened = expectBool(take(items, i + 1));
207
- components.push({
208
- kind: "index",
209
- index: expectUint31(head),
210
- hardened
211
- });
212
- i += 2;
213
- continue;
214
- }
215
- if (isArray(head)) {
216
- const inner = head.value;
217
- if (inner.length === 0) {
218
- const hardened = expectBool(take(items, i + 1));
219
- components.push({
220
- kind: "wildcard",
221
- hardened
222
- });
223
- i += 2;
224
- continue;
225
- }
226
- if (inner.length === 2) {
227
- const low = expectUint31(take(inner, 0));
228
- const high = expectUint31(take(inner, 1));
229
- if (low >= high) throw CborError.outOfRange();
230
- const hardened = expectBool(take(items, i + 1));
231
- components.push({
232
- kind: "range",
233
- low,
234
- high,
235
- hardened
236
- });
237
- i += 2;
238
- continue;
239
- }
240
- if (inner.length === 4) {
241
- components.push({
242
- kind: "pair",
243
- external: {
244
- index: expectUint31(take(inner, 0)),
245
- hardened: expectBool(take(inner, 1))
246
- },
247
- internal: {
248
- index: expectUint31(take(inner, 2)),
249
- hardened: expectBool(take(inner, 3))
250
- }
251
- });
252
- i += 1;
253
- continue;
254
- }
255
- }
256
- throw CborError.wrongType();
257
- }
258
- return components;
259
- }
260
- const keypathCodec = {
261
- tags: [TAGS.keypath, TAGS["crypto-keypath"]],
262
- encode(keypath) {
263
- if (keypath.components.length === 0 && keypath.sourceFingerprint === void 0 && keypath.depth === void 0) throw CborError.wrongType();
264
- const items = [];
265
- for (const c of keypath.components) items.push(...encodeComponent(c));
266
- const map = new CborMap();
267
- map.set(1, items);
268
- if (keypath.sourceFingerprint !== void 0) map.set(2, assertUint32Ne0$1(keypath.sourceFingerprint));
269
- if (keypath.depth !== void 0) map.set(3, assertUint8(keypath.depth));
270
- return cbor(map);
271
- },
272
- decode(value) {
273
- const map = expectClosedIntMap(value, KEYPATH_KEYS);
274
- const components = decodeComponents(map.getOrThrow(1));
275
- const fingerprint = map.get(2);
276
- const depth = map.get(3);
277
- if (components.length === 0 && fingerprint === void 0 && depth === void 0) throw CborError.wrongType();
278
- return Object.freeze({
279
- components: Object.freeze(components),
280
- ...fingerprint === void 0 ? {} : { sourceFingerprint: expectUint32Ne0(fingerprint) },
281
- ...depth === void 0 ? {} : { depth: expectUint8(depth) }
282
- });
283
- }
284
- };
285
- //#endregion
286
- //#region src/registry/coin-info.ts
287
- const COIN_INFO_KEYS = /* @__PURE__ */ new Set([1, 2]);
288
- const CoinType = {
289
- btc: 0,
290
- eth: 60
291
- };
292
- const Network = {
293
- mainnet: 0,
294
- btcTestnet: 1
295
- };
296
- function assertUint31(n) {
297
- if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
298
- return n;
299
- }
300
- function assertInt32(n) {
301
- if (!Number.isInteger(n) || n < -2147483648 || n > 2147483647) throw CborError.outOfRange();
302
- return n;
303
- }
304
- const coinInfoCodec = {
305
- tags: [TAGS["coin-info"], TAGS["crypto-coin-info"]],
306
- encode(info) {
307
- const map = new CborMap();
308
- if (info.type !== void 0 && info.type !== 0) map.set(1, assertUint31(info.type));
309
- if (info.network !== void 0 && info.network !== 0) map.set(2, assertInt32(info.network));
310
- return cbor(map);
311
- },
312
- decode(value) {
313
- const map = expectClosedIntMap(value, COIN_INFO_KEYS);
314
- const type = map.get(1);
315
- const network = map.get(2);
316
- return Object.freeze({
317
- ...type === void 0 ? {} : { type: expectUint31(type) },
318
- ...network === void 0 ? {} : { network: expectInt32(network) }
319
- });
320
- }
321
- };
322
- //#endregion
323
- //#region src/registry/eckey.ts
324
- const ECKEY_KEYS = /* @__PURE__ */ new Set([
325
- 1,
326
- 2,
327
- 3
328
- ]);
329
- const CURVE_SECP256K1 = 0;
330
- const PRIVATE_KEY_LEN = 32;
331
- function assertDataLen$1(curve, isPrivate, data) {
332
- if (curve !== CURVE_SECP256K1) {
333
- if (data.length === 0) throw CborError.outOfRange();
334
- return;
335
- }
336
- if (!(isPrivate ? data.length === PRIVATE_KEY_LEN : data.length === 33 || data.length === 65)) throw CborError.outOfRange();
337
- }
338
- function assertCurve(n) {
339
- if (!Number.isSafeInteger(n) || n < 0) throw CborError.outOfRange();
340
- return n;
341
- }
342
- function expectCurve(value) {
343
- const n = expectUnsigned(value);
344
- if (typeof n !== "number") throw CborError.outOfRange();
345
- return assertCurve(n);
346
- }
347
- const ecKeyCodec = {
348
- tags: [TAGS.eckey, TAGS["crypto-eckey"]],
349
- encode(key) {
350
- const curve = key.curve ?? CURVE_SECP256K1;
351
- const isPrivate = key.isPrivate ?? false;
352
- assertCurve(curve);
353
- assertDataLen$1(curve, isPrivate, key.data);
354
- const map = new CborMap();
355
- if (curve !== CURVE_SECP256K1) map.set(1, curve);
356
- if (isPrivate) map.set(2, true);
357
- map.set(3, cbor(copyBuf(key.data)));
358
- return cbor(map);
359
- },
360
- decode(value) {
361
- const map = expectClosedIntMap(value, ECKEY_KEYS);
362
- const curveV = map.get(1);
363
- const isPrivateV = map.get(2);
364
- const data = copyBytes(map.getOrThrow(3));
365
- const curve = curveV === void 0 ? CURVE_SECP256K1 : expectCurve(curveV);
366
- const isPrivate = isPrivateV === void 0 ? false : expectBool(isPrivateV);
367
- assertDataLen$1(curve, isPrivate, data);
368
- return Object.freeze({
369
- ...curveV === void 0 ? {} : { curve },
370
- ...isPrivateV === void 0 ? {} : { isPrivate },
371
- data
372
- });
373
- }
374
- };
375
- //#endregion
376
- //#region src/registry/address.ts
377
- const ADDRESS_KEYS = /* @__PURE__ */ new Set([
378
- 1,
379
- 2,
380
- 3
381
- ]);
382
- const TYPED_DATA_LEN = 20;
383
- const ADDRESS_TYPES = [
384
- "p2pkh",
385
- "p2sh",
386
- "p2wpkh"
387
- ];
388
- function assertDataLen(type, data) {
389
- if (type !== void 0) {
390
- if (data.length !== TYPED_DATA_LEN) throw CborError.outOfRange();
391
- return;
392
- }
393
- if (data.length === 0) throw CborError.outOfRange();
394
- }
395
- const addressCodec = {
396
- tags: [TAGS.address, TAGS["crypto-address"]],
397
- encode(address) {
398
- assertDataLen(address.type, address.data);
399
- const map = new CborMap();
400
- if (address.info !== void 0) map.set(1, toTagged(address.info, coinInfoCodec));
401
- if (address.type !== void 0) {
402
- const index = ADDRESS_TYPES.indexOf(address.type);
403
- if (index === -1) throw CborError.wrongType();
404
- map.set(2, index);
405
- }
406
- map.set(3, cbor(copyBuf(address.data)));
407
- return cbor(map);
408
- },
409
- decode(value) {
410
- const map = expectClosedIntMap(value, ADDRESS_KEYS);
411
- const info = map.get(1);
412
- const typeV = map.get(2);
413
- const data = copyBytes(map.getOrThrow(3));
414
- const type = typeV === void 0 ? void 0 : ADDRESS_TYPES[expectUint(typeV, 2)];
415
- assertDataLen(type, data);
416
- return Object.freeze({
417
- ...info === void 0 ? {} : { info: fromTagged(info, coinInfoCodec) },
418
- ...type === void 0 ? {} : { type },
419
- data
420
- });
421
- }
422
- };
423
- //#endregion
424
- //#region src/registry/hdkey.ts
425
- const MASTER_KEYS = /* @__PURE__ */ new Set([
426
- 1,
427
- 3,
428
- 4
429
- ]);
430
- const DERIVED_KEYS = /* @__PURE__ */ new Set([
431
- 2,
432
- 3,
433
- 4,
434
- 5,
435
- 6,
436
- 7,
437
- 8,
438
- 9,
439
- 10
440
- ]);
441
- const KEY_DATA_LEN = 33;
442
- const CHAIN_CODE_LEN = 32;
443
- function copyLen(bytes, len) {
444
- if (bytes.length !== len) throw CborError.outOfRange();
445
- return copyBuf(bytes);
446
- }
447
- function bytesOfLen(value, len) {
448
- const bytes = copyBytes(value);
449
- if (bytes.length !== len) throw CborError.outOfRange();
450
- return bytes;
451
- }
452
- function assertKeyData(isPrivate, data) {
453
- if (data.length !== KEY_DATA_LEN || isPrivate && data[0] !== 0) throw CborError.outOfRange();
454
- }
455
- function assertUint32Ne0(n) {
456
- if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
457
- return n;
458
- }
459
- const hdKeyCodec = {
460
- tags: [TAGS.hdkey, TAGS["crypto-hdkey"]],
461
- encode(key) {
462
- const map = new CborMap();
463
- if (key.kind === "master") {
464
- map.set(1, true);
465
- map.set(3, cbor(copyLen(key.keyData, KEY_DATA_LEN)));
466
- map.set(4, cbor(copyLen(key.chainCode, CHAIN_CODE_LEN)));
467
- return cbor(map);
468
- }
469
- if (key.isPrivate === true) map.set(2, true);
470
- assertKeyData(key.isPrivate === true, key.keyData);
471
- map.set(3, cbor(copyBuf(key.keyData)));
472
- if (key.chainCode !== void 0) map.set(4, cbor(copyLen(key.chainCode, CHAIN_CODE_LEN)));
473
- if (key.useInfo !== void 0) map.set(5, toTagged(key.useInfo, coinInfoCodec));
474
- if (key.origin !== void 0) map.set(6, toTagged(key.origin, keypathCodec));
475
- if (key.children !== void 0) map.set(7, toTagged(key.children, keypathCodec));
476
- if (key.parentFingerprint !== void 0) map.set(8, assertUint32Ne0(key.parentFingerprint));
477
- if (key.name !== void 0 && key.name !== "") map.set(9, assertText(key.name));
478
- if (key.note !== void 0 && key.note !== "") map.set(10, assertText(key.note));
479
- return cbor(map);
480
- },
481
- decode(value) {
482
- const masterFlag = expectMap(value).get(1);
483
- if (masterFlag !== void 0) {
484
- if (!expectBool(masterFlag)) throw CborError.wrongType();
485
- const map = expectClosedIntMap(value, MASTER_KEYS);
486
- return Object.freeze({
487
- kind: "master",
488
- keyData: bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN),
489
- chainCode: bytesOfLen(map.getOrThrow(4), CHAIN_CODE_LEN)
490
- });
491
- }
492
- const map = expectClosedIntMap(value, DERIVED_KEYS);
493
- const isPrivate = map.get(2);
494
- const priv = isPrivate === void 0 ? false : expectBool(isPrivate);
495
- const keyData = bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN);
496
- assertKeyData(priv, keyData);
497
- const chainCode = map.get(4);
498
- const useInfo = map.get(5);
499
- const origin = map.get(6);
500
- const children = map.get(7);
501
- const parentFingerprint = map.get(8);
502
- const name = map.get(9);
503
- const note = map.get(10);
504
- return Object.freeze({
505
- kind: "derived",
506
- ...isPrivate === void 0 ? {} : { isPrivate: priv },
507
- keyData,
508
- ...chainCode === void 0 ? {} : { chainCode: bytesOfLen(chainCode, CHAIN_CODE_LEN) },
509
- ...useInfo === void 0 ? {} : { useInfo: fromTagged(useInfo, coinInfoCodec) },
510
- ...origin === void 0 ? {} : { origin: fromTagged(origin, keypathCodec) },
511
- ...children === void 0 ? {} : { children: fromTagged(children, keypathCodec) },
512
- ...parentFingerprint === void 0 ? {} : { parentFingerprint: expectUint32Ne0(parentFingerprint) },
513
- ...name === void 0 ? {} : { name: expectText(name) },
514
- ...note === void 0 ? {} : { note: expectText(note) }
515
- });
516
- }
517
- };
518
- //#endregion
519
- //#region src/registry/script-expression.ts
520
- const MULTIKEY_KEYS = /* @__PURE__ */ new Set([1, 2]);
521
- function tagNumber(cbor) {
522
- if (cbor.type !== MajorType.Tagged) throw CborError.wrongType();
523
- return Number(cbor.tag);
524
- }
525
- function bytesOf(cbor) {
526
- if (cbor.type !== MajorType.ByteString || !(cbor.value instanceof Uint8Array)) throw CborError.wrongType();
527
- return cbor.value;
528
- }
529
- function closedMultikeyMap(cbor) {
530
- if (cbor.type !== MajorType.Map) throw CborError.wrongType();
531
- const map = cbor.value;
532
- for (const [k] of map) if (k.type !== MajorType.Unsigned || !MULTIKEY_KEYS.has(Number(k.value))) throw CborError.wrongType();
533
- return map;
534
- }
535
- /** Key_exp = #6.306 / #6.303 / #6.307 (v2 tag equivalents accepted). */
536
- function keyExp(cbor) {
537
- const tag = tagNumber(cbor);
538
- const content = extractTaggedContent(cbor);
539
- if (tag === 40306 || tag === 306) return {
540
- kind: "eckey",
541
- key: ecKeyCodec.decode(content)
542
- };
543
- if (tag === 40303 || tag === 303) return {
544
- kind: "hdkey",
545
- key: hdKeyCodec.decode(content)
546
- };
547
- if (tag === 40307 || tag === 307) return {
548
- kind: "address",
549
- address: addressCodec.decode(content)
550
- };
551
- throw CborError.wrongType();
552
- }
553
- function isKeyExpTag(tag) {
554
- return tag === 40306 || tag === 306 || tag === 40303 || tag === 303 || tag === 40307 || tag === 307;
555
- }
556
- const KEY_FN = {
557
- [402]: "pk",
558
- [403]: "pkh",
559
- [404]: "wpkh",
560
- [405]: "combo",
561
- [410]: "cosigner"
562
- };
563
- const WRAPPER_FN = {
564
- [400]: "sh",
565
- [401]: "wsh"
566
- };
567
- /**
568
- * BIP-380 text rendering of a v1 script-expression tree (BCR-2020-010 tags 400–410). Keys become
569
- * `@0`, `@1`, … in order of appearance and are collected as v2 `DescriptorKey` values. The grammar
570
- * gives every script function at most one child subtree (multikey's list is handled inline), so
571
- * each node's local `@n` numbering is already the global order. `parent` is the enclosing wrapper
572
- * tag, which bounds nesting to `sh(wsh(...))`. Unsupported combinations (e.g. a taproot script
573
- * tree) throw `CborError` — surfaced as `CborType` by the codec layer.
574
- */
575
- function scriptExp(cbor, parent) {
576
- const tag = tagNumber(cbor);
577
- const content = extractTaggedContent(cbor);
578
- const keyFn = KEY_FN[tag];
579
- if (keyFn !== void 0) return {
580
- text: `${keyFn}(@0)`,
581
- keys: [keyExp(content)]
582
- };
583
- const wrapper = WRAPPER_FN[tag];
584
- if (wrapper !== void 0) {
585
- if (!(parent === void 0 || parent === 400 && tag === 401)) throw CborError.wrongType();
586
- if (content.type === MajorType.Tagged && isKeyExpTag(Number(content.tag))) {
587
- const key = keyExp(content);
588
- return {
589
- text: `${wrapper}(cosigner(@0))`,
590
- keys: [key]
591
- };
592
- }
593
- const inner = scriptExp(content, tag);
594
- return {
595
- text: `${wrapper}(${inner.text})`,
596
- keys: inner.keys
597
- };
598
- }
599
- if (tag === 406 || tag === 407) {
600
- const map = closedMultikeyMap(content);
601
- const thresholdV = map.get(1);
602
- const keysV = map.get(2);
603
- if (thresholdV === void 0 || keysV === void 0) throw CborError.wrongType();
604
- const threshold = expectUint31(thresholdV);
605
- const keys = expectArray(keysV);
606
- if (threshold < 1 || threshold > keys.length || keys.length === 0) throw CborError.outOfRange();
607
- const parts = [];
608
- const collected = [];
609
- for (const k of keys) {
610
- collected.push(keyExp(k));
611
- parts.push(`@${collected.length - 1}`);
612
- }
613
- return {
614
- text: `${tag === 406 ? "multi" : "sortedmulti"}(${threshold},${parts.join(",")})`,
615
- keys: collected
616
- };
617
- }
618
- if (tag === 408) return {
619
- text: `raw(${bytesToHex(bytesOf(content))})`,
620
- keys: []
621
- };
622
- if (tag === 409) return {
623
- text: "tr(@0)",
624
- keys: [keyExp(content)]
625
- };
626
- throw CborError.wrongType();
627
- }
628
- /**
629
- * Convert a v1 `crypto-output` body (a script-expression tagged value) into the v2
630
- * `OutputDescriptor` shape `{ source, keys }`.
631
- */
632
- function scriptExpressionToDescriptor(cbor) {
633
- const out = scriptExp(cbor);
634
- return Object.freeze({
635
- source: out.text,
636
- keys: Object.freeze(out.keys)
637
- });
638
- }
639
- //#endregion
640
- //#region src/registry/output-descriptor.ts
641
- const DESCRIPTOR_KEYS = /* @__PURE__ */ new Set([
642
- 1,
643
- 2,
644
- 3,
645
- 4
646
- ]);
647
- /** The `@n` placeholders in `source` must be exactly the set `0..keys.length-1`. */
648
- function assertPlaceholders(source, keyCount) {
649
- const found = /* @__PURE__ */ new Set();
650
- for (const m of source.matchAll(/@(\d+)/g)) {
651
- const n = Number(m[1]);
652
- if (!Number.isSafeInteger(n) || n < 0 || n >= keyCount) throw CborError.outOfRange();
653
- found.add(n);
654
- }
655
- if (found.size !== keyCount) throw CborError.outOfRange();
656
- }
657
- function encodeKey(key) {
658
- if (key.kind === "hdkey") return toTagged(key.key, hdKeyCodec);
659
- if (key.kind === "eckey") return toTagged(key.key, ecKeyCodec);
660
- return toTagged(key.address, addressCodec);
661
- }
662
- function decodeKey(cbor) {
663
- const tag = cbor.type === MajorType.Tagged ? Number(cbor.tag) : void 0;
664
- if (tag === TAGS.hdkey.value || tag === TAGS["crypto-hdkey"].value) return {
665
- kind: "hdkey",
666
- key: fromTagged(cbor, hdKeyCodec)
667
- };
668
- if (tag === TAGS.eckey.value || tag === TAGS["crypto-eckey"].value) return {
669
- kind: "eckey",
670
- key: fromTagged(cbor, ecKeyCodec)
671
- };
672
- if (tag === TAGS.address.value || tag === TAGS["crypto-address"].value) return {
673
- kind: "address",
674
- address: fromTagged(cbor, addressCodec)
675
- };
676
- throw CborError.wrongType();
677
- }
678
- function encodeDescriptor(desc) {
679
- assertPlaceholders(desc.source, desc.keys.length);
680
- const map = new CborMap();
681
- map.set(1, assertText(desc.source));
682
- if (desc.keys.length > 0) map.set(2, desc.keys.map(encodeKey));
683
- if (desc.name !== void 0 && desc.name !== "") map.set(3, assertText(desc.name));
684
- if (desc.note !== void 0 && desc.note !== "") map.set(4, assertText(desc.note));
685
- return cbor(map);
686
- }
687
- function decodeDescriptorMap(value) {
688
- const map = expectClosedIntMap(value, DESCRIPTOR_KEYS);
689
- const source = expectText(map.getOrThrow(1));
690
- const keysV = map.get(2);
691
- const nameV = map.get(3);
692
- const noteV = map.get(4);
693
- const keys = keysV === void 0 ? [] : expectArray(keysV).map(decodeKey);
694
- assertPlaceholders(source, keys.length);
695
- return Object.freeze({
696
- source,
697
- keys: Object.freeze(keys),
698
- ...nameV === void 0 ? {} : { name: expectText(nameV) },
699
- ...noteV === void 0 ? {} : { note: expectText(noteV) }
700
- });
701
- }
702
- const outputDescriptorCodec = {
703
- tags: [TAGS["output-descriptor"], TAGS["crypto-output"]],
704
- encode(desc) {
705
- return encodeDescriptor(desc);
706
- },
707
- decode(value) {
708
- if (isMap(value)) return decodeDescriptorMap(value);
709
- const { source, keys } = scriptExpressionToDescriptor(value);
710
- return Object.freeze({
711
- source,
712
- keys
713
- });
714
- }
715
- };
716
- //#endregion
717
- //#region src/registry/account-descriptor.ts
718
- const ACCOUNT_KEYS = /* @__PURE__ */ new Set([1, 2]);
719
- function assertFingerprint(n) {
720
- if (!Number.isInteger(n) || n < 0 || n > 4294967295) throw CborError.outOfRange();
721
- return n;
722
- }
723
- function assertDescriptors(descriptors) {
724
- if (descriptors.length === 0) throw CborError.outOfRange();
725
- }
726
- /**
727
- * One `output-descriptors` entry: a tagged `40308` map (v2), a tagged `308` crypto-output script
728
- * expression (v1, per BCR-2020-015), or — leniently, as KeystoneHQ writes it — a bare `400…410`
729
- * script expression.
730
- */
731
- function decodeEntry(item) {
732
- const tag = item.type === MajorType.Tagged ? Number(item.tag) : void 0;
733
- if (tag !== void 0 && tag >= 400 && tag <= 410) {
734
- const { source, keys } = scriptExpressionToDescriptor(item);
735
- return Object.freeze({
736
- source,
737
- keys
738
- });
739
- }
740
- return fromTagged(item, outputDescriptorCodec);
741
- }
742
- const accountDescriptorCodec = {
743
- tags: [TAGS["account-descriptor"], TAGS["crypto-account"]],
744
- encode(account) {
745
- assertFingerprint(account.masterFingerprint);
746
- assertDescriptors(account.outputDescriptors);
747
- const map = new CborMap();
748
- map.set(1, account.masterFingerprint);
749
- map.set(2, account.outputDescriptors.map((d) => toTagged(d, outputDescriptorCodec)));
750
- return cbor(map);
751
- },
752
- decode(value) {
753
- const map = expectClosedIntMap(value, ACCOUNT_KEYS);
754
- const descriptors = expectArray(map.getOrThrow(2)).map(decodeEntry);
755
- assertDescriptors(descriptors);
756
- return Object.freeze({
757
- masterFingerprint: expectUint(map.getOrThrow(1), 4294967295),
758
- outputDescriptors: Object.freeze(descriptors)
759
- });
760
- }
761
- };
762
- //#endregion
763
- //#region src/registry/sskr.ts
764
- const HEADER_LEN = 5;
765
- function assertUint(n, min, max) {
766
- if (!Number.isInteger(n) || n < min || n > max) throw CborError.outOfRange();
767
- return n;
768
- }
769
- function assertGroup(groupThreshold, groupCount, groupIndex) {
770
- if (groupThreshold > groupCount || groupIndex >= groupCount) throw CborError.outOfRange();
771
- }
772
- function pack(share) {
773
- const identifier = assertUint(share.identifier, 0, 65535);
774
- const groupThreshold = assertUint(share.groupThreshold, 1, 16);
775
- const groupCount = assertUint(share.groupCount, 1, 16);
776
- const groupIndex = assertUint(share.groupIndex, 0, 15);
777
- const memberThreshold = assertUint(share.memberThreshold, 1, 16);
778
- const memberIndex = assertUint(share.memberIndex, 0, 15);
779
- assertGroup(groupThreshold, groupCount, groupIndex);
780
- const value = copyBuf(share.shareValue);
781
- const bytes = new Uint8Array(HEADER_LEN + value.length);
782
- bytes[0] = identifier >> 8;
783
- bytes[1] = identifier & 255;
784
- bytes[2] = groupThreshold - 1 << 4 | groupCount - 1;
785
- bytes[3] = groupIndex << 4 | memberThreshold - 1;
786
- bytes[4] = memberIndex;
787
- bytes.set(value, HEADER_LEN);
788
- return bytes;
789
- }
790
- function unpack(bytes) {
791
- if (bytes.length < HEADER_LEN) throw CborError.outOfRange();
792
- const header = new DataView(bytes.buffer, bytes.byteOffset, HEADER_LEN);
793
- if (header.getUint8(4) >> 4 !== 0) throw CborError.wrongType();
794
- const identifier = header.getUint16(0);
795
- const groupThreshold = (header.getUint8(2) >> 4) + 1;
796
- const groupCount = (header.getUint8(2) & 15) + 1;
797
- const groupIndex = header.getUint8(3) >> 4;
798
- const memberThreshold = (header.getUint8(3) & 15) + 1;
799
- const memberIndex = header.getUint8(4) & 15;
800
- assertGroup(groupThreshold, groupCount, groupIndex);
801
- return Object.freeze({
802
- identifier,
803
- groupThreshold,
804
- groupCount,
805
- groupIndex,
806
- memberThreshold,
807
- memberIndex,
808
- shareValue: copyBuf(bytes.subarray(HEADER_LEN))
809
- });
810
- }
811
- const sskrCodec = {
812
- tags: [TAGS.sskr, TAGS["crypto-sskr"]],
813
- encode(share) {
814
- return cbor(pack(share));
815
- },
816
- decode(value) {
817
- return unpack(copyBytes(value));
818
- }
819
- };
820
- //#endregion
821
- //#region src/registry/envelope.ts
822
- /** Inclusive cap: depth 0..=64 accepted (65 frames). Predicate is `>`, not `>=`. */
823
- const ENVELOPE_MAX_DEPTH = 64;
824
- function isElided(cbor) {
825
- return isBytes(cbor) && cbor.value.length === 32;
826
- }
827
- function assertAssertion(cbor, depth) {
828
- if (!isMap(cbor) || cbor.value.size !== 1) throw CborError.wrongType();
829
- for (const [key, value] of cbor.value) {
830
- assertEnvelopeContent(key, depth);
831
- assertEnvelopeContent(value, depth);
832
- }
833
- }
834
- function assertEnvelopeContent(cbor, depth = 0) {
835
- if (depth > 64) throw CborError.outOfRange();
836
- if (isTagged(cbor)) {
837
- if (hasTag(cbor, 201)) return;
838
- if (hasTag(cbor, 200)) {
839
- assertEnvelopeContent(cbor.value, depth + 1);
840
- return;
841
- }
842
- if (hasTag(cbor, 4e4)) {
843
- expectUnsigned(cbor.value);
844
- return;
845
- }
846
- if (hasTag(cbor, 40002) || hasTag(cbor, 40003)) return;
847
- throw CborError.wrongType();
848
- }
849
- if (isElided(cbor)) return;
850
- if (isArray(cbor)) {
851
- const items = cbor.value;
852
- if (items.length < 2) throw CborError.wrongType();
853
- const [subject] = items;
854
- if (subject === void 0) throw CborError.wrongType();
855
- assertEnvelopeContent(subject, depth + 1);
856
- for (const element of items.slice(1)) {
857
- if (isElided(element)) continue;
858
- assertAssertion(element, depth + 1);
859
- }
860
- return;
861
- }
862
- if (isMap(cbor) && cbor.value.size === 1) {
863
- assertAssertion(cbor, depth + 1);
864
- return;
865
- }
866
- throw CborError.wrongType();
867
- }
868
- function identity(value) {
869
- assertEnvelopeContent(value);
870
- return value;
871
- }
872
- const envelopeCodec = {
873
- tags: [TAGS.envelope],
874
- encode: identity,
875
- decode: identity
876
- };
877
- //#endregion
878
- //#region src/registry/digest.ts
879
- /** BCR-2021-002: SHA-256 of the raw seed payload, not the CBOR map. */
880
- function seedDigest(seed) {
881
- return sha256(seed.payload);
882
- }
883
- /** BCR-2021-002: dCBOR of [keyData, chainCode | null, coinType, network]. */
884
- function hdKeyDigestSource(key) {
885
- const chainCode = key.chainCode === void 0 ? null : copyBuf(key.chainCode);
886
- const coinType = key.kind === "master" ? 0 : key.useInfo?.type ?? 0;
887
- const network = key.kind === "master" ? 0 : key.useInfo?.network ?? 0;
888
- return encodeCbor(cbor([
889
- copyBuf(key.keyData),
890
- chainCode,
891
- coinType,
892
- network
893
- ]));
894
- }
895
- function hdKeyDigest(key) {
896
- return sha256(hdKeyDigestSource(key));
897
- }
898
- //#endregion
899
- export { CoinType, ENVELOPE_MAX_DEPTH, Network, SCRIPT_TAGS, TAGS, Ur, UrError, accountDescriptorCodec, addressCodec, assertEnvelopeContent, codecMap, codecUrTypes, coinInfoCodec, ecKeyCodec, envelopeCodec, fromTagged, fromUr, fromUrWith, hdKeyCodec, hdKeyDigest, hdKeyDigestSource, isUrType, keypathCodec, outputDescriptorCodec, parseUrType, psbtCodec, seedCodec, seedDigest, sskrCodec, toTagged, toUr };
1
+ import { o as isUrType, s as parseUrType, w as UrError } from "./ur-ChGW72hQ.mjs";
2
+ import { a as fromUrWith, c as Ur, i as fromUr, n as codecUrTypes, o as toTagged, r as fromTagged, s as toUr, t as codecMap } from "./typed-C8j8iVYT.mjs";
3
+ import { C as cryptoKeypathCodec, D as seedCodec, E as psbtCodec, O as SCRIPT_TAGS, S as cryptoCoinInfoCodec, T as cryptoPsbtCodec, _ as addressCodec, a as assertEnvelopeContent, b as Network, c as accountDescriptorCodec, d as outputDescriptorCodec, f as cryptoHdKeyCodec, i as ENVELOPE_MAX_DEPTH, k as TAGS, l as cryptoAccountCodec, m as hdKeyCodec, n as hdKeyDigestSource, o as envelopeCodec, r as seedDigest, s as sskrCodec, t as hdKeyDigest, u as cryptoOutputCodec, v as ecKeyCodec, w as keypathCodec, x as coinInfoCodec, y as CoinType } from "./registry-BGnkPqkV.mjs";
4
+ export { CoinType, ENVELOPE_MAX_DEPTH, Network, SCRIPT_TAGS, TAGS, Ur, UrError, accountDescriptorCodec, addressCodec, assertEnvelopeContent, codecMap, codecUrTypes, coinInfoCodec, cryptoAccountCodec, cryptoCoinInfoCodec, cryptoHdKeyCodec, cryptoKeypathCodec, cryptoOutputCodec, cryptoPsbtCodec, ecKeyCodec, envelopeCodec, fromTagged, fromUr, fromUrWith, hdKeyCodec, hdKeyDigest, hdKeyDigestSource, isUrType, keypathCodec, outputDescriptorCodec, parseUrType, psbtCodec, seedCodec, seedDigest, sskrCodec, toTagged, toUr };