@qntx/ur 1.8.0 → 2.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/dist/registry.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { C as fail, S as UrError, d as UrType } from "./ur-BUUcXyia.mjs";
2
- import { a as fromUrString, c as Ur, i as fromUr, n as MultipartEncoder, o as toUr, r as firstTagUrType, s as toUrString, t as MultipartDecoder } from "./typed-BmHx2GWT.mjs";
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
3
  import { sha256 } from "@noble/hashes/sha2.js";
4
- import { CborDate, CborError, CborMap, Tag, cbor, encodeCbor, expectArray, expectBoolean, expectBytes, expectInteger, expectMap, expectTaggedContent, expectText, expectUnsigned, hasTag, isArray, isBytes, isMap, isTagged, isUnsigned, taggedValue } from "@blockchaincommons/dcbor";
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
5
  //#region src/registry/bytes.ts
6
6
  function copyBuf(bytes) {
7
7
  return new Uint8Array(bytes);
@@ -40,29 +40,52 @@ function expectInt32(cbor) {
40
40
  function expectBool(cbor) {
41
41
  return expectBoolean(cbor);
42
42
  }
43
- //#endregion
44
- //#region src/registry/tags.ts
45
- const TAG_ENVELOPE = 200;
46
- const TAG_ENVELOPE_LEAF = 201;
47
- const TAG_SEED = 40300;
48
- const TAG_HDKEY = 40303;
49
- const TAG_KEYPATH = 40304;
50
- const TAG_COIN_INFO = 40305;
51
- const TAG_SSKR = 40309;
52
- const TAG_PSBT = 40310;
53
- const TAG_KNOWN_VALUE = 4e4;
54
- const TAG_DIGEST = 40001;
55
- const TAG_ENCRYPTED = 40002;
56
- const TAG_COMPRESSED = 40003;
57
- /** Codecs that exist in this version. Number constants for later tags live above. */
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
+ */
58
53
  const TAGS = {
59
54
  envelope: Tag.from(200, "envelope"),
60
- seed: Tag.from(TAG_SEED, "seed"),
61
- hdkey: Tag.from(TAG_HDKEY, "hdkey"),
62
- keypath: Tag.from(TAG_KEYPATH, "keypath"),
63
- "coin-info": Tag.from(TAG_COIN_INFO, "coin-info"),
64
- sskr: Tag.from(TAG_SSKR, "sskr"),
65
- psbt: Tag.from(TAG_PSBT, "psbt")
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")
66
89
  };
67
90
  //#endregion
68
91
  //#region src/registry/seed.ts
@@ -73,21 +96,31 @@ const SEED_KEYS = /* @__PURE__ */ new Set([
73
96
  4
74
97
  ]);
75
98
  const MAX_PAYLOAD = 64;
99
+ const TAG_EPOCH_DAYS = 100;
100
+ const SECONDS_PER_DAY = 86400;
76
101
  function assertPayloadLen(bytes) {
77
102
  if (bytes.length === 0 || bytes.length > MAX_PAYLOAD) throw CborError.outOfRange();
78
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
+ }
79
112
  const seedCodec = {
80
- tags: [TAGS.seed],
81
- untaggedCbor(seed) {
113
+ tags: [TAGS.seed, TAGS["crypto-seed"]],
114
+ encode(seed) {
82
115
  assertPayloadLen(seed.payload);
83
116
  const map = new CborMap();
84
117
  map.set(1, cbor(copyBuf(seed.payload)));
85
118
  if (seed.creationDate !== void 0) map.set(2, seed.creationDate);
86
- if (seed.name !== void 0 && seed.name !== "") map.set(3, seed.name);
87
- if (seed.note !== void 0 && seed.note !== "") map.set(4, seed.note);
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));
88
121
  return cbor(map);
89
122
  },
90
- fromUntaggedCbor(value) {
123
+ decode(value) {
91
124
  const map = expectClosedIntMap(value, SEED_KEYS);
92
125
  const payload = copyBytes(map.getOrThrow(1));
93
126
  assertPayloadLen(payload);
@@ -96,7 +129,7 @@ const seedCodec = {
96
129
  const note = map.get(4);
97
130
  return Object.freeze({
98
131
  payload,
99
- ...date === void 0 ? {} : { creationDate: CborDate.fromTaggedCbor(date) },
132
+ ...date === void 0 ? {} : { creationDate: expectSeedDate(date) },
100
133
  ...name === void 0 ? {} : { name: expectText(name) },
101
134
  ...note === void 0 ? {} : { note: expectText(note) }
102
135
  });
@@ -117,11 +150,11 @@ function copyPsbt(bytes) {
117
150
  return copyBuf(bytes);
118
151
  }
119
152
  const psbtCodec = {
120
- tags: [TAGS.psbt],
121
- untaggedCbor(psbt) {
153
+ tags: [TAGS.psbt, TAGS["crypto-psbt"]],
154
+ encode(psbt) {
122
155
  return cbor(copyPsbt(psbt.bytes));
123
156
  },
124
- fromUntaggedCbor(value) {
157
+ decode(value) {
125
158
  return Object.freeze({ bytes: copyPsbt(copyBytes(value)) });
126
159
  }
127
160
  };
@@ -225,9 +258,9 @@ function decodeComponents(value) {
225
258
  return components;
226
259
  }
227
260
  const keypathCodec = {
228
- tags: [TAGS.keypath],
229
- untaggedCbor(keypath) {
230
- if (keypath.components.length === 0 && keypath.sourceFingerprint === void 0) throw CborError.wrongType();
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();
231
264
  const items = [];
232
265
  for (const c of keypath.components) items.push(...encodeComponent(c));
233
266
  const map = new CborMap();
@@ -236,12 +269,12 @@ const keypathCodec = {
236
269
  if (keypath.depth !== void 0) map.set(3, assertUint8(keypath.depth));
237
270
  return cbor(map);
238
271
  },
239
- fromUntaggedCbor(value) {
272
+ decode(value) {
240
273
  const map = expectClosedIntMap(value, KEYPATH_KEYS);
241
274
  const components = decodeComponents(map.getOrThrow(1));
242
275
  const fingerprint = map.get(2);
243
276
  const depth = map.get(3);
244
- if (components.length === 0 && fingerprint === void 0) throw CborError.wrongType();
277
+ if (components.length === 0 && fingerprint === void 0 && depth === void 0) throw CborError.wrongType();
245
278
  return Object.freeze({
246
279
  components: Object.freeze(components),
247
280
  ...fingerprint === void 0 ? {} : { sourceFingerprint: expectUint32Ne0(fingerprint) },
@@ -269,14 +302,14 @@ function assertInt32(n) {
269
302
  return n;
270
303
  }
271
304
  const coinInfoCodec = {
272
- tags: [TAGS["coin-info"]],
273
- untaggedCbor(info) {
305
+ tags: [TAGS["coin-info"], TAGS["crypto-coin-info"]],
306
+ encode(info) {
274
307
  const map = new CborMap();
275
308
  if (info.type !== void 0 && info.type !== 0) map.set(1, assertUint31(info.type));
276
309
  if (info.network !== void 0 && info.network !== 0) map.set(2, assertInt32(info.network));
277
310
  return cbor(map);
278
311
  },
279
- fromUntaggedCbor(value) {
312
+ decode(value) {
280
313
  const map = expectClosedIntMap(value, COIN_INFO_KEYS);
281
314
  const type = map.get(1);
282
315
  const network = map.get(2);
@@ -287,17 +320,106 @@ const coinInfoCodec = {
287
320
  }
288
321
  };
289
322
  //#endregion
290
- //#region src/registry/tagged.ts
291
- function toTagged(codec, value) {
292
- const [tag] = codec.tags;
293
- if (tag === void 0) throw CborError.wrongType();
294
- return taggedValue(tag, codec.untaggedCbor(value));
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();
295
337
  }
296
- function fromTagged(codec, cbor) {
297
- const [tag] = codec.tags;
298
- if (tag === void 0) throw CborError.wrongType();
299
- return codec.fromUntaggedCbor(expectTaggedContent(cbor, tag.value));
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();
300
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
+ };
301
423
  //#endregion
302
424
  //#region src/registry/hdkey.ts
303
425
  const MASTER_KEYS = /* @__PURE__ */ new Set([
@@ -327,13 +449,16 @@ function bytesOfLen(value, len) {
327
449
  if (bytes.length !== len) throw CborError.outOfRange();
328
450
  return bytes;
329
451
  }
452
+ function assertKeyData(isPrivate, data) {
453
+ if (data.length !== KEY_DATA_LEN || isPrivate && data[0] !== 0) throw CborError.outOfRange();
454
+ }
330
455
  function assertUint32Ne0(n) {
331
456
  if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
332
457
  return n;
333
458
  }
334
459
  const hdKeyCodec = {
335
- tags: [TAGS.hdkey],
336
- untaggedCbor(key) {
460
+ tags: [TAGS.hdkey, TAGS["crypto-hdkey"]],
461
+ encode(key) {
337
462
  const map = new CborMap();
338
463
  if (key.kind === "master") {
339
464
  map.set(1, true);
@@ -342,17 +467,18 @@ const hdKeyCodec = {
342
467
  return cbor(map);
343
468
  }
344
469
  if (key.isPrivate === true) map.set(2, true);
345
- map.set(3, cbor(copyLen(key.keyData, KEY_DATA_LEN)));
470
+ assertKeyData(key.isPrivate === true, key.keyData);
471
+ map.set(3, cbor(copyBuf(key.keyData)));
346
472
  if (key.chainCode !== void 0) map.set(4, cbor(copyLen(key.chainCode, CHAIN_CODE_LEN)));
347
- if (key.useInfo !== void 0) map.set(5, toTagged(coinInfoCodec, key.useInfo));
348
- if (key.origin !== void 0) map.set(6, toTagged(keypathCodec, key.origin));
349
- if (key.children !== void 0) map.set(7, toTagged(keypathCodec, key.children));
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));
350
476
  if (key.parentFingerprint !== void 0) map.set(8, assertUint32Ne0(key.parentFingerprint));
351
- if (key.name !== void 0 && key.name !== "") map.set(9, key.name);
352
- if (key.note !== void 0 && key.note !== "") map.set(10, key.note);
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));
353
479
  return cbor(map);
354
480
  },
355
- fromUntaggedCbor(value) {
481
+ decode(value) {
356
482
  const masterFlag = expectMap(value).get(1);
357
483
  if (masterFlag !== void 0) {
358
484
  if (!expectBool(masterFlag)) throw CborError.wrongType();
@@ -365,6 +491,9 @@ const hdKeyCodec = {
365
491
  }
366
492
  const map = expectClosedIntMap(value, DERIVED_KEYS);
367
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);
368
497
  const chainCode = map.get(4);
369
498
  const useInfo = map.get(5);
370
499
  const origin = map.get(6);
@@ -374,12 +503,12 @@ const hdKeyCodec = {
374
503
  const note = map.get(10);
375
504
  return Object.freeze({
376
505
  kind: "derived",
377
- ...isPrivate === void 0 ? {} : { isPrivate: expectBool(isPrivate) },
378
- keyData: bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN),
506
+ ...isPrivate === void 0 ? {} : { isPrivate: priv },
507
+ keyData,
379
508
  ...chainCode === void 0 ? {} : { chainCode: bytesOfLen(chainCode, CHAIN_CODE_LEN) },
380
- ...useInfo === void 0 ? {} : { useInfo: fromTagged(coinInfoCodec, useInfo) },
381
- ...origin === void 0 ? {} : { origin: fromTagged(keypathCodec, origin) },
382
- ...children === void 0 ? {} : { children: fromTagged(keypathCodec, children) },
509
+ ...useInfo === void 0 ? {} : { useInfo: fromTagged(useInfo, coinInfoCodec) },
510
+ ...origin === void 0 ? {} : { origin: fromTagged(origin, keypathCodec) },
511
+ ...children === void 0 ? {} : { children: fromTagged(children, keypathCodec) },
383
512
  ...parentFingerprint === void 0 ? {} : { parentFingerprint: expectUint32Ne0(parentFingerprint) },
384
513
  ...name === void 0 ? {} : { name: expectText(name) },
385
514
  ...note === void 0 ? {} : { note: expectText(note) }
@@ -387,6 +516,250 @@ const hdKeyCodec = {
387
516
  }
388
517
  };
389
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
390
763
  //#region src/registry/sskr.ts
391
764
  const HEADER_LEN = 5;
392
765
  function assertUint(n, min, max) {
@@ -436,11 +809,11 @@ function unpack(bytes) {
436
809
  });
437
810
  }
438
811
  const sskrCodec = {
439
- tags: [TAGS.sskr],
440
- untaggedCbor(share) {
812
+ tags: [TAGS.sskr, TAGS["crypto-sskr"]],
813
+ encode(share) {
441
814
  return cbor(pack(share));
442
815
  },
443
- fromUntaggedCbor(value) {
816
+ decode(value) {
444
817
  return unpack(copyBytes(value));
445
818
  }
446
819
  };
@@ -498,33 +871,10 @@ function identity(value) {
498
871
  }
499
872
  const envelopeCodec = {
500
873
  tags: [TAGS.envelope],
501
- untaggedCbor: identity,
502
- fromUntaggedCbor: identity
874
+ encode: identity,
875
+ decode: identity
503
876
  };
504
877
  //#endregion
505
- //#region src/registry/codecs.ts
506
- function codecMap(codecs) {
507
- const m = /* @__PURE__ */ new Map();
508
- for (const c of codecs) {
509
- const token = firstTagUrType(c.tags).value;
510
- if (m.has(token)) throw new TypeError(`duplicate codec for UR type ${token}`);
511
- m.set(token, c);
512
- }
513
- return m;
514
- }
515
- function fromUrStringWith(uri, codecs) {
516
- const ur = Ur.fromUrString(uri);
517
- const codec = codecs.get(ur.type.value);
518
- if (codec === void 0) fail("UnexpectedType", {
519
- expected: [...codecs.keys()].join("|"),
520
- found: ur.type.value
521
- });
522
- return {
523
- type: ur.type.value,
524
- value: fromUr(ur, codec)
525
- };
526
- }
527
- //#endregion
528
878
  //#region src/registry/digest.ts
529
879
  /** BCR-2021-002: SHA-256 of the raw seed payload, not the CBOR map. */
530
880
  function seedDigest(seed) {
@@ -546,4 +896,4 @@ function hdKeyDigest(key) {
546
896
  return sha256(hdKeyDigestSource(key));
547
897
  }
548
898
  //#endregion
549
- export { CoinType, ENVELOPE_MAX_DEPTH, MultipartDecoder, MultipartEncoder, Network, TAGS, TAG_COIN_INFO, TAG_COMPRESSED, TAG_DIGEST, TAG_ENCRYPTED, TAG_ENVELOPE, TAG_ENVELOPE_LEAF, TAG_HDKEY, TAG_KEYPATH, TAG_KNOWN_VALUE, TAG_PSBT, TAG_SEED, TAG_SSKR, Ur, UrError, UrType, assertEnvelopeContent, codecMap, coinInfoCodec, envelopeCodec, firstTagUrType, fromUr, fromUrString, fromUrStringWith, hdKeyCodec, hdKeyDigest, hdKeyDigestSource, keypathCodec, psbtCodec, seedCodec, seedDigest, sskrCodec, toUr, toUrString };
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 };