ECDSA: invalid signature 'v' value

I am learning solidity. I am trying to verify the signature of an address using Openzepplin ECDSA lib. I just can not make it work. I even look a the ECDSA test at:

:1234: Code to reproduce


contract MonitoringCenter{
......

    /**
     * @notice Verify an address
     * @param message signed message
     * @param signature signature
     * @param account address for verification
     */
    function _verify(
        bytes32 message,
        bytes memory signature,
        address account
    ) internal pure returns (bool) {
        return
            keccak256(abi.encode(message)).toEthSignedMessageHash().recover(signature) ==
            account;
    }

    /**
     * @notice Verify an address
     * @param signature signature
     */
    function register(bytes memory signature) public returns (bool) {
        require(_verify("register", signature, msg.sender) == true);
        _setupRole(USER_ROLE, msg.sender);
        return true;
    }
}

//test.js

    it('Test register', async function () {
        // Hashing first makes a few things easier.
        accounts = await web3.eth.getAccounts();
        console.log(accounts);
        console.log(web3.eth.providers);
        var message = "register";
        var signature = await web3.eth.sign(message, accounts[0]);
        console.log(signature);
        let result = await this.monitoringCenter.register(signature, { from: accounts[0] });
        expect(result).to.be.true;
    })

:computer: Environment

Truffle v5.4.10 (core: 5.4.10)
Solidity - 0.8.4 (solc-js)
Node v16.9.1
Web3.js v1.5.2

I am connecting to ganache. web3 works just fine. but I am getting
Error: Returned error: VM Exception while processing transaction: revert ECDSA: invalid signature 'v' value -- Reason given: ECDSA: invalid signature 'v' value.

What I am doing wrong?

2 Likes