-
Notifications
You must be signed in to change notification settings - Fork 0
/
ara-network-keys
executable file
·253 lines (216 loc) · 6.36 KB
/
ara-network-keys
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
#!/usr/bin/env node
const { resolve, basename } = require('path')
const { warn, error } = require('ara-console')
const inquirer = require('inquirer')
const { DID } = require('did-uri')
const program = require('yargs')
const crypto = require('ara-crypto')
const debug = require('debug')('ara:network:keys')
const keys = require('../keys')
const pify = require('pify')
const pkg = require('../package.json')
const rc = require('../rc')()
const ss = require('ara-secret-storage')
const fs = require('fs')
const $0 = basename(process.argv[1] || 'ara-network-keys')
process.on('unhandledRejection', onfatal)
process.on('uncaughtException', onfatal)
// program usage
program
.usage('usage: $0 [-hDPV] [options]')
.wrap(null)
// booleans
program
.group([ 'help', 'version', 'debug' ], 'General Options:')
.alias('help', 'h')
.alias('version', 'V')
.option('debug', {
alias: 'D',
describe: 'Enable debug output'
})
// parameters
program
.group([ 'identity' ], 'Identity Options:')
program.option('identity', {
alias: 'i',
default: rc.network.identity.whoami,
requiresArg: true,
required: true,
defaultDescription: (
rc.network.identity.whoami
? `${rc.network.identity.whoami.slice(0, 16)}...`
: undefined
),
describe:
`A valid, local, and resolvable Ara identity DID
URI of the owner of the given keyring. You will be
prompted for the associated passphrase`,
})
program
.group([ 'personal', 'secret', 'network', 'keyring' ], 'Keyring Options:')
program.option('personal', {
alias: 'P',
describe:
`Generate personal network keys for given identity.
Personal network keys are stored in the public and
secret keyrings named by the given identity's
public and secret key respectively`
})
program.option('secret', {
alias: 's',
requiresArg: true,
required: true,
describe:
`A shared secret key value or a path to a file that
contains a shared secret key`,
})
program.option('network', {
alias: 'n',
requiresArg: true,
required: true,
describe:
`A human readable network name for keys generated
and appended to the given keyring`,
})
program.option('keyring', {
alias: 'k',
default: rc.network.identity.keyring || rc.network.keyring,
describe: 'Keyring output path',
required: true,
requiresArg: true,
})
program
.epilog(`
For documentation and more information, please visit ${pkg.homepage}
For bugs and other issues, please visit ${pkg.bugs.url}`)
// remove type boolean type annotations
// see https://github.com/yargs/yargs/issues/319
void program.getOptions().boolean.splice(-2)
const { argv } = program
// eslint-disable-next-line wrap-iife
void async function main() {
process.title = $0
if (program.argv.debug) {
/* eslint-disable-next-line global-require */
require('debug').enable('ara:network*')
}
if (!argv.secret) {
const err = 'Missing shared secret. See \'ank --help\' for command usage.'
onfatal(new Error(err))
return
}
if (argv.name && 'string' === typeof argv.name && !argv.network) {
warn('Please use \'--network\' instead of \'--name\'.')
// eslint-disable-next-line no-param-reassign
argv.network = argv.name
}
if (!argv.personal && !argv.network) {
onfatal(new Error('Network name required for non-personal keyrings'))
return
}
let publicKey = null
let secretKey = null
let password = null
let keystore = null
let secret = null
let buffer = null
try {
const stat = await pify(fs.stat)(argv.secret)
if (stat.isFile()) {
secret = await pify(fs.readFile)(argv.secret)
}
} catch (err) {
void err
}
if (argv.password) {
({ password } = argv)
} else {
({ password } = await inquirer.prompt([ {
type: 'password',
name: 'password',
message:
'Please enter the passphrase associated with the signing identity.\n' +
'Passphrase:'
} ]))
}
if (argv.identity && 0 !== argv.identity.indexOf('did:ara:')) {
argv.identity = `did:ara:${argv.identity}`
}
const did = new DID(argv.identity)
try {
secret = Buffer.from(argv.secret)
password = crypto.blake2b(Buffer.from(password))
publicKey = Buffer.from(did.identifier, 'hex')
} catch (err) {
debug(err)
onfatal(new Error('Failed to compute BLAKE2b password hash'))
}
try {
// @TODO(jwerle): this should a function of AID
const hash = crypto.blake2b(publicKey).toString('hex')
const path = resolve(rc.network.identity.root, hash, 'keystore/ara')
keystore = JSON.parse(await pify(fs.readFile)(path, 'utf8'))
} catch (err) {
debug(err)
onfatal(new Error('Failed to read identity keystore'))
return
}
try {
secretKey = ss.decrypt(keystore, { key: password.slice(0, 16) })
} catch (err) {
debug(err)
onfatal(new Error('Invalid passphrase'))
return
}
argv.keyring = resolve(argv.keyring)
try {
if (argv.personal) {
buffer = keys.generate({ secret, publicKey, secretKey })
} else {
const keyPair = keys.derive({
secretKey, name: argv.network
})
buffer = keys.generate({
secret,
publicKey: keyPair.publicKey,
secretKey: keyPair.secretKey
})
}
} catch (err) {
onfatal(err)
}
try {
const secretKeyring = keys.keyRing(argv.keyring, { secret: secretKey })
const publicKeyring = keys.keyRing(`${argv.keyring}.pub`, { secret })
if (argv.personal) {
if (false === await publicKeyring.has(publicKey)) {
await publicKeyring.append(publicKey, buffer.slice(0, keys.PK_SIZE))
} else {
warn('Public personal keys already exists in public key ring')
}
if (false === await secretKeyring.has(secretKey)) {
await secretKeyring.append(secretKey, buffer.slice(keys.PK_SIZE))
} else {
warn('Secret personal keys already exists in secret key ring')
}
} else {
if (false === await publicKeyring.has(argv.network)) {
await publicKeyring.append(argv.network, buffer.slice(0, keys.PK_SIZE))
} else {
warn(`'${argv.network}' already exists in public key ring`)
}
if (false === await secretKeyring.has(argv.network)) {
await secretKeyring.append(argv.network, buffer.slice(keys.PK_SIZE))
} else {
warn(`'${argv.network}' already exists in secret key ring`)
}
}
} catch (err) {
onfatal(err)
}
}()
function onfatal(err) {
error('fatal:', err.message)
debug(err.stack)
process.exit(1)
}