How to create an IPFS compatible multihash

前端 未结 5 1865
天命终不由人
天命终不由人 2020-12-30 07:00

I\'m trying to create an IPFS compatible mutihash but it is not matching. I am asking here because I have not yet found an example that takes this from hashing to the end r

5条回答
  •  庸人自扰
    2020-12-30 07:41

    Some of the other answers are outdated. This is what worked for me:

        const Unixfs = require('ipfs-unixfs'); // @0.1.16
        const { DAGNode } = require('ipld-dag-pb'); // @0.18.1
        const bs58 = require('bs58'); // @4.0.1
    
        // ...
    
        const data = Buffer.from('hello world');
        const unixFs = new Unixfs('file', data)
        const dagNode = new DAGNode(unixFs.marshal());
        const link = await dagNode.toDAGLink();
        const multihash = bs58.encode(link._cid.multihash).toString();
        console.log(multihash); // Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD
    

提交回复
热议问题