hypercore 11.40.2 → 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 +39 -18
- package/lib/merkle-tree.js +3 -1
- package/lib/receiver-queue.js +1 -1
- package/lib/replicator.js +3 -3
- 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,
|
|
@@ -72,7 +69,14 @@ async function verify(storage, buffer, { referrer = null } = {}) {
|
|
|
72
69
|
|
|
73
70
|
if (!auth.manifest || !auth.manifest.signers.length) return null
|
|
74
71
|
|
|
75
|
-
|
|
72
|
+
let batch = null
|
|
73
|
+
try {
|
|
74
|
+
batch = MerkleTree.verifyFullyRemote(new SlimSession(core, auth, head, roots), proof)
|
|
75
|
+
} catch (err) {
|
|
76
|
+
if (err.code !== 'INVALID_PROOF') throw err
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
79
|
+
|
|
76
80
|
const publicKey = auth.manifest.signers[0].publicKey
|
|
77
81
|
|
|
78
82
|
let signable = null
|
|
@@ -102,29 +106,46 @@ async function verify(storage, buffer, { referrer = null } = {}) {
|
|
|
102
106
|
return result
|
|
103
107
|
}
|
|
104
108
|
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
block: block ? { index, nodes: 0 } : null,
|
|
108
|
-
upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
|
|
109
|
-
})
|
|
109
|
+
function decodeProof(buffer) {
|
|
110
|
+
const state = { buffer, start: 0, end: buffer.byteLength }
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
proof
|
|
112
|
+
const discoveryKey = c.fixed32.decode(state)
|
|
113
|
+
const proof = messages.wire.data.decode(state)
|
|
113
114
|
|
|
115
|
+
return { discoveryKey, proof }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function encodeProof({ discoveryKey, proof }) {
|
|
114
119
|
const state = { buffer: null, start: 0, end: 0 }
|
|
115
|
-
const data = { request: 0, ...proof }
|
|
116
120
|
|
|
117
|
-
c.fixed32.preencode(state,
|
|
118
|
-
messages.wire.data.preencode(state,
|
|
121
|
+
c.fixed32.preencode(state, discoveryKey)
|
|
122
|
+
messages.wire.data.preencode(state, proof)
|
|
119
123
|
|
|
120
124
|
state.buffer = b4a.allocUnsafe(state.end)
|
|
121
125
|
|
|
122
|
-
c.fixed32.encode(state,
|
|
123
|
-
messages.wire.data.encode(state,
|
|
126
|
+
c.fixed32.encode(state, discoveryKey)
|
|
127
|
+
messages.wire.data.encode(state, proof)
|
|
124
128
|
|
|
125
129
|
return state.buffer
|
|
126
130
|
}
|
|
127
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
|
+
|
|
128
149
|
function isNull(x) {
|
|
129
150
|
return x === null
|
|
130
151
|
}
|
package/lib/merkle-tree.js
CHANGED
|
@@ -825,6 +825,8 @@ class MerkleTree {
|
|
|
825
825
|
}
|
|
826
826
|
}
|
|
827
827
|
|
|
828
|
+
if (unverified) throw INVALID_PROOF('Proof not covered by the upgrade')
|
|
829
|
+
|
|
828
830
|
return batch
|
|
829
831
|
}
|
|
830
832
|
|
|
@@ -1154,7 +1156,7 @@ async function seekFromHead(session, head, bytes, padding) {
|
|
|
1154
1156
|
|
|
1155
1157
|
for (let i = 0; i < roots.length; i++) {
|
|
1156
1158
|
const root = roots[i]
|
|
1157
|
-
const node = await
|
|
1159
|
+
const node = await getTreeNodeFromStorageOrError(session.storage, root)
|
|
1158
1160
|
const size = getUnpaddedSize(node, padding, null)
|
|
1159
1161
|
|
|
1160
1162
|
if (bytes === size) return root
|
package/lib/receiver-queue.js
CHANGED
package/lib/replicator.js
CHANGED
|
@@ -1386,7 +1386,7 @@ class Peer {
|
|
|
1386
1386
|
return
|
|
1387
1387
|
}
|
|
1388
1388
|
|
|
1389
|
-
const rem = start & (RemoteBitfield.
|
|
1389
|
+
const rem = start & (RemoteBitfield.BITS_PER_PAGE - 1)
|
|
1390
1390
|
if (rem > 0) {
|
|
1391
1391
|
start -= rem
|
|
1392
1392
|
length += rem
|
|
@@ -1401,7 +1401,7 @@ class Peer {
|
|
|
1401
1401
|
this.missingBlocks.insert(start, remote.bitfield)
|
|
1402
1402
|
}
|
|
1403
1403
|
|
|
1404
|
-
start += RemoteBitfield.
|
|
1404
|
+
start += RemoteBitfield.BITS_PER_PAGE
|
|
1405
1405
|
}
|
|
1406
1406
|
|
|
1407
1407
|
this._clearLocalRange(fixedStart, length)
|
|
@@ -1596,7 +1596,7 @@ class Peer {
|
|
|
1596
1596
|
|
|
1597
1597
|
for (let i = 0; i < len; i++) {
|
|
1598
1598
|
let index = off + i
|
|
1599
|
-
if (index
|
|
1599
|
+
if (index >= s.seeker.end) index -= len
|
|
1600
1600
|
|
|
1601
1601
|
if (this._remoteHasBlock(index) === false) continue
|
|
1602
1602
|
if (this.core.bitfield.get(index) === true) continue
|