@dxos/keys 0.8.4-main.fd6878d → 0.8.4-main.fdfb99ef29

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,18 +82,20 @@ 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
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);
@@ -103,7 +105,46 @@ var ObjectId = class extends ObjectIdSchema {
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
@@ -182,7 +223,6 @@ var randomBytes = (length) => {
182
223
  };
183
224
 
184
225
  // src/space-id.ts
185
- var __dxlog_file = "/__w/dxos/dxos/packages/common/keys/src/space-id.ts";
186
226
  var MULTIBASE_PREFIX = "B";
187
227
  var ENCODED_LENGTH = 33;
188
228
  var isValid = (value) => {
@@ -191,36 +231,12 @@ var isValid = (value) => {
191
231
  var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
192
232
  static byteLength = 20;
193
233
  static encode = (value) => {
194
- invariant(value instanceof Uint8Array, "Invalid type", {
195
- F: __dxlog_file,
196
- L: 43,
197
- S: this,
198
- A: [
199
- "value instanceof Uint8Array",
200
- "'Invalid type'"
201
- ]
202
- });
203
- invariant(value.length === SpaceId.byteLength, "Invalid length", {
204
- F: __dxlog_file,
205
- L: 44,
206
- S: this,
207
- A: [
208
- "value.length === SpaceId.byteLength",
209
- "'Invalid length'"
210
- ]
211
- });
234
+ invariant(value instanceof Uint8Array, "Invalid type");
235
+ invariant(value.length === SpaceId.byteLength, "Invalid length");
212
236
  return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
213
237
  };
214
238
  static decode = (value) => {
215
- invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
216
- F: __dxlog_file,
217
- L: 49,
218
- S: this,
219
- A: [
220
- "value.startsWith(MULTIBASE_PREFIX)",
221
- "'Invalid multibase32 encoding'"
222
- ]
223
- });
239
+ invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding");
224
240
  return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
225
241
  };
226
242
  static isValid = isValid;
@@ -230,7 +246,6 @@ var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
230
246
  };
231
247
 
232
248
  // src/dxn.ts
233
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
234
249
  var LOCAL_SPACE_TAG = "@";
235
250
  var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
236
251
  var QueueSubspaceTags = Object.freeze({
@@ -243,12 +258,12 @@ var DXN = class _DXN {
243
258
  static Schema = Schema3.NonEmptyString.pipe(
244
259
  Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
245
260
  // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
246
- // FormatAnnotation.set(FormatEnum.DXN),
261
+ // FormatAnnotation.set(TypeFormat.DXN),
247
262
  Schema3.annotations({
248
263
  title: "DXN",
249
264
  description: "DXN URI",
250
265
  examples: [
251
- "dxn:type:example.com/type/MyType",
266
+ "dxn:type:com.example.type.my-type",
252
267
  "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
253
268
  ]
254
269
  })
@@ -261,15 +276,15 @@ var DXN = class _DXN {
261
276
  */
262
277
  static kind = Object.freeze({
263
278
  /**
264
- * dxn:type:<type name>[:<version>]
279
+ * dxn:type:<type_name>[:<version>]
265
280
  */
266
281
  TYPE: "type",
267
282
  /**
268
- * dxn:echo:<space id>:<echo id>
269
- * dxn:echo:@:<echo id>
283
+ * dxn:echo:<space_id>:<echo_id>
284
+ * dxn:echo:@:<echo_id>
270
285
  */
271
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
272
- // TODO(burdon): Add separate Kind for space.
286
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
287
+ // TODO(burdon): Add separate Kind for space?
273
288
  ECHO: "echo",
274
289
  /**
275
290
  * The subspace tag enables us to partition queues by usage within the context of a space.
@@ -279,12 +294,17 @@ var DXN = class _DXN {
279
294
  */
280
295
  QUEUE: "queue"
281
296
  });
282
- get kind() {
283
- return this.#kind;
284
- }
297
+ /**
298
+ * Exactly equals.
299
+ */
285
300
  static equals(a, b) {
286
301
  return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
287
302
  }
303
+ static equalsEchoId(a, b) {
304
+ const a1 = a.asEchoDXN();
305
+ const b1 = b.asEchoDXN();
306
+ return !!a1 && !!b1 && a1.echoId === b1.echoId;
307
+ }
288
308
  // TODO(burdon): Rename isValid.
289
309
  static isDXNString(dxn) {
290
310
  return dxn.startsWith("dxn:");
@@ -313,7 +333,7 @@ var DXN = class _DXN {
313
333
  }
314
334
  }
315
335
  /**
316
- * @example `dxn:type:example.com/type/Contact`
336
+ * @example `dxn:type:com.example.type.person`
317
337
  */
318
338
  static fromTypename(typename) {
319
339
  return new _DXN(_DXN.kind.TYPE, [
@@ -321,7 +341,7 @@ var DXN = class _DXN {
321
341
  ]);
322
342
  }
323
343
  /**
324
- * @example `dxn:type:example.com/type/Contact:0.1.0`
344
+ * @example `dxn:type:com.example.type.person:0.1.0`
325
345
  */
326
346
  // TODO(dmaretskyi): Consider using @ as the version separator.
327
347
  static fromTypenameAndVersion(typename, version) {
@@ -335,7 +355,7 @@ var DXN = class _DXN {
335
355
  */
336
356
  static fromSpaceAndObjectId(spaceId, objectId) {
337
357
  assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
338
- assertArgument(ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
358
+ assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
339
359
  return new _DXN(_DXN.kind.ECHO, [
340
360
  spaceId,
341
361
  objectId
@@ -345,7 +365,7 @@ var DXN = class _DXN {
345
365
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
346
366
  */
347
367
  static fromLocalObjectId(id) {
348
- assertArgument(ObjectId.isValid(id), `Invalid object ID: ${id}`);
368
+ assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
349
369
  return new _DXN(_DXN.kind.ECHO, [
350
370
  LOCAL_SPACE_TAG,
351
371
  id
@@ -353,8 +373,8 @@ var DXN = class _DXN {
353
373
  }
354
374
  static fromQueue(subspaceTag, spaceId, queueId, objectId) {
355
375
  assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
356
- assertArgument(ObjectId.isValid(queueId), `Invalid queue ID: ${queueId}`);
357
- assertArgument(!objectId || ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
376
+ assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
377
+ assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
358
378
  return new _DXN(_DXN.kind.QUEUE, [
359
379
  subspaceTag,
360
380
  spaceId,
@@ -367,17 +387,17 @@ var DXN = class _DXN {
367
387
  #kind;
368
388
  #parts;
369
389
  constructor(kind, parts) {
370
- assertArgument(parts.length > 0, `Invalid DXN: ${parts}`);
371
- assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), `Invalid DXN: ${parts}`);
390
+ assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
391
+ assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
372
392
  switch (kind) {
373
393
  case _DXN.kind.TYPE:
374
394
  if (parts.length > 2) {
375
- throw new Error('Invalid "type" DXN');
395
+ throw new Error("Invalid DXN.kind.TYPE");
376
396
  }
377
397
  break;
378
398
  case _DXN.kind.ECHO:
379
399
  if (parts.length !== 2) {
380
- throw new Error('Invalid "echo" DXN');
400
+ throw new Error("Invalid DXN.kind.ECHO");
381
401
  }
382
402
  break;
383
403
  }
@@ -412,22 +432,20 @@ var DXN = class _DXN {
412
432
  }
413
433
  };
414
434
  }
435
+ get kind() {
436
+ return this.#kind;
437
+ }
415
438
  get parts() {
416
439
  return this.#parts;
417
440
  }
418
441
  // TODO(burdon): Should getters fail?
419
442
  get typename() {
420
- invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
421
- F: __dxlog_file2,
422
- L: 234,
423
- S: this,
424
- A: [
425
- "this.#kind === DXN.kind.TYPE",
426
- ""
427
- ]
428
- });
443
+ invariant2(this.#kind === _DXN.kind.TYPE);
429
444
  return this.#parts[0];
430
445
  }
446
+ equals(other) {
447
+ return _DXN.equals(this, other);
448
+ }
431
449
  hasTypenameOf(typename) {
432
450
  return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
433
451
  }
@@ -452,6 +470,7 @@ var DXN = class _DXN {
452
470
  const [spaceId, echoId] = this.#parts;
453
471
  return {
454
472
  spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
473
+ // TODO(burdon): objectId.
455
474
  echoId
456
475
  };
457
476
  }
@@ -484,40 +503,15 @@ var DXN = class _DXN {
484
503
  // src/identity-did.ts
485
504
  var import_base32_decode2 = __toESM(require_base32_decode(), 1);
486
505
  import { invariant as invariant3 } from "@dxos/invariant";
487
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/keys/src/identity-did.ts";
488
506
  var IdentityDid = Object.freeze({
489
507
  byteLength: 20,
490
508
  encode: (value) => {
491
- invariant3(value instanceof Uint8Array, "Invalid type", {
492
- F: __dxlog_file3,
493
- L: 22,
494
- S: void 0,
495
- A: [
496
- "value instanceof Uint8Array",
497
- "'Invalid type'"
498
- ]
499
- });
500
- invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
501
- F: __dxlog_file3,
502
- L: 23,
503
- S: void 0,
504
- A: [
505
- "value.length === IdentityDid.byteLength",
506
- "'Invalid length'"
507
- ]
508
- });
509
+ invariant3(value instanceof Uint8Array, "Invalid type");
510
+ invariant3(value.length === IdentityDid.byteLength, "Invalid length");
509
511
  return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
510
512
  },
511
513
  decode: (value) => {
512
- invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
513
- F: __dxlog_file3,
514
- L: 28,
515
- S: void 0,
516
- A: [
517
- "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
518
- "'Invalid multibase32 encoding'"
519
- ]
520
- });
514
+ invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding");
521
515
  return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
522
516
  },
523
517
  isValid: (value) => {
@@ -535,7 +529,6 @@ var ENCODED_LENGTH2 = 42;
535
529
  var import_base32_decode3 = __toESM(require_base32_decode(), 1);
536
530
  import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
537
531
  import { invariant as invariant4 } from "@dxos/invariant";
538
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/keys/src/public-key.ts";
539
532
  var PUBLIC_KEY_LENGTH = 32;
540
533
  var SECRET_KEY_LENGTH = 64;
541
534
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
@@ -548,15 +541,7 @@ var PublicKey = class _PublicKey {
548
541
  * @returns PublicKey
549
542
  */
550
543
  static from(source) {
551
- invariant4(source, void 0, {
552
- F: __dxlog_file4,
553
- L: 50,
554
- S: this,
555
- A: [
556
- "source",
557
- ""
558
- ]
559
- });
544
+ invariant4(source);
560
545
  if (source instanceof _PublicKey) {
561
546
  return source;
562
547
  } else if (source instanceof Buffer) {
@@ -640,15 +625,7 @@ var PublicKey = class _PublicKey {
640
625
  * @deprecated All keys should be represented as instances of PublicKey.
641
626
  */
642
627
  static bufferize(str) {
643
- invariant4(typeof str === "string", "Invalid type", {
644
- F: __dxlog_file4,
645
- L: 153,
646
- S: this,
647
- A: [
648
- "typeof str === 'string'",
649
- "'Invalid type'"
650
- ]
651
- });
628
+ invariant4(typeof str === "string", "Invalid type");
652
629
  const buffer = Buffer.from(str, "hex");
653
630
  return buffer;
654
631
  }
@@ -663,15 +640,7 @@ var PublicKey = class _PublicKey {
663
640
  } else if (key instanceof Uint8Array) {
664
641
  key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
665
642
  }
666
- invariant4(key instanceof Buffer, "Invalid type", {
667
- F: __dxlog_file4,
668
- L: 172,
669
- S: this,
670
- A: [
671
- "key instanceof Buffer",
672
- "'Invalid type'"
673
- ]
674
- });
643
+ invariant4(key instanceof Buffer, "Invalid type");
675
644
  return key.toString("hex");
676
645
  }
677
646
  /**
@@ -682,15 +651,7 @@ var PublicKey = class _PublicKey {
682
651
  return key.toHex();
683
652
  }
684
653
  static fromMultibase32(encoded) {
685
- invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
686
- F: __dxlog_file4,
687
- L: 185,
688
- S: this,
689
- A: [
690
- "encoded.startsWith('B')",
691
- "'Invalid multibase32 encoding'"
692
- ]
693
- });
654
+ invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding");
694
655
  return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
695
656
  }
696
657
  constructor(_value) {
@@ -717,7 +678,7 @@ var PublicKey = class _PublicKey {
717
678
  toMultibase32() {
718
679
  return "B" + base32Encode(this._value, "RFC4648");
719
680
  }
720
- truncate(length = void 0) {
681
+ truncate(length) {
721
682
  return truncateKey(this, length);
722
683
  }
723
684
  asBuffer() {
@@ -831,6 +792,7 @@ export {
831
792
  PublicKey,
832
793
  QueueSubspaceTags,
833
794
  SECRET_KEY_LENGTH,
795
+ SimplePRNG,
834
796
  SpaceId
835
797
  };
836
798
  //# sourceMappingURL=index.mjs.map