Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Last active March 31, 2017 20:46
Show Gist options
  • Select an option

  • Save dignifiedquire/c0722bdc05b375a5e8ed7e1224a6edae to your computer and use it in GitHub Desktop.

Select an option

Save dignifiedquire/c0722bdc05b375a5e8ed7e1224a6edae to your computer and use it in GitHub Desktop.
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