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.
- package/lib/fully-remote-proof.js +31 -17
- package/package.json +1 -1
|
@@ -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
|
|
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
|
-
|
|
113
|
-
const
|
|
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
|
-
|
|
119
|
-
proof
|
|
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,
|
|
125
|
-
messages.wire.data.preencode(state,
|
|
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,
|
|
130
|
-
messages.wire.data.encode(state,
|
|
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
|
}
|