@dxos/keys 0.8.2 → 0.8.3-main.7f5a14c

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.
@@ -81,7 +81,33 @@ var require_base32_decode = __commonJS({
81
81
  }
82
82
  });
83
83
 
84
- // packages/common/keys/src/identity-did.ts
84
+ // packages/common/keys/src/dxn.ts
85
+ import { Schema as Schema3 } from "effect";
86
+ import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
87
+ import { invariant as invariant2 } from "@dxos/invariant";
88
+
89
+ // packages/common/keys/src/object-id.ts
90
+ import { Schema } from "effect";
91
+ import { ulid } from "ulidx";
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",
94
+ pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
95
+ });
96
+ var ObjectId = class extends ObjectIdSchema {
97
+ static isValid(id) {
98
+ try {
99
+ Schema.decodeSync(ObjectId)(id);
100
+ return true;
101
+ } catch (err) {
102
+ return false;
103
+ }
104
+ }
105
+ static random() {
106
+ return ulid();
107
+ }
108
+ };
109
+
110
+ // packages/common/keys/src/space-id.ts
85
111
  var import_base32_decode = __toESM(require_base32_decode(), 1);
86
112
 
87
113
  // node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
@@ -143,7 +169,8 @@ function base32Encode(data, variant, options) {
143
169
  return output;
144
170
  }
145
171
 
146
- // packages/common/keys/src/identity-did.ts
172
+ // packages/common/keys/src/space-id.ts
173
+ import { Schema as Schema2 } from "effect";
147
174
  import { invariant } from "@dxos/invariant";
148
175
 
149
176
  // packages/common/keys/src/random-bytes.ts
@@ -154,683 +181,672 @@ var randomBytes = (length) => {
154
181
  return bytes;
155
182
  };
156
183
 
157
- // packages/common/keys/src/identity-did.ts
158
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/identity-did.ts";
159
- var IdentityDid = Object.freeze({
160
- byteLength: 20,
161
- encode: (value) => {
162
- invariant(value instanceof Uint8Array, "Invalid type", {
163
- F: __dxlog_file,
164
- L: 22,
165
- S: void 0,
166
- A: [
167
- "value instanceof Uint8Array",
168
- "'Invalid type'"
169
- ]
170
- });
171
- invariant(value.length === IdentityDid.byteLength, "Invalid length", {
172
- F: __dxlog_file,
173
- L: 23,
174
- S: void 0,
175
- A: [
176
- "value.length === IdentityDid.byteLength",
177
- "'Invalid length'"
178
- ]
179
- });
180
- return DID_PREFIX + MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
181
- },
182
- decode: (value) => {
183
- invariant(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
184
- F: __dxlog_file,
185
- L: 28,
186
- S: void 0,
187
- A: [
188
- "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
189
- "'Invalid multibase32 encoding'"
190
- ]
191
- });
192
- return new Uint8Array((0, import_base32_decode.default)(value.slice(10), "RFC4648"));
193
- },
194
- isValid: (value) => {
195
- return typeof value === "string" && value.startsWith(DID_PREFIX + MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
196
- },
197
- random: () => {
198
- return IdentityDid.encode(randomBytes(IdentityDid.byteLength));
199
- }
200
- });
184
+ // packages/common/keys/src/space-id.ts
185
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/space-id.ts";
201
186
  var MULTIBASE_PREFIX = "B";
202
- var DID_PREFIX = "did:halo:";
203
- var ENCODED_LENGTH = 42;
187
+ var ENCODED_LENGTH = 33;
188
+ var isValid = (value) => {
189
+ return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
190
+ };
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
+ }
240
+ };
204
241
 
205
- // packages/common/keys/src/public-key.ts
206
- var import_base32_decode2 = __toESM(require_base32_decode(), 1);
207
- import { devtoolsFormatter, equalsSymbol, inspectCustom, truncateKey } from "@dxos/debug";
208
- import { invariant as invariant2 } from "@dxos/invariant";
209
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
210
- var PUBLIC_KEY_LENGTH = 32;
211
- var SECRET_KEY_LENGTH = 64;
212
- var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
213
- var PublicKey = class _PublicKey {
242
+ // packages/common/keys/src/dxn.ts
243
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
244
+ var LOCAL_SPACE_TAG = "@";
245
+ var QueueSubspaceTags = Object.freeze({
246
+ DATA: "data",
247
+ TRACE: "trace"
248
+ });
249
+ var DXN = class _DXN {
214
250
  static {
215
- this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
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
+ );
216
265
  }
217
- /**
218
- * Creates new instance of PublicKey automatically determining the input format.
219
- * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
220
- * @returns PublicKey
221
- */
222
- static from(source) {
223
- invariant2(source, void 0, {
224
- F: __dxlog_file2,
225
- L: 49,
226
- S: this,
227
- A: [
228
- "source",
229
- ""
230
- ]
266
+ static hash(dxn) {
267
+ return dxn.toString();
268
+ }
269
+ static {
270
+ /**
271
+ * Kind constants.
272
+ */
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"
231
292
  });
232
- if (source instanceof _PublicKey) {
233
- return source;
234
- } else if (source instanceof Buffer) {
235
- return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
236
- } else if (source instanceof Uint8Array) {
237
- return new _PublicKey(source);
238
- } else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {
239
- return new _PublicKey(new Uint8Array(source));
240
- } else if (typeof source === "string") {
241
- return _PublicKey.fromHex(source);
242
- } else if (source.asUint8Array) {
243
- return new _PublicKey(source.asUint8Array());
244
- } else {
245
- throw new TypeError(`Unable to create PublicKey from ${source}`);
246
- }
247
293
  }
248
- /**
249
- * Same as `PublicKey.from` but does not throw and instead returns a `{ key: PublicKey }` or `{ error: Error }`
250
- * @param source Same PublicKeyLike argument as for `PublicKey.from`
251
- * @returns PublicKey
252
- */
253
- static safeFrom(source) {
254
- if (!source) {
255
- return void 0;
294
+ get kind() {
295
+ return this.#kind;
296
+ }
297
+ static equals(a, b) {
298
+ return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
299
+ }
300
+ // TODO(burdon): Rename isValid.
301
+ static isDXNString(dxn) {
302
+ return dxn.startsWith("dxn:");
303
+ }
304
+ static parse(dxn) {
305
+ if (typeof dxn !== "string") {
306
+ throw new Error(`Invalid DXN: ${dxn}`);
307
+ }
308
+ const [prefix, kind, ...parts] = dxn.split(":");
309
+ if (!(prefix === "dxn")) {
310
+ throw new Error(`Invalid DXN: ${dxn}`);
311
+ }
312
+ if (!(typeof kind === "string" && kind.length > 0)) {
313
+ throw new Error(`Invalid DXN: ${dxn}`);
256
314
  }
315
+ if (!(parts.length > 0)) {
316
+ throw new Error(`Invalid DXN: ${dxn}`);
317
+ }
318
+ return new _DXN(kind, parts);
319
+ }
320
+ static tryParse(dxn) {
257
321
  try {
258
- const key = _PublicKey.from(source);
259
- return key;
260
- } catch (err) {
322
+ return _DXN.parse(dxn);
323
+ } catch (error) {
261
324
  return void 0;
262
325
  }
263
326
  }
264
327
  /**
265
- * Creates new instance of PublicKey from hex string.
328
+ * @example `dxn:type:example.com/type/Contact`
266
329
  */
267
- static fromHex(hex) {
268
- if (hex.startsWith("0x")) {
269
- hex = hex.slice(2);
270
- }
271
- const buf = Buffer.from(hex, "hex");
272
- return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
330
+ static fromTypename(typename) {
331
+ return new _DXN(_DXN.kind.TYPE, [
332
+ typename
333
+ ]);
273
334
  }
274
335
  /**
275
- * Creates a new key.
336
+ * @example `dxn:type:example.com/type/Contact:0.1.0`
276
337
  */
277
- static random() {
278
- return _PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
279
- }
280
- static randomOfLength(length) {
281
- return _PublicKey.from(randomBytes(length));
282
- }
283
- static *randomSequence() {
284
- for (let i = 0; i < 1e4; i++) {
285
- yield _PublicKey.random();
286
- }
287
- throw new Error("Too many keys requested");
288
- }
289
- /**
290
- * Tests if provided values is an instance of PublicKey.
291
- */
292
- static isPublicKey(value) {
293
- return value instanceof _PublicKey;
294
- }
295
- /**
296
- * Asserts that provided values is an instance of PublicKey.
297
- */
298
- static assertValidPublicKey(value) {
299
- if (!this.isPublicKey(value)) {
300
- throw new TypeError("Invalid PublicKey");
301
- }
338
+ // TODO(dmaretskyi): Consider using @ as the version separator.
339
+ static fromTypenameAndVersion(typename, version) {
340
+ return new _DXN(_DXN.kind.TYPE, [
341
+ typename,
342
+ version
343
+ ]);
302
344
  }
303
345
  /**
304
- * Tests two keys for equality.
346
+ * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
305
347
  */
306
- static equals(left, right) {
307
- return _PublicKey.from(left).equals(right);
348
+ static fromLocalObjectId(id) {
349
+ return new _DXN(_DXN.kind.ECHO, [
350
+ LOCAL_SPACE_TAG,
351
+ id
352
+ ]);
308
353
  }
309
- /**
310
- * @param str string representation of key.
311
- * @return Key buffer.
312
- * @deprecated All keys should be represented as instances of PublicKey.
313
- */
314
- static bufferize(str) {
315
- invariant2(typeof str === "string", "Invalid type", {
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, {
316
374
  F: __dxlog_file2,
317
375
  L: 152,
318
376
  S: this,
319
377
  A: [
320
- "typeof str === 'string'",
321
- "'Invalid type'"
378
+ "!objectId || ObjectId.isValid(objectId)",
379
+ ""
322
380
  ]
323
381
  });
324
- const buffer = Buffer.from(str, "hex");
325
- return buffer;
382
+ return new _DXN(_DXN.kind.QUEUE, [
383
+ subspaceTag,
384
+ spaceId,
385
+ queueId,
386
+ ...objectId ? [
387
+ objectId
388
+ ] : []
389
+ ]);
326
390
  }
327
- /**
328
- * @param key key like data structure (but not PublicKey which should use toString).
329
- * @return Hex string representation of key.
330
- * @deprecated All keys should be represented as instances of PublicKey.
331
- */
332
- static stringify(key) {
333
- if (key instanceof _PublicKey) {
334
- key = key.asBuffer();
335
- } else if (key instanceof Uint8Array) {
336
- key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
337
- }
338
- invariant2(key instanceof Buffer, "Invalid type", {
391
+ #kind;
392
+ #parts;
393
+ constructor(kind, parts) {
394
+ invariant2(parts.length > 0, void 0, {
339
395
  F: __dxlog_file2,
340
- L: 171,
396
+ L: 161,
341
397
  S: this,
342
398
  A: [
343
- "key instanceof Buffer",
344
- "'Invalid type'"
399
+ "parts.length > 0",
400
+ ""
345
401
  ]
346
402
  });
347
- return key.toString("hex");
348
- }
349
- /**
350
- * To be used with ComplexMap and ComplexSet.
351
- * Returns a scalar representation for this key.
352
- */
353
- static hash(key) {
354
- return key.toHex();
355
- }
356
- static fromMultibase32(encoded) {
357
- invariant2(encoded.startsWith("B"), "Invalid multibase32 encoding", {
403
+ invariant2(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
358
404
  F: __dxlog_file2,
359
- L: 184,
405
+ L: 162,
360
406
  S: this,
361
407
  A: [
362
- "encoded.startsWith('B')",
363
- "'Invalid multibase32 encoding'"
408
+ "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
409
+ ""
364
410
  ]
365
411
  });
366
- return new _PublicKey(new Uint8Array((0, import_base32_decode2.default)(encoded.slice(1), "RFC4648")));
367
- }
368
- constructor(_value) {
369
- this._value = _value;
370
- if (!(_value instanceof Uint8Array)) {
371
- throw new TypeError(`Expected Uint8Array, got: ${_value}`);
412
+ switch (kind) {
413
+ case _DXN.kind.TYPE:
414
+ if (parts.length > 2) {
415
+ throw new Error('Invalid "type" DXN');
416
+ }
417
+ break;
418
+ case _DXN.kind.ECHO:
419
+ if (parts.length !== 2) {
420
+ throw new Error('Invalid "echo" DXN');
421
+ }
422
+ break;
372
423
  }
424
+ this.#kind = kind;
425
+ this.#parts = parts;
373
426
  }
374
- toString() {
375
- return this.toHex();
376
- }
377
- toJSON() {
378
- return this.toHex();
379
- }
380
- toJSONL() {
381
- return this.truncate();
427
+ get parts() {
428
+ return this.#parts;
382
429
  }
383
- get length() {
384
- return this._value.length;
430
+ // TODO(burdon): Should getters fail?
431
+ get typename() {
432
+ invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
433
+ F: __dxlog_file2,
434
+ L: 188,
435
+ S: this,
436
+ A: [
437
+ "this.#kind === DXN.kind.TYPE",
438
+ ""
439
+ ]
440
+ });
441
+ return this.#parts[0];
385
442
  }
386
- toHex() {
387
- return this.asBuffer().toString("hex");
443
+ hasTypenameOf(typename) {
444
+ return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
388
445
  }
389
- toMultibase32() {
390
- return "B" + base32Encode(this._value, "RFC4648");
446
+ isLocalObjectId() {
447
+ return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
391
448
  }
392
- truncate(length = void 0) {
393
- return truncateKey(this, length);
449
+ asTypeDXN() {
450
+ if (this.kind !== _DXN.kind.TYPE) {
451
+ return void 0;
452
+ }
453
+ const [type, version] = this.#parts;
454
+ return {
455
+ type,
456
+ version
457
+ };
394
458
  }
395
- asBuffer() {
396
- return Buffer.from(this._value.buffer, this._value.byteOffset, this._value.byteLength);
459
+ asEchoDXN() {
460
+ if (this.kind !== _DXN.kind.ECHO) {
461
+ return void 0;
462
+ }
463
+ const [spaceId, echoId] = this.#parts;
464
+ return {
465
+ spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
466
+ echoId
467
+ };
397
468
  }
398
- asUint8Array() {
399
- return this._value;
469
+ asQueueDXN() {
470
+ if (this.kind !== _DXN.kind.QUEUE) {
471
+ return void 0;
472
+ }
473
+ const [subspaceTag, spaceId, queueId, objectId] = this.#parts;
474
+ if (typeof queueId !== "string") {
475
+ return void 0;
476
+ }
477
+ return {
478
+ subspaceTag,
479
+ spaceId,
480
+ queueId,
481
+ objectId
482
+ };
400
483
  }
401
- getInsecureHash(modulo) {
402
- return Math.abs(this._value.reduce((acc, val) => acc ^ val | 0, 0)) % modulo;
484
+ toString() {
485
+ return `dxn:${this.#kind}:${this.#parts.join(":")}`;
403
486
  }
404
487
  /**
405
488
  * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
406
489
  */
407
490
  [inspectCustom](depth, options, inspectFn) {
408
- if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
409
- return `<PublicKey ${this.truncate()}>`;
410
- }
411
491
  const printControlCode = (code) => {
412
492
  return `\x1B[${code}m`;
413
493
  };
414
- const colors = [
415
- "red",
416
- "green",
417
- "yellow",
418
- "blue",
419
- "magenta",
420
- "cyan",
421
- "redBright",
422
- "greenBright",
423
- "yellowBright",
424
- "blueBright",
425
- "magentaBright",
426
- "cyanBright",
427
- "whiteBright"
428
- ];
429
- const color = colors[this.getInsecureHash(colors.length)];
430
- return `PublicKey(${printControlCode(inspectFn.colors[color][0])}${this.truncate()}${printControlCode(inspectFn.colors.reset[0])})`;
494
+ return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
431
495
  }
432
496
  get [devtoolsFormatter]() {
433
497
  return {
434
498
  header: () => {
435
- const colors = [
436
- "darkred",
437
- "green",
438
- "orange",
439
- "blue",
440
- "darkmagenta",
441
- "darkcyan",
442
- "red",
443
- "green",
444
- "orange",
445
- "blue",
446
- "magenta",
447
- "darkcyan",
448
- "black"
449
- ];
450
- const color = colors[this.getInsecureHash(colors.length)];
451
499
  return [
452
500
  "span",
453
- {},
454
- [
455
- "span",
456
- {},
457
- "PublicKey("
458
- ],
459
- [
460
- "span",
461
- {
462
- style: `color: ${color};`
463
- },
464
- this.truncate()
465
- ],
466
- [
467
- "span",
468
- {},
469
- ")"
470
- ]
501
+ {
502
+ style: "font-weight: bold;"
503
+ },
504
+ this.toString()
471
505
  ];
472
506
  }
473
507
  };
474
508
  }
475
- /**
476
- * Test this key for equality with some other key.
477
- */
478
- equals(other) {
479
- const otherConverted = _PublicKey.from(other);
480
- if (this._value.length !== otherConverted._value.length) {
481
- return false;
482
- }
483
- let equal = true;
484
- for (let i = 0; i < this._value.length; i++) {
485
- equal &&= this._value[i] === otherConverted._value[i];
486
- }
487
- return equal;
488
- }
489
- [equalsSymbol](other) {
490
- if (!_PublicKey.isPublicKey(other)) {
491
- return false;
492
- }
493
- return this.equals(other);
494
- }
495
509
  };
496
510
 
497
- // packages/common/keys/src/space-id.ts
498
- var import_base32_decode3 = __toESM(require_base32_decode(), 1);
499
- import { Schema } from "effect";
511
+ // packages/common/keys/src/identity-did.ts
512
+ var import_base32_decode2 = __toESM(require_base32_decode(), 1);
500
513
  import { invariant as invariant3 } from "@dxos/invariant";
501
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/space-id.ts";
502
- var MULTIBASE_PREFIX2 = "B";
503
- var ENCODED_LENGTH2 = 33;
504
- var isValid = (value) => {
505
- return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX2) && value.length === ENCODED_LENGTH2;
506
- };
507
- var SpaceId = class extends Schema.String.pipe(Schema.filter(isValid)) {
508
- static {
509
- this.byteLength = 20;
510
- }
511
- static {
512
- this.encode = (value) => {
513
- invariant3(value instanceof Uint8Array, "Invalid type", {
514
- F: __dxlog_file3,
515
- L: 43,
516
- S: this,
517
- A: [
518
- "value instanceof Uint8Array",
519
- "'Invalid type'"
520
- ]
521
- });
522
- invariant3(value.length === SpaceId.byteLength, "Invalid length", {
523
- F: __dxlog_file3,
524
- L: 44,
525
- S: this,
526
- A: [
527
- "value.length === SpaceId.byteLength",
528
- "'Invalid length'"
529
- ]
530
- });
531
- return MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
532
- };
533
- }
534
- static {
535
- this.decode = (value) => {
536
- invariant3(value.startsWith(MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
537
- F: __dxlog_file3,
538
- L: 49,
539
- S: this,
540
- A: [
541
- "value.startsWith(MULTIBASE_PREFIX)",
542
- "'Invalid multibase32 encoding'"
543
- ]
544
- });
545
- return new Uint8Array((0, import_base32_decode3.default)(value.slice(1), "RFC4648"));
546
- };
547
- }
548
- static {
549
- this.isValid = isValid;
550
- }
551
- static {
552
- this.random = () => {
553
- return SpaceId.encode(randomBytes(SpaceId.byteLength));
554
- };
514
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/identity-did.ts";
515
+ var IdentityDid = Object.freeze({
516
+ byteLength: 20,
517
+ encode: (value) => {
518
+ invariant3(value instanceof Uint8Array, "Invalid type", {
519
+ F: __dxlog_file3,
520
+ L: 22,
521
+ S: void 0,
522
+ A: [
523
+ "value instanceof Uint8Array",
524
+ "'Invalid type'"
525
+ ]
526
+ });
527
+ invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
528
+ F: __dxlog_file3,
529
+ L: 23,
530
+ S: void 0,
531
+ A: [
532
+ "value.length === IdentityDid.byteLength",
533
+ "'Invalid length'"
534
+ ]
535
+ });
536
+ return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
537
+ },
538
+ decode: (value) => {
539
+ invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
540
+ F: __dxlog_file3,
541
+ L: 28,
542
+ S: void 0,
543
+ A: [
544
+ "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
545
+ "'Invalid multibase32 encoding'"
546
+ ]
547
+ });
548
+ return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
549
+ },
550
+ isValid: (value) => {
551
+ return typeof value === "string" && value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2) && value.length === ENCODED_LENGTH2;
552
+ },
553
+ random: () => {
554
+ return IdentityDid.encode(randomBytes(IdentityDid.byteLength));
555
555
  }
556
- };
557
-
558
- // packages/common/keys/src/dxn.ts
559
- import { Schema as Schema3 } from "effect";
560
- import { inspectCustom as inspectCustom2 } from "@dxos/debug";
561
- import { invariant as invariant4 } from "@dxos/invariant";
562
-
563
- // packages/common/keys/src/object-id.ts
564
- import { Schema as Schema2 } from "effect";
565
- import { ulid } from "ulidx";
566
- var ObjectIdSchema = Schema2.String.pipe(Schema2.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
567
- description: "a Universally Unique Lexicographically Sortable Identifier",
568
- pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
569
556
  });
570
- var ObjectId = class extends ObjectIdSchema {
571
- static isValid(id) {
572
- try {
573
- Schema2.decodeSync(ObjectId)(id);
574
- return true;
575
- } catch (err) {
576
- return false;
577
- }
578
- }
579
- static random() {
580
- return ulid();
581
- }
582
- };
557
+ var MULTIBASE_PREFIX2 = "B";
558
+ var DID_PREFIX = "did:halo:";
559
+ var ENCODED_LENGTH2 = 42;
583
560
 
584
- // packages/common/keys/src/dxn.ts
585
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
586
- var LOCAL_SPACE_TAG = "@";
587
- var QueueSubspaceTags = Object.freeze({
588
- DATA: "data",
589
- TRACE: "trace"
590
- });
591
- var DXN = class _DXN {
561
+ // packages/common/keys/src/public-key.ts
562
+ var import_base32_decode3 = __toESM(require_base32_decode(), 1);
563
+ import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
564
+ import { invariant as invariant4 } from "@dxos/invariant";
565
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
566
+ var PUBLIC_KEY_LENGTH = 32;
567
+ var SECRET_KEY_LENGTH = 64;
568
+ var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
569
+ var PublicKey = class _PublicKey {
592
570
  static {
593
- // TODO(dmaretskyi): Should this be a transformation into the DXN type?
594
- this.Schema = Schema3.NonEmptyString.pipe(
595
- Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
596
- // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
597
- // FormatAnnotation.set(FormatEnum.DXN),
598
- Schema3.annotations({
599
- title: "DXN",
600
- description: "DXN URI",
601
- examples: [
602
- "dxn:type:example.com/type/MyType",
603
- "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
604
- ]
605
- })
606
- );
571
+ this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
607
572
  }
608
- static {
609
- /**
610
- * Kind constants.
611
- */
612
- this.kind = Object.freeze({
613
- /**
614
- * dxn:type:<type name>[:<version>]
615
- */
616
- TYPE: "type",
617
- /**
618
- * dxn:echo:<space id>:<echo id>
619
- * dxn:echo:@:<echo id>
620
- */
621
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
622
- // TODO(burdon): Add separate Kind for space.
623
- ECHO: "echo",
624
- /**
625
- * The subspace tag enables us to partition queues by usage within the context of a space.
626
- * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
627
- * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
628
- * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
629
- */
630
- QUEUE: "queue"
573
+ /**
574
+ * Creates new instance of PublicKey automatically determining the input format.
575
+ * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
576
+ * @returns PublicKey
577
+ */
578
+ static from(source) {
579
+ invariant4(source, void 0, {
580
+ F: __dxlog_file4,
581
+ L: 49,
582
+ S: this,
583
+ A: [
584
+ "source",
585
+ ""
586
+ ]
631
587
  });
632
- }
633
- get kind() {
634
- return this.#kind;
635
- }
636
- static equals(a, b) {
637
- return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
638
- }
639
- // TODO(burdon): Rename isValid.
640
- static isDXNString(dxn) {
641
- return dxn.startsWith("dxn:");
642
- }
643
- static parse(dxn) {
644
- if (typeof dxn !== "string") {
645
- throw new Error(`Invalid DXN: ${dxn}`);
588
+ if (source instanceof _PublicKey) {
589
+ return source;
590
+ } else if (source instanceof Buffer) {
591
+ return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
592
+ } else if (source instanceof Uint8Array) {
593
+ return new _PublicKey(source);
594
+ } else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {
595
+ return new _PublicKey(new Uint8Array(source));
596
+ } else if (typeof source === "string") {
597
+ return _PublicKey.fromHex(source);
598
+ } else if (source.asUint8Array) {
599
+ return new _PublicKey(source.asUint8Array());
600
+ } else {
601
+ throw new TypeError(`Unable to create PublicKey from ${source}`);
646
602
  }
647
- const [prefix, kind, ...parts] = dxn.split(":");
648
- if (!(prefix === "dxn")) {
649
- throw new Error(`Invalid DXN: ${dxn}`);
603
+ }
604
+ /**
605
+ * Same as `PublicKey.from` but does not throw and instead returns a `{ key: PublicKey }` or `{ error: Error }`
606
+ * @param source Same PublicKeyLike argument as for `PublicKey.from`
607
+ * @returns PublicKey
608
+ */
609
+ static safeFrom(source) {
610
+ if (!source) {
611
+ return void 0;
650
612
  }
651
- if (!(typeof kind === "string" && kind.length > 0)) {
652
- throw new Error(`Invalid DXN: ${dxn}`);
613
+ try {
614
+ const key = _PublicKey.from(source);
615
+ return key;
616
+ } catch (err) {
617
+ return void 0;
653
618
  }
654
- if (!(parts.length > 0)) {
655
- throw new Error(`Invalid DXN: ${dxn}`);
619
+ }
620
+ /**
621
+ * Creates new instance of PublicKey from hex string.
622
+ */
623
+ static fromHex(hex) {
624
+ if (hex.startsWith("0x")) {
625
+ hex = hex.slice(2);
656
626
  }
657
- return new _DXN(kind, parts);
627
+ const buf = Buffer.from(hex, "hex");
628
+ return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
658
629
  }
659
- static tryParse(dxn) {
660
- try {
661
- return _DXN.parse(dxn);
662
- } catch (error) {
663
- return void 0;
630
+ /**
631
+ * Creates a new key.
632
+ */
633
+ static random() {
634
+ return _PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
635
+ }
636
+ static randomOfLength(length) {
637
+ return _PublicKey.from(randomBytes(length));
638
+ }
639
+ static *randomSequence() {
640
+ for (let i = 0; i < 1e4; i++) {
641
+ yield _PublicKey.random();
664
642
  }
643
+ throw new Error("Too many keys requested");
665
644
  }
666
645
  /**
667
- * @example `dxn:type:example.com/type/Contact`
646
+ * Tests if provided values is an instance of PublicKey.
668
647
  */
669
- static fromTypename(typename) {
670
- return new _DXN(_DXN.kind.TYPE, [
671
- typename
672
- ]);
648
+ static isPublicKey(value) {
649
+ return value instanceof _PublicKey;
673
650
  }
674
651
  /**
675
- * @example `dxn:type:example.com/type/Contact:0.1.0`
652
+ * Asserts that provided values is an instance of PublicKey.
676
653
  */
677
- // TODO(dmaretskyi): Consider using @ as the version separator.
678
- static fromTypenameAndVersion(typename, version) {
679
- return new _DXN(_DXN.kind.TYPE, [
680
- typename,
681
- version
682
- ]);
654
+ static assertValidPublicKey(value) {
655
+ if (!this.isPublicKey(value)) {
656
+ throw new TypeError("Invalid PublicKey");
657
+ }
683
658
  }
684
659
  /**
685
- * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
660
+ * Tests two keys for equality.
686
661
  */
687
- static fromLocalObjectId(id) {
688
- return new _DXN(_DXN.kind.ECHO, [
689
- LOCAL_SPACE_TAG,
690
- id
691
- ]);
662
+ static equals(left, right) {
663
+ return _PublicKey.from(left).equals(right);
692
664
  }
693
- static fromQueue(subspaceTag, spaceId, queueId, objectId) {
694
- invariant4(SpaceId.isValid(spaceId), void 0, {
695
- F: __dxlog_file4,
696
- L: 146,
697
- S: this,
698
- A: [
699
- "SpaceId.isValid(spaceId)",
700
- ""
701
- ]
702
- });
703
- invariant4(ObjectId.isValid(queueId), void 0, {
704
- F: __dxlog_file4,
705
- L: 147,
706
- S: this,
707
- A: [
708
- "ObjectId.isValid(queueId)",
709
- ""
710
- ]
711
- });
712
- invariant4(!objectId || ObjectId.isValid(objectId), void 0, {
665
+ /**
666
+ * @param str string representation of key.
667
+ * @return Key buffer.
668
+ * @deprecated All keys should be represented as instances of PublicKey.
669
+ */
670
+ static bufferize(str) {
671
+ invariant4(typeof str === "string", "Invalid type", {
713
672
  F: __dxlog_file4,
714
- L: 148,
673
+ L: 152,
715
674
  S: this,
716
675
  A: [
717
- "!objectId || ObjectId.isValid(objectId)",
718
- ""
676
+ "typeof str === 'string'",
677
+ "'Invalid type'"
719
678
  ]
720
679
  });
721
- return new _DXN(_DXN.kind.QUEUE, [
722
- subspaceTag,
723
- spaceId,
724
- queueId,
725
- ...objectId ? [
726
- objectId
727
- ] : []
728
- ]);
680
+ const buffer = Buffer.from(str, "hex");
681
+ return buffer;
729
682
  }
730
- #kind;
731
- #parts;
732
- constructor(kind, parts) {
733
- invariant4(parts.length > 0, void 0, {
734
- F: __dxlog_file4,
735
- L: 157,
736
- S: this,
737
- A: [
738
- "parts.length > 0",
739
- ""
740
- ]
741
- });
742
- invariant4(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
683
+ /**
684
+ * @param key key like data structure (but not PublicKey which should use toString).
685
+ * @return Hex string representation of key.
686
+ * @deprecated All keys should be represented as instances of PublicKey.
687
+ */
688
+ static stringify(key) {
689
+ if (key instanceof _PublicKey) {
690
+ key = key.asBuffer();
691
+ } else if (key instanceof Uint8Array) {
692
+ key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
693
+ }
694
+ invariant4(key instanceof Buffer, "Invalid type", {
743
695
  F: __dxlog_file4,
744
- L: 158,
696
+ L: 171,
745
697
  S: this,
746
698
  A: [
747
- "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
748
- ""
699
+ "key instanceof Buffer",
700
+ "'Invalid type'"
749
701
  ]
750
702
  });
751
- switch (kind) {
752
- case _DXN.kind.TYPE:
753
- if (parts.length > 2) {
754
- throw new Error('Invalid "type" DXN');
755
- }
756
- break;
757
- case _DXN.kind.ECHO:
758
- if (parts.length !== 2) {
759
- throw new Error('Invalid "echo" DXN');
760
- }
761
- break;
762
- }
763
- this.#kind = kind;
764
- this.#parts = parts;
703
+ return key.toString("hex");
765
704
  }
766
- get parts() {
767
- return this.#parts;
705
+ /**
706
+ * To be used with ComplexMap and ComplexSet.
707
+ * Returns a scalar representation for this key.
708
+ */
709
+ static hash(key) {
710
+ return key.toHex();
768
711
  }
769
- // TODO(burdon): Should getters fail?
770
- get typename() {
771
- invariant4(this.#kind === _DXN.kind.TYPE, void 0, {
712
+ static fromMultibase32(encoded) {
713
+ invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
772
714
  F: __dxlog_file4,
773
715
  L: 184,
774
716
  S: this,
775
717
  A: [
776
- "this.#kind === DXN.kind.TYPE",
777
- ""
718
+ "encoded.startsWith('B')",
719
+ "'Invalid multibase32 encoding'"
778
720
  ]
779
721
  });
780
- return this.#parts[0];
722
+ return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
781
723
  }
782
- hasTypenameOf(typename) {
783
- return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
724
+ constructor(_value) {
725
+ this._value = _value;
726
+ if (!(_value instanceof Uint8Array)) {
727
+ throw new TypeError(`Expected Uint8Array, got: ${_value}`);
728
+ }
784
729
  }
785
- isLocalObjectId() {
786
- return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
730
+ toString() {
731
+ return this.toHex();
787
732
  }
788
- asTypeDXN() {
789
- if (this.kind !== _DXN.kind.TYPE) {
790
- return void 0;
791
- }
792
- const [type, version] = this.#parts;
793
- return {
794
- type,
795
- version
796
- };
733
+ toJSON() {
734
+ return this.toHex();
797
735
  }
798
- asEchoDXN() {
799
- if (this.kind !== _DXN.kind.ECHO) {
800
- return void 0;
801
- }
802
- const [spaceId, echoId] = this.#parts;
803
- return {
804
- spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
805
- echoId
806
- };
736
+ toJSONL() {
737
+ return this.truncate();
807
738
  }
808
- asQueueDXN() {
809
- if (this.kind !== _DXN.kind.QUEUE) {
810
- return void 0;
811
- }
812
- const [subspaceTag, spaceId, queueId, objectId] = this.#parts;
813
- if (typeof queueId !== "string") {
814
- return void 0;
815
- }
816
- return {
817
- subspaceTag,
818
- spaceId,
819
- queueId,
820
- objectId
821
- };
739
+ get length() {
740
+ return this._value.length;
822
741
  }
823
- toString() {
824
- return `dxn:${this.#kind}:${this.#parts.join(":")}`;
742
+ toHex() {
743
+ return this.asBuffer().toString("hex");
744
+ }
745
+ toMultibase32() {
746
+ return "B" + base32Encode(this._value, "RFC4648");
747
+ }
748
+ truncate(length = void 0) {
749
+ return truncateKey(this, length);
750
+ }
751
+ asBuffer() {
752
+ return Buffer.from(this._value.buffer, this._value.byteOffset, this._value.byteLength);
753
+ }
754
+ asUint8Array() {
755
+ return this._value;
756
+ }
757
+ getInsecureHash(modulo) {
758
+ return Math.abs(this._value.reduce((acc, val) => acc ^ val | 0, 0)) % modulo;
825
759
  }
826
760
  /**
827
761
  * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
828
762
  */
829
763
  [inspectCustom2](depth, options, inspectFn) {
764
+ if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
765
+ return `<PublicKey ${this.truncate()}>`;
766
+ }
830
767
  const printControlCode = (code) => {
831
768
  return `\x1B[${code}m`;
832
769
  };
833
- return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
770
+ const colors = [
771
+ "red",
772
+ "green",
773
+ "yellow",
774
+ "blue",
775
+ "magenta",
776
+ "cyan",
777
+ "redBright",
778
+ "greenBright",
779
+ "yellowBright",
780
+ "blueBright",
781
+ "magentaBright",
782
+ "cyanBright",
783
+ "whiteBright"
784
+ ];
785
+ const color = colors[this.getInsecureHash(colors.length)];
786
+ return `PublicKey(${printControlCode(inspectFn.colors[color][0])}${this.truncate()}${printControlCode(inspectFn.colors.reset[0])})`;
787
+ }
788
+ get [devtoolsFormatter2]() {
789
+ return {
790
+ header: () => {
791
+ const colors = [
792
+ "darkred",
793
+ "green",
794
+ "orange",
795
+ "blue",
796
+ "darkmagenta",
797
+ "darkcyan",
798
+ "red",
799
+ "green",
800
+ "orange",
801
+ "blue",
802
+ "magenta",
803
+ "darkcyan",
804
+ "black"
805
+ ];
806
+ const color = colors[this.getInsecureHash(colors.length)];
807
+ return [
808
+ "span",
809
+ {},
810
+ [
811
+ "span",
812
+ {},
813
+ "PublicKey("
814
+ ],
815
+ [
816
+ "span",
817
+ {
818
+ style: `color: ${color};`
819
+ },
820
+ this.truncate()
821
+ ],
822
+ [
823
+ "span",
824
+ {},
825
+ ")"
826
+ ]
827
+ ];
828
+ }
829
+ };
830
+ }
831
+ /**
832
+ * Test this key for equality with some other key.
833
+ */
834
+ equals(other) {
835
+ const otherConverted = _PublicKey.from(other);
836
+ if (this._value.length !== otherConverted._value.length) {
837
+ return false;
838
+ }
839
+ let equal = true;
840
+ for (let i = 0; i < this._value.length; i++) {
841
+ equal &&= this._value[i] === otherConverted._value[i];
842
+ }
843
+ return equal;
844
+ }
845
+ [equalsSymbol](other) {
846
+ if (!_PublicKey.isPublicKey(other)) {
847
+ return false;
848
+ }
849
+ return this.equals(other);
834
850
  }
835
851
  };
836
852
  export {