haseebahmad
haseebahmad

Reputation: 563

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.
  }

}

Upvotes: 1

Views: 543

Answers (2)

turboza
turboza

Reputation: 238

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

Upvotes: 3

Flavio
Flavio

Reputation: 294

You can use the wallet address to validate if the source is valid.

For example:

contract TestSmartContract {

   address sourceAddress = "0x00...";
    
   function testFunction(uint256 id, uint256 claimDate) public returns(uint) {
       require(msg.sender == sourceAddress, "Not valid source");
       //code to execute
   }
}

Upvotes: 1

Related Questions