-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyring.js
210 lines (184 loc) · 5.73 KB
/
keyring.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const { unpack, keyRing, derive } = require('ara-network/keys')
const { readFile } = require('fs')
const { resolve } = require('path')
const { lstat } = require('fs')
const { DID } = require('did-uri')
const crypto = require('ara-crypto')
const http = require('http')
const pify = require('pify')
const url = require('url')
const ss = require('ara-secret-storage')
const rc = require('ara-runtime-configuration')()
const { MissingParamError } = require('./errors')
/**
* Checks if a keyring exists on local system
*
* @param {String} opts.keyring Path to the keyring
*
* @return {Boolean} Whether the keyring exists
*/
async function exists(keyring) {
if (!keyring) {
throw new MissingParamError({
expected: 'keyring',
actual: keyring,
suggestion: 'setting `rc.network.identity.keyring`'
})
}
try {
const uri = url.parse(keyring)
let result
if ('http:' === uri.protocol && 'https:' === uri.protocol) {
result = await http.request({ ...uri, method: 'HEAD' })
} else {
result = await pify(lstat)(resolve(keyring))
}
return Boolean(result)
} catch (e) {
throw new Error(`Error occurred while checking keyring ${keyring} existence`, e)
}
}
/**
* Retrieve secret keys of a specific key
*
* @param {String} opts.identity Identity of keyring owner
* @param {String} opts.network Network name to be retrieved
* @param {String} opts.password Password of the identity
* @param {String} [opts.keyring] Path to keyring
*
* @return {Object} Unpacked keyring
*/
async function getSecret(opts) {
if (!opts) {
throw new MissingParamError({
expected: 'opts',
actual: null
})
} else if ('object' !== typeof opts) {
throw new TypeError('Passed `opts` are not object')
} else if (!opts.network) {
throw new MissingParamError({
expected: 'opts.network',
actual: opts,
})
}
if (!opts.keyring) {
throw new MissingParamError({
expected: 'opts.keyring',
actual: opts,
suggestion: 'setting `rc.network.identity.keyring`'
})
} else if (!opts.password) {
throw new MissingParamError({
expected: 'opts.password',
actual: opts
})
} else if (!opts.identity) {
throw new MissingParamError({
expected: 'opts.identity',
actual: opts
})
}
try {
const parsedDID = new DID(opts.identity)
const publicKey = Buffer.from(parsedDID.identifier, 'hex')
opts.password = crypto.blake2b(Buffer.from(opts.password))
const hash = crypto.blake2b(publicKey).toString('hex')
const path = resolve(rc.network.identity.root, hash, 'keystore/ara')
const idFile = await pify(readFile)(path, 'utf8')
const keystore = JSON.parse(idFile)
const secretKey = ss.decrypt(keystore, { key: opts.password.slice(0, 16) })
if (!await exists(opts.keyring)) {
throw new Error(`Could not find ${opts.keyring}`)
}
const keyring = keyRing(opts.keyring, { secret: secretKey })
if (!await keyring.has(opts.network)) {
throw new Error(`Could not find '${opts.network}' in ${opts.keyring}`)
}
const buffer = await keyring.get(opts.network)
const unpacked = unpack({ buffer })
const kp = derive({ secretKey, name: opts.network })
return {
...unpacked,
identity: {
publicKey: kp.publicKey,
secretKey: kp.secretKey
}
}
} catch (e) {
throw new Error(`Error occurred while getting secret key of ${opts.network} (${e})`)
}
}
/**
* Retrieve public keys of a specific key
*
* @param {String} opts.network Network name to be retrieved from keyring
* @param {String} opts.secret Secret for decrypting keyring
* @param {String} [opts.keyring] Path to keyring
*
* @return {Object} Unpacked keyring
*/
async function getPublic(opts) {
if (!opts) {
throw new MissingParamError({
expected: 'opts',
actual: null
})
} else if ('object' !== typeof opts) {
throw new TypeError('Passed `opts` are not object')
} else if (!opts.network) {
throw new MissingParamError({
expected: 'opts.network',
actual: opts,
})
}
if (!opts.keyring) {
throw new MissingParamError({
expected: 'opts.keyring',
actual: opts,
suggestion: 'setting `rc.network.identity.keyring`'
})
} else if (!opts.secret) {
throw new MissingParamError({
expected: 'opts.secret',
actual: opts
})
} else if (!opts.identity) {
throw new MissingParamError({
expected: 'opts.identity',
actual: opts
})
} else if (!opts.password) {
throw new MissingParamError({
expected: 'opts.password',
actual: opts
})
}
try {
const parsedDID = new DID(opts.identity)
const publicKey = Buffer.from(parsedDID.identifier, 'hex')
opts.password = crypto.blake2b(Buffer.from(opts.password))
const hash = crypto.blake2b(publicKey).toString('hex')
const path = resolve(rc.network.identity.root, hash, 'keystore/ara')
const idFile = await pify(readFile)(path, 'utf8')
const keystore = JSON.parse(idFile)
const secretKey = ss.decrypt(keystore, { key: opts.password.slice(0, 16) })
if (!await exists(opts.keyring)) {
throw new Error(`Could not find ${opts.keyring}`)
}
const keyring = keyRing(opts.keyring, { secret: Buffer.from(opts.secret) })
if (!await keyring.has(opts.network)) {
throw new Error(`Could not find '${opts.network}' in ${opts.keyring}`)
}
const buffer = await keyring.get(opts.network)
const unpacked = unpack({ buffer })
return { identity: { publicKey, secretKey }, ...unpacked }
} catch (e) {
throw new Error(`Error occurred while getting public key of ${opts.network}: ${e}`)
}
}
module.exports = {
exists,
getPublic,
getSecret,
}