Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducible local writers #163

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ Options include:
map: node => mappedNode, // map nodes before returning them
reduce: (a, b) => someNode, // reduce the nodes array before returning it
firstNode: false, // set to true to reduce the nodes array to the first node in it
valueEncoding: 'binary' // set the value encoding of the db
valueEncoding: 'binary', // set the value encoding of the db
localKey: localKeyBuffer, // public key to use for the local writer
localSecretKey: localSecretKeyBuffer // secret key to use for the local writer
}
```

The `localKey` and `localSecretKey` options are useful when you need reproducible local writers (for example to recreate a database). If they are not provided, a new key pair will be generated for the local writer. If provided, both keys must be provided together.

#### `db.key`

Buffer containing the public key identifying this hyperdb.
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ function HyperDB (storage, key, opts) {
this._watching = checkout ? checkout._watching : []
this._replicating = []
this._localWriter = null
this._localKey = opts.localKey || null
this._localSecretKey = opts.localSecretKey || null
this._byKey = new Map()
this._heads = opts.heads || null
this._version = opts.version || null
Expand Down Expand Up @@ -582,7 +584,7 @@ HyperDB.prototype._ready = function (cb) {
this.source.ready(function (err) {
if (err) return done(err)
if (self.source.writable) self.local = self.source
if (!self.local) self.local = feed('local')
if (!self.local) self.local = feed('local', self._localKey, { secretKey: self._localSecretKey })

self.key = self.source.key
self.discoveryKey = self.source.discoveryKey
Expand Down
17 changes: 17 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ var Readable = require('stream').Readable
var create = require('./helpers/create')
var run = require('./helpers/run')

tape('reproducible local hypercore', function (t) {
var key = Buffer.from('de14f28853123596830410a371b38bd4768fcb95880142e9e54a00aa8c418158', 'hex')
var localKey = Buffer.from('c575d78d42d42b88db80c3b66ba6cacdb0e633f98e375226b23788c5d5dd4a17', 'hex')
var localSecretKey = Buffer.from('778e1ed4513175ebac7cfcc97dbde5bd87795c990811784e85de0abc3b2da36ac575d78d42d42b88db80c3b66ba6cacdb0e633f98e375226b23788c5d5dd4a17', 'hex')

var db = create.one(key, {
localKey,
localSecretKey
})

db.on('ready', function () {
t.same(db.local.key, localKey)
t.same(db.local.secretKey, localSecretKey)
t.end()
})
})

tape('basic put/get', function (t) {
var db = create.one()
db.put('hello', 'world', function (err, node) {
Expand Down