Skip to content

Commit

Permalink
feat: validate CID multicodec
Browse files Browse the repository at this point in the history
- require CID with 'libp2p-key' (CIDv1) or 'dag-pb' (CIDv0 converted to CIDv1)
- delegate CID validation to CID constructor

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Oct 28, 2019
1 parent d4fee10 commit 2b11192
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
15 changes: 7 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,14 @@ exports.createFromB58String = (str) => {
return exports.createFromCID(str) // B58String is CIDv0
}

const validMulticodec = (cid) => {
// supported: 'libp2p-key' (CIDv1) and 'dag-pb' (CIDv0 converted to CIDv1)
return cid.codec === 'libp2p-key' || cid.codec === 'dag-pb'
}

exports.createFromCID = (cid) => {
if (typeof cid === 'string' || Buffer.isBuffer(cid)) {
cid = new CID(cid)
} else if (CID.isCID(cid)) {
CID.validateCID(cid) // throws on error
} else {
// provide more meaningful error than the one in CID.validateCID
throw new Error('Supplied cid value is neither String|CID|Buffer')
}
cid = CID.isCID(cid) ? cid : new CID(cid)
if (!validMulticodec(cid)) throw new Error('Supplied PeerID CID has invalid multicodec: ' + cid.codec)
return new PeerIdWithIs(cid.multihash)
}

Expand Down
36 changes: 31 additions & 5 deletions test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,21 @@ describe('PeerId', () => {
expect(testIdBytes).to.deep.equal(id.toBytes())
})

it('recreate from CIDv1 Base32', () => {
const id = PeerId.createFromCID(testIdCIDString)
expect(testIdCIDString).to.equal(id.toString())
expect(testId.id).to.equal(id.toHexString())
it('recreate from CIDv1 Base32 (libp2p-key multicodec)', () => {
const cid = new CID(1, 'libp2p-key', testIdBytes)
const cidString = cid.toBaseEncodedString('base32')
const id = PeerId.createFromCID(cidString)
expect(cidString).to.equal(id.toString())
expect(testIdBytes).to.deep.equal(id.toBytes())
})

it('recreate from CIDv1 Base32 (dag-pb multicodec)', () => {
const cid = new CID(1, 'dag-pb', testIdBytes)
const cidString = cid.toBaseEncodedString('base32')
const id = PeerId.createFromCID(cidString)
// toString should return CID with multicodec set to libp2p-key
expect(new CID(id.toString()).codec).to.equal('libp2p-key')
expect(testIdBytes).to.deep.equal(id.toBytes())
})

it('recreate from CID Buffer', () => {
Expand All @@ -107,6 +118,14 @@ describe('PeerId', () => {
expect(testIdBytes).to.deep.equal(id.toBytes())
})

it('throws on invalid CID multicodec', () => {
// only libp2p and dag-pb are supported
const invalidCID = new CID(1, 'raw', testIdBytes).toBaseEncodedString('base32')
expect(() => {
PeerId.createFromCID(invalidCID)
}).to.throw(/Supplied PeerID CID has invalid multicodec: raw/)
})

it('throws on invalid CID value', () => {
// using function code that does not represent valid hash function
// https://github.com/multiformats/js-multihash/blob/b85999d5768bf06f1b0f16b926ef2cb6d9c14265/src/constants.js#L345
Expand All @@ -120,7 +139,14 @@ describe('PeerId', () => {
const invalidCID = {}
expect(() => {
PeerId.createFromCID(invalidCID)
}).to.throw(/Supplied cid value is neither String|CID|Buffer/)
}).to.throw(/Invalid version, must be a number equal to 1 or 0/)
})

it('throws on invalid CID object', () => {
const invalidCID = {}
expect(() => {
PeerId.createFromCID(invalidCID)
}).to.throw(/Invalid version, must be a number equal to 1 or 0/)
})

it('recreate from a Public Key', async () => {
Expand Down

0 comments on commit 2b11192

Please sign in to comment.