Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
feat: If link hashes to serializer are base58 encoded, then decode th…
Browse files Browse the repository at this point in the history
…em [Fi… (#43)

* If link hashes to serializer are base58 encoded, then decode them [Fixes #28]

* Add test
  • Loading branch information
atvanguard authored and daviddias committed Dec 2, 2017
1 parent 3062b11 commit 9f66bca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/dag-link/index.js
Expand Up @@ -61,3 +61,4 @@ class DAGLink {

exports = module.exports = DAGLink
exports.create = require('./create')
exports.util = require('./util')
19 changes: 19 additions & 0 deletions src/dag-link/util.js
@@ -0,0 +1,19 @@
'use strict'

const DAGLink = require('./index')

function isDagLink (link) {
return link && link.constructor && link.constructor.name === 'DAGLink'
}

function createDagLinkFromB58EncodedHash (link) {
return new DAGLink(
link.name ? link.name : link.Name,
link.size ? link.size : link.Size,
link.hash || link.Hash || link.multihash
)
}

exports = module.exports
exports.isDagLink = isDagLink
exports.createDagLinkFromB58EncodedHash = createDagLinkFromB58EncodedHash
12 changes: 2 additions & 10 deletions src/dag-node/create.js
Expand Up @@ -33,16 +33,8 @@ function create (data, dagLinks, hashAlg, callback) {
hashAlg = 'sha2-256'
}

const links = dagLinks.map((l) => {
if (l.constructor && l.constructor.name === 'DAGLink') {
return l
}

return new DAGLink(
l.name ? l.name : l.Name,
l.size ? l.size : l.Size,
l.hash || l.Hash || l.multihash
)
const links = dagLinks.map((link) => {
return DAGLink.util.isDagLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
const sortedLinks = sort(links, linkSort)

Expand Down
7 changes: 7 additions & 0 deletions src/util.js
Expand Up @@ -18,6 +18,13 @@ function cid (node, callback) {
function serialize (node, callback) {
let serialized

// If the node is not an instance of a DAGNode, the link.hash might be a Base58 encoded string; decode it
if (node.constructor.name !== 'DAGNode' && node.links) {
node.links = node.links.map((link) => {
return DAGLink.util.isDagLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
}

try {
serialized = proto.PBNode.encode(toProtoBuf(node))
} catch (err) {
Expand Down
35 changes: 35 additions & 0 deletions test/dag-node-test.js
Expand Up @@ -587,5 +587,40 @@ module.exports = (repo) => {
done()
})
})

it('deserializing a node and an object should yield the same result', (done) => {
expect(10).checks(done)
const obj = {
data: Buffer.from('Hello World'),
links: [{
multihash: 'QmUxD5gZfKzm8UN4WaguAMAZjw2TzZ2ZUmcqm2qXPtais7',
name: 'payload',
size: 819
}]
}

DAGNode.create(obj.data, obj.links, (err, node) => {
expect(err).to.not.exist.mark()
expect(node.data.length).to.be.above(0).mark()
expect(Buffer.isBuffer(node.data)).to.be.true.mark()
expect(node.size).to.be.above(0).mark()
expect(node.toJSON().multihash).to.be.equal('QmR2W8uRZuVfUk8YtuAH3ezJwJzMuVbuehL2NAc4TmAz93')

dagPB.util.serialize(node, (err, serialized) => {
expect(err).to.not.exist.mark()
dagPB.util.serialize(obj, (err, serializedObject) => {
expect(err).to.not.exist.mark()
dagPB.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist.mark()
dagPB.util.deserialize(serializedObject, (err, deserializedObject) => {
expect(err).to.not.exist.mark()
expect(deserialized.toJSON()).to.deep.equal(deserializedObject.toJSON()).mark()
done()
})
})
})
})
})
}).timeout(6000)
})
}

0 comments on commit 9f66bca

Please sign in to comment.