나의 발자취

[solidity] 머클트리란? 본문

블록체인

[solidity] 머클트리란?

달모드 2020. 11. 21. 22:36

MerkleProofGas.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;

library MerkleProof {
    /**
     * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
     * defined by `root`. For this, a `proof` must be provided, containing
     * sibling hashes on the branch from the leaf to the root of the tree. Each
     * pair of leaves and each pair of pre-images are assumed to be sorted.
     */
    function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
        //leaf: 검증하고싶은 데이터, proof: 검증하는데 필요한 노드, root: root
        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;
    }
}

contract MerkleProofGas{

    bytes32 root;
    bytes32 leaf;
    bytes32[] proof;
    bytes32[] tree;

    constructor() public {

        tree.push(keccak256('a'));
        tree.push(keccak256('b'));
        tree.push(keccak256('c'));
        tree.push(keccak256('d'));

        //hashing(왼쪽값, 오른쪽값)을 하면 keccak(왼쪽값+오른쪽값)
        // tree.push a + b
        tree.push(hashing(tree[0],tree[1]));
        // tree.push c + d
        tree.push(hashing(tree[2],tree[3]));
        // tree.push a + b + c + d -> root
        tree.push(hashing(tree[4],tree[5]));

        root = tree[6]; // a + b + c + d
        leaf = tree[0]; // a

        proof.push(tree[1]); // b
        proof.push(tree[5]); // c + d

    }

    function hashing(bytes32 left, bytes32 right) private pure returns (bytes32) {
        if (left <= right) {
            return keccak256(abi.encodePacked(left, right));
        } else {
            return keccak256(abi.encodePacked(right, left));
        }
    }
    function doVerify() public view returns (bool) {

        return MerkleProof.verify(proof, root, leaf);
    }

}

verifyMerkleProof.js

const MerkleProofGas = artifacts.require('MerkleProofGas');

// require('chai')
//     .should();

// const { expect, assert } = require('chai');

contract('MerkleProofGas', () => {


    beforeEach(async function () {
        this.merkle = await MerkleProofGas.new();
    });

    describe('Should be verified', function() {
        it(' ', async function() {
            const result = await this.merkle.doVerify();
            console.log(result);
            // assert(result == true);
        });

    })
})

'블록체인' 카테고리의 다른 글

학회 마지막 정규세션: Libra Move language 특징  (0) 2020.11.28
Mastering Bitcoin Cp3_Bitcoin Core  (0) 2020.10.08
해커톤 멘토링 내용  (0) 2020.10.06
[Mastering Bitcoin] Cp.2  (0) 2020.10.05
Comments