logoAcademy

Verify if Sender is Teleporter

Safety verification of the message sender

We can also leverage the registry to check if msg.sender is a registered teleporter contract. Previously, we hardcoded this check in the contract:

import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol";
 
contract ReceiverOnSubnet is ITeleporterReceiver {
    ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
 
    string public lastMessage;
 
    function receiveTeleporterMessage(bytes32, address, bytes calldata message) external {
        // Only the Teleporter receiver can deliver a message.
        require(msg.sender == address(messenger), "ReceiverOnSubnet: unauthorized TeleporterMessenger");
 
        // Store the message.
        lastMessage = abi.decode(message, (string));
    }
}

If there was now a new Teleporter version that would be used for sending messages, we would have to update the contract.

Instead, we can use the registry to check if the sender is a registered Teleporter contract.

pragma solidity ^0.8.18;
 
import "@teleporter/upgrades/TeleporterRegistry.sol";
import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol";
 
contract ReceiverOnDispatchWithRegistry is ITeleporterReceiver {
    // The Teleporter registry contract manages different Teleporter contract versions.
    TeleporterRegistry public immutable teleporterRegistry =
        TeleporterRegistry(0x827364Da64e8f8466c23520d81731e94c8DDe510);
 
    string public lastMessage;
 
    function receiveTeleporterMessage(bytes32, address, bytes calldata message) external {
        // Only a Teleporter Messenger registered in the registry can deliver a message.
        // Function throws an error if msg.sender is not registered.
        teleporterRegistry.getVersionFromAddress(msg.sender);
 
        // Store the message.
        lastMessage = abi.decode(message, (string));
    }
}
Updated:

On this page

No Headings
Edit on Github