我正在尝试使用 merkletreejs 库测试 merkle 证明,但我不明白为什么它有效
\nconst tree = new MerkleTree(leaves, SHA256)\nconst root = tree.getHexRoot()\nconst leaf = SHA256('a')\nconst proof = tree.getProof(leaf) // \xd0\xa0\xd0\x90\xd0\x91\xd0\x9e\xd0\xa2\xd0\x90\xd0\x95\xd0\xa2\nconsole.log(tree.verify(proof, leaf, root)) // true\nRun Code Online (Sandbox Code Playgroud)\n但这不是吗?
\nconst tree = new MerkleTree(leaves, SHA256)\nconst root = tree.getHexRoot()\nconst leaf = SHA256('a')\nconst proof = tree.getHexProof(leaf)\nconsole.log(tree.verify(proof, leaf, root)) // false\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
看来它需要一些额外的代码才能工作。这段代码的工作原理:
const { MerkleTree } = require('merkletreejs')
const sha1 = require('crypto-js/sha1')
const leaves = [
'd89f84d948796605a413e196f40bce1d6294175d',
'32f04c7f572bf75a266268c6f4d8c92731dc3b7f',
'b80b52d80f5fe940ac2c987044bc439e4218ac94',
'1553c75a1d637961827f4904a0955e57915d8310'
]
const tree = new MerkleTree(leaves, sha1, {
sortLeaves: true,
sortPairs: true
})
const root = tree.getHexRoot()
const leaf = 'b80b52d80f5fe940ac2c987044bc439e4218ac94'
const proof = tree.getHexProof(leaf)
console.log(tree.verify(proof, leaf, root))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1581 次 |
| 最近记录: |