无节点方式生成匹配IPFS-Desktop CID的CID
Node-less way to generate a CID that matches IPFS-Desktop CID
我想为 javascript 中的文件生成 CID(内容标识符),而无需访问 IPFS 节点或互联网。我试过使用 js-multihashing-async to first hash the file and js-cid to generate a CID from the hash but I get a different CID than if I just add the file to ipfs-desktop. It looks like the problem is an IPFS node chunks data and the CID is for the DAG that links the files' chunks. I've tried this library but it doesn't produce the same CID as ipfs-desktop does for the same file. 问题与我的基本相同,但是 none 的答案给出了一个与 ipfs-desktop-generated CID 相匹配的 CID。
ipfs-only-hash
是用于从文件或缓冲区创建 IPFS CID 的正确模块,无需启动 IPFS 守护程序。对于相同的输入文件和相同的选项,它应该产生相同的 CID。
此示例来自 ipfs-only-hash
测试,它验证了它是否将相同的缓冲区散列到与 js-ipfs 节点相同的 CID。
test('should produce the same hash as IPFS', async t => {
const data = Buffer.from('TEST' + Date.now())
const ipfs = new Ipfs({ repo: path.join(os.tmpdir(), `${Date.now()}`) })
await new Promise((resolve, reject) => {
ipfs.on('ready', resolve).on('error', reject)
})
const files = await ipfs.add(data)
const hash = await Hash.of(data)
t.is(files[0].hash, hash)
})
我是 IPFS 桌面的维护者之一,在幕后,该应用程序在 http api 上为本地 IPFS 守护进程调用 ipfs.add
here
当通过 api 手动添加或散列文件时,有一些选项可以改变文件分块到块的方式、这些块如何链接在一起以及散列块的方式。如果任何选项值不同,则生成的散列和包含它的 CID 将不同,即使输入文件相同。
您可以尝试这些选项并在此处查看生成的 DAG(有向无环图)结构的可视化:https://dag.ipfs.io/
要深入了解 IPFS 如何分块和散列文件,您可以查看 ipfs-only-hash
的作者和 js-ipfs
的维护者,在此处进行解释 https://www.youtube.com/watch?v=Z5zNPwMDYGg
为了后代,下面是如何将通过获取下载的图像的 CID 与从 ipfs-desktop 为同一图像(从本地驱动器添加为文件)生成的 CID 进行匹配。您必须删除图像的 base64string 前面的前缀 data:*/*;base64,
并将字符串解码为缓冲区数组。然后你得到匹配的 CID。
async testHashes() {
const url = "https://raw.githubusercontent.com/IanPhilips/jst-cids-test/master/src/23196210.jpg";
fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob)
})).then(async dataUrl =>{
const strData = dataUrl as string;
// remove "data:*/*;base64," from dataUrl
const endOfPrefix = strData.indexOf(",");
const cleanStrData = strData.slice(endOfPrefix+1);
const data = Buffer.from(cleanStrData, "base64");
const hash = await Hash.of(data);
console.log("fetch data CID: " + hash); // QmYHzA8euDgUpNy3fh7JRwpPwt6jCgF35YTutYkyGGyr8f
});
console.log("ipfs-desktop CID: QmYHzA8euDgUpNy3fh7JRwpPwt6jCgF35YTutYkyGGyr8f");
}
我想为 javascript 中的文件生成 CID(内容标识符),而无需访问 IPFS 节点或互联网。我试过使用 js-multihashing-async to first hash the file and js-cid to generate a CID from the hash but I get a different CID than if I just add the file to ipfs-desktop. It looks like the problem is an IPFS node chunks data and the CID is for the DAG that links the files' chunks. I've tried this library but it doesn't produce the same CID as ipfs-desktop does for the same file.
ipfs-only-hash
是用于从文件或缓冲区创建 IPFS CID 的正确模块,无需启动 IPFS 守护程序。对于相同的输入文件和相同的选项,它应该产生相同的 CID。
此示例来自 ipfs-only-hash
测试,它验证了它是否将相同的缓冲区散列到与 js-ipfs 节点相同的 CID。
test('should produce the same hash as IPFS', async t => {
const data = Buffer.from('TEST' + Date.now())
const ipfs = new Ipfs({ repo: path.join(os.tmpdir(), `${Date.now()}`) })
await new Promise((resolve, reject) => {
ipfs.on('ready', resolve).on('error', reject)
})
const files = await ipfs.add(data)
const hash = await Hash.of(data)
t.is(files[0].hash, hash)
})
我是 IPFS 桌面的维护者之一,在幕后,该应用程序在 http api 上为本地 IPFS 守护进程调用 ipfs.add
here
当通过 api 手动添加或散列文件时,有一些选项可以改变文件分块到块的方式、这些块如何链接在一起以及散列块的方式。如果任何选项值不同,则生成的散列和包含它的 CID 将不同,即使输入文件相同。
您可以尝试这些选项并在此处查看生成的 DAG(有向无环图)结构的可视化:https://dag.ipfs.io/
要深入了解 IPFS 如何分块和散列文件,您可以查看 ipfs-only-hash
的作者和 js-ipfs
的维护者,在此处进行解释 https://www.youtube.com/watch?v=Z5zNPwMDYGg
为了后代,下面是如何将通过获取下载的图像的 CID 与从 ipfs-desktop 为同一图像(从本地驱动器添加为文件)生成的 CID 进行匹配。您必须删除图像的 base64string 前面的前缀 data:*/*;base64,
并将字符串解码为缓冲区数组。然后你得到匹配的 CID。
async testHashes() {
const url = "https://raw.githubusercontent.com/IanPhilips/jst-cids-test/master/src/23196210.jpg";
fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob)
})).then(async dataUrl =>{
const strData = dataUrl as string;
// remove "data:*/*;base64," from dataUrl
const endOfPrefix = strData.indexOf(",");
const cleanStrData = strData.slice(endOfPrefix+1);
const data = Buffer.from(cleanStrData, "base64");
const hash = await Hash.of(data);
console.log("fetch data CID: " + hash); // QmYHzA8euDgUpNy3fh7JRwpPwt6jCgF35YTutYkyGGyr8f
});
console.log("ipfs-desktop CID: QmYHzA8euDgUpNy3fh7JRwpPwt6jCgF35YTutYkyGGyr8f");
}