0

I have been trying to do it but it always return me false, tried a lot of ways but I don't know. Here is the smart contract

function _verify(
        bytes32 hashedMessage,
        bytes memory signature
    ) public view returns (bool) {
        require(signature.length == 65, "Invalid Signature");
        bytes32 ethHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hashedMessage));
        (bytes32 r, bytes32 s, uint8 v) = _split(signature);
        return ecrecover(ethHash, v, r, s) == owner;
    }

    function _split(bytes memory signature) internal pure returns (bytes32 r, bytes32 s, uint8 v) {
        assembly {
            r := mload(add(signature, 32))
            s := mload(add(signature, 64))
            v := byte(0, mload(add(signature, 96)))
        }
    }

Here is the typescript code where I am getting the result

    async mintNFT(tokenId: number): Promise<any> {
        const hashedMesage = keccak256(toUtf8Bytes(tokenId.toString()));
        const wallet = new Wallet(process.env.NEXT_PUBLIC_PRIVATE_KEY as string);
        const signature = await wallet.signMessage((hashedMesage));
        return this.sendContract.getFunction('_verify')(hashedMesage, signature)
        .then((data) => {
            console.log(data);
            return data;
        })
        .catch((err) => {
            console.log(err.message);
            return 'error'
        });
    }

1 Answer 1

0

The problem was that you did not need this line

bytes32 ethHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hashedMessage));

because when you sign the message with ethers, it already adds it to the hash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.