hypercore-signing-request 5.0.1 → 5.1.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/README.md +17 -0
- package/index.js +106 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,23 @@ Decode the signing request. Looks like this:
|
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
#### `responseBuffer = encodeResponse(response)`
|
|
45
|
+
|
|
46
|
+
Encode the signing response. Looks like this:
|
|
47
|
+
|
|
48
|
+
``` js
|
|
49
|
+
{
|
|
50
|
+
version, // request version
|
|
51
|
+
publicKey, // signer public key
|
|
52
|
+
requestHash, // request hash
|
|
53
|
+
signatures // array of signatures
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### `res = encodeResponse(responseBuffer)`
|
|
58
|
+
|
|
59
|
+
Decode the signing response.
|
|
60
|
+
|
|
44
61
|
#### `buffer = signable(publicKey, req)`
|
|
45
62
|
|
|
46
63
|
Get the buffer to sign. Pass your public key and it validates that you can sign it.
|
package/index.js
CHANGED
|
@@ -5,12 +5,20 @@ const caps = require('hypercore/lib/caps')
|
|
|
5
5
|
const m = require('hypercore/lib/messages')
|
|
6
6
|
const c = require('compact-encoding')
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const COMPAT_VERSION = 2
|
|
9
|
+
const MAX_SUPPORTED_VERSION = 3
|
|
10
|
+
|
|
9
11
|
const FLAG_DRIVE = 1
|
|
12
|
+
const REQUEST = 0
|
|
13
|
+
const RESPONSE = 1
|
|
10
14
|
|
|
11
15
|
const Request = {
|
|
12
16
|
preencode (state, req) {
|
|
13
17
|
c.uint.preencode(state, req.version)
|
|
18
|
+
if (req.version > 2) {
|
|
19
|
+
c.uint8.preencode(state, REQUEST)
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
c.uint.preencode(state, req.length)
|
|
15
23
|
c.uint.preencode(state, req.fork)
|
|
16
24
|
c.fixed32.preencode(state, req.treeHash)
|
|
@@ -25,6 +33,10 @@ const Request = {
|
|
|
25
33
|
},
|
|
26
34
|
encode (state, req) {
|
|
27
35
|
c.uint.encode(state, req.version)
|
|
36
|
+
if (req.version > 2) {
|
|
37
|
+
c.uint8.encode(state, REQUEST)
|
|
38
|
+
}
|
|
39
|
+
|
|
28
40
|
c.uint.encode(state, req.length)
|
|
29
41
|
c.uint.encode(state, req.fork)
|
|
30
42
|
c.fixed32.encode(state, req.treeHash)
|
|
@@ -41,7 +53,14 @@ const Request = {
|
|
|
41
53
|
},
|
|
42
54
|
decode (state) {
|
|
43
55
|
const version = c.uint.decode(state)
|
|
44
|
-
if (version >
|
|
56
|
+
if (version > MAX_SUPPORTED_VERSION) {
|
|
57
|
+
throw new Error('Unknown signing request version: ' + version)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const type = version < COMPAT_VERSION ? REQUEST : c.uint8.decode(state)
|
|
61
|
+
if (type !== REQUEST) {
|
|
62
|
+
throw new Error('Expected an encoded request')
|
|
63
|
+
}
|
|
45
64
|
|
|
46
65
|
const length = c.uint.decode(state)
|
|
47
66
|
const fork = c.uint.decode(state)
|
|
@@ -65,6 +84,7 @@ const Request = {
|
|
|
65
84
|
|
|
66
85
|
return {
|
|
67
86
|
version,
|
|
87
|
+
type,
|
|
68
88
|
id,
|
|
69
89
|
key,
|
|
70
90
|
length,
|
|
@@ -77,11 +97,59 @@ const Request = {
|
|
|
77
97
|
}
|
|
78
98
|
}
|
|
79
99
|
|
|
100
|
+
const Signatures = c.array(c.fixed64)
|
|
101
|
+
|
|
102
|
+
const Response = {
|
|
103
|
+
preencode (state, res) {
|
|
104
|
+
c.uint.preencode(state, res.version)
|
|
105
|
+
if (res.version > 2) {
|
|
106
|
+
c.uint8.preencode(state, RESPONSE)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
c.fixed32.preencode(state, res.requestHash)
|
|
110
|
+
c.fixed32.preencode(state, res.publicKey)
|
|
111
|
+
Signatures.preencode(state, res.signatures)
|
|
112
|
+
},
|
|
113
|
+
encode (state, res) {
|
|
114
|
+
c.uint.encode(state, res.version)
|
|
115
|
+
if (res.version > 2) {
|
|
116
|
+
c.uint8.encode(state, RESPONSE)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
c.fixed32.encode(state, res.requestHash)
|
|
120
|
+
c.fixed32.encode(state, res.publicKey)
|
|
121
|
+
Signatures.encode(state, res.signatures)
|
|
122
|
+
},
|
|
123
|
+
decode (state, res) {
|
|
124
|
+
const version = c.uint.decode(state)
|
|
125
|
+
if (version > MAX_SUPPORTED_VERSION) {
|
|
126
|
+
throw new Error('Response version is not supported, please upgrade')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const type = version > COMPAT_VERSION ? c.uint8.decode(state) : RESPONSE
|
|
130
|
+
if (type !== RESPONSE) {
|
|
131
|
+
throw new Error('Expected an encoded response')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
version,
|
|
136
|
+
type,
|
|
137
|
+
requestHash: c.fixed32.decode(state),
|
|
138
|
+
publicKey: c.fixed32.decode(state),
|
|
139
|
+
signatures: Signatures.decode(state)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
80
144
|
module.exports = {
|
|
81
145
|
generate,
|
|
82
146
|
generateDrive,
|
|
83
147
|
decode,
|
|
84
|
-
|
|
148
|
+
encodeResponse,
|
|
149
|
+
decodeResponse,
|
|
150
|
+
signable,
|
|
151
|
+
isRequest,
|
|
152
|
+
isResponse
|
|
85
153
|
}
|
|
86
154
|
|
|
87
155
|
async function generate (core, { length = core.length, fork = core.fork, manifest = null } = {}) {
|
|
@@ -93,7 +161,7 @@ async function generate (core, { length = core.length, fork = core.fork, manifes
|
|
|
93
161
|
if (!manifest) manifest = core.manifest
|
|
94
162
|
|
|
95
163
|
return c.encode(Request, {
|
|
96
|
-
version:
|
|
164
|
+
version: MAX_SUPPORTED_VERSION,
|
|
97
165
|
length,
|
|
98
166
|
fork,
|
|
99
167
|
treeHash: await core.treeHash(length),
|
|
@@ -115,7 +183,7 @@ async function generateDrive (drive, { length = drive.core.length, fork = drive.
|
|
|
115
183
|
}
|
|
116
184
|
|
|
117
185
|
return c.encode(Request, {
|
|
118
|
-
version:
|
|
186
|
+
version: MAX_SUPPORTED_VERSION,
|
|
119
187
|
length,
|
|
120
188
|
fork,
|
|
121
189
|
treeHash: await drive.core.treeHash(length),
|
|
@@ -124,6 +192,26 @@ async function generateDrive (drive, { length = drive.core.length, fork = drive.
|
|
|
124
192
|
})
|
|
125
193
|
}
|
|
126
194
|
|
|
195
|
+
function isRequest (buffer) {
|
|
196
|
+
const state = { start: 0, end: buffer.byteLength, buffer }
|
|
197
|
+
|
|
198
|
+
const version = c.uint.decode(state)
|
|
199
|
+
if (version <= COMPAT_VERSION) return true
|
|
200
|
+
|
|
201
|
+
const type = c.uint8.decode(state)
|
|
202
|
+
return type === REQUEST
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function isResponse (buffer) {
|
|
206
|
+
const state = { start: 0, end: buffer.byteLength, buffer }
|
|
207
|
+
|
|
208
|
+
const version = c.uint.decode(state)
|
|
209
|
+
if (version <= COMPAT_VERSION) return true
|
|
210
|
+
|
|
211
|
+
const type = c.uint8.decode(state)
|
|
212
|
+
return type === RESPONSE
|
|
213
|
+
}
|
|
214
|
+
|
|
127
215
|
function decode (buffer) {
|
|
128
216
|
const state = { start: 0, end: buffer.byteLength, buffer }
|
|
129
217
|
const req = Request.decode(state)
|
|
@@ -134,6 +222,19 @@ function decode (buffer) {
|
|
|
134
222
|
return req
|
|
135
223
|
}
|
|
136
224
|
|
|
225
|
+
function encodeResponse (res) {
|
|
226
|
+
return c.encode(Response, res)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function decodeResponse (buffer) {
|
|
230
|
+
const state = { start: 0, end: buffer.byteLength, buffer }
|
|
231
|
+
const res = Response.decode(state)
|
|
232
|
+
|
|
233
|
+
if (state.start < state.end) throw new Error('Unparsed padding left in request, bailing')
|
|
234
|
+
|
|
235
|
+
return res
|
|
236
|
+
}
|
|
237
|
+
|
|
137
238
|
function signable (pub, req) {
|
|
138
239
|
const v = req.manifest.version
|
|
139
240
|
|