@dxos/keys 0.6.13 → 0.6.14-main.7bd9c89

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.
@@ -1 +1 @@
1
- {"inputs":{"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js":{"bytes":1217,"imports":[],"format":"cjs"},"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js":{"bytes":410,"imports":[],"format":"esm"},"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js":{"bytes":1266,"imports":[{"path":"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js","kind":"import-statement","original":"to-data-view"}],"format":"esm"},"packages/common/keys/src/random-bytes.ts":{"bytes":1792,"imports":[{"path":"node:crypto","kind":"require-call","external":true}],"format":"esm"},"packages/common/keys/src/public-key.ts":{"bytes":30405,"imports":[{"path":"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js","kind":"import-statement","original":"base32-decode"},{"path":"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js","kind":"import-statement","original":"base32-encode"},{"path":"node:util","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/common/keys/src/random-bytes.ts","kind":"import-statement","original":"./random-bytes"}],"format":"esm"},"packages/common/keys/src/types.ts":{"bytes":528,"imports":[],"format":"esm"},"packages/common/keys/src/space-id.ts":{"bytes":5162,"imports":[{"path":"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js","kind":"import-statement","original":"base32-decode"},{"path":"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js","kind":"import-statement","original":"base32-encode"},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/common/keys/src/random-bytes.ts","kind":"import-statement","original":"./random-bytes"}],"format":"esm"},"packages/common/keys/src/dxn.ts":{"bytes":8722,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/keys/src/index.ts":{"bytes":746,"imports":[{"path":"packages/common/keys/src/public-key.ts","kind":"import-statement","original":"./public-key"},{"path":"packages/common/keys/src/types.ts","kind":"import-statement","original":"./types"},{"path":"packages/common/keys/src/space-id.ts","kind":"import-statement","original":"./space-id"},{"path":"packages/common/keys/src/dxn.ts","kind":"import-statement","original":"./dxn"}],"format":"esm"}},"outputs":{"packages/common/keys/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":26396},"packages/common/keys/dist/lib/node/index.cjs":{"imports":[{"path":"node:util","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"node:crypto","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["DXN","LOCAL_SPACE_TAG","PUBLIC_KEY_LENGTH","PublicKey","SECRET_KEY_LENGTH","SpaceId"],"entryPoint":"packages/common/keys/src/index.ts","inputs":{"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js":{"bytesInOutput":1554},"packages/common/keys/src/public-key.ts":{"bytesInOutput":7723},"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js":{"bytesInOutput":395},"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js":{"bytesInOutput":1206},"packages/common/keys/src/random-bytes.ts":{"bytesInOutput":204},"packages/common/keys/src/index.ts":{"bytesInOutput":0},"packages/common/keys/src/space-id.ts":{"bytesInOutput":1431},"packages/common/keys/src/dxn.ts":{"bytesInOutput":2074}},"bytes":16897}}}
1
+ {"inputs":{"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js":{"bytes":1217,"imports":[],"format":"cjs"},"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js":{"bytes":410,"imports":[],"format":"esm"},"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js":{"bytes":1266,"imports":[{"path":"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js","kind":"import-statement","original":"to-data-view"}],"format":"esm"},"packages/common/keys/src/random-bytes.ts":{"bytes":1792,"imports":[{"path":"node:crypto","kind":"require-call","external":true}],"format":"esm"},"packages/common/keys/src/public-key.ts":{"bytes":31290,"imports":[{"path":"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js","kind":"import-statement","original":"base32-decode"},{"path":"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js","kind":"import-statement","original":"base32-encode"},{"path":"node:util","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/common/keys/src/random-bytes.ts","kind":"import-statement","original":"./random-bytes"}],"format":"esm"},"packages/common/keys/src/types.ts":{"bytes":528,"imports":[],"format":"esm"},"packages/common/keys/src/space-id.ts":{"bytes":5162,"imports":[{"path":"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js","kind":"import-statement","original":"base32-decode"},{"path":"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js","kind":"import-statement","original":"base32-encode"},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/common/keys/src/random-bytes.ts","kind":"import-statement","original":"./random-bytes"}],"format":"esm"},"packages/common/keys/src/dxn.ts":{"bytes":10230,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/keys/src/index.ts":{"bytes":746,"imports":[{"path":"packages/common/keys/src/public-key.ts","kind":"import-statement","original":"./public-key"},{"path":"packages/common/keys/src/types.ts","kind":"import-statement","original":"./types"},{"path":"packages/common/keys/src/space-id.ts","kind":"import-statement","original":"./space-id"},{"path":"packages/common/keys/src/dxn.ts","kind":"import-statement","original":"./dxn"}],"format":"esm"}},"outputs":{"packages/common/keys/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":27806},"packages/common/keys/dist/lib/node/index.cjs":{"imports":[{"path":"node:util","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"node:crypto","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["DXN","LOCAL_SPACE_TAG","PUBLIC_KEY_LENGTH","PublicKey","SECRET_KEY_LENGTH","SpaceId"],"entryPoint":"packages/common/keys/src/index.ts","inputs":{"node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js":{"bytesInOutput":1554},"packages/common/keys/src/public-key.ts":{"bytesInOutput":7899},"node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js":{"bytesInOutput":395},"node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js":{"bytesInOutput":1206},"packages/common/keys/src/random-bytes.ts":{"bytesInOutput":204},"packages/common/keys/src/index.ts":{"bytesInOutput":0},"packages/common/keys/src/space-id.ts":{"bytesInOutput":1431},"packages/common/keys/src/dxn.ts":{"bytesInOutput":2259}},"bytes":17258}}}
@@ -0,0 +1,596 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
9
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
10
+ }) : x)(function(x) {
11
+ if (typeof require !== "undefined") return require.apply(this, arguments);
12
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
+ });
14
+ var __commonJS = (cb, mod) => function __require2() {
15
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
31
+ mod
32
+ ));
33
+
34
+ // node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js
35
+ var require_base32_decode = __commonJS({
36
+ "node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js"(exports, module) {
37
+ var RFC46482 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
38
+ var RFC4648_HEX2 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
39
+ var CROCKFORD2 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
40
+ function readChar(alphabet, char) {
41
+ var idx = alphabet.indexOf(char);
42
+ if (idx === -1) {
43
+ throw new Error("Invalid character found: " + char);
44
+ }
45
+ return idx;
46
+ }
47
+ module.exports = function base32Decode3(input, variant) {
48
+ var alphabet;
49
+ switch (variant) {
50
+ case "RFC3548":
51
+ case "RFC4648":
52
+ alphabet = RFC46482;
53
+ input = input.replace(/=+$/, "");
54
+ break;
55
+ case "RFC4648-HEX":
56
+ alphabet = RFC4648_HEX2;
57
+ input = input.replace(/=+$/, "");
58
+ break;
59
+ case "Crockford":
60
+ alphabet = CROCKFORD2;
61
+ input = input.toUpperCase().replace(/O/g, "0").replace(/[IL]/g, "1");
62
+ break;
63
+ default:
64
+ throw new Error("Unknown base32 variant: " + variant);
65
+ }
66
+ var length = input.length;
67
+ var bits = 0;
68
+ var value = 0;
69
+ var index = 0;
70
+ var output = new Uint8Array(length * 5 / 8 | 0);
71
+ for (var i = 0; i < length; i++) {
72
+ value = value << 5 | readChar(alphabet, input[i]);
73
+ bits += 5;
74
+ if (bits >= 8) {
75
+ output[index++] = value >>> bits - 8 & 255;
76
+ bits -= 8;
77
+ }
78
+ }
79
+ return output.buffer;
80
+ };
81
+ }
82
+ });
83
+
84
+ // packages/common/keys/src/public-key.ts
85
+ var import_base32_decode = __toESM(require_base32_decode());
86
+
87
+ // node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
88
+ function toDataView(data) {
89
+ if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
90
+ return new DataView(data.buffer, data.byteOffset, data.byteLength);
91
+ }
92
+ if (data instanceof ArrayBuffer) {
93
+ return new DataView(data);
94
+ }
95
+ throw new TypeError("Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray");
96
+ }
97
+
98
+ // node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js
99
+ var RFC4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
100
+ var RFC4648_HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
101
+ var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
102
+ function base32Encode(data, variant, options) {
103
+ options = options || {};
104
+ let alphabet, defaultPadding;
105
+ switch (variant) {
106
+ case "RFC3548":
107
+ case "RFC4648":
108
+ alphabet = RFC4648;
109
+ defaultPadding = true;
110
+ break;
111
+ case "RFC4648-HEX":
112
+ alphabet = RFC4648_HEX;
113
+ defaultPadding = true;
114
+ break;
115
+ case "Crockford":
116
+ alphabet = CROCKFORD;
117
+ defaultPadding = false;
118
+ break;
119
+ default:
120
+ throw new Error("Unknown base32 variant: " + variant);
121
+ }
122
+ const padding = options.padding !== void 0 ? options.padding : defaultPadding;
123
+ const view = toDataView(data);
124
+ let bits = 0;
125
+ let value = 0;
126
+ let output = "";
127
+ for (let i = 0; i < view.byteLength; i++) {
128
+ value = value << 8 | view.getUint8(i);
129
+ bits += 8;
130
+ while (bits >= 5) {
131
+ output += alphabet[value >>> bits - 5 & 31];
132
+ bits -= 5;
133
+ }
134
+ }
135
+ if (bits > 0) {
136
+ output += alphabet[value << 5 - bits & 31];
137
+ }
138
+ if (padding) {
139
+ while (output.length % 8 !== 0) {
140
+ output += "=";
141
+ }
142
+ }
143
+ return output;
144
+ }
145
+
146
+ // packages/common/keys/src/public-key.ts
147
+ import { inspect } from "node:util";
148
+ import { truncateKey, devtoolsFormatter, equalsSymbol } from "@dxos/debug";
149
+ import { invariant } from "@dxos/invariant";
150
+
151
+ // packages/common/keys/src/random-bytes.ts
152
+ var randomBytes = (length) => {
153
+ const webCrypto = globalThis.crypto ?? __require("node:crypto").webcrypto;
154
+ const bytes = new Uint8Array(length);
155
+ webCrypto.getRandomValues(bytes);
156
+ return bytes;
157
+ };
158
+
159
+ // packages/common/keys/src/public-key.ts
160
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
161
+ var PUBLIC_KEY_LENGTH = 32;
162
+ var SECRET_KEY_LENGTH = 64;
163
+ var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
164
+ var PublicKey = class _PublicKey {
165
+ static {
166
+ this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
167
+ }
168
+ /**
169
+ * Creates new instance of PublicKey automatically determining the input format.
170
+ * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
171
+ * @returns PublicKey
172
+ */
173
+ static from(source) {
174
+ invariant(source, void 0, {
175
+ F: __dxlog_file,
176
+ L: 42,
177
+ S: this,
178
+ A: [
179
+ "source",
180
+ ""
181
+ ]
182
+ });
183
+ if (source instanceof _PublicKey) {
184
+ return source;
185
+ } else if (source instanceof Buffer) {
186
+ return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
187
+ } else if (source instanceof Uint8Array) {
188
+ return new _PublicKey(source);
189
+ } else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {
190
+ return new _PublicKey(new Uint8Array(source));
191
+ } else if (typeof source === "string") {
192
+ return _PublicKey.fromHex(source);
193
+ } else if (source.asUint8Array) {
194
+ return new _PublicKey(source.asUint8Array());
195
+ } else {
196
+ throw new TypeError(`Unable to create PublicKey from ${source}`);
197
+ }
198
+ }
199
+ /**
200
+ * Same as `PublicKey.from` but does not throw and instead returns a `{ key: PublicKey }` or `{ error: Error }`
201
+ * @param source Same PublicKeyLike argument as for `PublicKey.from`
202
+ * @returns PublicKey
203
+ */
204
+ static safeFrom(source) {
205
+ if (!source) {
206
+ return void 0;
207
+ }
208
+ try {
209
+ const key = _PublicKey.from(source);
210
+ return key;
211
+ } catch (err) {
212
+ return void 0;
213
+ }
214
+ }
215
+ /**
216
+ * Creates new instance of PublicKey from hex string.
217
+ */
218
+ static fromHex(hex) {
219
+ if (hex.startsWith("0x")) {
220
+ hex = hex.slice(2);
221
+ }
222
+ const buf = Buffer.from(hex, "hex");
223
+ return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
224
+ }
225
+ /**
226
+ * Creates a new key.
227
+ */
228
+ static random() {
229
+ return _PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
230
+ }
231
+ static randomOfLength(length) {
232
+ return _PublicKey.from(randomBytes(length));
233
+ }
234
+ static *randomSequence() {
235
+ for (let i = 0; i < 1e4; i++) {
236
+ yield _PublicKey.random();
237
+ }
238
+ throw new Error("Too many keys requested");
239
+ }
240
+ /**
241
+ * Tests if provided values is an instance of PublicKey.
242
+ */
243
+ static isPublicKey(value) {
244
+ return value instanceof _PublicKey;
245
+ }
246
+ /**
247
+ * Asserts that provided values is an instance of PublicKey.
248
+ */
249
+ static assertValidPublicKey(value) {
250
+ if (!this.isPublicKey(value)) {
251
+ throw new TypeError("Invalid PublicKey");
252
+ }
253
+ }
254
+ /**
255
+ * Tests two keys for equality.
256
+ */
257
+ static equals(left, right) {
258
+ return _PublicKey.from(left).equals(right);
259
+ }
260
+ /**
261
+ * @param str string representation of key.
262
+ * @return Key buffer.
263
+ * @deprecated All keys should be represented as instances of PublicKey.
264
+ */
265
+ static bufferize(str) {
266
+ invariant(typeof str === "string", "Invalid type", {
267
+ F: __dxlog_file,
268
+ L: 145,
269
+ S: this,
270
+ A: [
271
+ "typeof str === 'string'",
272
+ "'Invalid type'"
273
+ ]
274
+ });
275
+ const buffer = Buffer.from(str, "hex");
276
+ return buffer;
277
+ }
278
+ /**
279
+ * @param key key like data structure (but not PublicKey which should use toString).
280
+ * @return Hex string representation of key.
281
+ * @deprecated All keys should be represented as instances of PublicKey.
282
+ */
283
+ static stringify(key) {
284
+ if (key instanceof _PublicKey) {
285
+ key = key.asBuffer();
286
+ } else if (key instanceof Uint8Array) {
287
+ key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
288
+ }
289
+ invariant(key instanceof Buffer, "Invalid type", {
290
+ F: __dxlog_file,
291
+ L: 164,
292
+ S: this,
293
+ A: [
294
+ "key instanceof Buffer",
295
+ "'Invalid type'"
296
+ ]
297
+ });
298
+ return key.toString("hex");
299
+ }
300
+ /**
301
+ * To be used with ComplexMap and ComplexSet.
302
+ * Returns a scalar representation for this key.
303
+ */
304
+ static hash(key) {
305
+ return key.toHex();
306
+ }
307
+ static fromMultibase32(encoded) {
308
+ invariant(encoded.startsWith("B"), "Invalid multibase32 encoding", {
309
+ F: __dxlog_file,
310
+ L: 177,
311
+ S: this,
312
+ A: [
313
+ "encoded.startsWith('B')",
314
+ "'Invalid multibase32 encoding'"
315
+ ]
316
+ });
317
+ return new _PublicKey(new Uint8Array((0, import_base32_decode.default)(encoded.slice(1), "RFC4648")));
318
+ }
319
+ constructor(_value) {
320
+ this._value = _value;
321
+ if (!(_value instanceof Uint8Array)) {
322
+ throw new TypeError(`Expected Uint8Array, got: ${_value}`);
323
+ }
324
+ }
325
+ toString() {
326
+ return this.toHex();
327
+ }
328
+ toJSON() {
329
+ return this.toHex();
330
+ }
331
+ toJSONL() {
332
+ return this.truncate();
333
+ }
334
+ get length() {
335
+ return this._value.length;
336
+ }
337
+ toHex() {
338
+ return this.asBuffer().toString("hex");
339
+ }
340
+ toMultibase32() {
341
+ return "B" + base32Encode(this._value, "RFC4648");
342
+ }
343
+ truncate(length = void 0) {
344
+ return truncateKey(this, length);
345
+ }
346
+ asBuffer() {
347
+ return Buffer.from(this._value.buffer, this._value.byteOffset, this._value.byteLength);
348
+ }
349
+ asUint8Array() {
350
+ return this._value;
351
+ }
352
+ getInsecureHash(modulo) {
353
+ return Math.abs(this._value.reduce((acc, val) => acc ^ val | 0, 0)) % modulo;
354
+ }
355
+ /**
356
+ * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
357
+ */
358
+ [inspect.custom](depth, options) {
359
+ if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
360
+ return `<PublicKey ${this.truncate()}>`;
361
+ }
362
+ const printControlCode = (code) => {
363
+ return `\x1B[${code}m`;
364
+ };
365
+ const colors = [
366
+ "red",
367
+ "green",
368
+ "yellow",
369
+ "blue",
370
+ "magenta",
371
+ "cyan",
372
+ "redBright",
373
+ "greenBright",
374
+ "yellowBright",
375
+ "blueBright",
376
+ "magentaBright",
377
+ "cyanBright",
378
+ "whiteBright"
379
+ ];
380
+ const color = colors[this.getInsecureHash(colors.length)];
381
+ return `PublicKey(${printControlCode(inspect.colors[color][0])}${this.truncate()}${printControlCode(inspect.colors.reset[0])})`;
382
+ }
383
+ get [devtoolsFormatter]() {
384
+ return {
385
+ header: () => {
386
+ const colors = [
387
+ "darkred",
388
+ "green",
389
+ "orange",
390
+ "blue",
391
+ "darkmagenta",
392
+ "darkcyan",
393
+ "red",
394
+ "green",
395
+ "orange",
396
+ "blue",
397
+ "magenta",
398
+ "darkcyan",
399
+ "black"
400
+ ];
401
+ const color = colors[this.getInsecureHash(colors.length)];
402
+ return [
403
+ "span",
404
+ {},
405
+ [
406
+ "span",
407
+ {},
408
+ "PublicKey("
409
+ ],
410
+ [
411
+ "span",
412
+ {
413
+ style: `color: ${color};`
414
+ },
415
+ this.truncate()
416
+ ],
417
+ [
418
+ "span",
419
+ {},
420
+ ")"
421
+ ]
422
+ ];
423
+ }
424
+ };
425
+ }
426
+ /**
427
+ * Test this key for equality with some other key.
428
+ */
429
+ equals(other) {
430
+ const otherConverted = _PublicKey.from(other);
431
+ if (this._value.length !== otherConverted._value.length) {
432
+ return false;
433
+ }
434
+ let equal = true;
435
+ for (let i = 0; i < this._value.length; i++) {
436
+ equal &&= this._value[i] === otherConverted._value[i];
437
+ }
438
+ return equal;
439
+ }
440
+ [equalsSymbol](other) {
441
+ if (!_PublicKey.isPublicKey(other)) {
442
+ return false;
443
+ }
444
+ return this.equals(other);
445
+ }
446
+ };
447
+
448
+ // packages/common/keys/src/space-id.ts
449
+ var import_base32_decode2 = __toESM(require_base32_decode());
450
+ import { invariant as invariant2 } from "@dxos/invariant";
451
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/common/keys/src/space-id.ts";
452
+ var SpaceId = Object.freeze({
453
+ byteLength: 20,
454
+ encode: (value) => {
455
+ invariant2(value instanceof Uint8Array, "Invalid type", {
456
+ F: __dxlog_file2,
457
+ L: 22,
458
+ S: void 0,
459
+ A: [
460
+ "value instanceof Uint8Array",
461
+ "'Invalid type'"
462
+ ]
463
+ });
464
+ invariant2(value.length === SpaceId.byteLength, "Invalid length", {
465
+ F: __dxlog_file2,
466
+ L: 23,
467
+ S: void 0,
468
+ A: [
469
+ "value.length === SpaceId.byteLength",
470
+ "'Invalid length'"
471
+ ]
472
+ });
473
+ return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
474
+ },
475
+ decode: (value) => {
476
+ invariant2(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
477
+ F: __dxlog_file2,
478
+ L: 28,
479
+ S: void 0,
480
+ A: [
481
+ "value.startsWith(MULTIBASE_PREFIX)",
482
+ "'Invalid multibase32 encoding'"
483
+ ]
484
+ });
485
+ return new Uint8Array((0, import_base32_decode2.default)(value.slice(1), "RFC4648"));
486
+ },
487
+ isValid: (value) => {
488
+ return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
489
+ },
490
+ random: () => {
491
+ return SpaceId.encode(randomBytes(SpaceId.byteLength));
492
+ }
493
+ });
494
+ var MULTIBASE_PREFIX = "B";
495
+ var ENCODED_LENGTH = 33;
496
+
497
+ // packages/common/keys/src/dxn.ts
498
+ import { invariant as invariant3 } from "@dxos/invariant";
499
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
500
+ var DXN = class _DXN {
501
+ static {
502
+ /**
503
+ * Kind constants.
504
+ */
505
+ this.kind = Object.freeze({
506
+ ECHO: "echo",
507
+ TYPE: "type"
508
+ });
509
+ }
510
+ static parse(dxn) {
511
+ if (typeof dxn !== "string") {
512
+ throw new Error("Invalid DXN");
513
+ }
514
+ const [prefix, kind, ...parts] = dxn.split(":");
515
+ if (!(prefix === "dxn")) {
516
+ throw new Error("Invalid DXN");
517
+ }
518
+ if (!(typeof kind === "string" && kind.length > 0)) {
519
+ throw new Error("Invalid DXN");
520
+ }
521
+ if (!(parts.length > 0)) {
522
+ throw new Error("Invalid DXN");
523
+ }
524
+ return new _DXN(kind, parts);
525
+ }
526
+ static isDXNString(dxn) {
527
+ return dxn.startsWith("dxn:");
528
+ }
529
+ static localEchoObjectDXN(id) {
530
+ return new _DXN(_DXN.kind.ECHO, [
531
+ LOCAL_SPACE_TAG,
532
+ id
533
+ ]);
534
+ }
535
+ #kind;
536
+ #parts;
537
+ constructor(kind, parts) {
538
+ invariant3(parts.length > 0, void 0, {
539
+ F: __dxlog_file3,
540
+ L: 62,
541
+ S: this,
542
+ A: [
543
+ "parts.length > 0",
544
+ ""
545
+ ]
546
+ });
547
+ invariant3(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
548
+ F: __dxlog_file3,
549
+ L: 63,
550
+ S: this,
551
+ A: [
552
+ "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
553
+ ""
554
+ ]
555
+ });
556
+ switch (kind) {
557
+ case _DXN.kind.ECHO:
558
+ if (parts.length !== 2) {
559
+ throw new Error('Invalid "echo" DXN');
560
+ }
561
+ break;
562
+ case _DXN.kind.TYPE:
563
+ if (parts.length !== 1) {
564
+ throw new Error('Invalid "type" DXN');
565
+ }
566
+ break;
567
+ }
568
+ this.#kind = kind;
569
+ this.#parts = parts;
570
+ }
571
+ get kind() {
572
+ return this.#kind;
573
+ }
574
+ get parts() {
575
+ return this.#parts;
576
+ }
577
+ isTypeDXNOf(typename) {
578
+ return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
579
+ }
580
+ isLocalEchoObjectDXN() {
581
+ return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
582
+ }
583
+ toString() {
584
+ return `dxn:${this.#kind}:${this.#parts.join(":")}`;
585
+ }
586
+ };
587
+ var LOCAL_SPACE_TAG = "@";
588
+ export {
589
+ DXN,
590
+ LOCAL_SPACE_TAG,
591
+ PUBLIC_KEY_LENGTH,
592
+ PublicKey,
593
+ SECRET_KEY_LENGTH,
594
+ SpaceId
595
+ };
596
+ //# sourceMappingURL=index.mjs.map