@dxos/keys 0.8.4-main.f9ba587 → 0.8.4-main.fcc0d83b33
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.
- package/dist/lib/browser/index.mjs +160 -228
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +160 -228
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/dxn.d.ts +35 -18
- package/dist/types/src/dxn.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/object-id.d.ts +50 -1
- package/dist/types/src/object-id.d.ts.map +1 -1
- package/dist/types/src/prng.d.ts +32 -0
- package/dist/types/src/prng.d.ts.map +1 -0
- package/dist/types/src/public-key.d.ts +3 -3
- package/dist/types/src/public-key.d.ts.map +1 -1
- package/dist/types/src/random-bytes.d.ts.map +1 -1
- package/dist/types/src/space-id.d.ts +2 -2
- package/dist/types/src/space-id.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -14
- package/src/dxn.ts +82 -61
- package/src/index.ts +1 -1
- package/src/object-id.ts +94 -5
- package/src/prng.ts +54 -0
- package/src/public-key.ts +4 -4
- package/src/random-bytes.ts +1 -1
- package/src/space-id.ts +3 -3
|
@@ -100,29 +100,70 @@ init_inject_globals();
|
|
|
100
100
|
|
|
101
101
|
// src/dxn.ts
|
|
102
102
|
init_inject_globals();
|
|
103
|
-
import
|
|
103
|
+
import * as Schema3 from "effect/Schema";
|
|
104
104
|
import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
|
|
105
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
105
|
+
import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
|
|
106
106
|
|
|
107
107
|
// src/object-id.ts
|
|
108
108
|
init_inject_globals();
|
|
109
|
-
import
|
|
110
|
-
import {
|
|
109
|
+
import * as Schema from "effect/Schema";
|
|
110
|
+
import { monotonicFactory } from "ulidx";
|
|
111
111
|
var ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
|
|
112
|
-
description: "
|
|
112
|
+
description: "A Universally Unique Lexicographically Sortable Identifier",
|
|
113
113
|
pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
|
|
114
114
|
});
|
|
115
115
|
var ObjectId = class extends ObjectIdSchema {
|
|
116
|
+
static #factory = monotonicFactory();
|
|
117
|
+
static #seedTime = void 0;
|
|
116
118
|
static isValid(id) {
|
|
117
119
|
try {
|
|
118
120
|
Schema.decodeSync(ObjectId)(id);
|
|
119
121
|
return true;
|
|
120
|
-
} catch
|
|
122
|
+
} catch {
|
|
121
123
|
return false;
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
126
|
static random() {
|
|
125
|
-
return
|
|
127
|
+
return this.#factory(this.#seedTime);
|
|
128
|
+
}
|
|
129
|
+
static dangerouslyDisableRandomness() {
|
|
130
|
+
this.#factory = monotonicFactory(makeTestPRNG());
|
|
131
|
+
this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
var makeTestPRNG = () => {
|
|
135
|
+
const rng = new SimplePRNG();
|
|
136
|
+
return () => {
|
|
137
|
+
return rng.next();
|
|
138
|
+
};
|
|
139
|
+
};
|
|
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;
|
|
126
167
|
}
|
|
127
168
|
};
|
|
128
169
|
|
|
@@ -194,7 +235,7 @@ function base32Encode(data, variant, options) {
|
|
|
194
235
|
}
|
|
195
236
|
|
|
196
237
|
// src/space-id.ts
|
|
197
|
-
import
|
|
238
|
+
import * as Schema2 from "effect/Schema";
|
|
198
239
|
import { invariant } from "@dxos/invariant";
|
|
199
240
|
|
|
200
241
|
// src/random-bytes.ts
|
|
@@ -207,121 +248,88 @@ var randomBytes = (length) => {
|
|
|
207
248
|
};
|
|
208
249
|
|
|
209
250
|
// src/space-id.ts
|
|
210
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/common/keys/src/space-id.ts";
|
|
211
251
|
var MULTIBASE_PREFIX = "B";
|
|
212
252
|
var ENCODED_LENGTH = 33;
|
|
213
253
|
var isValid = (value) => {
|
|
214
254
|
return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
|
|
215
255
|
};
|
|
216
256
|
var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
|
|
217
|
-
static
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
invariant(value.length === SpaceId.byteLength, "Invalid length", {
|
|
232
|
-
F: __dxlog_file,
|
|
233
|
-
L: 44,
|
|
234
|
-
S: this,
|
|
235
|
-
A: [
|
|
236
|
-
"value.length === SpaceId.byteLength",
|
|
237
|
-
"'Invalid length'"
|
|
238
|
-
]
|
|
239
|
-
});
|
|
240
|
-
return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
static {
|
|
244
|
-
this.decode = (value) => {
|
|
245
|
-
invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
|
|
246
|
-
F: __dxlog_file,
|
|
247
|
-
L: 49,
|
|
248
|
-
S: this,
|
|
249
|
-
A: [
|
|
250
|
-
"value.startsWith(MULTIBASE_PREFIX)",
|
|
251
|
-
"'Invalid multibase32 encoding'"
|
|
252
|
-
]
|
|
253
|
-
});
|
|
254
|
-
return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
static {
|
|
258
|
-
this.isValid = isValid;
|
|
259
|
-
}
|
|
260
|
-
static {
|
|
261
|
-
this.random = () => {
|
|
262
|
-
return SpaceId.encode(randomBytes(SpaceId.byteLength));
|
|
263
|
-
};
|
|
264
|
-
}
|
|
257
|
+
static byteLength = 20;
|
|
258
|
+
static encode = (value) => {
|
|
259
|
+
invariant(value instanceof Uint8Array, "Invalid type");
|
|
260
|
+
invariant(value.length === SpaceId.byteLength, "Invalid length");
|
|
261
|
+
return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
|
|
262
|
+
};
|
|
263
|
+
static decode = (value) => {
|
|
264
|
+
invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding");
|
|
265
|
+
return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
|
|
266
|
+
};
|
|
267
|
+
static isValid = isValid;
|
|
268
|
+
static random = () => {
|
|
269
|
+
return SpaceId.encode(randomBytes(SpaceId.byteLength));
|
|
270
|
+
};
|
|
265
271
|
};
|
|
266
272
|
|
|
267
273
|
// src/dxn.ts
|
|
268
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
|
|
269
274
|
var LOCAL_SPACE_TAG = "@";
|
|
275
|
+
var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
|
|
270
276
|
var QueueSubspaceTags = Object.freeze({
|
|
271
277
|
DATA: "data",
|
|
272
278
|
TRACE: "trace"
|
|
273
279
|
});
|
|
274
280
|
var DXN = class _DXN {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
281
|
+
// TODO(burdon): Rename to DXN (i.e., DXN.DXN).
|
|
282
|
+
// TODO(dmaretskyi): Should this be a transformation into the DXN type?
|
|
283
|
+
static Schema = Schema3.NonEmptyString.pipe(
|
|
284
|
+
Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
|
|
285
|
+
// TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
|
|
286
|
+
// FormatAnnotation.set(TypeFormat.DXN),
|
|
287
|
+
Schema3.annotations({
|
|
288
|
+
title: "DXN",
|
|
289
|
+
description: "DXN URI",
|
|
290
|
+
examples: [
|
|
291
|
+
"dxn:type:com.example.type.my-type",
|
|
292
|
+
"dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
|
|
293
|
+
]
|
|
294
|
+
})
|
|
295
|
+
);
|
|
291
296
|
static hash(dxn) {
|
|
292
297
|
return dxn.toString();
|
|
293
298
|
}
|
|
294
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Kind constants.
|
|
301
|
+
*/
|
|
302
|
+
static kind = Object.freeze({
|
|
295
303
|
/**
|
|
296
|
-
*
|
|
304
|
+
* dxn:type:<type_name>[:<version>]
|
|
297
305
|
*/
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
get kind() {
|
|
320
|
-
return this.#kind;
|
|
321
|
-
}
|
|
306
|
+
TYPE: "type",
|
|
307
|
+
/**
|
|
308
|
+
* dxn:echo:<space_id>:<echo_id>
|
|
309
|
+
* dxn:echo:@:<echo_id>
|
|
310
|
+
*/
|
|
311
|
+
// TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
|
|
312
|
+
// TODO(burdon): Add separate Kind for space?
|
|
313
|
+
ECHO: "echo",
|
|
314
|
+
/**
|
|
315
|
+
* The subspace tag enables us to partition queues by usage within the context of a space.
|
|
316
|
+
* dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
|
|
317
|
+
* dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
|
|
318
|
+
* dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
|
|
319
|
+
*/
|
|
320
|
+
QUEUE: "queue"
|
|
321
|
+
});
|
|
322
|
+
/**
|
|
323
|
+
* Exactly equals.
|
|
324
|
+
*/
|
|
322
325
|
static equals(a, b) {
|
|
323
326
|
return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
|
|
324
327
|
}
|
|
328
|
+
static equalsEchoId(a, b) {
|
|
329
|
+
const a1 = a.asEchoDXN();
|
|
330
|
+
const b1 = b.asEchoDXN();
|
|
331
|
+
return !!a1 && !!b1 && a1.echoId === b1.echoId;
|
|
332
|
+
}
|
|
325
333
|
// TODO(burdon): Rename isValid.
|
|
326
334
|
static isDXNString(dxn) {
|
|
327
335
|
return dxn.startsWith("dxn:");
|
|
@@ -345,12 +353,12 @@ var DXN = class _DXN {
|
|
|
345
353
|
static tryParse(dxn) {
|
|
346
354
|
try {
|
|
347
355
|
return _DXN.parse(dxn);
|
|
348
|
-
} catch
|
|
356
|
+
} catch {
|
|
349
357
|
return void 0;
|
|
350
358
|
}
|
|
351
359
|
}
|
|
352
360
|
/**
|
|
353
|
-
* @example `dxn:type:example.
|
|
361
|
+
* @example `dxn:type:com.example.type.person`
|
|
354
362
|
*/
|
|
355
363
|
static fromTypename(typename) {
|
|
356
364
|
return new _DXN(_DXN.kind.TYPE, [
|
|
@@ -358,7 +366,7 @@ var DXN = class _DXN {
|
|
|
358
366
|
]);
|
|
359
367
|
}
|
|
360
368
|
/**
|
|
361
|
-
* @example `dxn:type:example.
|
|
369
|
+
* @example `dxn:type:com.example.type.person:0.1.0`
|
|
362
370
|
*/
|
|
363
371
|
// TODO(dmaretskyi): Consider using @ as the version separator.
|
|
364
372
|
static fromTypenameAndVersion(typename, version) {
|
|
@@ -368,42 +376,30 @@ var DXN = class _DXN {
|
|
|
368
376
|
]);
|
|
369
377
|
}
|
|
370
378
|
/**
|
|
379
|
+
* @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
|
|
380
|
+
*/
|
|
381
|
+
static fromSpaceAndObjectId(spaceId, objectId) {
|
|
382
|
+
assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
|
|
383
|
+
assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
|
|
384
|
+
return new _DXN(_DXN.kind.ECHO, [
|
|
385
|
+
spaceId,
|
|
386
|
+
objectId
|
|
387
|
+
]);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
371
390
|
* @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
|
|
372
391
|
*/
|
|
373
392
|
static fromLocalObjectId(id) {
|
|
393
|
+
assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
|
|
374
394
|
return new _DXN(_DXN.kind.ECHO, [
|
|
375
395
|
LOCAL_SPACE_TAG,
|
|
376
396
|
id
|
|
377
397
|
]);
|
|
378
398
|
}
|
|
379
399
|
static fromQueue(subspaceTag, spaceId, queueId, objectId) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
S: this,
|
|
384
|
-
A: [
|
|
385
|
-
"SpaceId.isValid(spaceId)",
|
|
386
|
-
""
|
|
387
|
-
]
|
|
388
|
-
});
|
|
389
|
-
invariant2(ObjectId.isValid(queueId), void 0, {
|
|
390
|
-
F: __dxlog_file2,
|
|
391
|
-
L: 151,
|
|
392
|
-
S: this,
|
|
393
|
-
A: [
|
|
394
|
-
"ObjectId.isValid(queueId)",
|
|
395
|
-
""
|
|
396
|
-
]
|
|
397
|
-
});
|
|
398
|
-
invariant2(!objectId || ObjectId.isValid(objectId), void 0, {
|
|
399
|
-
F: __dxlog_file2,
|
|
400
|
-
L: 152,
|
|
401
|
-
S: this,
|
|
402
|
-
A: [
|
|
403
|
-
"!objectId || ObjectId.isValid(objectId)",
|
|
404
|
-
""
|
|
405
|
-
]
|
|
406
|
-
});
|
|
400
|
+
assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
|
|
401
|
+
assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
|
|
402
|
+
assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
|
|
407
403
|
return new _DXN(_DXN.kind.QUEUE, [
|
|
408
404
|
subspaceTag,
|
|
409
405
|
spaceId,
|
|
@@ -416,33 +412,17 @@ var DXN = class _DXN {
|
|
|
416
412
|
#kind;
|
|
417
413
|
#parts;
|
|
418
414
|
constructor(kind, parts) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
L: 161,
|
|
422
|
-
S: this,
|
|
423
|
-
A: [
|
|
424
|
-
"parts.length > 0",
|
|
425
|
-
""
|
|
426
|
-
]
|
|
427
|
-
});
|
|
428
|
-
invariant2(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
|
|
429
|
-
F: __dxlog_file2,
|
|
430
|
-
L: 162,
|
|
431
|
-
S: this,
|
|
432
|
-
A: [
|
|
433
|
-
"parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
|
|
434
|
-
""
|
|
435
|
-
]
|
|
436
|
-
});
|
|
415
|
+
assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
|
|
416
|
+
assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
|
|
437
417
|
switch (kind) {
|
|
438
418
|
case _DXN.kind.TYPE:
|
|
439
419
|
if (parts.length > 2) {
|
|
440
|
-
throw new Error(
|
|
420
|
+
throw new Error("Invalid DXN.kind.TYPE");
|
|
441
421
|
}
|
|
442
422
|
break;
|
|
443
423
|
case _DXN.kind.ECHO:
|
|
444
424
|
if (parts.length !== 2) {
|
|
445
|
-
throw new Error(
|
|
425
|
+
throw new Error("Invalid DXN.kind.ECHO");
|
|
446
426
|
}
|
|
447
427
|
break;
|
|
448
428
|
}
|
|
@@ -477,22 +457,20 @@ var DXN = class _DXN {
|
|
|
477
457
|
}
|
|
478
458
|
};
|
|
479
459
|
}
|
|
460
|
+
get kind() {
|
|
461
|
+
return this.#kind;
|
|
462
|
+
}
|
|
480
463
|
get parts() {
|
|
481
464
|
return this.#parts;
|
|
482
465
|
}
|
|
483
466
|
// TODO(burdon): Should getters fail?
|
|
484
467
|
get typename() {
|
|
485
|
-
invariant2(this.#kind === _DXN.kind.TYPE
|
|
486
|
-
F: __dxlog_file2,
|
|
487
|
-
L: 217,
|
|
488
|
-
S: this,
|
|
489
|
-
A: [
|
|
490
|
-
"this.#kind === DXN.kind.TYPE",
|
|
491
|
-
""
|
|
492
|
-
]
|
|
493
|
-
});
|
|
468
|
+
invariant2(this.#kind === _DXN.kind.TYPE);
|
|
494
469
|
return this.#parts[0];
|
|
495
470
|
}
|
|
471
|
+
equals(other) {
|
|
472
|
+
return _DXN.equals(this, other);
|
|
473
|
+
}
|
|
496
474
|
hasTypenameOf(typename) {
|
|
497
475
|
return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
|
|
498
476
|
}
|
|
@@ -505,6 +483,7 @@ var DXN = class _DXN {
|
|
|
505
483
|
}
|
|
506
484
|
const [type, version] = this.#parts;
|
|
507
485
|
return {
|
|
486
|
+
// TODO(wittjosiah): Should be `typename` for consistency.
|
|
508
487
|
type,
|
|
509
488
|
version
|
|
510
489
|
};
|
|
@@ -516,6 +495,7 @@ var DXN = class _DXN {
|
|
|
516
495
|
const [spaceId, echoId] = this.#parts;
|
|
517
496
|
return {
|
|
518
497
|
spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
|
|
498
|
+
// TODO(burdon): objectId.
|
|
519
499
|
echoId
|
|
520
500
|
};
|
|
521
501
|
}
|
|
@@ -534,46 +514,30 @@ var DXN = class _DXN {
|
|
|
534
514
|
objectId
|
|
535
515
|
};
|
|
536
516
|
}
|
|
517
|
+
/**
|
|
518
|
+
* Produces a new DXN with the given parts appended.
|
|
519
|
+
*/
|
|
520
|
+
extend(parts) {
|
|
521
|
+
return new _DXN(this.#kind, [
|
|
522
|
+
...this.#parts,
|
|
523
|
+
...parts
|
|
524
|
+
]);
|
|
525
|
+
}
|
|
537
526
|
};
|
|
538
527
|
|
|
539
528
|
// src/identity-did.ts
|
|
540
529
|
init_inject_globals();
|
|
541
530
|
var import_base32_decode2 = __toESM(require_base32_decode(), 1);
|
|
542
531
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
543
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/keys/src/identity-did.ts";
|
|
544
532
|
var IdentityDid = Object.freeze({
|
|
545
533
|
byteLength: 20,
|
|
546
534
|
encode: (value) => {
|
|
547
|
-
invariant3(value instanceof Uint8Array, "Invalid type"
|
|
548
|
-
|
|
549
|
-
L: 22,
|
|
550
|
-
S: void 0,
|
|
551
|
-
A: [
|
|
552
|
-
"value instanceof Uint8Array",
|
|
553
|
-
"'Invalid type'"
|
|
554
|
-
]
|
|
555
|
-
});
|
|
556
|
-
invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
|
|
557
|
-
F: __dxlog_file3,
|
|
558
|
-
L: 23,
|
|
559
|
-
S: void 0,
|
|
560
|
-
A: [
|
|
561
|
-
"value.length === IdentityDid.byteLength",
|
|
562
|
-
"'Invalid length'"
|
|
563
|
-
]
|
|
564
|
-
});
|
|
535
|
+
invariant3(value instanceof Uint8Array, "Invalid type");
|
|
536
|
+
invariant3(value.length === IdentityDid.byteLength, "Invalid length");
|
|
565
537
|
return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
|
|
566
538
|
},
|
|
567
539
|
decode: (value) => {
|
|
568
|
-
invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding"
|
|
569
|
-
F: __dxlog_file3,
|
|
570
|
-
L: 28,
|
|
571
|
-
S: void 0,
|
|
572
|
-
A: [
|
|
573
|
-
"value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
|
|
574
|
-
"'Invalid multibase32 encoding'"
|
|
575
|
-
]
|
|
576
|
-
});
|
|
540
|
+
invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding");
|
|
577
541
|
return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
|
|
578
542
|
},
|
|
579
543
|
isValid: (value) => {
|
|
@@ -592,29 +556,19 @@ init_inject_globals();
|
|
|
592
556
|
var import_base32_decode3 = __toESM(require_base32_decode(), 1);
|
|
593
557
|
import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
|
|
594
558
|
import { invariant as invariant4 } from "@dxos/invariant";
|
|
595
|
-
var __dxlog_file4 = "/__w/dxos/dxos/packages/common/keys/src/public-key.ts";
|
|
596
559
|
var PUBLIC_KEY_LENGTH = 32;
|
|
597
560
|
var SECRET_KEY_LENGTH = 64;
|
|
598
561
|
var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
|
|
599
562
|
var PublicKey = class _PublicKey {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
}
|
|
563
|
+
_value;
|
|
564
|
+
static ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
|
|
603
565
|
/**
|
|
604
566
|
* Creates new instance of PublicKey automatically determining the input format.
|
|
605
567
|
* @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
|
|
606
568
|
* @returns PublicKey
|
|
607
569
|
*/
|
|
608
570
|
static from(source) {
|
|
609
|
-
invariant4(source
|
|
610
|
-
F: __dxlog_file4,
|
|
611
|
-
L: 49,
|
|
612
|
-
S: this,
|
|
613
|
-
A: [
|
|
614
|
-
"source",
|
|
615
|
-
""
|
|
616
|
-
]
|
|
617
|
-
});
|
|
571
|
+
invariant4(source);
|
|
618
572
|
if (source instanceof _PublicKey) {
|
|
619
573
|
return source;
|
|
620
574
|
} else if (source instanceof Buffer2) {
|
|
@@ -698,15 +652,7 @@ var PublicKey = class _PublicKey {
|
|
|
698
652
|
* @deprecated All keys should be represented as instances of PublicKey.
|
|
699
653
|
*/
|
|
700
654
|
static bufferize(str) {
|
|
701
|
-
invariant4(typeof str === "string", "Invalid type"
|
|
702
|
-
F: __dxlog_file4,
|
|
703
|
-
L: 152,
|
|
704
|
-
S: this,
|
|
705
|
-
A: [
|
|
706
|
-
"typeof str === 'string'",
|
|
707
|
-
"'Invalid type'"
|
|
708
|
-
]
|
|
709
|
-
});
|
|
655
|
+
invariant4(typeof str === "string", "Invalid type");
|
|
710
656
|
const buffer = Buffer2.from(str, "hex");
|
|
711
657
|
return buffer;
|
|
712
658
|
}
|
|
@@ -721,15 +667,7 @@ var PublicKey = class _PublicKey {
|
|
|
721
667
|
} else if (key instanceof Uint8Array) {
|
|
722
668
|
key = Buffer2.from(key.buffer, key.byteOffset, key.byteLength);
|
|
723
669
|
}
|
|
724
|
-
invariant4(key instanceof Buffer2, "Invalid type"
|
|
725
|
-
F: __dxlog_file4,
|
|
726
|
-
L: 171,
|
|
727
|
-
S: this,
|
|
728
|
-
A: [
|
|
729
|
-
"key instanceof Buffer",
|
|
730
|
-
"'Invalid type'"
|
|
731
|
-
]
|
|
732
|
-
});
|
|
670
|
+
invariant4(key instanceof Buffer2, "Invalid type");
|
|
733
671
|
return key.toString("hex");
|
|
734
672
|
}
|
|
735
673
|
/**
|
|
@@ -740,15 +678,7 @@ var PublicKey = class _PublicKey {
|
|
|
740
678
|
return key.toHex();
|
|
741
679
|
}
|
|
742
680
|
static fromMultibase32(encoded) {
|
|
743
|
-
invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding"
|
|
744
|
-
F: __dxlog_file4,
|
|
745
|
-
L: 184,
|
|
746
|
-
S: this,
|
|
747
|
-
A: [
|
|
748
|
-
"encoded.startsWith('B')",
|
|
749
|
-
"'Invalid multibase32 encoding'"
|
|
750
|
-
]
|
|
751
|
-
});
|
|
681
|
+
invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding");
|
|
752
682
|
return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
|
|
753
683
|
}
|
|
754
684
|
constructor(_value) {
|
|
@@ -775,7 +705,7 @@ var PublicKey = class _PublicKey {
|
|
|
775
705
|
toMultibase32() {
|
|
776
706
|
return "B" + base32Encode(this._value, "RFC4648");
|
|
777
707
|
}
|
|
778
|
-
truncate(length
|
|
708
|
+
truncate(length) {
|
|
779
709
|
return truncateKey(this, length);
|
|
780
710
|
}
|
|
781
711
|
asBuffer() {
|
|
@@ -881,6 +811,7 @@ var PublicKey = class _PublicKey {
|
|
|
881
811
|
};
|
|
882
812
|
export {
|
|
883
813
|
DXN,
|
|
814
|
+
DXN_ECHO_REGEXP,
|
|
884
815
|
IdentityDid,
|
|
885
816
|
LOCAL_SPACE_TAG,
|
|
886
817
|
ObjectId,
|
|
@@ -888,6 +819,7 @@ export {
|
|
|
888
819
|
PublicKey,
|
|
889
820
|
QueueSubspaceTags,
|
|
890
821
|
SECRET_KEY_LENGTH,
|
|
822
|
+
SimplePRNG,
|
|
891
823
|
SpaceId
|
|
892
824
|
};
|
|
893
825
|
//# sourceMappingURL=index.mjs.map
|