hypercore 11.0.30 → 11.0.32

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/index.js CHANGED
@@ -935,11 +935,18 @@ class Hypercore extends EventEmitter {
935
935
  async proof (opts) {
936
936
  if (this.opened === false) await this.opening
937
937
  const rx = this.state.storage.read()
938
- const promise = MerkleTree.proof(this.state, rx)
938
+ const promise = MerkleTree.proof(this.state, rx, opts)
939
939
  rx.tryFlush()
940
940
  return promise
941
941
  }
942
942
 
943
+ async verifyFullyRemote (proof) {
944
+ if (this.opened === false) await this.opening
945
+ const batch = await MerkleTree.verifyFullyRemote(this.state, proof)
946
+ await this.core._verifyBatchUpgrade(batch, proof.manifest)
947
+ return batch
948
+ }
949
+
943
950
  registerExtension (name, handlers = {}) {
944
951
  if (this.extensions.has(name)) {
945
952
  const ext = this.extensions.get(name)
@@ -1022,7 +1029,7 @@ class Hypercore extends EventEmitter {
1022
1029
 
1023
1030
  _updateEncryption () {
1024
1031
  const e = this.encryption
1025
- this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, block: e.block })
1032
+ this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, block: b4a.equals(e.blockKey, e.key) })
1026
1033
  if (e === this.core.encryption) this.core.encryption = this.encryption
1027
1034
  }
1028
1035
  }
package/lib/core.js CHANGED
@@ -532,7 +532,21 @@ module.exports = class Core {
532
532
  async verifyReorg (proof) {
533
533
  const batch = new ReorgBatch(this.state)
534
534
  await MerkleTree.reorg(this.state, proof, batch)
535
- this._verifyBatchUpgrade(batch, proof.manifest)
535
+ const manifest = this._verifyBatchUpgrade(batch, proof.manifest)
536
+
537
+ if (manifest && !this.header.manifest) {
538
+ await this.state.mutex.lock()
539
+ try {
540
+ if (manifest && this.header.manifest === null) {
541
+ const tx = this.state.createWriteBatch()
542
+ this._setManifest(tx, Verifier.createManifest(manifest), null)
543
+ await this.state.flush()
544
+ }
545
+ } finally {
546
+ this.state._unlock()
547
+ }
548
+ }
549
+
536
550
  return batch
537
551
  }
538
552
 
@@ -359,15 +359,15 @@ module.exports = class SessionState {
359
359
  }
360
360
 
361
361
  async truncate (length, fork, { signature, keyPair } = {}) {
362
- if (this.prologue && length < this.prologue.length) {
363
- throw INVALID_OPERATION('Truncation breaks prologue')
364
- }
365
-
366
362
  if (!keyPair && this.isDefault()) keyPair = this.core.header.keyPair
367
363
 
368
364
  await this.mutex.lock()
369
365
 
370
366
  try {
367
+ if (this.prologue && length < this.prologue.length) {
368
+ throw INVALID_OPERATION('Truncation breaks prologue')
369
+ }
370
+
371
371
  const batch = this.createTreeBatch()
372
372
  await MerkleTree.truncate(this, length, batch, fork)
373
373
 
@@ -759,31 +759,33 @@ module.exports = class SessionState {
759
759
 
760
760
  const totalLength = Math.max(length, this.length)
761
761
 
762
- const firstPage = getBitfieldPage(treeLength)
763
- const lastPage = getBitfieldPage(totalLength)
762
+ if (totalLength > treeLength) {
763
+ const firstPage = getBitfieldPage(treeLength)
764
+ const lastPage = getBitfieldPage(totalLength - 1)
764
765
 
765
- const srx = this.storage.read()
766
- const bitfieldPagePromise = srx.getBitfieldPage(firstPage)
767
- srx.tryFlush()
766
+ const srx = this.storage.read()
767
+ const bitfieldPagePromise = srx.getBitfieldPage(firstPage)
768
+ srx.tryFlush()
768
769
 
769
- const bitfieldPage = await bitfieldPagePromise
770
+ const bitfieldPage = await bitfieldPagePromise
770
771
 
771
- let index = treeLength
772
+ let index = treeLength
772
773
 
773
- for (let i = firstPage; i <= lastPage; i++) {
774
- const page = b4a.alloc(Bitfield.BYTES_PER_PAGE)
775
- tx.putBitfieldPage(i, page)
774
+ for (let i = firstPage; i <= lastPage; i++) {
775
+ const page = b4a.alloc(Bitfield.BYTES_PER_PAGE)
776
+ tx.putBitfieldPage(i, page)
776
777
 
777
- // copy existing bits in
778
- if (i === firstPage && bitfieldPage) page.set(bitfieldPage)
778
+ // copy existing bits in
779
+ if (i === firstPage && bitfieldPage) page.set(bitfieldPage)
779
780
 
780
- if (index < length) {
781
- index = fillBitfieldPage(page, index, length, i, true)
782
- if (index < length) continue
783
- }
781
+ if (index < length) {
782
+ index = fillBitfieldPage(page, index, length, i, true)
783
+ if (index < length) continue
784
+ }
784
785
 
785
- if (index < this.length) {
786
- index = fillBitfieldPage(page, index, this.length, i, false)
786
+ if (index < this.length) {
787
+ index = fillBitfieldPage(page, index, this.length, i, false)
788
+ }
787
789
  }
788
790
  }
789
791
 
@@ -1027,16 +1029,14 @@ function getBitfieldPage (index) {
1027
1029
  return Math.floor(index / Bitfield.BITS_PER_PAGE)
1028
1030
  }
1029
1031
 
1030
- function getBitfieldOffset (index) {
1031
- return index & (Bitfield.BITS_PER_PAGE - 1)
1032
- }
1033
-
1034
1032
  function fillBitfieldPage (page, start, end, pageIndex, value) {
1035
- const last = ((pageIndex + 1) * Bitfield.BITS_PER_PAGE) - 1
1036
- const from = getBitfieldOffset(start)
1033
+ const offset = pageIndex * Bitfield.BITS_PER_PAGE
1034
+ const max = offset + Bitfield.BITS_PER_PAGE
1037
1035
 
1038
- const index = last < end ? last : end
1039
- const to = getBitfieldOffset(index)
1036
+ const index = max < end ? max : end
1037
+
1038
+ const from = start - offset
1039
+ const to = index - offset
1040
1040
 
1041
1041
  quickbit.fill(page, value, from, to)
1042
1042
 
@@ -1044,8 +1044,10 @@ function fillBitfieldPage (page, start, end, pageIndex, value) {
1044
1044
  }
1045
1045
 
1046
1046
  async function storeBitfieldRange (storage, tx, from, to, value) {
1047
+ if (from >= to) return
1048
+
1047
1049
  const firstPage = getBitfieldPage(from)
1048
- const lastPage = getBitfieldPage(to)
1050
+ const lastPage = getBitfieldPage(to - 1)
1049
1051
 
1050
1052
  let index = from
1051
1053
 
@@ -1057,9 +1059,11 @@ async function storeBitfieldRange (storage, tx, from, to, value) {
1057
1059
  }
1058
1060
 
1059
1061
  rx.tryFlush()
1062
+
1060
1063
  const pages = await Promise.all(promises)
1064
+ const cnt = lastPage - firstPage + 1
1061
1065
 
1062
- for (let i = 0; i <= lastPage - firstPage; i++) {
1066
+ for (let i = 0; i < cnt; i++) {
1063
1067
  const pageIndex = i + firstPage
1064
1068
  if (!pages[i]) pages[i] = b4a.alloc(Bitfield.BYTES_PER_PAGE)
1065
1069
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.0.30",
3
+ "version": "11.0.32",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {