Advanced guides
Validating Messages
Iris protocol does provide simple helpers to validate the message received. Listener.sol
in @aurelianoa/iris-channels
does have varoius helpers.
Event Index
Every message does have a eventIndex
to help diferentiate messages within the same network. Just use the compareIndex
helper.
import { Listener } from "@aurelianoa/iris-channels/contracts/listener/Listener.sol";
contract MyListener is Listener {
/// Override the onReceivedMessage function
function onReceivedMessage(address sender, string memory eventIndex, bytes memory data) internal override {
/// define your logic based on the eventIndex received
if(compareIndex(eventIndex, "myEventIndex")) {
/// process the data if theres any
/// your logic here
}
}
}
Validating the sender
Another important aspect is to validate the Sender of the message (Channel Address). Simply register your channel sender (channel address) in your listener using setChannel
and then use the isChannelAvailable
.
Important!
setChannel
is a permissioned external function that needs to run under authorized wallets like the contract owner. To setup this simply override the middleware
function with your preferred security strategy.
import { Listener } from "@aurelianoa/iris-channels/contracts/listener/Listener.sol";
contract MyListener is Listener {
address private owner;
constructor () {
owner = msg.sender;
}
/// override middleware from Listener.sol with your preferred security strategy
/// to access setChannel
function middleware() internal virtual override returns (bool) {
return msg.sender == owner;
}
/// Override the onReceivedMessage function
/// @dev to use isChannelAvailable call setChannel first
function onReceivedMessage(address sender, string memory eventIndex, bytes memory data) internal override {
/// define your logic based on the eventIndex received
if(isChannelAvailable(sender) && compareIndex(eventIndex, "myEventIndex")) {
/// process the data if theres any
/// your logic here
}
}
}
You should know!
Iris protocol does support registering the same listener within different Channels (networks). This is why is important not only validating the message but also the sender (Channel address) registering them using the setChannel
in the actual listener.