@dxos/keys 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b

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.
@@ -82,28 +82,69 @@ var require_base32_decode = __commonJS({
82
82
  });
83
83
 
84
84
  // src/dxn.ts
85
- import { Schema as Schema3 } from "effect";
85
+ import * as Schema3 from "effect/Schema";
86
86
  import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
87
87
  import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
88
88
 
89
89
  // src/object-id.ts
90
- import { Schema } from "effect";
91
- import { ulid } from "ulidx";
90
+ import * as Schema from "effect/Schema";
91
+ import { monotonicFactory } from "ulidx";
92
92
  var ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
93
- description: "a Universally Unique Lexicographically Sortable Identifier",
93
+ description: "A Universally Unique Lexicographically Sortable Identifier",
94
94
  pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
95
95
  });
96
96
  var ObjectId = class extends ObjectIdSchema {
97
+ static #factory = monotonicFactory();
98
+ static #seedTime = void 0;
97
99
  static isValid(id) {
98
100
  try {
99
101
  Schema.decodeSync(ObjectId)(id);
100
102
  return true;
101
- } catch (err) {
103
+ } catch {
102
104
  return false;
103
105
  }
104
106
  }
105
107
  static random() {
106
- return ulid();
108
+ return this.#factory(this.#seedTime);
109
+ }
110
+ static dangerouslyDisableRandomness() {
111
+ this.#factory = monotonicFactory(makeTestPRNG());
112
+ this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
113
+ }
114
+ };
115
+ var makeTestPRNG = () => {
116
+ const rng = new SimplePRNG();
117
+ return () => {
118
+ return rng.next();
119
+ };
120
+ };
121
+ var SimplePRNG = class _SimplePRNG {
122
+ #seed;
123
+ // LCG parameters (from Numerical Recipes)
124
+ static #a = 1664525;
125
+ static #c = 1013904223;
126
+ static #m = Math.pow(2, 32);
127
+ /**
128
+ * Creates a new PRNG instance.
129
+ * @param seed - Initial seed value. If not provided, uses 0.
130
+ */
131
+ constructor(seed = 0) {
132
+ this.#seed = seed;
133
+ }
134
+ /**
135
+ * Generates the next pseudo-random number in the range [0, 1).
136
+ * @returns A pseudo-random number between 0 (inclusive) and 1 (exclusive).
137
+ */
138
+ next() {
139
+ this.#seed = (_SimplePRNG.#a * this.#seed + _SimplePRNG.#c) % _SimplePRNG.#m;
140
+ return this.#seed / _SimplePRNG.#m;
141
+ }
142
+ /**
143
+ * Resets the generator with a new seed.
144
+ * @param seed - New seed value.
145
+ */
146
+ reset(seed) {
147
+ this.#seed = seed;
107
148
  }
108
149
  };
109
150
 
@@ -170,7 +211,7 @@ function base32Encode(data, variant, options) {
170
211
  }
171
212
 
172
213
  // src/space-id.ts
173
- import { Schema as Schema2 } from "effect";
214
+ import * as Schema2 from "effect/Schema";
174
215
  import { invariant } from "@dxos/invariant";
175
216
 
176
217
  // src/random-bytes.ts
@@ -189,114 +230,107 @@ var isValid = (value) => {
189
230
  return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
190
231
  };
191
232
  var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
192
- static {
193
- this.byteLength = 20;
194
- }
195
- static {
196
- this.encode = (value) => {
197
- invariant(value instanceof Uint8Array, "Invalid type", {
198
- F: __dxlog_file,
199
- L: 43,
200
- S: this,
201
- A: [
202
- "value instanceof Uint8Array",
203
- "'Invalid type'"
204
- ]
205
- });
206
- invariant(value.length === SpaceId.byteLength, "Invalid length", {
207
- F: __dxlog_file,
208
- L: 44,
209
- S: this,
210
- A: [
211
- "value.length === SpaceId.byteLength",
212
- "'Invalid length'"
213
- ]
214
- });
215
- return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
216
- };
217
- }
218
- static {
219
- this.decode = (value) => {
220
- invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
221
- F: __dxlog_file,
222
- L: 49,
223
- S: this,
224
- A: [
225
- "value.startsWith(MULTIBASE_PREFIX)",
226
- "'Invalid multibase32 encoding'"
227
- ]
228
- });
229
- return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
230
- };
231
- }
232
- static {
233
- this.isValid = isValid;
234
- }
235
- static {
236
- this.random = () => {
237
- return SpaceId.encode(randomBytes(SpaceId.byteLength));
238
- };
239
- }
233
+ static byteLength = 20;
234
+ static encode = (value) => {
235
+ invariant(value instanceof Uint8Array, "Invalid type", {
236
+ F: __dxlog_file,
237
+ L: 43,
238
+ S: this,
239
+ A: [
240
+ "value instanceof Uint8Array",
241
+ "'Invalid type'"
242
+ ]
243
+ });
244
+ invariant(value.length === SpaceId.byteLength, "Invalid length", {
245
+ F: __dxlog_file,
246
+ L: 44,
247
+ S: this,
248
+ A: [
249
+ "value.length === SpaceId.byteLength",
250
+ "'Invalid length'"
251
+ ]
252
+ });
253
+ return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
254
+ };
255
+ static decode = (value) => {
256
+ invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
257
+ F: __dxlog_file,
258
+ L: 49,
259
+ S: this,
260
+ A: [
261
+ "value.startsWith(MULTIBASE_PREFIX)",
262
+ "'Invalid multibase32 encoding'"
263
+ ]
264
+ });
265
+ return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
266
+ };
267
+ static isValid = isValid;
268
+ static random = () => {
269
+ return SpaceId.encode(randomBytes(SpaceId.byteLength));
270
+ };
240
271
  };
241
272
 
242
273
  // src/dxn.ts
243
274
  var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
244
275
  var LOCAL_SPACE_TAG = "@";
276
+ var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
245
277
  var QueueSubspaceTags = Object.freeze({
246
278
  DATA: "data",
247
279
  TRACE: "trace"
248
280
  });
249
281
  var DXN = class _DXN {
250
- static {
251
- // TODO(dmaretskyi): Should this be a transformation into the DXN type?
252
- this.Schema = Schema3.NonEmptyString.pipe(
253
- Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
254
- // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
255
- // FormatAnnotation.set(FormatEnum.DXN),
256
- Schema3.annotations({
257
- title: "DXN",
258
- description: "DXN URI",
259
- examples: [
260
- "dxn:type:example.com/type/MyType",
261
- "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
262
- ]
263
- })
264
- );
265
- }
282
+ // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
283
+ // TODO(dmaretskyi): Should this be a transformation into the DXN type?
284
+ static Schema = Schema3.NonEmptyString.pipe(
285
+ Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
286
+ // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
287
+ // FormatAnnotation.set(TypeFormat.DXN),
288
+ Schema3.annotations({
289
+ title: "DXN",
290
+ description: "DXN URI",
291
+ examples: [
292
+ "dxn:type:example.com/type/MyType",
293
+ "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
294
+ ]
295
+ })
296
+ );
266
297
  static hash(dxn) {
267
298
  return dxn.toString();
268
299
  }
269
- static {
300
+ /**
301
+ * Kind constants.
302
+ */
303
+ static kind = Object.freeze({
270
304
  /**
271
- * Kind constants.
305
+ * dxn:type:<type_name>[:<version>]
272
306
  */
273
- this.kind = Object.freeze({
274
- /**
275
- * dxn:type:<type name>[:<version>]
276
- */
277
- TYPE: "type",
278
- /**
279
- * dxn:echo:<space id>:<echo id>
280
- * dxn:echo:@:<echo id>
281
- */
282
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
283
- // TODO(burdon): Add separate Kind for space.
284
- ECHO: "echo",
285
- /**
286
- * The subspace tag enables us to partition queues by usage within the context of a space.
287
- * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
288
- * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
289
- * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
290
- */
291
- QUEUE: "queue"
292
- });
293
- }
294
- get kind() {
295
- return this.#kind;
296
- }
307
+ TYPE: "type",
308
+ /**
309
+ * dxn:echo:<space_id>:<echo_id>
310
+ * dxn:echo:@:<echo_id>
311
+ */
312
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
313
+ // TODO(burdon): Add separate Kind for space?
314
+ ECHO: "echo",
315
+ /**
316
+ * The subspace tag enables us to partition queues by usage within the context of a space.
317
+ * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
318
+ * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
319
+ * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
320
+ */
321
+ QUEUE: "queue"
322
+ });
323
+ /**
324
+ * Exactly equals.
325
+ */
297
326
  static equals(a, b) {
298
327
  return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
299
328
  }
329
+ static equalsEchoId(a, b) {
330
+ const a1 = a.asEchoDXN();
331
+ const b1 = b.asEchoDXN();
332
+ return !!a1 && !!b1 && a1.echoId === b1.echoId;
333
+ }
300
334
  // TODO(burdon): Rename isValid.
301
335
  static isDXNString(dxn) {
302
336
  return dxn.startsWith("dxn:");
@@ -320,12 +354,12 @@ var DXN = class _DXN {
320
354
  static tryParse(dxn) {
321
355
  try {
322
356
  return _DXN.parse(dxn);
323
- } catch (error) {
357
+ } catch {
324
358
  return void 0;
325
359
  }
326
360
  }
327
361
  /**
328
- * @example `dxn:type:example.com/type/Contact`
362
+ * @example `dxn:type:example.com/type/Person`
329
363
  */
330
364
  static fromTypename(typename) {
331
365
  return new _DXN(_DXN.kind.TYPE, [
@@ -333,7 +367,7 @@ var DXN = class _DXN {
333
367
  ]);
334
368
  }
335
369
  /**
336
- * @example `dxn:type:example.com/type/Contact:0.1.0`
370
+ * @example `dxn:type:example.com/type/Person:0.1.0`
337
371
  */
338
372
  // TODO(dmaretskyi): Consider using @ as the version separator.
339
373
  static fromTypenameAndVersion(typename, version) {
@@ -343,43 +377,30 @@ var DXN = class _DXN {
343
377
  ]);
344
378
  }
345
379
  /**
380
+ * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
381
+ */
382
+ static fromSpaceAndObjectId(spaceId, objectId) {
383
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
384
+ assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
385
+ return new _DXN(_DXN.kind.ECHO, [
386
+ spaceId,
387
+ objectId
388
+ ]);
389
+ }
390
+ /**
346
391
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
347
392
  */
348
393
  static fromLocalObjectId(id) {
349
- assertArgument(ObjectId.isValid(id), `Invalid object ID: ${id}`);
394
+ assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
350
395
  return new _DXN(_DXN.kind.ECHO, [
351
396
  LOCAL_SPACE_TAG,
352
397
  id
353
398
  ]);
354
399
  }
355
400
  static fromQueue(subspaceTag, spaceId, queueId, objectId) {
356
- invariant2(SpaceId.isValid(spaceId), void 0, {
357
- F: __dxlog_file2,
358
- L: 151,
359
- S: this,
360
- A: [
361
- "SpaceId.isValid(spaceId)",
362
- ""
363
- ]
364
- });
365
- invariant2(ObjectId.isValid(queueId), void 0, {
366
- F: __dxlog_file2,
367
- L: 152,
368
- S: this,
369
- A: [
370
- "ObjectId.isValid(queueId)",
371
- ""
372
- ]
373
- });
374
- invariant2(!objectId || ObjectId.isValid(objectId), void 0, {
375
- F: __dxlog_file2,
376
- L: 153,
377
- S: this,
378
- A: [
379
- "!objectId || ObjectId.isValid(objectId)",
380
- ""
381
- ]
382
- });
401
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
402
+ assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
403
+ assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
383
404
  return new _DXN(_DXN.kind.QUEUE, [
384
405
  subspaceTag,
385
406
  spaceId,
@@ -392,33 +413,17 @@ var DXN = class _DXN {
392
413
  #kind;
393
414
  #parts;
394
415
  constructor(kind, parts) {
395
- invariant2(parts.length > 0, void 0, {
396
- F: __dxlog_file2,
397
- L: 162,
398
- S: this,
399
- A: [
400
- "parts.length > 0",
401
- ""
402
- ]
403
- });
404
- invariant2(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
405
- F: __dxlog_file2,
406
- L: 163,
407
- S: this,
408
- A: [
409
- "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
410
- ""
411
- ]
412
- });
416
+ assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
417
+ assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
413
418
  switch (kind) {
414
419
  case _DXN.kind.TYPE:
415
420
  if (parts.length > 2) {
416
- throw new Error('Invalid "type" DXN');
421
+ throw new Error("Invalid DXN.kind.TYPE");
417
422
  }
418
423
  break;
419
424
  case _DXN.kind.ECHO:
420
425
  if (parts.length !== 2) {
421
- throw new Error('Invalid "echo" DXN');
426
+ throw new Error("Invalid DXN.kind.ECHO");
422
427
  }
423
428
  break;
424
429
  }
@@ -453,6 +458,9 @@ var DXN = class _DXN {
453
458
  }
454
459
  };
455
460
  }
461
+ get kind() {
462
+ return this.#kind;
463
+ }
456
464
  get parts() {
457
465
  return this.#parts;
458
466
  }
@@ -460,7 +468,7 @@ var DXN = class _DXN {
460
468
  get typename() {
461
469
  invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
462
470
  F: __dxlog_file2,
463
- L: 218,
471
+ L: 250,
464
472
  S: this,
465
473
  A: [
466
474
  "this.#kind === DXN.kind.TYPE",
@@ -469,6 +477,9 @@ var DXN = class _DXN {
469
477
  });
470
478
  return this.#parts[0];
471
479
  }
480
+ equals(other) {
481
+ return _DXN.equals(this, other);
482
+ }
472
483
  hasTypenameOf(typename) {
473
484
  return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
474
485
  }
@@ -493,6 +504,7 @@ var DXN = class _DXN {
493
504
  const [spaceId, echoId] = this.#parts;
494
505
  return {
495
506
  spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
507
+ // TODO(burdon): objectId.
496
508
  echoId
497
509
  };
498
510
  }
@@ -511,6 +523,15 @@ var DXN = class _DXN {
511
523
  objectId
512
524
  };
513
525
  }
526
+ /**
527
+ * Produces a new DXN with the given parts appended.
528
+ */
529
+ extend(parts) {
530
+ return new _DXN(this.#kind, [
531
+ ...this.#parts,
532
+ ...parts
533
+ ]);
534
+ }
514
535
  };
515
536
 
516
537
  // src/identity-did.ts
@@ -572,9 +593,8 @@ var PUBLIC_KEY_LENGTH = 32;
572
593
  var SECRET_KEY_LENGTH = 64;
573
594
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
574
595
  var PublicKey = class _PublicKey {
575
- static {
576
- this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
577
- }
596
+ _value;
597
+ static ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
578
598
  /**
579
599
  * Creates new instance of PublicKey automatically determining the input format.
580
600
  * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
@@ -583,7 +603,7 @@ var PublicKey = class _PublicKey {
583
603
  static from(source) {
584
604
  invariant4(source, void 0, {
585
605
  F: __dxlog_file4,
586
- L: 49,
606
+ L: 50,
587
607
  S: this,
588
608
  A: [
589
609
  "source",
@@ -675,7 +695,7 @@ var PublicKey = class _PublicKey {
675
695
  static bufferize(str) {
676
696
  invariant4(typeof str === "string", "Invalid type", {
677
697
  F: __dxlog_file4,
678
- L: 152,
698
+ L: 153,
679
699
  S: this,
680
700
  A: [
681
701
  "typeof str === 'string'",
@@ -698,7 +718,7 @@ var PublicKey = class _PublicKey {
698
718
  }
699
719
  invariant4(key instanceof Buffer, "Invalid type", {
700
720
  F: __dxlog_file4,
701
- L: 171,
721
+ L: 172,
702
722
  S: this,
703
723
  A: [
704
724
  "key instanceof Buffer",
@@ -717,7 +737,7 @@ var PublicKey = class _PublicKey {
717
737
  static fromMultibase32(encoded) {
718
738
  invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
719
739
  F: __dxlog_file4,
720
- L: 184,
740
+ L: 185,
721
741
  S: this,
722
742
  A: [
723
743
  "encoded.startsWith('B')",
@@ -856,6 +876,7 @@ var PublicKey = class _PublicKey {
856
876
  };
857
877
  export {
858
878
  DXN,
879
+ DXN_ECHO_REGEXP,
859
880
  IdentityDid,
860
881
  LOCAL_SPACE_TAG,
861
882
  ObjectId,
@@ -863,6 +884,7 @@ export {
863
884
  PublicKey,
864
885
  QueueSubspaceTags,
865
886
  SECRET_KEY_LENGTH,
887
+ SimplePRNG,
866
888
  SpaceId
867
889
  };
868
890
  //# sourceMappingURL=index.mjs.map