hyper-multisig-cli 0.1.0 → 1.0.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 +136 -18
  2. package/bin.js +96 -167
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,14 +1,19 @@
1
1
  ![CI](https://github.com/holepunchto/hyper-multisig-cli/actions/workflows/ci.yml/badge.svg)
2
2
 
3
- # Hyper Multisig Cli
3
+ # Hyper Multisig CLI
4
4
 
5
- - Create signing request
6
- - Commit changes to multisig core/drive (dry-run supported)
5
+ CLI to safely create, verify and commit multisig signing requests for hypercores and hyperdrives.
6
+
7
+ Includes sanity checks to avoid common mistakes and risky releases, like detecting conflicts and ensuring all cores are fully seeded by other peers before committing.
8
+
9
+ Uses [hyper-multisig](https://github.com/holepunchto/hyper-multisig) under the hood.
10
+
11
+ Supports both Node.js and Bare.
7
12
 
8
13
  ## Installation
9
14
 
10
15
  ```shell
11
- npm i -g hyper-multisig-cli@latest
16
+ npm i -g hyper-multisig-cli
12
17
  ```
13
18
 
14
19
  ## Usage
@@ -18,36 +23,149 @@ hyper-multisig --help
18
23
  hyper-multisig-bare --help
19
24
  ```
20
25
 
21
- ### Example
26
+ ## Example
27
+
28
+ This example sets up a multisig hypercore with 1 signer and a quorum of 1, so it can be run by a single person. A more standard setup is 3 signers with a quorum of 2.
29
+
30
+ ### Create Signing Key
31
+
32
+ First use [hypercore-sign](https://github.com/holepunchto/hypercore-sign) to create your signing key:
33
+
34
+ ```
35
+ npm i -g hypercore-sign
36
+ hypercore-sign-generate-keys
37
+ ```
38
+
39
+ Take note of your public key.
40
+
41
+ ### Create Source Core
42
+
43
+ We just create a dummy source core for this example:
44
+
45
+ ```
46
+ import Corestore from 'corestore'
47
+ import Hyperswarm from 'hyperswarm'
48
+
49
+ const desiredLength = 3
22
50
 
23
- Use [hypercore-sign](https://github.com/holepunchto/hypercore-sign) to create public keys
51
+ const store = new Corestore('temp-example-store')
52
+ const core = store.get({ name: 'source-core' })
53
+ await core.ready()
24
54
 
25
- Create `config.json`
55
+ for (let i = core.length; i < desiredLength; i++) await core.append(`Block-${i}`)
56
+
57
+ const swarm = new Hyperswarm()
58
+ swarm.on('connection', (conn) => { store.replicate(conn) })
59
+ swarm.join(core.discoveryKey)
60
+ console.log(`Swarming key ${core.id} (length ${core.length})`)
61
+ ```
62
+
63
+ Keep this process running, because we will need to download this hypercore to be able to create and commit the signing request.
64
+
65
+ ### Create Config
66
+
67
+ Create `multisig.json` in your current directory
26
68
 
27
69
  ```json
28
70
  {
29
- "publicKeys": ["abc123...", "abc456...", "abc789..."],
30
- "namespace": "holepunchto/my-app",
31
- "srcKey": "abcdef..."
71
+ "type": "core", // core or drive
72
+ "publicKeys": ["paste-here-the-key-generated-by-hypercore-sign-generate-keys"],
73
+ "quorum": 1,
74
+ "namespace": "dummy-test-core",
75
+ "srcKey": "paste-here-the-key-of-your-source-core"
32
76
  }
33
77
  ```
34
78
 
35
- Create signing request
79
+ Note that the key of the multisig hypercore is fully determined by the public keys and namespace. This means you can never use the same namespace with the same signers.
80
+
81
+ It is possible to switch to a different `srcKey`.
82
+
83
+ ### Create Signing Request
36
84
 
37
85
  ```shell
38
- hyper-multisig request-core 1
86
+ hyper-multisig request 3
39
87
  ```
40
88
 
41
- Use [hypercore-sign](https://github.com/holepunchto/hypercore-sign) to create signing responses
89
+ You should see an error that the source core is not well seeded. It errors because committing any requests in this situation is dangerous.
42
90
 
43
- Dry-run
91
+ Fix it by running this script in another terminal window, filling in the correct source key:
44
92
 
45
- ```shell
46
- hyper-multisig verify-core <request> <response1> <response2>
47
93
  ```
94
+ import Corestore from 'corestore'
95
+ import Hyperswarm from 'hyperswarm'
96
+ import IdEnc from 'hypercore-id-encoding'
97
+
98
+ const stringKey = '' // use the key of your source core
99
+
100
+ const store = new Corestore(`temp-example-seeder-store-${Math.round(Math.random() * 100000)}`)
101
+ const key = IdEnc.decode(stringKey)
102
+ const core = store.get({ key })
103
+ await core.ready()
104
+
105
+ const swarm = new Hyperswarm()
106
+ swarm.on('connection', (conn) => { store.replicate(conn) })
107
+ swarm.join(core.discoveryKey)
108
+ console.log(`Swarming key ${core.id}`)
109
+
110
+ core.download({ start: 0, end: -1 })
111
+ ```
112
+
113
+ Note: in practice you would use a seeder service, like a [blind-peer](https://github.com/holepunchto/blind-peer), to ensure your source core is well seeded. Having two seeder processes running locally is not safe (if your computer is turned off, the core is no longer available).
48
114
 
49
- Commit multisig
115
+ Now rerun the command:
50
116
 
51
117
  ```shell
52
- hyper-multisig commit-core <request> <response1> <response2>
118
+ hyper-multisig request 3
53
119
  ```
120
+
121
+ Take note of the signing request. It looks like this:
122
+
123
+ ```
124
+ hypercore-sign yebob...
125
+ ```
126
+
127
+ ### Sign Request
128
+
129
+ Run the hypercore-sign command, and take note of the result. It looks like
130
+
131
+ ```
132
+ Reply with:
133
+
134
+ yeqmm...
135
+ ```
136
+
137
+ ### Verify Request
138
+
139
+ Replace `yebob...` with your own signing request, and `yeqmm` with your own response. Then run:
140
+
141
+ ```
142
+ hyper-multisig verify --first-commit yebob... yeqmm...
143
+ ```
144
+
145
+ You should see the confirmation that the core is safe to commit.
146
+
147
+ ### Commit Request
148
+
149
+ Run the same command as above, but replace 'verify' with 'commit'.
150
+
151
+ ```
152
+ hyper-multisig commit --first-commit <the signing request> <your signing response>
153
+ ```
154
+
155
+ You should see logs like
156
+
157
+ ```
158
+ Verifying the core is safe to commit (source <source key> to multisig target <target key>)
159
+ Opened connection
160
+ Opened connection
161
+ Committing the core...
162
+ Committed the core (key <target key>)
163
+ Waiting for remote seeders to pick up the changes...
164
+ 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.
165
+ ```
166
+
167
+ Do as the logs instruct, and add your target key to 2 or more seeders. For this example, simple run 2 more seeder scripts like above, but this time for the target key.
168
+
169
+ Once the program detects at least 2 seeders have fully downloaded the multisig drive, it will inform you it is done. Shut down with ctrl-c.
170
+
171
+ Note: for any future updates to your multisig core, remove the `first-commit` flag.
package/bin.js CHANGED
@@ -10,20 +10,20 @@ const Hyperswarm = require('hyperswarm')
10
10
  const { command, flag, arg, rest, description } = require('paparam')
11
11
  const z32 = require('z32')
12
12
 
13
- const DEFAULT_CONFIG_PATH = './config.json'
13
+ const DEFAULT_CONFIG_PATH = './multisig.json'
14
14
  const DEFAULT_STORAGE_PATH = './storage'
15
15
 
16
- const cmdRequestCore = command(
17
- 'request-core',
16
+ const cmdRequest = command(
17
+ 'request',
18
18
  description('Create signing request'),
19
19
  flag('--force', 'Skip sanity checks'),
20
20
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
21
21
  arg('<length>', 'Core length to request'),
22
- requestCore
22
+ request
23
23
  )
24
24
 
25
- const cmdVerifyCore = command(
26
- 'verify-core',
25
+ const cmdVerify = command(
26
+ 'verify',
27
27
  description('Verify multisig'),
28
28
  flag(
29
29
  '--first-commit',
@@ -32,11 +32,11 @@ const cmdVerifyCore = command(
32
32
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
33
33
  arg('request', 'Signing request'),
34
34
  rest('[...responses]', 'Signing responses'),
35
- verifyCore
35
+ verify
36
36
  )
37
37
 
38
- const cmdCommitCore = command(
39
- 'commit-core',
38
+ const cmdCommit = command(
39
+ 'commit',
40
40
  description('Commit multisig'),
41
41
  flag(
42
42
  '--first-commit',
@@ -46,203 +46,131 @@ const cmdCommitCore = command(
46
46
  flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
47
47
  arg('request', 'Signing request'),
48
48
  rest('[...responses]', 'Signing responses'),
49
- commitCore
50
- )
51
-
52
- const cmdRequestDrive = command(
53
- 'request-drive',
54
- description('Create signing request'),
55
- flag('--force', 'Skip sanity checks'),
56
- flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
57
- arg('<length>', 'Drive length to request'),
58
- requestDrive
59
- )
60
-
61
- const cmdVerifyDrive = command(
62
- 'verify-drive',
63
- description('Verify multisig'),
64
- flag(
65
- '--first-commit',
66
- 'Set when this is the first commit to the multisig target, so it skips those checks'
67
- ),
68
- flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
69
- arg('request', 'Signing request'),
70
- rest('[...responses]', 'Signing responses'),
71
- verifyDrive
72
- )
73
-
74
- const cmdCommitDrive = command(
75
- 'commit-drive',
76
- description('Commit multisig'),
77
- flag(
78
- '--first-commit',
79
- 'Set when this is the first commit to the multisig target, so it skips those checks'
80
- ),
81
- flag('--force-dangerous', 'Advanced option, it might break the drive on misuse'),
82
- flag('--peer-update-timeout <ms>', 'Peer update timeout in ms'),
83
- arg('request', 'Signing request'),
84
- rest('[...responses]', 'Signing responses'),
85
- commitDrive
49
+ commit
86
50
  )
87
51
 
88
52
  const cmd = command(
89
53
  'multisig',
90
54
  flag('--config|-c <config>', `Config file path (default to ${DEFAULT_CONFIG_PATH})`),
91
55
  flag('--storage|-s <storage>', `Storage path (default to ${DEFAULT_STORAGE_PATH})`),
92
- cmdRequestCore,
93
- cmdVerifyCore,
94
- cmdCommitCore,
95
- cmdRequestDrive,
96
- cmdVerifyDrive,
97
- cmdCommitDrive,
56
+ cmdRequest,
57
+ cmdVerify,
58
+ cmdCommit,
98
59
  () => console.log(cmd.help())
99
60
  )
100
61
 
101
- async function requestCore() {
102
- const length = +cmdRequestCore.args.length
103
- const { force, peerUpdateTimeout } = cmdRequestCore.flags
104
-
62
+ async function request() {
63
+ const length = +cmdRequest.args.length
64
+ const { force, peerUpdateTimeout } = cmdRequest.flags
105
65
  if (!length) throw new Error('Invalid command')
106
66
 
107
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
108
- const srcCore = store.get({ key: idEnc.decode(srcKey) })
109
- const multisig = new Multisig(store, swarm)
110
- const res = await multisig
111
- .requestCore(publicKeys, namespace, srcCore, length, {
112
- force,
113
- peerUpdateTimeout: peerUpdateTimeout,
114
- quorum
115
- })
116
- .done()
117
-
118
- printRequest(res.request)
119
- goodbye.exit()
120
- }
121
-
122
- async function verifyCore() {
123
- const { firstCommit, peerUpdateTimeout } = cmdVerifyCore.flags
124
- const request = cmdVerifyCore.args.request
125
- const responses = cmdVerifyCore.rest || []
126
- if (!request) throw new Error('Invalid command')
127
-
128
- console.info(`Verifying request ${request}`)
129
- console.info(`Responses:\n -${responses.join('\n -')}`)
67
+ const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
130
68
 
131
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
132
- const srcCore = store.get({ key: idEnc.decode(srcKey) })
133
69
  const multisig = new Multisig(store, swarm)
134
- const runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
135
- dryRun: true,
136
- skipTargetChecks: firstCommit,
137
- peerUpdateTimeout: peerUpdateTimeout,
138
- quorum
139
- })
140
- setupProgressLogs(runner, 'core', firstCommit)
141
70
 
142
- const res = await runner.done()
71
+ let request
72
+ if (type === 'core') {
73
+ const srcCore = store.get({ key: idEnc.decode(srcKey) })
74
+ const res = await multisig
75
+ .requestCore(publicKeys, namespace, srcCore, length, {
76
+ force,
77
+ peerUpdateTimeout: peerUpdateTimeout,
78
+ quorum
79
+ })
80
+ .done()
81
+ request = res.request
82
+ } else {
83
+ const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
84
+ const res = await multisig
85
+ .requestDrive(publicKeys, namespace, srcDrive, length, {
86
+ force,
87
+ peerUpdateTimeout: peerUpdateTimeout,
88
+ quorum
89
+ })
90
+ .done()
91
+ request = res.request
92
+ }
93
+ printRequest(request)
143
94
 
144
- printCommit(res.manifest, res.quorum, res.result, true)
145
- console.info(`Core key: ${res.result.destCore.key} is safe to commit`)
146
95
  goodbye.exit()
147
96
  }
148
97
 
149
- async function commitCore() {
150
- const request = cmdCommitCore.args.request
151
- const responses = cmdCommitCore.rest || []
152
- const { firstCommit, forceDangerous, peerUpdateTimeout } = cmdCommitCore.flags
153
- if (!request || !responses.length) throw new Error('Invalid command')
154
-
98
+ async function verify() {
99
+ const { firstCommit, peerUpdateTimeout } = cmdVerify.flags
100
+ const request = cmdVerify.args.request
101
+ const responses = cmdVerify.rest || []
102
+ if (!request) throw new Error('Invalid command')
155
103
  console.info(`Committing request ${request}`)
156
104
  console.info(`Responses:\n -${responses.join('\n -')}`)
157
105
 
158
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
159
- const srcCore = store.get({ key: idEnc.decode(srcKey) })
106
+ const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
160
107
  const multisig = new Multisig(store, swarm)
161
- const runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
162
- skipTargetChecks: firstCommit,
163
- force: forceDangerous,
164
- peerUpdateTimeout: peerUpdateTimeout,
165
- quorum
166
- })
167
-
168
- setupProgressLogs(runner, 'core', firstCommit)
169
- const res = await runner.done()
170
-
171
- printCommit(res.manifest, res.quorum, res.result)
172
- console.info(`Core key: ${res.result.destCore.key}`)
173
- }
174
108
 
175
- async function requestDrive() {
176
- const length = +cmdRequestDrive.args.length
177
- const { force, peerUpdateTimeout } = cmdRequestDrive.flags
178
- if (!length) throw new Error('Invalid command')
179
-
180
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
181
- const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
182
- const multisig = new Multisig(store, swarm)
183
- const res = await multisig
184
- .requestDrive(publicKeys, namespace, srcDrive, length, {
185
- force,
109
+ let runner
110
+ if (type === 'core') {
111
+ const srcCore = store.get({ key: idEnc.decode(srcKey) })
112
+ runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
113
+ dryRun: true,
114
+ skipTargetChecks: firstCommit,
186
115
  peerUpdateTimeout: peerUpdateTimeout,
187
116
  quorum
188
117
  })
189
- .done()
190
-
191
- printRequest(res.request)
192
- goodbye.exit()
193
- }
194
-
195
- async function verifyDrive() {
196
- const { firstCommit, peerUpdateTimeout } = cmdVerifyDrive.flags
197
- const request = cmdVerifyDrive.args.request
198
- const responses = cmdVerifyDrive.rest || []
199
- if (!request) throw new Error('Invalid command')
200
- console.info(`Committing request ${request}`)
201
- console.info(`Responses:\n -${responses.join('\n -')}`)
202
-
203
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
204
- const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
205
- const multisig = new Multisig(store, swarm)
206
- const runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
207
- dryRun: true,
208
- skipTargetChecks: firstCommit,
209
- peerUpdateTimeout: peerUpdateTimeout,
210
- quorum
211
- })
212
-
213
- setupProgressLogs(runner, 'drive', firstCommit)
118
+ } else {
119
+ const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
120
+ runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
121
+ dryRun: true,
122
+ skipTargetChecks: firstCommit,
123
+ peerUpdateTimeout: peerUpdateTimeout,
124
+ quorum
125
+ })
126
+ }
127
+ setupProgressLogs(runner, type, firstCommit)
214
128
 
215
129
  const res = await runner.done()
216
130
 
217
131
  printCommit(res.manifest, res.quorum, res.result, true)
218
- console.info(`Drive key: ${res.result.db.destCore.key} is safe to commit`)
132
+ const destKey = type === 'core' ? res.result.destCore.key : res.result.db.destCore.key
133
+ console.info(`${type} key: ${destKey} is safe to commit`)
219
134
  goodbye.exit()
220
135
  }
221
136
 
222
- async function commitDrive() {
223
- const request = cmdCommitDrive.args.request
224
- const responses = cmdCommitDrive.rest || []
225
- const { firstCommit, forceDangerous, peerUpdateTimeout } = cmdCommitDrive.flags
137
+ async function commit() {
138
+ const request = cmdCommit.args.request
139
+ const responses = cmdCommit.rest || []
140
+ const { firstCommit, forceDangerous, peerUpdateTimeout } = cmdCommit.flags
226
141
  if (!request || !responses?.length) throw new Error('Invalid command')
227
142
 
228
143
  console.info(`Committing request ${request}`)
229
144
  console.info(`Responses:\n -${responses.join('\n -')}`)
230
145
 
231
- const { publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
232
- const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
146
+ const { type, publicKeys, namespace, srcKey, quorum, store, swarm } = await setup()
233
147
  const multisig = new Multisig(store, swarm)
234
- const runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
235
- skipTargetChecks: firstCommit,
236
- force: forceDangerous,
237
- peerUpdateTimeout: peerUpdateTimeout,
238
- quorum
239
- })
240
- setupProgressLogs(runner, 'drive', firstCommit)
148
+
149
+ let runner
150
+ if (type === 'core') {
151
+ const srcCore = store.get({ key: idEnc.decode(srcKey) })
152
+ runner = multisig.commitCore(publicKeys, namespace, srcCore, request, responses, {
153
+ skipTargetChecks: firstCommit,
154
+ force: forceDangerous,
155
+ peerUpdateTimeout: peerUpdateTimeout,
156
+ quorum
157
+ })
158
+ } else {
159
+ const srcDrive = new Hyperdrive(store, idEnc.decode(srcKey))
160
+ runner = multisig.commitDrive(publicKeys, namespace, srcDrive, request, responses, {
161
+ skipTargetChecks: firstCommit,
162
+ force: forceDangerous,
163
+ peerUpdateTimeout: peerUpdateTimeout,
164
+ quorum
165
+ })
166
+ }
167
+ setupProgressLogs(runner, type, firstCommit)
241
168
 
242
169
  const res = await runner.done()
243
170
 
244
171
  printCommit(res.manifest, res.quorum, res.result)
245
- console.info(`Drive key: ${res.result.db.destCore.key}`)
172
+ const destKey = type === 'core' ? res.result.destCore.key : res.result.db.destCore.key
173
+ console.info(`${type} key: ${destKey}`)
246
174
  }
247
175
 
248
176
  function setupProgressLogs(req, name, firstCommit) {
@@ -291,9 +219,9 @@ async function setup() {
291
219
  const configPath = cmd.flags.config || DEFAULT_CONFIG_PATH
292
220
  const storage = cmd.flags.storage || DEFAULT_STORAGE_PATH
293
221
 
294
- const { publicKeys, namespace, srcKey, bootstrap, quorum } = await loadConfig(configPath)
222
+ const { type, publicKeys, namespace, srcKey, bootstrap, quorum } = await loadConfig(configPath)
295
223
  const { store, swarm } = await replication(storage, bootstrap)
296
- return { publicKeys, namespace, srcKey, quorum, store, swarm }
224
+ return { type, publicKeys, namespace, srcKey, quorum, store, swarm }
297
225
  }
298
226
 
299
227
  /**
@@ -301,6 +229,7 @@ async function setup() {
301
229
  */
302
230
  async function loadConfig(configPath) {
303
231
  const {
232
+ type,
304
233
  publicKeys,
305
234
  namespace,
306
235
  srcKey,
@@ -308,12 +237,12 @@ async function loadConfig(configPath) {
308
237
  quorum = null
309
238
  } = JSON.parse(await fs.readFile(configPath, 'utf-8'))
310
239
 
311
- if (!publicKeys?.length || !namespace || !srcKey) {
240
+ if (!(type === 'core' || type === 'drive') || !publicKeys?.length || !namespace || !srcKey) {
312
241
  throw new Error('Invalid config file')
313
242
  }
314
243
 
315
244
  if (bootstrap) console.info(`Using non-default bootstrap`)
316
- return { publicKeys, namespace, srcKey, bootstrap, quorum }
245
+ return { type, publicKeys, namespace, srcKey, bootstrap, quorum }
317
246
  }
318
247
 
319
248
  /**
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "hyper-multisig-cli",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "CLI of hyper-multisig",
5
5
  "main": "bin.js",
6
6
  "bin": {
7
- "hyper-multisig": "./bin.js",
8
- "hyper-multisig-bare": "./bin-bare.js"
7
+ "hyper-multisig": "bin.js",
8
+ "hyper-multisig-bare": "bin-bare.js"
9
9
  },
10
10
  "files": [
11
11
  "bin-bare.js",