Skip to content

Commit

Permalink
fix: get discovery key from core.key
Browse files Browse the repository at this point in the history
core.discoveryKey is null when the hypercore is first initialized.
We ensure that core.key is always defined, but `core.discoveryKey`
is a getter, so we can't override that.
  • Loading branch information
gmaclennan committed Aug 30, 2023
1 parent f361ff1 commit 25865bf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Writable } = require('streamx')
const { TypedEmitter } = require('tiny-typed-emitter')
const { once } = require('events')
const raf = require('random-access-file')
const { discoveryKey } = require('hypercore-crypto')
// const log = require('debug')('multi-core-indexer')
const { CoreIndexStream } = require('./lib/core-index-stream')
const { MultiCoreIndexStream } = require('./lib/multi-core-index-stream')
Expand Down Expand Up @@ -174,9 +175,8 @@ class MultiCoreIndexer extends TypedEmitter {

module.exports = MultiCoreIndexer

/** @param {{ discoveryKey: null | Buffer }} core */
/** @param {{ key: Buffer }} core */
function getStorageName(core) {
// @ts-expect-error - we want this to throw if core.discoveryKey is nullish
const id = core.discoveryKey.toString('hex')
const id = discoveryKey(core.key).toString('hex')
return [id.slice(0, 2), id.slice(2, 4), id].join('/')
}
91 changes: 48 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"b4a": "^1.6.4",
"big-sparse-array": "^1.0.2",
"debug": "^4.3.3",
"hypercore-crypto": "^3.4.0",
"random-access-file": "^4.0.4",
"streamx": "^2.15.0",
"tiny-typed-emitter": "^2.1.0"
Expand Down
3 changes: 3 additions & 0 deletions vendor/types/hypercore-crypto.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'hypercore-crypto' {
export function discoveryKey(key: Buffer): Buffer
}

0 comments on commit 25865bf

Please sign in to comment.