@qntx/ur 1.1.0 → 1.8.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 +2 -16
- package/dist/index-BeG2XIr5.d.mts +59 -0
- package/dist/index.d.mts +29 -31
- package/dist/index.mjs +1 -1
- package/dist/registry.d.mts +142 -0
- package/dist/registry.mjs +549 -0
- package/dist/{type-CSfO9fHL.d.mts → type-CZ0CIHJk.d.mts} +10 -6
- package/dist/typed-BmHx2GWT.mjs +150 -0
- package/dist/typed.d.mts +2 -58
- package/dist/typed.mjs +2 -145
- package/dist/{ur-C5n_FgVn.mjs → ur-BUUcXyia.mjs} +209 -132
- package/package.json +14 -13
|
@@ -0,0 +1,549 @@
|
|
|
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";
|
|
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";
|
|
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
|
+
//#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. */
|
|
58
|
+
const TAGS = {
|
|
59
|
+
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")
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/registry/seed.ts
|
|
69
|
+
const SEED_KEYS = /* @__PURE__ */ new Set([
|
|
70
|
+
1,
|
|
71
|
+
2,
|
|
72
|
+
3,
|
|
73
|
+
4
|
|
74
|
+
]);
|
|
75
|
+
const MAX_PAYLOAD = 64;
|
|
76
|
+
function assertPayloadLen(bytes) {
|
|
77
|
+
if (bytes.length === 0 || bytes.length > MAX_PAYLOAD) throw CborError.outOfRange();
|
|
78
|
+
}
|
|
79
|
+
const seedCodec = {
|
|
80
|
+
tags: [TAGS.seed],
|
|
81
|
+
untaggedCbor(seed) {
|
|
82
|
+
assertPayloadLen(seed.payload);
|
|
83
|
+
const map = new CborMap();
|
|
84
|
+
map.set(1, cbor(copyBuf(seed.payload)));
|
|
85
|
+
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);
|
|
88
|
+
return cbor(map);
|
|
89
|
+
},
|
|
90
|
+
fromUntaggedCbor(value) {
|
|
91
|
+
const map = expectClosedIntMap(value, SEED_KEYS);
|
|
92
|
+
const payload = copyBytes(map.getOrThrow(1));
|
|
93
|
+
assertPayloadLen(payload);
|
|
94
|
+
const date = map.get(2);
|
|
95
|
+
const name = map.get(3);
|
|
96
|
+
const note = map.get(4);
|
|
97
|
+
return Object.freeze({
|
|
98
|
+
payload,
|
|
99
|
+
...date === void 0 ? {} : { creationDate: CborDate.fromTaggedCbor(date) },
|
|
100
|
+
...name === void 0 ? {} : { name: expectText(name) },
|
|
101
|
+
...note === void 0 ? {} : { note: expectText(note) }
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/registry/psbt.ts
|
|
107
|
+
const PSBT_MAGIC = [
|
|
108
|
+
112,
|
|
109
|
+
115,
|
|
110
|
+
98,
|
|
111
|
+
116,
|
|
112
|
+
255
|
|
113
|
+
];
|
|
114
|
+
function copyPsbt(bytes) {
|
|
115
|
+
if (bytes.length < PSBT_MAGIC.length) throw CborError.outOfRange();
|
|
116
|
+
for (let i = 0; i < PSBT_MAGIC.length; i++) if (bytes[i] !== PSBT_MAGIC[i]) throw CborError.wrongType();
|
|
117
|
+
return copyBuf(bytes);
|
|
118
|
+
}
|
|
119
|
+
const psbtCodec = {
|
|
120
|
+
tags: [TAGS.psbt],
|
|
121
|
+
untaggedCbor(psbt) {
|
|
122
|
+
return cbor(copyPsbt(psbt.bytes));
|
|
123
|
+
},
|
|
124
|
+
fromUntaggedCbor(value) {
|
|
125
|
+
return Object.freeze({ bytes: copyPsbt(copyBytes(value)) });
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/registry/keypath.ts
|
|
130
|
+
const KEYPATH_KEYS = /* @__PURE__ */ new Set([
|
|
131
|
+
1,
|
|
132
|
+
2,
|
|
133
|
+
3
|
|
134
|
+
]);
|
|
135
|
+
function assertUint31$1(n) {
|
|
136
|
+
if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
|
|
137
|
+
return n;
|
|
138
|
+
}
|
|
139
|
+
function assertUint8(n) {
|
|
140
|
+
if (!Number.isInteger(n) || n < 0 || n > 255) throw CborError.outOfRange();
|
|
141
|
+
return n;
|
|
142
|
+
}
|
|
143
|
+
function assertUint32Ne0$1(n) {
|
|
144
|
+
if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
|
|
145
|
+
return n;
|
|
146
|
+
}
|
|
147
|
+
function take(items, i) {
|
|
148
|
+
const item = items[i];
|
|
149
|
+
if (item === void 0) throw CborError.wrongType();
|
|
150
|
+
return item;
|
|
151
|
+
}
|
|
152
|
+
function encodeComponent(c) {
|
|
153
|
+
if (c.kind === "index") return [assertUint31$1(c.index), c.hardened];
|
|
154
|
+
if (c.kind === "wildcard") return [[], c.hardened];
|
|
155
|
+
if (c.kind === "range") {
|
|
156
|
+
if (c.low >= c.high) throw CborError.outOfRange();
|
|
157
|
+
return [[assertUint31$1(c.low), assertUint31$1(c.high)], c.hardened];
|
|
158
|
+
}
|
|
159
|
+
return [[
|
|
160
|
+
assertUint31$1(c.external.index),
|
|
161
|
+
c.external.hardened,
|
|
162
|
+
assertUint31$1(c.internal.index),
|
|
163
|
+
c.internal.hardened
|
|
164
|
+
]];
|
|
165
|
+
}
|
|
166
|
+
function decodeComponents(value) {
|
|
167
|
+
const items = expectArray(value);
|
|
168
|
+
const components = [];
|
|
169
|
+
let i = 0;
|
|
170
|
+
while (i < items.length) {
|
|
171
|
+
const head = take(items, i);
|
|
172
|
+
if (isUnsigned(head)) {
|
|
173
|
+
const hardened = expectBool(take(items, i + 1));
|
|
174
|
+
components.push({
|
|
175
|
+
kind: "index",
|
|
176
|
+
index: expectUint31(head),
|
|
177
|
+
hardened
|
|
178
|
+
});
|
|
179
|
+
i += 2;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (isArray(head)) {
|
|
183
|
+
const inner = head.value;
|
|
184
|
+
if (inner.length === 0) {
|
|
185
|
+
const hardened = expectBool(take(items, i + 1));
|
|
186
|
+
components.push({
|
|
187
|
+
kind: "wildcard",
|
|
188
|
+
hardened
|
|
189
|
+
});
|
|
190
|
+
i += 2;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (inner.length === 2) {
|
|
194
|
+
const low = expectUint31(take(inner, 0));
|
|
195
|
+
const high = expectUint31(take(inner, 1));
|
|
196
|
+
if (low >= high) throw CborError.outOfRange();
|
|
197
|
+
const hardened = expectBool(take(items, i + 1));
|
|
198
|
+
components.push({
|
|
199
|
+
kind: "range",
|
|
200
|
+
low,
|
|
201
|
+
high,
|
|
202
|
+
hardened
|
|
203
|
+
});
|
|
204
|
+
i += 2;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (inner.length === 4) {
|
|
208
|
+
components.push({
|
|
209
|
+
kind: "pair",
|
|
210
|
+
external: {
|
|
211
|
+
index: expectUint31(take(inner, 0)),
|
|
212
|
+
hardened: expectBool(take(inner, 1))
|
|
213
|
+
},
|
|
214
|
+
internal: {
|
|
215
|
+
index: expectUint31(take(inner, 2)),
|
|
216
|
+
hardened: expectBool(take(inner, 3))
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
i += 1;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
throw CborError.wrongType();
|
|
224
|
+
}
|
|
225
|
+
return components;
|
|
226
|
+
}
|
|
227
|
+
const keypathCodec = {
|
|
228
|
+
tags: [TAGS.keypath],
|
|
229
|
+
untaggedCbor(keypath) {
|
|
230
|
+
if (keypath.components.length === 0 && keypath.sourceFingerprint === void 0) throw CborError.wrongType();
|
|
231
|
+
const items = [];
|
|
232
|
+
for (const c of keypath.components) items.push(...encodeComponent(c));
|
|
233
|
+
const map = new CborMap();
|
|
234
|
+
map.set(1, items);
|
|
235
|
+
if (keypath.sourceFingerprint !== void 0) map.set(2, assertUint32Ne0$1(keypath.sourceFingerprint));
|
|
236
|
+
if (keypath.depth !== void 0) map.set(3, assertUint8(keypath.depth));
|
|
237
|
+
return cbor(map);
|
|
238
|
+
},
|
|
239
|
+
fromUntaggedCbor(value) {
|
|
240
|
+
const map = expectClosedIntMap(value, KEYPATH_KEYS);
|
|
241
|
+
const components = decodeComponents(map.getOrThrow(1));
|
|
242
|
+
const fingerprint = map.get(2);
|
|
243
|
+
const depth = map.get(3);
|
|
244
|
+
if (components.length === 0 && fingerprint === void 0) throw CborError.wrongType();
|
|
245
|
+
return Object.freeze({
|
|
246
|
+
components: Object.freeze(components),
|
|
247
|
+
...fingerprint === void 0 ? {} : { sourceFingerprint: expectUint32Ne0(fingerprint) },
|
|
248
|
+
...depth === void 0 ? {} : { depth: expectUint8(depth) }
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/registry/coin-info.ts
|
|
254
|
+
const COIN_INFO_KEYS = /* @__PURE__ */ new Set([1, 2]);
|
|
255
|
+
const CoinType = {
|
|
256
|
+
btc: 0,
|
|
257
|
+
eth: 60
|
|
258
|
+
};
|
|
259
|
+
const Network = {
|
|
260
|
+
mainnet: 0,
|
|
261
|
+
btcTestnet: 1
|
|
262
|
+
};
|
|
263
|
+
function assertUint31(n) {
|
|
264
|
+
if (!Number.isInteger(n) || n < 0 || n > 2147483647) throw CborError.outOfRange();
|
|
265
|
+
return n;
|
|
266
|
+
}
|
|
267
|
+
function assertInt32(n) {
|
|
268
|
+
if (!Number.isInteger(n) || n < -2147483648 || n > 2147483647) throw CborError.outOfRange();
|
|
269
|
+
return n;
|
|
270
|
+
}
|
|
271
|
+
const coinInfoCodec = {
|
|
272
|
+
tags: [TAGS["coin-info"]],
|
|
273
|
+
untaggedCbor(info) {
|
|
274
|
+
const map = new CborMap();
|
|
275
|
+
if (info.type !== void 0 && info.type !== 0) map.set(1, assertUint31(info.type));
|
|
276
|
+
if (info.network !== void 0 && info.network !== 0) map.set(2, assertInt32(info.network));
|
|
277
|
+
return cbor(map);
|
|
278
|
+
},
|
|
279
|
+
fromUntaggedCbor(value) {
|
|
280
|
+
const map = expectClosedIntMap(value, COIN_INFO_KEYS);
|
|
281
|
+
const type = map.get(1);
|
|
282
|
+
const network = map.get(2);
|
|
283
|
+
return Object.freeze({
|
|
284
|
+
...type === void 0 ? {} : { type: expectUint31(type) },
|
|
285
|
+
...network === void 0 ? {} : { network: expectInt32(network) }
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
//#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));
|
|
295
|
+
}
|
|
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));
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region src/registry/hdkey.ts
|
|
303
|
+
const MASTER_KEYS = /* @__PURE__ */ new Set([
|
|
304
|
+
1,
|
|
305
|
+
3,
|
|
306
|
+
4
|
|
307
|
+
]);
|
|
308
|
+
const DERIVED_KEYS = /* @__PURE__ */ new Set([
|
|
309
|
+
2,
|
|
310
|
+
3,
|
|
311
|
+
4,
|
|
312
|
+
5,
|
|
313
|
+
6,
|
|
314
|
+
7,
|
|
315
|
+
8,
|
|
316
|
+
9,
|
|
317
|
+
10
|
|
318
|
+
]);
|
|
319
|
+
const KEY_DATA_LEN = 33;
|
|
320
|
+
const CHAIN_CODE_LEN = 32;
|
|
321
|
+
function copyLen(bytes, len) {
|
|
322
|
+
if (bytes.length !== len) throw CborError.outOfRange();
|
|
323
|
+
return copyBuf(bytes);
|
|
324
|
+
}
|
|
325
|
+
function bytesOfLen(value, len) {
|
|
326
|
+
const bytes = copyBytes(value);
|
|
327
|
+
if (bytes.length !== len) throw CborError.outOfRange();
|
|
328
|
+
return bytes;
|
|
329
|
+
}
|
|
330
|
+
function assertUint32Ne0(n) {
|
|
331
|
+
if (!Number.isInteger(n) || n < 1 || n > 4294967295) throw CborError.outOfRange();
|
|
332
|
+
return n;
|
|
333
|
+
}
|
|
334
|
+
const hdKeyCodec = {
|
|
335
|
+
tags: [TAGS.hdkey],
|
|
336
|
+
untaggedCbor(key) {
|
|
337
|
+
const map = new CborMap();
|
|
338
|
+
if (key.kind === "master") {
|
|
339
|
+
map.set(1, true);
|
|
340
|
+
map.set(3, cbor(copyLen(key.keyData, KEY_DATA_LEN)));
|
|
341
|
+
map.set(4, cbor(copyLen(key.chainCode, CHAIN_CODE_LEN)));
|
|
342
|
+
return cbor(map);
|
|
343
|
+
}
|
|
344
|
+
if (key.isPrivate === true) map.set(2, true);
|
|
345
|
+
map.set(3, cbor(copyLen(key.keyData, KEY_DATA_LEN)));
|
|
346
|
+
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));
|
|
350
|
+
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);
|
|
353
|
+
return cbor(map);
|
|
354
|
+
},
|
|
355
|
+
fromUntaggedCbor(value) {
|
|
356
|
+
const masterFlag = expectMap(value).get(1);
|
|
357
|
+
if (masterFlag !== void 0) {
|
|
358
|
+
if (!expectBool(masterFlag)) throw CborError.wrongType();
|
|
359
|
+
const map = expectClosedIntMap(value, MASTER_KEYS);
|
|
360
|
+
return Object.freeze({
|
|
361
|
+
kind: "master",
|
|
362
|
+
keyData: bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN),
|
|
363
|
+
chainCode: bytesOfLen(map.getOrThrow(4), CHAIN_CODE_LEN)
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
const map = expectClosedIntMap(value, DERIVED_KEYS);
|
|
367
|
+
const isPrivate = map.get(2);
|
|
368
|
+
const chainCode = map.get(4);
|
|
369
|
+
const useInfo = map.get(5);
|
|
370
|
+
const origin = map.get(6);
|
|
371
|
+
const children = map.get(7);
|
|
372
|
+
const parentFingerprint = map.get(8);
|
|
373
|
+
const name = map.get(9);
|
|
374
|
+
const note = map.get(10);
|
|
375
|
+
return Object.freeze({
|
|
376
|
+
kind: "derived",
|
|
377
|
+
...isPrivate === void 0 ? {} : { isPrivate: expectBool(isPrivate) },
|
|
378
|
+
keyData: bytesOfLen(map.getOrThrow(3), KEY_DATA_LEN),
|
|
379
|
+
...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) },
|
|
383
|
+
...parentFingerprint === void 0 ? {} : { parentFingerprint: expectUint32Ne0(parentFingerprint) },
|
|
384
|
+
...name === void 0 ? {} : { name: expectText(name) },
|
|
385
|
+
...note === void 0 ? {} : { note: expectText(note) }
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/registry/sskr.ts
|
|
391
|
+
const HEADER_LEN = 5;
|
|
392
|
+
function assertUint(n, min, max) {
|
|
393
|
+
if (!Number.isInteger(n) || n < min || n > max) throw CborError.outOfRange();
|
|
394
|
+
return n;
|
|
395
|
+
}
|
|
396
|
+
function assertGroup(groupThreshold, groupCount, groupIndex) {
|
|
397
|
+
if (groupThreshold > groupCount || groupIndex >= groupCount) throw CborError.outOfRange();
|
|
398
|
+
}
|
|
399
|
+
function pack(share) {
|
|
400
|
+
const identifier = assertUint(share.identifier, 0, 65535);
|
|
401
|
+
const groupThreshold = assertUint(share.groupThreshold, 1, 16);
|
|
402
|
+
const groupCount = assertUint(share.groupCount, 1, 16);
|
|
403
|
+
const groupIndex = assertUint(share.groupIndex, 0, 15);
|
|
404
|
+
const memberThreshold = assertUint(share.memberThreshold, 1, 16);
|
|
405
|
+
const memberIndex = assertUint(share.memberIndex, 0, 15);
|
|
406
|
+
assertGroup(groupThreshold, groupCount, groupIndex);
|
|
407
|
+
const value = copyBuf(share.shareValue);
|
|
408
|
+
const bytes = new Uint8Array(HEADER_LEN + value.length);
|
|
409
|
+
bytes[0] = identifier >> 8;
|
|
410
|
+
bytes[1] = identifier & 255;
|
|
411
|
+
bytes[2] = groupThreshold - 1 << 4 | groupCount - 1;
|
|
412
|
+
bytes[3] = groupIndex << 4 | memberThreshold - 1;
|
|
413
|
+
bytes[4] = memberIndex;
|
|
414
|
+
bytes.set(value, HEADER_LEN);
|
|
415
|
+
return bytes;
|
|
416
|
+
}
|
|
417
|
+
function unpack(bytes) {
|
|
418
|
+
if (bytes.length < HEADER_LEN) throw CborError.outOfRange();
|
|
419
|
+
const header = new DataView(bytes.buffer, bytes.byteOffset, HEADER_LEN);
|
|
420
|
+
if (header.getUint8(4) >> 4 !== 0) throw CborError.wrongType();
|
|
421
|
+
const identifier = header.getUint16(0);
|
|
422
|
+
const groupThreshold = (header.getUint8(2) >> 4) + 1;
|
|
423
|
+
const groupCount = (header.getUint8(2) & 15) + 1;
|
|
424
|
+
const groupIndex = header.getUint8(3) >> 4;
|
|
425
|
+
const memberThreshold = (header.getUint8(3) & 15) + 1;
|
|
426
|
+
const memberIndex = header.getUint8(4) & 15;
|
|
427
|
+
assertGroup(groupThreshold, groupCount, groupIndex);
|
|
428
|
+
return Object.freeze({
|
|
429
|
+
identifier,
|
|
430
|
+
groupThreshold,
|
|
431
|
+
groupCount,
|
|
432
|
+
groupIndex,
|
|
433
|
+
memberThreshold,
|
|
434
|
+
memberIndex,
|
|
435
|
+
shareValue: copyBuf(bytes.subarray(HEADER_LEN))
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
const sskrCodec = {
|
|
439
|
+
tags: [TAGS.sskr],
|
|
440
|
+
untaggedCbor(share) {
|
|
441
|
+
return cbor(pack(share));
|
|
442
|
+
},
|
|
443
|
+
fromUntaggedCbor(value) {
|
|
444
|
+
return unpack(copyBytes(value));
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
//#endregion
|
|
448
|
+
//#region src/registry/envelope.ts
|
|
449
|
+
/** Inclusive cap: depth 0..=64 accepted (65 frames). Predicate is `>`, not `>=`. */
|
|
450
|
+
const ENVELOPE_MAX_DEPTH = 64;
|
|
451
|
+
function isElided(cbor) {
|
|
452
|
+
return isBytes(cbor) && cbor.value.length === 32;
|
|
453
|
+
}
|
|
454
|
+
function assertAssertion(cbor, depth) {
|
|
455
|
+
if (!isMap(cbor) || cbor.value.size !== 1) throw CborError.wrongType();
|
|
456
|
+
for (const [key, value] of cbor.value) {
|
|
457
|
+
assertEnvelopeContent(key, depth);
|
|
458
|
+
assertEnvelopeContent(value, depth);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
function assertEnvelopeContent(cbor, depth = 0) {
|
|
462
|
+
if (depth > 64) throw CborError.outOfRange();
|
|
463
|
+
if (isTagged(cbor)) {
|
|
464
|
+
if (hasTag(cbor, 201)) return;
|
|
465
|
+
if (hasTag(cbor, 200)) {
|
|
466
|
+
assertEnvelopeContent(cbor.value, depth + 1);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (hasTag(cbor, 4e4)) {
|
|
470
|
+
expectUnsigned(cbor.value);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (hasTag(cbor, 40002) || hasTag(cbor, 40003)) return;
|
|
474
|
+
throw CborError.wrongType();
|
|
475
|
+
}
|
|
476
|
+
if (isElided(cbor)) return;
|
|
477
|
+
if (isArray(cbor)) {
|
|
478
|
+
const items = cbor.value;
|
|
479
|
+
if (items.length < 2) throw CborError.wrongType();
|
|
480
|
+
const [subject] = items;
|
|
481
|
+
if (subject === void 0) throw CborError.wrongType();
|
|
482
|
+
assertEnvelopeContent(subject, depth + 1);
|
|
483
|
+
for (const element of items.slice(1)) {
|
|
484
|
+
if (isElided(element)) continue;
|
|
485
|
+
assertAssertion(element, depth + 1);
|
|
486
|
+
}
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (isMap(cbor) && cbor.value.size === 1) {
|
|
490
|
+
assertAssertion(cbor, depth + 1);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
throw CborError.wrongType();
|
|
494
|
+
}
|
|
495
|
+
function identity(value) {
|
|
496
|
+
assertEnvelopeContent(value);
|
|
497
|
+
return value;
|
|
498
|
+
}
|
|
499
|
+
const envelopeCodec = {
|
|
500
|
+
tags: [TAGS.envelope],
|
|
501
|
+
untaggedCbor: identity,
|
|
502
|
+
fromUntaggedCbor: identity
|
|
503
|
+
};
|
|
504
|
+
//#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
|
+
//#region src/registry/digest.ts
|
|
529
|
+
/** BCR-2021-002: SHA-256 of the raw seed payload, not the CBOR map. */
|
|
530
|
+
function seedDigest(seed) {
|
|
531
|
+
return sha256(seed.payload);
|
|
532
|
+
}
|
|
533
|
+
/** BCR-2021-002: dCBOR of [keyData, chainCode | null, coinType, network]. */
|
|
534
|
+
function hdKeyDigestSource(key) {
|
|
535
|
+
const chainCode = key.chainCode === void 0 ? null : copyBuf(key.chainCode);
|
|
536
|
+
const coinType = key.kind === "master" ? 0 : key.useInfo?.type ?? 0;
|
|
537
|
+
const network = key.kind === "master" ? 0 : key.useInfo?.network ?? 0;
|
|
538
|
+
return encodeCbor(cbor([
|
|
539
|
+
copyBuf(key.keyData),
|
|
540
|
+
chainCode,
|
|
541
|
+
coinType,
|
|
542
|
+
network
|
|
543
|
+
]));
|
|
544
|
+
}
|
|
545
|
+
function hdKeyDigest(key) {
|
|
546
|
+
return sha256(hdKeyDigestSource(key));
|
|
547
|
+
}
|
|
548
|
+
//#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 };
|
|
@@ -4,9 +4,9 @@ type UrErrorCode = "InvalidWord" | "InvalidBytewordsChecksum" | "InvalidByteword
|
|
|
4
4
|
/** Structured error thrown by the UR stack. */
|
|
5
5
|
declare class UrError extends Error {
|
|
6
6
|
readonly code: UrErrorCode;
|
|
7
|
-
readonly expected?: string;
|
|
8
|
-
readonly found?: string;
|
|
9
|
-
readonly limit?: string;
|
|
7
|
+
readonly expected?: string | undefined;
|
|
8
|
+
readonly found?: string | undefined;
|
|
9
|
+
readonly limit?: string | undefined;
|
|
10
10
|
constructor(code: UrErrorCode, options?: {
|
|
11
11
|
expected?: string;
|
|
12
12
|
found?: string;
|
|
@@ -26,15 +26,19 @@ declare function failPoison(p: DecoderPoison): never;
|
|
|
26
26
|
//#endregion
|
|
27
27
|
//#region src/fountain/limits.d.ts
|
|
28
28
|
/** Hard limits for adversarial multi-part streams. */
|
|
29
|
-
|
|
29
|
+
type DecoderLimits = {
|
|
30
30
|
maxMessageLength: number;
|
|
31
31
|
maxFragmentCount: number;
|
|
32
32
|
maxFragmentDataLength: number;
|
|
33
33
|
maxBufferParts: number;
|
|
34
34
|
maxReceivedParts: number;
|
|
35
35
|
maxUriLen: number;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Production default; same integers as bcur 1.0.
|
|
39
|
+
*
|
|
40
|
+
* Hosts that need another budget pass `new Decoder({ limits })`.
|
|
41
|
+
*/
|
|
38
42
|
declare const DEFAULT_LIMITS: DecoderLimits;
|
|
39
43
|
//#endregion
|
|
40
44
|
//#region src/ur/type.d.ts
|