Skip to content

Commit d7c8e51

Browse files
authored
fix: parse peer id from message correctly (#127)
Peer IDs are sent as a buffer containing the utf bytes for `/ipns/` then the bytes that make up the peer id. Our tests were using a buffer that contained the utf bytes for `/ipns/` followed by the utf bytes for the base encoded peer id, which is wrong. I think this only worked previously because the validator passed the bytes to `PeerId.createFromBytes` which passes them to the `cids` `CID` class constructor which is a lot more forgiving in terms of input than the multiformats `CID` class.
1 parent 1e6686d commit d7c8e51

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const ERRORS = require('./errors')
2929
const ID_MULTIHASH_CODE = identity.code
3030

3131
const namespace = '/ipns/'
32+
const IPNS_PREFIX = uint8ArrayFromString('/ipns/')
3233

3334
/**
3435
* @typedef {import('./types').IPNSEntry} IPNSEntry
@@ -453,8 +454,8 @@ const validator = {
453454
*/
454455
validate: async (marshalledData, key) => {
455456
const receivedEntry = unmarshal(marshalledData)
456-
const bufferId = uint8ArrayToString(key).substring('/ipns/'.length)
457-
const peerId = PeerId.parse(bufferId)
457+
const bufferId = key.slice(IPNS_PREFIX.length)
458+
const peerId = PeerId.createFromBytes(bufferId)
458459

459460
// extract public key
460461
const pubKey = extractPublicKey(peerId, receivedEntry)

test/index.spec.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { expect } = require('aegir/utils/chai')
55
const { base58btc } = require('multiformats/bases/base58')
66
const uint8ArrayFromString = require('uint8arrays/from-string')
7+
const uint8ArrayConcat = require('uint8arrays/concat')
78
const PeerId = require('peer-id')
89

910
const crypto = require('libp2p-crypto')
@@ -193,7 +194,8 @@ describe('ipns', function () {
193194
const entry = await ipns.create(rsa, cid, sequence, validity)
194195

195196
const marshalledData = ipns.marshal(entry)
196-
const key = uint8ArrayFromString(`/ipns/${ipfsId.id}`)
197+
const keyBytes = base58btc.decode(`z${ipfsId.id}`)
198+
const key = uint8ArrayConcat([uint8ArrayFromString('/ipns/'), keyBytes])
197199

198200
try {
199201
await ipns.validator.validate(marshalledData, key)
@@ -222,7 +224,9 @@ describe('ipns', function () {
222224
await ipns.embedPublicKey(rsa.public, entry)
223225

224226
const marshalledData = ipns.marshal(entry)
225-
const key = uint8ArrayFromString(`/ipns/${ipfsId.id}`)
227+
228+
const keyBytes = base58btc.decode(`z${ipfsId.id}`)
229+
const key = uint8ArrayConcat([uint8ArrayFromString('/ipns/'), keyBytes])
226230

227231
await ipns.validator.validate(marshalledData, key)
228232
})
@@ -237,7 +241,9 @@ describe('ipns', function () {
237241
// corrupt the record by changing the value to random bytes
238242
entry.value = crypto.randomBytes(46)
239243
const marshalledData = ipns.marshal(entry)
240-
const key = uint8ArrayFromString(`/ipns/${ipfsId.id}`)
244+
245+
const keyBytes = base58btc.decode(`z${ipfsId.id}`)
246+
const key = uint8ArrayConcat([uint8ArrayFromString('/ipns/'), keyBytes])
241247

242248
try {
243249
await ipns.validator.validate(marshalledData, key)

0 commit comments

Comments
 (0)