Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hyperlane adapter #17

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
185 changes: 185 additions & 0 deletions src/hyperlane-adapter/HyperlaneReceiverAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.16;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { IMailbox } from "./interfaces/IMailbox.sol";
import { IMessageRecipient } from "./interfaces/IMessageRecipient.sol";
import { IInterchainSecurityModule, ISpecifiesInterchainSecurityModule } from "./interfaces/IInterchainSecurityModule.sol";
import { TypeCasts } from "./libraries/TypeCasts.sol";
import { Errors } from "./libraries/Errors.sol";
import { IMessageDispatcher } from "./interfaces/IMessageDispatcher.sol";
import { IMessageExecutor } from "./interfaces/IMessageExecutor.sol";
import "./libraries/MessageLib.sol";

/**
* @title HyperlaneReceiverAdapter implementation.
* @notice `IBridgeReceiverAdapter` implementation that uses Hyperlane as the bridge.
*/
contract HyperlaneReceiverAdapter is
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to indicate that this only supports v2

Copy link
Author

@degencodebeast degencodebeast Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMessageRecipient,
ISpecifiesInterchainSecurityModule,
Ownable
{
/// @notice `Mailbox` contract reference.
IMailbox public immutable mailbox;

/// @notice `ISM` contract reference.
IInterchainSecurityModule public ism;

/**
* @notice Sender adapter address for each source chain.
* @dev srcChainId => senderAdapter address.
*/
mapping(uint256 => IMessageDispatcher) public senderAdapters;

/**
* @notice Ensure that messages cannot be replayed once they have been executed.
* @dev msgId => isExecuted.
*/
mapping(bytes32 => bool) public executedMessages;

/**
* @notice Emitted when the ISM is set.
* @param module The new ISM for this adapter/recipient.
*/
event IsmSet(address indexed module);

/**
* @notice Emitted when a message has successfully been executed.
* @param fromChainId ID of the chain that dispatched the message
* @param messageId ID uniquely identifying the message that was executed
*/
event MessageIdExecuted(uint256 indexed fromChainId, bytes32 indexed messageId);

/**
* @notice Emitted when a messageId has already been executed.
* @param messageId ID uniquely identifying the message or message batch that were re-executed
*/
error MessageIdAlreadyExecuted(bytes32 messageId);

/**
* @notice Emitted when a sender adapter for a source chain is updated.
* @param srcChainId Source chain identifier.
* @param senderAdapter Address of the sender adapter.
*/
event SenderAdapterUpdated(uint256 srcChainId, IMessageDispatcher senderAdapter);

/* Constructor */
/**
* @notice HyperlaneReceiverAdapter constructor.
* @param _mailbox Address of the Hyperlane `Mailbox` contract.
*/
constructor(address _mailbox) {
if (_mailbox == address(0)) {
revert Errors.InvalidMailboxZeroAddress();
}
mailbox = IMailbox(_mailbox);
}

/// @notice Restrict access to trusted `Mailbox` contract.
modifier onlyMailbox() {
if (msg.sender != address(mailbox)) {
revert Errors.UnauthorizedMailbox(msg.sender);
}
_;
}

/// @inheritdoc ISpecifiesInterchainSecurityModule
function interchainSecurityModule() external view returns (IInterchainSecurityModule) {
return ism;
}

/**
* @notice Sets the ISM for this adapter/recipient.
* @param _ism The ISM contract address.
*/
function setIsm(address _ism) external onlyOwner {
ism = IInterchainSecurityModule(_ism);
emit IsmSet(_ism);
}

function executeMessage(
address _to,
bytes memory _message,
bytes32 _messageId,
uint256 _fromChainId,
address _from,
bool _executedMessageId
) internal {
MessageLib.executeMessage(_to, _message, _messageId, _fromChainId, _from, _executedMessageId);

emit MessageIdExecuted(_fromChainId, _messageId);
}

function executeMessageBatch(
MessageLib.Message[] memory _messages,
bytes32 _messageId,
uint256 _fromChainId,
address _from,
bool _executedMessageId
) internal {
MessageLib.executeMessageBatch(_messages, _messageId, _fromChainId, _from, _executedMessageId);

emit MessageIdExecuted(_fromChainId, _messageId);
}

/**
* @notice Called by Hyperlane `Mailbox` contract on destination chain to receive cross-chain messages.
* @dev _origin Source chain domain identifier (not currently used).
* @param _sender Address of the sender on the source chain.
* @param _body Body of the message.
*/
function handle(
uint32,
/* _origin*/
bytes32 _sender,
bytes memory _body
) external onlyMailbox {
address adapter = TypeCasts.bytes32ToAddress(_sender);
bool _executedMessageId;
(
MessageLib.Message[] memory _messages,
bytes32 msgId,
uint256 srcChainId,
address srcSender
) = abi.decode(_body, (MessageLib.Message[], bytes32, uint256, address));

if (IMessageDispatcher(adapter) != senderAdapters[srcChainId]) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should compare to the origin argument from the handle, no? Otherwise an origin chain sender from a different chain can falsely claim a different chain (even if it has to be permissioned as an adapter)

Copy link
Author

@degencodebeast degencodebeast Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert Errors.UnauthorizedAdapter(srcChainId, adapter);
}
if (executedMessages[msgId]) {
revert MessageIdAlreadyExecuted(msgId);
} else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else is technically superflous, i like having all the revert conditions up front, and then do the state changes after

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_executedMessageId = executedMessages[msgId];
executedMessages[msgId] = true;
}
if (_messages.length < 1) {
revert Errors.NoMessagesSent(srcChainId);
}
if (_messages.length == 1) {
MessageLib.Message memory _message = _messages[0];
executeMessage(_message.to, _message.data, msgId, srcChainId, srcSender, _executedMessageId);
} else {
executeMessageBatch(_messages, msgId, srcChainId, srcSender, _executedMessageId);
}
}

function updateSenderAdapter(
uint256[] calldata _srcChainIds,
IMessageDispatcher[] calldata _senderAdapters
) external onlyOwner {
if (_srcChainIds.length != _senderAdapters.length) {
revert Errors.MismatchChainsAdaptersLength(_srcChainIds.length, _senderAdapters.length);
}
for (uint256 i; i < _srcChainIds.length; ++i) {
senderAdapters[_srcChainIds[i]] = _senderAdapters[i];
emit SenderAdapterUpdated(_srcChainIds[i], _senderAdapters[i]);
}
}

function getSenderAdapter(
uint256 _srcChainId
) public view returns (IMessageDispatcher _senderAdapter) {
_senderAdapter = senderAdapters[_srcChainId];
}
}
Loading