xdapp-book/src/development/messages/receiving/evm.md

29 lines
1.3 KiB
Markdown
Raw Normal View History

2022-09-26 18:19:24 -07:00
# Receiving Messages on EVM
2022-06-17 06:54:29 -07:00
To receive messages in EVM, first we need to implement a function `receiveMsg`. The name of this function is arbitrary, so any function name will work as long as your relayer knows what to call on the application contract when submitting messages.
2022-06-17 06:54:29 -07:00
2022-07-01 06:29:03 -07:00
This function will take in an encoded set of `bytes` as the VAA and then call the `parseAndVerifyVM` function on the Core Bridge to validate the message.
2022-06-17 06:54:29 -07:00
```solidity
function receiveEncodedMsg(bytes memory encodedMsg) public {
(IWormhole.VM memory vm, bool valid, string memory reason) = core_bridge.parseAndVerifyVM(encodedMsg);
//1. Check Wormhole Guardian Signatures
// If the VM is NOT valid, will return the reason it's not valid
// If the VM IS valid, reason will be blank
require(valid, reason);
//2. Check if the Emitter Chain contract is registered
require(_applicationContracts[vm.emitterChainId] == vm.emitterAddress, "Invalid Emitter Address!");
//3. Check that the message hasn't already been processed
require(!_completedMessages[vm.hash], "Message already processed");
_completedMessages[vm.hash] = true;
//Process the message. In this case, we just store the string in the VAA to storage
current_msg = string(vm.payload);
}
2022-07-01 06:29:03 -07:00
```