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

feat: added Interchain Router #105

Merged
merged 19 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions contracts/interfaces/IInterchainRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IContractIdentifier } from './IContractIdentifier.sol';

/**
* @title IRemoteAddressValidator
* @dev Manages and validates remote addresses, keeps track of addresses supported by the Axelar gateway contract
*/
interface IInterchainRouter is IContractIdentifier {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
error ZeroAddress();
error LengthMismatch();
error ZeroStringLength();
error UntrustedChain();

event TrustedAddressAdded(string sourceChain, string sourceAddress);
event TrustedAddressRemoved(string sourceChain);

/**
* @dev Gets the name of the chain this is deployed at
*/
function getChainName() external view returns (string memory chainName);
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

/**
* @dev Gets the trusted address at a remote chain
* @param chain Chain name of the remote chain
* @return trustedAddress the trusted address for the chain. Returns '' if the chain is untrusted
*/
function getTrustedAddress(string memory chain) external view returns (string memory trustedAddress);
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

/**
* @dev Gets the trusted address hash at a remote chain
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
* @param chain Chain name of the remote chain
* @return trustedAddressHash the hash of the trusted address
*/
function getTrustedAddressHash(string memory chain) external view returns (bytes32 trustedAddressHash);

/**
* @dev Validates that the sender is a valid interchain token service address
* @param sourceChain Source chain of the transaction
* @param sourceAddress Source address of the transaction
* @return bool true if the sender is validated, false otherwise
*/
function validateSender(string calldata sourceChain, string calldata sourceAddress) external view returns (bool);

/**
* @dev Adds a trusted interchain token service address for the specified chain
* @param sourceChain Chain name of the interchain token service
* @param sourceAddress Interchain token service address to be added
*/
function addTrustedAddress(string memory sourceChain, string memory sourceAddress) external;

/**
* @dev Removes a trusted interchain token service address
* @param sourceChain Chain name of the interchain token service to be removed
*/
function removeTrustedAddress(string calldata sourceChain) external;

/**
* @dev Fetches the interchain token service address for the specified chain
* @param chainName Name of the chain
* @return remoteAddress Interchain token service address for the specified chain
*/
function getRemoteAddress(string calldata chainName) external view returns (string memory remoteAddress);
}
33 changes: 33 additions & 0 deletions contracts/test/utils/TestInterchainRouterProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { Proxy } from '../../upgradable/Proxy.sol';

/**
* @title TestInterchainRouterProxy
* @dev Proxy contract for the InterchainRouter contract. Inherits from the Proxy contract.
*/
contract TestInterchainRouterProxy is Proxy {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
bytes32 private constant CONTRACT_ID = keccak256('interchain-router');

/**
* @dev Constructs the InterchainRouterProxy contract.
* @param implementationAddress Address of the InterchainRouter implementation
* @param owner Address of the owner of the proxy
* @param params The params to be passed to the _setup function of the implementation.
*/
constructor(
address implementationAddress,
address owner,
bytes memory params
) Proxy(implementationAddress, owner, params) {}

/**
* @dev Override for the `contractId` function in Proxy. Returns a unique identifier for this contract.
* @return bytes32 Identifier for this contract.
*/
function contractId() internal pure override returns (bytes32) {
return CONTRACT_ID;
}
}
171 changes: 171 additions & 0 deletions contracts/utils/InterchainRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { IInterchainRouter } from '../interfaces/IInterchainRouter.sol';
import { Upgradable } from '../upgradable/Upgradable.sol';

/**
* @title RemoteAddressValidator
* @dev Manages and validates remote addresses, keeps track of addresses supported by the Axelar gateway contract
*/
contract InterchainRouter is IInterchainRouter, Upgradable {
bytes32 internal constant PREFIX_ADDRESS_MAPPING = keccak256('interchain-router-address-mapping');
bytes32 internal constant PREFIX_ADDRESS_HASH_MAPPING = keccak256('interchain-router-address-hash-mapping');
uint256 internal constant CHAIN_NAME_SLOT = 0x6406a0b603e31e24a15e9f663879eedde3bef27687f318a9875bafac9d63fc1f;
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

bytes32 private constant CONTRACT_ID = keccak256('interchain-router');

struct StringStorage {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
string value;
}

/**
* @dev Constructs the RemoteAddressValidator contract, both array parameters must be equal in length.
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
* @param chainName_ The name of the current chain.
*/
constructor(string memory chainName_) {
if (bytes(chainName_).length == 0) revert ZeroStringLength();
_getStringStorage(CHAIN_NAME_SLOT).value = chainName_;
}

/**
* @notice Getter for the contract id.
*/
function contractId() external pure returns (bytes32) {
return CONTRACT_ID;
}

function _setup(bytes calldata params) internal override {
(string[] memory trustedChainNames, string[] memory trustedAddresses) = abi.decode(
params,
(string[], string[])
);
uint256 length = trustedChainNames.length;
if (length != trustedAddresses.length) revert LengthMismatch();

for (uint256 i; i < length; ++i) {
addTrustedAddress(trustedChainNames[i], trustedAddresses[i]);
}
}

/**
* @dev Gets the name of the chain this is deployed at
*/
function getChainName() external view returns (string memory chainName) {
chainName = _getStringStorage(CHAIN_NAME_SLOT).value;
}

/**
* @dev Gets the key for the trusted address at a remote chain
* @param chain Chain name of the remote chain
* @return slot the slot to store the trusted address in
*/
function _getTrustedAddressSlot(string memory chain) internal pure returns (uint256 slot) {
slot = uint256(keccak256(abi.encode(PREFIX_ADDRESS_MAPPING, chain)));
}

/**
* @dev Gets the key for the trusted address at a remote chain
* @param chain Chain name of the remote chain
* @return slot the slot to store the trusted address hash in
*/
function _getTrustedAddressHashSlot(string memory chain) internal pure returns (uint256 slot) {
slot = uint256(keccak256(abi.encode(PREFIX_ADDRESS_HASH_MAPPING, chain)));
}

function _getStringStorage(uint256 slot) internal pure returns (StringStorage storage storageStr) {
assembly {
storageStr.slot := slot
}
}

/**
* @dev Sets the trusted address and its hash for a remote chain
* @param chain Chain name of the remote chain
* @param trustedAddress the string representation of the trusted address
*/
function _setTrustedAddress(string memory chain, string memory trustedAddress) internal {
_getStringStorage(_getTrustedAddressSlot(chain)).value = trustedAddress;
uint256 slot = _getTrustedAddressHashSlot(chain);
bytes32 addressHash = keccak256(bytes(trustedAddress));
assembly {
sstore(slot, addressHash)
}
}

/**
* @dev Gets the trusted address at a remote chain
* @param chain Chain name of the remote chain
* @return trustedAddress the trusted address for the chain. Returns '' if the chain is untrusted
*/
function getTrustedAddress(string memory chain) public view returns (string memory trustedAddress) {
trustedAddress = _getStringStorage((_getTrustedAddressSlot(chain))).value;
}

/**
* @dev Gets the trusted address hash at a remote chain
* @param chain Chain name of the remote chain
* @return trustedAddressHash the hash of the trusted address
*/
function getTrustedAddressHash(string memory chain) public view returns (bytes32 trustedAddressHash) {
uint256 slot = _getTrustedAddressHashSlot(chain);
assembly {
trustedAddressHash := sload(slot)
}
}

/**
* @dev Validates that the sender is a valid interchain token service address
* @param sourceChain Source chain of the transaction
* @param sourceAddress Source address of the transaction
* @return bool true if the sender is validated, false otherwise
*/
function validateSender(string calldata sourceChain, string calldata sourceAddress) external view returns (bool) {
bytes32 sourceAddressHash = keccak256(bytes(sourceAddress));

return sourceAddressHash == getTrustedAddressHash(sourceChain);
}

/**
* @dev Adds a trusted interchain token service address for the specified chain
* @param sourceChain Chain name of the interchain token service
* @param sourceAddress Interchain token service address to be added
*/
function addTrustedAddress(string memory sourceChain, string memory sourceAddress) public onlyOwner {
if (bytes(sourceChain).length == 0) revert ZeroStringLength();
if (bytes(sourceAddress).length == 0) revert ZeroStringLength();

_setTrustedAddress(sourceChain, sourceAddress);

emit TrustedAddressAdded(sourceChain, sourceAddress);
}

/**
* @dev Removes a trusted interchain token service address
* @param sourceChain Chain name of the interchain token service to be removed
*/
function removeTrustedAddress(string calldata sourceChain) external onlyOwner {
if (bytes(sourceChain).length == 0) revert ZeroStringLength();

delete _getStringStorage(_getTrustedAddressSlot(sourceChain)).value;
uint256 slot = _getTrustedAddressHashSlot(sourceChain);
assembly {
sstore(slot, 0)
}
emit TrustedAddressRemoved(sourceChain);
}

/**
* @dev Fetches the interchain token service address for the specified chain
* @param chainName Name of the chain
* @return remoteAddress Interchain token service address for the specified chain
*/
function getRemoteAddress(string calldata chainName) external view returns (string memory remoteAddress) {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
remoteAddress = getTrustedAddress(chainName);

if (bytes(remoteAddress).length == 0) {
revert UntrustedChain();
}
}
}
Loading