Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: persist CIDv1 multicodec #37

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
const CID = require('cids');

/**
* Take an ipfsHash and convert it to a CID v1 encoded in base32.
* Take any ipfsHash and convert it to a CID v1 encoded in base32.
* @param {string} ipfsHash a regular ipfs hash either a cid v0 or v1 (v1 will remain unchanged)
* @return {string} the resulting ipfs hash as a cid v1
*/
const cidV0ToV1Base32 = (ipfsHash) => {
const cidV0 = new CID(ipfsHash);
const cidV1Base32 = new CID(1, 'dag-pb', cidV0.multihash, 'base32');
return cidV1Base32.toString();
let cid = new CID(ipfsHash);
if (cid.version === 0) {
cid = cid.toV1();
}
return cid.toString('base32');
}

exports.cidV0ToV1Base32 = cidV0ToV1Base32;
exports.cidV0ToV1Base32 = cidV0ToV1Base32;
18 changes: 10 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const onion = 'zqktlwi4fecvo6ri'
const onion_contentHash = 'bc037a716b746c776934666563766f367269';
const onion3 = 'p53lf57qovyuvwsc6xnrppyply3vtqm7l6pcobkmyqsiofyeznfu5uqd';
const onion3_contentHash = 'bd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164';
const ipfsBase32 = 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4';
const ipfsBase32DagPb = 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4';
const ipfsBase32Libp2pKey = 'bafzbeie5745rpv2m6tjyuugywy4d5ewrqgqqhfnf445he3omzpjbx5xqxe';

describe('content-hash (legacy tests)', () =>
describe('content-hash (legacy tests)', () =>
{
it('should decode a content hash', () => {

const actual_0 = contentHash.decode(ipfs_contentHash);
const actual_1 = contentHash.decode(swarm_contentHash);
let actual_2 = contentHash.decode(onion_contentHash);
Expand All @@ -27,22 +27,18 @@ describe('content-hash (legacy tests)', () =>
actual_2.should.be.equal(onion);
});
it('should encode an ipfs address', () => {

const actual = contentHash.fromIpfs(ipfs);
actual.should.be.equal(ipfs_contentHash);
});
it('should encode a swarm address', () => {

const actual = contentHash.fromSwarm(swarm);
actual.should.be.equal(swarm_contentHash);
});
it('should encode an onion address', () => {

const actual = contentHash.encode('onion', onion);
actual.should.be.equal(onion_contentHash);
});
it('should get a codec from a content hash', () => {

const actual_0 = contentHash.getCodec(ipfs_contentHash);
const actual_1 = contentHash.getCodec(swarm_contentHash);
const actual_2 = contentHash.getCodec(onion_contentHash);
Expand Down Expand Up @@ -128,7 +124,13 @@ describe('content-hash', () => {
describe('helpers', () => {
it('should convert CID v0 into v1', () => {
const actual = contentHash.helpers.cidV0ToV1Base32(ipfs);
actual.should.be.equal(ipfsBase32);
actual.should.be.equal(ipfsBase32DagPb);
});
it('should keep CID v1 Base32 as-is', () => {
const dagPbCid = contentHash.helpers.cidV0ToV1Base32(ipfsBase32DagPb);
dagPbCid.should.be.equal(ipfsBase32DagPb);
const libp2pKeyCid = contentHash.helpers.cidV0ToV1Base32(ipfsBase32Libp2pKey);
libp2pKeyCid.should.be.equal(ipfsBase32Libp2pKey);
});
});
});