@didcid/keymaster 0.3.9 → 0.4.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.
@@ -6,7 +6,6 @@ var imageSize = require('image-size');
6
6
  var fileType = require('file-type');
7
7
  var db_typeGuards = require('./db/typeGuards.cjs');
8
8
  require('crypto');
9
- var encryption = require('./encryption.cjs');
10
9
 
11
10
  function equals$1(aa, bb) {
12
11
  if (aa === bb) {
@@ -1082,6 +1081,1582 @@ function isValidDID(did) {
1082
1081
  return isValidCID(suffix);
1083
1082
  }
1084
1083
 
1084
+ const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
1085
+
1086
+ /**
1087
+ * Utilities for hex, bytes, CSPRNG.
1088
+ * @module
1089
+ */
1090
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1091
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
1092
+ // node.js versions earlier than v19 don't declare it in global scope.
1093
+ // For node.js, package.json#exports field mapping rewrites import
1094
+ // from `crypto` to `cryptoNode`, which imports native module.
1095
+ // Makes the utils un-importable in browsers without a bundler.
1096
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
1097
+ /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
1098
+ function isBytes$2(a) {
1099
+ return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
1100
+ }
1101
+ /** Asserts something is positive integer. */
1102
+ function anumber(n) {
1103
+ if (!Number.isSafeInteger(n) || n < 0)
1104
+ throw new Error('positive integer expected, got ' + n);
1105
+ }
1106
+ /** Asserts something is Uint8Array. */
1107
+ function abytes(b, ...lengths) {
1108
+ if (!isBytes$2(b))
1109
+ throw new Error('Uint8Array expected');
1110
+ if (lengths.length > 0 && !lengths.includes(b.length))
1111
+ throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
1112
+ }
1113
+ /** Asserts something is hash */
1114
+ function ahash(h) {
1115
+ if (typeof h !== 'function' || typeof h.create !== 'function')
1116
+ throw new Error('Hash should be wrapped by utils.createHasher');
1117
+ anumber(h.outputLen);
1118
+ anumber(h.blockLen);
1119
+ }
1120
+ /** Asserts a hash instance has not been destroyed / finished */
1121
+ function aexists(instance, checkFinished = true) {
1122
+ if (instance.destroyed)
1123
+ throw new Error('Hash instance has been destroyed');
1124
+ if (checkFinished && instance.finished)
1125
+ throw new Error('Hash#digest() has already been called');
1126
+ }
1127
+ /** Asserts output is properly-sized byte array */
1128
+ function aoutput(out, instance) {
1129
+ abytes(out);
1130
+ const min = instance.outputLen;
1131
+ if (out.length < min) {
1132
+ throw new Error('digestInto() expects output buffer of length at least ' + min);
1133
+ }
1134
+ }
1135
+ /** Zeroize a byte array. Warning: JS provides no guarantees. */
1136
+ function clean(...arrays) {
1137
+ for (let i = 0; i < arrays.length; i++) {
1138
+ arrays[i].fill(0);
1139
+ }
1140
+ }
1141
+ /** Create DataView of an array for easy byte-level manipulation. */
1142
+ function createView$1(arr) {
1143
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
1144
+ }
1145
+ /**
1146
+ * There is no setImmediate in browser and setTimeout is slow.
1147
+ * Call of async fn will return Promise, which will be fullfiled only on
1148
+ * next scheduler queue processing step and this is exactly what we need.
1149
+ */
1150
+ const nextTick = async () => { };
1151
+ /** Returns control to thread each 'tick' ms to avoid blocking. */
1152
+ async function asyncLoop(iters, tick, cb) {
1153
+ let ts = Date.now();
1154
+ for (let i = 0; i < iters; i++) {
1155
+ cb(i);
1156
+ // Date.now() is not monotonic, so in case if clock goes backwards we return return control too
1157
+ const diff = Date.now() - ts;
1158
+ if (diff >= 0 && diff < tick)
1159
+ continue;
1160
+ await nextTick();
1161
+ ts += diff;
1162
+ }
1163
+ }
1164
+ /**
1165
+ * Converts string to bytes using UTF8 encoding.
1166
+ * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
1167
+ */
1168
+ function utf8ToBytes$1(str) {
1169
+ if (typeof str !== 'string')
1170
+ throw new Error('string expected');
1171
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
1172
+ }
1173
+ /**
1174
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
1175
+ * Warning: when Uint8Array is passed, it would NOT get copied.
1176
+ * Keep in mind for future mutable operations.
1177
+ */
1178
+ function toBytes$1(data) {
1179
+ if (typeof data === 'string')
1180
+ data = utf8ToBytes$1(data);
1181
+ abytes(data);
1182
+ return data;
1183
+ }
1184
+ /**
1185
+ * Helper for KDFs: consumes uint8array or string.
1186
+ * When string is passed, does utf8 decoding, using TextDecoder.
1187
+ */
1188
+ function kdfInputToBytes(data) {
1189
+ if (typeof data === 'string')
1190
+ data = utf8ToBytes$1(data);
1191
+ abytes(data);
1192
+ return data;
1193
+ }
1194
+ function checkOpts(defaults, opts) {
1195
+ if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
1196
+ throw new Error('options should be object or undefined');
1197
+ const merged = Object.assign(defaults, opts);
1198
+ return merged;
1199
+ }
1200
+ /** For runtime check if class implements interface */
1201
+ class Hash {
1202
+ }
1203
+ /** Wraps hash function, creating an interface on top of it */
1204
+ function createHasher(hashCons) {
1205
+ const hashC = (msg) => hashCons().update(toBytes$1(msg)).digest();
1206
+ const tmp = hashCons();
1207
+ hashC.outputLen = tmp.outputLen;
1208
+ hashC.blockLen = tmp.blockLen;
1209
+ hashC.create = () => hashCons();
1210
+ return hashC;
1211
+ }
1212
+ /** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
1213
+ function randomBytes(bytesLength = 32) {
1214
+ if (crypto && typeof crypto.getRandomValues === 'function') {
1215
+ return crypto.getRandomValues(new Uint8Array(bytesLength));
1216
+ }
1217
+ // Legacy Node.js compatibility
1218
+ if (crypto && typeof crypto.randomBytes === 'function') {
1219
+ return Uint8Array.from(crypto.randomBytes(bytesLength));
1220
+ }
1221
+ throw new Error('crypto.getRandomValues must be defined');
1222
+ }
1223
+
1224
+ /**
1225
+ * HMAC: RFC2104 message authentication code.
1226
+ * @module
1227
+ */
1228
+ class HMAC extends Hash {
1229
+ constructor(hash, _key) {
1230
+ super();
1231
+ this.finished = false;
1232
+ this.destroyed = false;
1233
+ ahash(hash);
1234
+ const key = toBytes$1(_key);
1235
+ this.iHash = hash.create();
1236
+ if (typeof this.iHash.update !== 'function')
1237
+ throw new Error('Expected instance of class which extends utils.Hash');
1238
+ this.blockLen = this.iHash.blockLen;
1239
+ this.outputLen = this.iHash.outputLen;
1240
+ const blockLen = this.blockLen;
1241
+ const pad = new Uint8Array(blockLen);
1242
+ // blockLen can be bigger than outputLen
1243
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
1244
+ for (let i = 0; i < pad.length; i++)
1245
+ pad[i] ^= 0x36;
1246
+ this.iHash.update(pad);
1247
+ // By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
1248
+ this.oHash = hash.create();
1249
+ // Undo internal XOR && apply outer XOR
1250
+ for (let i = 0; i < pad.length; i++)
1251
+ pad[i] ^= 0x36 ^ 0x5c;
1252
+ this.oHash.update(pad);
1253
+ clean(pad);
1254
+ }
1255
+ update(buf) {
1256
+ aexists(this);
1257
+ this.iHash.update(buf);
1258
+ return this;
1259
+ }
1260
+ digestInto(out) {
1261
+ aexists(this);
1262
+ abytes(out, this.outputLen);
1263
+ this.finished = true;
1264
+ this.iHash.digestInto(out);
1265
+ this.oHash.update(out);
1266
+ this.oHash.digestInto(out);
1267
+ this.destroy();
1268
+ }
1269
+ digest() {
1270
+ const out = new Uint8Array(this.oHash.outputLen);
1271
+ this.digestInto(out);
1272
+ return out;
1273
+ }
1274
+ _cloneInto(to) {
1275
+ // Create new instance without calling constructor since key already in state and we don't know it.
1276
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
1277
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
1278
+ to = to;
1279
+ to.finished = finished;
1280
+ to.destroyed = destroyed;
1281
+ to.blockLen = blockLen;
1282
+ to.outputLen = outputLen;
1283
+ to.oHash = oHash._cloneInto(to.oHash);
1284
+ to.iHash = iHash._cloneInto(to.iHash);
1285
+ return to;
1286
+ }
1287
+ clone() {
1288
+ return this._cloneInto();
1289
+ }
1290
+ destroy() {
1291
+ this.destroyed = true;
1292
+ this.oHash.destroy();
1293
+ this.iHash.destroy();
1294
+ }
1295
+ }
1296
+ /**
1297
+ * HMAC: RFC2104 message authentication code.
1298
+ * @param hash - function that would be used e.g. sha256
1299
+ * @param key - message key
1300
+ * @param message - message data
1301
+ * @example
1302
+ * import { hmac } from '@noble/hashes/hmac';
1303
+ * import { sha256 } from '@noble/hashes/sha2';
1304
+ * const mac1 = hmac(sha256, 'key', 'message');
1305
+ */
1306
+ const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
1307
+ hmac.create = (hash, key) => new HMAC(hash, key);
1308
+
1309
+ /**
1310
+ * PBKDF (RFC 2898). Can be used to create a key from password and salt.
1311
+ * @module
1312
+ */
1313
+ // Common prologue and epilogue for sync/async functions
1314
+ function pbkdf2Init(hash, _password, _salt, _opts) {
1315
+ ahash(hash);
1316
+ const opts = checkOpts({ dkLen: 32, asyncTick: 10 }, _opts);
1317
+ const { c, dkLen, asyncTick } = opts;
1318
+ anumber(c);
1319
+ anumber(dkLen);
1320
+ anumber(asyncTick);
1321
+ if (c < 1)
1322
+ throw new Error('iterations (c) should be >= 1');
1323
+ const password = kdfInputToBytes(_password);
1324
+ const salt = kdfInputToBytes(_salt);
1325
+ // DK = PBKDF2(PRF, Password, Salt, c, dkLen);
1326
+ const DK = new Uint8Array(dkLen);
1327
+ // U1 = PRF(Password, Salt + INT_32_BE(i))
1328
+ const PRF = hmac.create(hash, password);
1329
+ const PRFSalt = PRF._cloneInto().update(salt);
1330
+ return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
1331
+ }
1332
+ function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
1333
+ PRF.destroy();
1334
+ PRFSalt.destroy();
1335
+ if (prfW)
1336
+ prfW.destroy();
1337
+ clean(u);
1338
+ return DK;
1339
+ }
1340
+ /**
1341
+ * PBKDF2-HMAC: RFC 2898 key derivation function. Async version.
1342
+ * @example
1343
+ * await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
1344
+ */
1345
+ async function pbkdf2Async(hash, password, salt, opts) {
1346
+ const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
1347
+ let prfW; // Working copy
1348
+ const arr = new Uint8Array(4);
1349
+ const view = createView$1(arr);
1350
+ const u = new Uint8Array(PRF.outputLen);
1351
+ // DK = T1 + T2 + ⋯ + Tdklen/hlen
1352
+ for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
1353
+ // Ti = F(Password, Salt, c, i)
1354
+ const Ti = DK.subarray(pos, pos + PRF.outputLen);
1355
+ view.setInt32(0, ti, false);
1356
+ // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
1357
+ // U1 = PRF(Password, Salt + INT_32_BE(i))
1358
+ (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
1359
+ Ti.set(u.subarray(0, Ti.length));
1360
+ await asyncLoop(c - 1, asyncTick, () => {
1361
+ // Uc = PRF(Password, Uc−1)
1362
+ PRF._cloneInto(prfW).update(u).digestInto(u);
1363
+ for (let i = 0; i < Ti.length; i++)
1364
+ Ti[i] ^= u[i];
1365
+ });
1366
+ }
1367
+ return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
1368
+ }
1369
+
1370
+ /**
1371
+ * Internal Merkle-Damgard hash utils.
1372
+ * @module
1373
+ */
1374
+ /** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
1375
+ function setBigUint64$1(view, byteOffset, value, isLE) {
1376
+ if (typeof view.setBigUint64 === 'function')
1377
+ return view.setBigUint64(byteOffset, value, isLE);
1378
+ const _32n = BigInt(32);
1379
+ const _u32_max = BigInt(0xffffffff);
1380
+ const wh = Number((value >> _32n) & _u32_max);
1381
+ const wl = Number(value & _u32_max);
1382
+ const h = isLE ? 4 : 0;
1383
+ const l = isLE ? 0 : 4;
1384
+ view.setUint32(byteOffset + h, wh, isLE);
1385
+ view.setUint32(byteOffset + l, wl, isLE);
1386
+ }
1387
+ /**
1388
+ * Merkle-Damgard hash construction base class.
1389
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
1390
+ */
1391
+ class HashMD extends Hash {
1392
+ constructor(blockLen, outputLen, padOffset, isLE) {
1393
+ super();
1394
+ this.finished = false;
1395
+ this.length = 0;
1396
+ this.pos = 0;
1397
+ this.destroyed = false;
1398
+ this.blockLen = blockLen;
1399
+ this.outputLen = outputLen;
1400
+ this.padOffset = padOffset;
1401
+ this.isLE = isLE;
1402
+ this.buffer = new Uint8Array(blockLen);
1403
+ this.view = createView$1(this.buffer);
1404
+ }
1405
+ update(data) {
1406
+ aexists(this);
1407
+ data = toBytes$1(data);
1408
+ abytes(data);
1409
+ const { view, buffer, blockLen } = this;
1410
+ const len = data.length;
1411
+ for (let pos = 0; pos < len;) {
1412
+ const take = Math.min(blockLen - this.pos, len - pos);
1413
+ // Fast path: we have at least one block in input, cast it to view and process
1414
+ if (take === blockLen) {
1415
+ const dataView = createView$1(data);
1416
+ for (; blockLen <= len - pos; pos += blockLen)
1417
+ this.process(dataView, pos);
1418
+ continue;
1419
+ }
1420
+ buffer.set(data.subarray(pos, pos + take), this.pos);
1421
+ this.pos += take;
1422
+ pos += take;
1423
+ if (this.pos === blockLen) {
1424
+ this.process(view, 0);
1425
+ this.pos = 0;
1426
+ }
1427
+ }
1428
+ this.length += data.length;
1429
+ this.roundClean();
1430
+ return this;
1431
+ }
1432
+ digestInto(out) {
1433
+ aexists(this);
1434
+ aoutput(out, this);
1435
+ this.finished = true;
1436
+ // Padding
1437
+ // We can avoid allocation of buffer for padding completely if it
1438
+ // was previously not allocated here. But it won't change performance.
1439
+ const { buffer, view, blockLen, isLE } = this;
1440
+ let { pos } = this;
1441
+ // append the bit '1' to the message
1442
+ buffer[pos++] = 0b10000000;
1443
+ clean(this.buffer.subarray(pos));
1444
+ // we have less than padOffset left in buffer, so we cannot put length in
1445
+ // current block, need process it and pad again
1446
+ if (this.padOffset > blockLen - pos) {
1447
+ this.process(view, 0);
1448
+ pos = 0;
1449
+ }
1450
+ // Pad until full block byte with zeros
1451
+ for (let i = pos; i < blockLen; i++)
1452
+ buffer[i] = 0;
1453
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
1454
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
1455
+ // So we just write lowest 64 bits of that value.
1456
+ setBigUint64$1(view, blockLen - 8, BigInt(this.length * 8), isLE);
1457
+ this.process(view, 0);
1458
+ const oview = createView$1(out);
1459
+ const len = this.outputLen;
1460
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
1461
+ if (len % 4)
1462
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
1463
+ const outLen = len / 4;
1464
+ const state = this.get();
1465
+ if (outLen > state.length)
1466
+ throw new Error('_sha2: outputLen bigger than state');
1467
+ for (let i = 0; i < outLen; i++)
1468
+ oview.setUint32(4 * i, state[i], isLE);
1469
+ }
1470
+ digest() {
1471
+ const { buffer, outputLen } = this;
1472
+ this.digestInto(buffer);
1473
+ const res = buffer.slice(0, outputLen);
1474
+ this.destroy();
1475
+ return res;
1476
+ }
1477
+ _cloneInto(to) {
1478
+ to || (to = new this.constructor());
1479
+ to.set(...this.get());
1480
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
1481
+ to.destroyed = destroyed;
1482
+ to.finished = finished;
1483
+ to.length = length;
1484
+ to.pos = pos;
1485
+ if (length % blockLen)
1486
+ to.buffer.set(buffer);
1487
+ return to;
1488
+ }
1489
+ clone() {
1490
+ return this._cloneInto();
1491
+ }
1492
+ }
1493
+ /** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
1494
+ const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
1495
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
1496
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
1497
+ ]);
1498
+
1499
+ /**
1500
+ * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
1501
+ * @todo re-check https://issues.chromium.org/issues/42212588
1502
+ * @module
1503
+ */
1504
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
1505
+ const _32n = /* @__PURE__ */ BigInt(32);
1506
+ function fromBig(n, le = false) {
1507
+ if (le)
1508
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
1509
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
1510
+ }
1511
+ function split(lst, le = false) {
1512
+ const len = lst.length;
1513
+ let Ah = new Uint32Array(len);
1514
+ let Al = new Uint32Array(len);
1515
+ for (let i = 0; i < len; i++) {
1516
+ const { h, l } = fromBig(lst[i], le);
1517
+ [Ah[i], Al[i]] = [h, l];
1518
+ }
1519
+ return [Ah, Al];
1520
+ }
1521
+ // for Shift in [0, 32)
1522
+ const shrSH = (h, _l, s) => h >>> s;
1523
+ const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
1524
+ // Right rotate for Shift in [1, 32)
1525
+ const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
1526
+ const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
1527
+ // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
1528
+ const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
1529
+ const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
1530
+ // JS uses 32-bit signed integers for bitwise operations which means we cannot
1531
+ // simple take carry out of low bit sum by shift, we need to use division.
1532
+ function add(Ah, Al, Bh, Bl) {
1533
+ const l = (Al >>> 0) + (Bl >>> 0);
1534
+ return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
1535
+ }
1536
+ // Addition with more than 2 elements
1537
+ const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
1538
+ const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
1539
+ const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
1540
+ const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
1541
+ const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
1542
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
1543
+
1544
+ /**
1545
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
1546
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
1547
+ * Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
1548
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
1549
+ * @module
1550
+ */
1551
+ // SHA2-512 is slower than sha256 in js because u64 operations are slow.
1552
+ // Round contants
1553
+ // First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
1554
+ // prettier-ignore
1555
+ const K512 = /* @__PURE__ */ (() => split([
1556
+ '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
1557
+ '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
1558
+ '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
1559
+ '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
1560
+ '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
1561
+ '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
1562
+ '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
1563
+ '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
1564
+ '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
1565
+ '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
1566
+ '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
1567
+ '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
1568
+ '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
1569
+ '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
1570
+ '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
1571
+ '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
1572
+ '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
1573
+ '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
1574
+ '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
1575
+ '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
1576
+ ].map(n => BigInt(n))))();
1577
+ const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
1578
+ const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
1579
+ // Reusable temporary buffers
1580
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
1581
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
1582
+ class SHA512 extends HashMD {
1583
+ constructor(outputLen = 64) {
1584
+ super(128, outputLen, 16, false);
1585
+ // We cannot use array here since array allows indexing by variable
1586
+ // which means optimizer/compiler cannot use registers.
1587
+ // h -- high 32 bits, l -- low 32 bits
1588
+ this.Ah = SHA512_IV[0] | 0;
1589
+ this.Al = SHA512_IV[1] | 0;
1590
+ this.Bh = SHA512_IV[2] | 0;
1591
+ this.Bl = SHA512_IV[3] | 0;
1592
+ this.Ch = SHA512_IV[4] | 0;
1593
+ this.Cl = SHA512_IV[5] | 0;
1594
+ this.Dh = SHA512_IV[6] | 0;
1595
+ this.Dl = SHA512_IV[7] | 0;
1596
+ this.Eh = SHA512_IV[8] | 0;
1597
+ this.El = SHA512_IV[9] | 0;
1598
+ this.Fh = SHA512_IV[10] | 0;
1599
+ this.Fl = SHA512_IV[11] | 0;
1600
+ this.Gh = SHA512_IV[12] | 0;
1601
+ this.Gl = SHA512_IV[13] | 0;
1602
+ this.Hh = SHA512_IV[14] | 0;
1603
+ this.Hl = SHA512_IV[15] | 0;
1604
+ }
1605
+ // prettier-ignore
1606
+ get() {
1607
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1608
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
1609
+ }
1610
+ // prettier-ignore
1611
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
1612
+ this.Ah = Ah | 0;
1613
+ this.Al = Al | 0;
1614
+ this.Bh = Bh | 0;
1615
+ this.Bl = Bl | 0;
1616
+ this.Ch = Ch | 0;
1617
+ this.Cl = Cl | 0;
1618
+ this.Dh = Dh | 0;
1619
+ this.Dl = Dl | 0;
1620
+ this.Eh = Eh | 0;
1621
+ this.El = El | 0;
1622
+ this.Fh = Fh | 0;
1623
+ this.Fl = Fl | 0;
1624
+ this.Gh = Gh | 0;
1625
+ this.Gl = Gl | 0;
1626
+ this.Hh = Hh | 0;
1627
+ this.Hl = Hl | 0;
1628
+ }
1629
+ process(view, offset) {
1630
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
1631
+ for (let i = 0; i < 16; i++, offset += 4) {
1632
+ SHA512_W_H[i] = view.getUint32(offset);
1633
+ SHA512_W_L[i] = view.getUint32((offset += 4));
1634
+ }
1635
+ for (let i = 16; i < 80; i++) {
1636
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
1637
+ const W15h = SHA512_W_H[i - 15] | 0;
1638
+ const W15l = SHA512_W_L[i - 15] | 0;
1639
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
1640
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
1641
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
1642
+ const W2h = SHA512_W_H[i - 2] | 0;
1643
+ const W2l = SHA512_W_L[i - 2] | 0;
1644
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
1645
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
1646
+ // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
1647
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
1648
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
1649
+ SHA512_W_H[i] = SUMh | 0;
1650
+ SHA512_W_L[i] = SUMl | 0;
1651
+ }
1652
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1653
+ // Compression function main loop, 80 rounds
1654
+ for (let i = 0; i < 80; i++) {
1655
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
1656
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
1657
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
1658
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
1659
+ const CHIh = (Eh & Fh) ^ (~Eh & Gh);
1660
+ const CHIl = (El & Fl) ^ (~El & Gl);
1661
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
1662
+ // prettier-ignore
1663
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
1664
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
1665
+ const T1l = T1ll | 0;
1666
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
1667
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
1668
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
1669
+ const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
1670
+ const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
1671
+ Hh = Gh | 0;
1672
+ Hl = Gl | 0;
1673
+ Gh = Fh | 0;
1674
+ Gl = Fl | 0;
1675
+ Fh = Eh | 0;
1676
+ Fl = El | 0;
1677
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
1678
+ Dh = Ch | 0;
1679
+ Dl = Cl | 0;
1680
+ Ch = Bh | 0;
1681
+ Cl = Bl | 0;
1682
+ Bh = Ah | 0;
1683
+ Bl = Al | 0;
1684
+ const All = add3L(T1l, sigma0l, MAJl);
1685
+ Ah = add3H(All, T1h, sigma0h, MAJh);
1686
+ Al = All | 0;
1687
+ }
1688
+ // Add the compressed chunk to the current hash value
1689
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
1690
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
1691
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
1692
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
1693
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
1694
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
1695
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
1696
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
1697
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
1698
+ }
1699
+ roundClean() {
1700
+ clean(SHA512_W_H, SHA512_W_L);
1701
+ }
1702
+ destroy() {
1703
+ clean(this.buffer);
1704
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1705
+ }
1706
+ }
1707
+ /** SHA2-512 hash function from RFC 4634. */
1708
+ const sha512$1 = /* @__PURE__ */ createHasher(() => new SHA512());
1709
+
1710
+ /**
1711
+ * SHA2-512 a.k.a. sha512 and sha384. It is slower than sha256 in js because u64 operations are slow.
1712
+ *
1713
+ * Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
1714
+ * [the paper on truncated SHA512/256](https://eprint.iacr.org/2010/548.pdf).
1715
+ * @module
1716
+ * @deprecated
1717
+ */
1718
+ /** @deprecated Use import from `noble/hashes/sha2` module */
1719
+ const sha512 = sha512$1;
1720
+
1721
+ /*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
1722
+ // Cast array to different type
1723
+ const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
1724
+ const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
1725
+ function isBytes$1(a) {
1726
+ return (a instanceof Uint8Array ||
1727
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
1728
+ }
1729
+ // Cast array to view
1730
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
1731
+ // big-endian hardware is rare. Just in case someone still decides to run ciphers:
1732
+ // early-throw an error because we don't support BE yet.
1733
+ const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
1734
+ if (!isLE)
1735
+ throw new Error('Non little-endian hardware is not supported');
1736
+ /**
1737
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
1738
+ */
1739
+ function utf8ToBytes(str) {
1740
+ if (typeof str !== 'string')
1741
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
1742
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
1743
+ }
1744
+ /**
1745
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
1746
+ * Warning: when Uint8Array is passed, it would NOT get copied.
1747
+ * Keep in mind for future mutable operations.
1748
+ */
1749
+ function toBytes(data) {
1750
+ if (typeof data === 'string')
1751
+ data = utf8ToBytes(data);
1752
+ else if (isBytes$1(data))
1753
+ data = data.slice();
1754
+ else
1755
+ throw new Error(`expected Uint8Array, got ${typeof data}`);
1756
+ return data;
1757
+ }
1758
+ function ensureBytes(b, len) {
1759
+ if (!isBytes$1(b))
1760
+ throw new Error('Uint8Array expected');
1761
+ if (typeof len === 'number')
1762
+ if (b.length !== len)
1763
+ throw new Error(`Uint8Array length ${len} expected`);
1764
+ }
1765
+ // Compares 2 u8a-s in kinda constant time
1766
+ function equalBytes(a, b) {
1767
+ if (a.length !== b.length)
1768
+ return false;
1769
+ let diff = 0;
1770
+ for (let i = 0; i < a.length; i++)
1771
+ diff |= a[i] ^ b[i];
1772
+ return diff === 0;
1773
+ }
1774
+ const wrapCipher = (params, c) => {
1775
+ Object.assign(c, params);
1776
+ return c;
1777
+ };
1778
+ // Polyfill for Safari 14
1779
+ function setBigUint64(view, byteOffset, value, isLE) {
1780
+ if (typeof view.setBigUint64 === 'function')
1781
+ return view.setBigUint64(byteOffset, value, isLE);
1782
+ const _32n = BigInt(32);
1783
+ const _u32_max = BigInt(0xffffffff);
1784
+ const wh = Number((value >> _32n) & _u32_max);
1785
+ const wl = Number(value & _u32_max);
1786
+ const h = isLE ? 4 : 0;
1787
+ const l = isLE ? 0 : 4;
1788
+ view.setUint32(byteOffset + h, wh, isLE);
1789
+ view.setUint32(byteOffset + l, wl, isLE);
1790
+ }
1791
+
1792
+ // TODO: merge with utils
1793
+ function isBytes(a) {
1794
+ return (a != null &&
1795
+ typeof a === 'object' &&
1796
+ (a instanceof Uint8Array || a.constructor.name === 'Uint8Array'));
1797
+ }
1798
+ function bytes(b, ...lengths) {
1799
+ if (!isBytes(b))
1800
+ throw new Error('Uint8Array expected');
1801
+ if (lengths.length > 0 && !lengths.includes(b.length))
1802
+ throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
1803
+ }
1804
+ function exists(instance, checkFinished = true) {
1805
+ if (instance.destroyed)
1806
+ throw new Error('Hash instance has been destroyed');
1807
+ if (checkFinished && instance.finished)
1808
+ throw new Error('Hash#digest() has already been called');
1809
+ }
1810
+ function output(out, instance) {
1811
+ bytes(out);
1812
+ const min = instance.outputLen;
1813
+ if (out.length < min) {
1814
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
1815
+ }
1816
+ }
1817
+
1818
+ // GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV.
1819
+ // Implemented in terms of GHash with conversion function for keys
1820
+ // GCM GHASH from NIST SP800-38d, SIV from RFC 8452.
1821
+ // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
1822
+ // GHASH modulo: x^128 + x^7 + x^2 + x + 1
1823
+ // POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1
1824
+ const BLOCK_SIZE$1 = 16;
1825
+ // TODO: rewrite
1826
+ // temporary padding buffer
1827
+ const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
1828
+ const ZEROS32 = u32(ZEROS16);
1829
+ const POLY$1 = 0xe1; // v = 2*v % POLY
1830
+ // v = 2*v % POLY
1831
+ // NOTE: because x + x = 0 (add/sub is same), mul2(x) != x+x
1832
+ // We can multiply any number using montgomery ladder and this function (works as double, add is simple xor)
1833
+ const mul2$1 = (s0, s1, s2, s3) => {
1834
+ const hiBit = s3 & 1;
1835
+ return {
1836
+ s3: (s2 << 31) | (s3 >>> 1),
1837
+ s2: (s1 << 31) | (s2 >>> 1),
1838
+ s1: (s0 << 31) | (s1 >>> 1),
1839
+ s0: (s0 >>> 1) ^ ((POLY$1 << 24) & -(hiBit & 1)), // reduce % poly
1840
+ };
1841
+ };
1842
+ const swapLE = (n) => (((n >>> 0) & 0xff) << 24) |
1843
+ (((n >>> 8) & 0xff) << 16) |
1844
+ (((n >>> 16) & 0xff) << 8) |
1845
+ ((n >>> 24) & 0xff) |
1846
+ 0;
1847
+ /**
1848
+ * `mulX_POLYVAL(ByteReverse(H))` from spec
1849
+ * @param k mutated in place
1850
+ */
1851
+ function _toGHASHKey(k) {
1852
+ k.reverse();
1853
+ const hiBit = k[15] & 1;
1854
+ // k >>= 1
1855
+ let carry = 0;
1856
+ for (let i = 0; i < k.length; i++) {
1857
+ const t = k[i];
1858
+ k[i] = (t >>> 1) | carry;
1859
+ carry = (t & 1) << 7;
1860
+ }
1861
+ k[0] ^= -hiBit & 0xe1; // if (hiBit) n ^= 0xe1000000000000000000000000000000;
1862
+ return k;
1863
+ }
1864
+ const estimateWindow = (bytes) => {
1865
+ if (bytes > 64 * 1024)
1866
+ return 8;
1867
+ if (bytes > 1024)
1868
+ return 4;
1869
+ return 2;
1870
+ };
1871
+ class GHASH {
1872
+ // We select bits per window adaptively based on expectedLength
1873
+ constructor(key, expectedLength) {
1874
+ this.blockLen = BLOCK_SIZE$1;
1875
+ this.outputLen = BLOCK_SIZE$1;
1876
+ this.s0 = 0;
1877
+ this.s1 = 0;
1878
+ this.s2 = 0;
1879
+ this.s3 = 0;
1880
+ this.finished = false;
1881
+ key = toBytes(key);
1882
+ ensureBytes(key, 16);
1883
+ const kView = createView(key);
1884
+ let k0 = kView.getUint32(0, false);
1885
+ let k1 = kView.getUint32(4, false);
1886
+ let k2 = kView.getUint32(8, false);
1887
+ let k3 = kView.getUint32(12, false);
1888
+ // generate table of doubled keys (half of montgomery ladder)
1889
+ const doubles = [];
1890
+ for (let i = 0; i < 128; i++) {
1891
+ doubles.push({ s0: swapLE(k0), s1: swapLE(k1), s2: swapLE(k2), s3: swapLE(k3) });
1892
+ ({ s0: k0, s1: k1, s2: k2, s3: k3 } = mul2$1(k0, k1, k2, k3));
1893
+ }
1894
+ const W = estimateWindow(expectedLength || 1024);
1895
+ if (![1, 2, 4, 8].includes(W))
1896
+ throw new Error(`ghash: wrong window size=${W}, should be 2, 4 or 8`);
1897
+ this.W = W;
1898
+ const bits = 128; // always 128 bits;
1899
+ const windows = bits / W;
1900
+ const windowSize = (this.windowSize = 2 ** W);
1901
+ const items = [];
1902
+ // Create precompute table for window of W bits
1903
+ for (let w = 0; w < windows; w++) {
1904
+ // truth table: 00, 01, 10, 11
1905
+ for (let byte = 0; byte < windowSize; byte++) {
1906
+ // prettier-ignore
1907
+ let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
1908
+ for (let j = 0; j < W; j++) {
1909
+ const bit = (byte >>> (W - j - 1)) & 1;
1910
+ if (!bit)
1911
+ continue;
1912
+ const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
1913
+ (s0 ^= d0), (s1 ^= d1), (s2 ^= d2), (s3 ^= d3);
1914
+ }
1915
+ items.push({ s0, s1, s2, s3 });
1916
+ }
1917
+ }
1918
+ this.t = items;
1919
+ }
1920
+ _updateBlock(s0, s1, s2, s3) {
1921
+ (s0 ^= this.s0), (s1 ^= this.s1), (s2 ^= this.s2), (s3 ^= this.s3);
1922
+ const { W, t, windowSize } = this;
1923
+ // prettier-ignore
1924
+ let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
1925
+ const mask = (1 << W) - 1; // 2**W will kill performance.
1926
+ let w = 0;
1927
+ for (const num of [s0, s1, s2, s3]) {
1928
+ for (let bytePos = 0; bytePos < 4; bytePos++) {
1929
+ const byte = (num >>> (8 * bytePos)) & 0xff;
1930
+ for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
1931
+ const bit = (byte >>> (W * bitPos)) & mask;
1932
+ const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
1933
+ (o0 ^= e0), (o1 ^= e1), (o2 ^= e2), (o3 ^= e3);
1934
+ w += 1;
1935
+ }
1936
+ }
1937
+ }
1938
+ this.s0 = o0;
1939
+ this.s1 = o1;
1940
+ this.s2 = o2;
1941
+ this.s3 = o3;
1942
+ }
1943
+ update(data) {
1944
+ data = toBytes(data);
1945
+ exists(this);
1946
+ const b32 = u32(data);
1947
+ const blocks = Math.floor(data.length / BLOCK_SIZE$1);
1948
+ const left = data.length % BLOCK_SIZE$1;
1949
+ for (let i = 0; i < blocks; i++) {
1950
+ this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
1951
+ }
1952
+ if (left) {
1953
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
1954
+ this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
1955
+ ZEROS32.fill(0); // clean tmp buffer
1956
+ }
1957
+ return this;
1958
+ }
1959
+ destroy() {
1960
+ const { t } = this;
1961
+ // clean precompute table
1962
+ for (const elm of t) {
1963
+ (elm.s0 = 0), (elm.s1 = 0), (elm.s2 = 0), (elm.s3 = 0);
1964
+ }
1965
+ }
1966
+ digestInto(out) {
1967
+ exists(this);
1968
+ output(out, this);
1969
+ this.finished = true;
1970
+ const { s0, s1, s2, s3 } = this;
1971
+ const o32 = u32(out);
1972
+ o32[0] = s0;
1973
+ o32[1] = s1;
1974
+ o32[2] = s2;
1975
+ o32[3] = s3;
1976
+ return out;
1977
+ }
1978
+ digest() {
1979
+ const res = new Uint8Array(BLOCK_SIZE$1);
1980
+ this.digestInto(res);
1981
+ this.destroy();
1982
+ return res;
1983
+ }
1984
+ }
1985
+ class Polyval extends GHASH {
1986
+ constructor(key, expectedLength) {
1987
+ key = toBytes(key);
1988
+ const ghKey = _toGHASHKey(key.slice());
1989
+ super(ghKey, expectedLength);
1990
+ ghKey.fill(0);
1991
+ }
1992
+ update(data) {
1993
+ data = toBytes(data);
1994
+ exists(this);
1995
+ const b32 = u32(data);
1996
+ const left = data.length % BLOCK_SIZE$1;
1997
+ const blocks = Math.floor(data.length / BLOCK_SIZE$1);
1998
+ for (let i = 0; i < blocks; i++) {
1999
+ this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
2000
+ }
2001
+ if (left) {
2002
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
2003
+ this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
2004
+ ZEROS32.fill(0); // clean tmp buffer
2005
+ }
2006
+ return this;
2007
+ }
2008
+ digestInto(out) {
2009
+ exists(this);
2010
+ output(out, this);
2011
+ this.finished = true;
2012
+ // tmp ugly hack
2013
+ const { s0, s1, s2, s3 } = this;
2014
+ const o32 = u32(out);
2015
+ o32[0] = s0;
2016
+ o32[1] = s1;
2017
+ o32[2] = s2;
2018
+ o32[3] = s3;
2019
+ return out.reverse();
2020
+ }
2021
+ }
2022
+ function wrapConstructorWithKey(hashCons) {
2023
+ const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
2024
+ const tmp = hashCons(new Uint8Array(16), 0);
2025
+ hashC.outputLen = tmp.outputLen;
2026
+ hashC.blockLen = tmp.blockLen;
2027
+ hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
2028
+ return hashC;
2029
+ }
2030
+ const ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
2031
+ const polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
2032
+
2033
+ // AES (Advanced Encryption Standard) aka Rijndael block cipher.
2034
+ //
2035
+ // Data is split into 128-bit blocks. Encrypted in 10/12/14 rounds (128/192/256bit). Every round:
2036
+ // 1. **S-box**, table substitution
2037
+ // 2. **Shift rows**, cyclic shift left of all rows of data array
2038
+ // 3. **Mix columns**, multiplying every column by fixed polynomial
2039
+ // 4. **Add round key**, round_key xor i-th column of array
2040
+ //
2041
+ // Resources:
2042
+ // - FIPS-197 https://csrc.nist.gov/files/pubs/fips/197/final/docs/fips-197.pdf
2043
+ // - Original proposal: https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/aes-development/rijndael-ammended.pdf
2044
+ const BLOCK_SIZE = 16;
2045
+ const BLOCK_SIZE32 = 4;
2046
+ const EMPTY_BLOCK = new Uint8Array(BLOCK_SIZE);
2047
+ const POLY = 0x11b; // 1 + x + x**3 + x**4 + x**8
2048
+ // TODO: remove multiplication, binary ops only
2049
+ function mul2(n) {
2050
+ return (n << 1) ^ (POLY & -(n >> 7));
2051
+ }
2052
+ function mul(a, b) {
2053
+ let res = 0;
2054
+ for (; b > 0; b >>= 1) {
2055
+ // Montgomery ladder
2056
+ res ^= a & -(b & 1); // if (b&1) res ^=a (but const-time).
2057
+ a = mul2(a); // a = 2*a
2058
+ }
2059
+ return res;
2060
+ }
2061
+ // AES S-box is generated using finite field inversion,
2062
+ // an affine transform, and xor of a constant 0x63.
2063
+ const _sbox = /* @__PURE__ */ (() => {
2064
+ let t = new Uint8Array(256);
2065
+ for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x))
2066
+ t[i] = x;
2067
+ const sbox = new Uint8Array(256);
2068
+ sbox[0] = 0x63; // first elm
2069
+ for (let i = 0; i < 255; i++) {
2070
+ let x = t[255 - i];
2071
+ x |= x << 8;
2072
+ sbox[t[i]] = (x ^ (x >> 4) ^ (x >> 5) ^ (x >> 6) ^ (x >> 7) ^ 0x63) & 0xff;
2073
+ }
2074
+ return sbox;
2075
+ })();
2076
+ // Inverted S-box
2077
+ const _inv_sbox = /* @__PURE__ */ _sbox.map((_, j) => _sbox.indexOf(j));
2078
+ // Rotate u32 by 8
2079
+ const rotr32_8 = (n) => (n << 24) | (n >>> 8);
2080
+ const rotl32_8 = (n) => (n << 8) | (n >>> 24);
2081
+ // T-table is optimization suggested in 5.2 of original proposal (missed from FIPS-197). Changes:
2082
+ // - LE instead of BE
2083
+ // - bigger tables: T0 and T1 are merged into T01 table and T2 & T3 into T23;
2084
+ // so index is u16, instead of u8. This speeds up things, unexpectedly
2085
+ function genTtable(sbox, fn) {
2086
+ if (sbox.length !== 256)
2087
+ throw new Error('Wrong sbox length');
2088
+ const T0 = new Uint32Array(256).map((_, j) => fn(sbox[j]));
2089
+ const T1 = T0.map(rotl32_8);
2090
+ const T2 = T1.map(rotl32_8);
2091
+ const T3 = T2.map(rotl32_8);
2092
+ const T01 = new Uint32Array(256 * 256);
2093
+ const T23 = new Uint32Array(256 * 256);
2094
+ const sbox2 = new Uint16Array(256 * 256);
2095
+ for (let i = 0; i < 256; i++) {
2096
+ for (let j = 0; j < 256; j++) {
2097
+ const idx = i * 256 + j;
2098
+ T01[idx] = T0[i] ^ T1[j];
2099
+ T23[idx] = T2[i] ^ T3[j];
2100
+ sbox2[idx] = (sbox[i] << 8) | sbox[j];
2101
+ }
2102
+ }
2103
+ return { sbox, sbox2, T0, T1, T2, T3, T01, T23 };
2104
+ }
2105
+ const TABLE_ENC = /* @__PURE__ */ genTtable(_sbox, (s) => (mul(s, 3) << 24) | (s << 16) | (s << 8) | mul(s, 2));
2106
+ const TABLE_DEC = /* @__PURE__ */ genTtable(_inv_sbox, (s) => (mul(s, 11) << 24) | (mul(s, 13) << 16) | (mul(s, 9) << 8) | mul(s, 14));
2107
+ const POWX = /* @__PURE__ */ (() => {
2108
+ const p = new Uint8Array(16);
2109
+ for (let i = 0, x = 1; i < 16; i++, x = mul2(x))
2110
+ p[i] = x;
2111
+ return p;
2112
+ })();
2113
+ function expandKeyLE(key) {
2114
+ ensureBytes(key);
2115
+ const len = key.length;
2116
+ if (![16, 24, 32].includes(len))
2117
+ throw new Error(`aes: wrong key size: should be 16, 24 or 32, got: ${len}`);
2118
+ const { sbox2 } = TABLE_ENC;
2119
+ const k32 = u32(key);
2120
+ const Nk = k32.length;
2121
+ const subByte = (n) => applySbox(sbox2, n, n, n, n);
2122
+ const xk = new Uint32Array(len + 28); // expanded key
2123
+ xk.set(k32);
2124
+ // 4.3.1 Key expansion
2125
+ for (let i = Nk; i < xk.length; i++) {
2126
+ let t = xk[i - 1];
2127
+ if (i % Nk === 0)
2128
+ t = subByte(rotr32_8(t)) ^ POWX[i / Nk - 1];
2129
+ else if (Nk > 6 && i % Nk === 4)
2130
+ t = subByte(t);
2131
+ xk[i] = xk[i - Nk] ^ t;
2132
+ }
2133
+ return xk;
2134
+ }
2135
+ function expandKeyDecLE(key) {
2136
+ const encKey = expandKeyLE(key);
2137
+ const xk = encKey.slice();
2138
+ const Nk = encKey.length;
2139
+ const { sbox2 } = TABLE_ENC;
2140
+ const { T0, T1, T2, T3 } = TABLE_DEC;
2141
+ // Inverse key by chunks of 4 (rounds)
2142
+ for (let i = 0; i < Nk; i += 4) {
2143
+ for (let j = 0; j < 4; j++)
2144
+ xk[i + j] = encKey[Nk - i - 4 + j];
2145
+ }
2146
+ encKey.fill(0);
2147
+ // apply InvMixColumn except first & last round
2148
+ for (let i = 4; i < Nk - 4; i++) {
2149
+ const x = xk[i];
2150
+ const w = applySbox(sbox2, x, x, x, x);
2151
+ xk[i] = T0[w & 0xff] ^ T1[(w >>> 8) & 0xff] ^ T2[(w >>> 16) & 0xff] ^ T3[w >>> 24];
2152
+ }
2153
+ return xk;
2154
+ }
2155
+ // Apply tables
2156
+ function apply0123(T01, T23, s0, s1, s2, s3) {
2157
+ return (T01[((s0 << 8) & 0xff00) | ((s1 >>> 8) & 0xff)] ^
2158
+ T23[((s2 >>> 8) & 0xff00) | ((s3 >>> 24) & 0xff)]);
2159
+ }
2160
+ function applySbox(sbox2, s0, s1, s2, s3) {
2161
+ return (sbox2[(s0 & 0xff) | (s1 & 0xff00)] |
2162
+ (sbox2[((s2 >>> 16) & 0xff) | ((s3 >>> 16) & 0xff00)] << 16));
2163
+ }
2164
+ function encrypt(xk, s0, s1, s2, s3) {
2165
+ const { sbox2, T01, T23 } = TABLE_ENC;
2166
+ let k = 0;
2167
+ (s0 ^= xk[k++]), (s1 ^= xk[k++]), (s2 ^= xk[k++]), (s3 ^= xk[k++]);
2168
+ const rounds = xk.length / 4 - 2;
2169
+ for (let i = 0; i < rounds; i++) {
2170
+ const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
2171
+ const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
2172
+ const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
2173
+ const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
2174
+ (s0 = t0), (s1 = t1), (s2 = t2), (s3 = t3);
2175
+ }
2176
+ // last round (without mixcolumns, so using SBOX2 table)
2177
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
2178
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
2179
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
2180
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
2181
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
2182
+ }
2183
+ function decrypt(xk, s0, s1, s2, s3) {
2184
+ const { sbox2, T01, T23 } = TABLE_DEC;
2185
+ let k = 0;
2186
+ (s0 ^= xk[k++]), (s1 ^= xk[k++]), (s2 ^= xk[k++]), (s3 ^= xk[k++]);
2187
+ const rounds = xk.length / 4 - 2;
2188
+ for (let i = 0; i < rounds; i++) {
2189
+ const t0 = xk[k++] ^ apply0123(T01, T23, s0, s3, s2, s1);
2190
+ const t1 = xk[k++] ^ apply0123(T01, T23, s1, s0, s3, s2);
2191
+ const t2 = xk[k++] ^ apply0123(T01, T23, s2, s1, s0, s3);
2192
+ const t3 = xk[k++] ^ apply0123(T01, T23, s3, s2, s1, s0);
2193
+ (s0 = t0), (s1 = t1), (s2 = t2), (s3 = t3);
2194
+ }
2195
+ // Last round
2196
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s3, s2, s1);
2197
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s0, s3, s2);
2198
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s1, s0, s3);
2199
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s2, s1, s0);
2200
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
2201
+ }
2202
+ function getDst(len, dst) {
2203
+ if (!dst)
2204
+ return new Uint8Array(len);
2205
+ ensureBytes(dst);
2206
+ if (dst.length < len)
2207
+ throw new Error(`aes: wrong destination length, expected at least ${len}, got: ${dst.length}`);
2208
+ return dst;
2209
+ }
2210
+ // TODO: investigate merging with ctr32
2211
+ function ctrCounter(xk, nonce, src, dst) {
2212
+ ensureBytes(nonce, BLOCK_SIZE);
2213
+ ensureBytes(src);
2214
+ const srcLen = src.length;
2215
+ dst = getDst(srcLen, dst);
2216
+ const ctr = nonce;
2217
+ const c32 = u32(ctr);
2218
+ // Fill block (empty, ctr=0)
2219
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
2220
+ const src32 = u32(src);
2221
+ const dst32 = u32(dst);
2222
+ // process blocks
2223
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
2224
+ dst32[i + 0] = src32[i + 0] ^ s0;
2225
+ dst32[i + 1] = src32[i + 1] ^ s1;
2226
+ dst32[i + 2] = src32[i + 2] ^ s2;
2227
+ dst32[i + 3] = src32[i + 3] ^ s3;
2228
+ // Full 128 bit counter with wrap around
2229
+ let carry = 1;
2230
+ for (let i = ctr.length - 1; i >= 0; i--) {
2231
+ carry = (carry + (ctr[i] & 0xff)) | 0;
2232
+ ctr[i] = carry & 0xff;
2233
+ carry >>>= 8;
2234
+ }
2235
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
2236
+ }
2237
+ // leftovers (less than block)
2238
+ // It's possible to handle > u32 fast, but is it worth it?
2239
+ const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
2240
+ if (start < srcLen) {
2241
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
2242
+ const buf = u8(b32);
2243
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
2244
+ dst[i] = src[i] ^ buf[pos];
2245
+ }
2246
+ return dst;
2247
+ }
2248
+ // AES CTR with overflowing 32 bit counter
2249
+ // It's possible to do 32le significantly simpler (and probably faster) by using u32.
2250
+ // But, we need both, and perf bottleneck is in ghash anyway.
2251
+ function ctr32(xk, isLE, nonce, src, dst) {
2252
+ ensureBytes(nonce, BLOCK_SIZE);
2253
+ ensureBytes(src);
2254
+ dst = getDst(src.length, dst);
2255
+ const ctr = nonce; // write new value to nonce, so it can be re-used
2256
+ const c32 = u32(ctr);
2257
+ const view = createView(ctr);
2258
+ const src32 = u32(src);
2259
+ const dst32 = u32(dst);
2260
+ const ctrPos = isLE ? 0 : 12;
2261
+ const srcLen = src.length;
2262
+ // Fill block (empty, ctr=0)
2263
+ let ctrNum = view.getUint32(ctrPos, isLE); // read current counter value
2264
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
2265
+ // process blocks
2266
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
2267
+ dst32[i + 0] = src32[i + 0] ^ s0;
2268
+ dst32[i + 1] = src32[i + 1] ^ s1;
2269
+ dst32[i + 2] = src32[i + 2] ^ s2;
2270
+ dst32[i + 3] = src32[i + 3] ^ s3;
2271
+ ctrNum = (ctrNum + 1) >>> 0; // u32 wrap
2272
+ view.setUint32(ctrPos, ctrNum, isLE);
2273
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
2274
+ }
2275
+ // leftovers (less than a block)
2276
+ const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
2277
+ if (start < srcLen) {
2278
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
2279
+ const buf = u8(b32);
2280
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
2281
+ dst[i] = src[i] ^ buf[pos];
2282
+ }
2283
+ return dst;
2284
+ }
2285
+ /**
2286
+ * CTR: counter mode. Creates stream cipher.
2287
+ * Requires good IV. Parallelizable. OK, but no MAC.
2288
+ */
2289
+ wrapCipher({ blockSize: 16, nonceLength: 16 }, function ctr(key, nonce) {
2290
+ ensureBytes(key);
2291
+ ensureBytes(nonce, BLOCK_SIZE);
2292
+ function processCtr(buf, dst) {
2293
+ const xk = expandKeyLE(key);
2294
+ const n = nonce.slice();
2295
+ const out = ctrCounter(xk, n, buf, dst);
2296
+ xk.fill(0);
2297
+ n.fill(0);
2298
+ return out;
2299
+ }
2300
+ return {
2301
+ encrypt: (plaintext, dst) => processCtr(plaintext, dst),
2302
+ decrypt: (ciphertext, dst) => processCtr(ciphertext, dst),
2303
+ };
2304
+ });
2305
+ function validateBlockDecrypt(data) {
2306
+ ensureBytes(data);
2307
+ if (data.length % BLOCK_SIZE !== 0) {
2308
+ throw new Error(`aes/(cbc-ecb).decrypt ciphertext should consist of blocks with size ${BLOCK_SIZE}`);
2309
+ }
2310
+ }
2311
+ function validateBlockEncrypt(plaintext, pcks5, dst) {
2312
+ let outLen = plaintext.length;
2313
+ const remaining = outLen % BLOCK_SIZE;
2314
+ if (!pcks5 && remaining !== 0)
2315
+ throw new Error('aec/(cbc-ecb): unpadded plaintext with disabled padding');
2316
+ const b = u32(plaintext);
2317
+ if (pcks5) {
2318
+ let left = BLOCK_SIZE - remaining;
2319
+ if (!left)
2320
+ left = BLOCK_SIZE; // if no bytes left, create empty padding block
2321
+ outLen = outLen + left;
2322
+ }
2323
+ const out = getDst(outLen, dst);
2324
+ const o = u32(out);
2325
+ return { b, o, out };
2326
+ }
2327
+ function validatePCKS(data, pcks5) {
2328
+ if (!pcks5)
2329
+ return data;
2330
+ const len = data.length;
2331
+ if (!len)
2332
+ throw new Error(`aes/pcks5: empty ciphertext not allowed`);
2333
+ const lastByte = data[len - 1];
2334
+ if (lastByte <= 0 || lastByte > 16)
2335
+ throw new Error(`aes/pcks5: wrong padding byte: ${lastByte}`);
2336
+ const out = data.subarray(0, -lastByte);
2337
+ for (let i = 0; i < lastByte; i++)
2338
+ if (data[len - i - 1] !== lastByte)
2339
+ throw new Error(`aes/pcks5: wrong padding`);
2340
+ return out;
2341
+ }
2342
+ function padPCKS(left) {
2343
+ const tmp = new Uint8Array(16);
2344
+ const tmp32 = u32(tmp);
2345
+ tmp.set(left);
2346
+ const paddingByte = BLOCK_SIZE - left.length;
2347
+ for (let i = BLOCK_SIZE - paddingByte; i < BLOCK_SIZE; i++)
2348
+ tmp[i] = paddingByte;
2349
+ return tmp32;
2350
+ }
2351
+ /**
2352
+ * ECB: Electronic CodeBook. Simple deterministic replacement.
2353
+ * Dangerous: always map x to y. See [AES Penguin](https://words.filippo.io/the-ecb-penguin/).
2354
+ */
2355
+ wrapCipher({ blockSize: 16 }, function ecb(key, opts = {}) {
2356
+ ensureBytes(key);
2357
+ const pcks5 = !opts.disablePadding;
2358
+ return {
2359
+ encrypt: (plaintext, dst) => {
2360
+ ensureBytes(plaintext);
2361
+ const { b, o, out: _out } = validateBlockEncrypt(plaintext, pcks5, dst);
2362
+ const xk = expandKeyLE(key);
2363
+ let i = 0;
2364
+ for (; i + 4 <= b.length;) {
2365
+ const { s0, s1, s2, s3 } = encrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
2366
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
2367
+ }
2368
+ if (pcks5) {
2369
+ const tmp32 = padPCKS(plaintext.subarray(i * 4));
2370
+ const { s0, s1, s2, s3 } = encrypt(xk, tmp32[0], tmp32[1], tmp32[2], tmp32[3]);
2371
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
2372
+ }
2373
+ xk.fill(0);
2374
+ return _out;
2375
+ },
2376
+ decrypt: (ciphertext, dst) => {
2377
+ validateBlockDecrypt(ciphertext);
2378
+ const xk = expandKeyDecLE(key);
2379
+ const out = getDst(ciphertext.length, dst);
2380
+ const b = u32(ciphertext);
2381
+ const o = u32(out);
2382
+ for (let i = 0; i + 4 <= b.length;) {
2383
+ const { s0, s1, s2, s3 } = decrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
2384
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
2385
+ }
2386
+ xk.fill(0);
2387
+ return validatePCKS(out, pcks5);
2388
+ },
2389
+ };
2390
+ });
2391
+ /**
2392
+ * CBC: Cipher-Block-Chaining. Key is previous round’s block.
2393
+ * Fragile: needs proper padding. Unauthenticated: needs MAC.
2394
+ */
2395
+ wrapCipher({ blockSize: 16, nonceLength: 16 }, function cbc(key, iv, opts = {}) {
2396
+ ensureBytes(key);
2397
+ ensureBytes(iv, 16);
2398
+ const pcks5 = !opts.disablePadding;
2399
+ return {
2400
+ encrypt: (plaintext, dst) => {
2401
+ const xk = expandKeyLE(key);
2402
+ const { b, o, out: _out } = validateBlockEncrypt(plaintext, pcks5, dst);
2403
+ const n32 = u32(iv);
2404
+ // prettier-ignore
2405
+ let s0 = n32[0], s1 = n32[1], s2 = n32[2], s3 = n32[3];
2406
+ let i = 0;
2407
+ for (; i + 4 <= b.length;) {
2408
+ (s0 ^= b[i + 0]), (s1 ^= b[i + 1]), (s2 ^= b[i + 2]), (s3 ^= b[i + 3]);
2409
+ ({ s0, s1, s2, s3 } = encrypt(xk, s0, s1, s2, s3));
2410
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
2411
+ }
2412
+ if (pcks5) {
2413
+ const tmp32 = padPCKS(plaintext.subarray(i * 4));
2414
+ (s0 ^= tmp32[0]), (s1 ^= tmp32[1]), (s2 ^= tmp32[2]), (s3 ^= tmp32[3]);
2415
+ ({ s0, s1, s2, s3 } = encrypt(xk, s0, s1, s2, s3));
2416
+ (o[i++] = s0), (o[i++] = s1), (o[i++] = s2), (o[i++] = s3);
2417
+ }
2418
+ xk.fill(0);
2419
+ return _out;
2420
+ },
2421
+ decrypt: (ciphertext, dst) => {
2422
+ validateBlockDecrypt(ciphertext);
2423
+ const xk = expandKeyDecLE(key);
2424
+ const n32 = u32(iv);
2425
+ const out = getDst(ciphertext.length, dst);
2426
+ const b = u32(ciphertext);
2427
+ const o = u32(out);
2428
+ // prettier-ignore
2429
+ let s0 = n32[0], s1 = n32[1], s2 = n32[2], s3 = n32[3];
2430
+ for (let i = 0; i + 4 <= b.length;) {
2431
+ // prettier-ignore
2432
+ const ps0 = s0, ps1 = s1, ps2 = s2, ps3 = s3;
2433
+ (s0 = b[i + 0]), (s1 = b[i + 1]), (s2 = b[i + 2]), (s3 = b[i + 3]);
2434
+ const { s0: o0, s1: o1, s2: o2, s3: o3 } = decrypt(xk, s0, s1, s2, s3);
2435
+ (o[i++] = o0 ^ ps0), (o[i++] = o1 ^ ps1), (o[i++] = o2 ^ ps2), (o[i++] = o3 ^ ps3);
2436
+ }
2437
+ xk.fill(0);
2438
+ return validatePCKS(out, pcks5);
2439
+ },
2440
+ };
2441
+ });
2442
+ // TODO: merge with chacha, however gcm has bitLen while chacha has byteLen
2443
+ function computeTag(fn, isLE, key, data, AAD) {
2444
+ const h = fn.create(key, data.length + (AAD?.length || 0));
2445
+ if (AAD)
2446
+ h.update(AAD);
2447
+ h.update(data);
2448
+ const num = new Uint8Array(16);
2449
+ const view = createView(num);
2450
+ if (AAD)
2451
+ setBigUint64(view, 0, BigInt(AAD.length * 8), isLE);
2452
+ setBigUint64(view, 8, BigInt(data.length * 8), isLE);
2453
+ h.update(num);
2454
+ return h.digest();
2455
+ }
2456
+ /**
2457
+ * GCM: Galois/Counter Mode.
2458
+ * Good, modern version of CTR, parallel, with MAC.
2459
+ * Be careful: MACs can be forged.
2460
+ */
2461
+ const gcm = wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16 }, function gcm(key, nonce, AAD) {
2462
+ ensureBytes(nonce);
2463
+ // Nonce can be pretty much anything (even 1 byte). But smaller nonces less secure.
2464
+ if (nonce.length === 0)
2465
+ throw new Error('aes/gcm: empty nonce');
2466
+ const tagLength = 16;
2467
+ function _computeTag(authKey, tagMask, data) {
2468
+ const tag = computeTag(ghash, false, authKey, data, AAD);
2469
+ for (let i = 0; i < tagMask.length; i++)
2470
+ tag[i] ^= tagMask[i];
2471
+ return tag;
2472
+ }
2473
+ function deriveKeys() {
2474
+ const xk = expandKeyLE(key);
2475
+ const authKey = EMPTY_BLOCK.slice();
2476
+ const counter = EMPTY_BLOCK.slice();
2477
+ ctr32(xk, false, counter, counter, authKey);
2478
+ if (nonce.length === 12) {
2479
+ counter.set(nonce);
2480
+ }
2481
+ else {
2482
+ // Spec (NIST 800-38d) supports variable size nonce.
2483
+ // Not supported for now, but can be useful.
2484
+ const nonceLen = EMPTY_BLOCK.slice();
2485
+ const view = createView(nonceLen);
2486
+ setBigUint64(view, 8, BigInt(nonce.length * 8), false);
2487
+ // ghash(nonce || u64be(0) || u64be(nonceLen*8))
2488
+ ghash.create(authKey).update(nonce).update(nonceLen).digestInto(counter);
2489
+ }
2490
+ const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
2491
+ return { xk, authKey, counter, tagMask };
2492
+ }
2493
+ return {
2494
+ encrypt: (plaintext) => {
2495
+ ensureBytes(plaintext);
2496
+ const { xk, authKey, counter, tagMask } = deriveKeys();
2497
+ const out = new Uint8Array(plaintext.length + tagLength);
2498
+ ctr32(xk, false, counter, plaintext, out);
2499
+ const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
2500
+ out.set(tag, plaintext.length);
2501
+ xk.fill(0);
2502
+ return out;
2503
+ },
2504
+ decrypt: (ciphertext) => {
2505
+ ensureBytes(ciphertext);
2506
+ if (ciphertext.length < tagLength)
2507
+ throw new Error(`aes/gcm: ciphertext less than tagLen (${tagLength})`);
2508
+ const { xk, authKey, counter, tagMask } = deriveKeys();
2509
+ const data = ciphertext.subarray(0, -tagLength);
2510
+ const passedTag = ciphertext.subarray(-tagLength);
2511
+ const tag = _computeTag(authKey, tagMask, data);
2512
+ if (!equalBytes(tag, passedTag))
2513
+ throw new Error('aes/gcm: invalid ghash tag');
2514
+ const out = ctr32(xk, false, counter, data);
2515
+ authKey.fill(0);
2516
+ tagMask.fill(0);
2517
+ xk.fill(0);
2518
+ return out;
2519
+ },
2520
+ };
2521
+ });
2522
+ const limit = (name, min, max) => (value) => {
2523
+ if (!Number.isSafeInteger(value) || min > value || value > max)
2524
+ throw new Error(`${name}: invalid value=${value}, must be [${min}..${max}]`);
2525
+ };
2526
+ /**
2527
+ * AES-GCM-SIV: classic AES-GCM with nonce-misuse resistance.
2528
+ * Guarantees that, when a nonce is repeated, the only security loss is that identical
2529
+ * plaintexts will produce identical ciphertexts.
2530
+ * RFC 8452, https://datatracker.ietf.org/doc/html/rfc8452
2531
+ */
2532
+ wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16 }, function siv(key, nonce, AAD) {
2533
+ const tagLength = 16;
2534
+ // From RFC 8452: Section 6
2535
+ const AAD_LIMIT = limit('AAD', 0, 2 ** 36);
2536
+ const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 36);
2537
+ const NONCE_LIMIT = limit('nonce', 12, 12);
2538
+ const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 36 + 16);
2539
+ ensureBytes(nonce);
2540
+ NONCE_LIMIT(nonce.length);
2541
+ if (AAD) {
2542
+ ensureBytes(AAD);
2543
+ AAD_LIMIT(AAD.length);
2544
+ }
2545
+ function deriveKeys() {
2546
+ const len = key.length;
2547
+ if (len !== 16 && len !== 24 && len !== 32)
2548
+ throw new Error(`key length must be 16, 24 or 32 bytes, got: ${len} bytes`);
2549
+ const xk = expandKeyLE(key);
2550
+ const encKey = new Uint8Array(len);
2551
+ const authKey = new Uint8Array(16);
2552
+ const n32 = u32(nonce);
2553
+ // prettier-ignore
2554
+ let s0 = 0, s1 = n32[0], s2 = n32[1], s3 = n32[2];
2555
+ let counter = 0;
2556
+ for (const derivedKey of [authKey, encKey].map(u32)) {
2557
+ const d32 = u32(derivedKey);
2558
+ for (let i = 0; i < d32.length; i += 2) {
2559
+ // aes(u32le(0) || nonce)[:8] || aes(u32le(1) || nonce)[:8] ...
2560
+ const { s0: o0, s1: o1 } = encrypt(xk, s0, s1, s2, s3);
2561
+ d32[i + 0] = o0;
2562
+ d32[i + 1] = o1;
2563
+ s0 = ++counter; // increment counter inside state
2564
+ }
2565
+ }
2566
+ xk.fill(0);
2567
+ return { authKey, encKey: expandKeyLE(encKey) };
2568
+ }
2569
+ function _computeTag(encKey, authKey, data) {
2570
+ const tag = computeTag(polyval, true, authKey, data, AAD);
2571
+ // Compute the expected tag by XORing S_s and the nonce, clearing the
2572
+ // most significant bit of the last byte and encrypting with the
2573
+ // message-encryption key.
2574
+ for (let i = 0; i < 12; i++)
2575
+ tag[i] ^= nonce[i];
2576
+ tag[15] &= 0x7f; // Clear the highest bit
2577
+ // encrypt tag as block
2578
+ const t32 = u32(tag);
2579
+ // prettier-ignore
2580
+ let s0 = t32[0], s1 = t32[1], s2 = t32[2], s3 = t32[3];
2581
+ ({ s0, s1, s2, s3 } = encrypt(encKey, s0, s1, s2, s3));
2582
+ (t32[0] = s0), (t32[1] = s1), (t32[2] = s2), (t32[3] = s3);
2583
+ return tag;
2584
+ }
2585
+ // actual decrypt/encrypt of message.
2586
+ function processSiv(encKey, tag, input) {
2587
+ let block = tag.slice();
2588
+ block[15] |= 0x80; // Force highest bit
2589
+ return ctr32(encKey, true, block, input);
2590
+ }
2591
+ return {
2592
+ encrypt: (plaintext) => {
2593
+ ensureBytes(plaintext);
2594
+ PLAIN_LIMIT(plaintext.length);
2595
+ const { encKey, authKey } = deriveKeys();
2596
+ const tag = _computeTag(encKey, authKey, plaintext);
2597
+ const out = new Uint8Array(plaintext.length + tagLength);
2598
+ out.set(tag, plaintext.length);
2599
+ out.set(processSiv(encKey, tag, plaintext));
2600
+ encKey.fill(0);
2601
+ authKey.fill(0);
2602
+ return out;
2603
+ },
2604
+ decrypt: (ciphertext) => {
2605
+ ensureBytes(ciphertext);
2606
+ CIPHER_LIMIT(ciphertext.length);
2607
+ const tag = ciphertext.subarray(-tagLength);
2608
+ const { encKey, authKey } = deriveKeys();
2609
+ const plaintext = processSiv(encKey, tag, ciphertext.subarray(0, -tagLength));
2610
+ const expectedTag = _computeTag(encKey, authKey, plaintext);
2611
+ encKey.fill(0);
2612
+ authKey.fill(0);
2613
+ if (!equalBytes(tag, expectedTag))
2614
+ throw new Error('invalid polyval tag');
2615
+ return plaintext;
2616
+ },
2617
+ };
2618
+ });
2619
+
2620
+ const ENC_ITER_DEFAULT = 100_000;
2621
+ const IV_LEN = 12;
2622
+ const SALT_LEN = 16;
2623
+ function getIterations() {
2624
+ if (typeof process !== 'undefined' && process.env?.PBKDF2_ITERATIONS) {
2625
+ const parsed = parseInt(process.env.PBKDF2_ITERATIONS, 10);
2626
+ if (!isNaN(parsed) && parsed > 0) {
2627
+ return parsed;
2628
+ }
2629
+ }
2630
+ return ENC_ITER_DEFAULT;
2631
+ }
2632
+ async function encryptWithPassphrase(plaintext, pass) {
2633
+ const salt = randomBytes(SALT_LEN);
2634
+ const iv = randomBytes(IV_LEN);
2635
+ const key = await deriveKey(pass, salt);
2636
+ const cipher = gcm(key, iv);
2637
+ const ct = cipher.encrypt(new TextEncoder().encode(plaintext));
2638
+ return { salt: b64(salt), iv: b64(iv), data: b64(ct) };
2639
+ }
2640
+ async function decryptWithPassphrase(blob, pass) {
2641
+ const salt = ub64(blob.salt);
2642
+ const iv = ub64(blob.iv);
2643
+ const data = ub64(blob.data);
2644
+ const key = await deriveKey(pass, salt);
2645
+ const cipher = gcm(key, iv);
2646
+ const pt = cipher.decrypt(data);
2647
+ return new TextDecoder().decode(pt);
2648
+ }
2649
+ const b64 = (buf) => {
2650
+ return Buffer.from(buf).toString('base64');
2651
+ };
2652
+ const ub64 = (b64) => {
2653
+ return new Uint8Array(Buffer.from(b64, 'base64'));
2654
+ };
2655
+ async function deriveKey(pass, salt) {
2656
+ const enc = new TextEncoder();
2657
+ return pbkdf2Async(sha512, enc.encode(pass), salt, { c: getIterations(), dkLen: 32 });
2658
+ }
2659
+
1085
2660
  function hexToBase64url(hex) {
1086
2661
  const bytes = Buffer.from(hex, 'hex');
1087
2662
  return base64url.baseEncode(bytes);
@@ -1119,6 +2694,11 @@ exports.NoticeTags = void 0;
1119
2694
  NoticeTags["POLL"] = "poll";
1120
2695
  NoticeTags["CREDENTIAL"] = "credential";
1121
2696
  })(exports.NoticeTags || (exports.NoticeTags = {}));
2697
+ exports.PollItems = void 0;
2698
+ (function (PollItems) {
2699
+ PollItems["POLL"] = "poll";
2700
+ PollItems["RESULTS"] = "results";
2701
+ })(exports.PollItems || (exports.PollItems = {}));
1122
2702
  class Keymaster {
1123
2703
  passphrase;
1124
2704
  gatekeeper;
@@ -1206,7 +2786,7 @@ class Keymaster {
1206
2786
  catch (error) {
1207
2787
  throw new InvalidParameterError('mnemonic');
1208
2788
  }
1209
- const mnemonicEnc = await encryption.encMnemonic(mnemonic, this.passphrase);
2789
+ const mnemonicEnc = await encryptWithPassphrase(mnemonic, this.passphrase);
1210
2790
  const wallet = {
1211
2791
  version: 2,
1212
2792
  seed: { mnemonicEnc },
@@ -1224,7 +2804,7 @@ class Keymaster {
1224
2804
  return this.getMnemonicForDerivation(wallet);
1225
2805
  }
1226
2806
  async getMnemonicForDerivation(wallet) {
1227
- return encryption.decMnemonic(wallet.seed.mnemonicEnc, this.passphrase);
2807
+ return decryptWithPassphrase(wallet.seed.mnemonicEnc, this.passphrase);
1228
2808
  }
1229
2809
  async checkWallet() {
1230
2810
  const wallet = await this.loadWallet();
@@ -1436,7 +3016,7 @@ class Keymaster {
1436
3016
  const keypair = await this.hdKeyPair();
1437
3017
  const seedBank = await this.resolveSeedBank();
1438
3018
  const msg = JSON.stringify(wallet);
1439
- const backup = this.cipher.encryptMessage(keypair.publicJwk, keypair.privateJwk, msg);
3019
+ const backup = this.cipher.encryptMessage(keypair.publicJwk, msg);
1440
3020
  const operation = {
1441
3021
  type: "create",
1442
3022
  created: new Date().toISOString(),
@@ -1489,12 +3069,12 @@ class Keymaster {
1489
3069
  if (typeof castData.backup !== 'string') {
1490
3070
  throw new InvalidParameterError('Asset "backup" is missing or not a string');
1491
3071
  }
1492
- const backup = this.cipher.decryptMessage(keypair.publicJwk, keypair.privateJwk, castData.backup);
3072
+ const backup = this.cipher.decryptMessage(keypair.privateJwk, castData.backup, keypair.publicJwk);
1493
3073
  let wallet = JSON.parse(backup);
1494
3074
  if (db_typeGuards.isWalletFile(wallet)) {
1495
3075
  const mnemonic = await this.decryptMnemonic();
1496
3076
  // Backup might have a different mnemonic passphase so re-encrypt
1497
- wallet.seed.mnemonicEnc = await encryption.encMnemonic(mnemonic, this.passphrase);
3077
+ wallet.seed.mnemonicEnc = await encryptWithPassphrase(mnemonic, this.passphrase);
1498
3078
  }
1499
3079
  await this.mutateWallet(async (current) => {
1500
3080
  // Clear all existing properties from the current wallet
@@ -1658,7 +3238,7 @@ class Keymaster {
1658
3238
  const cloneData = { ...assetData, cloned: assetDoc.didDocument.id };
1659
3239
  return this.createAsset(cloneData, options);
1660
3240
  }
1661
- async generateImageAsset(buffer) {
3241
+ async generateImageAsset(filename, buffer) {
1662
3242
  let metadata;
1663
3243
  try {
1664
3244
  metadata = imageSize.imageSize(buffer);
@@ -1667,33 +3247,38 @@ class Keymaster {
1667
3247
  throw new InvalidParameterError('buffer');
1668
3248
  }
1669
3249
  const cid = await this.gatekeeper.addData(buffer);
1670
- const image = {
3250
+ const file = {
1671
3251
  cid,
3252
+ filename,
3253
+ type: `image/${metadata.type}`,
1672
3254
  bytes: buffer.length,
1673
- ...metadata,
1674
- type: `image/${metadata.type}`
1675
3255
  };
1676
- return image;
3256
+ const image = {
3257
+ width: metadata.width,
3258
+ height: metadata.height,
3259
+ };
3260
+ return { file, image };
1677
3261
  }
1678
3262
  async createImage(buffer, options = {}) {
1679
- const image = await this.generateImageAsset(buffer);
1680
- return this.createAsset({ image }, options);
3263
+ const filename = options.filename || 'image';
3264
+ const { file, image } = await this.generateImageAsset(filename, buffer);
3265
+ return this.createAsset({ file, image }, options);
1681
3266
  }
1682
- async updateImage(id, buffer) {
1683
- const image = await this.generateImageAsset(buffer);
1684
- return this.mergeData(id, { image });
3267
+ async updateImage(id, buffer, options = {}) {
3268
+ const filename = options.filename || 'image';
3269
+ const { file, image } = await this.generateImageAsset(filename, buffer);
3270
+ return this.mergeData(id, { file, image });
1685
3271
  }
1686
3272
  async getImage(id) {
1687
3273
  const asset = await this.resolveAsset(id);
1688
- const image = asset.image;
1689
- if (!image || !image.cid) {
3274
+ if (!asset.file || !asset.file.cid || !asset.image) {
1690
3275
  return null;
1691
3276
  }
1692
- const buffer = await this.gatekeeper.getData(image.cid);
3277
+ const buffer = await this.gatekeeper.getData(asset.file.cid);
1693
3278
  if (buffer) {
1694
- image.data = buffer;
3279
+ asset.file.data = buffer;
1695
3280
  }
1696
- return image;
3281
+ return { file: asset.file, image: asset.image };
1697
3282
  }
1698
3283
  async testImage(id) {
1699
3284
  try {
@@ -1736,24 +3321,32 @@ class Keymaster {
1736
3321
  };
1737
3322
  return file;
1738
3323
  }
1739
- async createDocument(buffer, options = {}) {
1740
- const filename = options.filename || 'document';
1741
- const document = await this.generateFileAsset(filename, buffer);
1742
- return this.createAsset({ document }, options);
3324
+ async createFile(buffer, options = {}) {
3325
+ const filename = options.filename || 'file';
3326
+ const file = await this.generateFileAsset(filename, buffer);
3327
+ return this.createAsset({ file }, options);
1743
3328
  }
1744
- async updateDocument(id, buffer, options = {}) {
1745
- const filename = options.filename || 'document';
1746
- const document = await this.generateFileAsset(filename, buffer);
1747
- return this.mergeData(id, { document });
3329
+ async updateFile(id, buffer, options = {}) {
3330
+ const filename = options.filename || 'file';
3331
+ const file = await this.generateFileAsset(filename, buffer);
3332
+ return this.mergeData(id, { file });
1748
3333
  }
1749
- async getDocument(id) {
3334
+ async getFile(id) {
1750
3335
  const asset = await this.resolveAsset(id);
1751
- return asset.document ?? null;
3336
+ const file = asset.file;
3337
+ if (!file || !file.cid) {
3338
+ return null;
3339
+ }
3340
+ const buffer = await this.gatekeeper.getData(file.cid);
3341
+ if (buffer) {
3342
+ file.data = buffer;
3343
+ }
3344
+ return file;
1752
3345
  }
1753
- async testDocument(id) {
3346
+ async testFile(id) {
1754
3347
  try {
1755
- const document = await this.getDocument(id);
1756
- return document !== null;
3348
+ const file = await this.getFile(id);
3349
+ return file !== null;
1757
3350
  }
1758
3351
  catch (error) {
1759
3352
  return false;
@@ -1761,19 +3354,16 @@ class Keymaster {
1761
3354
  }
1762
3355
  async encryptMessage(msg, receiver, options = {}) {
1763
3356
  const { encryptForSender = true, includeHash = false, } = options;
1764
- const id = await this.fetchIdInfo();
1765
3357
  const senderKeypair = await this.fetchKeyPair();
1766
3358
  if (!senderKeypair) {
1767
3359
  throw new KeymasterError('No valid sender keypair');
1768
3360
  }
1769
3361
  const doc = await this.resolveDID(receiver, { confirm: true });
1770
3362
  const receivePublicJwk = this.getPublicKeyJwk(doc);
1771
- const cipher_sender = encryptForSender ? this.cipher.encryptMessage(senderKeypair.publicJwk, senderKeypair.privateJwk, msg) : null;
1772
- const cipher_receiver = this.cipher.encryptMessage(receivePublicJwk, senderKeypair.privateJwk, msg);
3363
+ const cipher_sender = encryptForSender ? this.cipher.encryptMessage(senderKeypair.publicJwk, msg) : null;
3364
+ const cipher_receiver = this.cipher.encryptMessage(receivePublicJwk, msg);
1773
3365
  const cipher_hash = includeHash ? this.cipher.hashMessage(msg) : null;
1774
3366
  const encrypted = {
1775
- sender: id.did,
1776
- created: new Date().toISOString(),
1777
3367
  cipher_hash,
1778
3368
  cipher_sender,
1779
3369
  cipher_receiver,
@@ -1789,7 +3379,7 @@ class Keymaster {
1789
3379
  const didkey = hdkey.derive(path);
1790
3380
  const receiverKeypair = this.cipher.generateJwk(didkey.privateKey);
1791
3381
  try {
1792
- return this.cipher.decryptMessage(senderPublicJwk, receiverKeypair.privateJwk, ciphertext);
3382
+ return this.cipher.decryptMessage(receiverKeypair.privateJwk, ciphertext, senderPublicJwk);
1793
3383
  }
1794
3384
  catch (error) {
1795
3385
  index -= 1;
@@ -1800,7 +3390,8 @@ class Keymaster {
1800
3390
  async decryptMessage(did) {
1801
3391
  const wallet = await this.loadWallet();
1802
3392
  const id = await this.fetchIdInfo();
1803
- const asset = await this.resolveAsset(did);
3393
+ const msgDoc = await this.resolveDID(did);
3394
+ const asset = msgDoc.didDocumentData;
1804
3395
  if (!asset) {
1805
3396
  throw new InvalidParameterError('did not encrypted');
1806
3397
  }
@@ -1809,9 +3400,16 @@ class Keymaster {
1809
3400
  throw new InvalidParameterError('did not encrypted');
1810
3401
  }
1811
3402
  const crypt = (castAsset.encrypted ? castAsset.encrypted : castAsset);
1812
- const doc = await this.resolveDID(crypt.sender, { confirm: true, versionTime: crypt.created });
1813
- const senderPublicJwk = this.getPublicKeyJwk(doc);
1814
- const ciphertext = (crypt.sender === id.did && crypt.cipher_sender) ? crypt.cipher_sender : crypt.cipher_receiver;
3403
+ // Derive sender and created from the message DID document,
3404
+ // falling back to fields in the asset for legacy messages
3405
+ const sender = crypt.sender || msgDoc.didDocument?.controller;
3406
+ const created = crypt.created || msgDoc.didDocumentMetadata?.created;
3407
+ if (!sender) {
3408
+ throw new InvalidParameterError('Sender DID could not be determined from message or DID document');
3409
+ }
3410
+ const senderDoc = await this.resolveDID(sender, { confirm: true, versionTime: created });
3411
+ const senderPublicJwk = this.getPublicKeyJwk(senderDoc);
3412
+ const ciphertext = (sender === id.did && crypt.cipher_sender) ? crypt.cipher_sender : crypt.cipher_receiver;
1815
3413
  return await this.decryptWithDerivedKeys(wallet, id, senderPublicJwk, ciphertext);
1816
3414
  }
1817
3415
  async encryptJSON(json, did, options = {}) {
@@ -2192,7 +3790,7 @@ class Keymaster {
2192
3790
  id: idInfo,
2193
3791
  };
2194
3792
  const msg = JSON.stringify(data);
2195
- const backup = this.cipher.encryptMessage(keypair.publicJwk, keypair.privateJwk, msg);
3793
+ const backup = this.cipher.encryptMessage(keypair.publicJwk, msg);
2196
3794
  const doc = await this.resolveDID(idInfo.did);
2197
3795
  const registry = doc.didDocumentRegistration?.registry;
2198
3796
  if (!registry) {
@@ -2218,7 +3816,7 @@ class Keymaster {
2218
3816
  if (typeof backupStore.backup !== 'string') {
2219
3817
  throw new InvalidDIDError('backup not found in backupStore');
2220
3818
  }
2221
- const backup = this.cipher.decryptMessage(keypair.publicJwk, keypair.privateJwk, backupStore.backup);
3819
+ const backup = this.cipher.decryptMessage(keypair.privateJwk, backupStore.backup, keypair.publicJwk);
2222
3820
  const data = JSON.parse(backup);
2223
3821
  await this.mutateWallet((wallet) => {
2224
3822
  if (wallet.ids[data.name]) {
@@ -2319,7 +3917,13 @@ class Keymaster {
2319
3917
  validFrom = new Date().toISOString();
2320
3918
  }
2321
3919
  const id = await this.fetchIdInfo();
2322
- const subjectDID = await this.lookupDID(subjectId);
3920
+ let subjectURI;
3921
+ try {
3922
+ subjectURI = await this.lookupDID(subjectId);
3923
+ }
3924
+ catch {
3925
+ subjectURI = subjectId;
3926
+ }
2323
3927
  const vc = {
2324
3928
  "@context": [
2325
3929
  "https://www.w3.org/ns/credentials/v2",
@@ -2330,7 +3934,7 @@ class Keymaster {
2330
3934
  validFrom,
2331
3935
  validUntil,
2332
3936
  credentialSubject: {
2333
- id: subjectDID,
3937
+ id: subjectURI,
2334
3938
  },
2335
3939
  };
2336
3940
  // If schema provided, add credentialSchema and generate claims from schema
@@ -2355,7 +3959,7 @@ class Keymaster {
2355
3959
  }
2356
3960
  if (claims && Object.keys(claims).length) {
2357
3961
  vc.credentialSubject = {
2358
- id: subjectDID,
3962
+ id: subjectURI,
2359
3963
  ...claims,
2360
3964
  };
2361
3965
  }
@@ -2370,21 +3974,32 @@ class Keymaster {
2370
3974
  throw new InvalidParameterError('credential.issuer');
2371
3975
  }
2372
3976
  const signed = await this.addProof(credential);
2373
- return this.encryptJSON(signed, credential.credentialSubject.id, { ...options, includeHash: true });
3977
+ const subjectId = credential.credentialSubject.id;
3978
+ if (this.isManagedDID(subjectId)) {
3979
+ return this.encryptJSON(signed, subjectId, { ...options, includeHash: true });
3980
+ }
3981
+ return this.encryptJSON(signed, id.did, { ...options, includeHash: true, encryptForSender: false });
2374
3982
  }
2375
3983
  async sendCredential(did, options = {}) {
2376
3984
  const vc = await this.getCredential(did);
2377
3985
  if (!vc) {
2378
3986
  return null;
2379
3987
  }
3988
+ const subjectId = vc.credentialSubject.id;
3989
+ if (!this.isManagedDID(subjectId)) {
3990
+ return null;
3991
+ }
2380
3992
  const registry = this.ephemeralRegistry;
2381
3993
  const validUntil = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(); // Default to 7 days
2382
3994
  const message = {
2383
- to: [vc.credentialSubject.id],
3995
+ to: [subjectId],
2384
3996
  dids: [did],
2385
3997
  };
2386
3998
  return this.createNotice(message, { registry, validUntil, ...options });
2387
3999
  }
4000
+ isManagedDID(value) {
4001
+ return value.startsWith('did:cid:');
4002
+ }
2388
4003
  isVerifiableCredential(obj) {
2389
4004
  if (typeof obj !== 'object' || !obj) {
2390
4005
  return false;
@@ -2406,24 +4021,29 @@ class Keymaster {
2406
4021
  delete credential.proof;
2407
4022
  const signed = await this.addProof(credential);
2408
4023
  const msg = JSON.stringify(signed);
2409
- const id = await this.fetchIdInfo();
2410
4024
  const senderKeypair = await this.fetchKeyPair();
2411
4025
  if (!senderKeypair) {
2412
4026
  throw new KeymasterError('No valid sender keypair');
2413
4027
  }
2414
4028
  const holder = credential.credentialSubject.id;
2415
- const holderDoc = await this.resolveDID(holder, { confirm: true });
2416
- const receivePublicJwk = this.getPublicKeyJwk(holderDoc);
2417
- const cipher_sender = this.cipher.encryptMessage(senderKeypair.publicJwk, senderKeypair.privateJwk, msg);
2418
- const cipher_receiver = this.cipher.encryptMessage(receivePublicJwk, senderKeypair.privateJwk, msg);
2419
4029
  const msgHash = this.cipher.hashMessage(msg);
2420
- const encrypted = {
2421
- sender: id.did,
2422
- created: new Date().toISOString(),
2423
- cipher_hash: msgHash,
2424
- cipher_sender: cipher_sender,
2425
- cipher_receiver: cipher_receiver,
2426
- };
4030
+ let encrypted;
4031
+ if (this.isManagedDID(holder)) {
4032
+ const holderDoc = await this.resolveDID(holder, { confirm: true });
4033
+ const receivePublicJwk = this.getPublicKeyJwk(holderDoc);
4034
+ encrypted = {
4035
+ cipher_hash: msgHash,
4036
+ cipher_sender: this.cipher.encryptMessage(senderKeypair.publicJwk, msg),
4037
+ cipher_receiver: this.cipher.encryptMessage(receivePublicJwk, msg),
4038
+ };
4039
+ }
4040
+ else {
4041
+ encrypted = {
4042
+ cipher_hash: msgHash,
4043
+ cipher_sender: null,
4044
+ cipher_receiver: this.cipher.encryptMessage(senderKeypair.publicJwk, msg),
4045
+ };
4046
+ }
2427
4047
  return this.updateDID(did, { didDocumentData: { encrypted } });
2428
4048
  }
2429
4049
  async revokeCredential(credential) {
@@ -2919,72 +4539,64 @@ class Keymaster {
2919
4539
  const nextWeek = new Date();
2920
4540
  nextWeek.setDate(now.getDate() + 7);
2921
4541
  return {
2922
- type: 'poll',
2923
- version: 1,
4542
+ version: 2,
4543
+ name: 'poll-name',
2924
4544
  description: 'What is this poll about?',
2925
- roster: 'DID of the eligible voter group',
2926
4545
  options: ['yes', 'no', 'abstain'],
2927
4546
  deadline: nextWeek.toISOString(),
2928
4547
  };
2929
4548
  }
2930
- async createPoll(poll, options = {}) {
2931
- if (poll.type !== 'poll') {
2932
- throw new InvalidParameterError('poll');
2933
- }
2934
- if (poll.version !== 1) {
4549
+ async createPoll(config, options = {}) {
4550
+ if (config.version !== 2) {
2935
4551
  throw new InvalidParameterError('poll.version');
2936
4552
  }
2937
- if (!poll.description) {
4553
+ if (!config.name) {
4554
+ throw new InvalidParameterError('poll.name');
4555
+ }
4556
+ if (!config.description) {
2938
4557
  throw new InvalidParameterError('poll.description');
2939
4558
  }
2940
- if (!poll.options || !Array.isArray(poll.options) || poll.options.length < 2 || poll.options.length > 10) {
4559
+ if (!config.options || !Array.isArray(config.options) || config.options.length < 2 || config.options.length > 10) {
2941
4560
  throw new InvalidParameterError('poll.options');
2942
4561
  }
2943
- if (!poll.roster) {
2944
- // eslint-disable-next-line
2945
- throw new InvalidParameterError('poll.roster');
2946
- }
2947
- try {
2948
- const isValidGroup = await this.testGroup(poll.roster);
2949
- if (!isValidGroup) {
2950
- throw new InvalidParameterError('poll.roster');
2951
- }
2952
- }
2953
- catch {
2954
- throw new InvalidParameterError('poll.roster');
2955
- }
2956
- if (!poll.deadline) {
2957
- // eslint-disable-next-line
4562
+ if (!config.deadline) {
2958
4563
  throw new InvalidParameterError('poll.deadline');
2959
4564
  }
2960
- const deadline = new Date(poll.deadline);
4565
+ const deadline = new Date(config.deadline);
2961
4566
  if (isNaN(deadline.getTime())) {
2962
4567
  throw new InvalidParameterError('poll.deadline');
2963
4568
  }
2964
4569
  if (deadline < new Date()) {
2965
4570
  throw new InvalidParameterError('poll.deadline');
2966
4571
  }
2967
- return this.createAsset({ poll }, options);
4572
+ const vaultDid = await this.createVault(options);
4573
+ const buffer = Buffer.from(JSON.stringify(config), 'utf-8');
4574
+ await this.addVaultItem(vaultDid, exports.PollItems.POLL, buffer);
4575
+ return vaultDid;
2968
4576
  }
2969
4577
  async getPoll(id) {
2970
- const asset = await this.resolveAsset(id);
2971
- // TEMP during did:cid, return old version poll
2972
- const castOldAsset = asset;
2973
- if (castOldAsset.options) {
2974
- return castOldAsset;
4578
+ const isVault = await this.testVault(id);
4579
+ if (!isVault) {
4580
+ return null;
2975
4581
  }
2976
- const castAsset = asset;
2977
- if (!castAsset.poll) {
4582
+ try {
4583
+ const buffer = await this.getVaultItem(id, exports.PollItems.POLL);
4584
+ if (!buffer) {
4585
+ return null;
4586
+ }
4587
+ const config = JSON.parse(buffer.toString('utf-8'));
4588
+ return config;
4589
+ }
4590
+ catch {
2978
4591
  return null;
2979
4592
  }
2980
- return castAsset.poll;
2981
4593
  }
2982
4594
  async testPoll(id) {
2983
4595
  try {
2984
- const poll = await this.getPoll(id);
2985
- return poll !== null;
4596
+ const config = await this.getPoll(id);
4597
+ return config !== null;
2986
4598
  }
2987
- catch (error) {
4599
+ catch {
2988
4600
  return false;
2989
4601
  }
2990
4602
  }
@@ -2999,113 +4611,243 @@ class Keymaster {
2999
4611
  }
3000
4612
  return polls;
3001
4613
  }
4614
+ async addPollVoter(pollId, memberId) {
4615
+ const config = await this.getPoll(pollId);
4616
+ if (!config) {
4617
+ throw new InvalidParameterError('pollId');
4618
+ }
4619
+ return this.addVaultMember(pollId, memberId);
4620
+ }
4621
+ async removePollVoter(pollId, memberId) {
4622
+ const config = await this.getPoll(pollId);
4623
+ if (!config) {
4624
+ throw new InvalidParameterError('pollId');
4625
+ }
4626
+ return this.removeVaultMember(pollId, memberId);
4627
+ }
4628
+ async listPollVoters(pollId) {
4629
+ const config = await this.getPoll(pollId);
4630
+ if (!config) {
4631
+ throw new InvalidParameterError('pollId');
4632
+ }
4633
+ return this.listVaultMembers(pollId);
4634
+ }
3002
4635
  async viewPoll(pollId) {
3003
4636
  const id = await this.fetchIdInfo();
3004
- const poll = await this.getPoll(pollId);
3005
- if (!poll) {
4637
+ const config = await this.getPoll(pollId);
4638
+ if (!config) {
3006
4639
  throw new InvalidParameterError('pollId');
3007
4640
  }
4641
+ const doc = await this.resolveDID(pollId);
4642
+ const isOwner = (doc.didDocument?.controller === id.did);
4643
+ const voteExpired = Date.now() > new Date(config.deadline).getTime();
4644
+ let isEligible = false;
3008
4645
  let hasVoted = false;
3009
- if (poll.ballots) {
3010
- hasVoted = !!poll.ballots[id.did];
4646
+ const ballots = [];
4647
+ try {
4648
+ const vault = await this.getVault(pollId);
4649
+ const members = await this.listVaultMembers(pollId);
4650
+ isEligible = isOwner || !!members[id.did];
4651
+ const items = await this.listVaultItems(pollId);
4652
+ for (const itemName of Object.keys(items)) {
4653
+ if (itemName !== exports.PollItems.POLL && itemName !== exports.PollItems.RESULTS) {
4654
+ ballots.push(itemName);
4655
+ }
4656
+ }
4657
+ const myBallotKey = this.generateBallotKey(vault, id.did);
4658
+ hasVoted = ballots.includes(myBallotKey);
4659
+ }
4660
+ catch {
4661
+ isEligible = false;
3011
4662
  }
3012
- const voteExpired = Date.now() > new Date(poll.deadline).getTime();
3013
- const isEligible = await this.testGroup(poll.roster, id.did);
3014
- const doc = await this.resolveDID(pollId);
3015
4663
  const view = {
3016
- description: poll.description,
3017
- options: poll.options,
3018
- deadline: poll.deadline,
3019
- isOwner: (doc.didDocument?.controller === id.did),
3020
- isEligible: isEligible,
3021
- voteExpired: voteExpired,
3022
- hasVoted: hasVoted,
4664
+ description: config.description,
4665
+ options: config.options,
4666
+ deadline: config.deadline,
4667
+ isOwner,
4668
+ isEligible,
4669
+ voteExpired,
4670
+ hasVoted,
4671
+ ballots,
3023
4672
  };
3024
- if (id.did === doc.didDocument?.controller) {
3025
- let voted = 0;
3026
- const results = {
3027
- tally: [],
3028
- ballots: [],
3029
- };
3030
- results.tally.push({
3031
- vote: 0,
3032
- option: 'spoil',
3033
- count: 0,
3034
- });
3035
- for (let i = 0; i < poll.options.length; i++) {
3036
- results.tally.push({
3037
- vote: i + 1,
3038
- option: poll.options[i],
3039
- count: 0,
4673
+ if (isOwner) {
4674
+ view.results = await this.computePollResults(pollId, config);
4675
+ }
4676
+ else {
4677
+ try {
4678
+ const resultsBuffer = await this.getVaultItem(pollId, exports.PollItems.RESULTS);
4679
+ if (resultsBuffer) {
4680
+ view.results = JSON.parse(resultsBuffer.toString('utf-8'));
4681
+ }
4682
+ }
4683
+ catch { }
4684
+ }
4685
+ return view;
4686
+ }
4687
+ async computePollResults(pollId, config) {
4688
+ const vault = await this.getVault(pollId);
4689
+ const members = await this.listVaultMembers(pollId);
4690
+ const items = await this.listVaultItems(pollId);
4691
+ const results = {
4692
+ tally: [],
4693
+ ballots: [],
4694
+ };
4695
+ results.tally.push({ vote: 0, option: 'spoil', count: 0 });
4696
+ for (let i = 0; i < config.options.length; i++) {
4697
+ results.tally.push({ vote: i + 1, option: config.options[i], count: 0 });
4698
+ }
4699
+ // Build ballotKey → memberDID mapping
4700
+ const keyToMember = {};
4701
+ for (const memberDID of Object.keys(members)) {
4702
+ const ballotKey = this.generateBallotKey(vault, memberDID);
4703
+ keyToMember[ballotKey] = memberDID;
4704
+ }
4705
+ // Include owner in mapping
4706
+ const id = await this.fetchIdInfo();
4707
+ const ownerKey = this.generateBallotKey(vault, id.did);
4708
+ keyToMember[ownerKey] = id.did;
4709
+ let voted = 0;
4710
+ for (const [itemName, itemMeta] of Object.entries(items)) {
4711
+ if (itemName === exports.PollItems.POLL || itemName === exports.PollItems.RESULTS) {
4712
+ continue;
4713
+ }
4714
+ const ballotBuffer = await this.getVaultItem(pollId, itemName);
4715
+ if (!ballotBuffer) {
4716
+ continue;
4717
+ }
4718
+ const ballotDid = ballotBuffer.toString('utf-8');
4719
+ const decrypted = await this.decryptJSON(ballotDid);
4720
+ const vote = decrypted.vote;
4721
+ const voterDID = keyToMember[itemName] || itemName;
4722
+ if (results.ballots) {
4723
+ results.ballots.push({
4724
+ voter: voterDID,
4725
+ vote,
4726
+ option: vote === 0 ? 'spoil' : config.options[vote - 1],
4727
+ received: itemMeta.added || '',
3040
4728
  });
3041
4729
  }
3042
- for (let voter in poll.ballots) {
3043
- const ballot = poll.ballots[voter];
3044
- const decrypted = await this.decryptJSON(ballot.ballot);
3045
- const vote = decrypted.vote;
3046
- if (results.ballots) {
3047
- results.ballots.push({
3048
- ...ballot,
3049
- voter,
3050
- vote,
3051
- option: poll.options[vote - 1],
3052
- });
3053
- }
3054
- voted += 1;
4730
+ voted += 1;
4731
+ if (vote >= 0 && vote < results.tally.length) {
3055
4732
  results.tally[vote].count += 1;
3056
4733
  }
3057
- const roster = await this.getGroup(poll.roster);
3058
- const total = roster.members.length;
3059
- results.votes = {
3060
- eligible: total,
3061
- received: voted,
3062
- pending: total - voted,
3063
- };
3064
- results.final = voteExpired || (voted === total);
3065
- view.results = results;
3066
4734
  }
3067
- return view;
4735
+ const total = Object.keys(members).length + 1; // +1 for owner
4736
+ const voteExpired = Date.now() > new Date(config.deadline).getTime();
4737
+ results.votes = {
4738
+ eligible: total,
4739
+ received: voted,
4740
+ pending: total - voted,
4741
+ };
4742
+ results.final = voteExpired || (voted === total);
4743
+ return results;
3068
4744
  }
3069
4745
  async votePoll(pollId, vote, options = {}) {
3070
- const { spoil = false } = options;
3071
4746
  const id = await this.fetchIdInfo();
3072
4747
  const didPoll = await this.lookupDID(pollId);
3073
4748
  const doc = await this.resolveDID(didPoll);
3074
- const poll = await this.getPoll(pollId);
3075
- if (!poll) {
4749
+ const config = await this.getPoll(pollId);
4750
+ if (!config) {
3076
4751
  throw new InvalidParameterError('pollId');
3077
4752
  }
3078
- const eligible = await this.testGroup(poll.roster, id.did);
3079
- const expired = Date.now() > new Date(poll.deadline).getTime();
3080
4753
  const owner = doc.didDocument?.controller;
3081
4754
  if (!owner) {
3082
- throw new KeymasterError('owner mising from poll');
4755
+ throw new KeymasterError('owner missing from poll');
3083
4756
  }
3084
- if (!eligible) {
3085
- throw new InvalidParameterError('voter not in roster');
4757
+ // Check vault membership
4758
+ let isEligible = false;
4759
+ if (id.did === owner) {
4760
+ isEligible = true;
4761
+ }
4762
+ else {
4763
+ try {
4764
+ const vault = await this.getVault(didPoll);
4765
+ await this.decryptVault(vault);
4766
+ isEligible = true;
4767
+ }
4768
+ catch {
4769
+ isEligible = false;
4770
+ }
3086
4771
  }
4772
+ if (!isEligible) {
4773
+ throw new InvalidParameterError('voter is not a poll member');
4774
+ }
4775
+ const expired = Date.now() > new Date(config.deadline).getTime();
3087
4776
  if (expired) {
3088
4777
  throw new InvalidParameterError('poll has expired');
3089
4778
  }
3090
- let ballot;
3091
- if (spoil) {
3092
- ballot = {
3093
- poll: didPoll,
3094
- vote: 0,
3095
- };
4779
+ const max = config.options.length;
4780
+ if (!Number.isInteger(vote) || vote < 0 || vote > max) {
4781
+ throw new InvalidParameterError('vote');
3096
4782
  }
3097
- else {
3098
- const max = poll.options.length;
3099
- if (!Number.isInteger(vote) || vote < 1 || vote > max) {
3100
- throw new InvalidParameterError('vote');
4783
+ const ballot = {
4784
+ poll: didPoll,
4785
+ vote: vote,
4786
+ };
4787
+ // Encrypt for owner and sender (voter can view their own ballot)
4788
+ return await this.encryptJSON(ballot, owner, options);
4789
+ }
4790
+ async sendPoll(pollId) {
4791
+ const didPoll = await this.lookupDID(pollId);
4792
+ const config = await this.getPoll(didPoll);
4793
+ if (!config) {
4794
+ throw new InvalidParameterError('pollId');
4795
+ }
4796
+ const members = await this.listVaultMembers(didPoll);
4797
+ const voters = Object.keys(members);
4798
+ if (voters.length === 0) {
4799
+ throw new KeymasterError('No poll voters found');
4800
+ }
4801
+ const registry = this.ephemeralRegistry;
4802
+ const validUntil = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
4803
+ const message = {
4804
+ to: voters,
4805
+ dids: [didPoll],
4806
+ };
4807
+ return this.createNotice(message, { registry, validUntil });
4808
+ }
4809
+ async sendBallot(ballotDid, pollId) {
4810
+ const didPoll = await this.lookupDID(pollId);
4811
+ const config = await this.getPoll(didPoll);
4812
+ if (!config) {
4813
+ throw new InvalidParameterError('pollId is not a valid poll');
4814
+ }
4815
+ const pollDoc = await this.resolveDID(didPoll);
4816
+ const ownerDid = pollDoc.didDocument?.controller;
4817
+ if (!ownerDid) {
4818
+ throw new KeymasterError('poll owner not found');
4819
+ }
4820
+ const registry = this.ephemeralRegistry;
4821
+ const validUntil = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
4822
+ const message = {
4823
+ to: [ownerDid],
4824
+ dids: [ballotDid],
4825
+ };
4826
+ return this.createNotice(message, { registry, validUntil });
4827
+ }
4828
+ async viewBallot(ballotDid) {
4829
+ const docBallot = await this.resolveDID(ballotDid);
4830
+ const voter = docBallot.didDocument?.controller;
4831
+ const result = {
4832
+ poll: '',
4833
+ voter: voter || undefined,
4834
+ };
4835
+ try {
4836
+ const data = await this.decryptJSON(ballotDid);
4837
+ result.poll = data.poll;
4838
+ result.vote = data.vote;
4839
+ const config = await this.getPoll(data.poll);
4840
+ if (config && data.vote > 0 && data.vote <= config.options.length) {
4841
+ result.option = config.options[data.vote - 1];
3101
4842
  }
3102
- ballot = {
3103
- poll: didPoll,
3104
- vote: vote,
3105
- };
4843
+ else if (data.vote === 0) {
4844
+ result.option = 'spoil';
4845
+ }
4846
+ }
4847
+ catch {
4848
+ // Caller cannot decrypt (not the owner) — return limited info
3106
4849
  }
3107
- // Encrypt for receiver only
3108
- return await this.encryptJSON(ballot, owner, { ...options, encryptForSender: false });
4850
+ return result;
3109
4851
  }
3110
4852
  async updatePoll(ballot) {
3111
4853
  const id = await this.fetchIdInfo();
@@ -3115,7 +4857,7 @@ class Keymaster {
3115
4857
  let dataBallot;
3116
4858
  try {
3117
4859
  dataBallot = await this.decryptJSON(didBallot);
3118
- if (!dataBallot.poll || !dataBallot.vote) {
4860
+ if (!dataBallot.poll || dataBallot.vote === undefined) {
3119
4861
  throw new InvalidParameterError('ballot');
3120
4862
  }
3121
4863
  }
@@ -3125,34 +4867,34 @@ class Keymaster {
3125
4867
  const didPoll = dataBallot.poll;
3126
4868
  const docPoll = await this.resolveDID(didPoll);
3127
4869
  const didOwner = docPoll.didDocument.controller;
3128
- const poll = await this.getPoll(didPoll);
3129
- if (!poll) {
4870
+ const config = await this.getPoll(didPoll);
4871
+ if (!config) {
3130
4872
  throw new KeymasterError('Cannot find poll related to ballot');
3131
4873
  }
3132
4874
  if (id.did !== didOwner) {
3133
4875
  throw new InvalidParameterError('only owner can update a poll');
3134
4876
  }
3135
- const eligible = await this.testGroup(poll.roster, didVoter);
3136
- if (!eligible) {
3137
- throw new InvalidParameterError('voter not in roster');
4877
+ // Check voter is a vault member
4878
+ const vault = await this.getVault(didPoll);
4879
+ const voterBallotKey = this.generateBallotKey(vault, didVoter);
4880
+ const members = await this.listVaultMembers(didPoll);
4881
+ const isMember = !!members[didVoter] || didVoter === id.did;
4882
+ if (!isMember) {
4883
+ throw new InvalidParameterError('voter is not a poll member');
3138
4884
  }
3139
- const expired = Date.now() > new Date(poll.deadline).getTime();
4885
+ const expired = Date.now() > new Date(config.deadline).getTime();
3140
4886
  if (expired) {
3141
4887
  throw new InvalidParameterError('poll has expired');
3142
4888
  }
3143
- const max = poll.options.length;
4889
+ const max = config.options.length;
3144
4890
  const vote = dataBallot.vote;
3145
- if (!vote || vote < 0 || vote > max) {
4891
+ if (vote < 0 || vote > max) {
3146
4892
  throw new InvalidParameterError('ballot.vote');
3147
4893
  }
3148
- if (!poll.ballots) {
3149
- poll.ballots = {};
3150
- }
3151
- poll.ballots[didVoter] = {
3152
- ballot: didBallot,
3153
- received: new Date().toISOString(),
3154
- };
3155
- return this.mergeData(didPoll, { poll });
4894
+ // Store ballot DID as vault item keyed by voter's ballot key
4895
+ const buffer = Buffer.from(didBallot, 'utf-8');
4896
+ await this.addVaultItem(didPoll, voterBallotKey, buffer);
4897
+ return true;
3156
4898
  }
3157
4899
  async publishPoll(pollId, options = {}) {
3158
4900
  const { reveal = false } = options;
@@ -3162,19 +4904,20 @@ class Keymaster {
3162
4904
  if (id.did !== owner) {
3163
4905
  throw new InvalidParameterError('only owner can publish a poll');
3164
4906
  }
3165
- const view = await this.viewPoll(pollId);
3166
- if (!view.results?.final) {
3167
- throw new InvalidParameterError('poll not final');
4907
+ const config = await this.getPoll(pollId);
4908
+ if (!config) {
4909
+ throw new InvalidParameterError(pollId);
3168
4910
  }
3169
- if (!reveal && view.results.ballots) {
3170
- delete view.results.ballots;
4911
+ const results = await this.computePollResults(pollId, config);
4912
+ if (!results.final) {
4913
+ throw new InvalidParameterError('poll not final');
3171
4914
  }
3172
- const poll = await this.getPoll(pollId);
3173
- if (!poll) {
3174
- throw new InvalidParameterError(pollId);
4915
+ if (!reveal) {
4916
+ delete results.ballots;
3175
4917
  }
3176
- poll.results = view.results;
3177
- return this.mergeData(pollId, { poll });
4918
+ const buffer = Buffer.from(JSON.stringify(results), 'utf-8');
4919
+ await this.addVaultItem(pollId, exports.PollItems.RESULTS, buffer);
4920
+ return true;
3178
4921
  }
3179
4922
  async unpublishPoll(pollId) {
3180
4923
  const id = await this.fetchIdInfo();
@@ -3183,12 +4926,11 @@ class Keymaster {
3183
4926
  if (id.did !== owner) {
3184
4927
  throw new InvalidParameterError(pollId);
3185
4928
  }
3186
- const poll = await this.getPoll(pollId);
3187
- if (!poll) {
4929
+ const config = await this.getPoll(pollId);
4930
+ if (!config) {
3188
4931
  throw new InvalidParameterError(pollId);
3189
4932
  }
3190
- delete poll.results;
3191
- return this.mergeData(pollId, { poll });
4933
+ return this.removeVaultItem(pollId, exports.PollItems.RESULTS);
3192
4934
  }
3193
4935
  async createVault(options = {}) {
3194
4936
  const id = await this.fetchIdInfo();
@@ -3200,10 +4942,10 @@ class Keymaster {
3200
4942
  const salt = this.cipher.generateRandomSalt();
3201
4943
  const vaultKeypair = this.cipher.generateRandomJwk();
3202
4944
  const keys = {};
3203
- const config = this.cipher.encryptMessage(idKeypair.publicJwk, vaultKeypair.privateJwk, JSON.stringify(options));
4945
+ const config = this.cipher.encryptMessage(idKeypair.publicJwk, JSON.stringify(options));
3204
4946
  const publicJwk = options.secretMembers ? idKeypair.publicJwk : vaultKeypair.publicJwk; // If secret, encrypt for the owner only
3205
- const members = this.cipher.encryptMessage(publicJwk, vaultKeypair.privateJwk, JSON.stringify({}));
3206
- const items = this.cipher.encryptMessage(vaultKeypair.publicJwk, vaultKeypair.privateJwk, JSON.stringify({}));
4947
+ const members = this.cipher.encryptMessage(publicJwk, JSON.stringify({}));
4948
+ const items = this.cipher.encryptMessage(vaultKeypair.publicJwk, JSON.stringify({}));
3207
4949
  const sha256 = this.cipher.hashJSON({});
3208
4950
  const vault = {
3209
4951
  version,
@@ -3234,6 +4976,9 @@ class Keymaster {
3234
4976
  return false;
3235
4977
  }
3236
4978
  }
4979
+ generateBallotKey(vault, memberDID) {
4980
+ return this.generateSaltedId(vault, memberDID).slice(0, this.maxAliasLength);
4981
+ }
3237
4982
  generateSaltedId(vault, memberDID) {
3238
4983
  if (!vault.version) {
3239
4984
  return this.cipher.hashMessage(vault.salt + memberDID);
@@ -3272,13 +5017,13 @@ class Keymaster {
3272
5017
  }
3273
5018
  else {
3274
5019
  try {
3275
- const membersJSON = this.cipher.decryptMessage(vault.publicJwk, privateJwk, vault.members);
5020
+ const membersJSON = this.cipher.decryptMessage(privateJwk, vault.members, vault.publicJwk);
3276
5021
  members = JSON.parse(membersJSON);
3277
5022
  }
3278
5023
  catch (error) {
3279
5024
  }
3280
5025
  }
3281
- const itemsJSON = this.cipher.decryptMessage(vault.publicJwk, privateJwk, vault.items);
5026
+ const itemsJSON = this.cipher.decryptMessage(privateJwk, vault.items, vault.publicJwk);
3282
5027
  const items = JSON.parse(itemsJSON);
3283
5028
  return {
3284
5029
  isOwner,
@@ -3300,7 +5045,7 @@ class Keymaster {
3300
5045
  async addMemberKey(vault, memberDID, privateJwk) {
3301
5046
  const memberDoc = await this.resolveDID(memberDID, { confirm: true });
3302
5047
  const memberPublicJwk = this.getPublicKeyJwk(memberDoc);
3303
- const memberKey = this.cipher.encryptMessage(memberPublicJwk, privateJwk, JSON.stringify(privateJwk));
5048
+ const memberKey = this.cipher.encryptMessage(memberPublicJwk, JSON.stringify(privateJwk));
3304
5049
  const memberKeyId = this.generateSaltedId(vault, memberDID);
3305
5050
  vault.keys[memberKeyId] = memberKey;
3306
5051
  }
@@ -3345,7 +5090,7 @@ class Keymaster {
3345
5090
  }
3346
5091
  members[memberDID] = { added: new Date().toISOString() };
3347
5092
  const publicJwk = config.secretMembers ? idKeypair.publicJwk : vault.publicJwk;
3348
- vault.members = this.cipher.encryptMessage(publicJwk, privateJwk, JSON.stringify(members));
5093
+ vault.members = this.cipher.encryptMessage(publicJwk, JSON.stringify(members));
3349
5094
  await this.addMemberKey(vault, memberDID, privateJwk);
3350
5095
  return this.mergeData(vaultId, { vault });
3351
5096
  }
@@ -3353,7 +5098,7 @@ class Keymaster {
3353
5098
  const owner = await this.checkVaultOwner(vaultId);
3354
5099
  const idKeypair = await this.fetchKeyPair();
3355
5100
  const vault = await this.getVault(vaultId);
3356
- const { privateJwk, config, members } = await this.decryptVault(vault);
5101
+ const { config, members } = await this.decryptVault(vault);
3357
5102
  const memberDoc = await this.resolveDID(memberId, { confirm: true });
3358
5103
  const memberDID = this.getAgentDID(memberDoc);
3359
5104
  // Don't allow removing the vault owner
@@ -3362,7 +5107,7 @@ class Keymaster {
3362
5107
  }
3363
5108
  delete members[memberDID];
3364
5109
  const publicJwk = config.secretMembers ? idKeypair.publicJwk : vault.publicJwk;
3365
- vault.members = this.cipher.encryptMessage(publicJwk, privateJwk, JSON.stringify(members));
5110
+ vault.members = this.cipher.encryptMessage(publicJwk, JSON.stringify(members));
3366
5111
  const memberKeyId = this.generateSaltedId(vault, memberDID);
3367
5112
  delete vault.keys[memberKeyId];
3368
5113
  return this.mergeData(vaultId, { vault });
@@ -3378,9 +5123,9 @@ class Keymaster {
3378
5123
  async addVaultItem(vaultId, name, buffer) {
3379
5124
  await this.checkVaultOwner(vaultId);
3380
5125
  const vault = await this.getVault(vaultId);
3381
- const { privateJwk, items } = await this.decryptVault(vault);
5126
+ const { items } = await this.decryptVault(vault);
3382
5127
  const validName = this.validateAlias(name);
3383
- const encryptedData = this.cipher.encryptBytes(vault.publicJwk, privateJwk, buffer);
5128
+ const encryptedData = this.cipher.encryptBytes(vault.publicJwk, buffer);
3384
5129
  const cid = await this.gatekeeper.addText(encryptedData);
3385
5130
  const sha256 = this.cipher.hashMessage(buffer);
3386
5131
  const type = await this.getMimeType(buffer);
@@ -3393,16 +5138,16 @@ class Keymaster {
3393
5138
  added: new Date().toISOString(),
3394
5139
  data,
3395
5140
  };
3396
- vault.items = this.cipher.encryptMessage(vault.publicJwk, privateJwk, JSON.stringify(items));
5141
+ vault.items = this.cipher.encryptMessage(vault.publicJwk, JSON.stringify(items));
3397
5142
  vault.sha256 = this.cipher.hashJSON(items);
3398
5143
  return this.mergeData(vaultId, { vault });
3399
5144
  }
3400
5145
  async removeVaultItem(vaultId, name) {
3401
5146
  await this.checkVaultOwner(vaultId);
3402
5147
  const vault = await this.getVault(vaultId);
3403
- const { privateJwk, items } = await this.decryptVault(vault);
5148
+ const { items } = await this.decryptVault(vault);
3404
5149
  delete items[name];
3405
- vault.items = this.cipher.encryptMessage(vault.publicJwk, privateJwk, JSON.stringify(items));
5150
+ vault.items = this.cipher.encryptMessage(vault.publicJwk, JSON.stringify(items));
3406
5151
  vault.sha256 = this.cipher.hashJSON(items);
3407
5152
  return this.mergeData(vaultId, { vault });
3408
5153
  }
@@ -3421,7 +5166,7 @@ class Keymaster {
3421
5166
  if (!encryptedData) {
3422
5167
  throw new KeymasterError(`Failed to retrieve data for item '${name}' (CID: ${items[name].cid})`);
3423
5168
  }
3424
- const bytes = this.cipher.decryptBytes(vault.publicJwk, privateJwk, encryptedData);
5169
+ const bytes = this.cipher.decryptBytes(privateJwk, encryptedData, vault.publicJwk);
3425
5170
  return Buffer.from(bytes);
3426
5171
  }
3427
5172
  async listDmail() {
@@ -3705,7 +5450,7 @@ class Keymaster {
3705
5450
  if (poll) {
3706
5451
  const names = await this.listAliases();
3707
5452
  if (!Object.values(names).includes(noticeDID)) {
3708
- await this.addUnaliasedPoll(noticeDID);
5453
+ await this.addUnaliasedPoll(noticeDID, poll.name);
3709
5454
  }
3710
5455
  await this.addToNotices(did, [exports.NoticeTags.POLL]);
3711
5456
  continue;
@@ -3789,10 +5534,20 @@ class Keymaster {
3789
5534
  }
3790
5535
  return payload && typeof payload.poll === "string" && typeof payload.vote === "number";
3791
5536
  }
3792
- async addUnaliasedPoll(did) {
3793
- const fallbackName = did.slice(-32);
5537
+ async addUnaliasedPoll(did, name) {
5538
+ const baseName = name || did.slice(-32);
5539
+ const aliases = await this.listAliases();
5540
+ let candidate = baseName;
5541
+ let suffix = 2;
5542
+ while (candidate in aliases) {
5543
+ if (aliases[candidate] === did) {
5544
+ return; // Already aliased to this DID
5545
+ }
5546
+ candidate = `${baseName}-${suffix}`;
5547
+ suffix++;
5548
+ }
3794
5549
  try {
3795
- await this.addAlias(fallbackName, did);
5550
+ await this.addAlias(candidate, did);
3796
5551
  }
3797
5552
  catch { }
3798
5553
  }
@@ -3808,26 +5563,27 @@ class Keymaster {
3808
5563
  const { version, seed, ...rest } = decrypted;
3809
5564
  const safeSeed = { mnemonicEnc: seed.mnemonicEnc };
3810
5565
  const hdkey = await this.getHDKeyFromCacheOrMnemonic(decrypted);
3811
- const { publicJwk, privateJwk } = this.cipher.generateJwk(hdkey.privateKey);
5566
+ const { publicJwk } = this.cipher.generateJwk(hdkey.privateKey);
3812
5567
  const plaintext = JSON.stringify(rest);
3813
- const enc = this.cipher.encryptMessage(publicJwk, privateJwk, plaintext);
5568
+ const enc = this.cipher.encryptMessage(publicJwk, plaintext);
3814
5569
  return { version: version, seed: safeSeed, enc };
3815
5570
  }
3816
5571
  async decryptWalletFromStorage(stored) {
3817
5572
  let mnemonic;
3818
5573
  try {
3819
- mnemonic = await encryption.decMnemonic(stored.seed.mnemonicEnc, this.passphrase);
5574
+ mnemonic = await decryptWithPassphrase(stored.seed.mnemonicEnc, this.passphrase);
3820
5575
  }
3821
5576
  catch (error) {
3822
- // OperationError is thrown by crypto.subtle.decrypt when the passphrase is wrong
3823
- if (error?.name === 'OperationError') {
5577
+ const msg = error?.message || '';
5578
+ // OperationError: Web Crypto API (legacy); 'invalid ghash tag': @noble/ciphers
5579
+ if (error?.name === 'OperationError' || msg.includes('invalid ghash tag')) {
3824
5580
  throw new KeymasterError('Incorrect passphrase.');
3825
5581
  }
3826
5582
  throw error;
3827
5583
  }
3828
5584
  this._hdkeyCache = this.cipher.generateHDKey(mnemonic);
3829
5585
  const { publicJwk, privateJwk } = this.cipher.generateJwk(this._hdkeyCache.privateKey);
3830
- const plaintext = this.cipher.decryptMessage(publicJwk, privateJwk, stored.enc);
5586
+ const plaintext = this.cipher.decryptMessage(privateJwk, stored.enc, publicJwk);
3831
5587
  const data = JSON.parse(plaintext);
3832
5588
  const wallet = { version: stored.version, seed: stored.seed, ...data };
3833
5589
  return wallet;