hypercore 11.40.3 → 11.40.4

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.
@@ -22,13 +22,10 @@ class SlimSession {
22
22
  }
23
23
  }
24
24
 
25
- module.exports = { verify, proof }
25
+ module.exports = { verify, proof, encode: encodeProof, decode: decodeProof }
26
26
 
27
27
  async function verify(storage, buffer, { referrer = null } = {}) {
28
- const state = { buffer, start: 0, end: buffer.byteLength }
29
-
30
- const discoveryKey = c.fixed32.decode(state)
31
- const proof = messages.wire.data.decode(state)
28
+ const { discoveryKey, proof } = decodeProof(buffer)
32
29
 
33
30
  const result = {
34
31
  key: null,
@@ -109,29 +106,46 @@ async function verify(storage, buffer, { referrer = null } = {}) {
109
106
  return result
110
107
  }
111
108
 
112
- async function proof(sender, { index, block = null, upgrade = null } = {}) {
113
- const proof = await sender.proof({
114
- block: block ? { index, nodes: 0 } : null,
115
- upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
116
- })
109
+ function decodeProof(buffer) {
110
+ const state = { buffer, start: 0, end: buffer.byteLength }
117
111
 
118
- if (block) proof.block.value = block
119
- proof.manifest = sender.core.header.manifest
112
+ const discoveryKey = c.fixed32.decode(state)
113
+ const proof = messages.wire.data.decode(state)
114
+
115
+ return { discoveryKey, proof }
116
+ }
120
117
 
118
+ function encodeProof({ discoveryKey, proof }) {
121
119
  const state = { buffer: null, start: 0, end: 0 }
122
- const data = { request: 0, ...proof }
123
120
 
124
- c.fixed32.preencode(state, sender.discoveryKey)
125
- messages.wire.data.preencode(state, data)
121
+ c.fixed32.preencode(state, discoveryKey)
122
+ messages.wire.data.preencode(state, proof)
126
123
 
127
124
  state.buffer = b4a.allocUnsafe(state.end)
128
125
 
129
- c.fixed32.encode(state, sender.discoveryKey)
130
- messages.wire.data.encode(state, data)
126
+ c.fixed32.encode(state, discoveryKey)
127
+ messages.wire.data.encode(state, proof)
131
128
 
132
129
  return state.buffer
133
130
  }
134
131
 
132
+ async function proof(sender, { index, block = null, upgrade = null, encode = true } = {}) {
133
+ const proof = await sender.proof({
134
+ block: block ? { index, nodes: 0 } : null,
135
+ upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
136
+ })
137
+
138
+ if (block) proof.block.value = block
139
+ proof.manifest = sender.core.header.manifest
140
+
141
+ const result = {
142
+ discoveryKey: sender.discoveryKey,
143
+ proof: { request: 0, ...proof }
144
+ }
145
+
146
+ return encode ? encodeProof(result) : result
147
+ }
148
+
135
149
  function isNull(x) {
136
150
  return x === null
137
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.40.3",
3
+ "version": "11.40.4",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {