@did-btcr2/api 0.7.0 → 0.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 +131 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/browser.js +157 -583
- package/dist/browser.mjs +157 -583
- package/package.json +4 -4
package/dist/browser.js
CHANGED
|
@@ -225,7 +225,7 @@ var BTCR2 = (() => {
|
|
|
225
225
|
"../../node_modules/.pnpm/buffer@5.7.1/node_modules/buffer/index.js"(exports) {
|
|
226
226
|
"use strict";
|
|
227
227
|
init_shim();
|
|
228
|
-
var
|
|
228
|
+
var base647 = require_base64_js();
|
|
229
229
|
var ieee754 = require_ieee754();
|
|
230
230
|
var customInspectSymbol = typeof Symbol === "function" && typeof Symbol["for"] === "function" ? Symbol["for"]("nodejs.util.inspect.custom") : null;
|
|
231
231
|
exports.Buffer = Buffer3;
|
|
@@ -926,9 +926,9 @@ var BTCR2 = (() => {
|
|
|
926
926
|
};
|
|
927
927
|
function base64Slice(buf2, start2, end) {
|
|
928
928
|
if (start2 === 0 && end === buf2.length) {
|
|
929
|
-
return
|
|
929
|
+
return base647.fromByteArray(buf2);
|
|
930
930
|
} else {
|
|
931
|
-
return
|
|
931
|
+
return base647.fromByteArray(buf2.slice(start2, end));
|
|
932
932
|
}
|
|
933
933
|
}
|
|
934
934
|
function utf8Slice2(buf2, start2, end) {
|
|
@@ -1563,7 +1563,7 @@ var BTCR2 = (() => {
|
|
|
1563
1563
|
return byteArray;
|
|
1564
1564
|
}
|
|
1565
1565
|
function base64ToBytes(str) {
|
|
1566
|
-
return
|
|
1566
|
+
return base647.toByteArray(base64clean(str));
|
|
1567
1567
|
}
|
|
1568
1568
|
function blitBuffer(src6, dst, offset, length9) {
|
|
1569
1569
|
for (var i5 = 0; i5 < length9; ++i5) {
|
|
@@ -58469,11 +58469,11 @@ ${[...listenStats.errors.entries()].map(([addr, err]) => {
|
|
|
58469
58469
|
return "";
|
|
58470
58470
|
}
|
|
58471
58471
|
const dif = fullLength - str.length;
|
|
58472
|
-
const
|
|
58472
|
+
const padding2 = new Array(dif);
|
|
58473
58473
|
for (let i42 = 0; i42 < dif; i42++) {
|
|
58474
|
-
|
|
58474
|
+
padding2[i42] = "0";
|
|
58475
58475
|
}
|
|
58476
|
-
const paddingString =
|
|
58476
|
+
const paddingString = padding2.join("");
|
|
58477
58477
|
return paddingString.concat(str);
|
|
58478
58478
|
}
|
|
58479
58479
|
var log22;
|
|
@@ -105320,32 +105320,6 @@ a=end-of-candidates
|
|
|
105320
105320
|
};
|
|
105321
105321
|
}
|
|
105322
105322
|
// @__NO_SIDE_EFFECTS__
|
|
105323
|
-
function padding(bits, chr = "=") {
|
|
105324
|
-
anumber2(bits);
|
|
105325
|
-
astr("padding", chr);
|
|
105326
|
-
return {
|
|
105327
|
-
encode(data) {
|
|
105328
|
-
astrArr("padding.encode", data);
|
|
105329
|
-
while (data.length * bits % 8)
|
|
105330
|
-
data.push(chr);
|
|
105331
|
-
return data;
|
|
105332
|
-
},
|
|
105333
|
-
decode(input) {
|
|
105334
|
-
astrArr("padding.decode", input);
|
|
105335
|
-
let end = input.length;
|
|
105336
|
-
if (end * bits % 8)
|
|
105337
|
-
throw new Error("padding: invalid, string should have whole number of bytes");
|
|
105338
|
-
for (; end > 0 && input[end - 1] === chr; end--) {
|
|
105339
|
-
const last = end - 1;
|
|
105340
|
-
const byte = last * bits;
|
|
105341
|
-
if (byte % 8 === 0)
|
|
105342
|
-
throw new Error("padding: invalid, string has too much padding");
|
|
105343
|
-
}
|
|
105344
|
-
return input.slice(0, end);
|
|
105345
|
-
}
|
|
105346
|
-
};
|
|
105347
|
-
}
|
|
105348
|
-
// @__NO_SIDE_EFFECTS__
|
|
105349
105323
|
function normalize(fn) {
|
|
105350
105324
|
afn(fn);
|
|
105351
105325
|
return { encode: (from82) => from82, decode: (to) => fn(to) };
|
|
@@ -105406,7 +105380,7 @@ a=end-of-candidates
|
|
|
105406
105380
|
res.push(2 ** i42);
|
|
105407
105381
|
return res;
|
|
105408
105382
|
})();
|
|
105409
|
-
function convertRadix2(data, from82, to,
|
|
105383
|
+
function convertRadix2(data, from82, to, padding2) {
|
|
105410
105384
|
aArr(data);
|
|
105411
105385
|
if (from82 <= 0 || from82 > 32)
|
|
105412
105386
|
throw new Error(`convertRadix2: wrong from=${from82}`);
|
|
@@ -105436,11 +105410,11 @@ a=end-of-candidates
|
|
|
105436
105410
|
carry &= pow32 - 1;
|
|
105437
105411
|
}
|
|
105438
105412
|
carry = carry << to - pos & mask;
|
|
105439
|
-
if (!
|
|
105413
|
+
if (!padding2 && pos >= from82)
|
|
105440
105414
|
throw new Error("Excess padding");
|
|
105441
|
-
if (!
|
|
105415
|
+
if (!padding2 && carry > 0)
|
|
105442
105416
|
throw new Error(`Non-zero padding: ${carry}`);
|
|
105443
|
-
if (
|
|
105417
|
+
if (padding2 && pos > 0)
|
|
105444
105418
|
res.push(carry >>> 0);
|
|
105445
105419
|
return res;
|
|
105446
105420
|
}
|
|
@@ -105514,24 +105488,6 @@ a=end-of-candidates
|
|
|
105514
105488
|
}
|
|
105515
105489
|
};
|
|
105516
105490
|
}
|
|
105517
|
-
var hasBase64Builtin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toBase64 === "function" && typeof Uint8Array.fromBase64 === "function")();
|
|
105518
|
-
var decodeBase64Builtin = (s2, isUrl) => {
|
|
105519
|
-
astr("base64", s2);
|
|
105520
|
-
const re = isUrl ? /^[A-Za-z0-9=_-]+$/ : /^[A-Za-z0-9=+/]+$/;
|
|
105521
|
-
const alphabet52 = isUrl ? "base64url" : "base64";
|
|
105522
|
-
if (s2.length > 0 && !re.test(s2))
|
|
105523
|
-
throw new Error("invalid base64");
|
|
105524
|
-
return Uint8Array.fromBase64(s2, { alphabet: alphabet52, lastChunkHandling: "strict" });
|
|
105525
|
-
};
|
|
105526
|
-
var base64 = hasBase64Builtin ? {
|
|
105527
|
-
encode(b) {
|
|
105528
|
-
abytes2(b);
|
|
105529
|
-
return b.toBase64();
|
|
105530
|
-
},
|
|
105531
|
-
decode(s2) {
|
|
105532
|
-
return decodeBase64Builtin(s2, false);
|
|
105533
|
-
}
|
|
105534
|
-
} : /* @__PURE__ */ chain(/* @__PURE__ */ radix2(6), /* @__PURE__ */ alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ padding(6), /* @__PURE__ */ join(""));
|
|
105535
105491
|
var base64urlnopad = /* @__PURE__ */ chain(/* @__PURE__ */ radix2(6), /* @__PURE__ */ alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), /* @__PURE__ */ join(""));
|
|
105536
105492
|
var genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => /* @__PURE__ */ chain(/* @__PURE__ */ radix(58), /* @__PURE__ */ alphabet(abc), /* @__PURE__ */ join(""));
|
|
105537
105493
|
var base58 = /* @__PURE__ */ genBase58("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
@@ -109857,7 +109813,7 @@ a=end-of-candidates
|
|
|
109857
109813
|
};
|
|
109858
109814
|
}
|
|
109859
109815
|
// @__NO_SIDE_EFFECTS__
|
|
109860
|
-
function
|
|
109816
|
+
function padding(bits, chr = "=") {
|
|
109861
109817
|
anumber4(bits);
|
|
109862
109818
|
astr2("padding", chr);
|
|
109863
109819
|
return {
|
|
@@ -109938,7 +109894,7 @@ a=end-of-candidates
|
|
|
109938
109894
|
res.push(2 ** i42);
|
|
109939
109895
|
return res;
|
|
109940
109896
|
})();
|
|
109941
|
-
function convertRadix22(data, from82, to,
|
|
109897
|
+
function convertRadix22(data, from82, to, padding2) {
|
|
109942
109898
|
aArr2(data);
|
|
109943
109899
|
if (from82 <= 0 || from82 > 32)
|
|
109944
109900
|
throw new Error(`convertRadix2: wrong from=${from82}`);
|
|
@@ -109968,11 +109924,11 @@ a=end-of-candidates
|
|
|
109968
109924
|
carry &= pow32 - 1;
|
|
109969
109925
|
}
|
|
109970
109926
|
carry = carry << to - pos & mask;
|
|
109971
|
-
if (!
|
|
109927
|
+
if (!padding2 && pos >= from82)
|
|
109972
109928
|
throw new Error("Excess padding");
|
|
109973
|
-
if (!
|
|
109929
|
+
if (!padding2 && carry > 0)
|
|
109974
109930
|
throw new Error(`Non-zero padding: ${carry}`);
|
|
109975
|
-
if (
|
|
109931
|
+
if (padding2 && pos > 0)
|
|
109976
109932
|
res.push(carry >>> 0);
|
|
109977
109933
|
return res;
|
|
109978
109934
|
}
|
|
@@ -110020,8 +109976,8 @@ a=end-of-candidates
|
|
|
110020
109976
|
}
|
|
110021
109977
|
};
|
|
110022
109978
|
}
|
|
110023
|
-
var
|
|
110024
|
-
var
|
|
109979
|
+
var hasBase64Builtin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toBase64 === "function" && typeof Uint8Array.fromBase64 === "function")();
|
|
109980
|
+
var decodeBase64Builtin = (s2, isUrl) => {
|
|
110025
109981
|
astr2("base64", s2);
|
|
110026
109982
|
const re = isUrl ? /^[A-Za-z0-9=_-]+$/ : /^[A-Za-z0-9=+/]+$/;
|
|
110027
109983
|
const alphabet52 = isUrl ? "base64url" : "base64";
|
|
@@ -110029,15 +109985,15 @@ a=end-of-candidates
|
|
|
110029
109985
|
throw new Error("invalid base64");
|
|
110030
109986
|
return Uint8Array.fromBase64(s2, { alphabet: alphabet52, lastChunkHandling: "strict" });
|
|
110031
109987
|
};
|
|
110032
|
-
var
|
|
109988
|
+
var base64 = hasBase64Builtin ? {
|
|
110033
109989
|
encode(b) {
|
|
110034
109990
|
abytes4(b);
|
|
110035
109991
|
return b.toBase64();
|
|
110036
109992
|
},
|
|
110037
109993
|
decode(s2) {
|
|
110038
|
-
return
|
|
109994
|
+
return decodeBase64Builtin(s2, false);
|
|
110039
109995
|
}
|
|
110040
|
-
} : /* @__PURE__ */ chain2(/* @__PURE__ */ radix22(6), /* @__PURE__ */ alphabet2("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */
|
|
109996
|
+
} : /* @__PURE__ */ chain2(/* @__PURE__ */ radix22(6), /* @__PURE__ */ alphabet2("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ padding(6), /* @__PURE__ */ join2(""));
|
|
110041
109997
|
var genBase582 = /* @__NO_SIDE_EFFECTS__ */ (abc) => /* @__PURE__ */ chain2(/* @__PURE__ */ radix3(58), /* @__PURE__ */ alphabet2(abc), /* @__PURE__ */ join2(""));
|
|
110042
109998
|
var base582 = /* @__PURE__ */ genBase582("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
110043
109999
|
var BECH_ALPHABET2 = /* @__PURE__ */ chain2(/* @__PURE__ */ alphabet2("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ join2(""));
|
|
@@ -112263,123 +112219,23 @@ a=end-of-candidates
|
|
|
112263
112219
|
}
|
|
112264
112220
|
return s2;
|
|
112265
112221
|
}
|
|
112266
|
-
function hexToHash(hex22) {
|
|
112267
|
-
validateHex(hex22, true);
|
|
112268
|
-
const hash32 = new Uint8Array(HASH_BYTE_LENGTH);
|
|
112269
|
-
for (let i42 = 0; i42 < HASH_BYTE_LENGTH; i42++) {
|
|
112270
|
-
hash32[i42] = Number.parseInt(hex22.substring(i42 * 2, i42 * 2 + 2), 16);
|
|
112271
|
-
}
|
|
112272
|
-
return hash32;
|
|
112273
|
-
}
|
|
112274
|
-
function bigIntToHex(value2, padded) {
|
|
112275
|
-
const s2 = value2.toString(16);
|
|
112276
|
-
return padded ? s2.padStart(HASH_HEX_LENGTH, "0") : s2;
|
|
112277
|
-
}
|
|
112278
|
-
function hexToBigInt(hex22, padded) {
|
|
112279
|
-
validateHex(hex22, padded);
|
|
112280
|
-
return BigInt(`0x${hex22}`);
|
|
112281
|
-
}
|
|
112282
112222
|
function hashesEqual(a2, b) {
|
|
112283
112223
|
if (!isValidHash(a2) || !isValidHash(b))
|
|
112284
112224
|
return false;
|
|
112285
112225
|
return equalBytes(a2, b);
|
|
112286
112226
|
}
|
|
112287
|
-
function
|
|
112227
|
+
function hashToBase64Url(hash32) {
|
|
112288
112228
|
validateHash(hash32);
|
|
112289
|
-
return
|
|
112229
|
+
return base64urlnopad.encode(hash32);
|
|
112290
112230
|
}
|
|
112291
|
-
function
|
|
112292
|
-
const hash32 =
|
|
112231
|
+
function base64UrlToHash(b64u) {
|
|
112232
|
+
const hash32 = base64urlnopad.decode(b64u);
|
|
112293
112233
|
if (hash32.length !== HASH_BYTE_LENGTH) {
|
|
112294
|
-
throw new RangeError(`Invalid
|
|
112234
|
+
throw new RangeError(`Invalid base64url hash: expected ${HASH_BYTE_LENGTH} decoded bytes, got ${hash32.length}`);
|
|
112295
112235
|
}
|
|
112296
112236
|
return hash32;
|
|
112297
112237
|
}
|
|
112298
|
-
function bigIntToBase64(value2, padded) {
|
|
112299
|
-
let bytes32 = bigIntToHash(value2);
|
|
112300
|
-
if (!padded) {
|
|
112301
|
-
const firstNonZero = bytes32.findIndex((b) => b !== 0);
|
|
112302
|
-
bytes32 = firstNonZero === -1 ? new Uint8Array(1) : bytes32.slice(firstNonZero);
|
|
112303
|
-
}
|
|
112304
|
-
return base64.encode(bytes32);
|
|
112305
|
-
}
|
|
112306
|
-
function base64ToBigInt(b64, padded) {
|
|
112307
|
-
const bytes32 = base64.decode(b64);
|
|
112308
|
-
if (padded && bytes32.length !== HASH_BYTE_LENGTH) {
|
|
112309
|
-
throw new RangeError(`Invalid padded base64 bigint: expected ${HASH_BYTE_LENGTH} decoded bytes, got ${bytes32.length}`);
|
|
112310
|
-
}
|
|
112311
|
-
if (bytes32.length > HASH_BYTE_LENGTH) {
|
|
112312
|
-
throw new RangeError(`Value exceeds ${HASH_BYTE_LENGTH} bytes`);
|
|
112313
|
-
}
|
|
112314
|
-
let value2 = 0n;
|
|
112315
|
-
for (const byte of bytes32) {
|
|
112316
|
-
value2 = value2 << 8n | BigInt(byte);
|
|
112317
|
-
}
|
|
112318
|
-
return value2;
|
|
112319
|
-
}
|
|
112320
|
-
var HEX_RE = /^[0-9A-Fa-f]+$/;
|
|
112321
|
-
function validateHex(s2, requireHashLength) {
|
|
112322
|
-
const len = s2.length;
|
|
112323
|
-
const minLen = requireHashLength ? HASH_HEX_LENGTH : 1;
|
|
112324
|
-
if (len < minLen || len > HASH_HEX_LENGTH || !HEX_RE.test(s2)) {
|
|
112325
|
-
throw new RangeError(`Invalid hex string: expected ${requireHashLength ? HASH_HEX_LENGTH : "1-" + HASH_HEX_LENGTH} hex characters, got ${len}`);
|
|
112326
|
-
}
|
|
112327
|
-
}
|
|
112328
112238
|
init_shim2();
|
|
112329
|
-
var BaseNode = class {
|
|
112330
|
-
#index;
|
|
112331
|
-
#depth;
|
|
112332
|
-
constructor(index, depth) {
|
|
112333
|
-
this.#index = index;
|
|
112334
|
-
this.#depth = depth;
|
|
112335
|
-
}
|
|
112336
|
-
get index() {
|
|
112337
|
-
return this.#index;
|
|
112338
|
-
}
|
|
112339
|
-
get depth() {
|
|
112340
|
-
return this.#depth;
|
|
112341
|
-
}
|
|
112342
|
-
};
|
|
112343
|
-
var LeafNode = class extends BaseNode {
|
|
112344
|
-
#hash = null;
|
|
112345
|
-
get hash() {
|
|
112346
|
-
return this.#hash;
|
|
112347
|
-
}
|
|
112348
|
-
set hash(value2) {
|
|
112349
|
-
if (this.#hash !== null) {
|
|
112350
|
-
throw new RangeError("Leaf hash already set");
|
|
112351
|
-
}
|
|
112352
|
-
this.#hash = value2;
|
|
112353
|
-
}
|
|
112354
|
-
reset() {
|
|
112355
|
-
this.#hash = null;
|
|
112356
|
-
}
|
|
112357
|
-
};
|
|
112358
|
-
var ParentNode = class extends BaseNode {
|
|
112359
|
-
#left;
|
|
112360
|
-
#right;
|
|
112361
|
-
constructor(index, depth, left, right) {
|
|
112362
|
-
super(index, depth);
|
|
112363
|
-
this.#left = left;
|
|
112364
|
-
this.#right = right;
|
|
112365
|
-
}
|
|
112366
|
-
get left() {
|
|
112367
|
-
return this.#left;
|
|
112368
|
-
}
|
|
112369
|
-
set left(node) {
|
|
112370
|
-
this.#left = node;
|
|
112371
|
-
}
|
|
112372
|
-
get right() {
|
|
112373
|
-
return this.#right;
|
|
112374
|
-
}
|
|
112375
|
-
set right(node) {
|
|
112376
|
-
this.#right = node;
|
|
112377
|
-
}
|
|
112378
|
-
reset() {
|
|
112379
|
-
this.#left.reset();
|
|
112380
|
-
this.#right.reset();
|
|
112381
|
-
}
|
|
112382
|
-
};
|
|
112383
112239
|
init_shim2();
|
|
112384
112240
|
var ValidationState;
|
|
112385
112241
|
(function(ValidationState2) {
|
|
@@ -112387,375 +112243,80 @@ a=end-of-candidates
|
|
|
112387
112243
|
ValidationState2[ValidationState2["Valid"] = 1] = "Valid";
|
|
112388
112244
|
ValidationState2[ValidationState2["Invalid"] = 2] = "Invalid";
|
|
112389
112245
|
})(ValidationState || (ValidationState = {}));
|
|
112390
|
-
var SMTProof = class _SMTProof {
|
|
112391
|
-
#converge;
|
|
112392
|
-
#hashes;
|
|
112393
|
-
constructor(converge, hashes2) {
|
|
112394
|
-
this.#converge = converge;
|
|
112395
|
-
this.#hashes = hashes2;
|
|
112396
|
-
}
|
|
112397
|
-
/** Converge bitmap: bit `i` set means a sibling hash exists at depth `256 - i - 1`. */
|
|
112398
|
-
get converge() {
|
|
112399
|
-
return this.#converge;
|
|
112400
|
-
}
|
|
112401
|
-
/** Sibling hashes at converge points, ordered leaf-to-root. */
|
|
112402
|
-
get hashes() {
|
|
112403
|
-
return this.#hashes;
|
|
112404
|
-
}
|
|
112405
|
-
/**
|
|
112406
|
-
* Verify this proof for a single leaf.
|
|
112407
|
-
*
|
|
112408
|
-
* @param index - Leaf index in the 256-bit key space.
|
|
112409
|
-
* @param candidateHash - Expected leaf hash.
|
|
112410
|
-
* @param rootHash - Expected root hash.
|
|
112411
|
-
* @returns `true` if the proof is valid.
|
|
112412
|
-
*/
|
|
112413
|
-
isValid(index, candidateHash, rootHash) {
|
|
112414
|
-
return this.#validate(index, candidateHash, rootHash);
|
|
112415
|
-
}
|
|
112416
|
-
/**
|
|
112417
|
-
* Batch-validate multiple proofs against the same root hash.
|
|
112418
|
-
*
|
|
112419
|
-
* Caches intermediate (partial) proofs so that subsequent candidates
|
|
112420
|
-
* sharing an ancestor path can short-circuit once a cached match is found.
|
|
112421
|
-
*
|
|
112422
|
-
* @yields One {@link SMTProofResult} per candidate.
|
|
112423
|
-
*/
|
|
112424
|
-
static *isValidBatch(candidates, rootHash) {
|
|
112425
|
-
const cache42 = /* @__PURE__ */ new Map();
|
|
112426
|
-
for (const candidate of candidates) {
|
|
112427
|
-
const added = [];
|
|
112428
|
-
const { index } = candidate;
|
|
112429
|
-
const valid = candidate.proof.#validate(index | OUTER_BIT, candidate.hash, rootHash, (nodeIndex, partial) => {
|
|
112430
|
-
const cached = cache42.get(nodeIndex);
|
|
112431
|
-
if (cached === void 0) {
|
|
112432
|
-
cache42.set(nodeIndex, partial);
|
|
112433
|
-
added.push(nodeIndex);
|
|
112434
|
-
return ValidationState.Pending;
|
|
112435
|
-
}
|
|
112436
|
-
if (hashesEqual(partial.hash, cached.hash) && partial.converge === cached.converge && partial.hashes.length === cached.hashes.length && partial.hashes.every((h, i42) => hashesEqual(h, cached.hashes[i42]))) {
|
|
112437
|
-
return ValidationState.Valid;
|
|
112438
|
-
}
|
|
112439
|
-
return ValidationState.Invalid;
|
|
112440
|
-
});
|
|
112441
|
-
if (!valid) {
|
|
112442
|
-
for (const key of added)
|
|
112443
|
-
cache42.delete(key);
|
|
112444
|
-
}
|
|
112445
|
-
yield { index, valid, additional: candidate.additional };
|
|
112446
|
-
}
|
|
112447
|
-
}
|
|
112448
|
-
/**
|
|
112449
|
-
* Export to JSON.
|
|
112450
|
-
* @param base64 - Use base64 encoding instead of hex (default: `false`).
|
|
112451
|
-
* @param compact - Omit whitespace (default: `true`).
|
|
112452
|
-
*/
|
|
112453
|
-
toJSON(base6462 = false, compact = true) {
|
|
112454
|
-
const convergeStr = base6462 ? bigIntToBase64(this.#converge, false) : bigIntToHex(this.#converge, false);
|
|
112455
|
-
const hashStrs = this.#hashes.map((h) => base6462 ? hashToBase64(h) : hashToHex(h));
|
|
112456
|
-
const obj = { converge: convergeStr, hashes: hashStrs };
|
|
112457
|
-
return JSON.stringify(obj, null, compact ? 0 : 2);
|
|
112458
|
-
}
|
|
112459
|
-
/**
|
|
112460
|
-
* Import from JSON.
|
|
112461
|
-
* @param json - JSON string.
|
|
112462
|
-
* @param base64 - Parse base64 instead of hex (default: `false`).
|
|
112463
|
-
*/
|
|
112464
|
-
static fromJSON(json, base6462 = false) {
|
|
112465
|
-
const raw = JSON.parse(json);
|
|
112466
|
-
if (typeof raw?.converge !== "string" || !Array.isArray(raw.hashes)) {
|
|
112467
|
-
throw new RangeError("Invalid SMTProof JSON: expected { converge, hashes }");
|
|
112468
|
-
}
|
|
112469
|
-
const converge = base6462 ? base64ToBigInt(raw.converge, false) : hexToBigInt(raw.converge, false);
|
|
112470
|
-
const hashes2 = raw.hashes.map((h) => base6462 ? base64ToHash(h) : hexToHash(h));
|
|
112471
|
-
return new _SMTProof(converge, hashes2);
|
|
112472
|
-
}
|
|
112473
|
-
/**
|
|
112474
|
-
* Export to compact binary format.
|
|
112475
|
-
*
|
|
112476
|
-
* Layout: `[convergeZeroCount : 1] [truncatedConverge : 32-zc] [hashCount : 1] [hashes : N*32]`
|
|
112477
|
-
*/
|
|
112478
|
-
toBinary() {
|
|
112479
|
-
const convergeBin = bigIntToHash(this.#converge);
|
|
112480
|
-
let zc = 0;
|
|
112481
|
-
while (zc < HASH_BYTE_LENGTH && convergeBin[zc] === 0)
|
|
112482
|
-
zc++;
|
|
112483
|
-
const truncated = convergeBin.slice(zc);
|
|
112484
|
-
const hashCount = this.#hashes.length;
|
|
112485
|
-
const totalBytes = 1 + truncated.length + 1 + hashCount * HASH_BYTE_LENGTH;
|
|
112486
|
-
const out = new Uint8Array(totalBytes);
|
|
112487
|
-
let pos = 0;
|
|
112488
|
-
out[pos++] = zc;
|
|
112489
|
-
out.set(truncated, pos);
|
|
112490
|
-
pos += truncated.length;
|
|
112491
|
-
out[pos++] = hashCount;
|
|
112492
|
-
for (const h of this.#hashes) {
|
|
112493
|
-
out.set(h, pos);
|
|
112494
|
-
pos += HASH_BYTE_LENGTH;
|
|
112495
|
-
}
|
|
112496
|
-
return out;
|
|
112497
|
-
}
|
|
112498
|
-
/**
|
|
112499
|
-
* Import from compact binary format.
|
|
112500
|
-
* Accepts any sync or async byte iterable (e.g. `Uint8Array`, `ReadableStream`).
|
|
112501
|
-
*/
|
|
112502
|
-
static async fromBinary(source) {
|
|
112503
|
-
const iter = Symbol.iterator in source ? source[Symbol.iterator]() : source[Symbol.asyncIterator]();
|
|
112504
|
-
async function readBytes(n2) {
|
|
112505
|
-
const buf2 = new Uint8Array(n2);
|
|
112506
|
-
for (let i42 = 0; i42 < n2; i42++) {
|
|
112507
|
-
const r2 = await iter.next();
|
|
112508
|
-
if (r2.done)
|
|
112509
|
-
throw new Error("Unexpected end of binary source");
|
|
112510
|
-
buf2[i42] = r2.value;
|
|
112511
|
-
}
|
|
112512
|
-
return buf2;
|
|
112513
|
-
}
|
|
112514
|
-
const zc = (await readBytes(1))[0];
|
|
112515
|
-
const convergeBin = new Uint8Array(HASH_BYTE_LENGTH);
|
|
112516
|
-
convergeBin.set(await readBytes(HASH_BYTE_LENGTH - zc), zc);
|
|
112517
|
-
const hashCount = (await readBytes(1))[0];
|
|
112518
|
-
const hashes2 = new Array(hashCount);
|
|
112519
|
-
for (let i42 = 0; i42 < hashCount; i42++) {
|
|
112520
|
-
hashes2[i42] = await readBytes(HASH_BYTE_LENGTH);
|
|
112521
|
-
}
|
|
112522
|
-
return new _SMTProof(hashToBigInt(convergeBin), hashes2);
|
|
112523
|
-
}
|
|
112524
|
-
#validate(index, candidateHash, rootHash, onMerge) {
|
|
112525
|
-
let nodeIndex = index;
|
|
112526
|
-
let nodeHash = candidateHash;
|
|
112527
|
-
let remaining = this.#converge;
|
|
112528
|
-
const hashes2 = this.#hashes;
|
|
112529
|
-
let hi = 0;
|
|
112530
|
-
const leftPad = [];
|
|
112531
|
-
const rightPad = [];
|
|
112532
|
-
const finalizePadding = () => {
|
|
112533
|
-
if (leftPad.length > 0 || rightPad.length > 0) {
|
|
112534
|
-
nodeHash = blockHash(new Uint8Array(leftPad), nodeHash, new Uint8Array(rightPad));
|
|
112535
|
-
leftPad.length = 0;
|
|
112536
|
-
rightPad.length = 0;
|
|
112537
|
-
}
|
|
112538
|
-
};
|
|
112539
|
-
let state = ValidationState.Pending;
|
|
112540
|
-
for (let i42 = 0; state === ValidationState.Pending && i42 < HASH_BIT_LENGTH; i42++) {
|
|
112541
|
-
const isLeft = (nodeIndex & 1n) === 0n;
|
|
112542
|
-
nodeIndex >>= 1n;
|
|
112543
|
-
const bit = BITS[i42];
|
|
112544
|
-
if ((remaining & bit) !== 0n) {
|
|
112545
|
-
remaining ^= bit;
|
|
112546
|
-
finalizePadding();
|
|
112547
|
-
if (hi >= hashes2.length) {
|
|
112548
|
-
state = ValidationState.Invalid;
|
|
112549
|
-
} else {
|
|
112550
|
-
const peer = hashes2[hi++];
|
|
112551
|
-
nodeHash = isLeft ? blockHash(nodeHash, peer) : blockHash(peer, nodeHash);
|
|
112552
|
-
if (onMerge !== void 0) {
|
|
112553
|
-
state = onMerge(nodeIndex, {
|
|
112554
|
-
hash: nodeHash,
|
|
112555
|
-
converge: remaining,
|
|
112556
|
-
hashes: hashes2.slice(hi)
|
|
112557
|
-
});
|
|
112558
|
-
}
|
|
112559
|
-
}
|
|
112560
|
-
} else {
|
|
112561
|
-
const depth = HASH_BIT_LENGTH - i42 - 1;
|
|
112562
|
-
if (isLeft) {
|
|
112563
|
-
rightPad.push(depth);
|
|
112564
|
-
} else {
|
|
112565
|
-
leftPad.unshift(depth);
|
|
112566
|
-
}
|
|
112567
|
-
}
|
|
112568
|
-
}
|
|
112569
|
-
finalizePadding();
|
|
112570
|
-
if (state === ValidationState.Pending) {
|
|
112571
|
-
state = hi === hashes2.length && hashesEqual(nodeHash, rootHash) ? ValidationState.Valid : ValidationState.Invalid;
|
|
112572
|
-
}
|
|
112573
|
-
return state === ValidationState.Valid;
|
|
112574
|
-
}
|
|
112575
|
-
};
|
|
112576
112246
|
init_shim2();
|
|
112577
|
-
|
|
112578
|
-
|
|
112579
|
-
|
|
112580
|
-
|
|
112581
|
-
|
|
112582
|
-
|
|
112583
|
-
|
|
112584
|
-
|
|
112585
|
-
|
|
112586
|
-
|
|
112587
|
-
|
|
112588
|
-
}
|
|
112589
|
-
get allowNonInclusion() {
|
|
112590
|
-
return this.#allowNonInclusion;
|
|
112247
|
+
init_shim2();
|
|
112248
|
+
var TREE_DEPTH = HASH_BIT_LENGTH;
|
|
112249
|
+
function bitAt(index, position) {
|
|
112250
|
+
return Number(index >> BigInt(position) & 1n);
|
|
112251
|
+
}
|
|
112252
|
+
var CACHED_ZERO = (() => {
|
|
112253
|
+
const arr = new Array(TREE_DEPTH + 1);
|
|
112254
|
+
let z = new Uint8Array(HASH_BYTE_LENGTH);
|
|
112255
|
+
for (let h = 0; h <= TREE_DEPTH; h++) {
|
|
112256
|
+
z = blockHash(z, z);
|
|
112257
|
+
arr[h] = z;
|
|
112591
112258
|
}
|
|
112592
|
-
|
|
112593
|
-
|
|
112594
|
-
|
|
112595
|
-
|
|
112596
|
-
return
|
|
112597
|
-
|
|
112598
|
-
|
|
112599
|
-
|
|
112600
|
-
|
|
112601
|
-
|
|
112602
|
-
|
|
112603
|
-
|
|
112604
|
-
|
|
112605
|
-
|
|
112606
|
-
|
|
112607
|
-
|
|
112608
|
-
|
|
112609
|
-
|
|
112610
|
-
|
|
112611
|
-
|
|
112612
|
-
|
|
112613
|
-
|
|
112259
|
+
return arr;
|
|
112260
|
+
})();
|
|
112261
|
+
function subtreeHash(leaves, height) {
|
|
112262
|
+
if (leaves.length === 0)
|
|
112263
|
+
return CACHED_ZERO[height];
|
|
112264
|
+
if (height === 0)
|
|
112265
|
+
return leaves[0].leaf;
|
|
112266
|
+
const bit = TREE_DEPTH - height;
|
|
112267
|
+
const left = [];
|
|
112268
|
+
const right = [];
|
|
112269
|
+
for (const e2 of leaves)
|
|
112270
|
+
(bitAt(e2.index, bit) === 0 ? left : right).push(e2);
|
|
112271
|
+
return blockHash(subtreeHash(left, height - 1), subtreeHash(right, height - 1));
|
|
112272
|
+
}
|
|
112273
|
+
function zeroHashRoot(leaves) {
|
|
112274
|
+
return subtreeHash(leaves, TREE_DEPTH);
|
|
112275
|
+
}
|
|
112276
|
+
function generateZeroHashProof(leaves, targetIndex) {
|
|
112277
|
+
let collapsed = 0n;
|
|
112278
|
+
const hashes2 = [];
|
|
112279
|
+
for (let height = 1; height <= TREE_DEPTH; height++) {
|
|
112280
|
+
const bit = TREE_DEPTH - height;
|
|
112281
|
+
const siblingLeaves = [];
|
|
112282
|
+
for (const e2 of leaves) {
|
|
112283
|
+
if (e2.index === targetIndex)
|
|
112614
112284
|
continue;
|
|
112615
|
-
|
|
112616
|
-
let
|
|
112617
|
-
|
|
112618
|
-
|
|
112619
|
-
|
|
112620
|
-
let commonIndex = 0n;
|
|
112621
|
-
let commonDepth = 0;
|
|
112622
|
-
let done = false;
|
|
112623
|
-
while (!done) {
|
|
112624
|
-
const bit = BITS[commonDepth];
|
|
112625
|
-
const indexBit = index & bit;
|
|
112626
|
-
const isLeft = indexBit === 0n;
|
|
112627
|
-
if (commonDepth === node.depth) {
|
|
112628
|
-
if (node instanceof ParentNode) {
|
|
112629
|
-
const parent = node;
|
|
112630
|
-
if (isLeft) {
|
|
112631
|
-
node = parent.left;
|
|
112632
|
-
replaceNode = (n2) => {
|
|
112633
|
-
parent.left = n2;
|
|
112634
|
-
};
|
|
112635
|
-
} else {
|
|
112636
|
-
node = parent.right;
|
|
112637
|
-
replaceNode = (n2) => {
|
|
112638
|
-
parent.right = n2;
|
|
112639
|
-
};
|
|
112640
|
-
}
|
|
112641
|
-
} else {
|
|
112642
|
-
throw new RangeError("Duplicate index");
|
|
112643
|
-
}
|
|
112644
|
-
} else if ((node.index & bit) === indexBit) {
|
|
112645
|
-
commonIndex |= indexBit;
|
|
112646
|
-
commonDepth++;
|
|
112647
|
-
} else {
|
|
112648
|
-
replaceNode(new ParentNode(commonIndex, commonDepth, isLeft ? leaf : node, isLeft ? node : leaf));
|
|
112649
|
-
done = true;
|
|
112285
|
+
let sharesLowerPath = true;
|
|
112286
|
+
for (let lower = 0; lower < bit; lower++) {
|
|
112287
|
+
if (bitAt(e2.index, lower) !== bitAt(targetIndex, lower)) {
|
|
112288
|
+
sharesLowerPath = false;
|
|
112289
|
+
break;
|
|
112650
112290
|
}
|
|
112651
112291
|
}
|
|
112292
|
+
if (sharesLowerPath && bitAt(e2.index, bit) !== bitAt(targetIndex, bit))
|
|
112293
|
+
siblingLeaves.push(e2);
|
|
112652
112294
|
}
|
|
112653
|
-
|
|
112654
|
-
|
|
112655
|
-
|
|
112656
|
-
|
|
112657
|
-
* Each leaf's hash can only be set once (until {@link reset}).
|
|
112658
|
-
*/
|
|
112659
|
-
setHash(index, hash32) {
|
|
112660
|
-
this.#checkNotFinalized();
|
|
112661
|
-
validateHash(hash32);
|
|
112662
|
-
let node = this.#root;
|
|
112663
|
-
if (node === null)
|
|
112664
|
-
throw new RangeError("Empty SMT");
|
|
112665
|
-
while (node instanceof ParentNode) {
|
|
112666
|
-
node = (index & BITS[node.depth]) === 0n ? node.left : node.right;
|
|
112667
|
-
}
|
|
112668
|
-
if (node.index !== index)
|
|
112669
|
-
throw new RangeError("Index not found");
|
|
112670
|
-
node.hash = hash32;
|
|
112671
|
-
}
|
|
112672
|
-
// -----------------------------------------------------------------------
|
|
112673
|
-
// Finalize phase
|
|
112674
|
-
// -----------------------------------------------------------------------
|
|
112675
|
-
/**
|
|
112676
|
-
* Compute root hash and generate all proofs in a single recursive pass.
|
|
112677
|
-
* Must be called after all hashes are set.
|
|
112678
|
-
*/
|
|
112679
|
-
finalize() {
|
|
112680
|
-
if (this.#root === null) {
|
|
112681
|
-
this.#rootHash = NULL_HASH;
|
|
112682
|
-
return;
|
|
112295
|
+
if (siblingLeaves.length === 0) {
|
|
112296
|
+
collapsed |= 1n << BigInt(bit);
|
|
112297
|
+
} else {
|
|
112298
|
+
hashes2.push(subtreeHash(siblingLeaves, height - 1));
|
|
112683
112299
|
}
|
|
112684
|
-
const result = this.#finalizeStep(this.#root, 0n, 0);
|
|
112685
|
-
this.#rootHash = result.hash;
|
|
112686
|
-
result.saveProofs([]);
|
|
112687
|
-
}
|
|
112688
|
-
/** Retrieve the proof for an index. Only valid after {@link finalize}. */
|
|
112689
|
-
proof(index) {
|
|
112690
|
-
const p2 = this.#proofs.get(index);
|
|
112691
|
-
if (p2 === void 0)
|
|
112692
|
-
throw new RangeError("Proof not found");
|
|
112693
|
-
return p2;
|
|
112694
112300
|
}
|
|
112695
|
-
|
|
112696
|
-
|
|
112697
|
-
|
|
112698
|
-
|
|
112699
|
-
|
|
112700
|
-
|
|
112701
|
-
|
|
112702
|
-
|
|
112703
|
-
|
|
112704
|
-
|
|
112705
|
-
#checkNotFinalized() {
|
|
112706
|
-
if (this.#rootHash !== null)
|
|
112707
|
-
throw new Error("SMT already finalized");
|
|
112708
|
-
}
|
|
112709
|
-
/**
|
|
112710
|
-
* Perform a single step of finalization on the given node.
|
|
112711
|
-
* @param {Node} node The node to finalize.
|
|
112712
|
-
* @param {bigint} parentConverge The convergence value from the parent node.
|
|
112713
|
-
* @param {number} depth The current depth in the tree.
|
|
112714
|
-
* @returns {FinalizeStepResult} The result of the finalization step.
|
|
112715
|
-
*/
|
|
112716
|
-
#finalizeStep(node, parentConverge, depth) {
|
|
112717
|
-
const converge = parentConverge | BITS[HASH_BIT_LENGTH - depth];
|
|
112718
|
-
let hash32;
|
|
112719
|
-
let saveProofs;
|
|
112720
|
-
if (node instanceof ParentNode) {
|
|
112721
|
-
const childDepth = node.depth + 1;
|
|
112722
|
-
const leftResult = this.#finalizeStep(node.left, converge, childDepth);
|
|
112723
|
-
const rightResult = this.#finalizeStep(node.right, converge, childDepth);
|
|
112724
|
-
hash32 = blockHash(leftResult.hash, rightResult.hash);
|
|
112725
|
-
saveProofs = (hashes2) => {
|
|
112726
|
-
const leftHashes = hashes2;
|
|
112727
|
-
const rightHashes = hashes2.slice();
|
|
112728
|
-
leftHashes.unshift(rightResult.hash);
|
|
112729
|
-
rightHashes.unshift(leftResult.hash);
|
|
112730
|
-
leftResult.saveProofs(leftHashes);
|
|
112731
|
-
rightResult.saveProofs(rightHashes);
|
|
112732
|
-
};
|
|
112301
|
+
return { collapsed, hashes: hashes2 };
|
|
112302
|
+
}
|
|
112303
|
+
function verifyZeroHash(collapsed, hashes2, index, candidate, root) {
|
|
112304
|
+
let acc = candidate;
|
|
112305
|
+
let hashPtr = 0;
|
|
112306
|
+
for (let n2 = 0; n2 < TREE_DEPTH; n2++) {
|
|
112307
|
+
const i42 = TREE_DEPTH - 1 - n2;
|
|
112308
|
+
let sibling;
|
|
112309
|
+
if ((collapsed >> BigInt(i42) & 1n) === 1n) {
|
|
112310
|
+
sibling = CACHED_ZERO[n2];
|
|
112733
112311
|
} else {
|
|
112734
|
-
if (
|
|
112735
|
-
|
|
112736
|
-
|
|
112737
|
-
node.hash = NULL_HASH;
|
|
112738
|
-
}
|
|
112739
|
-
hash32 = node.hash;
|
|
112740
|
-
saveProofs = (hashes2) => {
|
|
112741
|
-
this.#proofs.set(node.index, new SMTProof(converge, hashes2));
|
|
112742
|
-
};
|
|
112743
|
-
}
|
|
112744
|
-
if (node.depth !== depth) {
|
|
112745
|
-
const leftPad = [];
|
|
112746
|
-
const rightPad = [];
|
|
112747
|
-
for (let i42 = node.depth - 1; i42 >= depth; i42--) {
|
|
112748
|
-
if ((node.index & BITS[i42]) === 0n) {
|
|
112749
|
-
rightPad.push(i42);
|
|
112750
|
-
} else {
|
|
112751
|
-
leftPad.unshift(i42);
|
|
112752
|
-
}
|
|
112753
|
-
}
|
|
112754
|
-
hash32 = blockHash(new Uint8Array(leftPad), hash32, new Uint8Array(rightPad));
|
|
112312
|
+
if (hashPtr >= hashes2.length)
|
|
112313
|
+
return false;
|
|
112314
|
+
sibling = hashes2[hashPtr++];
|
|
112755
112315
|
}
|
|
112756
|
-
|
|
112316
|
+
acc = bitAt(index, i42) === 1 ? blockHash(sibling, acc) : blockHash(acc, sibling);
|
|
112757
112317
|
}
|
|
112758
|
-
|
|
112318
|
+
return hashPtr === hashes2.length && hashesEqual(acc, root);
|
|
112319
|
+
}
|
|
112759
112320
|
init_shim2();
|
|
112760
112321
|
var encoder = new TextEncoder();
|
|
112761
112322
|
function didToIndex(did) {
|
|
@@ -112768,49 +112329,51 @@ a=end-of-candidates
|
|
|
112768
112329
|
return blockHash(blockHash(nonce));
|
|
112769
112330
|
}
|
|
112770
112331
|
init_shim2();
|
|
112771
|
-
function serializeProof(
|
|
112332
|
+
function serializeProof(rootHash, proof, options2) {
|
|
112772
112333
|
const result = {
|
|
112773
|
-
id:
|
|
112774
|
-
collapsed:
|
|
112775
|
-
hashes: proof.hashes.map((h) =>
|
|
112334
|
+
id: hashToBase64Url(rootHash),
|
|
112335
|
+
collapsed: hashToBase64Url(bigIntToHash(proof.collapsed)),
|
|
112336
|
+
hashes: proof.hashes.map((h) => hashToBase64Url(h))
|
|
112776
112337
|
};
|
|
112777
112338
|
if (options2?.nonce)
|
|
112778
|
-
result.nonce =
|
|
112339
|
+
result.nonce = hashToBase64Url(options2.nonce);
|
|
112779
112340
|
if (options2?.updateId)
|
|
112780
|
-
result.updateId =
|
|
112341
|
+
result.updateId = hashToBase64Url(options2.updateId);
|
|
112781
112342
|
return result;
|
|
112782
112343
|
}
|
|
112783
112344
|
function deserializeProof(serialized) {
|
|
112784
|
-
const converge = hexToBigInt(serialized.collapsed, false);
|
|
112785
|
-
const hashes2 = serialized.hashes.map((h) => hexToHash(h));
|
|
112786
112345
|
const result = {
|
|
112787
|
-
|
|
112788
|
-
|
|
112346
|
+
rootHash: base64UrlToHash(serialized.id),
|
|
112347
|
+
collapsed: hashToBigInt(base64UrlToHash(serialized.collapsed)),
|
|
112348
|
+
hashes: serialized.hashes.map((h) => base64UrlToHash(h))
|
|
112789
112349
|
};
|
|
112790
112350
|
if (serialized.nonce)
|
|
112791
|
-
result.nonce =
|
|
112351
|
+
result.nonce = base64UrlToHash(serialized.nonce);
|
|
112792
112352
|
if (serialized.updateId)
|
|
112793
|
-
result.updateId =
|
|
112353
|
+
result.updateId = base64UrlToHash(serialized.updateId);
|
|
112794
112354
|
return result;
|
|
112795
112355
|
}
|
|
112796
112356
|
function verifySerializedProof(serialized, index, candidateHash) {
|
|
112797
|
-
const {
|
|
112798
|
-
return
|
|
112357
|
+
const { rootHash, collapsed, hashes: hashes2 } = deserializeProof(serialized);
|
|
112358
|
+
return verifyZeroHash(collapsed, hashes2, index, candidateHash, rootHash);
|
|
112799
112359
|
}
|
|
112800
112360
|
init_shim2();
|
|
112801
112361
|
var BTCR2MerkleTree = class {
|
|
112802
|
-
#smt;
|
|
112803
112362
|
#entries = /* @__PURE__ */ new Map();
|
|
112804
112363
|
#indexByDid = /* @__PURE__ */ new Map();
|
|
112805
|
-
|
|
112806
|
-
|
|
112364
|
+
#leaves = null;
|
|
112365
|
+
#root = null;
|
|
112366
|
+
/**
|
|
112367
|
+
* @param _allowNonInclusion Retained for API compatibility; non-inclusion
|
|
112368
|
+
* leaves are always supported (an entry without `signedUpdate`).
|
|
112369
|
+
*/
|
|
112370
|
+
constructor(_allowNonInclusion = true) {
|
|
112807
112371
|
}
|
|
112808
112372
|
/**
|
|
112809
112373
|
* Add entries to the tree. May be called multiple times before
|
|
112810
112374
|
* {@link finalize}. Duplicate DIDs (same index) throw.
|
|
112811
112375
|
*/
|
|
112812
112376
|
addEntries(entries) {
|
|
112813
|
-
const indexes = [];
|
|
112814
112377
|
for (const entry of entries) {
|
|
112815
112378
|
const index = didToIndex(entry.did);
|
|
112816
112379
|
if (this.#entries.has(index)) {
|
|
@@ -112818,24 +112381,28 @@ a=end-of-candidates
|
|
|
112818
112381
|
}
|
|
112819
112382
|
this.#entries.set(index, entry);
|
|
112820
112383
|
this.#indexByDid.set(entry.did, index);
|
|
112821
|
-
indexes.push(index);
|
|
112822
112384
|
}
|
|
112823
|
-
this.#
|
|
112385
|
+
this.#leaves = null;
|
|
112386
|
+
this.#root = null;
|
|
112824
112387
|
}
|
|
112825
112388
|
/**
|
|
112826
|
-
* Compute leaf hashes and
|
|
112389
|
+
* Compute leaf hashes and the zero-hash root.
|
|
112827
112390
|
* After this call, {@link rootHash} and {@link proof} become available.
|
|
112828
112391
|
*/
|
|
112829
112392
|
finalize() {
|
|
112393
|
+
const leaves = [];
|
|
112830
112394
|
for (const [index, entry] of this.#entries) {
|
|
112831
|
-
const
|
|
112832
|
-
|
|
112395
|
+
const leaf = entry.signedUpdate !== void 0 ? inclusionLeafHash(entry.nonce, entry.signedUpdate) : nonInclusionLeafHash(entry.nonce);
|
|
112396
|
+
leaves.push({ index, leaf });
|
|
112833
112397
|
}
|
|
112834
|
-
this.#
|
|
112398
|
+
this.#leaves = leaves;
|
|
112399
|
+
this.#root = zeroHashRoot(leaves);
|
|
112835
112400
|
}
|
|
112836
112401
|
/** Root hash of the finalized tree. Throws if not finalized. */
|
|
112837
112402
|
get rootHash() {
|
|
112838
|
-
|
|
112403
|
+
if (this.#root === null)
|
|
112404
|
+
throw new Error("Tree not finalized: call finalize() first");
|
|
112405
|
+
return this.#root;
|
|
112839
112406
|
}
|
|
112840
112407
|
/**
|
|
112841
112408
|
* Get the did:btcr2 serialized proof for a DID.
|
|
@@ -112845,14 +112412,18 @@ a=end-of-candidates
|
|
|
112845
112412
|
const index = this.#indexByDid.get(did);
|
|
112846
112413
|
if (index === void 0)
|
|
112847
112414
|
throw new RangeError(`DID not in tree: ${did}`);
|
|
112415
|
+
if (this.#leaves === null || this.#root === null) {
|
|
112416
|
+
throw new Error("Tree not finalized: call finalize() first");
|
|
112417
|
+
}
|
|
112848
112418
|
const entry = this.#entries.get(index);
|
|
112849
|
-
const
|
|
112419
|
+
const proof = generateZeroHashProof(this.#leaves, index);
|
|
112850
112420
|
const updateId = entry.signedUpdate !== void 0 ? blockHash(entry.signedUpdate) : void 0;
|
|
112851
|
-
return serializeProof(
|
|
112421
|
+
return serializeProof(this.#root, proof, { nonce: entry.nonce, updateId });
|
|
112852
112422
|
}
|
|
112853
|
-
/** Clear
|
|
112423
|
+
/** Clear computed leaves and root, keeping entries. */
|
|
112854
112424
|
reset() {
|
|
112855
|
-
this.#
|
|
112425
|
+
this.#leaves = null;
|
|
112426
|
+
this.#root = null;
|
|
112856
112427
|
}
|
|
112857
112428
|
};
|
|
112858
112429
|
var CAS_STRATEGY = {
|
|
@@ -112885,12 +112456,12 @@ a=end-of-candidates
|
|
|
112885
112456
|
const smtProof = body.smtProof;
|
|
112886
112457
|
if (!smtProof?.updateId || !smtProof?.nonce) return { matches: false };
|
|
112887
112458
|
const canonicalBytes = new TextEncoder().encode(canonicalize2(submittedUpdate));
|
|
112888
|
-
const expectedUpdateId =
|
|
112459
|
+
const expectedUpdateId = hashToBase64Url(blockHash(canonicalBytes));
|
|
112889
112460
|
if (smtProof.updateId !== expectedUpdateId) {
|
|
112890
112461
|
return { matches: false, smtProof };
|
|
112891
112462
|
}
|
|
112892
112463
|
const index = didToIndex(participantDid);
|
|
112893
|
-
const candidateHash = blockHash(blockHash(
|
|
112464
|
+
const candidateHash = blockHash(blockHash(base64UrlToHash(smtProof.nonce)), base64UrlToHash(smtProof.updateId));
|
|
112894
112465
|
return {
|
|
112895
112466
|
matches: verifySerializedProof(smtProof, index, candidateHash),
|
|
112896
112467
|
smtProof
|
|
@@ -117166,8 +116737,8 @@ a=end-of-candidates
|
|
|
117166
116737
|
let iv = Uint8Array.from(randomBytes2(16));
|
|
117167
116738
|
let plaintext = utf8Encoder.encode(text);
|
|
117168
116739
|
let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext);
|
|
117169
|
-
let ctb64 =
|
|
117170
|
-
let ivb64 =
|
|
116740
|
+
let ctb64 = base64.encode(new Uint8Array(ciphertext));
|
|
116741
|
+
let ivb64 = base64.encode(new Uint8Array(iv.buffer));
|
|
117171
116742
|
return `${ctb64}?iv=${ivb64}`;
|
|
117172
116743
|
}
|
|
117173
116744
|
function decrypt2(secretKey, pubkey, data) {
|
|
@@ -117175,8 +116746,8 @@ a=end-of-candidates
|
|
|
117175
116746
|
let [ctb64, ivb64] = data.split("?iv=");
|
|
117176
116747
|
let key = secp256k1.getSharedSecret(privkey, hexToBytes2("02" + pubkey));
|
|
117177
116748
|
let normalizedKey = getNormalizedX(key);
|
|
117178
|
-
let iv =
|
|
117179
|
-
let ciphertext =
|
|
116749
|
+
let iv = base64.decode(ivb64);
|
|
116750
|
+
let ciphertext = base64.decode(ctb64);
|
|
117180
116751
|
let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext);
|
|
117181
116752
|
return utf8Decoder.decode(plaintext);
|
|
117182
116753
|
}
|
|
@@ -117496,7 +117067,7 @@ a=end-of-candidates
|
|
|
117496
117067
|
throw new Error("unknown encryption version");
|
|
117497
117068
|
let data;
|
|
117498
117069
|
try {
|
|
117499
|
-
data =
|
|
117070
|
+
data = base64.decode(payload);
|
|
117500
117071
|
} catch (error) {
|
|
117501
117072
|
throw new Error("invalid base64: " + error.message);
|
|
117502
117073
|
}
|
|
@@ -117517,7 +117088,7 @@ a=end-of-candidates
|
|
|
117517
117088
|
const padded = pad(plaintext);
|
|
117518
117089
|
const ciphertext = chacha20(chacha_key, chacha_nonce, padded);
|
|
117519
117090
|
const mac = hmacAad(hmac_key, ciphertext, nonce);
|
|
117520
|
-
return
|
|
117091
|
+
return base64.encode(concatBytes2(new Uint8Array([2]), nonce, ciphertext, mac));
|
|
117521
117092
|
}
|
|
117522
117093
|
function decrypt22(payload, conversationKey) {
|
|
117523
117094
|
const { nonce, ciphertext, mac } = decodePayload(payload);
|
|
@@ -118792,7 +118363,7 @@ a=end-of-candidates
|
|
|
118792
118363
|
}
|
|
118793
118364
|
const signedEvent = await sign(event);
|
|
118794
118365
|
const authorizationScheme = includeAuthorizationScheme ? _authorizationScheme : "";
|
|
118795
|
-
return authorizationScheme +
|
|
118366
|
+
return authorizationScheme + base64.encode(utf8Encoder.encode(JSON.stringify(signedEvent)));
|
|
118796
118367
|
}
|
|
118797
118368
|
async function validateToken(token, url, method) {
|
|
118798
118369
|
const event = await unpackEventFromToken(token).catch((error) => {
|
|
@@ -118808,7 +118379,7 @@ a=end-of-candidates
|
|
|
118808
118379
|
throw new Error("Missing token");
|
|
118809
118380
|
}
|
|
118810
118381
|
token = token.replace(_authorizationScheme, "");
|
|
118811
|
-
const eventB64 = utf8Decoder.decode(
|
|
118382
|
+
const eventB64 = utf8Decoder.decode(base64.decode(token));
|
|
118812
118383
|
if (!eventB64 || eventB64.length === 0 || !eventB64.startsWith("{")) {
|
|
118813
118384
|
throw new Error("Invalid token");
|
|
118814
118385
|
}
|
|
@@ -119388,9 +118959,6 @@ a=end-of-candidates
|
|
|
119388
118959
|
});
|
|
119389
118960
|
continue;
|
|
119390
118961
|
}
|
|
119391
|
-
if (!smtProof.updateId) {
|
|
119392
|
-
continue;
|
|
119393
|
-
}
|
|
119394
118962
|
if (!smtProof.nonce) {
|
|
119395
118963
|
throw new SMTBeaconError(
|
|
119396
118964
|
"SMT proof missing required nonce field.",
|
|
@@ -119399,7 +118967,8 @@ a=end-of-candidates
|
|
|
119399
118967
|
);
|
|
119400
118968
|
}
|
|
119401
118969
|
const index = didToIndex(did);
|
|
119402
|
-
const
|
|
118970
|
+
const nonceHash2 = base64UrlToHash(smtProof.nonce);
|
|
118971
|
+
const candidateHash = smtProof.updateId ? blockHash(blockHash(nonceHash2), base64UrlToHash(smtProof.updateId)) : blockHash(blockHash(nonceHash2));
|
|
119403
118972
|
const valid = verifySerializedProof(smtProof, index, candidateHash);
|
|
119404
118973
|
if (!valid) {
|
|
119405
118974
|
throw new SMTBeaconError(
|
|
@@ -119408,11 +118977,15 @@ a=end-of-candidates
|
|
|
119408
118977
|
{ smtProof, did }
|
|
119409
118978
|
);
|
|
119410
118979
|
}
|
|
119411
|
-
|
|
118980
|
+
if (!smtProof.updateId) {
|
|
118981
|
+
continue;
|
|
118982
|
+
}
|
|
118983
|
+
const updateHashHex = hashToHex(base64UrlToHash(smtProof.updateId));
|
|
118984
|
+
const signedUpdate = sidecar.updateMap.get(updateHashHex);
|
|
119412
118985
|
if (!signedUpdate) {
|
|
119413
118986
|
needs.push({
|
|
119414
118987
|
kind: "NeedSignedUpdate",
|
|
119415
|
-
updateHash:
|
|
118988
|
+
updateHash: updateHashHex,
|
|
119416
118989
|
beaconServiceId: this.service.id
|
|
119417
118990
|
});
|
|
119418
118991
|
continue;
|
|
@@ -135168,7 +134741,7 @@ a=end-of-candidates
|
|
|
135168
134741
|
const smtMap = /* @__PURE__ */ new Map();
|
|
135169
134742
|
if (sidecar.smtProofs?.length)
|
|
135170
134743
|
for (const proof of sidecar.smtProofs) {
|
|
135171
|
-
smtMap.set(proof.id, proof);
|
|
134744
|
+
smtMap.set(encode(decode(proof.id, "base64urlnopad"), "hex"), proof);
|
|
135172
134745
|
}
|
|
135173
134746
|
return { updateMap, casMap, smtMap };
|
|
135174
134747
|
}
|
|
@@ -135456,11 +135029,12 @@ a=end-of-candidates
|
|
|
135456
135029
|
case "NeedSMTProof": {
|
|
135457
135030
|
const smtNeed = need;
|
|
135458
135031
|
const proof = data;
|
|
135459
|
-
|
|
135032
|
+
const proofIdHex = encode(decode(proof.id, "base64urlnopad"), "hex");
|
|
135033
|
+
if (proofIdHex !== smtNeed.smtRootHash) {
|
|
135460
135034
|
throw new ResolveError(
|
|
135461
|
-
`SMT proof root hash mismatch: expected ${smtNeed.smtRootHash}, got ${
|
|
135035
|
+
`SMT proof root hash mismatch: expected ${smtNeed.smtRootHash}, got ${proofIdHex}`,
|
|
135462
135036
|
INVALID_DID_UPDATE,
|
|
135463
|
-
{ expected: smtNeed.smtRootHash, actual:
|
|
135037
|
+
{ expected: smtNeed.smtRootHash, actual: proofIdHex }
|
|
135464
135038
|
);
|
|
135465
135039
|
}
|
|
135466
135040
|
this.#sidecarData.smtMap.set(smtNeed.smtRootHash, proof);
|
|
@@ -136097,7 +135671,7 @@ a=end-of-candidates
|
|
|
136097
135671
|
res.push(2 ** i5);
|
|
136098
135672
|
return res;
|
|
136099
135673
|
})();
|
|
136100
|
-
function convertRadix23(data, from14, to,
|
|
135674
|
+
function convertRadix23(data, from14, to, padding2) {
|
|
136101
135675
|
aArr3(data);
|
|
136102
135676
|
if (from14 <= 0 || from14 > 32)
|
|
136103
135677
|
throw new Error(`convertRadix2: wrong from=${from14}`);
|
|
@@ -136127,11 +135701,11 @@ a=end-of-candidates
|
|
|
136127
135701
|
carry &= pow4 - 1;
|
|
136128
135702
|
}
|
|
136129
135703
|
carry = carry << to - pos & mask;
|
|
136130
|
-
if (!
|
|
135704
|
+
if (!padding2 && pos >= from14)
|
|
136131
135705
|
throw new Error("Excess padding");
|
|
136132
|
-
if (!
|
|
135706
|
+
if (!padding2 && carry > 0)
|
|
136133
135707
|
throw new Error(`Non-zero padding: ${carry}`);
|
|
136134
|
-
if (
|
|
135708
|
+
if (padding2 && pos > 0)
|
|
136135
135709
|
res.push(carry >>> 0);
|
|
136136
135710
|
return res;
|
|
136137
135711
|
}
|
|
@@ -146728,7 +146302,7 @@ a=end-of-candidates
|
|
|
146728
146302
|
|
|
146729
146303
|
// ../../node_modules/.pnpm/multiformats@13.1.0/node_modules/multiformats/dist/src/bases/base64.js
|
|
146730
146304
|
init_shim();
|
|
146731
|
-
var
|
|
146305
|
+
var base642 = rfc46485({
|
|
146732
146306
|
prefix: "m",
|
|
146733
146307
|
name: "base64",
|
|
146734
146308
|
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
|
@@ -158712,7 +158286,7 @@ a=end-of-candidates
|
|
|
158712
158286
|
};
|
|
158713
158287
|
|
|
158714
158288
|
// ../../node_modules/.pnpm/multiformats@12.1.3/node_modules/multiformats/src/bases/base64.js
|
|
158715
|
-
var
|
|
158289
|
+
var base646 = rfc46486({
|
|
158716
158290
|
prefix: "m",
|
|
158717
158291
|
name: "base64",
|
|
158718
158292
|
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|