Skip to content
Merged
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: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ block associated with the signature.
Callback is called with `(err, success)` where success is true only if the signature is
correct.

#### `feed.rootHashes(index, callback)`

Retrieve the root *hashes* for given `index`.

Callback is called with `(err, roots)`; `roots` is an *Array* of root *hashes*.

#### `var number = feed.downloaded([start], [end])`

Returns total number of downloaded blocks within range.
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ Feed.prototype.signature = function (index, cb) {
Feed.prototype.verify = function (index, signature, cb) {
var self = this

this._getRootsToVerify(index * 2 + 2, {}, [], function (err, roots) {
this._withRoots(index, function (err, roots) {
if (err) return cb(err)

var checksum = crypto.tree(roots)
Expand All @@ -518,6 +518,14 @@ Feed.prototype.verify = function (index, signature, cb) {
})
}

Feed.prototype.rootHashes = function (index, cb) {
this._withRoots(index, cb)
}

Feed.prototype._withRoots = function (index, cb) {
this._getRootsToVerify(index * 2 + 2, {}, [], cb)
}

Feed.prototype.seek = function (bytes, opts, cb) {
if (typeof opts === 'function') return this.seek(bytes, null, opts)
if (!opts) opts = {}
Expand Down
30 changes: 30 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ tape('verify', function (t) {
})
})

tape('rootHashes', function (t) {
t.plan(9)

var feed = create()
var evilfeed = create(feed.key, {secretKey: feed.secretKey})

feed.append('test', function (err) {
t.error(err, 'no error')

evilfeed.append('t\0st', function (err) {
t.error(err, 'no error')

var result = []

feed.rootHashes(0, onroots)
evilfeed.rootHashes(0, onroots)

function onroots (err, roots) {
t.error(err, 'no error')
t.ok(roots instanceof Array)
result.push(roots)
if (result.length < 2) return
t.notEqual(result[0], result[1])
t.equal(result[0].length, result[1].length)
t.notEqual(Buffer.compare(result[0][0].hash, result[1][0].hash), 0)
}
})
})
})

tape('pass in secret key', function (t) {
var keyPair = crypto.keyPair()
var secretKey = keyPair.secretKey
Expand Down