hypercore 10.20.1 → 10.20.2

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/core.js CHANGED
@@ -223,7 +223,7 @@ module.exports = class Core {
223
223
 
224
224
  this.tree.signature = signature || auth.sign(this.tree.signable())
225
225
 
226
- if (signature && !this._signed(this.tree)) {
226
+ if (signature && !this._verifyBatch(this.tree)) {
227
227
  // TODO: how to handle signature failure?
228
228
  this.tree.signature = null
229
229
  throw INVALID_SIGNATURE('Clone was provided with an invalid signature')
@@ -423,17 +423,18 @@ module.exports = class Core {
423
423
  }
424
424
  }
425
425
 
426
- _signed (batch, hash, auth = this.defaultAuth) {
426
+ _verifyBatch (batch) {
427
+ const hash = batch.hash()
427
428
  const signable = this._legacy ? batch.signableLegacy(hash) : batch.signable(hash)
428
- return auth.verify(signable, batch.signature, batch)
429
- }
429
+ const auth = this.defaultAuth
430
430
 
431
- async _verifyExclusive ({ batch, bitfield, value, from }) {
432
- // TODO: move this to tree.js
433
- const hash = batch.hash()
434
- if (!batch.signature || !this._signed(batch, hash)) {
431
+ if (!batch.signature || !auth.verify(signable, batch.signature, batch)) {
435
432
  throw INVALID_SIGNATURE('Proof contains an invalid signature')
436
433
  }
434
+ }
435
+
436
+ async _verifyExclusive ({ batch, bitfield, value, from }) {
437
+ this._verifyBatch(batch)
437
438
 
438
439
  await this._mutex.lock()
439
440
 
@@ -541,9 +542,7 @@ module.exports = class Core {
541
542
 
542
543
  const batch = this.tree.verifyFullyRemote(proof)
543
544
 
544
- if (!batch.signature || !this._signed(batch, batch.hash())) {
545
- throw INVALID_SIGNATURE('Proof contains an invalid signature with no input from us')
546
- }
545
+ this._verifyBatch(batch)
547
546
 
548
547
  const remoteTreeHash = this.crypto.tree(proof.upgrade.nodes)
549
548
  const localTreeHash = this.crypto.tree(await this.tree.getRoots(proof.upgrade.length))
@@ -554,6 +553,14 @@ module.exports = class Core {
554
553
  return true
555
554
  }
556
555
 
556
+ async verifyReorg (proof) {
557
+ const batch = await this.tree.reorg(proof)
558
+
559
+ this._verifyBatch(batch)
560
+
561
+ return batch
562
+ }
563
+
557
564
  async verify (proof, from) {
558
565
  // We cannot apply "other forks" atm.
559
566
  // We should probably still try and they are likely super similar for non upgrades
package/lib/oplog.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const cenc = require('compact-encoding')
2
2
  const b4a = require('b4a')
3
3
  const { crc32 } = require('crc-universal')
4
- const { OPLOG_CORRUPT } = require('hypercore-errors')
4
+ const { OPLOG_CORRUPT, OPLOG_HEADER_OVERFLOW } = require('hypercore-errors')
5
5
 
6
6
  module.exports = class Oplog {
7
7
  constructor (storage, { pageSize = 4096, headerEncoding = cenc.raw, entryEncoding = cenc.raw, readonly = false } = {}) {
@@ -155,6 +155,7 @@ module.exports = class Oplog {
155
155
  const bit = (this._headers[i] + 1) & 1
156
156
 
157
157
  this.headerEncoding.preencode(state, header)
158
+ if (state.end > this._pageSize) throw OPLOG_HEADER_OVERFLOW()
158
159
  state.buffer = b4a.allocUnsafe(state.end)
159
160
  this.headerEncoding.encode(state, header)
160
161
  this._addHeader(state, state.end - 8, bit, 0)
package/lib/replicator.js CHANGED
@@ -1526,6 +1526,7 @@ module.exports = class Replicator {
1526
1526
  }
1527
1527
 
1528
1528
  async _onreorgdata (peer, req, data) {
1529
+ const newBatch = data.upgrade && await this.core.verifyReorg(data)
1529
1530
  const f = this._addReorg(data.fork, peer)
1530
1531
 
1531
1532
  if (f === null) {
@@ -1538,7 +1539,7 @@ module.exports = class Replicator {
1538
1539
  if (f.batch) {
1539
1540
  await f.batch.update(data)
1540
1541
  } else if (data.upgrade) {
1541
- f.batch = await this.core.tree.reorg(data)
1542
+ f.batch = newBatch
1542
1543
 
1543
1544
  // Remove "older" reorgs in progress as we just verified this one.
1544
1545
  this._clearOldReorgs(f.fork)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.20.1",
3
+ "version": "10.20.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -44,7 +44,7 @@
44
44
  "fast-fifo": "^1.3.0",
45
45
  "flat-tree": "^1.9.0",
46
46
  "hypercore-crypto": "^3.2.1",
47
- "hypercore-errors": "^1.0.0",
47
+ "hypercore-errors": "^1.1.0",
48
48
  "is-options": "^1.0.1",
49
49
  "protomux": "^3.5.0",
50
50
  "quickbit-universal": "^2.1.1",