@qntx/ur 1.8.0 → 2.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/LICENSE-APACHE +201 -0
- package/README.md +7 -6
- package/dist/index-CcMfgay6.d.mts +54 -0
- package/dist/index-DTjNCWBI.d.mts +228 -0
- package/dist/index.d.mts +44 -163
- package/dist/index.mjs +2 -2
- package/dist/registry.d.mts +92 -33
- package/dist/registry.mjs +443 -93
- package/dist/typed-D-SyraAj.mjs +124 -0
- package/dist/typed.d.mts +3 -3
- package/dist/typed.mjs +3 -3
- package/dist/ur-7Ge3roVh.mjs +1993 -0
- package/package.json +22 -15
- package/dist/index-BeG2XIr5.d.mts +0 -59
- package/dist/rolldown-runtime-D7D4PA-g.mjs +0 -13
- package/dist/type-CZ0CIHJk.d.mts +0 -54
- package/dist/typed-BmHx2GWT.mjs +0 -150
- package/dist/ur-BUUcXyia.mjs +0 -1577
- /package/{LICENSE → LICENSE-MIT} +0 -0
|
@@ -0,0 +1,1993 @@
|
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
2
|
+
//#region src/error.ts
|
|
3
|
+
const MESSAGES = {
|
|
4
|
+
NonAscii: "bytewords string is not ASCII",
|
|
5
|
+
InvalidWord: "invalid bytewords word",
|
|
6
|
+
InvalidBytewordsLength: "invalid bytewords length",
|
|
7
|
+
InvalidBytewordsChecksum: "invalid bytewords checksum",
|
|
8
|
+
InvalidScheme: "invalid UR scheme",
|
|
9
|
+
TypeUnspecified: "UR type unspecified",
|
|
10
|
+
InvalidType: "invalid UR type",
|
|
11
|
+
InvalidIndices: "invalid multi-part indices",
|
|
12
|
+
UnexpectedType: "unexpected UR type",
|
|
13
|
+
InvalidPartCbor: "invalid fountain part CBOR",
|
|
14
|
+
InvalidPart: "invalid fountain part",
|
|
15
|
+
InconsistentPart: "fountain part inconsistent with previous parts",
|
|
16
|
+
ResourceLimit: "resource limit exceeded",
|
|
17
|
+
InvalidPadding: "invalid fountain part padding",
|
|
18
|
+
InvalidMessageChecksum: "invalid fountain message checksum",
|
|
19
|
+
EmptyMessage: "empty message",
|
|
20
|
+
InvalidFragmentLength: "invalid fragment length",
|
|
21
|
+
MessageTooLong: "message too long",
|
|
22
|
+
NotSinglePart: "expected single-part UR",
|
|
23
|
+
CborDecode: "dCBOR decode failed",
|
|
24
|
+
CborType: "dCBOR type mismatch",
|
|
25
|
+
Internal: "internal error"
|
|
26
|
+
};
|
|
27
|
+
const FATAL_CODES = /* @__PURE__ */ new Set([
|
|
28
|
+
"ResourceLimit",
|
|
29
|
+
"InvalidPadding",
|
|
30
|
+
"InvalidMessageChecksum",
|
|
31
|
+
"Internal"
|
|
32
|
+
]);
|
|
33
|
+
function describe(info) {
|
|
34
|
+
if (info.code === "ResourceLimit") return `${MESSAGES.ResourceLimit}: ${info.limit}`;
|
|
35
|
+
if (info.code === "UnexpectedType") {
|
|
36
|
+
const names = info.expected.join(", ");
|
|
37
|
+
return `${MESSAGES.UnexpectedType}: expected ${names}, found ${info.found}`;
|
|
38
|
+
}
|
|
39
|
+
return MESSAGES[info.code];
|
|
40
|
+
}
|
|
41
|
+
/** Structured error thrown by the UR stack. */
|
|
42
|
+
var UrError = class extends Error {
|
|
43
|
+
info;
|
|
44
|
+
constructor(info, options) {
|
|
45
|
+
super(describe(info), options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
46
|
+
this.name = "UrError";
|
|
47
|
+
this.info = info;
|
|
48
|
+
}
|
|
49
|
+
get code() {
|
|
50
|
+
return this.info.code;
|
|
51
|
+
}
|
|
52
|
+
get fatal() {
|
|
53
|
+
return FATAL_CODES.has(this.info.code);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
/** Throws a {@link UrError}; a bare code builds `{ code }` info. */
|
|
57
|
+
function fail(arg, options) {
|
|
58
|
+
throw new UrError(typeof arg === "string" ? { code: arg } : arg, options);
|
|
59
|
+
}
|
|
60
|
+
/** Converts a thrown value to {@link UrError}; non-`UrError` values become `Internal` with `cause`. */
|
|
61
|
+
function toUrError(error) {
|
|
62
|
+
return error instanceof UrError ? error : new UrError({ code: "Internal" }, { cause: error });
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/consensus/crc32.ts
|
|
66
|
+
/** CRC-32 ISO-HDLC (polynomial 0xedb88320), matching bcur / ur-rs. */
|
|
67
|
+
const TABLE = (() => {
|
|
68
|
+
const table = /* @__PURE__ */ new Uint32Array(256);
|
|
69
|
+
for (let i = 0; i < 256; i++) {
|
|
70
|
+
let c = i;
|
|
71
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
72
|
+
table[i] = c >>> 0;
|
|
73
|
+
}
|
|
74
|
+
return table;
|
|
75
|
+
})();
|
|
76
|
+
/** CRC-32 ISO-HDLC checksum of `data`. */
|
|
77
|
+
function checksum(data) {
|
|
78
|
+
let crc = 4294967295;
|
|
79
|
+
for (const b of data) crc = (TABLE[(crc ^ b) & 255] ?? fail("Internal")) ^ crc >>> 8;
|
|
80
|
+
return (crc ^ 4294967295) >>> 0;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/ascii.ts
|
|
84
|
+
/**
|
|
85
|
+
* ASCII-only character helpers. `String.prototype.toLowerCase`/`toUpperCase` are Unicode-aware
|
|
86
|
+
* (e.g. `"\u212A"` folds to `"k"`); the UR wire format folds ASCII letters only, like Rust
|
|
87
|
+
* `to_ascii_lowercase`/`to_ascii_uppercase`.
|
|
88
|
+
*/
|
|
89
|
+
/** True iff every code point of `text` is `<= 0x7F`. */
|
|
90
|
+
function isAscii(text) {
|
|
91
|
+
for (const ch of text) if ((ch.codePointAt(0) ?? 0) > 127) return false;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/** Lowercase ASCII `A-Z` only; every other code unit is left untouched. */
|
|
95
|
+
function asciiLower(text) {
|
|
96
|
+
return text.replaceAll(/[A-Z]/g, (ch) => String.fromCodePoint((ch.codePointAt(0) ?? 0) + 32));
|
|
97
|
+
}
|
|
98
|
+
/** Uppercase ASCII `a-z` only; every other code unit is left untouched. */
|
|
99
|
+
function asciiUpper(text) {
|
|
100
|
+
return text.replaceAll(/[a-z]/g, (ch) => String.fromCodePoint((ch.codePointAt(0) ?? 0) - 32));
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/constants.ts
|
|
104
|
+
/** BCR-2020-012 bytewords tables. */
|
|
105
|
+
/** Four-letter bytewords (index = byte value). */
|
|
106
|
+
const WORDS = [
|
|
107
|
+
"able",
|
|
108
|
+
"acid",
|
|
109
|
+
"also",
|
|
110
|
+
"apex",
|
|
111
|
+
"aqua",
|
|
112
|
+
"arch",
|
|
113
|
+
"atom",
|
|
114
|
+
"aunt",
|
|
115
|
+
"away",
|
|
116
|
+
"axis",
|
|
117
|
+
"back",
|
|
118
|
+
"bald",
|
|
119
|
+
"barn",
|
|
120
|
+
"belt",
|
|
121
|
+
"beta",
|
|
122
|
+
"bias",
|
|
123
|
+
"blue",
|
|
124
|
+
"body",
|
|
125
|
+
"brag",
|
|
126
|
+
"brew",
|
|
127
|
+
"bulb",
|
|
128
|
+
"buzz",
|
|
129
|
+
"calm",
|
|
130
|
+
"cash",
|
|
131
|
+
"cats",
|
|
132
|
+
"chef",
|
|
133
|
+
"city",
|
|
134
|
+
"claw",
|
|
135
|
+
"code",
|
|
136
|
+
"cola",
|
|
137
|
+
"cook",
|
|
138
|
+
"cost",
|
|
139
|
+
"crux",
|
|
140
|
+
"curl",
|
|
141
|
+
"cusp",
|
|
142
|
+
"cyan",
|
|
143
|
+
"dark",
|
|
144
|
+
"data",
|
|
145
|
+
"days",
|
|
146
|
+
"deli",
|
|
147
|
+
"dice",
|
|
148
|
+
"diet",
|
|
149
|
+
"door",
|
|
150
|
+
"down",
|
|
151
|
+
"draw",
|
|
152
|
+
"drop",
|
|
153
|
+
"drum",
|
|
154
|
+
"dull",
|
|
155
|
+
"duty",
|
|
156
|
+
"each",
|
|
157
|
+
"easy",
|
|
158
|
+
"echo",
|
|
159
|
+
"edge",
|
|
160
|
+
"epic",
|
|
161
|
+
"even",
|
|
162
|
+
"exam",
|
|
163
|
+
"exit",
|
|
164
|
+
"eyes",
|
|
165
|
+
"fact",
|
|
166
|
+
"fair",
|
|
167
|
+
"fern",
|
|
168
|
+
"figs",
|
|
169
|
+
"film",
|
|
170
|
+
"fish",
|
|
171
|
+
"fizz",
|
|
172
|
+
"flap",
|
|
173
|
+
"flew",
|
|
174
|
+
"flux",
|
|
175
|
+
"foxy",
|
|
176
|
+
"free",
|
|
177
|
+
"frog",
|
|
178
|
+
"fuel",
|
|
179
|
+
"fund",
|
|
180
|
+
"gala",
|
|
181
|
+
"game",
|
|
182
|
+
"gear",
|
|
183
|
+
"gems",
|
|
184
|
+
"gift",
|
|
185
|
+
"girl",
|
|
186
|
+
"glow",
|
|
187
|
+
"good",
|
|
188
|
+
"gray",
|
|
189
|
+
"grim",
|
|
190
|
+
"guru",
|
|
191
|
+
"gush",
|
|
192
|
+
"gyro",
|
|
193
|
+
"half",
|
|
194
|
+
"hang",
|
|
195
|
+
"hard",
|
|
196
|
+
"hawk",
|
|
197
|
+
"heat",
|
|
198
|
+
"help",
|
|
199
|
+
"high",
|
|
200
|
+
"hill",
|
|
201
|
+
"holy",
|
|
202
|
+
"hope",
|
|
203
|
+
"horn",
|
|
204
|
+
"huts",
|
|
205
|
+
"iced",
|
|
206
|
+
"idea",
|
|
207
|
+
"idle",
|
|
208
|
+
"inch",
|
|
209
|
+
"inky",
|
|
210
|
+
"into",
|
|
211
|
+
"iris",
|
|
212
|
+
"iron",
|
|
213
|
+
"item",
|
|
214
|
+
"jade",
|
|
215
|
+
"jazz",
|
|
216
|
+
"join",
|
|
217
|
+
"jolt",
|
|
218
|
+
"jowl",
|
|
219
|
+
"judo",
|
|
220
|
+
"jugs",
|
|
221
|
+
"jump",
|
|
222
|
+
"junk",
|
|
223
|
+
"jury",
|
|
224
|
+
"keep",
|
|
225
|
+
"keno",
|
|
226
|
+
"kept",
|
|
227
|
+
"keys",
|
|
228
|
+
"kick",
|
|
229
|
+
"kiln",
|
|
230
|
+
"king",
|
|
231
|
+
"kite",
|
|
232
|
+
"kiwi",
|
|
233
|
+
"knob",
|
|
234
|
+
"lamb",
|
|
235
|
+
"lava",
|
|
236
|
+
"lazy",
|
|
237
|
+
"leaf",
|
|
238
|
+
"legs",
|
|
239
|
+
"liar",
|
|
240
|
+
"limp",
|
|
241
|
+
"lion",
|
|
242
|
+
"list",
|
|
243
|
+
"logo",
|
|
244
|
+
"loud",
|
|
245
|
+
"love",
|
|
246
|
+
"luau",
|
|
247
|
+
"luck",
|
|
248
|
+
"lung",
|
|
249
|
+
"main",
|
|
250
|
+
"many",
|
|
251
|
+
"math",
|
|
252
|
+
"maze",
|
|
253
|
+
"memo",
|
|
254
|
+
"menu",
|
|
255
|
+
"meow",
|
|
256
|
+
"mild",
|
|
257
|
+
"mint",
|
|
258
|
+
"miss",
|
|
259
|
+
"monk",
|
|
260
|
+
"nail",
|
|
261
|
+
"navy",
|
|
262
|
+
"need",
|
|
263
|
+
"news",
|
|
264
|
+
"next",
|
|
265
|
+
"noon",
|
|
266
|
+
"note",
|
|
267
|
+
"numb",
|
|
268
|
+
"obey",
|
|
269
|
+
"oboe",
|
|
270
|
+
"omit",
|
|
271
|
+
"onyx",
|
|
272
|
+
"open",
|
|
273
|
+
"oval",
|
|
274
|
+
"owls",
|
|
275
|
+
"paid",
|
|
276
|
+
"part",
|
|
277
|
+
"peck",
|
|
278
|
+
"play",
|
|
279
|
+
"plus",
|
|
280
|
+
"poem",
|
|
281
|
+
"pool",
|
|
282
|
+
"pose",
|
|
283
|
+
"puff",
|
|
284
|
+
"puma",
|
|
285
|
+
"purr",
|
|
286
|
+
"quad",
|
|
287
|
+
"quiz",
|
|
288
|
+
"race",
|
|
289
|
+
"ramp",
|
|
290
|
+
"real",
|
|
291
|
+
"redo",
|
|
292
|
+
"rich",
|
|
293
|
+
"road",
|
|
294
|
+
"rock",
|
|
295
|
+
"roof",
|
|
296
|
+
"ruby",
|
|
297
|
+
"ruin",
|
|
298
|
+
"runs",
|
|
299
|
+
"rust",
|
|
300
|
+
"safe",
|
|
301
|
+
"saga",
|
|
302
|
+
"scar",
|
|
303
|
+
"sets",
|
|
304
|
+
"silk",
|
|
305
|
+
"skew",
|
|
306
|
+
"slot",
|
|
307
|
+
"soap",
|
|
308
|
+
"solo",
|
|
309
|
+
"song",
|
|
310
|
+
"stub",
|
|
311
|
+
"surf",
|
|
312
|
+
"swan",
|
|
313
|
+
"taco",
|
|
314
|
+
"task",
|
|
315
|
+
"taxi",
|
|
316
|
+
"tent",
|
|
317
|
+
"tied",
|
|
318
|
+
"time",
|
|
319
|
+
"tiny",
|
|
320
|
+
"toil",
|
|
321
|
+
"tomb",
|
|
322
|
+
"toys",
|
|
323
|
+
"trip",
|
|
324
|
+
"tuna",
|
|
325
|
+
"twin",
|
|
326
|
+
"ugly",
|
|
327
|
+
"undo",
|
|
328
|
+
"unit",
|
|
329
|
+
"urge",
|
|
330
|
+
"user",
|
|
331
|
+
"vast",
|
|
332
|
+
"very",
|
|
333
|
+
"veto",
|
|
334
|
+
"vial",
|
|
335
|
+
"vibe",
|
|
336
|
+
"view",
|
|
337
|
+
"visa",
|
|
338
|
+
"void",
|
|
339
|
+
"vows",
|
|
340
|
+
"wall",
|
|
341
|
+
"wand",
|
|
342
|
+
"warm",
|
|
343
|
+
"wasp",
|
|
344
|
+
"wave",
|
|
345
|
+
"waxy",
|
|
346
|
+
"webs",
|
|
347
|
+
"what",
|
|
348
|
+
"when",
|
|
349
|
+
"whiz",
|
|
350
|
+
"wolf",
|
|
351
|
+
"work",
|
|
352
|
+
"yank",
|
|
353
|
+
"yawn",
|
|
354
|
+
"yell",
|
|
355
|
+
"yoga",
|
|
356
|
+
"yurt",
|
|
357
|
+
"zaps",
|
|
358
|
+
"zero",
|
|
359
|
+
"zest",
|
|
360
|
+
"zinc",
|
|
361
|
+
"zone",
|
|
362
|
+
"zoom"
|
|
363
|
+
];
|
|
364
|
+
/** First+last letter of each byteword (UR minimal style). */
|
|
365
|
+
const MINIMALS = [
|
|
366
|
+
"ae",
|
|
367
|
+
"ad",
|
|
368
|
+
"ao",
|
|
369
|
+
"ax",
|
|
370
|
+
"aa",
|
|
371
|
+
"ah",
|
|
372
|
+
"am",
|
|
373
|
+
"at",
|
|
374
|
+
"ay",
|
|
375
|
+
"as",
|
|
376
|
+
"bk",
|
|
377
|
+
"bd",
|
|
378
|
+
"bn",
|
|
379
|
+
"bt",
|
|
380
|
+
"ba",
|
|
381
|
+
"bs",
|
|
382
|
+
"be",
|
|
383
|
+
"by",
|
|
384
|
+
"bg",
|
|
385
|
+
"bw",
|
|
386
|
+
"bb",
|
|
387
|
+
"bz",
|
|
388
|
+
"cm",
|
|
389
|
+
"ch",
|
|
390
|
+
"cs",
|
|
391
|
+
"cf",
|
|
392
|
+
"cy",
|
|
393
|
+
"cw",
|
|
394
|
+
"ce",
|
|
395
|
+
"ca",
|
|
396
|
+
"ck",
|
|
397
|
+
"ct",
|
|
398
|
+
"cx",
|
|
399
|
+
"cl",
|
|
400
|
+
"cp",
|
|
401
|
+
"cn",
|
|
402
|
+
"dk",
|
|
403
|
+
"da",
|
|
404
|
+
"ds",
|
|
405
|
+
"di",
|
|
406
|
+
"de",
|
|
407
|
+
"dt",
|
|
408
|
+
"dr",
|
|
409
|
+
"dn",
|
|
410
|
+
"dw",
|
|
411
|
+
"dp",
|
|
412
|
+
"dm",
|
|
413
|
+
"dl",
|
|
414
|
+
"dy",
|
|
415
|
+
"eh",
|
|
416
|
+
"ey",
|
|
417
|
+
"eo",
|
|
418
|
+
"ee",
|
|
419
|
+
"ec",
|
|
420
|
+
"en",
|
|
421
|
+
"em",
|
|
422
|
+
"et",
|
|
423
|
+
"es",
|
|
424
|
+
"ft",
|
|
425
|
+
"fr",
|
|
426
|
+
"fn",
|
|
427
|
+
"fs",
|
|
428
|
+
"fm",
|
|
429
|
+
"fh",
|
|
430
|
+
"fz",
|
|
431
|
+
"fp",
|
|
432
|
+
"fw",
|
|
433
|
+
"fx",
|
|
434
|
+
"fy",
|
|
435
|
+
"fe",
|
|
436
|
+
"fg",
|
|
437
|
+
"fl",
|
|
438
|
+
"fd",
|
|
439
|
+
"ga",
|
|
440
|
+
"ge",
|
|
441
|
+
"gr",
|
|
442
|
+
"gs",
|
|
443
|
+
"gt",
|
|
444
|
+
"gl",
|
|
445
|
+
"gw",
|
|
446
|
+
"gd",
|
|
447
|
+
"gy",
|
|
448
|
+
"gm",
|
|
449
|
+
"gu",
|
|
450
|
+
"gh",
|
|
451
|
+
"go",
|
|
452
|
+
"hf",
|
|
453
|
+
"hg",
|
|
454
|
+
"hd",
|
|
455
|
+
"hk",
|
|
456
|
+
"ht",
|
|
457
|
+
"hp",
|
|
458
|
+
"hh",
|
|
459
|
+
"hl",
|
|
460
|
+
"hy",
|
|
461
|
+
"he",
|
|
462
|
+
"hn",
|
|
463
|
+
"hs",
|
|
464
|
+
"id",
|
|
465
|
+
"ia",
|
|
466
|
+
"ie",
|
|
467
|
+
"ih",
|
|
468
|
+
"iy",
|
|
469
|
+
"io",
|
|
470
|
+
"is",
|
|
471
|
+
"in",
|
|
472
|
+
"im",
|
|
473
|
+
"je",
|
|
474
|
+
"jz",
|
|
475
|
+
"jn",
|
|
476
|
+
"jt",
|
|
477
|
+
"jl",
|
|
478
|
+
"jo",
|
|
479
|
+
"js",
|
|
480
|
+
"jp",
|
|
481
|
+
"jk",
|
|
482
|
+
"jy",
|
|
483
|
+
"kp",
|
|
484
|
+
"ko",
|
|
485
|
+
"kt",
|
|
486
|
+
"ks",
|
|
487
|
+
"kk",
|
|
488
|
+
"kn",
|
|
489
|
+
"kg",
|
|
490
|
+
"ke",
|
|
491
|
+
"ki",
|
|
492
|
+
"kb",
|
|
493
|
+
"lb",
|
|
494
|
+
"la",
|
|
495
|
+
"ly",
|
|
496
|
+
"lf",
|
|
497
|
+
"ls",
|
|
498
|
+
"lr",
|
|
499
|
+
"lp",
|
|
500
|
+
"ln",
|
|
501
|
+
"lt",
|
|
502
|
+
"lo",
|
|
503
|
+
"ld",
|
|
504
|
+
"le",
|
|
505
|
+
"lu",
|
|
506
|
+
"lk",
|
|
507
|
+
"lg",
|
|
508
|
+
"mn",
|
|
509
|
+
"my",
|
|
510
|
+
"mh",
|
|
511
|
+
"me",
|
|
512
|
+
"mo",
|
|
513
|
+
"mu",
|
|
514
|
+
"mw",
|
|
515
|
+
"md",
|
|
516
|
+
"mt",
|
|
517
|
+
"ms",
|
|
518
|
+
"mk",
|
|
519
|
+
"nl",
|
|
520
|
+
"ny",
|
|
521
|
+
"nd",
|
|
522
|
+
"ns",
|
|
523
|
+
"nt",
|
|
524
|
+
"nn",
|
|
525
|
+
"ne",
|
|
526
|
+
"nb",
|
|
527
|
+
"oy",
|
|
528
|
+
"oe",
|
|
529
|
+
"ot",
|
|
530
|
+
"ox",
|
|
531
|
+
"on",
|
|
532
|
+
"ol",
|
|
533
|
+
"os",
|
|
534
|
+
"pd",
|
|
535
|
+
"pt",
|
|
536
|
+
"pk",
|
|
537
|
+
"py",
|
|
538
|
+
"ps",
|
|
539
|
+
"pm",
|
|
540
|
+
"pl",
|
|
541
|
+
"pe",
|
|
542
|
+
"pf",
|
|
543
|
+
"pa",
|
|
544
|
+
"pr",
|
|
545
|
+
"qd",
|
|
546
|
+
"qz",
|
|
547
|
+
"re",
|
|
548
|
+
"rp",
|
|
549
|
+
"rl",
|
|
550
|
+
"ro",
|
|
551
|
+
"rh",
|
|
552
|
+
"rd",
|
|
553
|
+
"rk",
|
|
554
|
+
"rf",
|
|
555
|
+
"ry",
|
|
556
|
+
"rn",
|
|
557
|
+
"rs",
|
|
558
|
+
"rt",
|
|
559
|
+
"se",
|
|
560
|
+
"sa",
|
|
561
|
+
"sr",
|
|
562
|
+
"ss",
|
|
563
|
+
"sk",
|
|
564
|
+
"sw",
|
|
565
|
+
"st",
|
|
566
|
+
"sp",
|
|
567
|
+
"so",
|
|
568
|
+
"sg",
|
|
569
|
+
"sb",
|
|
570
|
+
"sf",
|
|
571
|
+
"sn",
|
|
572
|
+
"to",
|
|
573
|
+
"tk",
|
|
574
|
+
"ti",
|
|
575
|
+
"tt",
|
|
576
|
+
"td",
|
|
577
|
+
"te",
|
|
578
|
+
"ty",
|
|
579
|
+
"tl",
|
|
580
|
+
"tb",
|
|
581
|
+
"ts",
|
|
582
|
+
"tp",
|
|
583
|
+
"ta",
|
|
584
|
+
"tn",
|
|
585
|
+
"uy",
|
|
586
|
+
"uo",
|
|
587
|
+
"ut",
|
|
588
|
+
"ue",
|
|
589
|
+
"ur",
|
|
590
|
+
"vt",
|
|
591
|
+
"vy",
|
|
592
|
+
"vo",
|
|
593
|
+
"vl",
|
|
594
|
+
"ve",
|
|
595
|
+
"vw",
|
|
596
|
+
"va",
|
|
597
|
+
"vd",
|
|
598
|
+
"vs",
|
|
599
|
+
"wl",
|
|
600
|
+
"wd",
|
|
601
|
+
"wm",
|
|
602
|
+
"wp",
|
|
603
|
+
"we",
|
|
604
|
+
"wy",
|
|
605
|
+
"ws",
|
|
606
|
+
"wt",
|
|
607
|
+
"wn",
|
|
608
|
+
"wz",
|
|
609
|
+
"wf",
|
|
610
|
+
"wk",
|
|
611
|
+
"yk",
|
|
612
|
+
"yn",
|
|
613
|
+
"yl",
|
|
614
|
+
"ya",
|
|
615
|
+
"yt",
|
|
616
|
+
"zs",
|
|
617
|
+
"zo",
|
|
618
|
+
"zt",
|
|
619
|
+
"zc",
|
|
620
|
+
"ze",
|
|
621
|
+
"zm"
|
|
622
|
+
];
|
|
623
|
+
/** BCR-2024-008 bytemoji table (index = byte value). */
|
|
624
|
+
const BYTEMOJIS = [
|
|
625
|
+
"😀",
|
|
626
|
+
"😂",
|
|
627
|
+
"😆",
|
|
628
|
+
"😉",
|
|
629
|
+
"🙄",
|
|
630
|
+
"😋",
|
|
631
|
+
"😎",
|
|
632
|
+
"😍",
|
|
633
|
+
"😘",
|
|
634
|
+
"😭",
|
|
635
|
+
"🫠",
|
|
636
|
+
"🥱",
|
|
637
|
+
"🤩",
|
|
638
|
+
"😶",
|
|
639
|
+
"🤨",
|
|
640
|
+
"🫥",
|
|
641
|
+
"🥵",
|
|
642
|
+
"🥶",
|
|
643
|
+
"😳",
|
|
644
|
+
"🤪",
|
|
645
|
+
"😵",
|
|
646
|
+
"😡",
|
|
647
|
+
"🤢",
|
|
648
|
+
"😇",
|
|
649
|
+
"🤠",
|
|
650
|
+
"🤡",
|
|
651
|
+
"🥳",
|
|
652
|
+
"🥺",
|
|
653
|
+
"😬",
|
|
654
|
+
"🤑",
|
|
655
|
+
"🙃",
|
|
656
|
+
"🤯",
|
|
657
|
+
"😈",
|
|
658
|
+
"👹",
|
|
659
|
+
"👺",
|
|
660
|
+
"💀",
|
|
661
|
+
"👻",
|
|
662
|
+
"👽",
|
|
663
|
+
"😺",
|
|
664
|
+
"😹",
|
|
665
|
+
"😻",
|
|
666
|
+
"😽",
|
|
667
|
+
"🙀",
|
|
668
|
+
"😿",
|
|
669
|
+
"🫶",
|
|
670
|
+
"🤲",
|
|
671
|
+
"🙌",
|
|
672
|
+
"🤝",
|
|
673
|
+
"👍",
|
|
674
|
+
"👎",
|
|
675
|
+
"👈",
|
|
676
|
+
"👆",
|
|
677
|
+
"💪",
|
|
678
|
+
"👄",
|
|
679
|
+
"🦷",
|
|
680
|
+
"👂",
|
|
681
|
+
"👃",
|
|
682
|
+
"🧠",
|
|
683
|
+
"👀",
|
|
684
|
+
"🤚",
|
|
685
|
+
"🦶",
|
|
686
|
+
"🍎",
|
|
687
|
+
"🍊",
|
|
688
|
+
"🍋",
|
|
689
|
+
"🍌",
|
|
690
|
+
"🍉",
|
|
691
|
+
"🍇",
|
|
692
|
+
"🍓",
|
|
693
|
+
"🫐",
|
|
694
|
+
"🍒",
|
|
695
|
+
"🍑",
|
|
696
|
+
"🍍",
|
|
697
|
+
"🥝",
|
|
698
|
+
"🍆",
|
|
699
|
+
"🥑",
|
|
700
|
+
"🥦",
|
|
701
|
+
"🍅",
|
|
702
|
+
"🌽",
|
|
703
|
+
"🥕",
|
|
704
|
+
"🫒",
|
|
705
|
+
"🧄",
|
|
706
|
+
"🥐",
|
|
707
|
+
"🥯",
|
|
708
|
+
"🍞",
|
|
709
|
+
"🧀",
|
|
710
|
+
"🥚",
|
|
711
|
+
"🍗",
|
|
712
|
+
"🌭",
|
|
713
|
+
"🍔",
|
|
714
|
+
"🍟",
|
|
715
|
+
"🍕",
|
|
716
|
+
"🌮",
|
|
717
|
+
"🥙",
|
|
718
|
+
"🍱",
|
|
719
|
+
"🍜",
|
|
720
|
+
"🍤",
|
|
721
|
+
"🍚",
|
|
722
|
+
"🥠",
|
|
723
|
+
"🍨",
|
|
724
|
+
"🍦",
|
|
725
|
+
"🎂",
|
|
726
|
+
"🪴",
|
|
727
|
+
"🌵",
|
|
728
|
+
"🌱",
|
|
729
|
+
"💐",
|
|
730
|
+
"🍁",
|
|
731
|
+
"🍄",
|
|
732
|
+
"🌹",
|
|
733
|
+
"🌺",
|
|
734
|
+
"🌼",
|
|
735
|
+
"🌻",
|
|
736
|
+
"🌸",
|
|
737
|
+
"💨",
|
|
738
|
+
"🌊",
|
|
739
|
+
"💧",
|
|
740
|
+
"💦",
|
|
741
|
+
"🌀",
|
|
742
|
+
"🌈",
|
|
743
|
+
"🌞",
|
|
744
|
+
"🌝",
|
|
745
|
+
"🌛",
|
|
746
|
+
"🌜",
|
|
747
|
+
"🌙",
|
|
748
|
+
"🌎",
|
|
749
|
+
"💫",
|
|
750
|
+
"⭐",
|
|
751
|
+
"🪐",
|
|
752
|
+
"🌐",
|
|
753
|
+
"💛",
|
|
754
|
+
"💔",
|
|
755
|
+
"💘",
|
|
756
|
+
"💖",
|
|
757
|
+
"💕",
|
|
758
|
+
"🏁",
|
|
759
|
+
"🚩",
|
|
760
|
+
"💬",
|
|
761
|
+
"💯",
|
|
762
|
+
"🚫",
|
|
763
|
+
"🔴",
|
|
764
|
+
"🔷",
|
|
765
|
+
"🟩",
|
|
766
|
+
"🛑",
|
|
767
|
+
"🔺",
|
|
768
|
+
"🚗",
|
|
769
|
+
"🚑",
|
|
770
|
+
"🚒",
|
|
771
|
+
"🚜",
|
|
772
|
+
"🛵",
|
|
773
|
+
"🚨",
|
|
774
|
+
"🚀",
|
|
775
|
+
"🚁",
|
|
776
|
+
"🛟",
|
|
777
|
+
"🚦",
|
|
778
|
+
"🏰",
|
|
779
|
+
"🎡",
|
|
780
|
+
"🎢",
|
|
781
|
+
"🎠",
|
|
782
|
+
"🏠",
|
|
783
|
+
"🔔",
|
|
784
|
+
"🔑",
|
|
785
|
+
"🚪",
|
|
786
|
+
"🪑",
|
|
787
|
+
"🎈",
|
|
788
|
+
"💌",
|
|
789
|
+
"📦",
|
|
790
|
+
"📫",
|
|
791
|
+
"📖",
|
|
792
|
+
"📚",
|
|
793
|
+
"📌",
|
|
794
|
+
"🧮",
|
|
795
|
+
"🔒",
|
|
796
|
+
"💎",
|
|
797
|
+
"📷",
|
|
798
|
+
"⏰",
|
|
799
|
+
"⏳",
|
|
800
|
+
"📡",
|
|
801
|
+
"💡",
|
|
802
|
+
"💰",
|
|
803
|
+
"🧲",
|
|
804
|
+
"🧸",
|
|
805
|
+
"🎁",
|
|
806
|
+
"🎀",
|
|
807
|
+
"🎉",
|
|
808
|
+
"🪭",
|
|
809
|
+
"👑",
|
|
810
|
+
"🫖",
|
|
811
|
+
"🔭",
|
|
812
|
+
"🛁",
|
|
813
|
+
"🏆",
|
|
814
|
+
"🥁",
|
|
815
|
+
"🎷",
|
|
816
|
+
"🎺",
|
|
817
|
+
"🏀",
|
|
818
|
+
"🏈",
|
|
819
|
+
"🎾",
|
|
820
|
+
"🏓",
|
|
821
|
+
"✨",
|
|
822
|
+
"🔥",
|
|
823
|
+
"💥",
|
|
824
|
+
"👕",
|
|
825
|
+
"👚",
|
|
826
|
+
"👖",
|
|
827
|
+
"🩳",
|
|
828
|
+
"👗",
|
|
829
|
+
"👔",
|
|
830
|
+
"🧢",
|
|
831
|
+
"👓",
|
|
832
|
+
"🧶",
|
|
833
|
+
"🧵",
|
|
834
|
+
"💍",
|
|
835
|
+
"👠",
|
|
836
|
+
"👟",
|
|
837
|
+
"🧦",
|
|
838
|
+
"🧤",
|
|
839
|
+
"👒",
|
|
840
|
+
"👜",
|
|
841
|
+
"🐱",
|
|
842
|
+
"🐶",
|
|
843
|
+
"🐭",
|
|
844
|
+
"🐹",
|
|
845
|
+
"🐰",
|
|
846
|
+
"🦊",
|
|
847
|
+
"🐻",
|
|
848
|
+
"🐼",
|
|
849
|
+
"🐨",
|
|
850
|
+
"🐯",
|
|
851
|
+
"🦁",
|
|
852
|
+
"🐮",
|
|
853
|
+
"🐷",
|
|
854
|
+
"🐸",
|
|
855
|
+
"🐵",
|
|
856
|
+
"🐔",
|
|
857
|
+
"🐥",
|
|
858
|
+
"🦆",
|
|
859
|
+
"🦉",
|
|
860
|
+
"🐴",
|
|
861
|
+
"🦄",
|
|
862
|
+
"🐝",
|
|
863
|
+
"🐛",
|
|
864
|
+
"🦋",
|
|
865
|
+
"🐌",
|
|
866
|
+
"🐞",
|
|
867
|
+
"🐢",
|
|
868
|
+
"🐺",
|
|
869
|
+
"🐍",
|
|
870
|
+
"🪽",
|
|
871
|
+
"🐙",
|
|
872
|
+
"🦑",
|
|
873
|
+
"🪼",
|
|
874
|
+
"🦞",
|
|
875
|
+
"🦀",
|
|
876
|
+
"🐚",
|
|
877
|
+
"🦭",
|
|
878
|
+
"🐟",
|
|
879
|
+
"🐬",
|
|
880
|
+
"🐳"
|
|
881
|
+
];
|
|
882
|
+
//#endregion
|
|
883
|
+
//#region src/bytewords/index.ts
|
|
884
|
+
const wordByToken = /* @__PURE__ */ new Map();
|
|
885
|
+
for (const [i, word] of WORDS.entries()) wordByToken.set(word, i);
|
|
886
|
+
for (const [i, minimal] of MINIMALS.entries()) wordByToken.set(minimal, i);
|
|
887
|
+
function wordAt(table, byte) {
|
|
888
|
+
const word = table[byte];
|
|
889
|
+
if (word === void 0) throw new Error("byteword index out of range");
|
|
890
|
+
return word;
|
|
891
|
+
}
|
|
892
|
+
function checksumBytes(data) {
|
|
893
|
+
const crc = checksum(data);
|
|
894
|
+
return new Uint8Array([
|
|
895
|
+
crc >>> 24 & 255,
|
|
896
|
+
crc >>> 16 & 255,
|
|
897
|
+
crc >>> 8 & 255,
|
|
898
|
+
crc & 255
|
|
899
|
+
]);
|
|
900
|
+
}
|
|
901
|
+
function encodeWords(data, style) {
|
|
902
|
+
const parts = [];
|
|
903
|
+
const table = style === "minimal" ? MINIMALS : WORDS;
|
|
904
|
+
for (const b of data) parts.push(wordAt(table, b));
|
|
905
|
+
return parts.join({
|
|
906
|
+
standard: " ",
|
|
907
|
+
uri: "-",
|
|
908
|
+
minimal: ""
|
|
909
|
+
}[style]);
|
|
910
|
+
}
|
|
911
|
+
/** Encode `data` with trailing CRC-32 as bytewords. Empty data is allowed. */
|
|
912
|
+
function encodeBytewords(data, style) {
|
|
913
|
+
const withCrc = new Uint8Array(data.length + 4);
|
|
914
|
+
withCrc.set(data);
|
|
915
|
+
withCrc.set(checksumBytes(data), data.length);
|
|
916
|
+
return encodeWords(withCrc, style);
|
|
917
|
+
}
|
|
918
|
+
/** The four checksum words of `data` alone (URKit `checksumWords` semantics). */
|
|
919
|
+
function bytewordsChecksum(data, style) {
|
|
920
|
+
return encodeWords(checksumBytes(data), style);
|
|
921
|
+
}
|
|
922
|
+
/** Encoded length of `length` bytes (without a checksum) for `style`. */
|
|
923
|
+
function bytewordsEncodedLength(length, style) {
|
|
924
|
+
if (length <= 0) return 0;
|
|
925
|
+
return style === "minimal" ? length * 2 : length * 5 - 1;
|
|
926
|
+
}
|
|
927
|
+
/** Four-word standard-style identifier of a 4-byte digest (BCR-2020-012). */
|
|
928
|
+
function bytewordsIdentifier(data) {
|
|
929
|
+
if (data.length !== 4) throw new TypeError("bytewords identifier requires exactly 4 bytes");
|
|
930
|
+
return encodeWords(data, "standard");
|
|
931
|
+
}
|
|
932
|
+
/** Four-emoji identifier of a 4-byte digest (BCR-2024-008). */
|
|
933
|
+
function bytemojiIdentifier(data) {
|
|
934
|
+
if (data.length !== 4) throw new TypeError("bytemoji identifier requires exactly 4 bytes");
|
|
935
|
+
const emojis = [];
|
|
936
|
+
for (const b of data) emojis.push(wordAt(BYTEMOJIS, b));
|
|
937
|
+
return emojis.join(" ");
|
|
938
|
+
}
|
|
939
|
+
/** Decode bytewords and verify CRC-32. Case-insensitive. */
|
|
940
|
+
function decodeBytewords(text, style) {
|
|
941
|
+
for (const ch of text) if ((ch.codePointAt(0) ?? 0) > 127) fail("NonAscii");
|
|
942
|
+
const lowered = asciiLower(text);
|
|
943
|
+
if (style === "minimal") return decodeMinimal(lowered);
|
|
944
|
+
const sep = style === "standard" ? " " : "-";
|
|
945
|
+
return decodeParts(lowered.length === 0 ? [] : lowered.split(sep), false);
|
|
946
|
+
}
|
|
947
|
+
function decodeMinimal(encoded) {
|
|
948
|
+
if (encoded.length % 2 !== 0) fail("InvalidBytewordsLength");
|
|
949
|
+
const parts = [];
|
|
950
|
+
for (let i = 0; i < encoded.length; i += 2) parts.push(encoded.slice(i, i + 2));
|
|
951
|
+
return decodeParts(parts, true);
|
|
952
|
+
}
|
|
953
|
+
function decodeParts(parts, minimal) {
|
|
954
|
+
const data = new Uint8Array(parts.length);
|
|
955
|
+
for (const [i, part] of parts.entries()) {
|
|
956
|
+
const expectedLen = minimal ? 2 : 4;
|
|
957
|
+
if (part.length !== expectedLen) fail("InvalidWord");
|
|
958
|
+
const byte = wordByToken.get(part);
|
|
959
|
+
if (byte === void 0) fail("InvalidWord");
|
|
960
|
+
if (part !== wordAt(minimal ? MINIMALS : WORDS, byte)) fail("InvalidWord");
|
|
961
|
+
data[i] = byte;
|
|
962
|
+
}
|
|
963
|
+
return stripChecksum(data);
|
|
964
|
+
}
|
|
965
|
+
function stripChecksum(data) {
|
|
966
|
+
if (data.length < 4) fail("InvalidBytewordsChecksum");
|
|
967
|
+
const split = data.length - 4;
|
|
968
|
+
const payload = data.subarray(0, split);
|
|
969
|
+
const expected = checksum(payload);
|
|
970
|
+
if (new DataView(data.buffer, data.byteOffset + split, 4).getUint32(0) !== expected) fail("InvalidBytewordsChecksum");
|
|
971
|
+
return new Uint8Array(payload);
|
|
972
|
+
}
|
|
973
|
+
/** Canonicalize a 2–4 letter token to the full 4-letter lowercase byteword. */
|
|
974
|
+
function canonicalizeByteword(token) {
|
|
975
|
+
for (const ch of token) if ((ch.codePointAt(0) ?? 0) > 127) return;
|
|
976
|
+
const lower = asciiLower(token);
|
|
977
|
+
if (lower.length === 4) {
|
|
978
|
+
const b = wordByToken.get(lower);
|
|
979
|
+
return b === void 0 ? void 0 : WORDS[b];
|
|
980
|
+
}
|
|
981
|
+
if (lower.length === 2) {
|
|
982
|
+
const b = wordByToken.get(lower);
|
|
983
|
+
return b === void 0 ? void 0 : WORDS[b];
|
|
984
|
+
}
|
|
985
|
+
if (lower.length === 3) {
|
|
986
|
+
let found;
|
|
987
|
+
for (const word of WORDS) if (word.slice(0, 3) === lower || word.slice(1, 4) === lower) {
|
|
988
|
+
if (found !== void 0) return;
|
|
989
|
+
found = word;
|
|
990
|
+
}
|
|
991
|
+
return found;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
//#endregion
|
|
995
|
+
//#region src/consensus/sampler.ts
|
|
996
|
+
function at(list, i) {
|
|
997
|
+
const v = list[i];
|
|
998
|
+
if (v === void 0) throw new Error("unreachable: index out of range");
|
|
999
|
+
return v;
|
|
1000
|
+
}
|
|
1001
|
+
/** Walker's alias method with reverse-index ordered partition (ur-rs / bcur). */
|
|
1002
|
+
var Sampler = class {
|
|
1003
|
+
#aliases;
|
|
1004
|
+
#probs;
|
|
1005
|
+
constructor(weightsIn) {
|
|
1006
|
+
const weights = [...weightsIn];
|
|
1007
|
+
for (const w of weights) if (w < 0) throw new Error("negative probability encountered");
|
|
1008
|
+
const summed = weights.reduce((a, b) => a + b, 0);
|
|
1009
|
+
if (!(summed > 0)) throw new Error("probabilities don't sum to a positive value");
|
|
1010
|
+
const count = weights.length;
|
|
1011
|
+
for (const [i, w] of weights.entries()) weights[i] = w * (count / summed);
|
|
1012
|
+
const s = [];
|
|
1013
|
+
const l = [];
|
|
1014
|
+
for (let j = count - 1; j >= 0; j--) if (at(weights, j) < 1) s.push(j);
|
|
1015
|
+
else l.push(j);
|
|
1016
|
+
const probs = Array.from({ length: count }, () => 0);
|
|
1017
|
+
const aliases = Array.from({ length: count }, () => 0);
|
|
1018
|
+
while (s.length > 0 && l.length > 0) {
|
|
1019
|
+
const a = s.pop();
|
|
1020
|
+
const g = l.pop();
|
|
1021
|
+
if (a === void 0 || g === void 0) break;
|
|
1022
|
+
probs[a] = at(weights, a);
|
|
1023
|
+
aliases[a] = g;
|
|
1024
|
+
weights[g] = at(weights, g) + (at(weights, a) - 1);
|
|
1025
|
+
if (at(weights, g) < 1) s.push(g);
|
|
1026
|
+
else l.push(g);
|
|
1027
|
+
}
|
|
1028
|
+
for (const i of l) probs[i] = 1;
|
|
1029
|
+
for (const i of s) probs[i] = 1;
|
|
1030
|
+
this.#aliases = aliases;
|
|
1031
|
+
this.#probs = probs;
|
|
1032
|
+
}
|
|
1033
|
+
next(xoshiro) {
|
|
1034
|
+
const n = this.#probs.length;
|
|
1035
|
+
const i = xoshiro.nextInt(0, n - 1);
|
|
1036
|
+
if (xoshiro.nextDouble() < at(this.#probs, i)) return i;
|
|
1037
|
+
return at(this.#aliases, i);
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
//#endregion
|
|
1041
|
+
//#region src/consensus/xoshiro.ts
|
|
1042
|
+
const MASK64 = (1n << 64n) - 1n;
|
|
1043
|
+
const U32_MASK = 4294967295n;
|
|
1044
|
+
function rotl(x, k) {
|
|
1045
|
+
const v = x & MASK64;
|
|
1046
|
+
return (v << BigInt(k) | v >> BigInt(64 - k)) & MASK64;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* `Double(value) / 2^64` with a single round-to-nearest-even conversion: `hi * 2^32` is exact and
|
|
1050
|
+
* adding `lo` performs the one IEEE-754 rounding; division by `2^64` is exact.
|
|
1051
|
+
*/
|
|
1052
|
+
function unitInterval(value) {
|
|
1053
|
+
const hi = Number(value >> 32n);
|
|
1054
|
+
const lo = Number(value & U32_MASK);
|
|
1055
|
+
return (hi * 2 ** 32 + lo) / 2 ** 64;
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* `floor(d * (high - low + 1)) + low`, clamped to `high` when `d` is 1.0 — the deliberate deviation
|
|
1059
|
+
* documented on `Xoshiro256.nextInt`.
|
|
1060
|
+
*/
|
|
1061
|
+
function scaledInt(d, low, high) {
|
|
1062
|
+
const span = high - low + 1;
|
|
1063
|
+
return Math.min(Math.floor(d * span), span - 1) + low;
|
|
1064
|
+
}
|
|
1065
|
+
/** Xoshiro256** with SHA-256 seeding matching URKit / ur-rs / bcur. */
|
|
1066
|
+
var Xoshiro256 = class Xoshiro256 {
|
|
1067
|
+
s0;
|
|
1068
|
+
s1;
|
|
1069
|
+
s2;
|
|
1070
|
+
s3;
|
|
1071
|
+
constructor(s0, s1, s2, s3) {
|
|
1072
|
+
this.s0 = s0;
|
|
1073
|
+
this.s1 = s1;
|
|
1074
|
+
this.s2 = s2;
|
|
1075
|
+
this.s3 = s3;
|
|
1076
|
+
}
|
|
1077
|
+
/** Seed from arbitrary bytes via SHA-256 + BE-limb→LE packing. */
|
|
1078
|
+
static fromBytes(bytes) {
|
|
1079
|
+
return Xoshiro256.fromDigest(sha256(bytes));
|
|
1080
|
+
}
|
|
1081
|
+
static fromString(value) {
|
|
1082
|
+
return Xoshiro256.fromBytes(new TextEncoder().encode(value));
|
|
1083
|
+
}
|
|
1084
|
+
/** Seed from CRC-32 BE bytes of `bytes` (test helper path). */
|
|
1085
|
+
static fromCrc(bytes) {
|
|
1086
|
+
const c = checksum(bytes);
|
|
1087
|
+
const seed = /* @__PURE__ */ new Uint8Array(4);
|
|
1088
|
+
seed[0] = c >>> 24 & 255;
|
|
1089
|
+
seed[1] = c >>> 16 & 255;
|
|
1090
|
+
seed[2] = c >>> 8 & 255;
|
|
1091
|
+
seed[3] = c & 255;
|
|
1092
|
+
return Xoshiro256.fromBytes(seed);
|
|
1093
|
+
}
|
|
1094
|
+
static fromDigest(seed32) {
|
|
1095
|
+
const view = new DataView(seed32.buffer, seed32.byteOffset, seed32.byteLength);
|
|
1096
|
+
return new Xoshiro256(view.getBigUint64(0), view.getBigUint64(8), view.getBigUint64(16), view.getBigUint64(24));
|
|
1097
|
+
}
|
|
1098
|
+
nextU64() {
|
|
1099
|
+
const result = rotl(this.s1 * 5n & MASK64, 7) * 9n & MASK64;
|
|
1100
|
+
const t = this.s1 << 17n & MASK64;
|
|
1101
|
+
this.s2 = (this.s2 ^ this.s0) & MASK64;
|
|
1102
|
+
this.s3 = (this.s3 ^ this.s1) & MASK64;
|
|
1103
|
+
this.s1 = (this.s1 ^ this.s2) & MASK64;
|
|
1104
|
+
this.s0 = (this.s0 ^ this.s3) & MASK64;
|
|
1105
|
+
this.s2 = (this.s2 ^ t) & MASK64;
|
|
1106
|
+
this.s3 = rotl(this.s3, 45);
|
|
1107
|
+
return result;
|
|
1108
|
+
}
|
|
1109
|
+
nextDouble() {
|
|
1110
|
+
return unitInterval(this.nextU64());
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Inclusive `[low, high]` via double scaling (normative float path). Deliberate deviation: clamp
|
|
1114
|
+
* to `high` when `nextDouble()` rounds to 1.0 (raw >= 2^64 - 2^10); the reference implementations
|
|
1115
|
+
* index out of bounds there.
|
|
1116
|
+
*/
|
|
1117
|
+
nextInt(low, high) {
|
|
1118
|
+
return scaledInt(this.nextDouble(), low, high);
|
|
1119
|
+
}
|
|
1120
|
+
/** Remove-order shuffle (not Fisher–Yates); stops after `count` picks. */
|
|
1121
|
+
shuffled(items, count = items.length) {
|
|
1122
|
+
const pool = [...items];
|
|
1123
|
+
const out = [];
|
|
1124
|
+
while (pool.length > 0 && out.length < count) {
|
|
1125
|
+
const index = this.nextInt(0, pool.length - 1);
|
|
1126
|
+
const [item] = pool.splice(index, 1);
|
|
1127
|
+
if (item === void 0) throw new Error("unreachable: splice returned empty");
|
|
1128
|
+
out.push(item);
|
|
1129
|
+
}
|
|
1130
|
+
return out;
|
|
1131
|
+
}
|
|
1132
|
+
chooseDegree(length) {
|
|
1133
|
+
const weights = [];
|
|
1134
|
+
for (let x = 1; x <= length; x++) weights.push(1 / x);
|
|
1135
|
+
return new Sampler(weights).next(this) + 1;
|
|
1136
|
+
}
|
|
1137
|
+
nextByte() {
|
|
1138
|
+
return this.nextInt(0, 255);
|
|
1139
|
+
}
|
|
1140
|
+
nextBytes(n) {
|
|
1141
|
+
const out = new Uint8Array(n);
|
|
1142
|
+
for (let i = 0; i < n; i++) out[i] = this.nextByte();
|
|
1143
|
+
return out;
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
//#endregion
|
|
1147
|
+
//#region src/consensus/chooser.ts
|
|
1148
|
+
/**
|
|
1149
|
+
* Per-stream index generator (BCR-2024-001 §4 FragmentChooser): harmonic degree sampler built once,
|
|
1150
|
+
* partial remove-shuffle per sequence.
|
|
1151
|
+
*/
|
|
1152
|
+
var FragmentChooser = class {
|
|
1153
|
+
fragmentCount;
|
|
1154
|
+
checksum;
|
|
1155
|
+
#degrees;
|
|
1156
|
+
constructor(fragmentCount, checksum) {
|
|
1157
|
+
this.fragmentCount = fragmentCount;
|
|
1158
|
+
this.checksum = checksum;
|
|
1159
|
+
const weights = [];
|
|
1160
|
+
for (let x = 1; x <= fragmentCount; x++) weights.push(1 / x);
|
|
1161
|
+
this.#degrees = new Sampler(weights);
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Fragment indexes mixed into sequence `sequence` (1-based), sorted ascending. Normative: simple
|
|
1165
|
+
* if sequence <= K; else degree + remove-shuffle.
|
|
1166
|
+
*/
|
|
1167
|
+
choose(sequence) {
|
|
1168
|
+
if (sequence <= this.fragmentCount) return [sequence - 1];
|
|
1169
|
+
const seed = /* @__PURE__ */ new Uint8Array(8);
|
|
1170
|
+
seed[0] = sequence >>> 24 & 255;
|
|
1171
|
+
seed[1] = sequence >>> 16 & 255;
|
|
1172
|
+
seed[2] = sequence >>> 8 & 255;
|
|
1173
|
+
seed[3] = sequence & 255;
|
|
1174
|
+
seed[4] = this.checksum >>> 24 & 255;
|
|
1175
|
+
seed[5] = this.checksum >>> 16 & 255;
|
|
1176
|
+
seed[6] = this.checksum >>> 8 & 255;
|
|
1177
|
+
seed[7] = this.checksum & 255;
|
|
1178
|
+
const xoshiro = Xoshiro256.fromBytes(seed);
|
|
1179
|
+
const degree = this.#degrees.next(xoshiro) + 1;
|
|
1180
|
+
const indexes = Array.from({ length: this.fragmentCount }, (_, i) => i);
|
|
1181
|
+
return xoshiro.shuffled(indexes, degree).sort((a, b) => a - b);
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
//#endregion
|
|
1185
|
+
//#region src/fountain/limits.ts
|
|
1186
|
+
/**
|
|
1187
|
+
* Production default; same integers as bcur.
|
|
1188
|
+
*
|
|
1189
|
+
* Hosts that need another budget pass `new UrDecoder({ limits })`.
|
|
1190
|
+
*/
|
|
1191
|
+
const DEFAULT_LIMITS = {
|
|
1192
|
+
maxMessageLength: 1048576,
|
|
1193
|
+
maxFragmentCount: 2e3,
|
|
1194
|
+
maxFragmentLength: 8192,
|
|
1195
|
+
maxUriLength: 8192
|
|
1196
|
+
};
|
|
1197
|
+
function mergeLimits(partial) {
|
|
1198
|
+
return {
|
|
1199
|
+
...DEFAULT_LIMITS,
|
|
1200
|
+
...partial
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
//#endregion
|
|
1204
|
+
//#region src/fountain/part.ts
|
|
1205
|
+
const MAX_U32$2 = 4294967295;
|
|
1206
|
+
function isU32(v) {
|
|
1207
|
+
return Number.isSafeInteger(v) && v >= 0 && v <= MAX_U32$2;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Semantic part validation shared by `decodePart`, the encoder, and the decoder ingest path:
|
|
1211
|
+
* `sequence`, `sequenceCount`, and `messageLength` are nonzero u32; `checksum` is a u32; `data` is
|
|
1212
|
+
* nonempty; and `data.length` is a consistent fragment length, i.e. `K · fragLen ≥ messageLength`
|
|
1213
|
+
* with padding under one fragment.
|
|
1214
|
+
*/
|
|
1215
|
+
function validatePart(part) {
|
|
1216
|
+
const fragLen = part.data.length;
|
|
1217
|
+
if (!isU32(part.sequence) || part.sequence === 0 || !isU32(part.sequenceCount) || part.sequenceCount === 0 || !isU32(part.messageLength) || part.messageLength === 0 || !isU32(part.checksum) || fragLen === 0) fail("InvalidPart");
|
|
1218
|
+
const product = BigInt(part.sequenceCount) * BigInt(fragLen);
|
|
1219
|
+
const length = BigInt(part.messageLength);
|
|
1220
|
+
if (product < length || product - length >= BigInt(fragLen)) fail("InvalidPart");
|
|
1221
|
+
}
|
|
1222
|
+
//#endregion
|
|
1223
|
+
//#region src/fountain/decoder.ts
|
|
1224
|
+
function maskBit(mask, column) {
|
|
1225
|
+
return ((mask[column >>> 5] ?? 0) & 1 << (column & 31)) !== 0;
|
|
1226
|
+
}
|
|
1227
|
+
function maskSet(mask, column) {
|
|
1228
|
+
const word = column >>> 5;
|
|
1229
|
+
mask[word] = (mask[word] ?? 0) | 1 << (column & 31);
|
|
1230
|
+
}
|
|
1231
|
+
function maskLowestBit(mask) {
|
|
1232
|
+
for (let i = 0; i < mask.length; i += 1) {
|
|
1233
|
+
const word = mask[i] ?? 0;
|
|
1234
|
+
if (word !== 0) return i * 32 + (31 - Math.clz32(word & -word));
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
/** SWAR population count; ~5 ops per word vs a per-set-bit loop. */
|
|
1238
|
+
function popcount32(value) {
|
|
1239
|
+
let w = value;
|
|
1240
|
+
w -= w >>> 1 & 1431655765;
|
|
1241
|
+
w = (w & 858993459) + (w >>> 2 & 858993459);
|
|
1242
|
+
w = w + (w >>> 4) & 252645135;
|
|
1243
|
+
return w * 16843009 >>> 24;
|
|
1244
|
+
}
|
|
1245
|
+
function maskPopcount(mask) {
|
|
1246
|
+
let ones = 0;
|
|
1247
|
+
for (const word of mask) ones += popcount32(word);
|
|
1248
|
+
return ones;
|
|
1249
|
+
}
|
|
1250
|
+
function xorWords(target, source) {
|
|
1251
|
+
for (let i = 0; i < target.length; i += 1) target[i] = (target[i] ?? 0) ^ (source[i] ?? 0);
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* Incremental Gauss-Jordan fountain decoder over GF(2).
|
|
1255
|
+
*
|
|
1256
|
+
* Rows are stored in reduced row echelon form keyed by pivot column, at most `K` rows. `receive`
|
|
1257
|
+
* never throws for frame problems: outcomes are reported as {@link ReceiveResult}. A fatal result
|
|
1258
|
+
* moves the session to `failed`; every further frame is then a `duplicate`.
|
|
1259
|
+
*/
|
|
1260
|
+
var FountainDecoder = class {
|
|
1261
|
+
#limits;
|
|
1262
|
+
#session = { phase: "empty" };
|
|
1263
|
+
#stream;
|
|
1264
|
+
/** `rows[p]` is the row whose pivot is column `p`. */
|
|
1265
|
+
#rows = [];
|
|
1266
|
+
#rank = 0;
|
|
1267
|
+
#recovered = 0;
|
|
1268
|
+
#processed = 0;
|
|
1269
|
+
#lastIndexes = [];
|
|
1270
|
+
constructor(options) {
|
|
1271
|
+
this.#limits = mergeLimits(options?.limits);
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Receive a fountain part. `accepted`/`duplicate` increment `processed`; `rejected` leaves the
|
|
1275
|
+
* session unchanged; `fatal` enters `failed`.
|
|
1276
|
+
*/
|
|
1277
|
+
receive(part) {
|
|
1278
|
+
if (this.#session.phase === "complete" || this.#session.phase === "failed") {
|
|
1279
|
+
this.#processed += 1;
|
|
1280
|
+
return { status: "duplicate" };
|
|
1281
|
+
}
|
|
1282
|
+
try {
|
|
1283
|
+
validatePart(part);
|
|
1284
|
+
} catch (error) {
|
|
1285
|
+
return {
|
|
1286
|
+
status: "rejected",
|
|
1287
|
+
error: toUrError(error)
|
|
1288
|
+
};
|
|
1289
|
+
}
|
|
1290
|
+
const locked = this.#lockStream(part);
|
|
1291
|
+
if ("result" in locked) return locked.result;
|
|
1292
|
+
const { stream } = locked;
|
|
1293
|
+
const indexes = stream.chooser.choose(part.sequence);
|
|
1294
|
+
const accepted = this.#insertRow(stream, indexes, part.data);
|
|
1295
|
+
this.#lastIndexes = indexes;
|
|
1296
|
+
this.#processed += 1;
|
|
1297
|
+
if (!accepted) return { status: "duplicate" };
|
|
1298
|
+
if (this.#rank === stream.sequenceCount) {
|
|
1299
|
+
const failure = this.#join(stream);
|
|
1300
|
+
if (failure !== void 0) return failure;
|
|
1301
|
+
}
|
|
1302
|
+
return { status: "accepted" };
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Returns the locked stream metadata. While a stream is locked the consistency check runs first
|
|
1306
|
+
* (`InconsistentPart`, rejected — it subsumes the fragment-length check); the limits apply only
|
|
1307
|
+
* to the part that locks the stream and are fatal.
|
|
1308
|
+
*/
|
|
1309
|
+
#lockStream(part) {
|
|
1310
|
+
const stream = this.#stream;
|
|
1311
|
+
if (stream !== void 0) {
|
|
1312
|
+
if (part.sequenceCount !== stream.sequenceCount || part.messageLength !== stream.messageLength || part.checksum !== stream.checksum || part.data.length !== stream.fragmentLength) return { result: {
|
|
1313
|
+
status: "rejected",
|
|
1314
|
+
error: new UrError({ code: "InconsistentPart" })
|
|
1315
|
+
} };
|
|
1316
|
+
return { stream };
|
|
1317
|
+
}
|
|
1318
|
+
if (part.sequenceCount > this.#limits.maxFragmentCount) return { result: this.#fail({
|
|
1319
|
+
code: "ResourceLimit",
|
|
1320
|
+
limit: "fragmentCount"
|
|
1321
|
+
}) };
|
|
1322
|
+
if (part.messageLength > this.#limits.maxMessageLength) return { result: this.#fail({
|
|
1323
|
+
code: "ResourceLimit",
|
|
1324
|
+
limit: "messageLength"
|
|
1325
|
+
}) };
|
|
1326
|
+
if (part.data.length > this.#limits.maxFragmentLength) return { result: this.#fail({
|
|
1327
|
+
code: "ResourceLimit",
|
|
1328
|
+
limit: "fragmentLength"
|
|
1329
|
+
}) };
|
|
1330
|
+
const created = {
|
|
1331
|
+
chooser: new FragmentChooser(part.sequenceCount, part.checksum),
|
|
1332
|
+
sequenceCount: part.sequenceCount,
|
|
1333
|
+
messageLength: part.messageLength,
|
|
1334
|
+
checksum: part.checksum,
|
|
1335
|
+
fragmentLength: part.data.length
|
|
1336
|
+
};
|
|
1337
|
+
this.#stream = created;
|
|
1338
|
+
this.#rows = Array.from({ length: part.sequenceCount }, () => void 0);
|
|
1339
|
+
this.#session = { phase: "collecting" };
|
|
1340
|
+
return { stream: created };
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Forward-eliminates the part against existing pivot rows, then back-substitutes the new row into
|
|
1344
|
+
* every row sharing its pivot column. Returns whether the rank increased.
|
|
1345
|
+
*/
|
|
1346
|
+
#insertRow(stream, indexes, data) {
|
|
1347
|
+
const mask = new Uint32Array(Math.ceil(stream.sequenceCount / 32));
|
|
1348
|
+
for (const index of indexes) maskSet(mask, index);
|
|
1349
|
+
const rowData = new Uint32Array(Math.ceil(stream.fragmentLength / 4));
|
|
1350
|
+
new Uint8Array(rowData.buffer, rowData.byteOffset, stream.fragmentLength).set(data);
|
|
1351
|
+
for (const index of indexes) {
|
|
1352
|
+
if (!maskBit(mask, index)) continue;
|
|
1353
|
+
const row = this.#rows[index];
|
|
1354
|
+
if (row === void 0) continue;
|
|
1355
|
+
xorWords(mask, row.mask);
|
|
1356
|
+
xorWords(rowData, row.data);
|
|
1357
|
+
}
|
|
1358
|
+
const pivot = maskLowestBit(mask);
|
|
1359
|
+
if (pivot === void 0) return false;
|
|
1360
|
+
for (const row of this.#rows) {
|
|
1361
|
+
if (row === void 0 || !maskBit(row.mask, pivot)) continue;
|
|
1362
|
+
const wasUnit = row.ones === 1;
|
|
1363
|
+
xorWords(row.mask, mask);
|
|
1364
|
+
xorWords(row.data, rowData);
|
|
1365
|
+
row.ones = maskPopcount(row.mask);
|
|
1366
|
+
if (wasUnit && row.ones !== 1) this.#recovered -= 1;
|
|
1367
|
+
else if (!wasUnit && row.ones === 1) this.#recovered += 1;
|
|
1368
|
+
}
|
|
1369
|
+
const ones = maskPopcount(mask);
|
|
1370
|
+
if (ones === 1) this.#recovered += 1;
|
|
1371
|
+
this.#rows[pivot] = {
|
|
1372
|
+
mask,
|
|
1373
|
+
data: rowData,
|
|
1374
|
+
ones
|
|
1375
|
+
};
|
|
1376
|
+
this.#rank += 1;
|
|
1377
|
+
return true;
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Joins the `K` unit rows in fragment order and verifies padding and CRC-32 exactly once. Returns
|
|
1381
|
+
* a `fatal` result or `undefined` on success.
|
|
1382
|
+
*/
|
|
1383
|
+
#join(stream) {
|
|
1384
|
+
const combined = new Uint8Array(stream.fragmentLength * stream.sequenceCount);
|
|
1385
|
+
for (const [i, row] of this.#rows.entries()) {
|
|
1386
|
+
if (row === void 0 || row.ones !== 1) return this.#fail({ code: "Internal" });
|
|
1387
|
+
const bytes = new Uint8Array(row.data.buffer, row.data.byteOffset, stream.fragmentLength);
|
|
1388
|
+
combined.set(bytes, i * stream.fragmentLength);
|
|
1389
|
+
}
|
|
1390
|
+
for (let i = stream.messageLength; i < combined.length; i++) if (combined[i] !== 0) return this.#fail({ code: "InvalidPadding" });
|
|
1391
|
+
const message = combined.subarray(0, stream.messageLength);
|
|
1392
|
+
if (checksum(message) !== stream.checksum) return this.#fail({ code: "InvalidMessageChecksum" });
|
|
1393
|
+
this.#session = {
|
|
1394
|
+
phase: "complete",
|
|
1395
|
+
value: message
|
|
1396
|
+
};
|
|
1397
|
+
}
|
|
1398
|
+
/** Moves the session to `failed` and returns the fatal result. */
|
|
1399
|
+
#fail(info) {
|
|
1400
|
+
const error = new UrError(info);
|
|
1401
|
+
this.#session = {
|
|
1402
|
+
phase: "failed",
|
|
1403
|
+
error
|
|
1404
|
+
};
|
|
1405
|
+
return {
|
|
1406
|
+
status: "fatal",
|
|
1407
|
+
error
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
/** Current session state. */
|
|
1411
|
+
get state() {
|
|
1412
|
+
const session = this.#session;
|
|
1413
|
+
switch (session.phase) {
|
|
1414
|
+
case "complete": return {
|
|
1415
|
+
phase: "complete",
|
|
1416
|
+
value: session.value
|
|
1417
|
+
};
|
|
1418
|
+
case "failed": return {
|
|
1419
|
+
phase: "failed",
|
|
1420
|
+
error: session.error
|
|
1421
|
+
};
|
|
1422
|
+
case "collecting": return {
|
|
1423
|
+
phase: "collecting",
|
|
1424
|
+
progress: this.progress
|
|
1425
|
+
};
|
|
1426
|
+
default: return { phase: "empty" };
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
/** Progress snapshot (`empty` reports all zeros). */
|
|
1430
|
+
get progress() {
|
|
1431
|
+
const k = this.#stream?.sequenceCount ?? 0;
|
|
1432
|
+
return {
|
|
1433
|
+
fragmentCount: k,
|
|
1434
|
+
rank: this.#rank,
|
|
1435
|
+
recovered: this.#recovered,
|
|
1436
|
+
processed: this.#processed,
|
|
1437
|
+
ratio: k === 0 ? 0 : this.#rank / k
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
/** Fragment indexes of the most recent `accepted`/`duplicate` part. */
|
|
1441
|
+
get lastIndexes() {
|
|
1442
|
+
return this.#lastIndexes;
|
|
1443
|
+
}
|
|
1444
|
+
/** Returns the session to `empty`; limits are kept. */
|
|
1445
|
+
reset() {
|
|
1446
|
+
this.#session = { phase: "empty" };
|
|
1447
|
+
this.#stream = void 0;
|
|
1448
|
+
this.#rows = [];
|
|
1449
|
+
this.#rank = 0;
|
|
1450
|
+
this.#recovered = 0;
|
|
1451
|
+
this.#processed = 0;
|
|
1452
|
+
this.#lastIndexes = [];
|
|
1453
|
+
}
|
|
1454
|
+
};
|
|
1455
|
+
//#endregion
|
|
1456
|
+
//#region src/fountain/encoder.ts
|
|
1457
|
+
/** XOR `src` into `target`; buffers are equal-length by construction. */
|
|
1458
|
+
function xorInto(target, src) {
|
|
1459
|
+
for (const [i, a] of target.entries()) target[i] = a ^ (src[i] ?? 0);
|
|
1460
|
+
}
|
|
1461
|
+
function divCeil(a, b) {
|
|
1462
|
+
return Math.trunc((a + b - 1) / b);
|
|
1463
|
+
}
|
|
1464
|
+
const MAX_U32$1 = 4294967295;
|
|
1465
|
+
const DEFAULT_MIN_FRAGMENT_LENGTH = 10;
|
|
1466
|
+
/**
|
|
1467
|
+
* URKit `findNominalFragmentLength` in closed form: the smallest fragment count that fits `max` is
|
|
1468
|
+
* `ceil(len / max)`, bounded above by `floor(len / min)` (and at least 1). When `min` binds,
|
|
1469
|
+
* fragments may exceed `maxFragmentLength` — that is the reference behavior.
|
|
1470
|
+
*/
|
|
1471
|
+
function fragmentLength(dataLength, maxFragmentLength, minFragmentLength = DEFAULT_MIN_FRAGMENT_LENGTH) {
|
|
1472
|
+
return divCeil(dataLength, Math.min(divCeil(dataLength, maxFragmentLength), Math.max(1, Math.floor(dataLength / minFragmentLength))));
|
|
1473
|
+
}
|
|
1474
|
+
/** Pad and split a message into `fragmentLength`-sized fragments. */
|
|
1475
|
+
function partition(data, fragLen) {
|
|
1476
|
+
const pad = (fragLen - data.length % fragLen) % fragLen;
|
|
1477
|
+
const padded = new Uint8Array(data.length + pad);
|
|
1478
|
+
padded.set(data);
|
|
1479
|
+
const out = [];
|
|
1480
|
+
for (let i = 0; i < padded.length; i += fragLen) out.push(padded.subarray(i, i + fragLen));
|
|
1481
|
+
return out;
|
|
1482
|
+
}
|
|
1483
|
+
/**
|
|
1484
|
+
* Fountain encoder. An infinite iterator: produces parts with sequence `firstSequence + 1`, `+2`, …
|
|
1485
|
+
* and ends after `0xFFFFFFFF` (UR-ADR-028). For `K == 1` it keeps producing identical parts with
|
|
1486
|
+
* rising sequences.
|
|
1487
|
+
*/
|
|
1488
|
+
var FountainEncoder = class {
|
|
1489
|
+
#parts;
|
|
1490
|
+
#chooser;
|
|
1491
|
+
#fragLen;
|
|
1492
|
+
#msgLen;
|
|
1493
|
+
#seqCount;
|
|
1494
|
+
#messageChecksum;
|
|
1495
|
+
#seq;
|
|
1496
|
+
#lastIndexes = [];
|
|
1497
|
+
constructor(message, options) {
|
|
1498
|
+
const { maxFragmentLength, minFragmentLength = DEFAULT_MIN_FRAGMENT_LENGTH, firstSequence = 0 } = options;
|
|
1499
|
+
if (message.length === 0) fail("EmptyMessage");
|
|
1500
|
+
if (message.length > MAX_U32$1) fail("MessageTooLong");
|
|
1501
|
+
if (!Number.isSafeInteger(maxFragmentLength) || maxFragmentLength < 1 || !Number.isSafeInteger(minFragmentLength) || minFragmentLength < 1 || minFragmentLength > maxFragmentLength) fail("InvalidFragmentLength");
|
|
1502
|
+
if (!Number.isSafeInteger(firstSequence) || firstSequence < 0 || firstSequence > MAX_U32$1) throw new RangeError("firstSequence must be an integer in 0..=0xFFFFFFFF");
|
|
1503
|
+
const fragLen = fragmentLength(message.length, maxFragmentLength, minFragmentLength);
|
|
1504
|
+
const fragments = partition(message, fragLen);
|
|
1505
|
+
const messageChecksum = checksum(message);
|
|
1506
|
+
this.#parts = fragments;
|
|
1507
|
+
this.#chooser = new FragmentChooser(fragments.length, messageChecksum);
|
|
1508
|
+
this.#fragLen = fragLen;
|
|
1509
|
+
this.#msgLen = message.length;
|
|
1510
|
+
this.#seqCount = fragments.length;
|
|
1511
|
+
this.#messageChecksum = messageChecksum;
|
|
1512
|
+
this.#seq = firstSequence;
|
|
1513
|
+
}
|
|
1514
|
+
get fragmentCount() {
|
|
1515
|
+
return this.#seqCount;
|
|
1516
|
+
}
|
|
1517
|
+
get fragmentLength() {
|
|
1518
|
+
return this.#fragLen;
|
|
1519
|
+
}
|
|
1520
|
+
get messageLength() {
|
|
1521
|
+
return this.#msgLen;
|
|
1522
|
+
}
|
|
1523
|
+
/** Last emitted sequence number, or `firstSequence` before any part. */
|
|
1524
|
+
get sequence() {
|
|
1525
|
+
return this.#seq;
|
|
1526
|
+
}
|
|
1527
|
+
get isComplete() {
|
|
1528
|
+
return this.#seq >= this.#seqCount;
|
|
1529
|
+
}
|
|
1530
|
+
/** Fragment indexes mixed into the most recently produced part. */
|
|
1531
|
+
get lastFragmentIndexes() {
|
|
1532
|
+
return this.#lastIndexes;
|
|
1533
|
+
}
|
|
1534
|
+
next() {
|
|
1535
|
+
if (this.#seq === MAX_U32$1) return {
|
|
1536
|
+
value: void 0,
|
|
1537
|
+
done: true
|
|
1538
|
+
};
|
|
1539
|
+
this.#seq += 1;
|
|
1540
|
+
const indexes = this.#chooser.choose(this.#seq);
|
|
1541
|
+
const mixed = new Uint8Array(this.#fragLen);
|
|
1542
|
+
for (const i of indexes) {
|
|
1543
|
+
const fragment = this.#parts[i];
|
|
1544
|
+
if (fragment !== void 0) xorInto(mixed, fragment);
|
|
1545
|
+
}
|
|
1546
|
+
const part = {
|
|
1547
|
+
sequence: this.#seq,
|
|
1548
|
+
sequenceCount: this.#seqCount,
|
|
1549
|
+
messageLength: this.#msgLen,
|
|
1550
|
+
checksum: this.#messageChecksum,
|
|
1551
|
+
data: mixed
|
|
1552
|
+
};
|
|
1553
|
+
this.#lastIndexes = indexes;
|
|
1554
|
+
return {
|
|
1555
|
+
value: part,
|
|
1556
|
+
done: false
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
[Symbol.iterator]() {
|
|
1560
|
+
return this;
|
|
1561
|
+
}
|
|
1562
|
+
};
|
|
1563
|
+
//#endregion
|
|
1564
|
+
//#region src/fountain/part-cbor.ts
|
|
1565
|
+
const MAX_U32 = 4294967295;
|
|
1566
|
+
/**
|
|
1567
|
+
* Encode a part as fixed-schema deterministic CBOR: `array(5) [sequence, sequenceCount,
|
|
1568
|
+
* messageLength, checksum, data]` with shortest-form integers.
|
|
1569
|
+
*/
|
|
1570
|
+
function encodePart(part) {
|
|
1571
|
+
validatePart(part);
|
|
1572
|
+
const out = [133];
|
|
1573
|
+
encodeU32(out, part.sequence);
|
|
1574
|
+
encodeU32(out, part.sequenceCount);
|
|
1575
|
+
encodeU32(out, part.messageLength);
|
|
1576
|
+
encodeU32(out, part.checksum);
|
|
1577
|
+
encodeBstr(out, part.data);
|
|
1578
|
+
return new Uint8Array(out);
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Decode a part from CBOR. Lenient on integer width (UR-ADR-017): any well-formed definite-length
|
|
1582
|
+
* encoding is accepted; re-encode with {@link encodePart} for the shortest form. `limits` apply
|
|
1583
|
+
* `maxFragmentLength` to the bstr and `maxFragmentCount` to `sequenceCount`.
|
|
1584
|
+
*/
|
|
1585
|
+
function decodePart(bytes, limits) {
|
|
1586
|
+
return decodePartInner(bytes, mergeLimits(limits));
|
|
1587
|
+
}
|
|
1588
|
+
/**
|
|
1589
|
+
* Part decode without `maxFragmentLength`/`maxFragmentCount` checks, for callers whose input is
|
|
1590
|
+
* already bounded (the `UrDecoder` URI-length budget). Semantic validation still applies.
|
|
1591
|
+
*/
|
|
1592
|
+
function decodePartRaw(bytes) {
|
|
1593
|
+
return decodePartInner(bytes, void 0);
|
|
1594
|
+
}
|
|
1595
|
+
function decodePartInner(bytes, limits) {
|
|
1596
|
+
const cur = { i: 0 };
|
|
1597
|
+
if (decodeLen(bytes, cur, 4) !== 5) fail("InvalidPartCbor");
|
|
1598
|
+
const sequence = decodeU32(bytes, cur);
|
|
1599
|
+
const sequenceCount = decodeU32(bytes, cur);
|
|
1600
|
+
const messageLength = decodeU32(bytes, cur);
|
|
1601
|
+
const checksum = decodeU32(bytes, cur);
|
|
1602
|
+
const data = decodeBstr(bytes, cur, limits?.maxFragmentLength);
|
|
1603
|
+
if (cur.i !== bytes.length) fail("InvalidPartCbor");
|
|
1604
|
+
const part = {
|
|
1605
|
+
sequence,
|
|
1606
|
+
sequenceCount,
|
|
1607
|
+
messageLength,
|
|
1608
|
+
checksum,
|
|
1609
|
+
data
|
|
1610
|
+
};
|
|
1611
|
+
validatePart(part);
|
|
1612
|
+
if (limits !== void 0 && sequenceCount > limits.maxFragmentCount) fail({
|
|
1613
|
+
code: "ResourceLimit",
|
|
1614
|
+
limit: "fragmentCount"
|
|
1615
|
+
});
|
|
1616
|
+
return part;
|
|
1617
|
+
}
|
|
1618
|
+
function encodeU32(out, v) {
|
|
1619
|
+
if (v <= 23) out.push(v);
|
|
1620
|
+
else if (v <= 255) out.push(24, v);
|
|
1621
|
+
else if (v <= 65535) out.push(25, v >>> 8 & 255, v & 255);
|
|
1622
|
+
else out.push(26, v >>> 24 & 255, v >>> 16 & 255, v >>> 8 & 255, v & 255);
|
|
1623
|
+
}
|
|
1624
|
+
function encodeBstr(out, data) {
|
|
1625
|
+
const len = data.length;
|
|
1626
|
+
if (len <= 23) out.push(64 | len);
|
|
1627
|
+
else if (len <= 255) out.push(88, len);
|
|
1628
|
+
else if (len <= 65535) out.push(89, len >>> 8 & 255, len & 255);
|
|
1629
|
+
else out.push(90, len >>> 24 & 255, len >>> 16 & 255, len >>> 8 & 255, len & 255);
|
|
1630
|
+
for (const b of data) out.push(b);
|
|
1631
|
+
}
|
|
1632
|
+
/**
|
|
1633
|
+
* Reads a definite-length argument for the given major type in any width (including 8-byte u64).
|
|
1634
|
+
* Values above 2^53 lose low bits — callers only order or bound-check such values; equality is only
|
|
1635
|
+
* demanded of values <= u32::MAX.
|
|
1636
|
+
*/
|
|
1637
|
+
function decodeLen(bytes, cur, major) {
|
|
1638
|
+
const head = read(bytes, cur);
|
|
1639
|
+
if (head >> 5 !== major) fail("InvalidPartCbor");
|
|
1640
|
+
const ai = head & 31;
|
|
1641
|
+
if (ai <= 23) return ai;
|
|
1642
|
+
if (ai === 24) return read(bytes, cur);
|
|
1643
|
+
if (ai === 25) return read(bytes, cur) << 8 | read(bytes, cur);
|
|
1644
|
+
if (ai === 26) return (read(bytes, cur) << 24 | read(bytes, cur) << 16 | read(bytes, cur) << 8 | read(bytes, cur)) >>> 0;
|
|
1645
|
+
if (ai === 27) {
|
|
1646
|
+
const hi = (read(bytes, cur) << 24 | read(bytes, cur) << 16 | read(bytes, cur) << 8 | read(bytes, cur)) >>> 0;
|
|
1647
|
+
const lo = (read(bytes, cur) << 24 | read(bytes, cur) << 16 | read(bytes, cur) << 8 | read(bytes, cur)) >>> 0;
|
|
1648
|
+
return hi * 4294967296 + lo;
|
|
1649
|
+
}
|
|
1650
|
+
return fail("InvalidPartCbor");
|
|
1651
|
+
}
|
|
1652
|
+
function decodeU32(bytes, cur) {
|
|
1653
|
+
const v = decodeLen(bytes, cur, 0);
|
|
1654
|
+
if (v > MAX_U32) fail("InvalidPartCbor");
|
|
1655
|
+
return v;
|
|
1656
|
+
}
|
|
1657
|
+
function decodeBstr(bytes, cur, maxDataLen) {
|
|
1658
|
+
const len = decodeLen(bytes, cur, 2);
|
|
1659
|
+
if (maxDataLen !== void 0 && len > maxDataLen) fail({
|
|
1660
|
+
code: "ResourceLimit",
|
|
1661
|
+
limit: "fragmentLength"
|
|
1662
|
+
});
|
|
1663
|
+
if (cur.i + len > bytes.length) fail("InvalidPartCbor");
|
|
1664
|
+
const data = bytes.slice(cur.i, cur.i + len);
|
|
1665
|
+
cur.i += len;
|
|
1666
|
+
return data;
|
|
1667
|
+
}
|
|
1668
|
+
function read(bytes, cur) {
|
|
1669
|
+
const b = bytes[cur.i];
|
|
1670
|
+
if (b === void 0) fail("InvalidPartCbor");
|
|
1671
|
+
cur.i += 1;
|
|
1672
|
+
return b;
|
|
1673
|
+
}
|
|
1674
|
+
//#endregion
|
|
1675
|
+
//#region src/ur/type.ts
|
|
1676
|
+
/** Validate and lowercase a UR type token. Throws `UrError(InvalidType)`. */
|
|
1677
|
+
function parseUrType(text) {
|
|
1678
|
+
const lower = asciiLower(text);
|
|
1679
|
+
if (lower.length === 0 || !/^[a-z0-9-]+$/.test(lower)) fail("InvalidType");
|
|
1680
|
+
return lower;
|
|
1681
|
+
}
|
|
1682
|
+
/** True iff `text` is already a canonical lowercase UR type token (no folding). */
|
|
1683
|
+
function isUrType(text) {
|
|
1684
|
+
return /^[a-z0-9-]+$/.test(text);
|
|
1685
|
+
}
|
|
1686
|
+
//#endregion
|
|
1687
|
+
//#region src/ur/parse.ts
|
|
1688
|
+
/**
|
|
1689
|
+
* Scheme and type: an ASCII case-insensitive `ur:` prefix (`InvalidScheme`), then the token up to
|
|
1690
|
+
* the first `/` (`TypeUnspecified` if none, `InvalidType` if illegal). Only the type token is
|
|
1691
|
+
* lowercased — the body keeps its original case (bytewords decode is itself case-insensitive).
|
|
1692
|
+
*/
|
|
1693
|
+
function parseUrHead(text) {
|
|
1694
|
+
if (text.length < 3 || asciiLower(text.slice(0, 3)) !== "ur:") fail("InvalidScheme");
|
|
1695
|
+
const rest0 = text.slice(3);
|
|
1696
|
+
const slash = rest0.indexOf("/");
|
|
1697
|
+
if (slash === -1) fail("TypeUnspecified");
|
|
1698
|
+
return {
|
|
1699
|
+
type: parseUrType(rest0.slice(0, slash)),
|
|
1700
|
+
rest: rest0.slice(slash + 1)
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Text admission after type admission: the whole URI must be ASCII (`NonAscii`, so `.length` is the
|
|
1705
|
+
* byte count), then the URI-length budget (`ResourceLimit: uriLength`).
|
|
1706
|
+
*/
|
|
1707
|
+
function checkUriText(text, limits) {
|
|
1708
|
+
if (!isAscii(text)) fail("NonAscii");
|
|
1709
|
+
if (text.length > limits.maxUriLength) fail({
|
|
1710
|
+
code: "ResourceLimit",
|
|
1711
|
+
limit: "uriLength"
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
/** Indices grammar, bytewords, and part CBOR for the text after `ur:<type>/`. */
|
|
1715
|
+
function parseUrBody(type, rest, decode) {
|
|
1716
|
+
const lastSlash = rest.lastIndexOf("/");
|
|
1717
|
+
if (lastSlash === -1) return {
|
|
1718
|
+
kind: "single",
|
|
1719
|
+
type,
|
|
1720
|
+
message: decodeBytewords(rest, "minimal")
|
|
1721
|
+
};
|
|
1722
|
+
const { seq, count } = decodeIndices(rest.slice(0, lastSlash));
|
|
1723
|
+
const part = decode(decodeBytewords(rest.slice(lastSlash + 1), "minimal"));
|
|
1724
|
+
if (part.sequence !== seq || part.sequenceCount !== count) fail("InvalidIndices");
|
|
1725
|
+
return {
|
|
1726
|
+
kind: "multi",
|
|
1727
|
+
type,
|
|
1728
|
+
part
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* Parse and decode a UR string. Case-insensitive for the URI and bytewords content. `limits` bounds
|
|
1733
|
+
* the URI length and the multi-part CBOR fields. Throws `UrError`.
|
|
1734
|
+
*/
|
|
1735
|
+
function parseUr(text, limits) {
|
|
1736
|
+
const merged = mergeLimits(limits);
|
|
1737
|
+
const { type, rest } = parseUrHead(text);
|
|
1738
|
+
checkUriText(text, merged);
|
|
1739
|
+
return parseUrBody(type, rest, (bytes) => decodePart(bytes, merged));
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Decoder frame parse after type admission: same text budget, but the part decodes without
|
|
1743
|
+
* count/length caps — `maxUriLength` already bounds the frame.
|
|
1744
|
+
*/
|
|
1745
|
+
function parseUrFrame(head, text, limits) {
|
|
1746
|
+
checkUriText(text, limits);
|
|
1747
|
+
return parseUrBody(head.type, head.rest, decodePartRaw);
|
|
1748
|
+
}
|
|
1749
|
+
/** UR-ADR-029: `seq = 1*DIGIT "-" 1*DIGIT`, both in `1..=0xFFFFFFFF`. */
|
|
1750
|
+
function decodeIndices(indices) {
|
|
1751
|
+
const dash = indices.indexOf("-");
|
|
1752
|
+
if (dash === -1) fail("InvalidIndices");
|
|
1753
|
+
const a = indices.slice(0, dash);
|
|
1754
|
+
const b = indices.slice(dash + 1);
|
|
1755
|
+
if (!/^\d+$/.test(a) || !/^\d+$/.test(b)) fail("InvalidIndices");
|
|
1756
|
+
const seq = Number(a);
|
|
1757
|
+
const count = Number(b);
|
|
1758
|
+
if (!Number.isSafeInteger(seq) || !Number.isSafeInteger(count) || seq === 0 || count === 0 || seq > 4294967295 || count > 4294967295) fail("InvalidIndices");
|
|
1759
|
+
return {
|
|
1760
|
+
seq,
|
|
1761
|
+
count
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
//#endregion
|
|
1765
|
+
//#region src/ur/index.ts
|
|
1766
|
+
/** Encode a single-part UR. Empty `message` is allowed. */
|
|
1767
|
+
function encodeUr(type, message) {
|
|
1768
|
+
return `ur:${type}/${encodeBytewords(message, "minimal")}`;
|
|
1769
|
+
}
|
|
1770
|
+
/** Uppercase UR string for denser QR alphanumeric mode. */
|
|
1771
|
+
function toQrString(ur) {
|
|
1772
|
+
return asciiUpper(ur);
|
|
1773
|
+
}
|
|
1774
|
+
/**
|
|
1775
|
+
* UR string encoder. `K == 1` emits the same single-part UR on every step; larger messages emit
|
|
1776
|
+
* `ur:<type>/<seq>-<count>/<bytewords>` fountain parts until sequence `0xFFFFFFFF`, where iteration
|
|
1777
|
+
* ends.
|
|
1778
|
+
*/
|
|
1779
|
+
var UrEncoder = class {
|
|
1780
|
+
#type;
|
|
1781
|
+
#fountain;
|
|
1782
|
+
#single;
|
|
1783
|
+
#emitted = false;
|
|
1784
|
+
constructor(type, message, options) {
|
|
1785
|
+
this.#type = type;
|
|
1786
|
+
this.#fountain = new FountainEncoder(message, options);
|
|
1787
|
+
this.#single = this.#fountain.fragmentCount === 1 ? encodeUr(type, new Uint8Array(message)) : void 0;
|
|
1788
|
+
}
|
|
1789
|
+
get type() {
|
|
1790
|
+
return this.#type;
|
|
1791
|
+
}
|
|
1792
|
+
get fragmentCount() {
|
|
1793
|
+
return this.#fountain.fragmentCount;
|
|
1794
|
+
}
|
|
1795
|
+
get isSinglePart() {
|
|
1796
|
+
return this.#fountain.fragmentCount === 1;
|
|
1797
|
+
}
|
|
1798
|
+
get isComplete() {
|
|
1799
|
+
return this.#single === void 0 ? this.#fountain.isComplete : this.#emitted;
|
|
1800
|
+
}
|
|
1801
|
+
/** Fragment indexes mixed into the most recently produced part. */
|
|
1802
|
+
get lastFragmentIndexes() {
|
|
1803
|
+
return this.#fountain.lastFragmentIndexes;
|
|
1804
|
+
}
|
|
1805
|
+
next() {
|
|
1806
|
+
const single = this.#single;
|
|
1807
|
+
if (single !== void 0) {
|
|
1808
|
+
this.#emitted = true;
|
|
1809
|
+
return {
|
|
1810
|
+
value: single,
|
|
1811
|
+
done: false
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
const { value: part, done } = this.#fountain.next();
|
|
1815
|
+
if (done === true || part === void 0) return {
|
|
1816
|
+
value: void 0,
|
|
1817
|
+
done: true
|
|
1818
|
+
};
|
|
1819
|
+
const body = encodeBytewords(encodePart(part), "minimal");
|
|
1820
|
+
return {
|
|
1821
|
+
value: `ur:${this.#type}/${part.sequence}-${part.sequenceCount}/${body}`,
|
|
1822
|
+
done: false
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
[Symbol.iterator]() {
|
|
1826
|
+
return this;
|
|
1827
|
+
}
|
|
1828
|
+
};
|
|
1829
|
+
/**
|
|
1830
|
+
* UR decoder (single-part or fountain).
|
|
1831
|
+
*
|
|
1832
|
+
* `receive` never throws for frame problems: parse, type, index, bytewords, and consistency
|
|
1833
|
+
* failures come back `rejected` with the session unchanged; limit violations and completion-check
|
|
1834
|
+
* failures are `fatal` and move the session to `failed`. Type admission precedes every resource
|
|
1835
|
+
* limit: a frame whose type is foreign to the session is `rejected` `UnexpectedType` no matter its
|
|
1836
|
+
* size, and only an admitted frame can fail the session. Terminal sessions return `duplicate` for
|
|
1837
|
+
* every further frame without parsing it (UR-ADR-014). The first successfully ingested frame locks
|
|
1838
|
+
* the UR type.
|
|
1839
|
+
*/
|
|
1840
|
+
var UrDecoder = class {
|
|
1841
|
+
#limits;
|
|
1842
|
+
#accept;
|
|
1843
|
+
#fountain;
|
|
1844
|
+
#lockedType;
|
|
1845
|
+
#terminal;
|
|
1846
|
+
#processed = 0;
|
|
1847
|
+
constructor(options) {
|
|
1848
|
+
this.#limits = mergeLimits(options?.limits);
|
|
1849
|
+
this.#fountain = new FountainDecoder({ limits: this.#limits });
|
|
1850
|
+
this.#accept = options?.accept ?? [];
|
|
1851
|
+
}
|
|
1852
|
+
/** Receive one UR string (single-part or fountain part). */
|
|
1853
|
+
receive(text) {
|
|
1854
|
+
if (this.#terminal !== void 0) {
|
|
1855
|
+
this.#processed += 1;
|
|
1856
|
+
return { status: "duplicate" };
|
|
1857
|
+
}
|
|
1858
|
+
try {
|
|
1859
|
+
return this.#receiveParsed(text);
|
|
1860
|
+
} catch (error) {
|
|
1861
|
+
const urError = toUrError(error);
|
|
1862
|
+
if (urError.fatal) return this.#fatal(urError);
|
|
1863
|
+
return {
|
|
1864
|
+
status: "rejected",
|
|
1865
|
+
error: urError
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
#receiveParsed(text) {
|
|
1870
|
+
const head = parseUrHead(text);
|
|
1871
|
+
this.#checkType(head.type);
|
|
1872
|
+
const parsed = parseUrFrame(head, text, this.#limits);
|
|
1873
|
+
return parsed.kind === "single" ? this.#receiveSingle(parsed.type, parsed.message) : this.#receiveFountain(parsed.type, parsed.part);
|
|
1874
|
+
}
|
|
1875
|
+
/** Type admission: `accept` list (when non-empty), then the locked type. */
|
|
1876
|
+
#checkType(type) {
|
|
1877
|
+
if (this.#accept.length > 0 && !this.#accept.some((t) => t === type)) fail({
|
|
1878
|
+
code: "UnexpectedType",
|
|
1879
|
+
expected: this.#accept,
|
|
1880
|
+
found: type
|
|
1881
|
+
});
|
|
1882
|
+
const locked = this.#lockedType;
|
|
1883
|
+
if (locked !== void 0 && locked !== type) fail({
|
|
1884
|
+
code: "UnexpectedType",
|
|
1885
|
+
expected: [locked],
|
|
1886
|
+
found: type
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
#receiveSingle(type, message) {
|
|
1890
|
+
if (this.#fountain.state.phase !== "empty") fail("InconsistentPart");
|
|
1891
|
+
if (message.length > this.#limits.maxMessageLength) return this.#fail({
|
|
1892
|
+
code: "ResourceLimit",
|
|
1893
|
+
limit: "messageLength"
|
|
1894
|
+
});
|
|
1895
|
+
this.#lockedType ??= type;
|
|
1896
|
+
this.#terminal = {
|
|
1897
|
+
phase: "complete",
|
|
1898
|
+
value: {
|
|
1899
|
+
type,
|
|
1900
|
+
message
|
|
1901
|
+
}
|
|
1902
|
+
};
|
|
1903
|
+
this.#processed += 1;
|
|
1904
|
+
return { status: "accepted" };
|
|
1905
|
+
}
|
|
1906
|
+
#receiveFountain(type, part) {
|
|
1907
|
+
const result = this.#fountain.receive(part);
|
|
1908
|
+
if (result.status === "rejected") return result;
|
|
1909
|
+
if (result.status === "fatal") {
|
|
1910
|
+
this.#terminal = {
|
|
1911
|
+
phase: "failed",
|
|
1912
|
+
error: result.error
|
|
1913
|
+
};
|
|
1914
|
+
return result;
|
|
1915
|
+
}
|
|
1916
|
+
this.#lockedType ??= type;
|
|
1917
|
+
this.#processed += 1;
|
|
1918
|
+
const fountainState = this.#fountain.state;
|
|
1919
|
+
if (fountainState.phase === "complete") this.#terminal = {
|
|
1920
|
+
phase: "complete",
|
|
1921
|
+
value: {
|
|
1922
|
+
type,
|
|
1923
|
+
message: fountainState.value
|
|
1924
|
+
}
|
|
1925
|
+
};
|
|
1926
|
+
return result;
|
|
1927
|
+
}
|
|
1928
|
+
/** Moves the session to `failed` with a built error and returns `fatal`. */
|
|
1929
|
+
#fail(info) {
|
|
1930
|
+
return this.#fatal(new UrError(info));
|
|
1931
|
+
}
|
|
1932
|
+
/** Moves the session to `failed` with an existing error and returns `fatal`. */
|
|
1933
|
+
#fatal(error) {
|
|
1934
|
+
this.#terminal = {
|
|
1935
|
+
phase: "failed",
|
|
1936
|
+
error
|
|
1937
|
+
};
|
|
1938
|
+
return {
|
|
1939
|
+
status: "fatal",
|
|
1940
|
+
error
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
/** Current session state. */
|
|
1944
|
+
get state() {
|
|
1945
|
+
const terminal = this.#terminal;
|
|
1946
|
+
if (terminal !== void 0) return terminal.phase === "complete" ? {
|
|
1947
|
+
phase: "complete",
|
|
1948
|
+
value: terminal.value
|
|
1949
|
+
} : {
|
|
1950
|
+
phase: "failed",
|
|
1951
|
+
error: terminal.error
|
|
1952
|
+
};
|
|
1953
|
+
const fountain = this.#fountain.state;
|
|
1954
|
+
return fountain.phase === "collecting" ? {
|
|
1955
|
+
phase: "collecting",
|
|
1956
|
+
progress: fountain.progress
|
|
1957
|
+
} : { phase: "empty" };
|
|
1958
|
+
}
|
|
1959
|
+
/**
|
|
1960
|
+
* Progress snapshot (`empty` reports all zeros; a session completed via a single-part URI reports
|
|
1961
|
+
* `K = 1`).
|
|
1962
|
+
*/
|
|
1963
|
+
get progress() {
|
|
1964
|
+
const base = this.#fountain.progress;
|
|
1965
|
+
if (base.fragmentCount === 0 && this.#terminal?.phase === "complete") return {
|
|
1966
|
+
fragmentCount: 1,
|
|
1967
|
+
rank: 1,
|
|
1968
|
+
recovered: 1,
|
|
1969
|
+
processed: this.#processed,
|
|
1970
|
+
ratio: 1
|
|
1971
|
+
};
|
|
1972
|
+
return {
|
|
1973
|
+
fragmentCount: base.fragmentCount,
|
|
1974
|
+
rank: base.rank,
|
|
1975
|
+
recovered: base.recovered,
|
|
1976
|
+
processed: this.#processed,
|
|
1977
|
+
ratio: base.ratio
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
/** Fragment indexes of the most recent `accepted`/`duplicate` part. */
|
|
1981
|
+
get lastIndexes() {
|
|
1982
|
+
return this.#fountain.lastIndexes;
|
|
1983
|
+
}
|
|
1984
|
+
/** Returns the session to `empty`; limits and the `accept` list are kept. */
|
|
1985
|
+
reset() {
|
|
1986
|
+
this.#fountain = new FountainDecoder({ limits: this.#limits });
|
|
1987
|
+
this.#lockedType = void 0;
|
|
1988
|
+
this.#terminal = void 0;
|
|
1989
|
+
this.#processed = 0;
|
|
1990
|
+
}
|
|
1991
|
+
};
|
|
1992
|
+
//#endregion
|
|
1993
|
+
export { checksum as C, WORDS as S, fail as T, canonicalizeByteword as _, parseUr as a, BYTEMOJIS as b, decodePart as c, FountainDecoder as d, DEFAULT_LIMITS as f, bytewordsIdentifier as g, bytewordsEncodedLength as h, toQrString as i, encodePart as l, bytewordsChecksum as m, UrEncoder as n, isUrType as o, bytemojiIdentifier as p, encodeUr as r, parseUrType as s, UrDecoder as t, FountainEncoder as u, decodeBytewords as v, UrError as w, MINIMALS as x, encodeBytewords as y };
|