无法以坚固性恢复签名的消息

发布于 2025-02-03 04:13:49 字数 1151 浏览 1 评论 0原文

我正在签署一条消息,并试图通过固体(链)恢复它。不知何故,签名地址永远不会匹配。

Off-Chain Code

var message = 'Hello World';
var privateKey = '0xXXXXXXXX'; //Private key of the my using to sign message
var signatureMessage = web3.eth.accounts.sign(message, privateKey);

这给了我一个签名值,但是,当我使用相同的签名和牢固性的签名和消息来恢复歌手的公共地址时,我会得到不同的地址。

固体性

function publicSaleBuy(bytes calldata signature, string memory message) external payable
    {
        if (!matchAddresSigner(hashTransaction(message), signature)){
            revert DirectMintFromNotAllowed();
        }else {
            _mint(1);
        }

    }

    function matchAddresSigner(bytes32 hash, bytes memory signature)
        private
        view
        returns (bool)
    {
        return signerAddress == hash.recover(signature);
    }

    function hashTransaction(string memory message) public pure returns (bytes32) {
        bytes32 messageHash = keccak256(abi.encodePacked(message));
        bytes32 hash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", messageHash));        
        return hash;
    }

I am signing a message off-chain and trying to recover this via solidity(on-chain). Somehow, the signer address never matches.

Off-chain code

var message = 'Hello World';
var privateKey = '0xXXXXXXXX'; //Private key of the my using to sign message
var signatureMessage = web3.eth.accounts.sign(message, privateKey);

This gives me a signature value, however, when I am using the same signature and message on solidity to recover the singer's public address, I am getting a different address.

Solidity

function publicSaleBuy(bytes calldata signature, string memory message) external payable
    {
        if (!matchAddresSigner(hashTransaction(message), signature)){
            revert DirectMintFromNotAllowed();
        }else {
            _mint(1);
        }

    }

    function matchAddresSigner(bytes32 hash, bytes memory signature)
        private
        view
        returns (bool)
    {
        return signerAddress == hash.recover(signature);
    }

    function hashTransaction(string memory message) public pure returns (bytes32) {
        bytes32 messageHash = keccak256(abi.encodePacked(message));
        bytes32 hash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", messageHash));        
        return hash;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文