-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.js
More file actions
executable file
·443 lines (388 loc) · 14 KB
/
bin.js
File metadata and controls
executable file
·443 lines (388 loc) · 14 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env node
const Corestore = require('corestore')
const process = require('process')
const fs = require('fs').promises
const goodbye = require('graceful-goodbye')
const Multisig = require('hyper-multisig')
const idEnc = require('hypercore-id-encoding')
const SignRequest = require('hypercore-signing-request')
const Hyperdrive = require('hyperdrive')
const Hyperswarm = require('hyperswarm')
const { command, flag, arg, rest, description } = require('paparam')
const z32 = require('z32')
const DEFAULT_CONFIG_PATH = './multisig.json'
const DEFAULT_STORAGE_PATH = './storage'
const DEFAULT_SEED_LOG_INTERVAL = 15000
const cmdLink = command('link', description('Create multisig key'), wrapErrHandler(link))
const cmdRequest = command(
'request',
description('Create signing request'),
flag('--force', 'Skip sanity checks'),
flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
arg('<length>', 'Core length to request'),
wrapErrHandler(request)
)
const cmdVerify = command(
'verify',
description('Verify multisig'),
flag(
'--first-commit',
'Set when this is the first commit to the multisig target, so it skips those checks'
),
flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
arg('request', 'Signing request'),
rest('[...responses]', 'Signing responses'),
wrapErrHandler(verify)
)
const cmdCommit = command(
'commit',
description('Commit multisig'),
flag(
'--first-commit',
'Set when this is the first commit to the multisig target, so it skips those checks'
),
flag('--force-dangerous', 'Advanced option, it might break the core on misuse'),
flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
arg('request', 'Signing request'),
rest('[...responses]', 'Signing responses'),
wrapErrHandler(commit)
)
const cmdSeed = command(
'seed',
description('Seed both the source and multisig cores/drives'),
flag('--log-interval <logInterval>', 'Interval in ms to log replication status').default(
DEFAULT_SEED_LOG_INTERVAL
),
wrapErrHandler(seed)
)
const cmd = command(
'multisig',
flag('--config|-c <config>', `Config file path (default to ${DEFAULT_CONFIG_PATH})`),
flag('--storage|-s <storage>', `Storage path (default to ${DEFAULT_STORAGE_PATH})`),
cmdLink,
cmdRequest,
cmdVerify,
cmdCommit,
cmdSeed,
() => console.log(cmd.help())
)
async function link() {
const { publicKeys, namespace, quorum } = await setup({
withReplication: false,
srcKeyRequired: false
})
const key = Multisig.getCoreKey(publicKeys, namespace, { quorum })
console.info(`pear://${idEnc.normalize(key)}`)
}
async function request() {
const length = +cmdRequest.args.length
const { force, peerUpdateTimeout } = cmdRequest.flags
if (!length) throw new Error('Invalid command')
const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
const multisig = new Multisig(store, swarm)
let request
if (type === 'core') {
const srcCore = store.get({ key: idEnc.decode(srcKey) })
const res = await multisig
.requestCore(publicKeys, namespace, srcCore, length, {
force,
peerUpdateTimeout: peerUpdateTimeout,
quorum,
legacy: true // legacy requests compat with hypercore-sign v3
})
.done()
request = res.request
} else {
const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
const req = await multisig.requestDrive(publicKeys, namespace, srcDrive, length, {
force,
peerUpdateTimeout: peerUpdateTimeout,
quorum,
legacy: true // legacy requests compat with hypercore-sign v3
})
req.on('getting-src-blobs', () => {
console.log('Getting the source blobs...')
})
req.on('verify-db-requestable-start', () => {
console.log('Verifying the db core is requestable....')
})
req.on('getting-blobs-length', () => {
console.log('Getting the blobs length (this can take a while)...')
})
req.on('verify-blobs-requestable-start', () => {
console.log('Verifying the blobs core is requestable...')
})
req.on('creating-drive', () => {
console.log('Creating the drive...')
})
const res = await req.done()
request = res.request
}
printRequest(request)
goodbye.exit()
}
async function verify() {
const { firstCommit, peerUpdateTimeout } = cmdVerify.flags
const request = cmdVerify.args.request
const responses = cmdVerify.rest || []
if (!request) throw new Error('Invalid command')
console.info(`Committing request ${request}`)
console.info(`Responses:\n -${responses.join('\n -')}`)
const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
const multisig = new Multisig(store, swarm)
let runner
if (type === 'core') {
const srcCore = store.get({ key: idEnc.decode(srcKey) })
runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
dryRun: true,
skipTargetChecks: firstCommit,
peerUpdateTimeout: peerUpdateTimeout,
quorum
})
} else {
const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
dryRun: true,
skipTargetChecks: firstCommit,
peerUpdateTimeout: peerUpdateTimeout,
quorum
})
}
setupProgressLogs(runner, type, firstCommit)
const res = await runner.done()
printCommit(res.manifest, res.quorum, res.result, true)
const destKey = type === 'core' ? res.result.destCore.key : res.result.db.destCore.key
console.info(`${type} key: ${destKey} is safe to commit`)
goodbye.exit()
}
async function commit() {
const request = cmdCommit.args.request
const responses = cmdCommit.rest || []
const { firstCommit, forceDangerous, peerUpdateTimeout } = cmdCommit.flags
if (!request || !responses?.length) throw new Error('Invalid command')
console.info(`Committing request ${request}`)
console.info(`Responses:\n -${responses.join('\n -')}`)
const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
const multisig = new Multisig(store, swarm)
let runner
if (type === 'core') {
const srcCore = store.get({ key: idEnc.decode(srcKey) })
runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
skipTargetChecks: firstCommit,
force: forceDangerous,
peerUpdateTimeout: peerUpdateTimeout,
quorum
})
} else {
const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
skipTargetChecks: firstCommit,
force: forceDangerous,
peerUpdateTimeout: peerUpdateTimeout,
quorum
})
}
setupProgressLogs(runner, type, firstCommit)
const res = await runner.done()
printCommit(res.manifest, res.quorum, res.result)
const destKey = type === 'core' ? res.result.destCore.key : res.result.db.destCore.key
console.info(`${type} key: ${destKey}`)
}
async function seed() {
const logInterval = cmdSeed.flags.logInterval
? +cmdSeed.flags.logInterval
: DEFAULT_SEED_LOG_INTERVAL
const { type, publicKeys, namespace, srcKey, multisigKey, quorum, store, swarm } = await setup()
const multisig = new Multisig(store, swarm)
console.log('\nPreparing to seed ~ Press Ctrl+C to exit\n')
const allCores = []
if (type === 'core') {
const srcCore = store.get({ key: idEnc.decode(srcKey) })
await srcCore.ready()
swarm.join(srcCore.discoveryKey)
srcCore.download({ start: 0, end: -1 })
let tgtCore
if (multisigKey) {
tgtCore = store.get({ key: idEnc.decode(multisigKey) })
await tgtCore.ready()
} else {
const res = await multisig.createCore(publicKeys, namespace, { quorum })
tgtCore = res.core
}
swarm.join(tgtCore.discoveryKey)
tgtCore.download({ start: 0, end: -1 })
allCores.push({ core: srcCore, label: 'Source' }, { core: tgtCore, label: 'Multisig' })
} else {
const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
await srcDrive.ready()
swarm.join(srcDrive.discoveryKey)
srcDrive.db.core.download({ start: 0, end: -1 })
await srcDrive.getBlobs()
srcDrive.blobs.core.download({ start: 0, end: -1 })
let tgtCore
let tgtBlobsCore
if (multisigKey) {
const tgtDrive = new Hyperdrive(store, idEnc.decode(multisigKey))
await tgtDrive.ready()
tgtCore = tgtDrive.db.core
await tgtDrive.getBlobs()
tgtBlobsCore = tgtDrive.blobs.core
} else {
const res = await multisig.createDrive(publicKeys, namespace, { quorum })
tgtCore = res.core
tgtBlobsCore = res.blobsCore
await tgtBlobsCore.ready()
}
swarm.join(tgtCore.discoveryKey)
tgtCore.download({ start: 0, end: -1 })
tgtBlobsCore.download({ start: 0, end: -1 })
allCores.push(
{ core: srcDrive.db.core, label: 'Source DB' },
{ core: srcDrive.blobs.core, label: 'Source Blobs' },
{ core: tgtCore, label: 'Multisig DB' },
{ core: tgtBlobsCore, label: 'Multisig Blobs' }
)
}
for (const { core, label } of allCores) {
console.log(`${label} core:`)
console.log(` key: ${idEnc.normalize(core.key)}`)
console.log(` keyHex: ${core.key.toString('hex')}`)
console.log(` length: ${core.length}`)
console.log(` treeHash: ${idEnc.normalize(await core.treeHash())}\n`)
}
const interval = setInterval(() => {
allCores.forEach(({ core, label }) => {
const peers = core.peers.length
let fullyDownloadedPeers = 0
for (const p of core.peers) {
if (p.remoteContiguousLength >= core.length) fullyDownloadedPeers++
}
console.log(
`${label} core: ${peers} peers, ${fullyDownloadedPeers} fully downloaded, length: ${core.length}`
)
})
console.log()
}, logInterval)
goodbye(() => clearInterval(interval))
}
function setupProgressLogs(req, name, firstCommit) {
req.on('verify-committable-start', (srcKey, tgtKey) => {
console.log(
`Verifying the ${name} is safe to commit: source ${idEnc.normalize(srcKey)} (hex: ${srcKey.toString('hex')}) to multisig target ${idEnc.normalize(tgtKey)} (hex: ${tgtKey.toString('hex')})`
)
})
req.on('commit-start', () => {
console.log(`Committing the ${name}...`)
})
req.on('verify-committed-start', (key) => {
console.log(`Committed the ${name}, key ${idEnc.normalize(key)} (hex: ${key.toString('hex')})`)
console.log('Waiting for remote seeders to pick up the changes...')
if (firstCommit) {
console.log(
'Please add this key to the seeders now. The logs here will notify you when it is picked up by them. Do not shut down until that happens.'
)
}
})
}
function printRequest(request) {
const runner = SignRequest.decode(request)
const reqStr = z32.encode(request)
const reqMsg = {
key: runner.id,
keyHex: runner.key.toString('hex'),
length: runner.length,
treeHash: idEnc.normalize(runner.treeHash)
}
console.log('Request:', JSON.stringify(reqMsg, null, 2))
console.log('To sign, run:', `\nhypercore-sign ${reqStr}`)
}
function printCommit(manifest, quorum, result, dryRun) {
if (dryRun) {
console.log(`\nQuorum: ${quorum} / ${manifest.quorum}`)
console.log('\nReview batch to commit:', JSON.stringify(result, null, 2))
} else {
console.log('\nCommitted:', JSON.stringify(result, null, 2))
console.log('\n~ DONE ~ Seeding now ~ Press Ctrl+C to exit ~\n')
}
}
async function setup(opts = {}) {
const configPath = cmd.flags.config || DEFAULT_CONFIG_PATH
const storage = cmd.flags.storage || DEFAULT_STORAGE_PATH
const { withReplication = true } = opts
const { type, publicKeys, namespace, srcKey, bootstrap, quorum } = await loadConfig(
configPath,
opts
)
let store, swarm
if (withReplication) {
const res = await replication(storage, bootstrap)
store = res.store
swarm = res.swarm
}
return { type, publicKeys, namespace, srcKey, quorum, store, swarm }
}
/**
* @type {function(): Promise<{ publicKeys: string[], namespace: string, srcKey: string }>}
*/
async function loadConfig(configPath, opts = {}) {
const {
type,
publicKeys,
namespace,
srcKey,
bootstrap,
quorum = null,
multisigKey = null
} = JSON.parse(await fs.readFile(configPath, 'utf-8'))
const { srcKeyRequired = true } = opts
if (!(type === 'core' || type === 'drive')) {
throw new Error(`type must be either core or drive. Saw '${type}'`)
}
if (!namespace) throw new Error('namespace must be set')
if (!publicKeys?.length) throw new Error('publicKeys must be set and include at least 1 key')
for (let i = 0; i < publicKeys.length; i++) {
if (!idEnc.isValid(publicKeys[i])) throw new Error(`invalid publicKey ${i}: '${publicKeys[i]}'`)
}
if (srcKeyRequired && !srcKey) throw new Error('srcKey must be set')
if (srcKey && !idEnc.isValid(srcKey)) throw new Error(`invalid srcKey: '${srcKey}'`)
if (bootstrap) console.info(`Using non-default bootstrap`)
if (multisigKey) {
const calculatedKeyBuffer = Multisig.getCoreKey(publicKeys, namespace, { quorum })
const calculatedKey = idEnc.normalize(calculatedKeyBuffer)
const passedInKey = idEnc.normalize(multisigKey)
if (passedInKey !== calculatedKey) {
throw new Error(
`multisigKey does not correspond to the key generated from the config, expected ${calculatedKey} (hex: ${calculatedKeyBuffer.toString('hex')})`
)
}
}
return { type, publicKeys, namespace, srcKey, bootstrap, quorum }
}
/**
* @type {function(): Promise<{ store: Corestore, swarm: Hyperswarm }>}
*/
async function replication(storage, bootstrap) {
const store = new Corestore(storage)
goodbye(() => store.close(), 20)
await store.ready()
const swarm = new Hyperswarm({ bootstrap })
goodbye(() => swarm.destroy(), 10)
swarm.on('connection', (conn, peer) => {
console.info('Opened connection')
conn.on('close', () => console.info('Closed connection'))
store.replicate(conn)
})
return { store, swarm }
}
function wrapErrHandler(func) {
const res = async () => {
try {
await func()
} catch (e) {
console.error(e)
process.exit(1)
}
}
return res
}
cmd.parse()