On the js ipfs implementation, I can store a dag object with:
const obj = { simple: 'object' }
ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
console.log(cid.toBaseEncodedString())
// zdpuAzE1oAAMpsfdoexcJv6PmL9UhE8nddUYGU32R98tzV5fv
})
I believe this shoots a message from my client node to the IPFS service, where the CID is then generated as the data is stored, and I get the CID in the callback function. I would like to see what this looks like using pre-computed hashes, bypassing the network for generating those CIDs until I want to store an object.
I’ve got the SHA3 npm library, but I don’t think it’s as simple as running a JSON.stringify(obj) through SHA3Hash(512) then into bs58.
const bs58 = require('bs58')
const SHA3 = require('sha3');
const obj = { simple: 'object' }
const dig = new SHA3.SHA3Hash(512)
.update(JSON.stringify(obj))
.digest('hex')
console.log(bs58.encode(Buffer.from(dig, 'hex')))
// 42iHiPUmns7ZhEovnNFy8wpN8S9c6XUQoj9oB56sqRJqfyQuqUnibcL2XMoKxaCq2U5hAbXsiogdBfXXgDJ8KLT8
I was looking at the multihashes
module, but this wasn’t quite right:
const bs58 = require('bs58')
const multihash = require('multihashes')
const obj = { simple: 'object' }
const buf = new Buffer(JSON.stringify(obj))
const encoded = multihash.encode(buf, 'sha3-512')
console.log(bs58.encode(encoded))
// 2Ebtp43hDzyUmL5pb4Pe3n2dhRrEg
Also, I was looking at the dagCBOR / dagPB modules… but this doesn’t look quite right either.
const bs58 = require('bs58')
const dagCBOR = require('ipld-dag-cbor')
const dagPB = require('ipld-dag-pb')
const obj = { simple: 'object' }
dagCBOR.util.serialize(obj, (err, dagCbor) => {
dagPB.DAGNode.create(dagCbor, [], 'sha3-512', (err, res) => {
if (err) throw error
console.log(bs58.encode(res.multihash))
// 8tVWpxRmq6DbntaNX9GDtE3zv1ySh3TwZ9JvEqcvTr2GrJW2dimdeHSQ9jkiYLWtLXapBrwPtRJFnixyAtkBh8Cx6P
})
})
Which library might I be able to use to generate this same hash (zdpuAzE1oAAMpsfdoexcJv6PmL9UhE8nddUYGU32R98tzV5fv
) that ipfs.dag.put
provides for an offline object?
I couldn’t quite make out from the documentation whether a dag object is essentially a JSON object with a user-defined structure (eg. { simple: 'object', random: 'zdpuAzE1oAAMpsfdoexcJv6PmL9UhE8nddUYGU32R98tzV5fv' }
), or if its some sort of fixed structure with a Data buffer, Links array (name, size, multihash), Size integer, and b58-encoded Multihash. If I wanted that random
key to link to another object, would that usage suffice, or is there some specific Dag Linking
method?