-
Notifications
You must be signed in to change notification settings - Fork 3
/
YlideKeysRegistry.ts
295 lines (263 loc) · 9.01 KB
/
YlideKeysRegistry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { PrivateKeyAvailabilityState, YlidePrivateKey } from './YlidePrivateKey';
import { getPrivateKey } from './getPrivateKey';
import { RemotePublicKey } from './RemotePublicKey';
import { PublicKey, PublicKeyType } from '../primitives';
import { YlideError, YlideErrorType } from '../errors';
import { YlideMisusageError } from '../errors/YlideMisusageError';
import { box as naclbox } from 'tweetnacl';
import type { AbstractStorage } from '../storage/AbstractStorage';
import type { YlidePrivateKeyHandlers } from './getPrivateKey';
import type { YlideKeyVersion } from '../primitives';
export class YlideKeyRegistry {
private localPrivateKeys: Map<string, YlidePrivateKey[]> = new Map();
private remotePublicKeys: Map<string, RemotePublicKey[]> = new Map();
constructor(readonly storage: AbstractStorage, private readonly pfx = 'YLD6_') {
// super();
}
private prefixedStorageKey(str: string) {
return this.pfx + str;
}
/**
* Initializes underlaying storage engine and Keystore
*/
async init() {
if (await this.storage.init()) {
await this.load();
} else {
throw new YlideError(YlideErrorType.UNAVAILABLE, 'Storage is not available');
}
}
private async loadLocalPrivateKeys() {
this.localPrivateKeys = new Map();
const privateKeysLength = await this.storage.readJSON<number>(this.prefixedStorageKey('privateKeysLength'));
if (!privateKeysLength) {
return;
}
for (let keyIdx = 0; keyIdx < privateKeysLength; keyIdx++) {
const keyBytes = await this.storage.readBytes(this.prefixedStorageKey(`privateKey${keyIdx}`));
if (!keyBytes) {
console.warn(`Failed to load private key ${keyIdx}`);
continue;
}
const key = YlidePrivateKey.fromBytes(keyBytes);
this._addLocalPrivateKey(key);
}
}
private async loadRemotePublicKeys() {
this.remotePublicKeys = new Map();
const publicKeysLength = await this.storage.readJSON<number>(this.prefixedStorageKey('publicKeysLength'));
if (!publicKeysLength) {
return;
}
for (let keyIdx = 0; keyIdx < publicKeysLength; keyIdx++) {
const keyBytes = await this.storage.readBytes(this.prefixedStorageKey(`publicKey${keyIdx}`));
if (!keyBytes) {
console.warn(`Failed to load public key ${keyIdx}`);
continue;
}
const key = RemotePublicKey.fromBytes(keyBytes);
this._addRemotePublicKey(key);
}
}
private async saveLocalPrivateKeys() {
let keyIdx = 0;
for (const [address, keys] of this.localPrivateKeys.entries()) {
for (const key of keys) {
await this.storage.storeBytes(this.prefixedStorageKey(`privateKey${keyIdx}`), key.toBytes());
keyIdx++;
}
}
await this.storage.storeJSON(this.prefixedStorageKey('privateKeysLength'), keyIdx);
}
private async saveRemotePublicKeys() {
let keyIdx = 0;
for (const [address, keys] of this.remotePublicKeys.entries()) {
for (const key of keys) {
await this.storage.storeBytes(this.prefixedStorageKey(`publicKey${keyIdx}`), key.toBytes());
keyIdx++;
}
}
await this.storage.storeJSON(this.prefixedStorageKey('publicKeysLength'), keyIdx);
}
/**
* Method to load keys from internal storage
*/
async load() {
const initialized = await this.storage.readJSON<boolean>(this.prefixedStorageKey('init'));
if (!initialized) {
return;
}
await this.loadLocalPrivateKeys();
await this.loadRemotePublicKeys();
}
/**
* Method to clear internal storage
*/
async clear() {
const keys = await this.storage.getKeys();
for (const key of keys) {
if (key.startsWith(this.pfx)) {
await this.storage.delete(key);
}
}
}
/**
* Method to save keys to internal storage
*/
async save() {
await this.clear();
await this.storage.storeJSON(this.prefixedStorageKey('init'), true);
await this.saveLocalPrivateKeys();
await this.saveRemotePublicKeys();
}
private _addLocalPrivateKey(key: YlidePrivateKey) {
const existingKeys = this.localPrivateKeys.get(key.address);
if (!existingKeys) {
this.localPrivateKeys.set(key.address, [key]);
} else {
const existingKeyIdx = existingKeys.findIndex(k => k.publicKey.equals(key.publicKey));
if (existingKeyIdx === -1) {
existingKeys.push(key);
} else {
const existingKey = existingKeys[existingKeyIdx];
// check if key availability state is higher
if (
(existingKey.availabilityState === PrivateKeyAvailabilityState.UNAVAILABLE &&
key.availabilityState !== PrivateKeyAvailabilityState.UNAVAILABLE) ||
(existingKey.availabilityState === PrivateKeyAvailabilityState.ENCRYPTED &&
key.availabilityState === PrivateKeyAvailabilityState.AVAILABLE)
) {
existingKeys.splice(existingKeyIdx, 1, key);
}
}
}
}
private _addRemotePublicKey(key: RemotePublicKey) {
const existingKeys = this.remotePublicKeys.get(key.address);
if (!existingKeys || !existingKeys.find(k => k.publicKey.equals(key.publicKey))) {
const keys = this.remotePublicKeys.get(key.address) ?? [];
keys.push(key);
this.remotePublicKeys.set(key.address, keys);
}
const isPrivateKeyExists = this.localPrivateKeys
.get(key.address)
?.some(pk => pk.publicKey.equals(key.publicKey));
if (!isPrivateKeyExists) {
const newPrivateKey = new YlidePrivateKey(key.blockchainGroup, key.address, key.publicKey, null);
this._addLocalPrivateKey(newPrivateKey);
}
}
private _removeLocalPrivateKey(key: YlidePrivateKey) {
const existingKeys = this.localPrivateKeys.get(key.address);
if (existingKeys) {
const existingKeyIdx = existingKeys.findIndex(k => k === key);
if (existingKeyIdx !== -1) {
existingKeys.splice(existingKeyIdx, 1);
}
}
}
private _removeRemotePublicKey(key: RemotePublicKey) {
const existingKeys = this.remotePublicKeys.get(key.address);
if (existingKeys) {
const existingKeyIdx = existingKeys.findIndex(k => k === key);
if (existingKeyIdx !== -1) {
existingKeys.splice(existingKeyIdx, 1);
}
}
}
public async addLocalPrivateKey(key: YlidePrivateKey) {
this._addLocalPrivateKey(key);
await this.save();
}
public async addRemotePublicKey(key: RemotePublicKey) {
this._addRemotePublicKey(key);
await this.save();
}
public async addRemotePublicKeys(keys: RemotePublicKey[]) {
for (const key of keys) {
this._addRemotePublicKey(key);
}
await this.save();
}
public async removeLocalPrivateKey(key: YlidePrivateKey) {
this._removeLocalPrivateKey(key);
await this.save();
}
public async removeRemotePublicKey(key: RemotePublicKey) {
this._removeRemotePublicKey(key);
await this.save();
}
public async removeRemotePublicKeys(keys: RemotePublicKey[]) {
for (const key of keys) {
this._removeRemotePublicKey(key);
}
await this.save();
}
public async instantiateNewPrivateKey(
blockchainGroup: string,
address: string,
keyVersion: YlideKeyVersion,
availabilityState: PrivateKeyAvailabilityState,
handlers: YlidePrivateKeyHandlers,
) {
if (availabilityState === PrivateKeyAvailabilityState.ENCRYPTED && !handlers.onYlidePasswordRequest) {
throw new YlideMisusageError(
'YlideKeyRegistry',
'Ylide password request handler is not set, but it is required for the encryption of YlideKey',
);
}
const result = await getPrivateKey(keyVersion, address, 1, handlers);
const { privateKeyBytes } = result;
let { password } = result;
const publicKeyBytes = naclbox.keyPair.fromSecretKey(privateKeyBytes).publicKey;
const publicKey = new PublicKey(PublicKeyType.YLIDE, keyVersion, publicKeyBytes);
const key = new YlidePrivateKey(blockchainGroup, address, publicKey, {
isEncrypted: false,
privateKey: privateKeyBytes,
});
if (availabilityState === PrivateKeyAvailabilityState.ENCRYPTED) {
if (!password) {
const ylidePassword = await handlers.onYlidePasswordRequest!(address);
if (!ylidePassword) {
throw new YlideError(YlideErrorType.WAS_CANCELLED, `Can't encrypt YlideKey without Ylide password`);
}
password = ylidePassword;
}
await key.storeEncrypted(password);
} else if (availabilityState === PrivateKeyAvailabilityState.UNAVAILABLE) {
await key.storeNothing();
}
return key;
}
public getLocalPrivateKeyForRemotePublicKey(publicKey: RemotePublicKey): YlidePrivateKey | null {
const keys = this.localPrivateKeys.get(publicKey.address) ?? [];
for (const key of keys) {
if (key.publicKey.equals(publicKey.publicKey)) {
return key;
}
}
return null;
}
public getLocalPrivateKeyForPublicKeySignature(
address: string,
publicKeySignature: number,
): YlidePrivateKey | null {
return this.localPrivateKeys.get(address)?.find(k => k.publicKey.signature === publicKeySignature) ?? null;
}
public getLocalPrivateKeys(address: string): YlidePrivateKey[] {
return this.localPrivateKeys.get(address) ?? [];
}
public getRemotePublicKeys(address: string): RemotePublicKey[] {
return this.remotePublicKeys.get(address) ?? [];
}
public getFreshestRemotePublicKey(address: string): RemotePublicKey | null {
const keys = this.getRemotePublicKeys(address);
let freshestKey: RemotePublicKey | null = null;
for (const key of keys) {
if (freshestKey === null || freshestKey.timestamp < key.timestamp) {
freshestKey = key;
}
}
return freshestKey;
}
}