Skip to content

Commit e1c1332

Browse files
authored
fix: validator should create peer id (#13)
BREAKING CHANGE: having the libp2p-record protobuf definition compliant with go-libp2p-record. Author and signature were removed.
1 parent b690e7f commit e1c1332

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const ipns = require('ipns')
120120
const validator = ipns.validator
121121
```
122122

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

125125
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).
126126

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,16 @@ const marshal = ipnsEntryProto.encode
283283
const unmarshal = ipnsEntryProto.decode
284284

285285
const validator = {
286-
validate: (marshalledData, peerId, callback) => {
286+
validate: (marshalledData, key, callback) => {
287287
const receivedEntry = unmarshal(marshalledData)
288+
const bufferId = key.slice('/ipns/'.length)
289+
let peerId
290+
291+
try {
292+
peerId = PeerId.createFromBytes(bufferId)
293+
} catch (err) {
294+
return callback(err)
295+
}
288296

289297
// extract public key
290298
extractPublicKey(peerId, receivedEntry, (err, pubKey) => {

test/index.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ describe('ipns', function () {
267267
expect(err).to.not.exist()
268268

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

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

294-
ipns.validator.validate(marshalledData, ipfsId, (err) => {
296+
ipns.validator.validate(marshalledData, key, (err) => {
295297
expect(err).to.exist() // failed validation
296298
done()
297299
})

0 commit comments

Comments
 (0)