5

It’s a simple question, but I couldn’t find an answer. In an ERC20 contract lies this code

//The nonce for avoid transfer replay attacks
mapping(address => uint256) nonces;

.

    uint256 nonce = nonces[_address_from];
    bytes32 h = keccak256(_address_from ,_address_to ,_token_amount, _amount_fee ,nonce);
    if(_address_from != ecrecover(h,_v,_r,_s)) revert();

_r _s _v are functions parameters which must be set manually when calling it on contract execution.

How do I compute _r _s _v from my address and amounts in order to not get the transaction rejected ?

1

2 Answers 2

8

r, s and v are obtained from the signature of the transaction. if you have the signature (assume it is in a variable called sig) you can do:

r = sig.signature.slice(0, 32)
s = sig.signature.slice(32, 64)
v = sig.recovery + 27

So if you want to check that a message was signed by an account you need to pass the r,s and v values to your contract. Notice that when you send transactions to the network, the network do the verification automatically.

The signature can be obtained with web3, see here

Also for a complete discussion see this answer

Hope this help.

7
  • How do I compute transaction signature? Commented May 23, 2018 at 13:05
  • The link in the answer shows how to do it using web3.
    – Jaime
    Commented May 23, 2018 at 13:22
  • No sorry. I mean how do I compute the called signature requiring for computing ecrecover parameters. Commented May 23, 2018 at 14:57
  • Sorry, I do not understand. Maybe a bit of context in the question, indicating what you are doing, will make easy for me to provide more help. Let me know.
    – Jaime
    Commented May 23, 2018 at 16:53
  • ethereum.stackexchange.com/q/49299/25002 I now found the function. The problem is keccak256 and the nonce. Commented May 23, 2018 at 17:09
-1

In the EVM assembly language (For solidity) -

        assembly {
            r := mload(add(signature, 0x20))
            s := mload(add(signature, 0x40))
            v := byte(0, mload(add(signature, 0x60)))
        }
1
  • This is for verifying the signature… My question is how to compute it using the private key. Commented Sep 29 at 11:54

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.