-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
195 lines (165 loc) · 5.32 KB
/
cli.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
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
const vorpal = require('vorpal')();
const { documentLoader } = require('./documentLoader');
const { PgpKeyPair2021, PgpSignature2021 } = require('../dist');
const { sign } = require('@transmute/linked-data-proof');
const { ld } = require('@transmute/vc.js');
const { AssertionProofPurpose } = ld;
const { version } = require('../package.json');
vorpal.wait = seconds =>
new Promise(resolve => {
setTimeout(resolve, seconds * 1000);
});
vorpal.command('version', 'display version').action(async () => {
// eslint-disable-next-line
console.log(
JSON.stringify(
{
'lds-pgp2021': version,
},
null,
2
)
);
return vorpal.wait(1);
});
vorpal.command('resolve <did>', 'resolve a did').action(async args => {
let { document } = await documentLoader(args.did);
// eslint-disable-next-line
console.log(JSON.stringify(document, null, 2));
return vorpal.wait(1);
});
vorpal
.command(
'make-json-key <inputFilePath> <controller>',
'convert a public gpg key to json'
)
.action(async args => {
const publicKeyString = fs
.readFileSync(args.inputFilePath)
.toString('utf-8');
const options = {
id: '',
controller: args.controller,
publicKeyPgp: publicKeyString,
};
const key = await PgpKeyPair2021.from(options);
key.id = key.controller + '#' + (await key.fingerprint());
// eslint-disable-next-line
console.log(JSON.stringify(key.toKeyPair(false), null, 2));
return vorpal.wait(1);
});
vorpal
.command(
'import-gpg-keys-from-json <inputFilePath>',
'imports gpg keys from a json object'
)
.action(async args => {
const fileJson = fs.readFileSync(args.inputFilePath).toString('utf-8');
let parsedInputFile;
try {
parsedInputFile = JSON.parse(fileJson);
} catch (e) {
throw new Error('Could not parse inputFilePath as JSON.');
}
const command = `
echo "${parsedInputFile.publicKeyGpg}" | gpg --import;
echo "${parsedInputFile.privateKeyGpg}" | gpg --import;
`;
const result = shell.exec(command, { silent: true });
return vorpal.wait(1);
});
vorpal
.command('sign <inputFilePath> <verificationMethod>', 'Sign file')
.option(
'-u, --local-user <key>',
'Use name as the key to sign with. Note that this option overrides --default-key.'
)
.option('-o, --output <outputFilePath>', 'Write output to file')
.option('-p, --purpose <proofPurpose>', 'Purpose of signature')
.option('-c, --created <created>', 'Created date as iso string')
.action(async args => {
// set defaults properly
args.options.created = args.options.created || new Date().toISOString();
args.options.purpose = args.options.purpose || 'assertionMethod';
const fileJson = fs.readFileSync(args.inputFilePath).toString('utf-8');
let parsedInputFile;
try {
parsedInputFile = JSON.parse(fileJson);
} catch (e) {
throw new Error('Could not parse inputFilePath as JSON.');
}
let suite = new PgpSignature2021();
const vm = await suite.getVerificationMethod({
proof: { verificationMethod: args.verificationMethod },
documentLoader,
});
const key = await PgpKeyPair2021.from({
id: vm.id,
controller: vm.controller.id,
publicKeyPgp: vm['sec:publicKeyPgp'],
});
key.signer = () => {
return {
sign: ({ data }) => {
fs.writeFileSync(path.resolve(process.cwd(), 'data.dat'), data);
const keyName = args.options['local-user']
? `-u ${args.options['local-user']}`
: `--default-key`;
const signDetachedCommand = `
gpg --detach-sign --armor ${keyName} ${path.resolve(
process.cwd(),
'data.dat'
)}
`;
shell.exec(signDetachedCommand, { silent: true });
const signatureValue = fs.readFileSync('data.dat.asc').toString();
fs.unlinkSync(path.resolve(process.cwd(), 'data.dat'));
fs.unlinkSync('data.dat.asc');
return signatureValue;
},
};
};
suite = new PgpSignature2021({
key,
});
const signed = await sign(parsedInputFile, {
compactProof: true,
documentLoader: documentLoader,
purpose: new AssertionProofPurpose(),
suite,
});
const signedDocument = JSON.stringify(signed, null, 2);
console.log(signedDocument);
if (args.options.output) {
fs.writeFileSync(args.options.output, signedDocument);
}
return vorpal.wait(1);
});
vorpal
.command('verify <inputFilePath>', 'Verify file')
.action(async args => {
const fileJson = fs.readFileSync(args.inputFilePath).toString('utf-8');
let parsedInputFile;
try {
parsedInputFile = JSON.parse(fileJson);
} catch (e) {
throw new Error('Could not parse inputFilePath as JSON.');
}
const result = await jsigs.verify(parsedInputFile, {
compactProof: true,
documentLoader: documentLoader,
purpose: new AssertionProofPurpose(),
suite: new PgpSignature2021(),
});
// console.log(result)
console.log(JSON.stringify({ verified: result.verified }, null, 2));
return vorpal.wait(1);
});
vorpal.parse(process.argv);
if (process.argv.length === 0) {
vorpal.delimiter('🔏 ').show();
}