@dxos/keys 0.1.56-main.5ff23aa → 0.1.56-main.6293c6e
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 +27 -20
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +27 -20
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/public-key.d.ts +1 -0
- package/dist/types/src/public-key.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/public-key.ts +12 -3
|
@@ -8,7 +8,7 @@ import { invariant } from "@dxos/invariant";
|
|
|
8
8
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
|
|
9
9
|
var PUBLIC_KEY_LENGTH = 32;
|
|
10
10
|
var SECRET_KEY_LENGTH = 64;
|
|
11
|
-
var PublicKey = class {
|
|
11
|
+
var PublicKey = class _PublicKey {
|
|
12
12
|
/**
|
|
13
13
|
* Creates new instance of PublicKey automatically determining the input format.
|
|
14
14
|
* @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
|
|
@@ -24,18 +24,18 @@ var PublicKey = class {
|
|
|
24
24
|
""
|
|
25
25
|
]
|
|
26
26
|
});
|
|
27
|
-
if (source instanceof
|
|
27
|
+
if (source instanceof _PublicKey) {
|
|
28
28
|
return source;
|
|
29
29
|
} else if (source instanceof Buffer) {
|
|
30
|
-
return new
|
|
30
|
+
return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
|
|
31
31
|
} else if (source instanceof Uint8Array) {
|
|
32
|
-
return new
|
|
32
|
+
return new _PublicKey(source);
|
|
33
33
|
} else if (source instanceof ArrayBuffer) {
|
|
34
|
-
return new
|
|
34
|
+
return new _PublicKey(new Uint8Array(source));
|
|
35
35
|
} else if (typeof source === "string") {
|
|
36
|
-
return
|
|
36
|
+
return _PublicKey.fromHex(source);
|
|
37
37
|
} else if (source.asUint8Array) {
|
|
38
|
-
return new
|
|
38
|
+
return new _PublicKey(source.asUint8Array());
|
|
39
39
|
} else {
|
|
40
40
|
throw new TypeError(`Unable to create PublicKey from ${source}`);
|
|
41
41
|
}
|
|
@@ -50,8 +50,12 @@ var PublicKey = class {
|
|
|
50
50
|
return void 0;
|
|
51
51
|
}
|
|
52
52
|
try {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
const key = _PublicKey.from(source);
|
|
54
|
+
if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
return key;
|
|
58
|
+
} catch (err) {
|
|
55
59
|
return void 0;
|
|
56
60
|
}
|
|
57
61
|
}
|
|
@@ -63,17 +67,17 @@ var PublicKey = class {
|
|
|
63
67
|
hex = hex.slice(2);
|
|
64
68
|
}
|
|
65
69
|
const buf = Buffer.from(hex, "hex");
|
|
66
|
-
return new
|
|
70
|
+
return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
|
|
67
71
|
}
|
|
68
72
|
/**
|
|
69
73
|
* Creates a new key.
|
|
70
74
|
*/
|
|
71
75
|
static random() {
|
|
72
|
-
return
|
|
76
|
+
return _PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
|
|
73
77
|
}
|
|
74
78
|
static *randomSequence() {
|
|
75
79
|
for (let i = 0; i < 1e4; i++) {
|
|
76
|
-
yield
|
|
80
|
+
yield _PublicKey.random();
|
|
77
81
|
}
|
|
78
82
|
throw new Error("Too many keys requested");
|
|
79
83
|
}
|
|
@@ -81,7 +85,7 @@ var PublicKey = class {
|
|
|
81
85
|
* Tests if provided values is an instance of PublicKey.
|
|
82
86
|
*/
|
|
83
87
|
static isPublicKey(value) {
|
|
84
|
-
return value instanceof
|
|
88
|
+
return value instanceof _PublicKey;
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
91
|
* Asserts that provided values is an instance of PublicKey.
|
|
@@ -95,7 +99,7 @@ var PublicKey = class {
|
|
|
95
99
|
* Tests two keys for equality.
|
|
96
100
|
*/
|
|
97
101
|
static equals(left, right) {
|
|
98
|
-
return
|
|
102
|
+
return _PublicKey.from(left).equals(right);
|
|
99
103
|
}
|
|
100
104
|
/**
|
|
101
105
|
* @param str string representation of key.
|
|
@@ -105,7 +109,7 @@ var PublicKey = class {
|
|
|
105
109
|
static bufferize(str) {
|
|
106
110
|
invariant(typeof str === "string", "Invalid type", {
|
|
107
111
|
F: __dxlog_file,
|
|
108
|
-
L:
|
|
112
|
+
L: 129,
|
|
109
113
|
S: this,
|
|
110
114
|
A: [
|
|
111
115
|
"typeof str === 'string'",
|
|
@@ -121,14 +125,14 @@ var PublicKey = class {
|
|
|
121
125
|
* @deprecated All keys should be represented as instances of PublicKey.
|
|
122
126
|
*/
|
|
123
127
|
static stringify(key) {
|
|
124
|
-
if (key instanceof
|
|
128
|
+
if (key instanceof _PublicKey) {
|
|
125
129
|
key = key.asBuffer();
|
|
126
130
|
} else if (key instanceof Uint8Array) {
|
|
127
131
|
key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
|
|
128
132
|
}
|
|
129
133
|
invariant(key instanceof Buffer, "Invalid type", {
|
|
130
134
|
F: __dxlog_file,
|
|
131
|
-
L:
|
|
135
|
+
L: 148,
|
|
132
136
|
S: this,
|
|
133
137
|
A: [
|
|
134
138
|
"key instanceof Buffer",
|
|
@@ -156,6 +160,9 @@ var PublicKey = class {
|
|
|
156
160
|
toJSON() {
|
|
157
161
|
return this.toHex();
|
|
158
162
|
}
|
|
163
|
+
get length() {
|
|
164
|
+
return this._value.buffer.byteLength - 1;
|
|
165
|
+
}
|
|
159
166
|
toHex() {
|
|
160
167
|
return this.asBuffer().toString("hex");
|
|
161
168
|
}
|
|
@@ -246,18 +253,18 @@ var PublicKey = class {
|
|
|
246
253
|
* Test this key for equality with some other key.
|
|
247
254
|
*/
|
|
248
255
|
equals(other) {
|
|
249
|
-
const otherConverted =
|
|
256
|
+
const otherConverted = _PublicKey.from(other);
|
|
250
257
|
if (this._value.length !== otherConverted._value.length) {
|
|
251
258
|
return false;
|
|
252
259
|
}
|
|
253
260
|
let equal = true;
|
|
254
261
|
for (let i = 0; i < this._value.length; i++) {
|
|
255
|
-
equal
|
|
262
|
+
equal &&= this._value[i] === otherConverted._value[i];
|
|
256
263
|
}
|
|
257
264
|
return equal;
|
|
258
265
|
}
|
|
259
266
|
[equalsSymbol](other) {
|
|
260
|
-
if (!
|
|
267
|
+
if (!_PublicKey.isPublicKey(other)) {
|
|
261
268
|
return false;
|
|
262
269
|
}
|
|
263
270
|
return this.equals(other);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/public-key.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect, InspectOptionsStylized } from 'node:util';\nimport randomBytes from 'randombytes';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\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 /**\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 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
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,eAAuC;AAChD,OAAOC,iBAAiB;AAExB,SAASC,aAAaC,mBAAsCC,oBAA+B;AAC3F,SAASC,iBAAiB;;AAEnB,IAAMC,oBAAoB;AAC1B,IAAMC,oBAAoB;AAY1B,IAAMC,YAAN,MAAMA;;;;;;EAMX,OAAOC,KAAKC,QAAkC;AAC5CL,cAAUK,QAAAA,QAAAA;;;;;;;;;AACV,QAAIA,kBAAkBF,
|
|
6
|
-
"names": ["inspect", "randomBytes", "truncateKey", "devtoolsFormatter", "equalsSymbol", "invariant", "PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "PublicKey", "from", "source", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect, InspectOptionsStylized } from 'node:util';\nimport randomBytes from 'randombytes';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\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 /**\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 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 *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 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 get length() {\n return this._value.buffer.byteLength - 1;\n }\n\n toHex(): string {\n return this.asBuffer().toString('hex');\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 equ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AAQ4D,OAAOO,MAAO,CAAA,CAAE,CAAA;EAE5B;EAEA,KAAKhE,iBAAAA,IAAwC;AAC3C,WAAO;MACLiE,QAAQ,MAAA;AAEN,cAAMR,SAAS;UACb;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;AAEF,cAAMM,QAAQN,OAAO,KAAKV,gBAAgBU,OAAOrC,MAAM,CAAA;AAEvD,eAAO;UACL;UACA,CAAC;UACD;YAAC;YAAQ,CAAC;YAAG;;UACb;YAAC;YAAQ;cAAE8C,OAAO,UAAUH,KAAAA;YAAS;YAAG,KAAKjB,SAAQ;;UACrD;YAAC;YAAQ,CAAC;YAAG;;;MAEjB;IACF;EACF;;;;EAKAb,OAAOkC,OAAsB;AAC3B,UAAMC,iBAAiB/D,WAAUC,KAAK6D,KAAAA;AACtC,QAAI,KAAKvB,OAAOxB,WAAWgD,eAAexB,OAAOxB,QAAQ;AACvD,aAAO;IACT;AAEA,QAAIiD,QAAQ;AACZ,aAASzC,IAAI,GAAGA,IAAI,KAAKgB,OAAOxB,QAAQQ,KAAK;AAC3CyC,gBAAU,KAAKzB,OAAOhB,CAAAA,MAAOwC,eAAexB,OAAOhB,CAAAA;IACrD;AAEA,WAAOyC;EACT;EAEA,CAACpE,YAAAA,EAAckE,OAAY;AACzB,QAAI,CAAC9D,WAAUyB,YAAYqC,KAAAA,GAAQ;AACjC,aAAO;IACT;AAEA,WAAO,KAAKlC,OAAOkC,KAAAA;EACrB;AACF;",
|
|
6
|
+
"names": ["inspect", "randomBytes", "truncateKey", "devtoolsFormatter", "equalsSymbol", "invariant", "PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "PublicKey", "from", "source", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "key", "length", "err", "hex", "startsWith", "slice", "buf", "random", "randomSequence", "i", "Error", "isPublicKey", "value", "assertValidPublicKey", "equals", "left", "right", "bufferize", "str", "stringify", "asBuffer", "toString", "hash", "toHex", "constructor", "_value", "toJSON", "truncate", "getInsecureHash", "modulo", "Math", "abs", "reduce", "acc", "val", "custom", "depth", "options", "colors", "process", "stdout", "hasColors", "printControlCode", "code", "color", "reset", "header", "style", "other", "otherConverted", "equal"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":27455,"imports":[{"path":"@dxos/node-std/util","kind":"import-statement","external":true},{"path":"randombytes","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/keys/src/types.ts":{"bytes":641,"imports":[],"format":"esm"},"packages/common/keys/src/index.ts":{"bytes":547,"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"}],"format":"esm"}},"outputs":{"packages/common/keys/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":13068},"packages/common/keys/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/node-std/util","kind":"import-statement","external":true},{"path":"randombytes","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["PUBLIC_KEY_LENGTH","PublicKey","SECRET_KEY_LENGTH"],"entryPoint":"packages/common/keys/src/index.ts","inputs":{"packages/common/keys/src/public-key.ts":{"bytesInOutput":7172},"packages/common/keys/src/index.ts":{"bytesInOutput":0}},"bytes":7349}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -44,7 +44,7 @@ var import_invariant = require("@dxos/invariant");
|
|
|
44
44
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
|
|
45
45
|
var PUBLIC_KEY_LENGTH = 32;
|
|
46
46
|
var SECRET_KEY_LENGTH = 64;
|
|
47
|
-
var PublicKey = class {
|
|
47
|
+
var PublicKey = class _PublicKey {
|
|
48
48
|
/**
|
|
49
49
|
* Creates new instance of PublicKey automatically determining the input format.
|
|
50
50
|
* @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
|
|
@@ -60,18 +60,18 @@ var PublicKey = class {
|
|
|
60
60
|
""
|
|
61
61
|
]
|
|
62
62
|
});
|
|
63
|
-
if (source instanceof
|
|
63
|
+
if (source instanceof _PublicKey) {
|
|
64
64
|
return source;
|
|
65
65
|
} else if (source instanceof Buffer) {
|
|
66
|
-
return new
|
|
66
|
+
return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
|
|
67
67
|
} else if (source instanceof Uint8Array) {
|
|
68
|
-
return new
|
|
68
|
+
return new _PublicKey(source);
|
|
69
69
|
} else if (source instanceof ArrayBuffer) {
|
|
70
|
-
return new
|
|
70
|
+
return new _PublicKey(new Uint8Array(source));
|
|
71
71
|
} else if (typeof source === "string") {
|
|
72
|
-
return
|
|
72
|
+
return _PublicKey.fromHex(source);
|
|
73
73
|
} else if (source.asUint8Array) {
|
|
74
|
-
return new
|
|
74
|
+
return new _PublicKey(source.asUint8Array());
|
|
75
75
|
} else {
|
|
76
76
|
throw new TypeError(`Unable to create PublicKey from ${source}`);
|
|
77
77
|
}
|
|
@@ -86,8 +86,12 @@ var PublicKey = class {
|
|
|
86
86
|
return void 0;
|
|
87
87
|
}
|
|
88
88
|
try {
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
const key = _PublicKey.from(source);
|
|
90
|
+
if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
return key;
|
|
94
|
+
} catch (err) {
|
|
91
95
|
return void 0;
|
|
92
96
|
}
|
|
93
97
|
}
|
|
@@ -99,17 +103,17 @@ var PublicKey = class {
|
|
|
99
103
|
hex = hex.slice(2);
|
|
100
104
|
}
|
|
101
105
|
const buf = Buffer.from(hex, "hex");
|
|
102
|
-
return new
|
|
106
|
+
return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
|
|
103
107
|
}
|
|
104
108
|
/**
|
|
105
109
|
* Creates a new key.
|
|
106
110
|
*/
|
|
107
111
|
static random() {
|
|
108
|
-
return
|
|
112
|
+
return _PublicKey.from((0, import_randombytes.default)(PUBLIC_KEY_LENGTH));
|
|
109
113
|
}
|
|
110
114
|
static *randomSequence() {
|
|
111
115
|
for (let i = 0; i < 1e4; i++) {
|
|
112
|
-
yield
|
|
116
|
+
yield _PublicKey.random();
|
|
113
117
|
}
|
|
114
118
|
throw new Error("Too many keys requested");
|
|
115
119
|
}
|
|
@@ -117,7 +121,7 @@ var PublicKey = class {
|
|
|
117
121
|
* Tests if provided values is an instance of PublicKey.
|
|
118
122
|
*/
|
|
119
123
|
static isPublicKey(value) {
|
|
120
|
-
return value instanceof
|
|
124
|
+
return value instanceof _PublicKey;
|
|
121
125
|
}
|
|
122
126
|
/**
|
|
123
127
|
* Asserts that provided values is an instance of PublicKey.
|
|
@@ -131,7 +135,7 @@ var PublicKey = class {
|
|
|
131
135
|
* Tests two keys for equality.
|
|
132
136
|
*/
|
|
133
137
|
static equals(left, right) {
|
|
134
|
-
return
|
|
138
|
+
return _PublicKey.from(left).equals(right);
|
|
135
139
|
}
|
|
136
140
|
/**
|
|
137
141
|
* @param str string representation of key.
|
|
@@ -141,7 +145,7 @@ var PublicKey = class {
|
|
|
141
145
|
static bufferize(str) {
|
|
142
146
|
(0, import_invariant.invariant)(typeof str === "string", "Invalid type", {
|
|
143
147
|
F: __dxlog_file,
|
|
144
|
-
L:
|
|
148
|
+
L: 129,
|
|
145
149
|
S: this,
|
|
146
150
|
A: [
|
|
147
151
|
"typeof str === 'string'",
|
|
@@ -157,14 +161,14 @@ var PublicKey = class {
|
|
|
157
161
|
* @deprecated All keys should be represented as instances of PublicKey.
|
|
158
162
|
*/
|
|
159
163
|
static stringify(key) {
|
|
160
|
-
if (key instanceof
|
|
164
|
+
if (key instanceof _PublicKey) {
|
|
161
165
|
key = key.asBuffer();
|
|
162
166
|
} else if (key instanceof Uint8Array) {
|
|
163
167
|
key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
|
|
164
168
|
}
|
|
165
169
|
(0, import_invariant.invariant)(key instanceof Buffer, "Invalid type", {
|
|
166
170
|
F: __dxlog_file,
|
|
167
|
-
L:
|
|
171
|
+
L: 148,
|
|
168
172
|
S: this,
|
|
169
173
|
A: [
|
|
170
174
|
"key instanceof Buffer",
|
|
@@ -192,6 +196,9 @@ var PublicKey = class {
|
|
|
192
196
|
toJSON() {
|
|
193
197
|
return this.toHex();
|
|
194
198
|
}
|
|
199
|
+
get length() {
|
|
200
|
+
return this._value.buffer.byteLength - 1;
|
|
201
|
+
}
|
|
195
202
|
toHex() {
|
|
196
203
|
return this.asBuffer().toString("hex");
|
|
197
204
|
}
|
|
@@ -282,18 +289,18 @@ var PublicKey = class {
|
|
|
282
289
|
* Test this key for equality with some other key.
|
|
283
290
|
*/
|
|
284
291
|
equals(other) {
|
|
285
|
-
const otherConverted =
|
|
292
|
+
const otherConverted = _PublicKey.from(other);
|
|
286
293
|
if (this._value.length !== otherConverted._value.length) {
|
|
287
294
|
return false;
|
|
288
295
|
}
|
|
289
296
|
let equal = true;
|
|
290
297
|
for (let i = 0; i < this._value.length; i++) {
|
|
291
|
-
equal
|
|
298
|
+
equal &&= this._value[i] === otherConverted._value[i];
|
|
292
299
|
}
|
|
293
300
|
return equal;
|
|
294
301
|
}
|
|
295
302
|
[import_debug.equalsSymbol](other) {
|
|
296
|
-
if (!
|
|
303
|
+
if (!_PublicKey.isPublicKey(other)) {
|
|
297
304
|
return false;
|
|
298
305
|
}
|
|
299
306
|
return this.equals(other);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/index.ts", "../../../src/public-key.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nexport * from './public-key';\nexport * from './types';\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect, InspectOptionsStylized } from 'node:util';\nimport randomBytes from 'randombytes';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\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 /**\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 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
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,uBAAgD;AAChD,yBAAwB;AAExB,mBAA2F;AAC3F,uBAA0B;;AAEnB,IAAMA,oBAAoB;AAC1B,IAAMC,oBAAoB;AAY1B,IAAMC,YAAN,MAAMA;;;;;;EAMX,OAAOC,KAAKC,QAAkC;AAC5CC,oCAAUD,QAAAA,QAAAA;;;;;;;;;AACV,QAAIA,kBAAkBF,
|
|
6
|
-
"names": ["PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "PublicKey", "from", "source", "invariant", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nexport * from './public-key';\nexport * from './types';\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect, InspectOptionsStylized } from 'node:util';\nimport randomBytes from 'randombytes';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\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 /**\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 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 *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 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 get length() {\n return this._value.buffer.byteLength - 1;\n }\n\n toHex(): string {\n return this.asBuffer().toString('hex');\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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,uBAAgD;AAChD,yBAAwB;AAExB,mBAA2F;AAC3F,uBAA0B;;AAEnB,IAAMA,oBAAoB;AAC1B,IAAMC,oBAAoB;AAY1B,IAAMC,YAAN,MAAMA,WAAAA;;;;;;EAMX,OAAOC,KAAKC,QAAkC;AAC5CC,oCAAUD,QAAAA,QAAAA;;;;;;;;;AACV,QAAIA,kBAAkBF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,KAAKnB,SAAQ;;UACrD;YAAC;YAAQ,CAAC;YAAG;;;MAEjB;IACF;EACF;;;;EAKAb,OAAOqC,OAAsB;AAC3B,UAAMC,iBAAiBpE,WAAUC,KAAKkE,KAAAA;AACtC,QAAI,KAAK1B,OAAOzB,WAAWoD,eAAe3B,OAAOzB,QAAQ;AACvD,aAAO;IACT;AAEA,QAAIqD,QAAQ;AACZ,aAAS5C,IAAI,GAAGA,IAAI,KAAKgB,OAAOzB,QAAQS,KAAK;AAC3C4C,gBAAU,KAAK5B,OAAOhB,CAAAA,MAAO2C,eAAe3B,OAAOhB,CAAAA;IACrD;AAEA,WAAO4C;EACT;EAEA,CAACC,yBAAAA,EAAcH,OAAY;AACzB,QAAI,CAACnE,WAAU2B,YAAYwC,KAAAA,GAAQ;AACjC,aAAO;IACT;AAEA,WAAO,KAAKrC,OAAOqC,KAAAA;EACrB;AACF;",
|
|
6
|
+
"names": ["PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "PublicKey", "from", "source", "invariant", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "key", "length", "err", "hex", "startsWith", "slice", "buf", "random", "randomBytes", "randomSequence", "i", "Error", "isPublicKey", "value", "assertValidPublicKey", "equals", "left", "right", "bufferize", "str", "stringify", "asBuffer", "toString", "hash", "toHex", "constructor", "_value", "toJSON", "truncate", "truncateKey", "getInsecureHash", "modulo", "Math", "abs", "reduce", "acc", "val", "inspect", "custom", "depth", "options", "colors", "process", "stdout", "hasColors", "printControlCode", "code", "color", "reset", "devtoolsFormatter", "header", "style", "other", "otherConverted", "equal", "equalsSymbol"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":27455,"imports":[{"path":"node:util","kind":"import-statement","external":true},{"path":"randombytes","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/common/keys/src/types.ts":{"bytes":641,"imports":[],"format":"esm"},"packages/common/keys/src/index.ts":{"bytes":547,"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"}],"format":"esm"}},"outputs":{"packages/common/keys/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":13205},"packages/common/keys/dist/lib/node/index.cjs":{"imports":[{"path":"node:util","kind":"require-call","external":true},{"path":"randombytes","kind":"require-call","external":true},{"path":"@dxos/debug","kind":"require-call","external":true},{"path":"@dxos/invariant","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/keys/src/index.ts","inputs":{"packages/common/keys/src/index.ts":{"bytesInOutput":215},"packages/common/keys/src/public-key.ts":{"bytesInOutput":7343}},"bytes":9215}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public-key.d.ts","sourceRoot":"","sources":["../../../src/public-key.ts"],"names":[],"mappings":";;AAIA,OAAO,EAAE,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAG5D,OAAO,EAAe,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGzG,eAAO,MAAM,iBAAiB,KAAK,CAAC;AACpC,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,CAAC;AAEnF;;;;GAIG;AACH,qBAAa,SAAU,YAAW,SAAS;
|
|
1
|
+
{"version":3,"file":"public-key.d.ts","sourceRoot":"","sources":["../../../src/public-key.ts"],"names":[],"mappings":";;AAIA,OAAO,EAAE,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C,GAAG,EAAE,SAAS,GAAG,MAAM;gBAIN,MAAM,EAAE,UAAU;IAM/C,QAAQ,IAAI,MAAM;IAIlB,MAAM;IAIN,IAAI,MAAM,WAET;IAED,KAAK,IAAI,MAAM;IAIf,QAAQ,CAAC,MAAM,YAAY;IAI3B,QAAQ,IAAI,MAAM;IAIlB,YAAY,IAAI,UAAU;IAI1B,eAAe,CAAC,MAAM,EAAE,MAAM;IAI9B;;OAEG;IACH,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB;IAgC/D,IAAI,CAAC,iBAAiB,CAAC,IAAI,iBAAiB,CA8B3C;IAED;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,aAAa;IAc3B,CAAC,YAAY,CAAC,CAAC,KAAK,EAAE,GAAG;CAO1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/keys",
|
|
3
|
-
"version": "0.1.56-main.
|
|
3
|
+
"version": "0.1.56-main.6293c6e",
|
|
4
4
|
"description": "Key utils and definitions.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"randombytes": "^2.1.0",
|
|
20
|
-
"@dxos/debug": "0.1.56-main.
|
|
21
|
-
"@dxos/invariant": "0.1.56-main.
|
|
22
|
-
"@dxos/node-std": "0.1.56-main.
|
|
20
|
+
"@dxos/debug": "0.1.56-main.6293c6e",
|
|
21
|
+
"@dxos/invariant": "0.1.56-main.6293c6e",
|
|
22
|
+
"@dxos/node-std": "0.1.56-main.6293c6e"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/randombytes": "^2.0.0"
|
package/src/public-key.ts
CHANGED
|
@@ -38,6 +38,7 @@ export class PublicKey implements Equatable {
|
|
|
38
38
|
} else if (source instanceof ArrayBuffer) {
|
|
39
39
|
return new PublicKey(new Uint8Array(source));
|
|
40
40
|
} else if (typeof source === 'string') {
|
|
41
|
+
// TODO(burdon): Check length.
|
|
41
42
|
return PublicKey.fromHex(source);
|
|
42
43
|
} else if ((<any>source).asUint8Array) {
|
|
43
44
|
return new PublicKey((<any>source).asUint8Array());
|
|
@@ -57,8 +58,12 @@ export class PublicKey implements Equatable {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
const key = PublicKey.from(source);
|
|
62
|
+
if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return key;
|
|
66
|
+
} catch (err: any) {
|
|
62
67
|
return undefined;
|
|
63
68
|
}
|
|
64
69
|
}
|
|
@@ -81,7 +86,7 @@ export class PublicKey implements Equatable {
|
|
|
81
86
|
*/
|
|
82
87
|
static random(): PublicKey {
|
|
83
88
|
// TODO(burdon): Enable seed for debugging.
|
|
84
|
-
return PublicKey.from(randomBytes(
|
|
89
|
+
return PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
static *randomSequence(): Generator<PublicKey> {
|
|
@@ -166,6 +171,10 @@ export class PublicKey implements Equatable {
|
|
|
166
171
|
return this.toHex();
|
|
167
172
|
}
|
|
168
173
|
|
|
174
|
+
get length() {
|
|
175
|
+
return this._value.buffer.byteLength - 1;
|
|
176
|
+
}
|
|
177
|
+
|
|
169
178
|
toHex(): string {
|
|
170
179
|
return this.asBuffer().toString('hex');
|
|
171
180
|
}
|