-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicate.js
160 lines (131 loc) · 3.76 KB
/
replicate.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
/* eslint-disable no-await-in-loop */
const { createSwarm } = require('ara-network/discovery')
const { createCFS } = require('cfsnet/create')
const { DID } = require('did-uri')
const pify = require('pify')
const pump = require('pump')
const ram = require('random-access-memory')
const { toHex } = require('./util')
const kDIDIdentifierLength = 64
// in milliseconds
const kDefaultTimeout = 10000
/**
* Replicate & Download a DID's identity files from a remote server
* @public
* @param {String} identity
* @param {Object} opts
* @param {String} opts.timeout (optional)
* @return {Object}
*/
async function replicate(identity, opts) {
if (null === identity || 'string' !== typeof identity) {
throw new TypeError('Expecting a valid identity string to replicate.')
}
if (0 !== identity.indexOf('did:ara:')) {
// eslint-disable-next-line no-param-reassign
identity = `did:ara:${identity}`
}
const did = new DID(identity)
if (!did.identifier || kDIDIdentifierLength !== did.identifier.length) {
throw new TypeError('Invalid DID identifier length.')
}
if (!opts.timeout) {
// eslint-disable-next-line no-param-reassign
opts.timeout = kDefaultTimeout
}
const value = await findFiles(did, opts)
return value
}
async function findFiles(did, opts) {
let timeout = null
return pify(async (done) => {
timeout = setTimeout(onexpire, opts.timeout)
const key = Buffer.from(did.identifier, 'hex')
const id = toHex(key)
const cfs = await createCFS({
key,
id,
sparseMetadata: true,
shallow: true,
storage: ram,
sparse: true,
})
cfs.discovery = createSwarm({
stream: () => cfs.replicate(),
id: cfs.discoveryKey
})
cfs.discovery.on('connection', onconnection)
cfs.discovery.join(cfs.discoveryKey)
await Promise.race([
new Promise((resolve) => cfs.once('update', resolve)),
new Promise((resolve) => cfs.once('sync', resolve)),
])
await onupdate()
function onconnection(connection, peer) {
void peer
clearTimeout(timeout)
timeout = setTimeout(onexpire, opts.timeout)
const stream = cfs.replicate({
download: true,
upload: true,
live: false,
})
pump(connection, stream, connection)
}
async function onupdate() {
clearTimeout(timeout)
try {
const identityFiles = []
const response = {
publicKey: Buffer.from(did.identifier),
files: identityFiles,
ddo: null,
did
}
const files = await cfs.readdir('.')
for (const file of files) {
if ('keystore' === file) {
const ethKeystore = await cfs.readFile('keystore/eth')
const araKeystore = await cfs.readFile('keystore/ara')
identityFiles.push({
path: 'keystore/eth',
buffer: ethKeystore
}, {
path: 'keystore/ara',
buffer: araKeystore
})
} else {
const content = await cfs.readFile(file)
if ('ddo.json' === file) {
response.ddo = JSON.parse(content.toString('utf8'))
}
identityFiles.push({
path: file,
buffer: content
})
}
}
done(null, response)
onclose()
} catch (err) {
done(new Error(err))
onclose()
}
}
async function onclose() {
if (cfs && cfs.discovery) {
cfs.discovery.destroy(done)
cfs.discovery = null
} else {
done(null)
}
}
async function onexpire() {
clearTimeout(timeout)
done(new Error('Could not replicate DID from peer. Request Timed out'))
}
})()
}
module.exports = {
replicate
}