@dxos/keys 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8

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.
Files changed (57) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +294 -435
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +289 -433
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/DXN.d.ts +50 -0
  9. package/dist/types/src/DXN.d.ts.map +1 -0
  10. package/dist/types/src/DXN.test.d.ts +2 -0
  11. package/dist/types/src/DXN.test.d.ts.map +1 -0
  12. package/dist/types/src/EID.d.ts +74 -0
  13. package/dist/types/src/EID.d.ts.map +1 -0
  14. package/dist/types/src/EID.test.d.ts +2 -0
  15. package/dist/types/src/EID.test.d.ts.map +1 -0
  16. package/dist/types/src/URI.d.ts +23 -0
  17. package/dist/types/src/URI.d.ts.map +1 -0
  18. package/dist/types/src/entity-id.d.ts +104 -0
  19. package/dist/types/src/entity-id.d.ts.map +1 -0
  20. package/dist/types/src/entity-id.test.d.ts +2 -0
  21. package/dist/types/src/entity-id.test.d.ts.map +1 -0
  22. package/dist/types/src/identity-did.d.ts +6 -4
  23. package/dist/types/src/identity-did.d.ts.map +1 -1
  24. package/dist/types/src/index.d.ts +5 -2
  25. package/dist/types/src/index.d.ts.map +1 -1
  26. package/dist/types/src/parse-id.d.ts +10 -0
  27. package/dist/types/src/parse-id.d.ts.map +1 -0
  28. package/dist/types/src/parse-id.test.d.ts +2 -0
  29. package/dist/types/src/parse-id.test.d.ts.map +1 -0
  30. package/dist/types/src/prng.d.ts.map +1 -1
  31. package/dist/types/src/public-key.d.ts +1 -1
  32. package/dist/types/src/public-key.d.ts.map +1 -1
  33. package/dist/types/src/random-bytes.d.ts.map +1 -1
  34. package/dist/types/src/space-id.d.ts +1 -1
  35. package/dist/types/src/space-id.d.ts.map +1 -1
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +14 -10
  38. package/src/DXN.test.ts +85 -0
  39. package/src/DXN.ts +104 -0
  40. package/src/EID.test.ts +147 -0
  41. package/src/EID.ts +151 -0
  42. package/src/URI.ts +35 -0
  43. package/src/entity-id.test.ts +73 -0
  44. package/src/entity-id.ts +202 -0
  45. package/src/identity-did.test.ts +19 -2
  46. package/src/identity-did.ts +60 -25
  47. package/src/index.ts +6 -2
  48. package/src/parse-id.test.ts +32 -0
  49. package/src/parse-id.ts +32 -0
  50. package/src/public-key.ts +4 -5
  51. package/src/space-id.ts +2 -2
  52. package/dist/types/src/dxn.d.ts +0 -129
  53. package/dist/types/src/dxn.d.ts.map +0 -1
  54. package/dist/types/src/object-id.d.ts +0 -65
  55. package/dist/types/src/object-id.d.ts.map +0 -1
  56. package/src/dxn.ts +0 -346
  57. package/src/object-id.ts +0 -131
@@ -16,6 +16,10 @@ var __esm = (fn, res) => function __init() {
16
16
  var __commonJS = (cb, mod) => function __require2() {
17
17
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
18
18
  };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, { get: all[name], enumerable: true });
22
+ };
19
23
  var __copyProps = (to, from, except, desc) => {
20
24
  if (from && typeof from === "object" || typeof from === "function") {
21
25
  for (let key of __getOwnPropNames(from))
@@ -98,76 +102,147 @@ var require_base32_decode = __commonJS({
98
102
  // src/index.ts
99
103
  init_inject_globals();
100
104
 
101
- // src/dxn.ts
102
- init_inject_globals();
103
- import * as Schema3 from "effect/Schema";
104
- import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
105
- import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
106
-
107
- // src/object-id.ts
105
+ // src/DXN.ts
106
+ var DXN_exports = {};
107
+ __export(DXN_exports, {
108
+ Schema: () => Schema_,
109
+ getName: () => getName,
110
+ getVersion: () => getVersion,
111
+ isDXN: () => isDXN,
112
+ make: () => make,
113
+ tryMake: () => tryMake
114
+ });
108
115
  init_inject_globals();
109
116
  import * as Schema from "effect/Schema";
110
- import { monotonicFactory } from "ulidx";
111
- var ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
112
- description: "A Universally Unique Lexicographically Sortable Identifier",
113
- pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
114
- });
115
- var ObjectId = class extends ObjectIdSchema {
116
- static #factory = monotonicFactory();
117
- static #seedTime = void 0;
118
- static isValid(id) {
119
- try {
120
- Schema.decodeSync(ObjectId)(id);
121
- return true;
122
- } catch {
123
- return false;
124
- }
117
+ var DXN_SPEC_REGEXP = /^dxn:[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z][a-zA-Z0-9]{0,62})(:\d+\.\d+\.\d+)?$/;
118
+ var isDXN = (value) => typeof value === "string" && value.startsWith("dxn:");
119
+ var make = (nsid, version) => parse(version != null ? `dxn:${nsid}:${version}` : `dxn:${nsid}`);
120
+ var tryMake = (dxn) => {
121
+ try {
122
+ return parse(dxn);
123
+ } catch {
124
+ return void 0;
125
125
  }
126
- static random() {
127
- return this.#factory(this.#seedTime);
126
+ };
127
+ var parse = (dxn) => {
128
+ if (typeof dxn === "string" && DXN_SPEC_REGEXP.test(dxn)) {
129
+ return dxn;
128
130
  }
129
- static dangerouslyDisableRandomness() {
130
- this.#factory = monotonicFactory(makeTestPRNG());
131
- this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
131
+ throw new Error(`Invalid DXN: ${dxn}`);
132
+ };
133
+ var getName = (dxn) => {
134
+ const match = /^dxn:([^:]+)/.exec(dxn);
135
+ if (!match) {
136
+ throw new Error(`Invalid DXN: ${dxn}`);
132
137
  }
138
+ return match[1];
133
139
  };
134
- var makeTestPRNG = () => {
135
- const rng = new SimplePRNG();
136
- return () => {
137
- return rng.next();
138
- };
140
+ var getVersion = (dxn) => {
141
+ const match = /^dxn:[^:]+:(\d+\.\d+\.\d+)$/.exec(dxn);
142
+ return match?.[1];
139
143
  };
140
- var SimplePRNG = class _SimplePRNG {
141
- #seed;
142
- // LCG parameters (from Numerical Recipes)
143
- static #a = 1664525;
144
- static #c = 1013904223;
145
- static #m = Math.pow(2, 32);
146
- /**
147
- * Creates a new PRNG instance.
148
- * @param seed - Initial seed value. If not provided, uses 0.
149
- */
150
- constructor(seed = 0) {
151
- this.#seed = seed;
152
- }
153
- /**
154
- * Generates the next pseudo-random number in the range [0, 1).
155
- * @returns A pseudo-random number between 0 (inclusive) and 1 (exclusive).
156
- */
157
- next() {
158
- this.#seed = (_SimplePRNG.#a * this.#seed + _SimplePRNG.#c) % _SimplePRNG.#m;
159
- return this.#seed / _SimplePRNG.#m;
160
- }
161
- /**
162
- * Resets the generator with a new seed.
163
- * @param seed - New seed value.
164
- */
165
- reset(seed) {
166
- this.#seed = seed;
144
+ var Schema_ = Schema.String.pipe(Schema.filter((value) => isDXN(value), {
145
+ message: () => "Invalid DXN"
146
+ }), Schema.annotations({
147
+ title: "DXN",
148
+ description: "DXN URI: dxn:<nsid>[:<version>]"
149
+ }));
150
+
151
+ // src/EID.ts
152
+ var EID_exports = {};
153
+ __export(EID_exports, {
154
+ Schema: () => Schema_2,
155
+ equals: () => equals,
156
+ getEntityId: () => getEntityId,
157
+ getSpaceId: () => getSpaceId,
158
+ isEID: () => isEID,
159
+ isLocal: () => isLocal,
160
+ make: () => make2,
161
+ parse: () => parse2,
162
+ toLocal: () => toLocal,
163
+ tryParse: () => tryParse
164
+ });
165
+ init_inject_globals();
166
+ import * as Schema2 from "effect/Schema";
167
+ var ECHO_URI_REGEXP = /^echo:(?:\/\/[^/]+(?:\/[^/]+)?|(?:\/\/\/|\/)[^/]+)$/;
168
+ var QUALIFIED_RE = /^echo:\/\/([^/]+)\/([^/]+)$/;
169
+ var SPACE_ONLY_RE = /^echo:\/\/([^/]+)$/;
170
+ var LOCAL_RE = /^echo:(?:\/\/\/|\/)([^/]+)$/;
171
+ var isEID = (value) => typeof value === "string" && value.startsWith("echo:");
172
+ var parse2 = (uri) => {
173
+ if (!ECHO_URI_REGEXP.test(uri)) {
174
+ throw new Error(`Invalid EID: ${uri}`);
175
+ }
176
+ return uri;
177
+ };
178
+ var tryParse = (uri) => {
179
+ try {
180
+ return parse2(uri);
181
+ } catch {
182
+ return void 0;
167
183
  }
168
184
  };
185
+ var make2 = ({ spaceId, entityId }) => {
186
+ let raw;
187
+ if (spaceId != null && entityId != null) {
188
+ raw = `echo://${spaceId}/${entityId}`;
189
+ } else if (entityId != null) {
190
+ raw = `echo:/${entityId}`;
191
+ } else if (spaceId != null) {
192
+ raw = `echo://${spaceId}`;
193
+ } else {
194
+ throw new Error("EID.make requires at least one of spaceId or entityId");
195
+ }
196
+ return parse2(raw);
197
+ };
198
+ var getSpaceId = (uri) => {
199
+ const normalized = parse2(uri);
200
+ const match = QUALIFIED_RE.exec(normalized) ?? SPACE_ONLY_RE.exec(normalized);
201
+ return match?.[1];
202
+ };
203
+ var getEntityId = (uri) => {
204
+ const normalized = parse2(uri);
205
+ const qualMatch = QUALIFIED_RE.exec(normalized);
206
+ if (qualMatch) {
207
+ return qualMatch[2];
208
+ }
209
+ const localMatch = LOCAL_RE.exec(normalized);
210
+ return localMatch?.[1];
211
+ };
212
+ var isLocal = (uri) => {
213
+ const normalized = parse2(uri);
214
+ return LOCAL_RE.test(normalized);
215
+ };
216
+ var toLocal = (uri) => {
217
+ const entityId = getEntityId(uri);
218
+ return entityId != null ? make2({
219
+ entityId
220
+ }) : parse2(uri);
221
+ };
222
+ var equals = (a, b) => parse2(a) === parse2(b);
223
+ var Schema_2 = Schema2.String.pipe(Schema2.filter((value) => isEID(value), {
224
+ message: () => "Invalid EID: must start with echo:"
225
+ }), Schema2.annotations({
226
+ title: "EID",
227
+ description: "ECHO object/space URI: echo://<spaceId>[/<objectId>] or echo:/<objectId>"
228
+ }));
169
229
 
170
- // src/space-id.ts
230
+ // src/URI.ts
231
+ var URI_exports = {};
232
+ __export(URI_exports, {
233
+ Schema: () => Schema_3,
234
+ isURI: () => isURI,
235
+ make: () => make3
236
+ });
237
+ init_inject_globals();
238
+ import * as Schema3 from "effect/Schema";
239
+ var make3 = (uri) => uri;
240
+ var isURI = (value) => typeof value === "string" && /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(value);
241
+ var Schema_3 = Schema3.String.pipe(Schema3.filter((value) => isURI(value), {
242
+ message: () => "Invalid URI"
243
+ }));
244
+
245
+ // src/identity-did.ts
171
246
  init_inject_globals();
172
247
  var import_base32_decode = __toESM(require_base32_decode(), 1);
173
248
 
@@ -234,8 +309,8 @@ function base32Encode(data, variant, options) {
234
309
  return output;
235
310
  }
236
311
 
237
- // src/space-id.ts
238
- import * as Schema2 from "effect/Schema";
312
+ // src/identity-did.ts
313
+ import * as Schema4 from "effect/Schema";
239
314
  import { invariant } from "@dxos/invariant";
240
315
 
241
316
  // src/random-bytes.ts
@@ -247,375 +322,161 @@ var randomBytes = (length) => {
247
322
  return bytes;
248
323
  };
249
324
 
250
- // src/space-id.ts
251
- var __dxlog_file = "/__w/dxos/dxos/packages/common/keys/src/space-id.ts";
325
+ // src/identity-did.ts
252
326
  var MULTIBASE_PREFIX = "B";
253
- var ENCODED_LENGTH = 33;
327
+ var DID_PREFIX = "did:halo:";
328
+ var DECODED_BYTE_LENGTH = 20;
329
+ var ENCODED_LENGTH = 42;
330
+ var RFC4648_BASE32_PATTERN = /^[A-Z2-7]+$/;
254
331
  var isValid = (value) => {
255
- return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
332
+ if (typeof value !== "string" || !value.startsWith(DID_PREFIX + MULTIBASE_PREFIX) || value.length !== ENCODED_LENGTH) {
333
+ return false;
334
+ }
335
+ const encoded = value.slice(DID_PREFIX.length + MULTIBASE_PREFIX.length);
336
+ if (!RFC4648_BASE32_PATTERN.test(encoded)) {
337
+ return false;
338
+ }
339
+ try {
340
+ return (0, import_base32_decode.default)(encoded, "RFC4648").byteLength === DECODED_BYTE_LENGTH;
341
+ } catch {
342
+ return false;
343
+ }
256
344
  };
257
- var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
258
- static byteLength = 20;
345
+ var IdentityDid = class extends Schema4.String.pipe(Schema4.filter(isValid)) {
346
+ static byteLength = DECODED_BYTE_LENGTH;
259
347
  static encode = (value) => {
260
- invariant(value instanceof Uint8Array, "Invalid type", {
261
- F: __dxlog_file,
262
- L: 43,
263
- S: this,
264
- A: [
265
- "value instanceof Uint8Array",
266
- "'Invalid type'"
267
- ]
268
- });
269
- invariant(value.length === SpaceId.byteLength, "Invalid length", {
270
- F: __dxlog_file,
271
- L: 44,
272
- S: this,
273
- A: [
274
- "value.length === SpaceId.byteLength",
275
- "'Invalid length'"
276
- ]
277
- });
278
- return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
348
+ invariant(value instanceof Uint8Array, "Invalid type");
349
+ invariant(value.length === IdentityDid.byteLength, "Invalid length");
350
+ return DID_PREFIX + MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
279
351
  };
280
352
  static decode = (value) => {
281
- invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
282
- F: __dxlog_file,
283
- L: 49,
284
- S: this,
285
- A: [
286
- "value.startsWith(MULTIBASE_PREFIX)",
287
- "'Invalid multibase32 encoding'"
288
- ]
289
- });
290
- return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
353
+ invariant(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX), "Invalid multibase32 encoding");
354
+ return new Uint8Array((0, import_base32_decode.default)(value.slice(DID_PREFIX.length + MULTIBASE_PREFIX.length), "RFC4648"));
291
355
  };
292
356
  static isValid = isValid;
293
357
  static random = () => {
294
- return SpaceId.encode(randomBytes(SpaceId.byteLength));
358
+ return IdentityDid.encode(randomBytes(IdentityDid.byteLength));
295
359
  };
296
360
  };
297
361
 
298
- // src/dxn.ts
299
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
300
- var LOCAL_SPACE_TAG = "@";
301
- var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
302
- var QueueSubspaceTags = Object.freeze({
303
- DATA: "data",
304
- TRACE: "trace"
362
+ // src/entity-id.ts
363
+ init_inject_globals();
364
+ import * as Schema5 from "effect/Schema";
365
+ import { monotonicFactory } from "ulidx";
366
+ var ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
367
+ var EntityIdSchema = Schema5.String.pipe(Schema5.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
368
+ description: "A Universally Unique Lexicographically Sortable Identifier",
369
+ pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
305
370
  });
306
- var DXN = class _DXN {
307
- // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
308
- // TODO(dmaretskyi): Should this be a transformation into the DXN type?
309
- static Schema = Schema3.NonEmptyString.pipe(
310
- Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
311
- // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
312
- // FormatAnnotation.set(FormatEnum.DXN),
313
- Schema3.annotations({
314
- title: "DXN",
315
- description: "DXN URI",
316
- examples: [
317
- "dxn:type:example.com/type/MyType",
318
- "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
319
- ]
320
- })
321
- );
322
- static hash(dxn) {
323
- return dxn.toString();
324
- }
325
- /**
326
- * Kind constants.
327
- */
328
- static kind = Object.freeze({
329
- /**
330
- * dxn:type:<type_name>[:<version>]
331
- */
332
- TYPE: "type",
333
- /**
334
- * dxn:echo:<space_id>:<echo_id>
335
- * dxn:echo:@:<echo_id>
336
- */
337
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
338
- // TODO(burdon): Add separate Kind for space?
339
- ECHO: "echo",
340
- /**
341
- * The subspace tag enables us to partition queues by usage within the context of a space.
342
- * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
343
- * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
344
- * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
345
- */
346
- QUEUE: "queue"
347
- });
348
- /**
349
- * Exactly equals.
350
- */
351
- static equals(a, b) {
352
- return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
353
- }
354
- static equalsEchoId(a, b) {
355
- const a1 = a.asEchoDXN();
356
- const b1 = b.asEchoDXN();
357
- return !!a1 && !!b1 && a1.echoId === b1.echoId;
371
+ var EntityId = class extends EntityIdSchema {
372
+ static #factory = monotonicFactory();
373
+ static #seedTime = void 0;
374
+ static isValid(id) {
375
+ try {
376
+ Schema5.decodeSync(EntityId)(id);
377
+ return true;
378
+ } catch {
379
+ return false;
380
+ }
358
381
  }
359
- // TODO(burdon): Rename isValid.
360
- static isDXNString(dxn) {
361
- return dxn.startsWith("dxn:");
382
+ static random() {
383
+ return this.#factory(this.#seedTime);
362
384
  }
363
- static parse(dxn) {
364
- if (typeof dxn !== "string") {
365
- throw new Error(`Invalid DXN: ${dxn}`);
385
+ static deterministic(...seed) {
386
+ const input = seed.map((value) => String(value)).join("\0");
387
+ let h1 = 2166136261 >>> 0;
388
+ let h2 = 461845907 >>> 0;
389
+ for (let i = 0; i < input.length; i++) {
390
+ const code = input.charCodeAt(i);
391
+ h1 = Math.imul(h1 ^ code, 16777619) >>> 0;
392
+ h2 = Math.imul(h2 ^ (code << 13 | code >>> 3), 16777619) >>> 0;
366
393
  }
367
- const [prefix, kind, ...parts] = dxn.split(":");
368
- if (!(prefix === "dxn")) {
369
- throw new Error(`Invalid DXN: ${dxn}`);
394
+ const time = ALPHABET[0].repeat(10);
395
+ let bits = BigInt(h1) << 32n | BigInt(h2);
396
+ let rand = "";
397
+ for (let i = 0; i < 16; i++) {
398
+ rand = ALPHABET[Number(bits & 0x1fn)] + rand;
399
+ bits >>= 5n;
370
400
  }
371
- if (!(typeof kind === "string" && kind.length > 0)) {
372
- throw new Error(`Invalid DXN: ${dxn}`);
373
- }
374
- if (!(parts.length > 0)) {
375
- throw new Error(`Invalid DXN: ${dxn}`);
376
- }
377
- return new _DXN(kind, parts);
401
+ return time + rand;
378
402
  }
379
- static tryParse(dxn) {
380
- try {
381
- return _DXN.parse(dxn);
382
- } catch {
383
- return void 0;
384
- }
385
- }
386
- /**
387
- * @example `dxn:type:example.com/type/Person`
388
- */
389
- static fromTypename(typename) {
390
- return new _DXN(_DXN.kind.TYPE, [
391
- typename
392
- ]);
403
+ static dangerouslyDisableRandomness() {
404
+ this.#factory = monotonicFactory(makeTestPRNG());
405
+ this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
393
406
  }
394
- /**
395
- * @example `dxn:type:example.com/type/Person:0.1.0`
396
- */
397
- // TODO(dmaretskyi): Consider using @ as the version separator.
398
- static fromTypenameAndVersion(typename, version) {
399
- return new _DXN(_DXN.kind.TYPE, [
400
- typename,
401
- version
402
- ]);
407
+ static dangerouslySetSeed(time, seed) {
408
+ this.#factory = monotonicFactory(makeTestPRNG(seed));
409
+ this.#seedTime = time;
403
410
  }
411
+ };
412
+ var makeTestPRNG = (seed = 0) => {
413
+ const rng = new SimplePRNG(seed);
414
+ return () => {
415
+ return rng.next();
416
+ };
417
+ };
418
+ var SimplePRNG = class _SimplePRNG {
419
+ #seed;
420
+ // LCG parameters (from Numerical Recipes)
421
+ static #a = 1664525;
422
+ static #c = 1013904223;
423
+ static #m = Math.pow(2, 32);
404
424
  /**
405
- * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
425
+ * Creates a new PRNG instance.
426
+ * @param seed - Initial seed value. If not provided, uses 0.
406
427
  */
407
- static fromSpaceAndObjectId(spaceId, objectId) {
408
- assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
409
- assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
410
- return new _DXN(_DXN.kind.ECHO, [
411
- spaceId,
412
- objectId
413
- ]);
428
+ constructor(seed = 0) {
429
+ this.#seed = seed;
414
430
  }
415
431
  /**
416
- * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
432
+ * Generates the next pseudo-random number in the range [0, 1).
433
+ * @returns A pseudo-random number between 0 (inclusive) and 1 (exclusive).
417
434
  */
418
- static fromLocalObjectId(id) {
419
- assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
420
- return new _DXN(_DXN.kind.ECHO, [
421
- LOCAL_SPACE_TAG,
422
- id
423
- ]);
424
- }
425
- static fromQueue(subspaceTag, spaceId, queueId, objectId) {
426
- assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
427
- assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
428
- assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
429
- return new _DXN(_DXN.kind.QUEUE, [
430
- subspaceTag,
431
- spaceId,
432
- queueId,
433
- ...objectId ? [
434
- objectId
435
- ] : []
436
- ]);
437
- }
438
- #kind;
439
- #parts;
440
- constructor(kind, parts) {
441
- assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
442
- assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
443
- switch (kind) {
444
- case _DXN.kind.TYPE:
445
- if (parts.length > 2) {
446
- throw new Error("Invalid DXN.kind.TYPE");
447
- }
448
- break;
449
- case _DXN.kind.ECHO:
450
- if (parts.length !== 2) {
451
- throw new Error("Invalid DXN.kind.ECHO");
452
- }
453
- break;
454
- }
455
- this.#kind = kind;
456
- this.#parts = parts;
457
- }
458
- toString() {
459
- return `dxn:${this.#kind}:${this.#parts.join(":")}`;
460
- }
461
- toJSON() {
462
- return this.toString();
435
+ next() {
436
+ this.#seed = (_SimplePRNG.#a * this.#seed + _SimplePRNG.#c) % _SimplePRNG.#m;
437
+ return this.#seed / _SimplePRNG.#m;
463
438
  }
464
439
  /**
465
- * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
440
+ * Resets the generator with a new seed.
441
+ * @param seed - New seed value.
466
442
  */
467
- [inspectCustom](depth, options, inspectFn) {
468
- const printControlCode = (code) => {
469
- return `\x1B[${code}m`;
470
- };
471
- return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
472
- }
473
- get [devtoolsFormatter]() {
474
- return {
475
- header: () => {
476
- return [
477
- "span",
478
- {
479
- style: "font-weight: bold;"
480
- },
481
- this.toString()
482
- ];
483
- }
484
- };
485
- }
486
- get kind() {
487
- return this.#kind;
488
- }
489
- get parts() {
490
- return this.#parts;
491
- }
492
- // TODO(burdon): Should getters fail?
493
- get typename() {
494
- invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
495
- F: __dxlog_file2,
496
- L: 250,
497
- S: this,
498
- A: [
499
- "this.#kind === DXN.kind.TYPE",
500
- ""
501
- ]
502
- });
503
- return this.#parts[0];
504
- }
505
- equals(other) {
506
- return _DXN.equals(this, other);
507
- }
508
- hasTypenameOf(typename) {
509
- return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
510
- }
511
- isLocalObjectId() {
512
- return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
443
+ reset(seed) {
444
+ this.#seed = seed;
513
445
  }
514
- asTypeDXN() {
515
- if (this.kind !== _DXN.kind.TYPE) {
516
- return void 0;
517
- }
518
- const [type, version] = this.#parts;
446
+ };
447
+
448
+ // src/parse-id.ts
449
+ init_inject_globals();
450
+ var SPACE_ID_LENGTH = 33;
451
+ var OBJECT_ID_LENGTH = 26;
452
+ var FQ_ID_LENGTH = SPACE_ID_LENGTH + OBJECT_ID_LENGTH + 1;
453
+ var parseId = (id) => {
454
+ if (!id) {
455
+ return {};
456
+ } else if (id.length === SPACE_ID_LENGTH) {
519
457
  return {
520
- // TODO(wittjosiah): Should be `typename` for consistency.
521
- type,
522
- version
458
+ spaceId: id
523
459
  };
524
- }
525
- asEchoDXN() {
526
- if (this.kind !== _DXN.kind.ECHO) {
527
- return void 0;
528
- }
529
- const [spaceId, echoId] = this.#parts;
460
+ } else if (id.length === OBJECT_ID_LENGTH) {
530
461
  return {
531
- spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
532
- // TODO(burdon): objectId.
533
- echoId
462
+ objectId: id
534
463
  };
535
- }
536
- asQueueDXN() {
537
- if (this.kind !== _DXN.kind.QUEUE) {
538
- return void 0;
539
- }
540
- const [subspaceTag, spaceId, queueId, objectId] = this.#parts;
541
- if (typeof queueId !== "string") {
542
- return void 0;
543
- }
464
+ } else if (id.length === FQ_ID_LENGTH && id.indexOf(":") === SPACE_ID_LENGTH) {
465
+ const [spaceId, objectId] = id.split(":");
544
466
  return {
545
- subspaceTag,
546
467
  spaceId,
547
- queueId,
548
468
  objectId
549
469
  };
550
- }
551
- /**
552
- * Produces a new DXN with the given parts appended.
553
- */
554
- extend(parts) {
555
- return new _DXN(this.#kind, [
556
- ...this.#parts,
557
- ...parts
558
- ]);
470
+ } else {
471
+ return {};
559
472
  }
560
473
  };
561
474
 
562
- // src/identity-did.ts
563
- init_inject_globals();
564
- var import_base32_decode2 = __toESM(require_base32_decode(), 1);
565
- import { invariant as invariant3 } from "@dxos/invariant";
566
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/keys/src/identity-did.ts";
567
- var IdentityDid = Object.freeze({
568
- byteLength: 20,
569
- encode: (value) => {
570
- invariant3(value instanceof Uint8Array, "Invalid type", {
571
- F: __dxlog_file3,
572
- L: 22,
573
- S: void 0,
574
- A: [
575
- "value instanceof Uint8Array",
576
- "'Invalid type'"
577
- ]
578
- });
579
- invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
580
- F: __dxlog_file3,
581
- L: 23,
582
- S: void 0,
583
- A: [
584
- "value.length === IdentityDid.byteLength",
585
- "'Invalid length'"
586
- ]
587
- });
588
- return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
589
- },
590
- decode: (value) => {
591
- invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
592
- F: __dxlog_file3,
593
- L: 28,
594
- S: void 0,
595
- A: [
596
- "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
597
- "'Invalid multibase32 encoding'"
598
- ]
599
- });
600
- return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
601
- },
602
- isValid: (value) => {
603
- return typeof value === "string" && value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2) && value.length === ENCODED_LENGTH2;
604
- },
605
- random: () => {
606
- return IdentityDid.encode(randomBytes(IdentityDid.byteLength));
607
- }
608
- });
609
- var MULTIBASE_PREFIX2 = "B";
610
- var DID_PREFIX = "did:halo:";
611
- var ENCODED_LENGTH2 = 42;
612
-
613
475
  // src/public-key.ts
614
476
  init_inject_globals();
615
- var import_base32_decode3 = __toESM(require_base32_decode(), 1);
616
- import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
617
- import { invariant as invariant4 } from "@dxos/invariant";
618
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/keys/src/public-key.ts";
477
+ var import_base32_decode2 = __toESM(require_base32_decode(), 1);
478
+ import { devtoolsFormatter, equalsSymbol, inspectCustom, truncateKey } from "@dxos/debug";
479
+ import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
619
480
  var PUBLIC_KEY_LENGTH = 32;
620
481
  var SECRET_KEY_LENGTH = 64;
621
482
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
@@ -628,15 +489,7 @@ var PublicKey = class _PublicKey {
628
489
  * @returns PublicKey
629
490
  */
630
491
  static from(source) {
631
- invariant4(source, void 0, {
632
- F: __dxlog_file4,
633
- L: 50,
634
- S: this,
635
- A: [
636
- "source",
637
- ""
638
- ]
639
- });
492
+ invariant2(source);
640
493
  if (source instanceof _PublicKey) {
641
494
  return source;
642
495
  } else if (source instanceof Buffer2) {
@@ -720,15 +573,7 @@ var PublicKey = class _PublicKey {
720
573
  * @deprecated All keys should be represented as instances of PublicKey.
721
574
  */
722
575
  static bufferize(str) {
723
- invariant4(typeof str === "string", "Invalid type", {
724
- F: __dxlog_file4,
725
- L: 153,
726
- S: this,
727
- A: [
728
- "typeof str === 'string'",
729
- "'Invalid type'"
730
- ]
731
- });
576
+ assertArgument(typeof str === "string", "str", "Invalid type");
732
577
  const buffer = Buffer2.from(str, "hex");
733
578
  return buffer;
734
579
  }
@@ -743,15 +588,7 @@ var PublicKey = class _PublicKey {
743
588
  } else if (key instanceof Uint8Array) {
744
589
  key = Buffer2.from(key.buffer, key.byteOffset, key.byteLength);
745
590
  }
746
- invariant4(key instanceof Buffer2, "Invalid type", {
747
- F: __dxlog_file4,
748
- L: 172,
749
- S: this,
750
- A: [
751
- "key instanceof Buffer",
752
- "'Invalid type'"
753
- ]
754
- });
591
+ invariant2(key instanceof Buffer2, "Invalid type");
755
592
  return key.toString("hex");
756
593
  }
757
594
  /**
@@ -762,16 +599,8 @@ var PublicKey = class _PublicKey {
762
599
  return key.toHex();
763
600
  }
764
601
  static fromMultibase32(encoded) {
765
- invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
766
- F: __dxlog_file4,
767
- L: 185,
768
- S: this,
769
- A: [
770
- "encoded.startsWith('B')",
771
- "'Invalid multibase32 encoding'"
772
- ]
773
- });
774
- return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
602
+ invariant2(encoded.startsWith("B"), "Invalid multibase32 encoding");
603
+ return new _PublicKey(new Uint8Array((0, import_base32_decode2.default)(encoded.slice(1), "RFC4648")));
775
604
  }
776
605
  constructor(_value) {
777
606
  this._value = _value;
@@ -797,7 +626,7 @@ var PublicKey = class _PublicKey {
797
626
  toMultibase32() {
798
627
  return "B" + base32Encode(this._value, "RFC4648");
799
628
  }
800
- truncate(length = void 0) {
629
+ truncate(length) {
801
630
  return truncateKey(this, length);
802
631
  }
803
632
  asBuffer() {
@@ -812,7 +641,7 @@ var PublicKey = class _PublicKey {
812
641
  /**
813
642
  * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
814
643
  */
815
- [inspectCustom2](depth, options, inspectFn) {
644
+ [inspectCustom](depth, options, inspectFn) {
816
645
  if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
817
646
  return `<PublicKey ${this.truncate()}>`;
818
647
  }
@@ -837,7 +666,7 @@ var PublicKey = class _PublicKey {
837
666
  const color = colors[this.getInsecureHash(colors.length)];
838
667
  return `PublicKey(${printControlCode(inspectFn.colors[color][0])}${this.truncate()}${printControlCode(inspectFn.colors.reset[0])})`;
839
668
  }
840
- get [devtoolsFormatter2]() {
669
+ get [devtoolsFormatter]() {
841
670
  return {
842
671
  header: () => {
843
672
  const colors = [
@@ -901,17 +730,47 @@ var PublicKey = class _PublicKey {
901
730
  return this.equals(other);
902
731
  }
903
732
  };
733
+
734
+ // src/space-id.ts
735
+ init_inject_globals();
736
+ var import_base32_decode3 = __toESM(require_base32_decode(), 1);
737
+ import * as Schema6 from "effect/Schema";
738
+ import { invariant as invariant3 } from "@dxos/invariant";
739
+ var MULTIBASE_PREFIX2 = "B";
740
+ var ENCODED_LENGTH2 = 33;
741
+ var isValid2 = (value) => {
742
+ return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX2) && value.length === ENCODED_LENGTH2;
743
+ };
744
+ var SpaceId = class extends Schema6.String.pipe(Schema6.filter(isValid2)) {
745
+ static byteLength = 20;
746
+ static encode = (value) => {
747
+ invariant3(value instanceof Uint8Array, "Invalid type");
748
+ invariant3(value.length === SpaceId.byteLength, "Invalid length");
749
+ return MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
750
+ };
751
+ static decode = (value) => {
752
+ invariant3(value.startsWith(MULTIBASE_PREFIX2), "Invalid multibase32 encoding");
753
+ return new Uint8Array((0, import_base32_decode3.default)(value.slice(1), "RFC4648"));
754
+ };
755
+ static isValid = isValid2;
756
+ static random = () => {
757
+ return SpaceId.encode(randomBytes(SpaceId.byteLength));
758
+ };
759
+ };
904
760
  export {
905
- DXN,
906
- DXN_ECHO_REGEXP,
761
+ DXN_exports as DXN,
762
+ EID_exports as EID,
763
+ EntityId,
764
+ FQ_ID_LENGTH,
907
765
  IdentityDid,
908
- LOCAL_SPACE_TAG,
909
- ObjectId,
766
+ OBJECT_ID_LENGTH,
910
767
  PUBLIC_KEY_LENGTH,
911
768
  PublicKey,
912
- QueueSubspaceTags,
913
769
  SECRET_KEY_LENGTH,
770
+ SPACE_ID_LENGTH,
914
771
  SimplePRNG,
915
- SpaceId
772
+ SpaceId,
773
+ URI_exports as URI,
774
+ parseId
916
775
  };
917
776
  //# sourceMappingURL=index.mjs.map