Skip to content

Commit

Permalink
fix: validator should create peer id (#13)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: having the libp2p-record protobuf definition compliant with go-libp2p-record. Author and signature were removed.
  • Loading branch information
vasco-santos authored Nov 16, 2018
1 parent b690e7f commit e1c1332
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const ipns = require('ipns')
const validator = ipns.validator
```

Contains an object with `validate (marshalledData, peerId, callback)` and `select (dataA, dataB, callback)` functions.
Contains an object with `validate (marshalledData, key, callback)` and `select (dataA, dataB, callback)` functions.

The `validate` function aims to verify if an IPNS record is valid. First the record is unmarshalled, then the public key is obtained and finally the record is validated (signature and validity are verified).

Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,16 @@ const marshal = ipnsEntryProto.encode
const unmarshal = ipnsEntryProto.decode

const validator = {
validate: (marshalledData, peerId, callback) => {
validate: (marshalledData, key, callback) => {
const receivedEntry = unmarshal(marshalledData)
const bufferId = key.slice('/ipns/'.length)
let peerId

try {
peerId = PeerId.createFromBytes(bufferId)
} catch (err) {
return callback(err)
}

// extract public key
extractPublicKey(peerId, receivedEntry, (err, pubKey) => {
Expand Down
6 changes: 4 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ describe('ipns', function () {
expect(err).to.not.exist()

const marshalledData = ipns.marshal(entry)
const key = Buffer.from(`/ipns/${ipfsId.id}`)

ipns.validator.validate(marshalledData, ipfsId, (err, valid) => {
ipns.validator.validate(marshalledData, key, (err, valid) => {
expect(err).to.not.exist()
expect(valid).to.equal(true)
done()
Expand All @@ -290,8 +291,9 @@ describe('ipns', function () {
// corrupt the record by changing the value to random bytes
entry.value = crypto.randomBytes(46).toString()
const marshalledData = ipns.marshal(entry)
const key = Buffer.from(`/ipns/${ipfsId.id}`)

ipns.validator.validate(marshalledData, ipfsId, (err) => {
ipns.validator.validate(marshalledData, key, (err) => {
expect(err).to.exist() // failed validation
done()
})
Expand Down

0 comments on commit e1c1332

Please sign in to comment.