@dxos/protocols 2.33.7-dev.848c0285 → 2.33.7
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/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +17 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/proto/gen/dxos/devtools.d.ts +20 -16
- package/dist/src/proto/gen/dxos/devtools.d.ts.map +1 -1
- package/dist/src/proto/gen/dxos/halo/keys.d.ts +9 -9
- package/dist/src/proto/gen/index.d.ts +4 -4
- package/dist/src/proto/gen/index.d.ts.map +1 -1
- package/dist/src/proto/gen/index.js +1 -1
- package/dist/src/proto/gen/index.js.map +1 -1
- package/dist/src/proto/index.d.ts +2 -0
- package/dist/src/proto/index.d.ts.map +1 -0
- package/dist/src/proto/index.js +23 -0
- package/dist/src/proto/index.js.map +1 -0
- package/dist/src/public-key.d.ts +76 -0
- package/dist/src/public-key.d.ts.map +1 -0
- package/dist/src/public-key.js +146 -0
- package/dist/src/public-key.js.map +1 -0
- package/dist/src/public-key.test.d.ts +2 -0
- package/dist/src/public-key.test.d.ts.map +1 -0
- package/dist/src/public-key.test.js +43 -0
- package/dist/src/public-key.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -4
- package/src/index.ts +3 -0
- package/src/proto/dxos/devtools.proto +74 -32
- package/src/proto/dxos/halo/keys.proto +11 -11
- package/src/proto/gen/dxos/devtools.ts +20 -16
- package/src/proto/gen/dxos/halo/keys.ts +9 -9
- package/src/proto/gen/index.ts +5 -5
- package/src/proto/index.ts +8 -0
- package/src/public-key.test.ts +63 -0
- package/src/public-key.ts +163 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import randomBytes from 'randombytes';
|
|
6
|
+
import { inspect } from 'util';
|
|
7
|
+
|
|
8
|
+
export class PublicKey {
|
|
9
|
+
/**
|
|
10
|
+
* Creates new instance of PublicKey automatically determining the input format.
|
|
11
|
+
*/
|
|
12
|
+
static from (source: PublicKeyLike): PublicKey {
|
|
13
|
+
if (source instanceof PublicKey) {
|
|
14
|
+
return source;
|
|
15
|
+
} else if (source instanceof Buffer) {
|
|
16
|
+
return new PublicKey(new Uint8Array(source));
|
|
17
|
+
} else if (source instanceof Uint8Array) {
|
|
18
|
+
return new PublicKey(source);
|
|
19
|
+
} else if (typeof source === 'string') {
|
|
20
|
+
return PublicKey.fromHex(source);
|
|
21
|
+
} else if ((<any>source).asUint8Array) {
|
|
22
|
+
return new PublicKey((<any>source).asUint8Array());
|
|
23
|
+
} else {
|
|
24
|
+
throw new TypeError(`Unable to create PublicKey from ${source}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates new instance of PublicKey from hex string.
|
|
30
|
+
*/
|
|
31
|
+
static fromHex (hex: string) {
|
|
32
|
+
if (hex.startsWith('0x')) {
|
|
33
|
+
hex = hex.slice(2);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new PublicKey(new Uint8Array(Buffer.from(hex, 'hex')));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static random (): PublicKey {
|
|
40
|
+
return PublicKey.from(randomBytes(32));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Tests if provided values is an instance of PublicKey.
|
|
45
|
+
*/
|
|
46
|
+
static isPublicKey (value: any): value is PublicKey {
|
|
47
|
+
return value instanceof PublicKey;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Asserts that provided values is an instance of PublicKey.
|
|
52
|
+
*/
|
|
53
|
+
static assertValidPublicKey (value: any): asserts value is PublicKey {
|
|
54
|
+
if (!this.isPublicKey(value)) {
|
|
55
|
+
throw new TypeError('Invalid PublicKey');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Tests two keys for equality.
|
|
61
|
+
*/
|
|
62
|
+
static equals (left: PublicKeyLike, right: PublicKeyLike) {
|
|
63
|
+
return PublicKey.from(left).equals(right);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
constructor (
|
|
67
|
+
private readonly _value: Uint8Array
|
|
68
|
+
) {
|
|
69
|
+
if (!(_value instanceof Uint8Array)) {
|
|
70
|
+
throw new TypeError(`Expected Uint8Array, got: ${_value}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Return underlying Uint8Array representation.
|
|
76
|
+
*/
|
|
77
|
+
asUint8Array (): Uint8Array {
|
|
78
|
+
return this._value;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Covert this key to buffer.
|
|
83
|
+
*/
|
|
84
|
+
asBuffer (): Buffer {
|
|
85
|
+
return Buffer.from(this._value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Convert this key to hex-encoded string.
|
|
90
|
+
*/
|
|
91
|
+
toHex (): string {
|
|
92
|
+
return this.asBuffer().toString('hex');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Same as `PublicKey.humanize()`.
|
|
97
|
+
*/
|
|
98
|
+
toString (): string {
|
|
99
|
+
return this.toHex();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Same as `PublicKey.humanize()`.
|
|
104
|
+
*/
|
|
105
|
+
toJSON () {
|
|
106
|
+
return this.toHex();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
truncate (n = 4) {
|
|
110
|
+
const key = this.toHex();
|
|
111
|
+
if (key.length < n * 2 + 2) {
|
|
112
|
+
return key;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return `${key.substring(0, n)}..${key.substring(key.length - n)}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Used by NodeJS to get textual representation of this object when it's printed with a `console.log` statement.
|
|
120
|
+
*/
|
|
121
|
+
[inspect.custom] () {
|
|
122
|
+
return `<PublicKey ${this.truncate()}>`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Test this key for equality with some other key.
|
|
127
|
+
*/
|
|
128
|
+
equals (other: PublicKeyLike) {
|
|
129
|
+
const otherConverted = PublicKey.from(other);
|
|
130
|
+
if (this._value.length !== otherConverted._value.length) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
let equal = true;
|
|
134
|
+
this._value;
|
|
135
|
+
for (let i = 0; i < this._value.length; i++) {
|
|
136
|
+
equal &&= this._value[i] === otherConverted._value[i];
|
|
137
|
+
}
|
|
138
|
+
return equal;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* All representations that can be converted to a PublicKey.
|
|
144
|
+
*/
|
|
145
|
+
// TODO(burdon): Remove this.
|
|
146
|
+
export type PublicKeyLike =
|
|
147
|
+
| PublicKey
|
|
148
|
+
| Buffer
|
|
149
|
+
| Uint8Array
|
|
150
|
+
| string
|
|
151
|
+
|
|
152
|
+
export const publicKeySubstitutions = {
|
|
153
|
+
// TODO(dmaretskyi): Rename to dxos.crypto.PublicKey.
|
|
154
|
+
'dxos.halo.keys.PubKey': {
|
|
155
|
+
encode: (value: PublicKey) => ({ data: value.asUint8Array() }),
|
|
156
|
+
decode: (value: any) => PublicKey.from(new Uint8Array(value.data))
|
|
157
|
+
},
|
|
158
|
+
// TODO(dmaretskyi): Shouldn't be substitutted to PublicKey.
|
|
159
|
+
'dxos.halo.keys.PrivKey': {
|
|
160
|
+
encode: (value: Buffer) => ({ data: new Uint8Array(value) }),
|
|
161
|
+
decode: (value: any) => PublicKey.from(new Uint8Array(value.data)).asBuffer()
|
|
162
|
+
}
|
|
163
|
+
};
|