3

I'm reading a few examples from solidity docs: docs. I've come across this function:

function claimPayment(uint256 amount, uint256 nonce, bytes memory signature) external {
        require(!usedNonces[nonce]);
        usedNonces[nonce] = true;

        // this recreates the message that was signed on the client
        bytes32 message = prefixed(keccak256(abi.encodePacked(msg.sender, amount, nonce, this)));

        require(recoverSigner(message, signature) == owner);

        payable(msg.sender).transfer(amount);
    }

My question is: how do I handle the nonce from my frontend? Is it a random number that I generate using Math.random() valid? Or does ethers.js handle this? If so, what do I have to pass as argument when I call this contract function?

Thanks.

EDIT#1: fixed typo

1 Answer 1

4

In general, there are two types of accounts in the ethereum. An EOA and contract accounts.

EOA are wallets and contract accounts are the accounts that used to store the contracts.

When it comes to EOA, the nonce is equal to the number of transaction sent by an EOA account.

And for the contract accounts, the nonce is equal to the number of contracts saved by the contract accounts.

In ethereum blockchain, an EOA account cannot sign two transaction with the same nonce which is to prevent replay attacks.

It is mentioned in the link you have shared.

The contract in the solidity docs is just for reference on how to verify signatures. In reality, they are just trying to explain what and how can we verify signature through ecrecover in the solidity.

If you want to provide the unique nonce, you can write a unique number generator off-chain and provide it from the front-end.

Tell me if it helps!

0

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.