-
Notifications
You must be signed in to change notification settings - Fork 2
/
NodeManager.ts
51 lines (48 loc) · 1.71 KB
/
NodeManager.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
import { KeyStoreSet } from '../../keyStores/KeyStoreSet';
import { NodeCryptoOptions } from '../NodeCryptoOptions';
import { NodeConstructor } from './NodeConstructor';
import { getRSAPublicKeyFromPrivate } from '../../crypto/keys/generation';
import { PayloadPlaintext } from '../../messages/payloads/PayloadPlaintext';
import { PeerInternetAddress } from '../peer';
import { Node } from '../Node';
export abstract class NodeManager<
Payload extends PayloadPlaintext,
PeerAddress extends PeerInternetAddress,
> {
protected abstract readonly defaultNodeConstructor: NodeConstructor<Payload, PeerAddress>;
constructor(
public keyStores: KeyStoreSet,
protected cryptoOptions: Partial<NodeCryptoOptions> = {},
) {}
/**
* Get node by `id`.
*
* @param id
*/
public async get(id: string): Promise<Node<Payload, PeerAddress> | null>;
/**
* Get node by `id` but return instance of custom `customNodeClass`.
*
* @param id
* @param customNodeClass
*/
public async get<N extends Node<Payload, PeerAddress>>(
id: string,
customNodeClass: NodeConstructor<Payload, PeerAddress>,
): Promise<N | null>;
public async get(
id: string,
nodeConstructor?: NodeConstructor<Payload, PeerAddress>,
): Promise<Node<Payload, PeerAddress> | null> {
const nodePrivateKey = await this.keyStores.privateKeyStore.retrieveIdentityKey(id);
if (!nodePrivateKey) {
return null;
}
const nodeKeyPair: CryptoKeyPair = {
privateKey: nodePrivateKey,
publicKey: await getRSAPublicKeyFromPrivate(nodePrivateKey),
};
const constructor = nodeConstructor ?? this.defaultNodeConstructor;
return new constructor(id, nodeKeyPair, this.keyStores, this.cryptoOptions);
}
}