Ethereum
Here Are the Few core Function of the bridge
SWAP AND BRIDGE TRANSFER -
SwapAndBridge() initiates the cross-chain transfer by creating a unique message hash and transferring tokens to the bridge contract. This function handles the source chain operations and token locking mechanism
function swapAndBridge(
uint256 sourceChainId,
address addr,
uint256 amount,
address tokenAddress,
uint256 nonce,
uint256 destinationChainId
) public returns (bytes32) {
bytes32 message = hashMessage(sourceChainId, addr, amount, tokenAddress, nonce, destinationChainId);
messenger.sendMessage(message);
//Before calling this function, the user must approve the contract to spend their tokens
IERC20(tokenAddress).transferFrom(msg.sender, address(this), amount);
return message;
}
TOKEN RECEPTION HANDLER-
receiveTokens()
processes incoming bridge transfers by verifying the message hash and preventing duplicate transactions. This function manages token distribution on the destination chain after validation.
function receiveTokens(
uint256 sourceChainId,
address addr,
uint256 amount,
address tokenAddress,
uint256 nonce,
uint256 destinationChainId
) external {
bytes32 message = hashMessage(sourceChainId, addr, amount, tokenAddress, nonce, destinationChainId);
bool isReceived = messenger.hasMessageBeenReceived(message);
if (!isReceived) {
revert Bridge__MessageNotreceived();
}
if (processedMessages[message] != 0) {
revert Bridge__MessageAlreadyProcess();
}
processedMessages[message] = 1;
IERC20(tokenAddress).transfer(addr, amount);
}
VALIDATOR SIGNATURE VERIFICATION-
receiveMessage()
implements multi-signature validation using primary and secondary validator signatures through ecrecover. This function ensures secure message verification before completing cross-chain transfers
function resiveMessage(bytes32 message, uint8 v1, bytes32 r1, bytes32 s1, uint8 v2, bytes32 r2, bytes32 s2)
external
{
bytes32 prefixedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", message));
// bytes32 messageHash = keccak256(abi.encodePacked(message));
address primaryValidatorRecoveredAddress = ecrecover(prefixedHash, v1, r1, s1);
address secondaryValidatorsRecoveredAddress = ecrecover(prefixedHash, v2, r2, s2);
if (primaryValidatorRecoveredAddress != primaryValidator) {
revert Messenger__InvaildPrimaryValidat(primaryValidatorRecoveredAddress);
}
if (secondaryValidators[secondaryValidatorsRecoveredAddress] != true) {
revert Messenger__InvaildSecondaryValidat();
}
receivedMessages[message] = 1;
emit MessageReceived(message);
}
Last updated