hyper-multisig-cli 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/bin.js +142 -14
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -82,6 +82,8 @@ Note that the key of the multisig hypercore is fully determined by the public ke
82
82
 
83
83
  It is possible to switch to a different `srcKey`.
84
84
 
85
+ The config supports an optional `multisigKey` field. Set this to the multisig key corresponding to your config (the key returned by the `link`command). This can be useful for searching configs based on their multisigKey.
86
+
85
87
  ### Create Signing Request
86
88
 
87
89
  ```shell
package/bin.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  const Corestore = require('corestore')
3
+ const process = require('process')
3
4
  const fs = require('fs').promises
4
5
  const goodbye = require('graceful-goodbye')
5
6
  const Multisig = require('hyper-multisig')
@@ -12,8 +13,9 @@ const z32 = require('z32')
12
13
 
13
14
  const DEFAULT_CONFIG_PATH = './multisig.json'
14
15
  const DEFAULT_STORAGE_PATH = './storage'
16
+ const DEFAULT_SEED_LOG_INTERVAL = 15000
15
17
 
16
- const cmdLink = command('link', description('Create multisig key'), link)
18
+ const cmdLink = command('link', description('Create multisig key'), wrapErrHandler(link))
17
19
 
18
20
  const cmdRequest = command(
19
21
  'request',
@@ -21,7 +23,7 @@ const cmdRequest = command(
21
23
  flag('--force', 'Skip sanity checks'),
22
24
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
23
25
  arg('<length>', 'Core length to request'),
24
- request
26
+ wrapErrHandler(request)
25
27
  )
26
28
 
27
29
  const cmdVerify = command(
@@ -34,7 +36,7 @@ const cmdVerify = command(
34
36
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
35
37
  arg('request', 'Signing request'),
36
38
  rest('[...responses]', 'Signing responses'),
37
- verify
39
+ wrapErrHandler(verify)
38
40
  )
39
41
 
40
42
  const cmdCommit = command(
@@ -48,7 +50,16 @@ const cmdCommit = command(
48
50
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
49
51
  arg('request', 'Signing request'),
50
52
  rest('[...responses]', 'Signing responses'),
51
- commit
53
+ wrapErrHandler(commit)
54
+ )
55
+
56
+ const cmdSeed = command(
57
+ 'seed',
58
+ description('Seed both the source and multisig cores/drives'),
59
+ flag('--log-interval <logInterval>', 'Interval in ms to log replication status').default(
60
+ DEFAULT_SEED_LOG_INTERVAL
61
+ ),
62
+ wrapErrHandler(seed)
52
63
  )
53
64
 
54
65
  const cmd = command(
@@ -59,6 +70,7 @@ const cmd = command(
59
70
  cmdRequest,
60
71
  cmdVerify,
61
72
  cmdCommit,
73
+ cmdSeed,
62
74
  () => console.log(cmd.help())
63
75
  )
64
76
 
@@ -202,17 +214,104 @@ async function commit() {
202
214
  console.info(`${type} key: ${destKey}`)
203
215
  }
204
216
 
217
+ async function seed() {
218
+ const logInterval = cmdSeed.flags.logInterval
219
+ ? +cmdSeed.flags.logInterval
220
+ : DEFAULT_SEED_LOG_INTERVAL
221
+ const { type, publicKeys, namespace, srcKey, multisigKey, quorum, store, swarm } = await setup()
222
+
223
+ const multisig = new Multisig(store, swarm)
224
+ console.log('\nPreparing to seed ~ Press Ctrl+C to exit\n')
225
+
226
+ const allCores = []
227
+
228
+ if (type === 'core') {
229
+ const srcCore = store.get({ key: idEnc.decode(srcKey) })
230
+ await srcCore.ready()
231
+ swarm.join(srcCore.discoveryKey)
232
+ srcCore.download({ start: 0, end: -1 })
233
+
234
+ let tgtCore
235
+ if (multisigKey) {
236
+ tgtCore = store.get({ key: idEnc.decode(multisigKey) })
237
+ await tgtCore.ready()
238
+ } else {
239
+ const res = await multisig.createCore(publicKeys, namespace, { quorum })
240
+ tgtCore = res.core
241
+ }
242
+ swarm.join(tgtCore.discoveryKey)
243
+ tgtCore.download({ start: 0, end: -1 })
244
+
245
+ allCores.push({ core: srcCore, label: 'Source' }, { core: tgtCore, label: 'Multisig' })
246
+ } else {
247
+ const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
248
+ await srcDrive.ready()
249
+ swarm.join(srcDrive.discoveryKey)
250
+ srcDrive.db.core.download({ start: 0, end: -1 })
251
+ await srcDrive.getBlobs()
252
+ srcDrive.blobs.core.download({ start: 0, end: -1 })
253
+
254
+ let tgtCore
255
+ let tgtBlobsCore
256
+ if (multisigKey) {
257
+ const tgtDrive = new Hyperdrive(store, idEnc.decode(multisigKey))
258
+ await tgtDrive.ready()
259
+ tgtCore = tgtDrive.db.core
260
+ await tgtDrive.getBlobs()
261
+ tgtBlobsCore = tgtDrive.blobs.core
262
+ } else {
263
+ const res = await multisig.createDrive(publicKeys, namespace, { quorum })
264
+ tgtCore = res.core
265
+ tgtBlobsCore = res.blobsCore
266
+ await tgtBlobsCore.ready()
267
+ }
268
+ swarm.join(tgtCore.discoveryKey)
269
+ tgtCore.download({ start: 0, end: -1 })
270
+ tgtBlobsCore.download({ start: 0, end: -1 })
271
+
272
+ allCores.push(
273
+ { core: srcDrive.db.core, label: 'Source DB' },
274
+ { core: srcDrive.blobs.core, label: 'Source Blobs' },
275
+ { core: tgtCore, label: 'Multisig DB' },
276
+ { core: tgtBlobsCore, label: 'Multisig Blobs' }
277
+ )
278
+ }
279
+
280
+ for (const { core, label } of allCores) {
281
+ console.log(`${label} core:`)
282
+ console.log(` key: ${idEnc.normalize(core.key)}`)
283
+ console.log(` keyHex: ${core.key.toString('hex')}`)
284
+ console.log(` length: ${core.length}`)
285
+ console.log(` treeHash: ${idEnc.normalize(await core.treeHash())}\n`)
286
+ }
287
+
288
+ const interval = setInterval(() => {
289
+ allCores.forEach(({ core, label }) => {
290
+ const peers = core.peers.length
291
+ let fullyDownloadedPeers = 0
292
+ for (const p of core.peers) {
293
+ if (p.remoteContiguousLength >= core.length) fullyDownloadedPeers++
294
+ }
295
+ console.log(
296
+ `${label} core: ${peers} peers, ${fullyDownloadedPeers} fully downloaded, length: ${core.length}`
297
+ )
298
+ })
299
+ console.log()
300
+ }, logInterval)
301
+ goodbye(() => clearInterval(interval))
302
+ }
303
+
205
304
  function setupProgressLogs(req, name, firstCommit) {
206
305
  req.on('verify-committable-start', (srcKey, tgtKey) => {
207
306
  console.log(
208
- `Verifying the ${name} is safe to commit (source ${idEnc.normalize(srcKey)} to multisig target ${idEnc.normalize(tgtKey)})`
307
+ `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')})`
209
308
  )
210
309
  })
211
310
  req.on('commit-start', () => {
212
311
  console.log(`Committing the ${name}...`)
213
312
  })
214
313
  req.on('verify-committed-start', (key) => {
215
- console.log(`Committed the ${name} (key ${idEnc.normalize(key)})`)
314
+ console.log(`Committed the ${name}, key ${idEnc.normalize(key)} (hex: ${key.toString('hex')})`)
216
315
  console.log('Waiting for remote seeders to pick up the changes...')
217
316
  if (firstCommit) {
218
317
  console.log(
@@ -227,6 +326,7 @@ function printRequest(request) {
227
326
  const reqStr = z32.encode(request)
228
327
  const reqMsg = {
229
328
  key: runner.id,
329
+ keyHex: runner.key.toString('hex'),
230
330
  length: runner.length,
231
331
  treeHash: idEnc.normalize(runner.treeHash)
232
332
  }
@@ -275,21 +375,37 @@ async function loadConfig(configPath, opts = {}) {
275
375
  namespace,
276
376
  srcKey,
277
377
  bootstrap,
278
- quorum = null
378
+ quorum = null,
379
+ multisigKey = null
279
380
  } = JSON.parse(await fs.readFile(configPath, 'utf-8'))
280
381
 
281
382
  const { srcKeyRequired = true } = opts
282
383
 
283
- if (
284
- !(type === 'core' || type === 'drive') ||
285
- !publicKeys?.length ||
286
- !namespace ||
287
- (srcKeyRequired && !srcKey)
288
- ) {
289
- throw new Error('Invalid config file')
384
+ if (!(type === 'core' || type === 'drive')) {
385
+ throw new Error(`type must be either core or drive. Saw '${type}'`)
386
+ }
387
+ if (!namespace) throw new Error('namespace must be set')
388
+ if (!publicKeys?.length) throw new Error('publicKeys must be set and include at least 1 key')
389
+ for (let i = 0; i < publicKeys.length; i++) {
390
+ if (!idEnc.isValid(publicKeys[i])) throw new Error(`invalid publicKey ${i}: '${publicKeys[i]}'`)
290
391
  }
291
392
 
393
+ if (srcKeyRequired && !srcKey) throw new Error('srcKey must be set')
394
+ if (srcKey && !idEnc.isValid(srcKey)) throw new Error(`invalid srcKey: '${srcKey}'`)
395
+
292
396
  if (bootstrap) console.info(`Using non-default bootstrap`)
397
+
398
+ if (multisigKey) {
399
+ const calculatedKeyBuffer = Multisig.getCoreKey(publicKeys, namespace, { quorum })
400
+ const calculatedKey = idEnc.normalize(calculatedKeyBuffer)
401
+ const passedInKey = idEnc.normalize(multisigKey)
402
+ if (passedInKey !== calculatedKey) {
403
+ throw new Error(
404
+ `multisigKey does not correspond to the key generated from the config, expected ${calculatedKey} (hex: ${calculatedKeyBuffer.toString('hex')})`
405
+ )
406
+ }
407
+ }
408
+
293
409
  return { type, publicKeys, namespace, srcKey, bootstrap, quorum }
294
410
  }
295
411
 
@@ -312,4 +428,16 @@ async function replication(storage, bootstrap) {
312
428
  return { store, swarm }
313
429
  }
314
430
 
431
+ function wrapErrHandler(func) {
432
+ const res = async () => {
433
+ try {
434
+ await func()
435
+ } catch (e) {
436
+ console.error(e)
437
+ process.exit(1)
438
+ }
439
+ }
440
+ return res
441
+ }
442
+
315
443
  cmd.parse()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyper-multisig-cli",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "CLI of hyper-multisig",
5
5
  "main": "bin.js",
6
6
  "bin": {
@@ -21,7 +21,7 @@
21
21
  "bare-fs": "^4.5.4",
22
22
  "corestore": "^7.8.0",
23
23
  "graceful-goodbye": "^1.3.3",
24
- "hyper-multisig": "^2.1.0",
24
+ "hyper-multisig": "^2.2.1",
25
25
  "hypercore-id-encoding": "^1.3.0",
26
26
  "hypercore-signing-request": "^5.0.1",
27
27
  "hyperdrive": "^13.2.1",