Search code examples
ethereumsoliditysmartcontractsencryption-asymmetricether

Is there a way to pass verified parameters to smart contract function in Solidity?


I want to call a function named "testFunction" which takes "claimDate" as a parameter. claimDate parameter comes from a server via api.

contract TestSmartContract {

  testFunction(uint256 id, uint256 claimDate) public payable returns (uint) {
    // I need a logic to verify if claimDate is coming from a valid source which will mean it is correct.
  }

}

Solution

  • In short, you can use signature verification to check if the data comes from the verified source.

    The idea is that;

    1. Let the source sign the claimDate from off-chain (ex. backend server).
    2. Send the claimDate together with the signature to the contract.
    3. The contract which stores source address verifies if the signature is correct. If yes, it means the claimDate is really from the source.

    ⚠️ Note that signing claimDate alone might not be enough. Because once the signature is created, it can be visible and reused by exploiters.

    Read more: https://blog.chainsafe.io/how-to-verify-a-signed-message-in-solidity-6b3100277424