This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly serialize CID instances
The CID version agnostic tests ipfs-inactive/interface-js-ipfs-core#413 identified some functions were not properly serializing CID instances. This PR adds `cleanCID` step to several functions and also updates the `cleanCID` function to not assume buffer CIDs are CIDv0 multihashes. depends on ipfs-inactive/interface-js-ipfs-core#413 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Alan Shaw
committed
Dec 12, 2018
1 parent
72955db
commit 45b344c
Showing
4 changed files
with
25 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
'use strict' | ||
|
||
const bs58 = require('bs58') | ||
const CID = require('cids') | ||
|
||
module.exports = function (cid) { | ||
if (Buffer.isBuffer(cid)) { | ||
cid = bs58.encode(cid) | ||
return new CID(cid).toString() | ||
} | ||
if (CID.isCID(cid)) { | ||
cid = cid.toBaseEncodedString() | ||
return cid.toString() | ||
} | ||
if (typeof cid !== 'string') { | ||
throw new Error('unexpected cid type: ' + typeof cid) | ||
} | ||
CID.validateCID(new CID(cid.split('/')[0])) | ||
new CID(cid.split('/')[0]) // eslint-disable-line | ||
return cid | ||
} |