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;
}