-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
89 lines (71 loc) · 2.51 KB
/
client.js
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
// This is an executable script which is a CLI interface for the client instance.
import * as z from 'zod';
import { parseArgs } from 'node:util';
import { PORT, USERNAME, PASSWORD } from './defaults.js';
const { port, username, password } = z
.object({
PORT: z.coerce.number().default(PORT),
USERNAME: z.string().default(USERNAME),
PASSWORD: z.string().default(PASSWORD),
})
.transform((v) => ({ port: v.PORT, username: v.USERNAME, password: v.PASSWORD }))
.parse(process.env);
import { createClient } from './src/client/index.js';
import { loadPublicKey, loadPrivateKey } from './src/client/fs.js';
const client = createClient({ username, password, port });
const subcommand = process.argv[2];
switch (subcommand) {
case 'create': {
console.log('Creating key pair...');
client.keys.create();
console.log(` New keypair saved to disk.`);
break;
}
case 'publish': {
console.log('Publishing public key...');
const publicKey = loadPublicKey();
if (!publicKey) {
console.log('No public key found to be published. Try running "create" first.');
process.exit(1);
}
await client.setPublicKey(publicKey);
console.log(` Public key for ${username} published.`);
break;
}
case 'sign': {
const args = parseArgs({ options: { message: { type: 'string', short: 'm' } }, allowPositionals: true });
if (!args.values.message) {
console.log('Please provide a message to sign.');
process.exit(1);
}
const message = Buffer.from(args.values.message);
const signature = client.keys.sign(message);
console.log(message.toString());
console.log();
console.log(signature.toString('base64'));
break;
}
case 'verify': {
const args = parseArgs({
options: { message: { type: 'string', short: 'm' }, signature: { type: 'string', short: 's' } },
allowPositionals: true,
});
if (!args.values.message || !args.values.signature) {
console.log('Please provide a message and signature to verify.');
process.exit(1);
}
const message = Buffer.from(args.values.message);
const signature = Buffer.from(args.values.signature, 'base64');
const isValid = await client.verify(message.toString(), signature);
if (!isValid.ok) {
console.log(`The message was not signed by this signature.`);
process.exit(1);
} else {
console.log(`The message was signed by this signature.`);
}
break;
}
default:
console.log('Invalid subcommand:', subcommand);
process.exit(1);
}