@dxos/keys 0.6.13 → 0.6.14-main.2b6a0f3
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 +50 -29
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +49 -28
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +613 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/dxn.d.ts +11 -0
- package/dist/types/src/dxn.d.ts.map +1 -1
- package/dist/types/src/public-key.d.ts +3 -3
- package/dist/types/src/public-key.d.ts.map +1 -1
- package/package.json +7 -5
- package/src/dxn.ts +45 -2
- package/src/public-key.test.ts +1 -2
- package/src/public-key.ts +20 -7
|
@@ -143,8 +143,7 @@ function base32Encode(data, variant, options) {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
// packages/common/keys/src/public-key.ts
|
|
146
|
-
import {
|
|
147
|
-
import { truncateKey, devtoolsFormatter, equalsSymbol } from "@dxos/debug";
|
|
146
|
+
import { devtoolsFormatter, equalsSymbol, inspectCustom, truncateKey } from "@dxos/debug";
|
|
148
147
|
import { invariant } from "@dxos/invariant";
|
|
149
148
|
|
|
150
149
|
// packages/common/keys/src/random-bytes.ts
|
|
@@ -159,6 +158,7 @@ var randomBytes = (length) => {
|
|
|
159
158
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
|
|
160
159
|
var PUBLIC_KEY_LENGTH = 32;
|
|
161
160
|
var SECRET_KEY_LENGTH = 64;
|
|
161
|
+
var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
|
|
162
162
|
var PublicKey = class _PublicKey {
|
|
163
163
|
static {
|
|
164
164
|
this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
|
|
@@ -171,7 +171,7 @@ var PublicKey = class _PublicKey {
|
|
|
171
171
|
static from(source) {
|
|
172
172
|
invariant(source, void 0, {
|
|
173
173
|
F: __dxlog_file,
|
|
174
|
-
L:
|
|
174
|
+
L: 49,
|
|
175
175
|
S: this,
|
|
176
176
|
A: [
|
|
177
177
|
"source",
|
|
@@ -184,7 +184,7 @@ var PublicKey = class _PublicKey {
|
|
|
184
184
|
return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
|
|
185
185
|
} else if (source instanceof Uint8Array) {
|
|
186
186
|
return new _PublicKey(source);
|
|
187
|
-
} else if (source instanceof ArrayBuffer) {
|
|
187
|
+
} else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {
|
|
188
188
|
return new _PublicKey(new Uint8Array(source));
|
|
189
189
|
} else if (typeof source === "string") {
|
|
190
190
|
return _PublicKey.fromHex(source);
|
|
@@ -263,7 +263,7 @@ var PublicKey = class _PublicKey {
|
|
|
263
263
|
static bufferize(str) {
|
|
264
264
|
invariant(typeof str === "string", "Invalid type", {
|
|
265
265
|
F: __dxlog_file,
|
|
266
|
-
L:
|
|
266
|
+
L: 152,
|
|
267
267
|
S: this,
|
|
268
268
|
A: [
|
|
269
269
|
"typeof str === 'string'",
|
|
@@ -286,7 +286,7 @@ var PublicKey = class _PublicKey {
|
|
|
286
286
|
}
|
|
287
287
|
invariant(key instanceof Buffer, "Invalid type", {
|
|
288
288
|
F: __dxlog_file,
|
|
289
|
-
L:
|
|
289
|
+
L: 171,
|
|
290
290
|
S: this,
|
|
291
291
|
A: [
|
|
292
292
|
"key instanceof Buffer",
|
|
@@ -305,7 +305,7 @@ var PublicKey = class _PublicKey {
|
|
|
305
305
|
static fromMultibase32(encoded) {
|
|
306
306
|
invariant(encoded.startsWith("B"), "Invalid multibase32 encoding", {
|
|
307
307
|
F: __dxlog_file,
|
|
308
|
-
L:
|
|
308
|
+
L: 184,
|
|
309
309
|
S: this,
|
|
310
310
|
A: [
|
|
311
311
|
"encoded.startsWith('B')",
|
|
@@ -353,7 +353,7 @@ var PublicKey = class _PublicKey {
|
|
|
353
353
|
/**
|
|
354
354
|
* Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
|
|
355
355
|
*/
|
|
356
|
-
[
|
|
356
|
+
[inspectCustom](depth, options, inspectFn) {
|
|
357
357
|
if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
|
|
358
358
|
return `<PublicKey ${this.truncate()}>`;
|
|
359
359
|
}
|
|
@@ -376,7 +376,7 @@ var PublicKey = class _PublicKey {
|
|
|
376
376
|
"whiteBright"
|
|
377
377
|
];
|
|
378
378
|
const color = colors[this.getInsecureHash(colors.length)];
|
|
379
|
-
return `PublicKey(${printControlCode(
|
|
379
|
+
return `PublicKey(${printControlCode(inspectFn.colors[color][0])}${this.truncate()}${printControlCode(inspectFn.colors.reset[0])})`;
|
|
380
380
|
}
|
|
381
381
|
get [devtoolsFormatter]() {
|
|
382
382
|
return {
|
|
@@ -493,6 +493,7 @@ var MULTIBASE_PREFIX = "B";
|
|
|
493
493
|
var ENCODED_LENGTH = 33;
|
|
494
494
|
|
|
495
495
|
// packages/common/keys/src/dxn.ts
|
|
496
|
+
import { inspectCustom as inspectCustom2 } from "@dxos/debug";
|
|
496
497
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
497
498
|
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
|
|
498
499
|
var DXN = class _DXN {
|
|
@@ -506,6 +507,9 @@ var DXN = class _DXN {
|
|
|
506
507
|
});
|
|
507
508
|
}
|
|
508
509
|
static parse(dxn) {
|
|
510
|
+
if (typeof dxn !== "string") {
|
|
511
|
+
throw new Error("Invalid DXN");
|
|
512
|
+
}
|
|
509
513
|
const [prefix, kind, ...parts] = dxn.split(":");
|
|
510
514
|
if (!(prefix === "dxn")) {
|
|
511
515
|
throw new Error("Invalid DXN");
|
|
@@ -518,12 +522,29 @@ var DXN = class _DXN {
|
|
|
518
522
|
}
|
|
519
523
|
return new _DXN(kind, parts);
|
|
520
524
|
}
|
|
525
|
+
static equals(a, b) {
|
|
526
|
+
return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
|
|
527
|
+
}
|
|
528
|
+
static isDXNString(dxn) {
|
|
529
|
+
return dxn.startsWith("dxn:");
|
|
530
|
+
}
|
|
531
|
+
static typename(type) {
|
|
532
|
+
return new _DXN(_DXN.kind.TYPE, [
|
|
533
|
+
type
|
|
534
|
+
]);
|
|
535
|
+
}
|
|
536
|
+
static localEchoObjectDXN(id) {
|
|
537
|
+
return new _DXN(_DXN.kind.ECHO, [
|
|
538
|
+
LOCAL_SPACE_TAG,
|
|
539
|
+
id
|
|
540
|
+
]);
|
|
541
|
+
}
|
|
521
542
|
#kind;
|
|
522
543
|
#parts;
|
|
523
544
|
constructor(kind, parts) {
|
|
524
545
|
invariant3(parts.length > 0, void 0, {
|
|
525
546
|
F: __dxlog_file3,
|
|
526
|
-
L:
|
|
547
|
+
L: 73,
|
|
527
548
|
S: this,
|
|
528
549
|
A: [
|
|
529
550
|
"parts.length > 0",
|
|
@@ -532,7 +553,7 @@ var DXN = class _DXN {
|
|
|
532
553
|
});
|
|
533
554
|
invariant3(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
|
|
534
555
|
F: __dxlog_file3,
|
|
535
|
-
L:
|
|
556
|
+
L: 74,
|
|
536
557
|
S: this,
|
|
537
558
|
A: [
|
|
538
559
|
"parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
|
|
@@ -541,26 +562,14 @@ var DXN = class _DXN {
|
|
|
541
562
|
});
|
|
542
563
|
switch (kind) {
|
|
543
564
|
case _DXN.kind.ECHO:
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
S: this,
|
|
548
|
-
A: [
|
|
549
|
-
"parts.length === 2",
|
|
550
|
-
""
|
|
551
|
-
]
|
|
552
|
-
});
|
|
565
|
+
if (parts.length !== 2) {
|
|
566
|
+
throw new Error('Invalid "echo" DXN');
|
|
567
|
+
}
|
|
553
568
|
break;
|
|
554
569
|
case _DXN.kind.TYPE:
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
S: this,
|
|
559
|
-
A: [
|
|
560
|
-
"parts.length === 1",
|
|
561
|
-
""
|
|
562
|
-
]
|
|
563
|
-
});
|
|
570
|
+
if (parts.length !== 1) {
|
|
571
|
+
throw new Error('Invalid "type" DXN');
|
|
572
|
+
}
|
|
564
573
|
break;
|
|
565
574
|
}
|
|
566
575
|
this.#kind = kind;
|
|
@@ -575,9 +584,21 @@ var DXN = class _DXN {
|
|
|
575
584
|
isTypeDXNOf(typename) {
|
|
576
585
|
return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
|
|
577
586
|
}
|
|
587
|
+
isLocalEchoObjectDXN() {
|
|
588
|
+
return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
|
|
589
|
+
}
|
|
578
590
|
toString() {
|
|
579
591
|
return `dxn:${this.#kind}:${this.#parts.join(":")}`;
|
|
580
592
|
}
|
|
593
|
+
/**
|
|
594
|
+
* Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
|
|
595
|
+
*/
|
|
596
|
+
[inspectCustom2](depth, options, inspectFn) {
|
|
597
|
+
const printControlCode = (code) => {
|
|
598
|
+
return `\x1B[${code}m`;
|
|
599
|
+
};
|
|
600
|
+
return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
|
|
601
|
+
}
|
|
581
602
|
};
|
|
582
603
|
var LOCAL_SPACE_TAG = "@";
|
|
583
604
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js", "../../../src/public-key.ts", "../../../../../../node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js", "../../../../../../node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js", "../../../src/random-bytes.ts", "../../../src/space-id.ts", "../../../src/dxn.ts"],
|
|
4
|
-
"sourcesContent": ["var RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'\nvar RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV'\nvar CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'\n\nfunction readChar (alphabet, char) {\n var idx = alphabet.indexOf(char)\n\n if (idx === -1) {\n throw new Error('Invalid character found: ' + char)\n }\n\n return idx\n}\n\nmodule.exports = function base32Decode (input, variant) {\n var alphabet\n\n switch (variant) {\n case 'RFC3548':\n case 'RFC4648':\n alphabet = RFC4648\n input = input.replace(/=+$/, '')\n break\n case 'RFC4648-HEX':\n alphabet = RFC4648_HEX\n input = input.replace(/=+$/, '')\n break\n case 'Crockford':\n alphabet = CROCKFORD\n input = input.toUpperCase().replace(/O/g, '0').replace(/[IL]/g, '1')\n break\n default:\n throw new Error('Unknown base32 variant: ' + variant)\n }\n\n var length = input.length\n\n var bits = 0\n var value = 0\n\n var index = 0\n var output = new Uint8Array((length * 5 / 8) | 0)\n\n for (var i = 0; i < length; i++) {\n value = (value << 5) | readChar(alphabet, input[i])\n bits += 5\n\n if (bits >= 8) {\n output[index++] = (value >>> (bits - 8)) & 255\n bits -= 8\n }\n }\n\n return output.buffer\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport base32Decode from 'base32-decode';\nimport base32Encode from 'base32-encode';\nimport { inspect, type InspectOptionsStylized } from 'node:util';\n\nimport { truncateKey, devtoolsFormatter, type DevtoolsFormatter, equalsSymbol, type Equatable } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\n\nimport { randomBytes } from './random-bytes';\n\nexport const PUBLIC_KEY_LENGTH = 32;\nexport const SECRET_KEY_LENGTH = 64;\n\n/**\n * All representations that can be converted to a PublicKey.\n */\nexport type PublicKeyLike = PublicKey | Buffer | Uint8Array | ArrayBuffer | string;\n\n/**\n * The purpose of this class is to assure consistent use of keys throughout the project.\n * Keys should be maintained as buffers in objects and proto definitions, and converted to hex\n * strings as late as possible (eg, to log/display).\n */\nexport class PublicKey implements Equatable {\n static ZERO = PublicKey.from('00'.repeat(PUBLIC_KEY_LENGTH));\n\n /**\n * Creates new instance of PublicKey automatically determining the input format.\n * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it\n * @returns PublicKey\n */\n static from(source: PublicKeyLike): PublicKey {\n invariant(source);\n if (source instanceof PublicKey) {\n return source;\n } else if (source instanceof Buffer) {\n return new PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));\n } else if (source instanceof Uint8Array) {\n return new PublicKey(source);\n } else if (source instanceof ArrayBuffer) {\n return new PublicKey(new Uint8Array(source));\n } else if (typeof source === 'string') {\n // TODO(burdon): Check length.\n return PublicKey.fromHex(source);\n } else if ((<any>source).asUint8Array) {\n return new PublicKey((<any>source).asUint8Array());\n } else {\n throw new TypeError(`Unable to create PublicKey from ${source}`);\n }\n }\n\n /**\n * Same as `PublicKey.from` but does not throw and instead returns a `{ key: PublicKey }` or `{ error: Error }`\n * @param source Same PublicKeyLike argument as for `PublicKey.from`\n * @returns PublicKey\n */\n static safeFrom(source?: PublicKeyLike): PublicKey | undefined {\n if (!source) {\n return undefined;\n }\n\n try {\n const key = PublicKey.from(source);\n // TODO(wittjosiah): Space keys don't pass this check.\n // if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {\n // return undefined;\n // }\n return key;\n } catch (err: any) {\n return undefined;\n }\n }\n\n /**\n * Creates new instance of PublicKey from hex string.\n */\n static fromHex(hex: string) {\n if (hex.startsWith('0x')) {\n hex = hex.slice(2);\n }\n\n const buf = Buffer.from(hex, 'hex');\n // TODO(burdon): Test if key.\n return new PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));\n }\n\n /**\n * Creates a new key.\n */\n static random(): PublicKey {\n // TODO(burdon): Enable seed for debugging.\n return PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));\n }\n\n static randomOfLength(length: number): PublicKey {\n return PublicKey.from(randomBytes(length));\n }\n\n static *randomSequence(): Generator<PublicKey> {\n for (let i = 0; i < 1_0000; i++) {\n // Counter just to protect against infinite loops.\n yield PublicKey.random();\n }\n throw new Error('Too many keys requested');\n }\n\n /**\n * Tests if provided values is an instance of PublicKey.\n */\n static isPublicKey(value: any): value is PublicKey {\n return value instanceof PublicKey;\n }\n\n /**\n * Asserts that provided values is an instance of PublicKey.\n */\n static assertValidPublicKey(value: any): asserts value is PublicKey {\n if (!this.isPublicKey(value)) {\n throw new TypeError('Invalid PublicKey');\n }\n }\n\n /**\n * Tests two keys for equality.\n */\n static equals(left: PublicKeyLike, right: PublicKeyLike) {\n return PublicKey.from(left).equals(right);\n }\n\n /**\n * @param str string representation of key.\n * @return Key buffer.\n * @deprecated All keys should be represented as instances of PublicKey.\n */\n static bufferize(str: string): Buffer {\n invariant(typeof str === 'string', 'Invalid type');\n const buffer = Buffer.from(str, 'hex');\n // invariant(buffer.length === PUBLIC_KEY_LENGTH || buffer.length === SECRET_KEY_LENGTH,\n // `Invalid key length: ${buffer.length}`);\n return buffer;\n }\n\n /**\n * @param key key like data structure (but not PublicKey which should use toString).\n * @return Hex string representation of key.\n * @deprecated All keys should be represented as instances of PublicKey.\n */\n static stringify(key: Buffer | Uint8Array | ArrayBuffer): string {\n if (key instanceof PublicKey) {\n key = key.asBuffer();\n } else if (key instanceof Uint8Array) {\n key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);\n }\n\n invariant(key instanceof Buffer, 'Invalid type');\n return key.toString('hex');\n }\n\n /**\n * To be used with ComplexMap and ComplexSet.\n * Returns a scalar representation for this key.\n */\n static hash(key: PublicKey): string {\n return key.toHex();\n }\n\n static fromMultibase32(encoded: string): PublicKey {\n invariant(encoded.startsWith('B'), 'Invalid multibase32 encoding');\n\n return new PublicKey(new Uint8Array(base32Decode(encoded.slice(1), 'RFC4648')));\n }\n\n constructor(private readonly _value: Uint8Array) {\n if (!(_value instanceof Uint8Array)) {\n throw new TypeError(`Expected Uint8Array, got: ${_value}`);\n }\n }\n\n toString(): string {\n return this.toHex();\n }\n\n toJSON() {\n return this.toHex();\n }\n\n toJSONL(): string {\n return this.truncate();\n }\n\n get length() {\n return this._value.length;\n }\n\n toHex(): string {\n return this.asBuffer().toString('hex');\n }\n\n toMultibase32(): string {\n return 'B' + base32Encode(this._value, 'RFC4648');\n }\n\n truncate(length = undefined) {\n return truncateKey(this, length);\n }\n\n asBuffer(): Buffer {\n return Buffer.from(this._value.buffer, this._value.byteOffset, this._value.byteLength);\n }\n\n asUint8Array(): Uint8Array {\n return this._value;\n }\n\n getInsecureHash(modulo: number) {\n return Math.abs(this._value.reduce((acc, val) => (acc ^ val) | 0, 0)) % modulo;\n }\n\n /**\n * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.\n */\n [inspect.custom](depth: number, options: InspectOptionsStylized) {\n if (!options.colors || typeof process.stdout.hasColors !== 'function' || !process.stdout.hasColors()) {\n return `<PublicKey ${this.truncate()}>`;\n }\n\n const printControlCode = (code: number) => {\n return `\\x1b[${code}m`;\n };\n\n // NOTE: Keep in sync with formatter colors.\n const colors = [\n 'red',\n 'green',\n 'yellow',\n 'blue',\n 'magenta',\n 'cyan',\n 'redBright',\n 'greenBright',\n 'yellowBright',\n 'blueBright',\n 'magentaBright',\n 'cyanBright',\n 'whiteBright',\n ];\n const color = colors[this.getInsecureHash(colors.length)];\n\n return `PublicKey(${printControlCode(inspect.colors[color]![0])}${this.truncate()}${printControlCode(\n inspect.colors.reset![0],\n )})`;\n }\n\n get [devtoolsFormatter](): DevtoolsFormatter {\n return {\n header: () => {\n // NOTE: Keep in sync with inspect colors.\n const colors = [\n 'darkred',\n 'green',\n 'orange',\n 'blue',\n 'darkmagenta',\n 'darkcyan',\n 'red',\n 'green',\n 'orange',\n 'blue',\n 'magenta',\n 'darkcyan',\n 'black',\n ];\n const color = colors[this.getInsecureHash(colors.length)];\n\n return [\n 'span',\n {},\n ['span', {}, 'PublicKey('],\n ['span', { style: `color: ${color};` }, this.truncate()],\n ['span', {}, ')'],\n ];\n },\n };\n }\n\n /**\n * Test this key for equality with some other key.\n */\n equals(other: PublicKeyLike) {\n const otherConverted = PublicKey.from(other);\n if (this._value.length !== otherConverted._value.length) {\n return false;\n }\n\n let equal = true;\n for (let i = 0; i < this._value.length; i++) {\n equal &&= this._value[i] === otherConverted._value[i];\n }\n\n return equal;\n }\n\n [equalsSymbol](other: any) {\n if (!PublicKey.isPublicKey(other)) {\n return false;\n }\n\n return this.equals(other);\n }\n}\n", "export default function toDataView (data) {\n if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {\n return new DataView(data.buffer, data.byteOffset, data.byteLength)\n }\n\n if (data instanceof ArrayBuffer) {\n return new DataView(data)\n }\n\n throw new TypeError('Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray')\n}\n", "import toDataView from 'to-data-view'\n\nconst RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'\nconst RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV'\nconst CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'\n\nexport default function base32Encode (data, variant, options) {\n options = options || {}\n let alphabet, defaultPadding\n\n switch (variant) {\n case 'RFC3548':\n case 'RFC4648':\n alphabet = RFC4648\n defaultPadding = true\n break\n case 'RFC4648-HEX':\n alphabet = RFC4648_HEX\n defaultPadding = true\n break\n case 'Crockford':\n alphabet = CROCKFORD\n defaultPadding = false\n break\n default:\n throw new Error('Unknown base32 variant: ' + variant)\n }\n\n const padding = (options.padding !== undefined ? options.padding : defaultPadding)\n const view = toDataView(data)\n\n let bits = 0\n let value = 0\n let output = ''\n\n for (let i = 0; i < view.byteLength; i++) {\n value = (value << 8) | view.getUint8(i)\n bits += 8\n\n while (bits >= 5) {\n output += alphabet[(value >>> (bits - 5)) & 31]\n bits -= 5\n }\n }\n\n if (bits > 0) {\n output += alphabet[(value << (5 - bits)) & 31]\n }\n\n if (padding) {\n while ((output.length % 8) !== 0) {\n output += '='\n }\n }\n\n return output\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const randomBytes = (length: number) => {\n // globalThis.crypto is not available in Node.js when running in vitest even though the documentation says it should be.\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto;\n\n const bytes = new Uint8Array(length);\n webCrypto.getRandomValues(bytes);\n return bytes;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport base32Decode from 'base32-decode';\nimport base32Encode from 'base32-encode';\n\nimport { invariant } from '@dxos/invariant';\n\nimport { randomBytes } from './random-bytes';\n\n/**\n * A unique identifier for a space.\n * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).\n * @example BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE\n */\nexport type SpaceId = string & { __SpaceId: true };\n\nexport const SpaceId = Object.freeze({\n byteLength: 20,\n encode: (value: Uint8Array): SpaceId => {\n invariant(value instanceof Uint8Array, 'Invalid type');\n invariant(value.length === SpaceId.byteLength, 'Invalid length');\n\n return (MULTIBASE_PREFIX + base32Encode(value, 'RFC4648')) as SpaceId;\n },\n decode: (value: SpaceId): Uint8Array => {\n invariant(value.startsWith(MULTIBASE_PREFIX), 'Invalid multibase32 encoding');\n\n return new Uint8Array(base32Decode(value.slice(1), 'RFC4648'));\n },\n isValid: (value: string): value is SpaceId => {\n return typeof value === 'string' && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;\n },\n random: (): SpaceId => {\n return SpaceId.encode(randomBytes(SpaceId.byteLength));\n },\n});\n\n/**\n * Denotes RFC4648 base-32 format.\n */\nconst MULTIBASE_PREFIX = 'B';\n\nconst ENCODED_LENGTH = 33;\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\n/**\n * DXN unambiguously names a resource like an ECHO object, schema definition, plugin, etc.\n * Each DXN starts with a dx prefix, followed by a resource kind.\n * Colon Symbol : is used a delimiter between parts.\n * DXNs may contain slashes.\n * '@' in the place of the space id is used to denote that the DXN should be resolved in the local space.\n *\n * @example\n *\n * ```\n * dx:echo:<space key>:<echo id>\n * dx:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6\n * dx:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6\n * dx:type:dxos.org/type/Calendar\n * dx:plugin:dxos.org/agent/plugin/functions\n * ```\n */\nexport class DXN {\n /**\n * Kind constants.\n */\n static kind = Object.freeze({\n ECHO: 'echo',\n TYPE: 'type',\n });\n\n static parse(dxn: string): DXN {\n const [prefix, kind, ...parts] = dxn.split(':');\n if (!(prefix === 'dxn')) {\n throw new Error('Invalid DXN');\n }\n if (!(typeof kind === 'string' && kind.length > 0)) {\n throw new Error('Invalid DXN');\n }\n if (!(parts.length > 0)) {\n throw new Error('Invalid DXN');\n }\n return new DXN(kind, parts);\n }\n\n #kind: string;\n #parts: string[];\n\n constructor(kind: string, parts: string[]) {\n invariant(parts.length > 0);\n invariant(parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1));\n\n // Per-type validation.\n switch (kind) {\n case DXN.kind.ECHO:\n invariant(parts.length === 2);\n break;\n case DXN.kind.TYPE:\n invariant(parts.length === 1);\n break;\n }\n\n this.#kind = kind;\n this.#parts = parts;\n }\n\n get kind() {\n return this.#kind;\n }\n\n get parts() {\n return this.#parts;\n }\n\n isTypeDXNOf(typename: string) {\n return this.#kind === DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;\n }\n\n toString() {\n return `dxn:${this.#kind}:${this.#parts.join(':')}`;\n }\n}\n\n/**\n * Tags for ECHO DXNs that should resolve the object ID in the local space.\n */\nexport const LOCAL_SPACE_TAG = '@';\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,QAAIA,WAAU;AACd,QAAIC,eAAc;AAClB,QAAIC,aAAY;AAEhB,aAAS,SAAU,UAAU,MAAM;AACjC,UAAI,MAAM,SAAS,QAAQ,IAAI;AAE/B,UAAI,QAAQ,IAAI;AACd,cAAM,IAAI,MAAM,8BAA8B,IAAI;AAAA,MACpD;AAEA,aAAO;AAAA,IACT;AAEA,WAAO,UAAU,SAASC,cAAc,OAAO,SAAS;AACtD,UAAI;AAEJ,cAAQ,SAAS;AAAA,QACf,KAAK;AAAA,QACL,KAAK;AACH,qBAAWH;AACX,kBAAQ,MAAM,QAAQ,OAAO,EAAE;AAC/B;AAAA,QACF,KAAK;AACH,qBAAWC;AACX,kBAAQ,MAAM,QAAQ,OAAO,EAAE;AAC/B;AAAA,QACF,KAAK;AACH,qBAAWC;AACX,kBAAQ,MAAM,YAAY,EAAE,QAAQ,MAAM,GAAG,EAAE,QAAQ,SAAS,GAAG;AACnE;AAAA,QACF;AACE,gBAAM,IAAI,MAAM,6BAA6B,OAAO;AAAA,MACxD;AAEA,UAAI,SAAS,MAAM;AAEnB,UAAI,OAAO;AACX,UAAI,QAAQ;AAEZ,UAAI,QAAQ;AACZ,UAAI,SAAS,IAAI,WAAY,SAAS,IAAI,IAAK,CAAC;AAEhD,eAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,gBAAS,SAAS,IAAK,SAAS,UAAU,MAAM,CAAC,CAAC;AAClD,gBAAQ;AAER,YAAI,QAAQ,GAAG;AACb,iBAAO,OAAO,IAAK,UAAW,OAAO,IAAM;AAC3C,kBAAQ;AAAA,Q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AAEA,SAAO;AACT;;;
|
|
6
|
-
"names": ["RFC4648", "RFC4648_HEX", "CROCKFORD", "base32Decode", "
|
|
4
|
+
"sourcesContent": ["var RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'\nvar RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV'\nvar CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'\n\nfunction readChar (alphabet, char) {\n var idx = alphabet.indexOf(char)\n\n if (idx === -1) {\n throw new Error('Invalid character found: ' + char)\n }\n\n return idx\n}\n\nmodule.exports = function base32Decode (input, variant) {\n var alphabet\n\n switch (variant) {\n case 'RFC3548':\n case 'RFC4648':\n alphabet = RFC4648\n input = input.replace(/=+$/, '')\n break\n case 'RFC4648-HEX':\n alphabet = RFC4648_HEX\n input = input.replace(/=+$/, '')\n break\n case 'Crockford':\n alphabet = CROCKFORD\n input = input.toUpperCase().replace(/O/g, '0').replace(/[IL]/g, '1')\n break\n default:\n throw new Error('Unknown base32 variant: ' + variant)\n }\n\n var length = input.length\n\n var bits = 0\n var value = 0\n\n var index = 0\n var output = new Uint8Array((length * 5 / 8) | 0)\n\n for (var i = 0; i < length; i++) {\n value = (value << 5) | readChar(alphabet, input[i])\n bits += 5\n\n if (bits >= 8) {\n output[index++] = (value >>> (bits - 8)) & 255\n bits -= 8\n }\n }\n\n return output.buffer\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport base32Decode from 'base32-decode';\nimport base32Encode from 'base32-encode';\nimport { type inspect, type InspectOptionsStylized } from 'node:util';\n\nimport {\n devtoolsFormatter,\n type DevtoolsFormatter,\n equalsSymbol,\n type Equatable,\n inspectCustom,\n truncateKey,\n} from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\n\nimport { randomBytes } from './random-bytes';\n\nexport const PUBLIC_KEY_LENGTH = 32;\nexport const SECRET_KEY_LENGTH = 64;\n\n/**\n * All representations that can be converted to a PublicKey.\n */\nexport type PublicKeyLike = PublicKey | Buffer | Uint8Array | ArrayBuffer | string;\n\n/**\n * Vitest with JSDom causes instanceof ArrayBuffer check to fail\n */\nconst isLikeArrayBuffer = (value: any): value is ArrayBuffer =>\n typeof value === 'object' && value !== null && Object.getPrototypeOf(value).constructor.name === 'ArrayBuffer';\n\n/**\n * The purpose of this class is to assure consistent use of keys throughout the project.\n * Keys should be maintained as buffers in objects and proto definitions, and converted to hex\n * strings as late as possible (eg, to log/display).\n */\nexport class PublicKey implements Equatable {\n static ZERO = PublicKey.from('00'.repeat(PUBLIC_KEY_LENGTH));\n\n /**\n * Creates new instance of PublicKey automatically determining the input format.\n * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it\n * @returns PublicKey\n */\n static from(source: PublicKeyLike): PublicKey {\n invariant(source);\n if (source instanceof PublicKey) {\n return source;\n } else if (source instanceof Buffer) {\n return new PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));\n } else if (source instanceof Uint8Array) {\n return new PublicKey(source);\n } else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {\n return new PublicKey(new Uint8Array(source));\n } else if (typeof source === 'string') {\n // TODO(burdon): Check length.\n return PublicKey.fromHex(source);\n } else if ((<any>source).asUint8Array) {\n return new PublicKey((<any>source).asUint8Array());\n } else {\n throw new TypeError(`Unable to create PublicKey from ${source}`);\n }\n }\n\n /**\n * Same as `PublicKey.from` but does not throw and instead returns a `{ key: PublicKey }` or `{ error: Error }`\n * @param source Same PublicKeyLike argument as for `PublicKey.from`\n * @returns PublicKey\n */\n static safeFrom(source?: PublicKeyLike): PublicKey | undefined {\n if (!source) {\n return undefined;\n }\n\n try {\n const key = PublicKey.from(source);\n // TODO(wittjosiah): Space keys don't pass this check.\n // if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {\n // return undefined;\n // }\n return key;\n } catch (err: any) {\n return undefined;\n }\n }\n\n /**\n * Creates new instance of PublicKey from hex string.\n */\n static fromHex(hex: string) {\n if (hex.startsWith('0x')) {\n hex = hex.slice(2);\n }\n\n const buf = Buffer.from(hex, 'hex');\n // TODO(burdon): Test if key.\n return new PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));\n }\n\n /**\n * Creates a new key.\n */\n static random(): PublicKey {\n // TODO(burdon): Enable seed for debugging.\n return PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));\n }\n\n static randomOfLength(length: number): PublicKey {\n return PublicKey.from(randomBytes(length));\n }\n\n static *randomSequence(): Generator<PublicKey> {\n for (let i = 0; i < 1_0000; i++) {\n // Counter just to protect against infinite loops.\n yield PublicKey.random();\n }\n throw new Error('Too many keys requested');\n }\n\n /**\n * Tests if provided values is an instance of PublicKey.\n */\n static isPublicKey(value: any): value is PublicKey {\n return value instanceof PublicKey;\n }\n\n /**\n * Asserts that provided values is an instance of PublicKey.\n */\n static assertValidPublicKey(value: any): asserts value is PublicKey {\n if (!this.isPublicKey(value)) {\n throw new TypeError('Invalid PublicKey');\n }\n }\n\n /**\n * Tests two keys for equality.\n */\n static equals(left: PublicKeyLike, right: PublicKeyLike) {\n return PublicKey.from(left).equals(right);\n }\n\n /**\n * @param str string representation of key.\n * @return Key buffer.\n * @deprecated All keys should be represented as instances of PublicKey.\n */\n static bufferize(str: string): Buffer {\n invariant(typeof str === 'string', 'Invalid type');\n const buffer = Buffer.from(str, 'hex');\n // invariant(buffer.length === PUBLIC_KEY_LENGTH || buffer.length === SECRET_KEY_LENGTH,\n // `Invalid key length: ${buffer.length}`);\n return buffer;\n }\n\n /**\n * @param key key like data structure (but not PublicKey which should use toString).\n * @return Hex string representation of key.\n * @deprecated All keys should be represented as instances of PublicKey.\n */\n static stringify(key: Buffer | Uint8Array | ArrayBuffer): string {\n if (key instanceof PublicKey) {\n key = key.asBuffer();\n } else if (key instanceof Uint8Array) {\n key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);\n }\n\n invariant(key instanceof Buffer, 'Invalid type');\n return key.toString('hex');\n }\n\n /**\n * To be used with ComplexMap and ComplexSet.\n * Returns a scalar representation for this key.\n */\n static hash(key: PublicKey): string {\n return key.toHex();\n }\n\n static fromMultibase32(encoded: string): PublicKey {\n invariant(encoded.startsWith('B'), 'Invalid multibase32 encoding');\n\n return new PublicKey(new Uint8Array(base32Decode(encoded.slice(1), 'RFC4648')));\n }\n\n constructor(private readonly _value: Uint8Array) {\n if (!(_value instanceof Uint8Array)) {\n throw new TypeError(`Expected Uint8Array, got: ${_value}`);\n }\n }\n\n toString(): string {\n return this.toHex();\n }\n\n toJSON() {\n return this.toHex();\n }\n\n toJSONL(): string {\n return this.truncate();\n }\n\n get length() {\n return this._value.length;\n }\n\n toHex(): string {\n return this.asBuffer().toString('hex');\n }\n\n toMultibase32(): string {\n return 'B' + base32Encode(this._value, 'RFC4648');\n }\n\n truncate(length = undefined) {\n return truncateKey(this, length);\n }\n\n asBuffer(): Buffer {\n return Buffer.from(this._value.buffer, this._value.byteOffset, this._value.byteLength);\n }\n\n asUint8Array(): Uint8Array {\n return this._value;\n }\n\n getInsecureHash(modulo: number) {\n return Math.abs(this._value.reduce((acc, val) => (acc ^ val) | 0, 0)) % modulo;\n }\n\n /**\n * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.\n */\n [inspectCustom](depth: number, options: InspectOptionsStylized, inspectFn: typeof inspect) {\n if (!options.colors || typeof process.stdout.hasColors !== 'function' || !process.stdout.hasColors()) {\n return `<PublicKey ${this.truncate()}>`;\n }\n\n const printControlCode = (code: number) => {\n return `\\x1b[${code}m`;\n };\n\n // NOTE: Keep in sync with formatter colors.\n const colors = [\n 'red',\n 'green',\n 'yellow',\n 'blue',\n 'magenta',\n 'cyan',\n 'redBright',\n 'greenBright',\n 'yellowBright',\n 'blueBright',\n 'magentaBright',\n 'cyanBright',\n 'whiteBright',\n ];\n const color = colors[this.getInsecureHash(colors.length)];\n\n return `PublicKey(${printControlCode(inspectFn.colors[color]![0])}${this.truncate()}${printControlCode(\n inspectFn.colors.reset![0],\n )})`;\n }\n\n get [devtoolsFormatter](): DevtoolsFormatter {\n return {\n header: () => {\n // NOTE: Keep in sync with inspect colors.\n const colors = [\n 'darkred',\n 'green',\n 'orange',\n 'blue',\n 'darkmagenta',\n 'darkcyan',\n 'red',\n 'green',\n 'orange',\n 'blue',\n 'magenta',\n 'darkcyan',\n 'black',\n ];\n const color = colors[this.getInsecureHash(colors.length)];\n\n return [\n 'span',\n {},\n ['span', {}, 'PublicKey('],\n ['span', { style: `color: ${color};` }, this.truncate()],\n ['span', {}, ')'],\n ];\n },\n };\n }\n\n /**\n * Test this key for equality with some other key.\n */\n equals(other: PublicKeyLike) {\n const otherConverted = PublicKey.from(other);\n if (this._value.length !== otherConverted._value.length) {\n return false;\n }\n\n let equal = true;\n for (let i = 0; i < this._value.length; i++) {\n equal &&= this._value[i] === otherConverted._value[i];\n }\n\n return equal;\n }\n\n [equalsSymbol](other: any) {\n if (!PublicKey.isPublicKey(other)) {\n return false;\n }\n\n return this.equals(other);\n }\n}\n", "export default function toDataView (data) {\n if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {\n return new DataView(data.buffer, data.byteOffset, data.byteLength)\n }\n\n if (data instanceof ArrayBuffer) {\n return new DataView(data)\n }\n\n throw new TypeError('Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray')\n}\n", "import toDataView from 'to-data-view'\n\nconst RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'\nconst RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV'\nconst CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'\n\nexport default function base32Encode (data, variant, options) {\n options = options || {}\n let alphabet, defaultPadding\n\n switch (variant) {\n case 'RFC3548':\n case 'RFC4648':\n alphabet = RFC4648\n defaultPadding = true\n break\n case 'RFC4648-HEX':\n alphabet = RFC4648_HEX\n defaultPadding = true\n break\n case 'Crockford':\n alphabet = CROCKFORD\n defaultPadding = false\n break\n default:\n throw new Error('Unknown base32 variant: ' + variant)\n }\n\n const padding = (options.padding !== undefined ? options.padding : defaultPadding)\n const view = toDataView(data)\n\n let bits = 0\n let value = 0\n let output = ''\n\n for (let i = 0; i < view.byteLength; i++) {\n value = (value << 8) | view.getUint8(i)\n bits += 8\n\n while (bits >= 5) {\n output += alphabet[(value >>> (bits - 5)) & 31]\n bits -= 5\n }\n }\n\n if (bits > 0) {\n output += alphabet[(value << (5 - bits)) & 31]\n }\n\n if (padding) {\n while ((output.length % 8) !== 0) {\n output += '='\n }\n }\n\n return output\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const randomBytes = (length: number) => {\n // globalThis.crypto is not available in Node.js when running in vitest even though the documentation says it should be.\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto;\n\n const bytes = new Uint8Array(length);\n webCrypto.getRandomValues(bytes);\n return bytes;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport base32Decode from 'base32-decode';\nimport base32Encode from 'base32-encode';\n\nimport { invariant } from '@dxos/invariant';\n\nimport { randomBytes } from './random-bytes';\n\n/**\n * A unique identifier for a space.\n * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).\n * @example BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE\n */\nexport type SpaceId = string & { __SpaceId: true };\n\nexport const SpaceId = Object.freeze({\n byteLength: 20,\n encode: (value: Uint8Array): SpaceId => {\n invariant(value instanceof Uint8Array, 'Invalid type');\n invariant(value.length === SpaceId.byteLength, 'Invalid length');\n\n return (MULTIBASE_PREFIX + base32Encode(value, 'RFC4648')) as SpaceId;\n },\n decode: (value: SpaceId): Uint8Array => {\n invariant(value.startsWith(MULTIBASE_PREFIX), 'Invalid multibase32 encoding');\n\n return new Uint8Array(base32Decode(value.slice(1), 'RFC4648'));\n },\n isValid: (value: string): value is SpaceId => {\n return typeof value === 'string' && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;\n },\n random: (): SpaceId => {\n return SpaceId.encode(randomBytes(SpaceId.byteLength));\n },\n});\n\n/**\n * Denotes RFC4648 base-32 format.\n */\nconst MULTIBASE_PREFIX = 'B';\n\nconst ENCODED_LENGTH = 33;\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport type { inspect, InspectOptionsStylized } from 'node:util';\n\nimport { inspectCustom } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\n\n/**\n * DXN unambiguously names a resource like an ECHO object, schema definition, plugin, etc.\n * Each DXN starts with a dx prefix, followed by a resource kind.\n * Colon Symbol : is used a delimiter between parts.\n * DXNs may contain slashes.\n * '@' in the place of the space id is used to denote that the DXN should be resolved in the local space.\n *\n * @example\n *\n * ```\n * dx:echo:<space key>:<echo id>\n * dx:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6\n * dx:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6\n * dx:type:dxos.org/type/Calendar\n * dx:plugin:dxos.org/agent/plugin/functions\n * ```\n */\nexport class DXN {\n /**\n * Kind constants.\n */\n static kind = Object.freeze({\n ECHO: 'echo',\n TYPE: 'type',\n });\n\n static parse(dxn: string): DXN {\n if (typeof dxn !== 'string') {\n throw new Error('Invalid DXN');\n }\n const [prefix, kind, ...parts] = dxn.split(':');\n if (!(prefix === 'dxn')) {\n throw new Error('Invalid DXN');\n }\n if (!(typeof kind === 'string' && kind.length > 0)) {\n throw new Error('Invalid DXN');\n }\n if (!(parts.length > 0)) {\n throw new Error('Invalid DXN');\n }\n return new DXN(kind, parts);\n }\n\n static equals(a: DXN, b: DXN) {\n return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);\n }\n\n static isDXNString(dxn: string) {\n return dxn.startsWith('dxn:');\n }\n\n static typename(type: string) {\n return new DXN(DXN.kind.TYPE, [type]);\n }\n\n static localEchoObjectDXN(id: string) {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, id]);\n }\n\n #kind: string;\n #parts: string[];\n\n constructor(kind: string, parts: string[]) {\n invariant(parts.length > 0);\n invariant(parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1));\n\n // Per-type validation.\n switch (kind) {\n case DXN.kind.ECHO:\n if (parts.length !== 2) {\n throw new Error('Invalid \"echo\" DXN');\n }\n break;\n case DXN.kind.TYPE:\n if (parts.length !== 1) {\n throw new Error('Invalid \"type\" DXN');\n }\n break;\n }\n\n this.#kind = kind;\n this.#parts = parts;\n }\n\n get kind() {\n return this.#kind;\n }\n\n get parts() {\n return this.#parts;\n }\n\n isTypeDXNOf(typename: string) {\n return this.#kind === DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;\n }\n\n isLocalEchoObjectDXN() {\n return this.#kind === DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;\n }\n\n toString() {\n return `dxn:${this.#kind}:${this.#parts.join(':')}`;\n }\n\n /**\n * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.\n */\n [inspectCustom](depth: number, options: InspectOptionsStylized, inspectFn: typeof inspect) {\n const printControlCode = (code: number) => {\n return `\\x1b[${code}m`;\n };\n\n return (\n printControlCode(inspectFn.colors.blueBright![0]) + this.toString() + printControlCode(inspectFn.colors.reset![0])\n );\n }\n}\n\n/**\n * Tags for ECHO DXNs that should resolve the object ID in the local space.\n */\nexport const LOCAL_SPACE_TAG = '@';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,QAAIA,WAAU;AACd,QAAIC,eAAc;AAClB,QAAIC,aAAY;AAEhB,aAAS,SAAU,UAAU,MAAM;AACjC,UAAI,MAAM,SAAS,QAAQ,IAAI;AAE/B,UAAI,QAAQ,IAAI;AACd,cAAM,IAAI,MAAM,8BAA8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O,CAAA,MAAOqB,mBAAmB,KAAK,OAAOb,WAAW;EACtG;EAEAkB,WAAW;AACT,WAAO,OAAO,KAAK,KAAK,IAAI,KAAK,OAAOC,KAAK,GAAA,CAAA;EAC/C;;;;EAKA,CAACjC,cAAAA,EAAekC,OAAeC,SAAiCC,WAA2B;AACzF,UAAMC,mBAAmB,CAACC,SAAAA;AACxB,aAAO,QAAQA,IAAAA;IACjB;AAEA,WACED,iBAAiBD,UAAUG,OAAOC,WAAY,CAAA,CAAE,IAAI,KAAKR,SAAQ,IAAKK,iBAAiBD,UAAUG,OAAOE,MAAO,CAAA,CAAE;EAErH;AACF;AAKO,IAAMd,kBAAkB;",
|
|
6
|
+
"names": ["RFC4648", "RFC4648_HEX", "CROCKFORD", "base32Decode", "devtoolsFormatter", "equalsSymbol", "inspectCustom", "truncateKey", "invariant", "randomBytes", "length", "webCrypto", "globalThis", "crypto", "require", "webcrypto", "bytes", "Uint8Array", "getRandomValues", "PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "isLikeArrayBuffer", "value", "Object", "getPrototypeOf", "constructor", "name", "PublicKey", "ZERO", "from", "repeat", "source", "invariant", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "key", "err", "hex", "startsWith", "slice", "buf", "random", "randomBytes", "randomOfLength", "length", "randomSequence", "i", "Error", "isPublicKey", "assertValidPublicKey", "equals", "left", "right", "bufferize", "str", "stringify", "asBuffer", "toString", "hash", "toHex", "fromMultibase32", "encoded", "base32Decode", "_value", "toJSON", "toJSONL", "truncate", "toMultibase32", "base32Encode", "truncateKey", "getInsecureHash", "modulo", "Math", "abs", "reduce", "acc", "val", "inspectCustom", "depth", "options", "inspectFn", "colors", "process", "stdout", "hasColors", "printControlCode", "code", "color", "reset", "devtoolsFormatter", "header", "style", "other", "otherConverted", "equal", "equalsSymbol", "import_base32_decode", "invariant", "SpaceId", "Object", "freeze", "byteLength", "encode", "value", "invariant", "Uint8Array", "length", "MULTIBASE_PREFIX", "base32Encode", "decode", "startsWith", "base32Decode", "slice", "isValid", "ENCODED_LENGTH", "random", "randomBytes", "inspectCustom", "invariant", "DXN", "kind", "Object", "freeze", "ECHO", "TYPE", "parse", "dxn", "Error", "prefix", "parts", "split", "length", "equals", "a", "b", "every", "part", "i", "isDXNString", "startsWith", "typename", "type", "localEchoObjectDXN", "id", "LOCAL_SPACE_TAG", "constructor", "indexOf", "isTypeDXNOf", "isLocalEchoObjectDXN", "toString", "join", "depth", "options", "inspectFn", "printControlCode", "code", "colors", "blueBright", "reset"]
|
|
7
7
|
}
|
|
@@ -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":"@dxos/node-std/crypto","kind":"require-call","external":true}],"format":"esm"},"packages/common/keys/src/public-key.ts":{"bytes":
|
|
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":"@dxos/node-std/crypto","kind":"require-call","external":true}],"format":"esm"},"packages/common/keys/src/public-key.ts":{"bytes":31358,"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/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":13066,"imports":[{"path":"@dxos/debug","kind":"import-statement","external":true},{"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/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":29352},"packages/common/keys/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/node-std/crypto","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/debug","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":7891},"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":214},"packages/common/keys/src/index.ts":{"bytesInOutput":0},"packages/common/keys/src/space-id.ts":{"bytesInOutput":1431},"packages/common/keys/src/dxn.ts":{"bytesInOutput":2936}},"bytes":17937}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -26,10 +26,10 @@ __export(node_exports, {
|
|
|
26
26
|
SpaceId: () => SpaceId
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(node_exports);
|
|
29
|
-
var import_node_util = require("node:util");
|
|
30
29
|
var import_debug = require("@dxos/debug");
|
|
31
30
|
var import_invariant = require("@dxos/invariant");
|
|
32
31
|
var import_invariant2 = require("@dxos/invariant");
|
|
32
|
+
var import_debug2 = require("@dxos/debug");
|
|
33
33
|
var import_invariant3 = require("@dxos/invariant");
|
|
34
34
|
var __create = Object.create;
|
|
35
35
|
var __defProp2 = Object.defineProperty;
|
|
@@ -175,6 +175,7 @@ var randomBytes = (length) => {
|
|
|
175
175
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
|
|
176
176
|
var PUBLIC_KEY_LENGTH = 32;
|
|
177
177
|
var SECRET_KEY_LENGTH = 64;
|
|
178
|
+
var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
|
|
178
179
|
var PublicKey = class _PublicKey {
|
|
179
180
|
static {
|
|
180
181
|
this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
|
|
@@ -187,7 +188,7 @@ var PublicKey = class _PublicKey {
|
|
|
187
188
|
static from(source) {
|
|
188
189
|
(0, import_invariant.invariant)(source, void 0, {
|
|
189
190
|
F: __dxlog_file,
|
|
190
|
-
L:
|
|
191
|
+
L: 49,
|
|
191
192
|
S: this,
|
|
192
193
|
A: [
|
|
193
194
|
"source",
|
|
@@ -200,7 +201,7 @@ var PublicKey = class _PublicKey {
|
|
|
200
201
|
return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
|
|
201
202
|
} else if (source instanceof Uint8Array) {
|
|
202
203
|
return new _PublicKey(source);
|
|
203
|
-
} else if (source instanceof ArrayBuffer) {
|
|
204
|
+
} else if (source instanceof ArrayBuffer || isLikeArrayBuffer(source)) {
|
|
204
205
|
return new _PublicKey(new Uint8Array(source));
|
|
205
206
|
} else if (typeof source === "string") {
|
|
206
207
|
return _PublicKey.fromHex(source);
|
|
@@ -279,7 +280,7 @@ var PublicKey = class _PublicKey {
|
|
|
279
280
|
static bufferize(str) {
|
|
280
281
|
(0, import_invariant.invariant)(typeof str === "string", "Invalid type", {
|
|
281
282
|
F: __dxlog_file,
|
|
282
|
-
L:
|
|
283
|
+
L: 152,
|
|
283
284
|
S: this,
|
|
284
285
|
A: [
|
|
285
286
|
"typeof str === 'string'",
|
|
@@ -302,7 +303,7 @@ var PublicKey = class _PublicKey {
|
|
|
302
303
|
}
|
|
303
304
|
(0, import_invariant.invariant)(key instanceof Buffer, "Invalid type", {
|
|
304
305
|
F: __dxlog_file,
|
|
305
|
-
L:
|
|
306
|
+
L: 171,
|
|
306
307
|
S: this,
|
|
307
308
|
A: [
|
|
308
309
|
"key instanceof Buffer",
|
|
@@ -321,7 +322,7 @@ var PublicKey = class _PublicKey {
|
|
|
321
322
|
static fromMultibase32(encoded) {
|
|
322
323
|
(0, import_invariant.invariant)(encoded.startsWith("B"), "Invalid multibase32 encoding", {
|
|
323
324
|
F: __dxlog_file,
|
|
324
|
-
L:
|
|
325
|
+
L: 184,
|
|
325
326
|
S: this,
|
|
326
327
|
A: [
|
|
327
328
|
"encoded.startsWith('B')",
|
|
@@ -369,7 +370,7 @@ var PublicKey = class _PublicKey {
|
|
|
369
370
|
/**
|
|
370
371
|
* Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
|
|
371
372
|
*/
|
|
372
|
-
[
|
|
373
|
+
[import_debug.inspectCustom](depth, options, inspectFn) {
|
|
373
374
|
if (!options.colors || typeof process.stdout.hasColors !== "function" || !process.stdout.hasColors()) {
|
|
374
375
|
return `<PublicKey ${this.truncate()}>`;
|
|
375
376
|
}
|
|
@@ -392,7 +393,7 @@ var PublicKey = class _PublicKey {
|
|
|
392
393
|
"whiteBright"
|
|
393
394
|
];
|
|
394
395
|
const color = colors[this.getInsecureHash(colors.length)];
|
|
395
|
-
return `PublicKey(${printControlCode(
|
|
396
|
+
return `PublicKey(${printControlCode(inspectFn.colors[color][0])}${this.truncate()}${printControlCode(inspectFn.colors.reset[0])})`;
|
|
396
397
|
}
|
|
397
398
|
get [import_debug.devtoolsFormatter]() {
|
|
398
399
|
return {
|
|
@@ -513,6 +514,9 @@ var DXN = class _DXN {
|
|
|
513
514
|
});
|
|
514
515
|
}
|
|
515
516
|
static parse(dxn) {
|
|
517
|
+
if (typeof dxn !== "string") {
|
|
518
|
+
throw new Error("Invalid DXN");
|
|
519
|
+
}
|
|
516
520
|
const [prefix, kind, ...parts] = dxn.split(":");
|
|
517
521
|
if (!(prefix === "dxn")) {
|
|
518
522
|
throw new Error("Invalid DXN");
|
|
@@ -525,12 +529,29 @@ var DXN = class _DXN {
|
|
|
525
529
|
}
|
|
526
530
|
return new _DXN(kind, parts);
|
|
527
531
|
}
|
|
532
|
+
static equals(a, b) {
|
|
533
|
+
return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
|
|
534
|
+
}
|
|
535
|
+
static isDXNString(dxn) {
|
|
536
|
+
return dxn.startsWith("dxn:");
|
|
537
|
+
}
|
|
538
|
+
static typename(type) {
|
|
539
|
+
return new _DXN(_DXN.kind.TYPE, [
|
|
540
|
+
type
|
|
541
|
+
]);
|
|
542
|
+
}
|
|
543
|
+
static localEchoObjectDXN(id) {
|
|
544
|
+
return new _DXN(_DXN.kind.ECHO, [
|
|
545
|
+
LOCAL_SPACE_TAG,
|
|
546
|
+
id
|
|
547
|
+
]);
|
|
548
|
+
}
|
|
528
549
|
#kind;
|
|
529
550
|
#parts;
|
|
530
551
|
constructor(kind, parts) {
|
|
531
552
|
(0, import_invariant3.invariant)(parts.length > 0, void 0, {
|
|
532
553
|
F: __dxlog_file3,
|
|
533
|
-
L:
|
|
554
|
+
L: 73,
|
|
534
555
|
S: this,
|
|
535
556
|
A: [
|
|
536
557
|
"parts.length > 0",
|
|
@@ -539,7 +560,7 @@ var DXN = class _DXN {
|
|
|
539
560
|
});
|
|
540
561
|
(0, import_invariant3.invariant)(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
|
|
541
562
|
F: __dxlog_file3,
|
|
542
|
-
L:
|
|
563
|
+
L: 74,
|
|
543
564
|
S: this,
|
|
544
565
|
A: [
|
|
545
566
|
"parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
|
|
@@ -548,26 +569,14 @@ var DXN = class _DXN {
|
|
|
548
569
|
});
|
|
549
570
|
switch (kind) {
|
|
550
571
|
case _DXN.kind.ECHO:
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
S: this,
|
|
555
|
-
A: [
|
|
556
|
-
"parts.length === 2",
|
|
557
|
-
""
|
|
558
|
-
]
|
|
559
|
-
});
|
|
572
|
+
if (parts.length !== 2) {
|
|
573
|
+
throw new Error('Invalid "echo" DXN');
|
|
574
|
+
}
|
|
560
575
|
break;
|
|
561
576
|
case _DXN.kind.TYPE:
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
S: this,
|
|
566
|
-
A: [
|
|
567
|
-
"parts.length === 1",
|
|
568
|
-
""
|
|
569
|
-
]
|
|
570
|
-
});
|
|
577
|
+
if (parts.length !== 1) {
|
|
578
|
+
throw new Error('Invalid "type" DXN');
|
|
579
|
+
}
|
|
571
580
|
break;
|
|
572
581
|
}
|
|
573
582
|
this.#kind = kind;
|
|
@@ -582,9 +591,21 @@ var DXN = class _DXN {
|
|
|
582
591
|
isTypeDXNOf(typename) {
|
|
583
592
|
return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
|
|
584
593
|
}
|
|
594
|
+
isLocalEchoObjectDXN() {
|
|
595
|
+
return this.#kind === _DXN.kind.ECHO && this.#parts[0] === LOCAL_SPACE_TAG && this.#parts.length === 2;
|
|
596
|
+
}
|
|
585
597
|
toString() {
|
|
586
598
|
return `dxn:${this.#kind}:${this.#parts.join(":")}`;
|
|
587
599
|
}
|
|
600
|
+
/**
|
|
601
|
+
* Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
|
|
602
|
+
*/
|
|
603
|
+
[import_debug2.inspectCustom](depth, options, inspectFn) {
|
|
604
|
+
const printControlCode = (code) => {
|
|
605
|
+
return `\x1B[${code}m`;
|
|
606
|
+
};
|
|
607
|
+
return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
|
|
608
|
+
}
|
|
588
609
|
};
|
|
589
610
|
var LOCAL_SPACE_TAG = "@";
|
|
590
611
|
// Annotate the CommonJS export names for ESM import in node:
|