ガスのコストを節約するために、マークル ツリーを使用して NFT のホワイトリストを作成しようとしています。ここで javascript で素晴らしい実装を見ましたが、Python でやりたいと思います。ケッカックハッシュを使用してマークルツリーを作成できるようには見えません。これは、イーサリアムブロックチェーンで行う必要があると思います。Open-zeppelin のMerkleProof.solは、kacack ハッシュを使用してリーフを検証します。代わりに sha256 を使用するようにこれを変更できると思いますが、これらの簡単な修正は好きではありません。
これが私が以下で試したことです:
from pymerkle import MerkleTree
from Crypto.Hash import keccak
from scripts.helpful_scripts import get_account
from brownie import NFTCollectible, network, config
def hash(address):
k = keccak.new(digest_bits=256)
k.update(address.encode("utf_8"))
return k.hexdigest()
def main():
account = get_account()
nft_collectible = NFTCollectible[-1]
whitelistedAddresses = [
"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
"0x78D6CF3DEF45AF458442E787FE6E64A03750AB94",
"0xA7B0E4CF55AF900F6E80D619AE1E00965044BC4E",
"0x38C4278F42A26A588176017F5E4F6ED831835EA2",
"0x9F9A0AA0507FE79CA1FF8F26A8D84BD8BB0EC9DF",
"0xDABF258F5619942D19AD87C3472FABE893B26D7D",
"0xD30ED9C457C7D32F3F7B949964191E4084C53F66",
]
leafNodes = []
for address in whitelistedAddresses:
hashedAddress = hash(address)
leafNodes.append(hashedAddress)
merkleTree = MerkleTree()
for leaf in leafNodes:
merkleTree.update(leaf)
print(merkleTree)
root = merkleTree.rootHash.decode("utf-8")
print(root)
Merkle ツリーを返しますが、pymerkle には keccak ハッシュがないため、sha256 を使用してハッシュされます。(上記の keccak ライブラリは、ウォレットのハッシュに使用されます)。これを実行すると、JS のものとは異なるマークル ツリーが返されます。違いがハッシュによるものかどうかさえわかりません。
これは、私が堅実さを確認したい方法です:
// using merkle tree for whitelist
bytes32 leaf = keccak256(abi.encodePacked(msg.sender)); // this will work good since the wallets are hashed by keccak
require(
MerkleProof.verify(merkleProof, whitelistedMerkleRoot, leaf), // this will fail
"Not on the whitelist"
);