0

合同

contract Ex is ERC721{
  bytes32 public merkleRoot;

  function updateMerkleRoot(bytes32 newRoot) external onlyOwner {
    merkleRoot = newRoot;
  }

  function mintWihteList(uint256 _count, bytes32[] calldata merkleProof) public payable {
    bytes32 node = keccak256(abi.encodePacked(msg.sender));
    require(
        MerkleProof.verify(merkleProof, merkleRoot, node), 
        "MerkleDistributor: Invalid proof."
    );
  }
}

setRoot.js

const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');
const whiteList = [
'myAddress1',
'myAddress2'
]

const leafNodes = whiteList.map(addr => keccak256(addr));
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs:true });
const rootHash = merkleTree.getRoot();
myContract.methods.updateMerkleRoot(rootHash).send({
  from: 'myaddress',
  gas: 20000000, // transaction gas limit
});

到目前为止,它按预期工作。但我不知道如何在网上proof打电话mintWihteList

网络

whiteListmint = (count) => {
  const whiteList = [
    'myAddress1',
    'myAddress2'
  ]

  const leafNodes = whiteList.map(addr => keccak256(addr));
  const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs:true });
  const proof = merkleTree.getProof(keccak256(this.state.account));

  const data = web3.eth.abi.encodeFunctionCall(
  {
    name: 'mintPreSaleNFT',
    type: 'function',
    inputs: [
      {
        type: 'uint256',
        name: '_count'
      },
      {
        type: 'bytes32[]',
        name: 'merkleProof'
      }
    ]
  },
  [count,proof]
  )
  contractSend();
}

我试图做MerkleTree的是采用相同的方式并获得proof. 但我不认为这种方式很好。

是否有proof从 acontract或其他地方获取 a 的标准方法?

4

0 回答 0