hyper-multisig 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ ![CI](https://github.com/holepunchto/hyper-multisig/actions/workflows/ci.yml/badge.svg)
2
+
3
+ # Hyper Multisig
4
+
5
+ - Create multisig hypercores and hyperdrives
6
+ - Create signing requests for multisig cores and drives
7
+ - Sign and release multisig cores and drives
8
+
9
+ Includes sanity checks to avoid common mistakes and risky releases (detecting conflicts before committing, ensuring all cores are seeded by multiple other peers etc.)
10
+
11
+ ## Install
12
+
13
+ ```
14
+ npm i hyper-multisig
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ End users most likely want to use [hyper-multisig-cli](https://github.com/holepunchto/hyper-multisig-cli) instead of interacting directly with this module.
20
+
21
+ ## Multisig Flow
22
+
23
+ - Step 1: Each signer uses [hypercore-sign](https://github.com/holepunchto/hypercore-sign) to generate a key pair (secret key and public key). Later, public keys from all signers will be collected to create a multisig core.
24
+
25
+ ```shell
26
+ hypercore-sign generate-keys
27
+
28
+ # to create an additional key pair
29
+ HYPERCORE_SIGN_KEYS_DIRECTORY=<path-to-another-dir> hypercore-sign generate-keys
30
+ ```
31
+
32
+ - Step 2: Create a multisig core given the public keys from the previous step, and a namespace to avoid collisions (the combination signers-namespace has to be globally unique, as it determinstically defines the key of the resulting multisig hypercore).
33
+
34
+ ```js
35
+ // collect public keys of all signers generated by hypercore-sign
36
+ const publicKeys = ['o37a1...', 'qgbd9...', '6r66j...']
37
+
38
+ // to avoid collision, we structure namespace based on repo path
39
+ const namespace = 'holepunchto/my-repo'
40
+
41
+ const store = new Corestore('storage')
42
+ await store.ready()
43
+
44
+ const multisig = new HyperMultisig(store, swarm)
45
+ const { manifest, core } = await multisig.createCore(publicKeys, namespace)
46
+ ```
47
+
48
+ - Step 3: Generate a signing request based on a normal core (non-multisig) with the manifest from the previous step.
49
+
50
+ ```js
51
+ const srcCore = store.get({ name: 'my-core' })
52
+ await srcCore.ready()
53
+
54
+ srcCore.append('hello world 1')
55
+ srcCore.append('hello world 2')
56
+ srcCore.append('hello world 3')
57
+
58
+ const multisig = new HyperMultisig(store)
59
+ const { request } = await multisig.requestCore(publicKeys, namespace, srcCore, 3)
60
+ ```
61
+
62
+ - Step 4: Use [hypercore-sign](https://github.com/holepunchto/hypercore-sign) to sign the signing request to generate a signature. Later, signatures from all signers will be collected to sign the multisig core.
63
+
64
+ ```shell
65
+ hypercore-sign <signingRequest>
66
+ ```
67
+
68
+ - Step 5: Commit the multisig core with the signatures from the previous step.
69
+
70
+ ```js
71
+ // collect signatures from all signers generated by hypercore-sign
72
+ const signatures = [signature1, signature2, ...]
73
+
74
+ const multisig = new HyperMultisig(store, swarm)
75
+ await multisig.commitCore(publicKeys, namespace, srcCore, request, signatures)
76
+ ```
package/index.js ADDED
@@ -0,0 +1,245 @@
1
+ /**
2
+ * @typedef {import('corestore')} Corestore
3
+ * @typedef {import('hypercore')} Hypercore
4
+ * @typedef {import('hyperswarm')} Hyperswarm
5
+ * @typedef {import('hypercore/lib/download')} Download
6
+ * @typedef {{
7
+ * version: number
8
+ * hash: string
9
+ * quorum: number
10
+ * signers: Array<{
11
+ * signature: string
12
+ * publicKey: Buffer
13
+ * namespace: Buffer
14
+ * }>
15
+ * }} Manifest
16
+ * @typedef {{
17
+ * key: string
18
+ * length: number
19
+ * treeHash: string
20
+ * }} CoreInfo
21
+ */
22
+
23
+ const cenc = require('compact-encoding')
24
+ const CoreSign = require('hypercore-sign')
25
+ const SignRequest = require('hypercore-signing-request')
26
+ const Hyperdrive = require('hyperdrive')
27
+ const z32 = require('z32')
28
+
29
+ const MultisigUtil = require('./lib/util')
30
+
31
+ class HyperMultisig {
32
+ constructor(store, swarm) {
33
+ /** @type {Corestore} */
34
+ this.store = store
35
+ /** @type {Hyperswarm} */
36
+ this.swarm = swarm
37
+ }
38
+
39
+ /**
40
+ * @param {string[]} publicKeys
41
+ * @param {string} namespace
42
+ * @return {Promise<{ manifest: Manifest, key: Buffer, core: Hypercore }>}
43
+ */
44
+ async createCore(publicKeys, namespace, { quorum } = {}) {
45
+ const manifest = MultisigUtil.getManifest(publicKeys, namespace, { quorum })
46
+ const core = this.store.get({ manifest })
47
+ await core.ready()
48
+ return { manifest, key: core.key, core }
49
+ }
50
+
51
+ /**
52
+ * @param {string[]} publicKeys
53
+ * @param {string} namespace
54
+ * @return {Promise<{
55
+ * manifest: Manifest, key: Buffer, core: Hypercore,
56
+ * blobsManifest: Manifest, blobsKey: Buffer, blobsCore: Hypercore
57
+ * }>}
58
+ */
59
+ async createDrive(publicKeys, namespace, { quorum } = {}) {
60
+ const { manifest, key, core } = await this.createCore(publicKeys, namespace, { quorum })
61
+
62
+ const blobsManifest = Hyperdrive.getContentManifest(manifest, key)
63
+ const blobsKey = Hyperdrive.getContentKey(manifest, key)
64
+ const blobsCore = this.store.get({ manifest: blobsManifest })
65
+
66
+ return { manifest, key, core, blobsManifest, blobsKey, blobsCore }
67
+ }
68
+
69
+ async requestCore(
70
+ publicKeys,
71
+ namespace,
72
+ srcCore,
73
+ length,
74
+ { force = false, quorum, peerUpdateTimeout } = {}
75
+ ) {
76
+ await srcCore.ready()
77
+ this.swarm.join(srcCore.discoveryKey, { client: true, server: false })
78
+ await srcCore.download({ start: 0, end: length }).done()
79
+
80
+ if (!force) await MultisigUtil.verifyCoreRequestable(srcCore, length, { peerUpdateTimeout })
81
+
82
+ const manifest = MultisigUtil.getManifest(publicKeys, namespace, { quorum })
83
+ const request = await SignRequest.generate(srcCore, { manifest, length })
84
+ return { manifest, request }
85
+ }
86
+
87
+ async requestDrive(
88
+ publicKeys,
89
+ namespace,
90
+ srcDrive,
91
+ length,
92
+ { force = false, quorum, peerUpdateTimeout } = {}
93
+ ) {
94
+ await srcDrive.ready()
95
+ this.swarm.join(srcDrive.discoveryKey, { client: true, server: false })
96
+ await srcDrive.getBlobs()
97
+ length = length || srcDrive.core.length
98
+
99
+ if (!force) {
100
+ await MultisigUtil.verifyCoreRequestable(srcDrive.core, length, {
101
+ peerUpdateTimeout,
102
+ coreId: 'db'
103
+ })
104
+ const contentLength = await srcDrive.getBlobsLength(length)
105
+ await MultisigUtil.verifyCoreRequestable(srcDrive.blobs.core, contentLength, {
106
+ peerUpdateTimeout,
107
+ coreId: 'blobs'
108
+ })
109
+ }
110
+
111
+ const manifest = MultisigUtil.getManifest(publicKeys, namespace, { quorum })
112
+ const request = await SignRequest.generateDrive(srcDrive, { manifest, length })
113
+ return { manifest, request }
114
+ }
115
+
116
+ async commitCore(
117
+ publicKeys,
118
+ namespace,
119
+ srcCore,
120
+ request,
121
+ responses,
122
+ { force = false, dryRun, quorum, skipTargetChecks = false, peerUpdateTimeout } = {}
123
+ ) {
124
+ await srcCore.ready()
125
+ this.swarm.join(srcCore.discoveryKey, { client: true, server: false })
126
+
127
+ const { manifest, core } = await this.createCore(publicKeys, namespace, { quorum })
128
+ this.swarm.join(core.discoveryKey)
129
+
130
+ const { length } = SignRequest.decode(z32.decode(request))
131
+
132
+ if (!force) {
133
+ await MultisigUtil.verifyCoreCommittable(srcCore, core, length, {
134
+ skipTargetChecks,
135
+ peerUpdateTimeout
136
+ })
137
+ }
138
+
139
+ const signResponses = []
140
+ for (const response of responses) {
141
+ const res = cenc.decode(CoreSign.messages.Response, z32.decode(response))
142
+ await CoreSign.verify(response, request, z32.encode(res.publicKey))
143
+ const publicKeyHex = res.publicKey.toString('hex')
144
+ signResponses[publicKeyHex] = res
145
+ }
146
+ const obtainedQuorum = Object.keys(signResponses).length
147
+ if (!dryRun && obtainedQuorum < manifest.quorum) {
148
+ throw new Error(`Insufficient quorum: ${obtainedQuorum} / ${manifest.quorum}`)
149
+ }
150
+
151
+ // NOTE: the ordering is important here, must map to signers ordering
152
+ const signatures = manifest.signers.map((signer) => {
153
+ const publicKeyHex = signer.publicKey.toString('hex')
154
+ return signResponses[publicKeyHex]?.signatures[0]
155
+ })
156
+
157
+ const batch = await MultisigUtil.signCore(core, srcCore, signatures, {
158
+ end: length,
159
+ commit: !dryRun
160
+ })
161
+ const result = {
162
+ destCore: await MultisigUtil.getCoreInfo(core),
163
+ srcCore: await MultisigUtil.getCoreInfo(srcCore),
164
+ batch
165
+ }
166
+ return { manifest, quorum: obtainedQuorum, result }
167
+ }
168
+
169
+ async commitDrive(
170
+ publicKeys,
171
+ namespace,
172
+ srcDrive,
173
+ request,
174
+ responses,
175
+ { dryRun, quorum, force = false, skipTargetChecks = false, peerUpdateTimeout } = {}
176
+ ) {
177
+ await srcDrive.ready()
178
+ this.swarm.join(srcDrive.discoveryKey, { client: true, server: false })
179
+ await srcDrive.getBlobs()
180
+
181
+ const { manifest, core, blobsCore } = await this.createDrive(publicKeys, namespace, { quorum })
182
+ this.swarm.join(core.discoveryKey)
183
+
184
+ const { length, content } = SignRequest.decode(z32.decode(request))
185
+ const blobsLength = content.length
186
+
187
+ if (!force) {
188
+ await MultisigUtil.verifyCoreCommittable(srcDrive.db.core, core, length, {
189
+ skipTargetChecks,
190
+ peerUpdateTimeout,
191
+ coreId: 'db'
192
+ })
193
+ await MultisigUtil.verifyCoreCommittable(srcDrive.blobs.core, blobsCore, blobsLength, {
194
+ skipTargetChecks,
195
+ peerUpdateTimeout,
196
+ coreId: 'blobs'
197
+ })
198
+ }
199
+
200
+ const signResponses = []
201
+ for (const response of responses) {
202
+ const res = cenc.decode(CoreSign.messages.Response, z32.decode(response))
203
+ await CoreSign.verify(response, request, z32.encode(res.publicKey))
204
+ const publicKeyHex = res.publicKey.toString('hex')
205
+ signResponses[publicKeyHex] = res
206
+ }
207
+ const obtainedQuorum = Object.keys(signResponses).length
208
+ if (!dryRun && obtainedQuorum < manifest.quorum) {
209
+ throw new Error(`Insufficient quorum: ${obtainedQuorum} / ${manifest.quorum}`)
210
+ }
211
+
212
+ const allSignatures = manifest.signers.map((signer) => {
213
+ const publicKeyHex = signer.publicKey.toString('hex')
214
+ return signResponses[publicKeyHex]?.signatures
215
+ })
216
+ // NOTE: the ordering is important here, must map to signers ordering
217
+ const signatures = allSignatures.map((item) => item?.[0])
218
+ const blobsSignatures = allSignatures.map((item) => item?.[1])
219
+
220
+ const { batch, blobsBatch } = await MultisigUtil.signDrive(
221
+ core,
222
+ srcDrive.core,
223
+ signatures,
224
+ blobsCore,
225
+ srcDrive.blobs.core,
226
+ blobsSignatures,
227
+ { end: length, blobsEnd: blobsLength, commit: !dryRun }
228
+ )
229
+ const result = {
230
+ db: {
231
+ destCore: await MultisigUtil.getCoreInfo(core),
232
+ srcCore: await MultisigUtil.getCoreInfo(srcDrive.core),
233
+ batch
234
+ },
235
+ blobs: {
236
+ destCore: await MultisigUtil.getCoreInfo(blobsCore),
237
+ srcCore: await MultisigUtil.getCoreInfo(srcDrive.blobs.core),
238
+ batch: blobsBatch
239
+ }
240
+ }
241
+ return { manifest, quorum: obtainedQuorum, result }
242
+ }
243
+ }
244
+
245
+ module.exports = HyperMultisig
package/lib/error.js ADDED
@@ -0,0 +1,80 @@
1
+ class MultisigError extends Error {
2
+ constructor(msg, code, fn = MultisigError) {
3
+ super(`${code}: ${msg}`)
4
+ this.code = code
5
+
6
+ if (Error.captureStackTrace) {
7
+ Error.captureStackTrace(this, fn)
8
+ }
9
+ }
10
+
11
+ get name() {
12
+ return 'MultisigError'
13
+ }
14
+
15
+ static SOURCE_CORE_TOO_SMALL(length, { coreId = '' } = {}) {
16
+ return new MultisigError(
17
+ `${coreId} Source core is smaller than the requested length of ${length}`,
18
+ 'SOURCE_CORE_TOO_SMALL',
19
+ MultisigError.SOURCE_CORE_TOO_SMALL
20
+ )
21
+ }
22
+
23
+ static TARGET_CORE_TOO_BIG() {
24
+ return new MultisigError(
25
+ 'The target core already has higher length than the source core. Committing this signing request will most likely corrupt your core',
26
+ 'TARGET_CORE_TOO_BIG',
27
+ MultisigError.TARGET_CORE_TOO_BIG
28
+ )
29
+ }
30
+
31
+ static SOURCE_CORE_INSUFFICIENT_PEERS(nrPeers, minPeers) {
32
+ return new MultisigError(
33
+ `Source core is not well seeded (${nrPeers}/${minPeers} peers)`,
34
+ 'SOURCE_CORE_INSUFFICIENT_PEERS',
35
+ MultisigError.SOURCE_CORE_INSUFFICIENT_PEERS
36
+ )
37
+ }
38
+
39
+ static SOURCE_CORE_NOT_FULLY_SEEDED(fullCopies, minPeers, { coreId = '' } = {}) {
40
+ return new MultisigError(
41
+ `${coreId} Source core is not yet fully downloaded by sufficient seeders (${fullCopies}/${minPeers})`,
42
+ 'SOURCE_CORE_NOT_FULLY_SEEDED',
43
+ MultisigError.SOURCE_CORE_NOT_FULLY_SEEDED
44
+ )
45
+ }
46
+
47
+ static TARGET_NOT_EMPTY() {
48
+ return new MultisigError(
49
+ 'Target core is not empty, so you should not skip those checks',
50
+ 'TARGET_NOT_EMPTY',
51
+ MultisigError.TARGET_NOT_EMPTY
52
+ )
53
+ }
54
+
55
+ static TARGET_CORE_INSUFFICIENT_PEERS(nrPeers, minPeers, { coreId = '' } = {}) {
56
+ return new MultisigError(
57
+ `${coreId} Target core is not well seeded (${nrPeers}/${minPeers} peers)`,
58
+ 'TARGET_CORE_INSUFFICIENT_PEERS',
59
+ MultisigError.TARGET_CORE_INSUFFICIENT_PEERS
60
+ )
61
+ }
62
+
63
+ static TARGET_CORE_NOT_FULLY_SEEDED(fullCopies, minPeers, { coreId = '' } = {}) {
64
+ return new MultisigError(
65
+ `${coreId} Target core is not yet fully downloaded by sufficient seeders (${fullCopies}/${minPeers})`,
66
+ 'TARGET_CORE_NOT_FULLY_SEEDED',
67
+ MultisigError.TARGET_CORE_NOT_FULLY_SEEDED
68
+ )
69
+ }
70
+
71
+ static INCOMPATIBLE_SOURCE_AND_TARGET({ coreId = '' } = {}) {
72
+ return new MultisigError(
73
+ `${coreId} Target core contains different data from the source core. Committing this signing request will corrupt your core.`,
74
+ 'INCOMPATIBLE_SOURCE_AND_TARGET',
75
+ MultisigError.INCOMPATIBLE_SOURCE_AND_TARGET
76
+ )
77
+ }
78
+ }
79
+
80
+ module.exports = MultisigError
package/lib/util.js ADDED
@@ -0,0 +1,330 @@
1
+ /**
2
+ * @typedef {import('corestore')} Corestore
3
+ * @typedef {import('hypercore/lib/download')} Download
4
+ * @typedef {{
5
+ * version: number
6
+ * hash: string
7
+ * quorum: number
8
+ * signers: Array<{
9
+ * signature: string
10
+ * publicKey: Buffer
11
+ * namespace: Buffer
12
+ * }>
13
+ * }} Manifest
14
+ * @typedef {{
15
+ * key: string
16
+ * length: number
17
+ * treeHash: string
18
+ * }} CoreInfo
19
+ */
20
+
21
+ const b4a = require('b4a')
22
+ const Hypercore = require('hypercore')
23
+ const { assemble, partialSignature } = require('hypercore/lib/multisig')
24
+ const crypto = require('hypercore-crypto')
25
+ const idEnc = require('hypercore-id-encoding')
26
+
27
+ const MultisigError = require('./error')
28
+
29
+ /**
30
+ * @param {Hypercore} core
31
+ * @param {Hypercore} batch
32
+ * @param {Buffer[]} signatures
33
+ * @param {{
34
+ * length?: number
35
+ * start?: number
36
+ * end?: number
37
+ * commit?: boolean
38
+ * }} opts
39
+ * @return {Promise<CoreInfo>}
40
+ */
41
+ async function signCore(core, fromCore, signatures, { length, start, end, commit } = {}) {
42
+ /** @type {Hypercore | null} */
43
+ let batch = null
44
+ try {
45
+ batch = await createUpdateBatch(core, fromCore, { start, end })
46
+ if (commit) {
47
+ await commitUpdateBatch(core, batch, signatures, { length })
48
+ }
49
+ const batchInfo = await getCoreInfo(batch)
50
+ return batchInfo
51
+ } finally {
52
+ if (batch) await batch.close()
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @param {Hypercore} core
58
+ * @param {Hypercore} fromCore
59
+ * @param {Buffer[]} signatures
60
+ * @param {Hypercore} blobsCore
61
+ * @param {Hypercore} fromBlobsCore
62
+ * @param {Buffer[]} blobsSignatures
63
+ * @param {{
64
+ * start?: number
65
+ * end?: number
66
+ * length?: number
67
+ * blobsStart?: number
68
+ * blobsEnd?: number
69
+ * blobsLength?: number
70
+ * commit?: boolean
71
+ * }} opts
72
+ * @return {Promise<{ batch: CoreInfo, blobsBatch: CoreInfo }>}
73
+ */
74
+ async function signDrive(
75
+ core,
76
+ fromCore,
77
+ signatures,
78
+ blobsCore,
79
+ fromBlobsCore,
80
+ blobsSignatures,
81
+ { start, end, length, blobsStart, blobsEnd, blobsLength, commit } = {}
82
+ ) {
83
+ const blobsBatch = await signCore(blobsCore, fromBlobsCore, blobsSignatures, {
84
+ start: blobsStart,
85
+ end: blobsEnd,
86
+ length: blobsLength,
87
+ commit
88
+ })
89
+ const batch = await signCore(core, fromCore, signatures, {
90
+ start,
91
+ end,
92
+ length,
93
+ commit
94
+ })
95
+ return { batch, blobsBatch }
96
+ }
97
+
98
+ /**
99
+ * @param {Hypercore} core
100
+ * @param {Hypercore} fromCore
101
+ * @param {{ start?: number, end?: number }} opts
102
+ * @return {Promise<Hypercore>}
103
+ */
104
+ async function createUpdateBatch(core, fromCore, { start, end } = {}) {
105
+ start = start ?? core.length
106
+ end = end ?? fromCore.length
107
+
108
+ const download = fromCore.download({ start, end })
109
+ await download.done()
110
+
111
+ /** @type {Hypercore} */
112
+ const batch = core.session({ name: 'batch', overwrite: true })
113
+ for (let idx = start; idx < end; idx += 1) {
114
+ await batch.append(await fromCore.get(idx))
115
+ }
116
+ return batch
117
+ }
118
+
119
+ /**
120
+ * @param {Hypercore} core
121
+ * @param {Hypercore} batch
122
+ * @param {Buffer[]} signatures
123
+ * @param {{ length?: number }} opts
124
+ */
125
+ async function commitUpdateBatch(core, batch, signatures, { length } = {}) {
126
+ length = length || batch.length
127
+
128
+ const proofs = await Promise.all(
129
+ signatures.map(async (sig, idx) => {
130
+ if (!sig) return null
131
+ const proof = await partialSignature(batch, idx, length, length, sig) // idx is important here, must match with the signers index
132
+ return proof
133
+ })
134
+ )
135
+ const validProofs = proofs.filter(Boolean)
136
+ const multisig = assemble(validProofs)
137
+
138
+ await core.commit(batch, { signature: multisig, length })
139
+ }
140
+
141
+ /**
142
+ * @param {string[]} publicKeys
143
+ * @param {string} namespace
144
+ * @return {string}
145
+ */
146
+ function getCoreKey(publicKeys, namespace) {
147
+ const manifest = getManifest(publicKeys, namespace)
148
+ return Hypercore.key(manifest)
149
+ }
150
+
151
+ /**
152
+ * @param {string[]} publicKeys
153
+ * @param {string} namespace
154
+ * @return {Manifest}
155
+ */
156
+ function getManifest(publicKeys, namespace, { quorum } = {}) {
157
+ if (!quorum) quorum = Math.floor(publicKeys.length / 2) + 1
158
+
159
+ return {
160
+ version: 1,
161
+ hash: 'blake2b',
162
+ quorum,
163
+ signers: publicKeys.map((publicKey) => ({
164
+ signature: 'ed25519',
165
+ publicKey: idEnc.decode(publicKey),
166
+ namespace: idEnc.decode(getNamespace(namespace))
167
+ }))
168
+ }
169
+ }
170
+
171
+ /**
172
+ * @param {string} namespace
173
+ * @return {string}
174
+ */
175
+ function getNamespace(namespace) {
176
+ return idEnc.normalize(crypto.hash(Buffer.from(namespace)))
177
+ }
178
+
179
+ /**
180
+ * @param {Manifest} manifest
181
+ */
182
+ function normalizeManifest(manifest) {
183
+ return {
184
+ ...manifest,
185
+ signers: manifest.signers.map((signer) => ({
186
+ ...signer,
187
+ publicKey: idEnc.normalize(signer.publicKey),
188
+ namespace: idEnc.normalize(signer.namespace)
189
+ }))
190
+ }
191
+ }
192
+
193
+ /**
194
+ * @param {Hypercore} core
195
+ * @return {Promise<{ key: string, length: number, treeHash: string }>}
196
+ */
197
+ async function getCoreInfo(core) {
198
+ await core.ready()
199
+ return {
200
+ key: idEnc.normalize(core.key),
201
+ length: core.length,
202
+ treeHash: idEnc.normalize(await core.treeHash())
203
+ }
204
+ }
205
+
206
+ async function verifyCoreRequestable(
207
+ srcCore,
208
+ length,
209
+ { minPeers = 2, peerUpdateTimeout = 5000, coreId } = {}
210
+ ) {
211
+ await waitUntilCoreLength(srcCore, length, { timeout: peerUpdateTimeout })
212
+
213
+ if (length > srcCore.length) {
214
+ throw MultisigError.SOURCE_CORE_TOO_SMALL(length, { coreId })
215
+ }
216
+
217
+ await waitUntilSufficientPeers(srcCore, { minPeers, timeout: peerUpdateTimeout })
218
+
219
+ const nrSrcPeers = srcCore.peers.length
220
+ if (nrSrcPeers < minPeers) {
221
+ throw MultisigError.SOURCE_CORE_INSUFFICIENT_PEERS(nrSrcPeers, minPeers)
222
+ }
223
+
224
+ await waitUntilFullySeeded(srcCore, { minPeers, timeout: peerUpdateTimeout })
225
+
226
+ let srcFullCopies = 0
227
+ for (const p of srcCore.peers) {
228
+ if (p.remoteContiguousLength === srcCore.length) srcFullCopies++
229
+ }
230
+ if (srcFullCopies < minPeers) {
231
+ throw MultisigError.SOURCE_CORE_NOT_FULLY_SEEDED(srcFullCopies, minPeers, { coreId })
232
+ }
233
+ }
234
+
235
+ async function verifyCoreCommittable(
236
+ srcCore,
237
+ tgtCore,
238
+ length,
239
+ { minPeers = 2, skipTargetChecks = false, peerUpdateTimeout = 5000, coreId } = {}
240
+ ) {
241
+ await waitUntilCoreLength(srcCore, length, { timeout: peerUpdateTimeout })
242
+
243
+ if (length > srcCore.length) {
244
+ throw MultisigError.SOURCE_CORE_TOO_SMALL(length, { coreId })
245
+ }
246
+
247
+ // Either it corrupts the core, or it's a no-op (re-signing already signed data). There's no possible upside.
248
+ if (tgtCore.length > srcCore.length) {
249
+ throw MultisigError.TARGET_CORE_TOO_BIG()
250
+ }
251
+
252
+ await waitUntilSufficientPeers(srcCore, { minPeers, timeout: peerUpdateTimeout })
253
+
254
+ const nrSrcPeers = srcCore.peers.length
255
+ if (nrSrcPeers < minPeers) {
256
+ throw MultisigError.SOURCE_CORE_INSUFFICIENT_PEERS(nrSrcPeers, minPeers)
257
+ }
258
+
259
+ await waitUntilFullySeeded(srcCore, { minPeers, timeout: peerUpdateTimeout })
260
+
261
+ let srcFullCopies = 0
262
+ for (const p of srcCore.peers) {
263
+ if (p.remoteContiguousLength === srcCore.length) srcFullCopies++
264
+ }
265
+ if (srcFullCopies < minPeers) {
266
+ throw MultisigError.SOURCE_CORE_NOT_FULLY_SEEDED(srcFullCopies, minPeers, { coreId })
267
+ }
268
+
269
+ if (skipTargetChecks) {
270
+ // no checks on the target if it's the first commit
271
+ if (tgtCore.length > 0) throw MultisigError.TARGET_NOT_EMPTY()
272
+ return
273
+ }
274
+
275
+ const tgtPeers = tgtCore.peers.length
276
+ if (tgtPeers < minPeers) {
277
+ throw MultisigError.TARGET_CORE_INSUFFICIENT_PEERS(tgtPeers, minPeers, { coreId })
278
+ }
279
+
280
+ let tgtFullCopies = 0
281
+ for (const p of tgtCore.peers) {
282
+ if (p.remoteContiguousLength === tgtCore.length) tgtFullCopies++
283
+ }
284
+ if (tgtFullCopies < minPeers) {
285
+ throw MultisigError.TARGET_CORE_NOT_FULLY_SEEDED(tgtFullCopies, minPeers, { coreId })
286
+ }
287
+
288
+ if (!b4a.equals(await tgtCore.treeHash(tgtCore.length), await srcCore.treeHash(tgtCore.length))) {
289
+ throw MultisigError.INCOMPATIBLE_SOURCE_AND_TARGET({ coreId })
290
+ }
291
+ }
292
+
293
+ async function waitUntilCoreLength(core, length, { timeout = 5000 } = {}) {
294
+ return await waitUntil(() => core.length >= length, { timeout })
295
+ }
296
+
297
+ async function waitUntilSufficientPeers(core, { minPeers = 2, timeout = 5000 } = {}) {
298
+ return await waitUntil(() => core.peers?.length >= minPeers, { timeout })
299
+ }
300
+
301
+ async function waitUntilFullySeeded(core, { minPeers = 2, timeout = 5000 } = {}) {
302
+ return await waitUntil(
303
+ () => core.peers?.filter((p) => p.remoteContiguousLength === core.length).length >= minPeers,
304
+ { timeout }
305
+ )
306
+ }
307
+
308
+ async function waitUntil(conditionFn, { timeout = 5000, interval = 100 } = {}) {
309
+ if (await conditionFn()) return true
310
+ if (timeout < 0) return false
311
+ await new Promise((resolve) => setTimeout(resolve, interval))
312
+ return waitUntil(conditionFn, { timeout: timeout - interval, interval })
313
+ }
314
+
315
+ module.exports = {
316
+ signCore,
317
+ signDrive,
318
+ createUpdateBatch,
319
+ commitUpdateBatch,
320
+ getCoreKey,
321
+ getManifest,
322
+ getNamespace,
323
+ normalizeManifest,
324
+ getCoreInfo,
325
+ verifyCoreRequestable,
326
+ verifyCoreCommittable,
327
+ waitUntilCoreLength,
328
+ waitUntilSufficientPeers,
329
+ waitUntilFullySeeded
330
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "hyper-multisig",
3
+ "version": "0.0.1",
4
+ "description": "Multisig hypercore and hyperdrive",
5
+ "main": "index.js",
6
+ "files": [
7
+ "lib/",
8
+ "index.js"
9
+ ],
10
+ "scripts": {
11
+ "format": "prettier --write . && lunte --fix",
12
+ "lint": "prettier --check . && lunte",
13
+ "test": "brittle -c test.js",
14
+ "test-bare": "brittle-bare -c test.js"
15
+ },
16
+ "dependencies": {
17
+ "b4a": "^1.7.4",
18
+ "compact-encoding": "^2.18.0",
19
+ "hypercore": "^11.24.0",
20
+ "hypercore-crypto": "^3.6.1",
21
+ "hypercore-id-encoding": "^1.3.0",
22
+ "hypercore-sign": "^3.0.0",
23
+ "hypercore-signing-request": "^4.0.2",
24
+ "hyperdrive": "^13.2.1",
25
+ "z32": "^1.1.0"
26
+ },
27
+ "devDependencies": {
28
+ "brittle": "^3.19.1",
29
+ "corestore": "^7.8.0",
30
+ "hyperdht": "^6.29.0",
31
+ "hyperswarm": "^4.16.0",
32
+ "lunte": "^1.6.0",
33
+ "prettier": "^3.8.1",
34
+ "prettier-config-holepunch": "^2.0.0",
35
+ "sodium-native": "^5.0.10"
36
+ },
37
+ "author": "Holepunch",
38
+ "license": "Apache-2.0",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/holepunchto/hyper-multisig.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/holepunchto/hyper-multisig/issues"
45
+ },
46
+ "homepage": "https://github.com/holepunchto/hyper-multisig#readme"
47
+ }