1

我正在尝试在节点中使用 merkletreejs 来创建一个 hexproof - 使用它可以正常工作,但我还需要它能够为多个叶子生成一个证明。

我当前的js代码:

const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');

let tokenIDs = [
    "6719509505",
    "6231137281",
    "3766014977",
    "4955017921",
    "8583738032",
  ];

const leafNodes = tokenIDs.map(ids => keccak256(ids));
const merkleTree = new MerkleTree(leafNodes, keccak256, {sortLeaves: true, sortPairs: true});
const rootHash = merkleTree.getHexRoot();

// CLAIMING HERE
const claimingToken = keccak256("6719509505","6231137281","8583738032");

const hexProof = merkleTree.getHexProof(claimingToken);

console.log(merkleTree.verify(hexProof, claimingToken, rootHash));

但是,当我尝试这个时,它给了我同样的证据,就好像我只使用了第一个令牌 ID。基本上忽略了我输入的其他 ID。

而且它给了我无效的证明错误:

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";

contract merkle {

    bytes32 private merkleRoot;

    event leaf (
      bytes32 leaf2
    );

    event ids (
      uint256[] tokenID
    );

    event merk (
      bytes32[] _merkleProof
    );

    function mint(bytes32[] calldata _merkleProof, uint256[] calldata tokenID) public {
        bytes32 leaf2 = keccak256(abi.encodePacked(tokenID));
        emit ids(tokenID);
        emit leaf(leaf2);
        emit merk(_merkleProof);

        // Check for an invalid proof
       require(MerkleProof.verify(_merkleProof, merkleRoot, leaf2), "Invalid Merkle Proof.");
    }
}

关于如何使用多个叶子生成一个默克尔证明的任何想法?

编辑

有人告诉我 merkle 树一次只能验证一片叶子,所以现在我正试图弄清楚如何发送多个证明并可靠地验证每一个。

在 js 中,我创建了一个带有证明的示例数组 -

let tokenMerkle = [];

tokenMerkle.push({tokenId: "6719509505", merkleProof: proof1});
tokenMerkle.push({tokenId: "6231137281", merkleProof: proof2});
tokenMerkle.push({tokenId: "6231137281", merkleProof: proof3});

我将其与我的合同一起发送。不确定如何通过使用数组数组正确验证每一个 -

struct Proof {
    uint tokenID; 
    bytes32[] _merkleProof;
    }

function mint(Proof[] memory proofs) public {   
 for (uint i; i< proofs.length;i++){

    bytes32 leafs = keccak256(abi.encodePacked(proofs[i].tokenID));
    
    require(MerkleProof.verify(proofs[i]._merkleProof, merkleRoot, leafs), "Invalid Merkle Proof.");
    }
}
4

0 回答 0