Receiving external transactions
This tutorial explains how exactly a contract can accept external transactions.
verifyExternal()
The contract receiving an external transaction must implement an additional function so that this call is processed. Unless this function is present, a contract cannot accept external transactions. 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 transaction. Its body can hold any logic for checking the transaction signature.
As the receiving contract is charged with paying for external transactions, 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 transaction.
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);
}
}