@dxos/protocols 2.33.7-dev.c2ba88fb → 2.33.8-dev.6bc74570
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/halo/keys.d.ts +9 -9
- 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 +95 -0
- package/dist/src/public-key.d.ts.map +1 -0
- package/dist/src/public-key.js +180 -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 +50 -0
- package/dist/src/public-key.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -3
- package/src/index.ts +3 -0
- package/src/proto/dxos/halo/keys.proto +11 -11
- package/src/proto/gen/dxos/halo/keys.ts +9 -9
- package/src/proto/gen/index.ts +1 -1
- package/src/proto/index.ts +8 -0
- package/src/public-key.test.ts +73 -0
- package/src/public-key.ts +201 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { PublicKey } from './public-key';
|
|
6
|
+
|
|
7
|
+
const KEY_HEX = '2c28f0d08ccc5340aee02655675be5796227a28d27b9704df34b7d8b2d9fddc7';
|
|
8
|
+
|
|
9
|
+
it('Basic key operations', () => {
|
|
10
|
+
const publicKey = PublicKey.random().asBuffer();
|
|
11
|
+
|
|
12
|
+
expect(PublicKey.bufferize(PublicKey.stringify(publicKey))).toEqual(publicKey);
|
|
13
|
+
|
|
14
|
+
expect(() => PublicKey.stringify('not-a-buffer' as any)).toThrowError();
|
|
15
|
+
expect(() => PublicKey.bufferize('not-a-value-hex-key')).toThrowError();
|
|
16
|
+
expect(() => PublicKey.bufferize(publicKey as any)).toThrowError();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('formatting', () => {
|
|
20
|
+
const key = PublicKey.fromHex(KEY_HEX);
|
|
21
|
+
|
|
22
|
+
expect(PublicKey.isPublicKey(key)).toEqual(true);
|
|
23
|
+
PublicKey.assertValidPublicKey(key);
|
|
24
|
+
|
|
25
|
+
expect(key.toHex()).toEqual(KEY_HEX);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('asBuffer', () => {
|
|
29
|
+
const key = PublicKey.fromHex(KEY_HEX);
|
|
30
|
+
const buffer = key.asBuffer();
|
|
31
|
+
|
|
32
|
+
expect(buffer).toBeInstanceOf(Buffer);
|
|
33
|
+
|
|
34
|
+
expect(Buffer.from(KEY_HEX, 'hex').equals(buffer)).toEqual(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('asUint8Array', () => {
|
|
38
|
+
const key = PublicKey.fromHex(KEY_HEX);
|
|
39
|
+
|
|
40
|
+
const array = key.asUint8Array();
|
|
41
|
+
expect(array).toBeInstanceOf(Uint8Array);
|
|
42
|
+
expect(Buffer.from(KEY_HEX, 'hex').equals(Buffer.from(array))).toEqual(true);
|
|
43
|
+
|
|
44
|
+
expect(PublicKey.from(Buffer.from(KEY_HEX, 'hex')).asUint8Array()).toBeInstanceOf(Uint8Array);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('from', () => {
|
|
48
|
+
expect(PublicKey.from(KEY_HEX).toHex())
|
|
49
|
+
.toEqual(KEY_HEX);
|
|
50
|
+
|
|
51
|
+
expect(PublicKey.from(Buffer.from(KEY_HEX, 'hex')).toHex())
|
|
52
|
+
.toEqual(KEY_HEX);
|
|
53
|
+
|
|
54
|
+
expect(PublicKey.from(new Uint8Array(32)).toHex())
|
|
55
|
+
.toEqual('0000000000000000000000000000000000000000000000000000000000000000');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('equals', () => {
|
|
59
|
+
expect(PublicKey.equals(
|
|
60
|
+
PublicKey.from(KEY_HEX),
|
|
61
|
+
PublicKey.from(KEY_HEX)
|
|
62
|
+
)).toEqual(true);
|
|
63
|
+
|
|
64
|
+
expect(PublicKey.equals(
|
|
65
|
+
PublicKey.random(),
|
|
66
|
+
PublicKey.random()
|
|
67
|
+
)).toEqual(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('expect equality', () => {
|
|
71
|
+
const key = PublicKey.random();
|
|
72
|
+
expect(key).toEqual(PublicKey.from(key.toHex()));
|
|
73
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import assert from 'assert';
|
|
6
|
+
import randomBytes from 'randombytes';
|
|
7
|
+
import { inspect } from 'util';
|
|
8
|
+
|
|
9
|
+
export const PUBLIC_KEY_LENGTH = 32;
|
|
10
|
+
export const SECRET_KEY_LENGTH = 64;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The purpose of this class is to assure consistent use of keys throughout the project.
|
|
14
|
+
* Keys should be maintained as buffers in objects and proto definitions, and converted to hex
|
|
15
|
+
* strings as late as possible (eg, to log/display).
|
|
16
|
+
*/
|
|
17
|
+
export class PublicKey {
|
|
18
|
+
/**
|
|
19
|
+
* Creates new instance of PublicKey automatically determining the input format.
|
|
20
|
+
*/
|
|
21
|
+
static from (source: PublicKeyLike): PublicKey {
|
|
22
|
+
if (source instanceof PublicKey) {
|
|
23
|
+
return source;
|
|
24
|
+
} else if (source instanceof Buffer) {
|
|
25
|
+
return new PublicKey(new Uint8Array(source));
|
|
26
|
+
} else if (source instanceof Uint8Array) {
|
|
27
|
+
return new PublicKey(source);
|
|
28
|
+
} else if (typeof source === 'string') {
|
|
29
|
+
return PublicKey.fromHex(source);
|
|
30
|
+
} else if ((<any>source).asUint8Array) {
|
|
31
|
+
return new PublicKey((<any>source).asUint8Array());
|
|
32
|
+
} else {
|
|
33
|
+
throw new TypeError(`Unable to create PublicKey from ${source}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates new instance of PublicKey from hex string.
|
|
39
|
+
*/
|
|
40
|
+
static fromHex (hex: string) {
|
|
41
|
+
if (hex.startsWith('0x')) {
|
|
42
|
+
hex = hex.slice(2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return new PublicKey(new Uint8Array(Buffer.from(hex, 'hex')));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static random (): PublicKey {
|
|
49
|
+
return PublicKey.from(randomBytes(32));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Tests if provided values is an instance of PublicKey.
|
|
54
|
+
*/
|
|
55
|
+
static isPublicKey (value: any): value is PublicKey {
|
|
56
|
+
return value instanceof PublicKey;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Asserts that provided values is an instance of PublicKey.
|
|
61
|
+
*/
|
|
62
|
+
static assertValidPublicKey (value: any): asserts value is PublicKey {
|
|
63
|
+
if (!this.isPublicKey(value)) {
|
|
64
|
+
throw new TypeError('Invalid PublicKey');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Tests two keys for equality.
|
|
70
|
+
*/
|
|
71
|
+
static equals (left: PublicKeyLike, right: PublicKeyLike) {
|
|
72
|
+
return PublicKey.from(left).equals(right);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param Hex string representation of key.
|
|
77
|
+
* @return Key buffer.
|
|
78
|
+
* @deprecated All keys should be represented as instances of PublicKey.
|
|
79
|
+
*/
|
|
80
|
+
static bufferize (str: string): Buffer {
|
|
81
|
+
assert(typeof str === 'string', 'Invalid type');
|
|
82
|
+
const buffer = Buffer.from(str, 'hex');
|
|
83
|
+
assert(buffer.length === PUBLIC_KEY_LENGTH || buffer.length === SECRET_KEY_LENGTH,
|
|
84
|
+
`Invalid key length: ${buffer.length}`);
|
|
85
|
+
return buffer;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @param Public key like data structure (but not PublicKey which should use toString).
|
|
90
|
+
* @return Hex string representation of key.
|
|
91
|
+
* @deprecated All keys should be represented as instances of PublicKey.
|
|
92
|
+
*/
|
|
93
|
+
static stringify (key: Buffer | Uint8Array): string {
|
|
94
|
+
if (key instanceof PublicKey) {
|
|
95
|
+
key = key.asBuffer();
|
|
96
|
+
} else if (key instanceof Uint8Array) {
|
|
97
|
+
key = Buffer.from(key);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
assert(key instanceof Buffer, 'Invalid type');
|
|
101
|
+
return key.toString('hex');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
constructor (
|
|
105
|
+
private readonly _value: Uint8Array
|
|
106
|
+
) {
|
|
107
|
+
if (!(_value instanceof Uint8Array)) {
|
|
108
|
+
throw new TypeError(`Expected Uint8Array, got: ${_value}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Return underlying Uint8Array representation.
|
|
114
|
+
*/
|
|
115
|
+
asUint8Array (): Uint8Array {
|
|
116
|
+
return this._value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Covert this key to buffer.
|
|
121
|
+
*/
|
|
122
|
+
asBuffer (): Buffer {
|
|
123
|
+
return Buffer.from(this._value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Convert this key to hex-encoded string.
|
|
128
|
+
*/
|
|
129
|
+
toHex (): string {
|
|
130
|
+
return this.asBuffer().toString('hex');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Same as `PublicKey.humanize()`.
|
|
135
|
+
*/
|
|
136
|
+
toString (): string {
|
|
137
|
+
return this.toHex();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Same as `PublicKey.humanize()`.
|
|
142
|
+
*/
|
|
143
|
+
toJSON () {
|
|
144
|
+
return this.toHex();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
truncate (n = 4) {
|
|
148
|
+
const key = this.toHex();
|
|
149
|
+
if (key.length < n * 2 + 2) {
|
|
150
|
+
return key;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return `${key.substring(0, n)}..${key.substring(key.length - n)}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Used by NodeJS to get textual representation of this object when it's printed with a `console.log` statement.
|
|
158
|
+
*/
|
|
159
|
+
[inspect.custom] () {
|
|
160
|
+
return `<PublicKey ${this.truncate()}>`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Test this key for equality with some other key.
|
|
165
|
+
*/
|
|
166
|
+
equals (other: PublicKeyLike) {
|
|
167
|
+
const otherConverted = PublicKey.from(other);
|
|
168
|
+
if (this._value.length !== otherConverted._value.length) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
let equal = true;
|
|
172
|
+
this._value;
|
|
173
|
+
for (let i = 0; i < this._value.length; i++) {
|
|
174
|
+
equal &&= this._value[i] === otherConverted._value[i];
|
|
175
|
+
}
|
|
176
|
+
return equal;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* All representations that can be converted to a PublicKey.
|
|
182
|
+
*/
|
|
183
|
+
// TODO(burdon): Remove this.
|
|
184
|
+
export type PublicKeyLike =
|
|
185
|
+
| PublicKey
|
|
186
|
+
| Buffer
|
|
187
|
+
| Uint8Array
|
|
188
|
+
| string
|
|
189
|
+
|
|
190
|
+
export const publicKeySubstitutions = {
|
|
191
|
+
// TODO(dmaretskyi): Rename to dxos.crypto.PublicKey.
|
|
192
|
+
'dxos.halo.keys.PubKey': {
|
|
193
|
+
encode: (value: PublicKey) => ({ data: value.asUint8Array() }),
|
|
194
|
+
decode: (value: any) => PublicKey.from(new Uint8Array(value.data))
|
|
195
|
+
},
|
|
196
|
+
// TODO(dmaretskyi): Shouldn't be substitutted to PublicKey.
|
|
197
|
+
'dxos.halo.keys.PrivKey': {
|
|
198
|
+
encode: (value: Buffer) => ({ data: new Uint8Array(value) }),
|
|
199
|
+
decode: (value: any) => PublicKey.from(new Uint8Array(value.data)).asBuffer()
|
|
200
|
+
}
|
|
201
|
+
};
|