ipld / js-ipld-dag-pb

JavaScript Implementation of the MerkleDAG Nodes with Protobuf.
MIT License
69 stars 20 forks source link

calculate multihash #157

Closed jgeary closed 5 years ago

jgeary commented 5 years ago

Hello, Stack overflow and Medium have suggested the following for calculating an ipfs hash from a data buffer (which is my main goal here):

const Unixfs = require('ipfs-unixfs')
const {DAGNode} = require('ipld-dag-pb')

const unixFs = new Unixfs('file', buf)

DAGNode.create(unixFs.marshal(), (err, dagNode) => {
  if (err) return console.error(err)
  console.log(dagNode.toJSON().multihash)
})

I have confirmed this works using js-ipld-dag-pb 0.14.8 and this is fine for my purposes.

Obviously much has changed with this package, most notably the removal of node.toJSON().multihash. How would one achieve the same result with the current version, 0.18.1?

Edit: I will use ipfs-only-hash.

rvagg commented 5 years ago

FYI the tests should help show examples of this, such as: https://github.com/ipld/js-ipld-dag-pb/blob/1cb217dca8c2b9fb7880229e62e5910369e2d52c/test/dag-node-test.js#L236-L238