简体   繁体   English

MekaVerse NFT 智能合约正在使用 ECDSA,但我不明白它是如何工作的

[英]MekaVerse NFT smart contract is using ECDSA, but I don't understand how it works

In the smart contract of MekaVerse I can see these lines to enable a whitelisting, but I don't understand the theory behind it and how I can use it.在 MekaVerse 的智能合约中,我可以看到这些行来启用白名单,但我不明白它背后的理论以及如何使用它。

function mint(uint256[] memory _tokensId, uint256 _timestamp, bytes memory _signature) public payable saleIsOpen {

    uint256 total = totalToken();
    require(_tokensId.length <= 2, "Max limit");
    require(total + _tokensId.length <= MAX_ELEMENTS, "Max limit");
    require(msg.value >= price(_tokensId.length), "Value below price");

    address wallet = _msgSender();

    address signerOwner = signatureWallet(wallet,_tokensId,_timestamp,_signature);
    require(signerOwner == owner(), "Not authorized to mint");

    require(block.timestamp >= _timestamp - 30, "Out of time");

    for(uint8 i = 0; i < _tokensId.length; i++){
        require(rawOwnerOf(_tokensId[i]) == address(0) && _tokensId[i] > 0 && _tokensId[i] <= MAX_ELEMENTS, "Token already minted");
        _mintAnElement(wallet, _tokensId[i]);
    }

}

function signatureWallet(address wallet, uint256[] memory _tokensId, uint256 _timestamp, bytes memory _signature) public view returns (address){

    return ECDSA.recover(keccak256(abi.encode(wallet, _tokensId, _timestamp)), _signature);

}

The interesting part that I don't understand is here:我不明白的有趣部分在这里:

address signerOwner = signatureWallet(wallet,_tokensId,_timestamp,_signature);
require(signerOwner == owner(), "Not authorized to mint")

And here:和这里:

function signatureWallet(address wallet, uint256[] memory _tokensId, uint256 _timestamp, bytes memory _signature) public view returns (address){

return ECDSA.recover(keccak256(abi.encode(wallet, _tokensId, _timestamp)), _signature);

} }

Thank you for your help, Ben谢谢你的帮助,本

The MekaVerse contract uses the OpenZeppelin ECDSA implementation, specifically its recover() function. MekaVerse合约使用 OpenZeppelin ECDSA实现,特别是它的recover() function。 ECDSA stands for "Elliptic Curve Digital Signature Algorithm" and basically, it allows to sign a message using a private key and to check validity of the signature without providing the private key. ECDSA 代表“椭圆曲线数字签名算法”,基本上,它允许使用私钥对消息进行签名,并在不提供私钥的情况下检查签名的有效性。

The recover() function takes 2 arguments in this case: bytes32 (array of 32 bytes) hash of a signed message, and bytes (dynamic-length array of bytes) signature .在这种情况下, recover() function 需要 2 个 arguments: bytes32 (32 字节数组) hash signature消息的bytes (动态长度)数组。 Then it validates whether the hash and signature match according to the ECDSA.然后根据 ECDSA 验证hashsignature是否匹配。 If it does, it returns the signer address.如果是,则返回签名者地址。 If the validation fails, it returns the zero address ( 0x0 ).如果验证失败,则返回零地址 ( 0x0 )。

Note that the signature is a result of signing a message using a private key - but it's not the private key.请注意,签名是使用私钥签署消息的结果 - 但它不是私钥。

You can learn more about signing messages in the web3 documentation of the sign() function.您可以在sign() function 的 web3 文档中了解有关签名消息的更多信息。 If you're interested in the ECDSA (or cryptography in general) in more depth, the wiki page shows some basic information and links to other sources.如果您对 ECDSA(或一般的密码学)有更深入的兴趣, wiki 页面会显示一些基本信息和其他来源的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM