0

A dapp can sign a message with the web3.js:

// recipient is the address that should be paid.
// amount, in wei, specifies how much ether should be sent.
// nonce can be any unique number, used to prevent replay attacks.
// contractAddress is used to prevent cross-contract replay attacks.
function signPayment(recipient, amount, nonce, contractAddress, callback) {
  var hash = "0x" + ethereumjs.ABI.soliditySHA3(
    ["address", "uint256", "uint256", "address"],
    [recipient, amount, nonce, contractAddress]
  ).toString("hex");

  web3.personal.sign(hash, web3.eth.defaultAccount, callback);
}

The signed message can be sent when calling function in smart contract and there is built-in solidity function ecrecover which can recover the address used to sign the message. How does the smart contract recover other information such as recipient, amount, nonce, contractAddress in the signed message for further verification? For example, the smart contract can compare his own address with the contractAddress in signed message to make sure the message was indeed for him.

1

You can only recover the address from a signature.

Contracts usually receive parameters, construct the message, hash it and then recover the signature from the hashed message. That way you are sure the parameters were used to obtain the signature.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.