如何在IPFS中重新创建多哈希的哈希摘要



假设我像这样向IPFS添加数据:

$ echo Hello World | ipfs add

这将给我QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u——一个CID,它是Base58编码的多散列。

将其转换为Base16,告诉我IPFS添加的哈希摘要是SHA2-256哈希:

12 - 20 - 74410577111096cd817a3faed78630f2245636beded412d3b212a2e09ba593ca
<hash-type> - <hash-length> - <hash-digest>

我知道IPFS不仅仅是对数据进行散列,而是首先将其序列化为Unixfs protobuf,然后将其放入dag中。

我想解开如何访问74410577111096cd817a3faed78630f2245636beded412d3b212a2e09ba593ca的神秘面纱,但我真的不确定如何获得创建的dag,该dag包含Unixfs protobuf和数据。

例如,我可以将序列化的原始数据写入磁盘,并使用protobuf解码器进行检查:

$ ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u > /tmp/block.raw
$ protoc --decode_raw < /tmp/block.raw

这将为我提供可读格式的序列化数据:

1 {
1: 2
2: "Hello Worldn"
3: 12
}

然而,通过SHA-256进行管道传输仍然会给我一个不同的散列,这是有意义的,因为IPFS将protobuf放在dag中,并对其进行多重散列。

$ protoc --decode_raw < /tmp/block.raw | shasum -a 256

所以我决定弄清楚如何获得dag节点,自己对其进行散列,以获得我想要的散列。

我希望使用ipfs dag get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u能给我一个可以解码的多散列,但事实证明它返回了一些我不知道如何检查的其他数据散列:

$ ipfs dag get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
$ {"data":"CAISDEhlbGxvIFdvcmxkChgM","links":[]}

关于如何从这里解码data,有什么想法吗?

更新

data是原始数据的Base64表示:https://github.com/ipfs/go-ipfs/issues/4115

您要查找的哈希是ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u输出的哈希。IPFS对编码的值进行散列。

代替运行:

protoc --decode_raw < /tmp/block.raw | shasum -a 256

刚刚运行:

shasum -a 256 < /tmp/block.raw

但它返回了一些其他数据哈希,我不知道如何检查

这是因为我们目前在protobuf内部使用protobuf。外原体结构为{Data: DATA, Links: [{Name: ..., Size: ..., Hash: ...}]}

In:

1 {
1: 2
2: "Hello Worldn"
3: 12
}

1 { ... }部分是外质子束的数据场。但是,protoc --decode_raw *recursively* decodes this object so it decodes the数据字段改为:

  • 字段1(数据类型(:2(文件(
  • 字段2(数据(:"Hello World\n">
  • 字段3(文件大小(:12(字节(

对于上下文,相关的protobuf定义为:

外部:

// An IPFS MerkleDAG Link
message PBLink {
// multihash of the target object
optional bytes Hash = 1;
// utf string name. should be unique per object
optional string Name = 2;
// cumulative size of target object
optional uint64 Tsize = 3;
}
// An IPFS MerkleDAG Node
message PBNode {
// refs to other objects
repeated PBLink Links = 2;
// opaque user data
optional bytes Data = 1;
}

内部:

message Data {
enum DataType {
Raw = 0;
Directory = 1;
File = 2;
Metadata = 3;
Symlink = 4;
HAMTShard = 5;
}
required DataType Type = 1;
optional bytes Data = 2;
optional uint64 filesize = 3;
repeated uint64 blocksizes = 4;
optional uint64 hashType = 5;
optional uint64 fanout = 6;
}
message Metadata {
optional string MimeType = 1;
}

我不确定编码是什么,但你可以在js-ipfs:中这样解组dag数据字段

const IPFS = require('ipfs')
const Unixfs = require('ipfs-unixfs')
const ipfs = new IPFS
ipfs.dag.get('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u', (err, d) => {
console.log(Unixfs.unmarshal(d.value.data).data.toString()))
// prints Hello World
})

根据Steven的回答,使用protobuf是可行的。以下是我的方法的完整代码。

ipfs.proto

syntax = "proto3";
message PBNode {
bytes Data = 1;
}
message PBLink {
bytes Hash = 1;
string Name = 2;
uint64 Tsize = 3;
}
message Data {
enum DataType {
Raw = 0;
Directory = 1;
File = 2;
Metadata = 3;
Symlink = 4;
HAMTShard = 5;
}
DataType Type = 1;
bytes Data = 2;
}

cid.js

const mh = require('multihashes');
const axios = require('axios');
const crypto = require('crypto');
const protobuf = require("protobufjs");
const IPFS = protobuf.loadSync('./ipfs.proto').lookupType('PBNode');
class CID {
/**
* convert IPFS multihash to sha2-256 hash string
* @param {string} multihash
* @param {boolean} prefix
* @returns {string} sha2-256 hash string starting with 0x
*/
static toHash(multihash, prefix = false) {
return prefix ? '0x' : ''
+ mh.decode(mh.fromB58String(multihash)).digest.toString('hex')
}
/**
* convert sha2-256 hash string to IPFS multihash
* @param {string} str
* @returns {string} IPFS multihash starting with Qm
*/
static fromHash(str) {
str = str.startsWith('0x') ? str.slice(2) : str;
return mh.toB58String(mh.encode(Buffer.from(str, 'hex'), 'sha2-256'))
}
/**
* hash the buffer and get the SHA256 result compatible with IPFS multihash
* @param {Buffer} buf
* @returns {string}
*/
static hash(buf) {
const r = IPFS.encode({
Data: {
Type: 2,
Data: buf,
filesize: buf.length
}
}).finish();
return crypto.createHash('sha256').update(r).digest('hex');
}
}
async function ipfsGet(cid) {
const x = await axios.get(`http://your.address.xxx/ipfs/${cid}`, {
responseType: 'arraybuffer'
});
return Buffer.from(x.data);
}
const r = "QmfQj4DUWEudeFdWKVzPaTbYimdYzsp14DZX1VLV1BbtdN";
const hashFromCID = CID.toHash(r);
console.log(hashFromCID);
ipfsGet(r).then(buf => {
const hashCalculated = CID.hash(buf);
console.log(hashCalculated);
console.log(hashCalculated === hashFromCID);
console.log(CID.fromHash(hashCalculated) === r)
});
module.exports = CID;

我为Java创建了一个模块,它可以生成正确的文件哈希:https://github.com/rmnvalera/java-cid-generate-hash

最新更新