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.
@@ -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,
@@ -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
- const batch = await MerkleTree.verifyFullyRemote(new SlimSession(core, auth, head, roots), proof)
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
- async function proof(sender, { index, block = null, upgrade = null } = {}) {
106
- const proof = await sender.proof({
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
- if (block) proof.block.value = block
112
- proof.manifest = sender.core.header.manifest
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, sender.discoveryKey)
118
- messages.wire.data.preencode(state, data)
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, sender.discoveryKey)
123
- messages.wire.data.encode(state, data)
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
  }
@@ -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 getTreeNodeFromStorage(session.storage, root)
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
@@ -34,7 +34,7 @@ module.exports = class ReceiverQueue {
34
34
  }
35
35
 
36
36
  _processRequest(req) {
37
- if (req.block || req.hash || req.seek || req.upgrade || req.manifest) {
37
+ if (this.requests.get(req.id) === req) {
38
38
  this.requests.delete(req.id)
39
39
  this.length--
40
40
  return req
package/lib/replicator.js CHANGED
@@ -1386,7 +1386,7 @@ class Peer {
1386
1386
  return
1387
1387
  }
1388
1388
 
1389
- const rem = start & (RemoteBitfield.BITS_PER_SEGMENT - 1)
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.BITS_PER_SEGMENT
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 > s.seeker.end) index -= len
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.40.2",
3
+ "version": "11.40.4",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {