-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCommandJoin.ts
More file actions
74 lines (71 loc) · 2.53 KB
/
Copy pathCommandJoin.ts
File metadata and controls
74 lines (71 loc) · 2.53 KB
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
import type PolykeyClient from 'polykey/PolykeyClient.js';
import type { Hostname } from 'polykey/network/types.js';
import type { NodeIdEncoded, NodeAddress } from 'polykey/nodes/types.js';
import CommandPolykey from '../CommandPolykey.js';
import * as binUtils from '../utils/index.js';
import * as binOptions from '../utils/options.js';
import * as binProcessors from '../utils/processors.js';
class CommandJoin extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('join');
this.description('Join a network');
this.argument('<network>', 'Name of the network to join');
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
this.action(async (network: string, options) => {
const { default: PolykeyClient } = await import(
'polykey/PolykeyClient.js'
);
const nodesUtils = await import('polykey/nodes/utils.js');
const utils = await import('polykey/utils/index.js');
const clientOptions = await binProcessors.processClientOptions(
options.nodePath,
options.nodeId,
options.clientHost,
options.clientPort,
this.fs,
this.logger.getChild(binProcessors.processClientOptions.name),
);
const auth = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
});
try {
pkClient = await PolykeyClient.createPolykeyClient({
nodeId: clientOptions.nodeId,
host: clientOptions.clientHost,
port: clientOptions.clientPort,
options: {
nodePath: options.nodePath,
},
logger: this.logger.getChild(PolykeyClient.name),
});
const seedNodes = await nodesUtils.resolveSeednodes(
network as Hostname,
);
const initialNodes = Object.entries(seedNodes) as Array<
[NodeIdEncoded, NodeAddress]
>;
await binUtils.retryAuthentication(
(auth) =>
pkClient.rpcClient.methods.nodesSyncGraph({
network,
initialNodes,
metadata: auth,
}),
auth,
);
process.stdout.write(`Switched to network ${network}`);
} finally {
if (pkClient! != null) await pkClient.stop();
}
});
}
}
export default CommandJoin;