Last active
March 31, 2017 20:46
-
-
Save dignifiedquire/c0722bdc05b375a5e8ed7e1224a6edae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const multihashing = require('multihashing-async') | |
| const CID = require('cids') | |
| const mh = require('multihashing') | |
| // data - Buffer or string, the data to encode | |
| // hashtype - string | |
| // codec - string, 'dag-pb' or 'dag-cbor', one | |
| // callback - called with (Error, CID) depending on the result | |
| function elcidEncode (data, hashtype, codec, callback) { | |
| if (typeof data === 'string') { | |
| data = new Buffer(data) | |
| } | |
| multihashing(data, hashtype, (err, digest) => { | |
| if (err) { | |
| return callback(err) | |
| } | |
| callback(null, new CID(1, codec, digest)) | |
| }) | |
| } | |
| // data - Buffer or string, the data to decode | |
| function elcidDecode (data) { | |
| const cid = new CID(data) | |
| return { | |
| hashType: mh.decode(cid.multihash).name | |
| codec: cid.codec, | |
| data: cid.multihash.toString('hex') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment