Receiving external messages
This tutorial explains how exactly a contract can accept external messages.
verifyExternal()
The contract receiving an external message must implement an additional function so that this call is processed. Unless this function is present, a contract cannot accept external messages. The function signature:
function validateSignature(
bytes memory pubkey,
uint256 hash,
bytes memory signature
) internal view returns (bool)
The purpose of the function is to limit who can call the receiver contract via an external message. Its body can hold any logic for checking the message signature.
As the receiving contract is charged with paying for external messages, the verifyExternal()
function is needed so that external parties do not accidentally (or maliciously) drain the balance of the receiving contract.
verifyExternal()
is executed every time a contract is called via an external message.
Example
This example contains a simple mechanism for verifying authData
:
pragma solidity ^0.8.9;
import "@nilfoundation/smart-contracts/contracts/Nil.sol";
contract Receiver {
bytes pubkey;
constructor(bytes memory _pubkey) payable {
pubkey = _pubkey;
}
function verifyExternal(
uint256 hash,
bytes memory authData
) external view returns (bool) {
return Nil.validateSignature(pubkey, hash, authData);
}
}