@dxos/keys 0.1.56-main.e47dfd1 → 0.1.56-main.e79d64a

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.
@@ -3,30 +3,39 @@ import "@dxos/node-std/globals";
3
3
  // packages/common/keys/src/public-key.ts
4
4
  import { inspect } from "@dxos/node-std/util";
5
5
  import randomBytes from "randombytes";
6
- import invariant from "tiny-invariant";
7
6
  import { truncateKey, devtoolsFormatter, equalsSymbol } from "@dxos/debug";
7
+ import { invariant } from "@dxos/invariant";
8
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
8
9
  var PUBLIC_KEY_LENGTH = 32;
9
10
  var SECRET_KEY_LENGTH = 64;
10
- var PublicKey = class {
11
+ var PublicKey = class _PublicKey {
11
12
  /**
12
13
  * Creates new instance of PublicKey automatically determining the input format.
13
14
  * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
14
15
  * @returns PublicKey
15
16
  */
16
17
  static from(source) {
17
- invariant(source);
18
- if (source instanceof PublicKey) {
18
+ invariant(source, void 0, {
19
+ F: __dxlog_file,
20
+ L: 31,
21
+ S: this,
22
+ A: [
23
+ "source",
24
+ ""
25
+ ]
26
+ });
27
+ if (source instanceof _PublicKey) {
19
28
  return source;
20
29
  } else if (source instanceof Buffer) {
21
- return new PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
30
+ return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
22
31
  } else if (source instanceof Uint8Array) {
23
- return new PublicKey(source);
32
+ return new _PublicKey(source);
24
33
  } else if (source instanceof ArrayBuffer) {
25
- return new PublicKey(new Uint8Array(source));
34
+ return new _PublicKey(new Uint8Array(source));
26
35
  } else if (typeof source === "string") {
27
- return PublicKey.fromHex(source);
36
+ return _PublicKey.fromHex(source);
28
37
  } else if (source.asUint8Array) {
29
- return new PublicKey(source.asUint8Array());
38
+ return new _PublicKey(source.asUint8Array());
30
39
  } else {
31
40
  throw new TypeError(`Unable to create PublicKey from ${source}`);
32
41
  }
@@ -41,8 +50,9 @@ var PublicKey = class {
41
50
  return void 0;
42
51
  }
43
52
  try {
44
- return PublicKey.from(source);
45
- } catch (error) {
53
+ const key = _PublicKey.from(source);
54
+ return key;
55
+ } catch (err) {
46
56
  return void 0;
47
57
  }
48
58
  }
@@ -54,17 +64,17 @@ var PublicKey = class {
54
64
  hex = hex.slice(2);
55
65
  }
56
66
  const buf = Buffer.from(hex, "hex");
57
- return new PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
67
+ return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
58
68
  }
59
69
  /**
60
70
  * Creates a new key.
61
71
  */
62
72
  static random() {
63
- return PublicKey.from(randomBytes(32));
73
+ return _PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
64
74
  }
65
75
  static *randomSequence() {
66
76
  for (let i = 0; i < 1e4; i++) {
67
- yield PublicKey.random();
77
+ yield _PublicKey.random();
68
78
  }
69
79
  throw new Error("Too many keys requested");
70
80
  }
@@ -72,7 +82,7 @@ var PublicKey = class {
72
82
  * Tests if provided values is an instance of PublicKey.
73
83
  */
74
84
  static isPublicKey(value) {
75
- return value instanceof PublicKey;
85
+ return value instanceof _PublicKey;
76
86
  }
77
87
  /**
78
88
  * Asserts that provided values is an instance of PublicKey.
@@ -86,7 +96,7 @@ var PublicKey = class {
86
96
  * Tests two keys for equality.
87
97
  */
88
98
  static equals(left, right) {
89
- return PublicKey.from(left).equals(right);
99
+ return _PublicKey.from(left).equals(right);
90
100
  }
91
101
  /**
92
102
  * @param str string representation of key.
@@ -94,7 +104,15 @@ var PublicKey = class {
94
104
  * @deprecated All keys should be represented as instances of PublicKey.
95
105
  */
96
106
  static bufferize(str) {
97
- invariant(typeof str === "string", "Invalid type");
107
+ invariant(typeof str === "string", "Invalid type", {
108
+ F: __dxlog_file,
109
+ L: 130,
110
+ S: this,
111
+ A: [
112
+ "typeof str === 'string'",
113
+ "'Invalid type'"
114
+ ]
115
+ });
98
116
  const buffer = Buffer.from(str, "hex");
99
117
  return buffer;
100
118
  }
@@ -104,12 +122,20 @@ var PublicKey = class {
104
122
  * @deprecated All keys should be represented as instances of PublicKey.
105
123
  */
106
124
  static stringify(key) {
107
- if (key instanceof PublicKey) {
125
+ if (key instanceof _PublicKey) {
108
126
  key = key.asBuffer();
109
127
  } else if (key instanceof Uint8Array) {
110
128
  key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
111
129
  }
112
- invariant(key instanceof Buffer, "Invalid type");
130
+ invariant(key instanceof Buffer, "Invalid type", {
131
+ F: __dxlog_file,
132
+ L: 149,
133
+ S: this,
134
+ A: [
135
+ "key instanceof Buffer",
136
+ "'Invalid type'"
137
+ ]
138
+ });
113
139
  return key.toString("hex");
114
140
  }
115
141
  /**
@@ -131,6 +157,9 @@ var PublicKey = class {
131
157
  toJSON() {
132
158
  return this.toHex();
133
159
  }
160
+ get length() {
161
+ return this._value.length;
162
+ }
134
163
  toHex() {
135
164
  return this.asBuffer().toString("hex");
136
165
  }
@@ -221,18 +250,18 @@ var PublicKey = class {
221
250
  * Test this key for equality with some other key.
222
251
  */
223
252
  equals(other) {
224
- const otherConverted = PublicKey.from(other);
253
+ const otherConverted = _PublicKey.from(other);
225
254
  if (this._value.length !== otherConverted._value.length) {
226
255
  return false;
227
256
  }
228
257
  let equal = true;
229
258
  for (let i = 0; i < this._value.length; i++) {
230
- equal && (equal = this._value[i] === otherConverted._value[i]);
259
+ equal &&= this._value[i] === otherConverted._value[i];
231
260
  }
232
261
  return equal;
233
262
  }
234
263
  [equalsSymbol](other) {
235
- if (!PublicKey.isPublicKey(other)) {
264
+ if (!_PublicKey.isPublicKey(other)) {
236
265
  return false;
237
266
  }
238
267
  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';\nimport invariant from 'tiny-invariant';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\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 return PublicKey.from(source);\n } catch (error: 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(32));\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 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": ";;;AAIA,SAASA,eAAuC;AAChD,OAAOC,iBAAiB;AACxB,OAAOC,eAAe;AAEtB,SAASC,aAAaC,mBAAsCC,oBAA+B;AAEpF,IAAMC,oBAAoB;AAC1B,IAAMC,oBAAoB;AAY1B,IAAMC,YAAN,MAAMA;;;;;;EAMX,OAAOC,KAAKC,QAAkC;AAC5CC,cAAUD,MAAAA;AACV,QAAIA,kBAAkBF,WAAW;AAC/B,aAAOE;IACT,WAAWA,kBAAkBE,QAAQ;AACnC,aAAO,IAAIJ,UAAU,IAAIK,WAAWH,OAAOI,QAAQJ,OAAOK,YAAYL,OAAOM,UAAU,CAAA;IACzF,WAAWN,kBAAkBG,YAAY;AACvC,aAAO,IAAIL,UAAUE,MAAAA;IACvB,WAAWA,kBAAkBO,aAAa;AACxC,aAAO,IAAIT,UAAU,IAAIK,WAAWH,MAAAA,CAAAA;IACtC,WAAW,OAAOA,WAAW,UAAU;AACrC,aAAOF,UAAUU,QAAQR,MAAAA;IAC3B,WAAiBA,OAAQS,cAAc;AACrC,aAAO,IAAIX,UAAgBE,OAAQS,aAAY,CAAA;IACjD,OAAO;AACL,YAAM,IAAIC,UAAU,mCAAmCV,QAAQ;IACjE;EACF;;;;;;EAOA,OAAOW,SAASX,QAA+C;AAC7D,QAAI,CAACA,QAAQ;AACX,aAAOY;IACT;AAEA,QAAI;AACF,aAAOd,UAAUC,KAAKC,MAAAA;IACxB,SAASa,OAAP;AACA,aAAOD;IACT;EACF;;;;EAKA,OAAOJ,QAAQM,KAAa;AAC1B,QAAIA,IAAIC,WAAW,IAAA,GAAO;AACxBD,YAA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;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;AAEF,cAAMM,QAAQN,OAAO,KAAKX,gBAAgBW,OAAOb,MAAM,CAAA;AAEvD,eAAO;UACL;UACA,CAAC;UACD;YAAC;YAAQ,CAAC;YAAG;;UACb;YAAC;YAAQ;cAAEuB,OAAO,UAAUJ;YAAS;YAAG,KAAKpB,SAAQ;;UACrD;YAAC;YAAQ,CAAC;YAAG;;;MAEjB;IACF;EACF;;;;EAKAd,OAAOuC,OAAsB;AAC3B,UAAMC,iBAAiBpE,UAAUC,KAAKkE,KAAAA;AACtC,QAAI,KAAK3B,OAAOG,WAAWyB,eAAe5B,OAAOG,QAAQ;AACvD,aAAO;IACT;AAEA,QAAI0B,QAAQ;AACZ,aAAS9C,IAAI,GAAGA,IAAI,KAAKiB,OAAOG,QAAQpB,KAAK;AAC3C8C,wBAAU,KAAK7B,OAAOjB,CAAAA,MAAO6C,eAAe5B,OAAOjB,CAAAA;IACrD;AAEA,WAAO8C;EACT;EAEA,CAACC,YAAAA,EAAcH,OAAY;AACzB,QAAI,CAACnE,UAAUyB,YAAY0C,KAAAA,GAAQ;AACjC,aAAO;IACT;AAEA,WAAO,KAAKvC,OAAOuC,KAAAA;EACrB;AACF;",
6
- "names": ["inspect", "randomBytes", "invariant", "truncateKey", "devtoolsFormatter", "equalsSymbol", "PUBLIC_KEY_LENGTH", "SECRET_KEY_LENGTH", "PublicKey", "from", "source", "invariant", "Buffer", "Uint8Array", "buffer", "byteOffset", "byteLength", "ArrayBuffer", "fromHex", "asUint8Array", "TypeError", "safeFrom", "undefined", "error", "hex", "startsWith", "slice", "buf", "random", "randomBytes", "randomSequence", "i", "Error", "isPublicKey", "value", "assertValidPublicKey", "equals", "left", "right", "bufferize", "str", "stringify", "key", "asBuffer", "toString", "hash", "toHex", "constructor", "_value", "toJSON", "truncate", "length", "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"]
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 // 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 *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.length;\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": ";;;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,WAAAA;;;;;;EAMX,OAAOC,KAAKC,QAAkC;AAC5CL,cAAUK,QAAAA,QAAAA;;;;;;;;;AACV,QAAIA,kBAAkBF,YAAW;AAC/B,aAAOE;IACT,WAAWA,kBAAkBC,QAAQ;AACnC,aAAO,IAAIH,WAAU,IAAII,WAAWF,OAAOG,QAAQH,OAAOI,YAAYJ,OAAOK,UAAU,CAAA;IACzF,WAAWL,kBAAkBE,YAAY;AACvC,aAAO,IAAIJ,WAAUE,MAAAA;IACvB,WAAWA,kBAAkBM,aAAa;AACxC,aAAO,IAAIR,WAAU,IAAII,WAAWF,MAAAA,CAAAA;IACtC,WAAW,OAAOA,WAAW,UAAU;AAErC,aAAOF,WAAUS,QAAQP,MAAAA;IAC3B,WAAiBA,OAAQQ,cAAc;AACrC,aAAO,IAAIV,WAAgBE,OAAQQ,aAAY,CAAA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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", "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", "length", "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":25970,"imports":[{"path":"@dxos/node-std/util","kind":"import-statement","external":true},{"path":"randombytes","kind":"import-statement","external":true},{"path":"tiny-invariant","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true}]},"packages/common/keys/src/types.ts":{"bytes":641,"imports":[]},"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"}]}},"outputs":{"packages/common/keys/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12645},"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":"tiny-invariant","kind":"import-statement","external":true},{"path":"@dxos/debug","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":6451},"packages/common/keys/src/index.ts":{"bytesInOutput":0}},"bytes":6628}}}
1
+ {"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":27526,"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":13032},"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":7044},"packages/common/keys/src/index.ts":{"bytesInOutput":0}},"bytes":7221}}}
@@ -39,30 +39,39 @@ module.exports = __toCommonJS(src_exports);
39
39
  // packages/common/keys/src/public-key.ts
40
40
  var import_node_util = require("node:util");
41
41
  var import_randombytes = __toESM(require("randombytes"));
42
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
43
42
  var import_debug = require("@dxos/debug");
43
+ var import_invariant = require("@dxos/invariant");
44
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
44
45
  var PUBLIC_KEY_LENGTH = 32;
45
46
  var SECRET_KEY_LENGTH = 64;
46
- var PublicKey = class {
47
+ var PublicKey = class _PublicKey {
47
48
  /**
48
49
  * Creates new instance of PublicKey automatically determining the input format.
49
50
  * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
50
51
  * @returns PublicKey
51
52
  */
52
53
  static from(source) {
53
- (0, import_tiny_invariant.default)(source);
54
- if (source instanceof PublicKey) {
54
+ (0, import_invariant.invariant)(source, void 0, {
55
+ F: __dxlog_file,
56
+ L: 31,
57
+ S: this,
58
+ A: [
59
+ "source",
60
+ ""
61
+ ]
62
+ });
63
+ if (source instanceof _PublicKey) {
55
64
  return source;
56
65
  } else if (source instanceof Buffer) {
57
- return new PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
66
+ return new _PublicKey(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
58
67
  } else if (source instanceof Uint8Array) {
59
- return new PublicKey(source);
68
+ return new _PublicKey(source);
60
69
  } else if (source instanceof ArrayBuffer) {
61
- return new PublicKey(new Uint8Array(source));
70
+ return new _PublicKey(new Uint8Array(source));
62
71
  } else if (typeof source === "string") {
63
- return PublicKey.fromHex(source);
72
+ return _PublicKey.fromHex(source);
64
73
  } else if (source.asUint8Array) {
65
- return new PublicKey(source.asUint8Array());
74
+ return new _PublicKey(source.asUint8Array());
66
75
  } else {
67
76
  throw new TypeError(`Unable to create PublicKey from ${source}`);
68
77
  }
@@ -77,8 +86,9 @@ var PublicKey = class {
77
86
  return void 0;
78
87
  }
79
88
  try {
80
- return PublicKey.from(source);
81
- } catch (error) {
89
+ const key = _PublicKey.from(source);
90
+ return key;
91
+ } catch (err) {
82
92
  return void 0;
83
93
  }
84
94
  }
@@ -90,17 +100,17 @@ var PublicKey = class {
90
100
  hex = hex.slice(2);
91
101
  }
92
102
  const buf = Buffer.from(hex, "hex");
93
- return new PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
103
+ return new _PublicKey(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
94
104
  }
95
105
  /**
96
106
  * Creates a new key.
97
107
  */
98
108
  static random() {
99
- return PublicKey.from((0, import_randombytes.default)(32));
109
+ return _PublicKey.from((0, import_randombytes.default)(PUBLIC_KEY_LENGTH));
100
110
  }
101
111
  static *randomSequence() {
102
112
  for (let i = 0; i < 1e4; i++) {
103
- yield PublicKey.random();
113
+ yield _PublicKey.random();
104
114
  }
105
115
  throw new Error("Too many keys requested");
106
116
  }
@@ -108,7 +118,7 @@ var PublicKey = class {
108
118
  * Tests if provided values is an instance of PublicKey.
109
119
  */
110
120
  static isPublicKey(value) {
111
- return value instanceof PublicKey;
121
+ return value instanceof _PublicKey;
112
122
  }
113
123
  /**
114
124
  * Asserts that provided values is an instance of PublicKey.
@@ -122,7 +132,7 @@ var PublicKey = class {
122
132
  * Tests two keys for equality.
123
133
  */
124
134
  static equals(left, right) {
125
- return PublicKey.from(left).equals(right);
135
+ return _PublicKey.from(left).equals(right);
126
136
  }
127
137
  /**
128
138
  * @param str string representation of key.
@@ -130,7 +140,15 @@ var PublicKey = class {
130
140
  * @deprecated All keys should be represented as instances of PublicKey.
131
141
  */
132
142
  static bufferize(str) {
133
- (0, import_tiny_invariant.default)(typeof str === "string", "Invalid type");
143
+ (0, import_invariant.invariant)(typeof str === "string", "Invalid type", {
144
+ F: __dxlog_file,
145
+ L: 130,
146
+ S: this,
147
+ A: [
148
+ "typeof str === 'string'",
149
+ "'Invalid type'"
150
+ ]
151
+ });
134
152
  const buffer = Buffer.from(str, "hex");
135
153
  return buffer;
136
154
  }
@@ -140,12 +158,20 @@ var PublicKey = class {
140
158
  * @deprecated All keys should be represented as instances of PublicKey.
141
159
  */
142
160
  static stringify(key) {
143
- if (key instanceof PublicKey) {
161
+ if (key instanceof _PublicKey) {
144
162
  key = key.asBuffer();
145
163
  } else if (key instanceof Uint8Array) {
146
164
  key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
147
165
  }
148
- (0, import_tiny_invariant.default)(key instanceof Buffer, "Invalid type");
166
+ (0, import_invariant.invariant)(key instanceof Buffer, "Invalid type", {
167
+ F: __dxlog_file,
168
+ L: 149,
169
+ S: this,
170
+ A: [
171
+ "key instanceof Buffer",
172
+ "'Invalid type'"
173
+ ]
174
+ });
149
175
  return key.toString("hex");
150
176
  }
151
177
  /**
@@ -167,6 +193,9 @@ var PublicKey = class {
167
193
  toJSON() {
168
194
  return this.toHex();
169
195
  }
196
+ get length() {
197
+ return this._value.length;
198
+ }
170
199
  toHex() {
171
200
  return this.asBuffer().toString("hex");
172
201
  }
@@ -257,18 +286,18 @@ var PublicKey = class {
257
286
  * Test this key for equality with some other key.
258
287
  */
259
288
  equals(other) {
260
- const otherConverted = PublicKey.from(other);
289
+ const otherConverted = _PublicKey.from(other);
261
290
  if (this._value.length !== otherConverted._value.length) {
262
291
  return false;
263
292
  }
264
293
  let equal = true;
265
294
  for (let i = 0; i < this._value.length; i++) {
266
- equal && (equal = this._value[i] === otherConverted._value[i]);
295
+ equal &&= this._value[i] === otherConverted._value[i];
267
296
  }
268
297
  return equal;
269
298
  }
270
299
  [import_debug.equalsSymbol](other) {
271
- if (!PublicKey.isPublicKey(other)) {
300
+ if (!_PublicKey.isPublicKey(other)) {
272
301
  return false;
273
302
  }
274
303
  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';\nimport invariant from 'tiny-invariant';\n\nimport { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';\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 return PublicKey.from(source);\n } catch (error: 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(32));\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 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;AACxB,4BAAsB;AAEtB,mBAA2F;AAEpF,IAAMA,oBAA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UAAMC,iBAAiBpE,UAAUC,KAAKkE,KAAAA;AACtC,QAAI,KAAK3B,OAAOG,WAAWyB,eAAe5B,OAAOG,QAAQ;AACvD,aAAO;IACT;AAEA,QAAI0B,QAAQ;AACZ,aAAS9C,IAAI,GAAGA,IAAI,KAAKiB,OAAOG,QAAQpB,KAAK;AAC3C8C,wBAAU,KAAK7B,OAAOjB,CAAAA,MAAO6C,eAAe5B,OAAOjB,CAAAA;IACrD;AAEA,WAAO8C;EACT;EAEA,CAACC,yBAAAA,EAAcH,OAAY;AACzB,QAAI,CAACnE,UAAUyB,YAAY0C,KAAAA,GAAQ;AACjC,aAAO;IACT;AAEA,WAAO,KAAKvC,OAAOuC,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", "error", "hex", "startsWith", "slice", "buf", "random", "randomBytes", "randomSequence", "i", "Error", "isPublicKey", "value", "assertValidPublicKey", "equals", "left", "right", "bufferize", "str", "stringify", "key", "asBuffer", "toString", "hash", "toHex", "constructor", "_value", "toJSON", "truncate", "length", "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"]
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 // 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 *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.length;\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,YAAW;AAC/B,aAAOE;IACT,WAAWA,kBAAkBE,QAAQ;AACnC,aAAO,IAAIJ,WAAU,IAAIK,WAAWH,OAAOI,QAAQJ,OAAOK,YAAYL,OAAOM,UAAU,CAAA;IACzF,WAAWN,kBAAkBG,YAAY;AACvC,aAAO,IAAIL,WAAUE,MAAAA;IACvB,WAAWA,kBAAkBO,aAAa;AACxC,aAAO,IAAIT,WAAU,IAAIK,WAAWH,MAAAA,CAAAA;IACtC,WAAW,OAAOA,WAAW,UAAU;AAErC,aAAOF,WAAUU,QAAQR,MAAAA;IAC3B,WAAiBA,OAAQS,cAAc;AACrC,aAAO,IAAIX,WAAgBE,OAAQS,aAAY,CAAA;IACjD,OAAO;AACL,YAAM,IAAIC,UAAU,mCAAmCV,MAAAA,EAAQ;IACjE;EACF;;;;;;EAOA,OAAOW,SAASX,QAA+C;AAC7D,QAAI,CAACA,QAAQ;AACX,aAAOY;IACT;AAEA,QAAI;AACF,YAAMC,MAAMf,WAAUC,KAAKC,MAAAA;AAK3B,aAAOa;IACT,SAASC,KAAU;AACjB,aAAOF;IACT;EACF;;;;EAKA,OAAOJ,QAAQO,KAAa;AAC1B,QAAIA,IAAIC,WAAW,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iBAClFR,yBAAQI,OAAOO,MAAO,CAAA,CAAE,CAAA;EAE5B;EAEA,KAAKC,8BAAAA,IAAwC;AAC3C,WAAO;MACLC,QAAQ,MAAA;AAEN,cAAMT,SAAS;UACb;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;AAEF,cAAMM,QAAQN,OAAO,KAAKX,gBAAgBW,OAAOd,MAAM,CAAA;AAEvD,eAAO;UACL;UACA,CAAC;UACD;YAAC;YAAQ,CAAC;YAAG;;UACb;YAAC;YAAQ;cAAEwB,OAAO,UAAUJ,KAAAA;YAAS;YAAG,KAAKnB,SAAQ;;UACrD;YAAC;YAAQ,CAAC;YAAG;;;MAEjB;IACF;EACF;;;;EAKAd,OAAOsC,OAAsB;AAC3B,UAAMC,iBAAiBpE,WAAUC,KAAKkE,KAAAA;AACtC,QAAI,KAAK3B,OAAOE,WAAW0B,eAAe5B,OAAOE,QAAQ;AACvD,aAAO;IACT;AAEA,QAAI2B,QAAQ;AACZ,aAAS7C,IAAI,GAAGA,IAAI,KAAKgB,OAAOE,QAAQlB,KAAK;AAC3C6C,gBAAU,KAAK7B,OAAOhB,CAAAA,MAAO4C,eAAe5B,OAAOhB,CAAAA;IACrD;AAEA,WAAO6C;EACT;EAEA,CAACC,yBAAAA,EAAcH,OAAY;AACzB,QAAI,CAACnE,WAAU0B,YAAYyC,KAAAA,GAAQ;AACjC,aAAO;IACT;AAEA,WAAO,KAAKtC,OAAOsC,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", "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", "length", "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
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":25970,"imports":[{"path":"node:util","kind":"import-statement","external":true},{"path":"randombytes","kind":"import-statement","external":true},{"path":"tiny-invariant","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true}]},"packages/common/keys/src/types.ts":{"bytes":641,"imports":[]},"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"}]}},"outputs":{"packages/common/keys/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12716},"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":"tiny-invariant","kind":"require-call","external":true},{"path":"@dxos/debug","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":6649}},"bytes":8521}}}
1
+ {"inputs":{"packages/common/keys/src/public-key.ts":{"bytes":27526,"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":13168},"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":7215}},"bytes":9087}}}
@@ -68,6 +68,7 @@ export declare class PublicKey implements Equatable {
68
68
  constructor(_value: Uint8Array);
69
69
  toString(): string;
70
70
  toJSON(): string;
71
+ get length(): number;
71
72
  toHex(): string;
72
73
  truncate(length?: undefined): string;
73
74
  asBuffer(): Buffer;
@@ -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;AAI5D,OAAO,EAAe,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEzG,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;IAmI7B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAlInC;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,aAAa,GAAG,SAAS;IAmB7C;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,GAAG,SAAS;IAY9D;;O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,IAAI,CAAC,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.e47dfd1",
3
+ "version": "0.1.56-main.e79d64a",
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
- "tiny-invariant": "^1.3.1",
21
- "@dxos/debug": "0.1.56-main.e47dfd1",
22
- "@dxos/node-std": "0.1.56-main.e47dfd1"
20
+ "@dxos/debug": "0.1.56-main.e79d64a",
21
+ "@dxos/invariant": "0.1.56-main.e79d64a",
22
+ "@dxos/node-std": "0.1.56-main.e79d64a"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/randombytes": "^2.0.0"
package/src/public-key.ts CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  import { inspect, InspectOptionsStylized } from 'node:util';
6
6
  import randomBytes from 'randombytes';
7
- import invariant from 'tiny-invariant';
8
7
 
9
8
  import { truncateKey, devtoolsFormatter, DevtoolsFormatter, equalsSymbol, Equatable } from '@dxos/debug';
9
+ import { invariant } from '@dxos/invariant';
10
10
 
11
11
  export const PUBLIC_KEY_LENGTH = 32;
12
12
  export const SECRET_KEY_LENGTH = 64;
@@ -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,13 @@ export class PublicKey implements Equatable {
57
58
  }
58
59
 
59
60
  try {
60
- return PublicKey.from(source);
61
- } catch (error: any) {
61
+ const key = PublicKey.from(source);
62
+ // TODO(wittjosiah): Space keys don't pass this check.
63
+ // if (key.length !== PUBLIC_KEY_LENGTH && key.length !== SECRET_KEY_LENGTH) {
64
+ // return undefined;
65
+ // }
66
+ return key;
67
+ } catch (err: any) {
62
68
  return undefined;
63
69
  }
64
70
  }
@@ -81,7 +87,7 @@ export class PublicKey implements Equatable {
81
87
  */
82
88
  static random(): PublicKey {
83
89
  // TODO(burdon): Enable seed for debugging.
84
- return PublicKey.from(randomBytes(32));
90
+ return PublicKey.from(randomBytes(PUBLIC_KEY_LENGTH));
85
91
  }
86
92
 
87
93
  static *randomSequence(): Generator<PublicKey> {
@@ -166,6 +172,10 @@ export class PublicKey implements Equatable {
166
172
  return this.toHex();
167
173
  }
168
174
 
175
+ get length() {
176
+ return this._value.length;
177
+ }
178
+
169
179
  toHex(): string {
170
180
  return this.asBuffer().toString('hex');
171
181
  }