dignity.js 0.5.2 → 0.5.4

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.
@@ -16,43 +16,6 @@ var __commonJS = (cb, mod) => function __require2() {
16
16
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
17
17
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
18
18
 
19
- // src/utils/event-emitter.js
20
- var require_event_emitter = __commonJS({
21
- "src/utils/event-emitter.js"(exports, module) {
22
- var EventEmitter = class {
23
- constructor() {
24
- this.handlers = /* @__PURE__ */ new Map();
25
- }
26
- on(eventName, handler) {
27
- if (!this.handlers.has(eventName)) {
28
- this.handlers.set(eventName, /* @__PURE__ */ new Set());
29
- }
30
- this.handlers.get(eventName).add(handler);
31
- }
32
- off(eventName, handler) {
33
- const eventHandlers = this.handlers.get(eventName);
34
- if (!eventHandlers) {
35
- return;
36
- }
37
- eventHandlers.delete(handler);
38
- if (eventHandlers.size === 0) {
39
- this.handlers.delete(eventName);
40
- }
41
- }
42
- emit(eventName, payload) {
43
- const eventHandlers = this.handlers.get(eventName);
44
- if (!eventHandlers) {
45
- return;
46
- }
47
- for (const handler of eventHandlers) {
48
- handler(payload);
49
- }
50
- }
51
- };
52
- module.exports = EventEmitter;
53
- }
54
- });
55
-
56
19
  // (disabled):crypto
57
20
  var require_crypto = __commonJS({
58
21
  "(disabled):crypto"() {
@@ -2349,6 +2312,43 @@ var require_nacl_util = __commonJS({
2349
2312
  }
2350
2313
  });
2351
2314
 
2315
+ // src/utils/event-emitter.js
2316
+ var require_event_emitter = __commonJS({
2317
+ "src/utils/event-emitter.js"(exports, module) {
2318
+ var EventEmitter = class {
2319
+ constructor() {
2320
+ this.handlers = /* @__PURE__ */ new Map();
2321
+ }
2322
+ on(eventName, handler) {
2323
+ if (!this.handlers.has(eventName)) {
2324
+ this.handlers.set(eventName, /* @__PURE__ */ new Set());
2325
+ }
2326
+ this.handlers.get(eventName).add(handler);
2327
+ }
2328
+ off(eventName, handler) {
2329
+ const eventHandlers = this.handlers.get(eventName);
2330
+ if (!eventHandlers) {
2331
+ return;
2332
+ }
2333
+ eventHandlers.delete(handler);
2334
+ if (eventHandlers.size === 0) {
2335
+ this.handlers.delete(eventName);
2336
+ }
2337
+ }
2338
+ emit(eventName, payload) {
2339
+ const eventHandlers = this.handlers.get(eventName);
2340
+ if (!eventHandlers) {
2341
+ return;
2342
+ }
2343
+ for (const handler of eventHandlers) {
2344
+ handler(payload);
2345
+ }
2346
+ }
2347
+ };
2348
+ module.exports = EventEmitter;
2349
+ }
2350
+ });
2351
+
2352
2352
  // src/security/sloth-vdf.js
2353
2353
  var require_sloth_vdf = __commonJS({
2354
2354
  "src/security/sloth-vdf.js"(exports, module) {
@@ -2361,25 +2361,25 @@ var require_sloth_vdf = __commonJS({
2361
2361
  let powBase = base % modulus;
2362
2362
  let powExponent = exponent;
2363
2363
  while (powExponent > 0) {
2364
- if (powExponent % BigInt(2) === BigInt(1)) {
2364
+ if ((powExponent & BigInt(1)) === BigInt(1)) {
2365
2365
  result = result * powBase % modulus;
2366
2366
  }
2367
- powExponent = powExponent / BigInt(2);
2367
+ powExponent = powExponent >> BigInt(1);
2368
2368
  powBase = powBase * powBase % modulus;
2369
2369
  }
2370
2370
  return result;
2371
2371
  }
2372
2372
  quadRes(x) {
2373
- return this.fastPow(x, (_SlothPermutation.p - BigInt(1)) / BigInt(2), _SlothPermutation.p) === BigInt(1);
2373
+ return this.fastPow(x, _SlothPermutation.pHalf, _SlothPermutation.p) === BigInt(1);
2374
2374
  }
2375
2375
  modSqrtOp(x) {
2376
2376
  let y;
2377
2377
  let value = x;
2378
2378
  if (this.quadRes(value)) {
2379
- y = this.fastPow(value, (_SlothPermutation.p + BigInt(1)) / BigInt(4), _SlothPermutation.p);
2379
+ y = this.fastPow(value, _SlothPermutation.pQuarter, _SlothPermutation.p);
2380
2380
  } else {
2381
2381
  value = (-value + _SlothPermutation.p) % _SlothPermutation.p;
2382
- y = this.fastPow(value, (_SlothPermutation.p + BigInt(1)) / BigInt(4), _SlothPermutation.p);
2382
+ y = this.fastPow(value, _SlothPermutation.pQuarter, _SlothPermutation.p);
2383
2383
  }
2384
2384
  return y;
2385
2385
  }
@@ -2411,6 +2411,12 @@ var require_sloth_vdf = __commonJS({
2411
2411
  __publicField(_SlothPermutation, "p", BigInt(
2412
2412
  "170082004324204494273811327264862981553264701145937538369570764779791492622392118654022654452947093285873855529044371650895045691292912712699015605832276411308653107069798639938826015099738961427172366594187783204437869906954750443653318078358839409699824714551430573905637228307966826784684174483831608534979"
2413
2413
  ));
2414
+ // precompute values for optimization:
2415
+ // (p - 1) / 2
2416
+ __publicField(_SlothPermutation, "pHalf", _SlothPermutation.p - BigInt(1) >> BigInt(1));
2417
+ // (p + 1) / 4
2418
+ // p ≡ 3 (mod 4) ⇒ (p+1) divisible by 4
2419
+ __publicField(_SlothPermutation, "pQuarter", _SlothPermutation.p + BigInt(1) >> BigInt(2));
2414
2420
  var SlothPermutation = _SlothPermutation;
2415
2421
  module.exports = SlothPermutation;
2416
2422
  }
@@ -2871,8 +2877,17 @@ var require_message_security_service = __commonJS({
2871
2877
  // src/core/dignity-p2p.js
2872
2878
  var require_dignity_p2p = __commonJS({
2873
2879
  "src/core/dignity-p2p.js"(exports, module) {
2880
+ var nacl = require_nacl_fast();
2881
+ var naclUtil = require_nacl_util();
2874
2882
  var EventEmitter = require_event_emitter();
2875
- var { MessageSecurityService } = require_message_security_service();
2883
+ var { MessageSecurityService, stableStringify } = require_message_security_service();
2884
+ function computeContentHash(data) {
2885
+ const canonical = stableStringify(data || {});
2886
+ const bytes = naclUtil.decodeUTF8(canonical);
2887
+ const hash = nacl.hash(bytes);
2888
+ const hex = Array.from(hash, (b) => b.toString(16).padStart(2, "0")).join("");
2889
+ return `sha512:${hex}`;
2890
+ }
2876
2891
  var DignityP2P = class extends EventEmitter {
2877
2892
  constructor({ nodeId, networkAdapter, idGenerator, now, security } = {}) {
2878
2893
  super();
@@ -2931,6 +2946,7 @@ var require_dignity_p2p = __commonJS({
2931
2946
  if (!record || record.deletedAt) {
2932
2947
  return null;
2933
2948
  }
2949
+ const normalizedData = { ...record.data || {} };
2934
2950
  return {
2935
2951
  id: record.id,
2936
2952
  ownerId: record.ownerId,
@@ -2938,7 +2954,8 @@ var require_dignity_p2p = __commonJS({
2938
2954
  createdAt: record.createdAt,
2939
2955
  updatedAt: record.updatedAt,
2940
2956
  version: record.version,
2941
- data: { ...record.data }
2957
+ hash: record.hash || computeContentHash(normalizedData),
2958
+ data: normalizedData
2942
2959
  };
2943
2960
  }
2944
2961
  canUpdateRecord(record, actorId) {
@@ -3016,7 +3033,7 @@ var require_dignity_p2p = __commonJS({
3016
3033
  ownerId: this.nodeId,
3017
3034
  collaboratorIds,
3018
3035
  timestamp,
3019
- payload: { ...data }
3036
+ payload: { ...data || {} }
3020
3037
  };
3021
3038
  this.applyOperation(operation);
3022
3039
  await this.broadcastMessage("operation", operation, {
@@ -3549,11 +3566,23 @@ var require_dignity_p2p = __commonJS({
3549
3566
  if (current && current.version >= record.version) {
3550
3567
  return false;
3551
3568
  }
3569
+ const restoredData = { ...record.data || {} };
3570
+ const computedHash = computeContentHash(restoredData);
3571
+ if (record.hash && record.hash !== computedHash) {
3572
+ this.emit("warning", {
3573
+ type: "content-hash-mismatch",
3574
+ collection: collectionName,
3575
+ id: record.id,
3576
+ advertisedHash: record.hash,
3577
+ computedHash
3578
+ });
3579
+ }
3552
3580
  collection.set(record.id, {
3553
3581
  id: record.id,
3554
3582
  ownerId: record.ownerId,
3555
3583
  collaboratorIds: this.normalizeCollaboratorIds(record.collaboratorIds),
3556
- data: { ...record.data || {} },
3584
+ data: restoredData,
3585
+ hash: computedHash,
3557
3586
  createdAt: record.createdAt,
3558
3587
  updatedAt: record.updatedAt,
3559
3588
  deletedAt: record.deletedAt || null,
@@ -3571,7 +3600,8 @@ var require_dignity_p2p = __commonJS({
3571
3600
  id: raw.id,
3572
3601
  ownerId: raw.ownerId,
3573
3602
  collaboratorIds: Array.isArray(raw.collaboratorIds) ? [...raw.collaboratorIds] : [],
3574
- data: { ...raw.data },
3603
+ data: { ...raw.data || {} },
3604
+ hash: raw.hash || computeContentHash(raw.data || {}),
3575
3605
  createdAt: raw.createdAt,
3576
3606
  updatedAt: raw.updatedAt,
3577
3607
  deletedAt: raw.deletedAt || null,
@@ -3601,7 +3631,8 @@ var require_dignity_p2p = __commonJS({
3601
3631
  id: operation.id,
3602
3632
  ownerId: operation.ownerId,
3603
3633
  collaboratorIds: this.normalizeCollaboratorIds(operation.collaboratorIds),
3604
- data: { ...operation.payload },
3634
+ data: { ...operation.payload || {} },
3635
+ hash: computeContentHash(operation.payload || {}),
3605
3636
  createdAt: operation.timestamp,
3606
3637
  updatedAt: operation.timestamp,
3607
3638
  deletedAt: null,
@@ -3704,6 +3735,7 @@ var require_dignity_p2p = __commonJS({
3704
3735
  ...current.data,
3705
3736
  ...operation.payload
3706
3737
  };
3738
+ current.hash = computeContentHash(current.data);
3707
3739
  if (Array.isArray(operation.collaboratorIds) && operation.actorId === current.ownerId) {
3708
3740
  current.collaboratorIds = this.normalizeCollaboratorIds(operation.collaboratorIds);
3709
3741
  }
@@ -3789,6 +3821,14 @@ var require_signaling_pool = __commonJS({
3789
3821
  // src/signaling/websocket-signaling-provider.js
3790
3822
  var require_websocket_signaling_provider = __commonJS({
3791
3823
  "src/signaling/websocket-signaling-provider.js"(exports, module) {
3824
+ function randomBase36(length) {
3825
+ let value = "";
3826
+ while (value.length < length) {
3827
+ const chunk = Math.random().toString(36).slice(2);
3828
+ value += chunk.length > 0 ? chunk : "0";
3829
+ }
3830
+ return value.slice(0, length);
3831
+ }
3792
3832
  var WebSocketSignalingProvider = class {
3793
3833
  constructor({ id, url, WebSocketImpl, priority = 0 }) {
3794
3834
  if (!url) {
@@ -3832,8 +3872,8 @@ var require_websocket_signaling_provider = __commonJS({
3832
3872
  if (!peerJsHostPattern.test(this.url)) {
3833
3873
  return this.url;
3834
3874
  }
3835
- const connectionId = `dignityjs_${Math.random().toString(36).slice(2, 12)}`;
3836
- const token = Math.random().toString(36).slice(2, 12);
3875
+ const connectionId = `dignityjs_${randomBase36(10)}`;
3876
+ const token = randomBase36(10);
3837
3877
  const hasQuery = this.url.includes("?");
3838
3878
  const hasId = /[?&]id=/.test(this.url);
3839
3879
  const hasToken = /[?&]token=/.test(this.url);
@@ -11059,6 +11099,7 @@ var require_indexeddb_persistence = __commonJS({
11059
11099
  ownerId: record.ownerId,
11060
11100
  collaboratorIds: Array.isArray(record.collaboratorIds) ? [...record.collaboratorIds] : [],
11061
11101
  data: { ...record.data },
11102
+ hash: record.hash || null,
11062
11103
  createdAt: record.createdAt,
11063
11104
  updatedAt: record.updatedAt,
11064
11105
  deletedAt: record.deletedAt,
@@ -11119,6 +11160,7 @@ var require_indexeddb_persistence = __commonJS({
11119
11160
  ownerId: stored.ownerId,
11120
11161
  collaboratorIds: stored.collaboratorIds,
11121
11162
  data: stored.data,
11163
+ hash: stored.hash || null,
11122
11164
  createdAt: stored.createdAt,
11123
11165
  updatedAt: stored.updatedAt,
11124
11166
  deletedAt: stored.deletedAt,