Skip to content

Commit 602e27f

Browse files
jacobheunvasco-santos
authored andcommitted
fix: verify public key exists in validator (#21)
1 parent 5750dd0 commit 602e27f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ const extractPublicKey = (peerId, entry, callback) => {
193193
}
194194
return callback(null, pubKey)
195195
}
196-
callback(null, peerId.pubKey)
196+
197+
if (peerId.pubKey) {
198+
callback(null, peerId.pubKey)
199+
} else {
200+
callback(Object.assign(new Error('no public key is available'), { code: ERRORS.ERR_UNDEFINED_PARAMETER }))
201+
}
197202
}
198203

199204
// rawStdEncoding with RFC4648

test/index.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ describe('ipns', function () {
256256
})
257257
})
258258

259+
it('validator with no valid public key should error', (done) => {
260+
const sequence = 0
261+
const validity = 1000000
262+
263+
ipns.create(rsa, cid, sequence, validity, (err, entry) => {
264+
expect(err).to.not.exist()
265+
266+
const marshalledData = ipns.marshal(entry)
267+
const key = Buffer.from(`/ipns/${ipfsId.id}`)
268+
269+
ipns.validator.validate(marshalledData, key, (err, valid) => {
270+
expect(err).to.exist()
271+
expect(err.code).to.eql(ERRORS.ERR_UNDEFINED_PARAMETER)
272+
expect(valid).to.not.exist()
273+
done()
274+
})
275+
})
276+
})
277+
259278
it('should use validator.validate to validate a record', (done) => {
260279
const sequence = 0
261280
const validity = 1000000

0 commit comments

Comments
 (0)