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

Commit 9f66bca

Browse files
atvanguarddaviddias
authored andcommitted
feat: If link hashes to serializer are base58 encoded, then decode them [Fi… (#43)
* If link hashes to serializer are base58 encoded, then decode them [Fixes #28] * Add test
1 parent 3062b11 commit 9f66bca

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

src/dag-link/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ class DAGLink {
6161

6262
exports = module.exports = DAGLink
6363
exports.create = require('./create')
64+
exports.util = require('./util')

src/dag-link/util.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const DAGLink = require('./index')
4+
5+
function isDagLink (link) {
6+
return link && link.constructor && link.constructor.name === 'DAGLink'
7+
}
8+
9+
function createDagLinkFromB58EncodedHash (link) {
10+
return new DAGLink(
11+
link.name ? link.name : link.Name,
12+
link.size ? link.size : link.Size,
13+
link.hash || link.Hash || link.multihash
14+
)
15+
}
16+
17+
exports = module.exports
18+
exports.isDagLink = isDagLink
19+
exports.createDagLinkFromB58EncodedHash = createDagLinkFromB58EncodedHash

src/dag-node/create.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,8 @@ function create (data, dagLinks, hashAlg, callback) {
3333
hashAlg = 'sha2-256'
3434
}
3535

36-
const links = dagLinks.map((l) => {
37-
if (l.constructor && l.constructor.name === 'DAGLink') {
38-
return l
39-
}
40-
41-
return new DAGLink(
42-
l.name ? l.name : l.Name,
43-
l.size ? l.size : l.Size,
44-
l.hash || l.Hash || l.multihash
45-
)
36+
const links = dagLinks.map((link) => {
37+
return DAGLink.util.isDagLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
4638
})
4739
const sortedLinks = sort(links, linkSort)
4840

src/util.js

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ function cid (node, callback) {
1818
function serialize (node, callback) {
1919
let serialized
2020

21+
// If the node is not an instance of a DAGNode, the link.hash might be a Base58 encoded string; decode it
22+
if (node.constructor.name !== 'DAGNode' && node.links) {
23+
node.links = node.links.map((link) => {
24+
return DAGLink.util.isDagLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
25+
})
26+
}
27+
2128
try {
2229
serialized = proto.PBNode.encode(toProtoBuf(node))
2330
} catch (err) {

test/dag-node-test.js

+35
Original file line numberDiff line numberDiff line change
@@ -587,5 +587,40 @@ module.exports = (repo) => {
587587
done()
588588
})
589589
})
590+
591+
it('deserializing a node and an object should yield the same result', (done) => {
592+
expect(10).checks(done)
593+
const obj = {
594+
data: Buffer.from('Hello World'),
595+
links: [{
596+
multihash: 'QmUxD5gZfKzm8UN4WaguAMAZjw2TzZ2ZUmcqm2qXPtais7',
597+
name: 'payload',
598+
size: 819
599+
}]
600+
}
601+
602+
DAGNode.create(obj.data, obj.links, (err, node) => {
603+
expect(err).to.not.exist.mark()
604+
expect(node.data.length).to.be.above(0).mark()
605+
expect(Buffer.isBuffer(node.data)).to.be.true.mark()
606+
expect(node.size).to.be.above(0).mark()
607+
expect(node.toJSON().multihash).to.be.equal('QmR2W8uRZuVfUk8YtuAH3ezJwJzMuVbuehL2NAc4TmAz93')
608+
609+
dagPB.util.serialize(node, (err, serialized) => {
610+
expect(err).to.not.exist.mark()
611+
dagPB.util.serialize(obj, (err, serializedObject) => {
612+
expect(err).to.not.exist.mark()
613+
dagPB.util.deserialize(serialized, (err, deserialized) => {
614+
expect(err).to.not.exist.mark()
615+
dagPB.util.deserialize(serializedObject, (err, deserializedObject) => {
616+
expect(err).to.not.exist.mark()
617+
expect(deserialized.toJSON()).to.deep.equal(deserializedObject.toJSON()).mark()
618+
done()
619+
})
620+
})
621+
})
622+
})
623+
})
624+
}).timeout(6000)
590625
})
591626
}

0 commit comments

Comments
 (0)