-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
290 lines (250 loc) · 7.35 KB
/
create.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-disable no-shadow */
const { kEd25519VerificationKey2018 } = require('ld-cryptosuite-registry')
const { createAFSKeyPath, getCache } = require('./key-path')
const { defaultStorage } = require('./storage')
const hasDIDMethod = require('has-did-method')
const { createCFS } = require('cfsnet/create')
const crypto = require('ara-crypto')
const aid = require('ara-identity')
const extend = require('extend')
const pify = require('pify')
const debug = require('debug')('ara-filesystem:create')
const {
KEY_LENGTH,
AID_PREFIX,
OWNER_SUFFIX
} = require('./constants')
const {
proxyExists,
getProxyAddress
} = require('ara-contracts/registry')
const {
validate,
transform: { toHexString }
} = require('ara-util')
/**
* Creates an AFS with the given Ara identity
* @param {Object} opts
* @param {String} opts.did
* @param {String} opts.owner
* @param {?Object} opts.ddo
* @param {String} opts.password
* @param {String} opts.afsPassword
* @param {Function} opts.storage
* @param {Object} [opts.keyringOpts]
* @param {Object} [opts.keyringOpts.archiver]
* @param {Object} [opts.keyringOpts.resolver]
* @return {Object}
*/
async function create(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if (('string' !== typeof opts.owner || !opts.owner)
&& ('string' !== typeof opts.did || !opts.did)) {
throw new TypeError('Expecting non-empty string.')
} else if (opts.ddo && 'object' !== typeof opts.ddo) {
throw new TypeError('Expecting opts.ddo to be an object.')
} else if (opts.password && 'string' !== typeof opts.password) {
throw TypeError('Expecting non-empty password.')
} else if (opts.afsPassword && 'string' !== typeof opts.afsPassword) {
throw TypeError('Expecting non-empty password.')
} else if (opts.storage && 'function' !== typeof opts.storage) {
throw new TypeError('Expecting storage to be a function.')
}
let {
did,
ddo,
keyringOpts = {},
afsPassword
} = opts
const {
owner,
password,
storage
} = opts
if (password && afsPassword && password === afsPassword) {
throw new Error('Expecting opts.afsPassword to be different from opts.password.')
}
keyringOpts = extend(true, {
archiver: {
network: (keyringOpts.archiver && keyringOpts.archiver.network) || keyringOpts.network,
secret: (keyringOpts.archiver && keyringOpts.archiver.secret) || keyringOpts.secret,
keyring: (keyringOpts.archiver && keyringOpts.archiver.keyring) || keyringOpts.keyring
},
resolver: {
network: (keyringOpts.resolver && keyringOpts.resolver.network) || keyringOpts.network,
secret: (keyringOpts.resolver && keyringOpts.resolver.secret) || keyringOpts.secret,
keyring: (keyringOpts.resolver && keyringOpts.resolver.keyring) || keyringOpts.keyring
}
}, keyringOpts)
afsPassword = afsPassword || password
let afs
let mnemonic
const writable = Boolean(afsPassword)
const cache = await getCache()
if (did) {
let proxy
if (!ddo) {
if (await proxyExists(did)) {
proxy = await getProxyAddress(did)
}
}
try {
({ did, ddo } = await validate({
did,
password: afsPassword,
label: 'create',
ddo,
keyringOpts: keyringOpts.resolver
}))
} catch (err) {
throw err
}
const etcKeyMatcher = key => 'metadata' === key.id.split('#')[1]
const [ { publicKeyHex: etcKey } ] = ddo.publicKey.filter(etcKeyMatcher)
// retrieve local options from cache
let cachedOpts = await pify(cache.read)(did) || {}
if (!cachedOpts.id || !cachedOpts.path) {
cachedOpts = extend(true, cachedOpts, {
id: did,
path: createAFSKeyPath(did),
})
await pify(cache.write)(did, cachedOpts)
}
opts = extend(true, cachedOpts, opts, {
did,
storage: defaultStorage(did, writable, storage, proxy),
key: Buffer.from(did, 'hex'),
partitions: {
etc: {
key: etcKey
}
}
})
try {
afs = await createCFS(opts)
} catch (err) {
await pify(cache.delete)(did)
throw err
}
afs.did = did
afs.ddo = ddo
} else if (owner) {
try {
({ owner: did } = await validate({
did: owner,
password,
label: 'create',
ddo,
keyringOpts: keyringOpts.resolver
}))
} catch (err) {
throw err
}
const afsId = await createIdentity({ password: afsPassword, owner })
// Note: Do not change this to `({ mnemonic } = afsId)`, it causes a weird scoping issue.
// eslint-disable-next-line prefer-destructuring
mnemonic = afsId.mnemonic
const { publicKey, secretKey } = afsId
const afsDid = toHexString(publicKey)
let afsDdo
try {
// generate AFS key path
const path = createAFSKeyPath(afsDid)
afs = await createCFS({
id: afsDid,
key: publicKey,
secretKey,
path,
storage: defaultStorage(afsDid, writable, storage)
})
// add local options to cache
const cachedOpts = {
id: afsDid,
path,
}
await pify(cache.write)(afsDid, cachedOpts)
// metadata partition publicKey
const metadataPublicKey = toHexString(afs.partitions.etc.key)
// recreate identity with additional publicKey
const afsId = await createIdentity({
password: afsPassword,
mnemonic,
owner,
metadataPublicKey
})
await aid.util.writeIdentity(afsId)
if (!ddo) {
await aid.archive(afsId, keyringOpts.archiver)
afsDdo = await aid.resolve(toHexString(afsId.publicKey), keyringOpts.resolver)
if (null == afsDdo || 'object' !== typeof afsDdo) {
throw new TypeError('AFS identity not successfully resolved.')
}
} else {
afsDdo = JSON.parse(JSON.stringify(afsId.ddo))
}
} catch (err) {
throw err
}
afs.did = afsDid
afs.ddo = afsDdo
// clear buffers
publicKey.fill(0)
secretKey.fill(0)
}
return {
afs,
mnemonic
}
}
async function createIdentity({
password = '',
owner = '',
metadataPublicKey = '',
mnemonic
} = {}) {
if (null == password || 'string' !== typeof password) {
throw new TypeError('Expecting password to be non-empty string.')
}
if (null == owner || 'string' !== typeof owner) {
throw new TypeError('Expecting non-empty string.')
}
if ((hasDIDMethod(owner) && KEY_LENGTH !== owner.slice(AID_PREFIX.length).length)
|| (!hasDIDMethod(owner) && KEY_LENGTH !== owner.length)) {
throw new TypeError('Owner identifier must be 64 chars.')
}
owner += OWNER_SUFFIX
if (!hasDIDMethod(owner)) {
owner = `${AID_PREFIX}${owner}`
}
let publicKey
if (metadataPublicKey) {
const buf = Buffer.from(metadataPublicKey, 'hex')
publicKey = [ {
id: 'metadata',
publicKeyHex: metadataPublicKey,
publicKeyBase64: crypto.base64.encode(buf).toString(),
publicKeyBase58: crypto.base58.encode(buf).toString()
} ]
}
let identity
try {
const ddo = {
authentication: {
type: kEd25519VerificationKey2018,
publicKey: owner
},
publicKey
}
identity = await aid.create({
mnemonic,
password,
ddo
})
} catch (err) { debug(err.stack || err) }
return identity
}
module.exports = {
create,
createIdentity
}