@dxos/keys 0.8.3-staging.0fa589b → 0.8.4-main.28f8d3d

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.
@@ -31,9 +31,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
31
31
  mod
32
32
  ));
33
33
 
34
- // node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js
34
+ // ../../../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js
35
35
  var require_base32_decode = __commonJS({
36
- "node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js"(exports, module) {
36
+ "../../../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js"(exports, module) {
37
37
  var RFC46482 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
38
38
  var RFC4648_HEX2 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
39
39
  var CROCKFORD2 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
@@ -81,16 +81,16 @@ var require_base32_decode = __commonJS({
81
81
  }
82
82
  });
83
83
 
84
- // packages/common/keys/src/dxn.ts
84
+ // src/dxn.ts
85
85
  import { Schema as Schema3 } from "effect";
86
86
  import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
87
- import { invariant as invariant2 } from "@dxos/invariant";
87
+ import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
88
88
 
89
- // packages/common/keys/src/object-id.ts
89
+ // src/object-id.ts
90
90
  import { Schema } from "effect";
91
91
  import { ulid } 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 {
@@ -98,7 +98,7 @@ var ObjectId = class extends ObjectIdSchema {
98
98
  try {
99
99
  Schema.decodeSync(ObjectId)(id);
100
100
  return true;
101
- } catch (err) {
101
+ } catch {
102
102
  return false;
103
103
  }
104
104
  }
@@ -107,10 +107,10 @@ var ObjectId = class extends ObjectIdSchema {
107
107
  }
108
108
  };
109
109
 
110
- // packages/common/keys/src/space-id.ts
110
+ // src/space-id.ts
111
111
  var import_base32_decode = __toESM(require_base32_decode(), 1);
112
112
 
113
- // node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
113
+ // ../../../node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
114
114
  function toDataView(data) {
115
115
  if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
116
116
  return new DataView(data.buffer, data.byteOffset, data.byteLength);
@@ -121,7 +121,7 @@ function toDataView(data) {
121
121
  throw new TypeError("Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray");
122
122
  }
123
123
 
124
- // node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js
124
+ // ../../../node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js
125
125
  var RFC4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
126
126
  var RFC4648_HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
127
127
  var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
@@ -169,11 +169,11 @@ function base32Encode(data, variant, options) {
169
169
  return output;
170
170
  }
171
171
 
172
- // packages/common/keys/src/space-id.ts
172
+ // src/space-id.ts
173
173
  import { Schema as Schema2 } from "effect";
174
174
  import { invariant } from "@dxos/invariant";
175
175
 
176
- // packages/common/keys/src/random-bytes.ts
176
+ // src/random-bytes.ts
177
177
  var randomBytes = (length) => {
178
178
  const webCrypto = globalThis.crypto ?? __require("node:crypto").webcrypto;
179
179
  const bytes = new Uint8Array(length);
@@ -181,116 +181,104 @@ var randomBytes = (length) => {
181
181
  return bytes;
182
182
  };
183
183
 
184
- // packages/common/keys/src/space-id.ts
185
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/space-id.ts";
184
+ // src/space-id.ts
185
+ var __dxlog_file = "/__w/dxos/dxos/packages/common/keys/src/space-id.ts";
186
186
  var MULTIBASE_PREFIX = "B";
187
187
  var ENCODED_LENGTH = 33;
188
188
  var isValid = (value) => {
189
189
  return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
190
190
  };
191
191
  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
- }
192
+ static byteLength = 20;
193
+ 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
+ });
212
+ return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
213
+ };
214
+ 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
+ });
224
+ return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
225
+ };
226
+ static isValid = isValid;
227
+ static random = () => {
228
+ return SpaceId.encode(randomBytes(SpaceId.byteLength));
229
+ };
240
230
  };
241
231
 
242
- // packages/common/keys/src/dxn.ts
243
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
232
+ // src/dxn.ts
233
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
244
234
  var LOCAL_SPACE_TAG = "@";
235
+ var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
245
236
  var QueueSubspaceTags = Object.freeze({
246
237
  DATA: "data",
247
238
  TRACE: "trace"
248
239
  });
249
240
  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
- }
241
+ // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
242
+ // TODO(dmaretskyi): Should this be a transformation into the DXN type?
243
+ static Schema = Schema3.NonEmptyString.pipe(
244
+ Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
245
+ // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
246
+ // FormatAnnotation.set(FormatEnum.DXN),
247
+ Schema3.annotations({
248
+ title: "DXN",
249
+ description: "DXN URI",
250
+ examples: [
251
+ "dxn:type:example.com/type/MyType",
252
+ "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
253
+ ]
254
+ })
255
+ );
266
256
  static hash(dxn) {
267
257
  return dxn.toString();
268
258
  }
269
- static {
259
+ /**
260
+ * Kind constants.
261
+ */
262
+ static kind = Object.freeze({
270
263
  /**
271
- * Kind constants.
264
+ * dxn:type:<type name>[:<version>]
272
265
  */
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
- }
266
+ TYPE: "type",
267
+ /**
268
+ * dxn:echo:<space id>:<echo id>
269
+ * dxn:echo:@:<echo id>
270
+ */
271
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
272
+ // TODO(burdon): Add separate Kind for space.
273
+ ECHO: "echo",
274
+ /**
275
+ * The subspace tag enables us to partition queues by usage within the context of a space.
276
+ * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
277
+ * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
278
+ * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
279
+ */
280
+ QUEUE: "queue"
281
+ });
294
282
  get kind() {
295
283
  return this.#kind;
296
284
  }
@@ -320,7 +308,7 @@ var DXN = class _DXN {
320
308
  static tryParse(dxn) {
321
309
  try {
322
310
  return _DXN.parse(dxn);
323
- } catch (error) {
311
+ } catch {
324
312
  return void 0;
325
313
  }
326
314
  }
@@ -343,42 +331,30 @@ var DXN = class _DXN {
343
331
  ]);
344
332
  }
345
333
  /**
334
+ * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
335
+ */
336
+ static fromSpaceAndObjectId(spaceId, objectId) {
337
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
338
+ assertArgument(ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
339
+ return new _DXN(_DXN.kind.ECHO, [
340
+ spaceId,
341
+ objectId
342
+ ]);
343
+ }
344
+ /**
346
345
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
347
346
  */
348
347
  static fromLocalObjectId(id) {
348
+ assertArgument(ObjectId.isValid(id), `Invalid object ID: ${id}`);
349
349
  return new _DXN(_DXN.kind.ECHO, [
350
350
  LOCAL_SPACE_TAG,
351
351
  id
352
352
  ]);
353
353
  }
354
354
  static fromQueue(subspaceTag, spaceId, queueId, objectId) {
355
- invariant2(SpaceId.isValid(spaceId), void 0, {
356
- F: __dxlog_file2,
357
- L: 150,
358
- S: this,
359
- A: [
360
- "SpaceId.isValid(spaceId)",
361
- ""
362
- ]
363
- });
364
- invariant2(ObjectId.isValid(queueId), void 0, {
365
- F: __dxlog_file2,
366
- L: 151,
367
- S: this,
368
- A: [
369
- "ObjectId.isValid(queueId)",
370
- ""
371
- ]
372
- });
373
- invariant2(!objectId || ObjectId.isValid(objectId), void 0, {
374
- F: __dxlog_file2,
375
- L: 152,
376
- S: this,
377
- A: [
378
- "!objectId || ObjectId.isValid(objectId)",
379
- ""
380
- ]
381
- });
355
+ 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}`);
382
358
  return new _DXN(_DXN.kind.QUEUE, [
383
359
  subspaceTag,
384
360
  spaceId,
@@ -391,24 +367,8 @@ var DXN = class _DXN {
391
367
  #kind;
392
368
  #parts;
393
369
  constructor(kind, parts) {
394
- invariant2(parts.length > 0, void 0, {
395
- F: __dxlog_file2,
396
- L: 161,
397
- S: this,
398
- A: [
399
- "parts.length > 0",
400
- ""
401
- ]
402
- });
403
- invariant2(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
404
- F: __dxlog_file2,
405
- L: 162,
406
- S: this,
407
- A: [
408
- "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
409
- ""
410
- ]
411
- });
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}`);
412
372
  switch (kind) {
413
373
  case _DXN.kind.TYPE:
414
374
  if (parts.length > 2) {
@@ -424,6 +384,34 @@ var DXN = class _DXN {
424
384
  this.#kind = kind;
425
385
  this.#parts = parts;
426
386
  }
387
+ toString() {
388
+ return `dxn:${this.#kind}:${this.#parts.join(":")}`;
389
+ }
390
+ toJSON() {
391
+ return this.toString();
392
+ }
393
+ /**
394
+ * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
395
+ */
396
+ [inspectCustom](depth, options, inspectFn) {
397
+ const printControlCode = (code) => {
398
+ return `\x1B[${code}m`;
399
+ };
400
+ return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
401
+ }
402
+ get [devtoolsFormatter]() {
403
+ return {
404
+ header: () => {
405
+ return [
406
+ "span",
407
+ {
408
+ style: "font-weight: bold;"
409
+ },
410
+ this.toString()
411
+ ];
412
+ }
413
+ };
414
+ }
427
415
  get parts() {
428
416
  return this.#parts;
429
417
  }
@@ -431,7 +419,7 @@ var DXN = class _DXN {
431
419
  get typename() {
432
420
  invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
433
421
  F: __dxlog_file2,
434
- L: 188,
422
+ L: 234,
435
423
  S: this,
436
424
  A: [
437
425
  "this.#kind === DXN.kind.TYPE",
@@ -452,6 +440,7 @@ var DXN = class _DXN {
452
440
  }
453
441
  const [type, version] = this.#parts;
454
442
  return {
443
+ // TODO(wittjosiah): Should be `typename` for consistency.
455
444
  type,
456
445
  version
457
446
  };
@@ -481,37 +470,21 @@ var DXN = class _DXN {
481
470
  objectId
482
471
  };
483
472
  }
484
- toString() {
485
- return `dxn:${this.#kind}:${this.#parts.join(":")}`;
486
- }
487
473
  /**
488
- * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
474
+ * Produces a new DXN with the given parts appended.
489
475
  */
490
- [inspectCustom](depth, options, inspectFn) {
491
- const printControlCode = (code) => {
492
- return `\x1B[${code}m`;
493
- };
494
- return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
495
- }
496
- get [devtoolsFormatter]() {
497
- return {
498
- header: () => {
499
- return [
500
- "span",
501
- {
502
- style: "font-weight: bold;"
503
- },
504
- this.toString()
505
- ];
506
- }
507
- };
476
+ extend(parts) {
477
+ return new _DXN(this.#kind, [
478
+ ...this.#parts,
479
+ ...parts
480
+ ]);
508
481
  }
509
482
  };
510
483
 
511
- // packages/common/keys/src/identity-did.ts
484
+ // src/identity-did.ts
512
485
  var import_base32_decode2 = __toESM(require_base32_decode(), 1);
513
486
  import { invariant as invariant3 } from "@dxos/invariant";
514
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/identity-did.ts";
487
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/common/keys/src/identity-did.ts";
515
488
  var IdentityDid = Object.freeze({
516
489
  byteLength: 20,
517
490
  encode: (value) => {
@@ -558,18 +531,17 @@ var MULTIBASE_PREFIX2 = "B";
558
531
  var DID_PREFIX = "did:halo:";
559
532
  var ENCODED_LENGTH2 = 42;
560
533
 
561
- // packages/common/keys/src/public-key.ts
534
+ // src/public-key.ts
562
535
  var import_base32_decode3 = __toESM(require_base32_decode(), 1);
563
536
  import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
564
537
  import { invariant as invariant4 } from "@dxos/invariant";
565
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
538
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/common/keys/src/public-key.ts";
566
539
  var PUBLIC_KEY_LENGTH = 32;
567
540
  var SECRET_KEY_LENGTH = 64;
568
541
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
569
542
  var PublicKey = class _PublicKey {
570
- static {
571
- this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
572
- }
543
+ _value;
544
+ static ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
573
545
  /**
574
546
  * Creates new instance of PublicKey automatically determining the input format.
575
547
  * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
@@ -578,7 +550,7 @@ var PublicKey = class _PublicKey {
578
550
  static from(source) {
579
551
  invariant4(source, void 0, {
580
552
  F: __dxlog_file4,
581
- L: 49,
553
+ L: 50,
582
554
  S: this,
583
555
  A: [
584
556
  "source",
@@ -670,7 +642,7 @@ var PublicKey = class _PublicKey {
670
642
  static bufferize(str) {
671
643
  invariant4(typeof str === "string", "Invalid type", {
672
644
  F: __dxlog_file4,
673
- L: 152,
645
+ L: 153,
674
646
  S: this,
675
647
  A: [
676
648
  "typeof str === 'string'",
@@ -693,7 +665,7 @@ var PublicKey = class _PublicKey {
693
665
  }
694
666
  invariant4(key instanceof Buffer, "Invalid type", {
695
667
  F: __dxlog_file4,
696
- L: 171,
668
+ L: 172,
697
669
  S: this,
698
670
  A: [
699
671
  "key instanceof Buffer",
@@ -712,7 +684,7 @@ var PublicKey = class _PublicKey {
712
684
  static fromMultibase32(encoded) {
713
685
  invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
714
686
  F: __dxlog_file4,
715
- L: 184,
687
+ L: 185,
716
688
  S: this,
717
689
  A: [
718
690
  "encoded.startsWith('B')",
@@ -851,6 +823,7 @@ var PublicKey = class _PublicKey {
851
823
  };
852
824
  export {
853
825
  DXN,
826
+ DXN_ECHO_REGEXP,
854
827
  IdentityDid,
855
828
  LOCAL_SPACE_TAG,
856
829
  ObjectId,