Search code examples
node.jssoliditydigital-signaturesmartcontractsweb3js

How to verify nodejs signature in Ethereum solidity?


I create a signature use 'crypto'

let privateKey = loadKey("./rsa-prv.pem");
let publicKey = loadKey("./rsa-pub.pem");
let message = "Hello, world!";


let sign = crypto.createSign("RSA-SHA256");
sign.update(message);
let signature = sign.sign({key: privateKey, passphrase: "123"}, "hex");

have seen many algorithms to verify the signature, but they can't solve my problem, buacuse the signature generation in the IoT devices.



function verify(address _signer, string memory _message, bytes memory _sig) external pure returns (bool)
{
    bytes32 messageHash = getMessageHash(_message);
    bytes32 ethSignedMessageHash = getEthSignedMessageHash(messageHash);
    return recover(ethSignedMessageHash, _sig) == _signer;
}



How do nodejs signatures match smart contract validation?

Solution

  • Your Node.js script uses a different algorithm for signing (RSA) than the EVM verifier (ECDSA).

    EVM currently supports veryfying messages only signed by ECDSA, so you'll need to use the same in your JS script.

    Note that as per EIP-191, EVM expects the signed message to be prefixed with "\x19Ethereum Signed Message:\n" + <message_byte_length>

    You can use for example the signer.signMessage() function from ethers libary.