MerkleTree.js Solidity example – 梅克尔树.js坚固性示例区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,MerkleTree.js Solidity example – 梅克尔树.js坚固性示例区块链毕设代写 是一篇很好的国外资料

MerkleTree.js Solidity example

Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.

Example

contracts/MerkleProof.sol

pragma solidity ^0.5.2;  contract MerkleProof {   function verify(     bytes32 root,     bytes32 leaf,     bytes32[] memory proof   )     public     pure     returns (bool)   {     bytes32 computedHash = leaf;      for (uint256 i = 0; i < proof.length; i++) {       bytes32 proofElement = proof[i];        if (computedHash < proofElement) {         // Hash(current computed hash + current element of the proof)         computedHash = keccak256(abi.encodePacked(computedHash, proofElement));       } else {         // Hash(current element of the proof + current computed hash)         computedHash = keccak256(abi.encodePacked(proofElement, computedHash));       }     }      // Check if the computed hash (root) is equal to the provided root     return computedHash == root;   } }

test/merkleproof.js

const MerkleProof = artifacts.require('MerkleProof') const MerkleTree = require('merkletreejs') const keccak256 = require('keccak256')  const contract = await MerkleProof.new()  const leaves = ['a', 'b', 'c', 'd'].map(v => keccak256(v)) const tree = new MerkleTree(leaves, keccak256, { sort: true }) const root = tree.getHexRoot() const leaf = keccak256('a') const proof = tree.getHexProof(leaf) console.log(await contract.verify.call(root, leaf, proof)) // true  const badLeaves = ['a', 'b', 'x', 'd'].map(v => keccak256(v)) const badTree = new MerkleTree(badLeaves, keccak256, { sort: true }) const badProof = badTree.getHexProof(leaf) console.log(await contract.verify.call(root, leaf, badProof)) // false

Test

make test

License

MIT


梅克尔树.js稳定性示例

用梅克尔树.js并验证merkle证据的可靠性。

示例

合同/梅克尔防病毒.sol

pragma solidity ^0.5.2;  contract MerkleProof {   function verify(     bytes32 root,     bytes32 leaf,     bytes32[] memory proof   )     public     pure     returns (bool)   {     bytes32 computedHash = leaf;      for (uint256 i = 0; i < proof.length; i++) {       bytes32 proofElement = proof[i];        if (computedHash < proofElement) {         // Hash(current computed hash + current element of the proof)         computedHash = keccak256(abi.encodePacked(computedHash, proofElement));       } else {         // Hash(current element of the proof + current computed hash)         computedHash = keccak256(abi.encodePacked(proofElement, computedHash));       }     }      // Check if the computed hash (root) is equal to the provided root     return computedHash == root;   } }

测试/merkleproof.js

const MerkleProof = artifacts.require('MerkleProof') const MerkleTree = require('merkletreejs') const keccak256 = require('keccak256')  const contract = await MerkleProof.new()  const leaves = ['a', 'b', 'c', 'd'].map(v => keccak256(v)) const tree = new MerkleTree(leaves, keccak256, { sort: true }) const root = tree.getHexRoot() const leaf = keccak256('a') const proof = tree.getHexProof(leaf) console.log(await contract.verify.call(root, leaf, proof)) // true  const badLeaves = ['a', 'b', 'x', 'd'].map(v => keccak256(v)) const badTree = new MerkleTree(badLeaves, keccak256, { sort: true }) const badProof = badTree.getHexProof(leaf) console.log(await contract.verify.call(root, leaf, badProof)) // false

测试

make test

许可证

麻省理工学院

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/16391.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 !
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » MerkleTree.js Solidity example – 梅克尔树.js坚固性示例区块链毕设代写

提供最优质的资源集合

立即查看 了解详情