hypercore-signing-request 5.0.1 → 5.1.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.
- package/README.md +17 -0
- package/index.js +83 -4
- 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,10 +97,56 @@ 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,
|
|
148
|
+
encodeResponse,
|
|
149
|
+
decodeResponse,
|
|
84
150
|
signable
|
|
85
151
|
}
|
|
86
152
|
|
|
@@ -93,7 +159,7 @@ async function generate (core, { length = core.length, fork = core.fork, manifes
|
|
|
93
159
|
if (!manifest) manifest = core.manifest
|
|
94
160
|
|
|
95
161
|
return c.encode(Request, {
|
|
96
|
-
version:
|
|
162
|
+
version: MAX_SUPPORTED_VERSION,
|
|
97
163
|
length,
|
|
98
164
|
fork,
|
|
99
165
|
treeHash: await core.treeHash(length),
|
|
@@ -115,7 +181,7 @@ async function generateDrive (drive, { length = drive.core.length, fork = drive.
|
|
|
115
181
|
}
|
|
116
182
|
|
|
117
183
|
return c.encode(Request, {
|
|
118
|
-
version:
|
|
184
|
+
version: MAX_SUPPORTED_VERSION,
|
|
119
185
|
length,
|
|
120
186
|
fork,
|
|
121
187
|
treeHash: await drive.core.treeHash(length),
|
|
@@ -134,6 +200,19 @@ function decode (buffer) {
|
|
|
134
200
|
return req
|
|
135
201
|
}
|
|
136
202
|
|
|
203
|
+
function encodeResponse (res) {
|
|
204
|
+
return c.encode(Response, res)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function decodeResponse (buffer) {
|
|
208
|
+
const state = { start: 0, end: buffer.byteLength, buffer }
|
|
209
|
+
const res = Response.decode(state)
|
|
210
|
+
|
|
211
|
+
if (state.start < state.end) throw new Error('Unparsed padding left in request, bailing')
|
|
212
|
+
|
|
213
|
+
return res
|
|
214
|
+
}
|
|
215
|
+
|
|
137
216
|
function signable (pub, req) {
|
|
138
217
|
const v = req.manifest.version
|
|
139
218
|
|