// SPDX-License-Identifier: BUSL-1.1// https://github.com/ensdomains/bufferpragmasolidity ^0.7.0;/**
* @dev A library for working with mutable byte buffers in Solidity.
*
* Byte buffers are mutable and expandable, and provide a variety of primitives
* for writing to them. At any time you can fetch a bytes object containing the
* current contents of the buffer. The bytes object should not be stored between
* operations, as it may change due to resizing of the buffer.
*/libraryBuffer{
/**
* @dev Represents a mutable buffer. Buffers have a current value (buf) and
* a capacity. The capacity may be longer than the current value, in
* which case it can be extended without the need to allocate more memory.
*/structbuffer {
bytes buf;
uint capacity;
}
/**
* @dev Initializes a buffer with an initial capacity.a co
* @param buf The buffer to initialize.
* @param capacity The number of bytes of space to allocate the buffer.
* @return The buffer, for chaining.
*/functioninit(buffer memory buf, uint capacity) internalpurereturns (buffer memory) {
if (capacity %32!=0) {
capacity +=32- (capacity %32);
}
// Allocate space for the buffer data
buf.capacity = capacity;
assembly {
let ptr :=mload(0x40)
mstore(buf, ptr)
mstore(ptr, 0)
mstore(0x40, add(32, add(ptr, capacity)))
}
return buf;
}
/**
* @dev Writes a byte string to a buffer. Resizes if doing so would exceed
* the capacity of the buffer.
* @param buf The buffer to append to.
* @param off The start offset to write to.
* @param rawData The data to append.
* @param len The number of bytes to copy.
* @return The original buffer, for chaining.
*/functionwriteRawBytes(
buffer memory buf,
uint off,
bytesmemory rawData,
uint offData,
uint len
) internalpurereturns (buffer memory) {
if (off + len > buf.capacity) {
resize(buf, max(buf.capacity, len + off) *2);
}
uint dest;
uint src;
assembly {
// Memory address of the buffer datalet bufptr :=mload(buf)
// Length of existing buffer datalet buflen :=mload(bufptr)
// Start address = buffer address + offset + sizeof(buffer length)
dest :=add(add(bufptr, 32), off)
// Update buffer length if we're extending itifgt(add(len, off), buflen) {
mstore(bufptr, add(len, off))
}
src :=add(rawData, offData)
}
// Copy word-length chunks while possiblefor (; len >=32; len -=32) {
assembly {
mstore(dest, mload(src))
}
dest +=32;
src +=32;
}
// Copy remaining bytesuint mask =256**(32- len) -1;
assembly {
let srcpart :=and(mload(src), not(mask))
let destpart :=and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
}
return buf;
}
/**
* @dev Writes a byte string to a buffer. Resizes if doing so would exceed
* the capacity of the buffer.
* @param buf The buffer to append to.
* @param off The start offset to write to.
* @param data The data to append.
* @param len The number of bytes to copy.
* @return The original buffer, for chaining.
*/functionwrite(buffer memory buf, uint off, bytesmemory data, uint len) internalpurereturns (buffer memory) {
require(len <= data.length);
if (off + len > buf.capacity) {
resize(buf, max(buf.capacity, len + off) *2);
}
uint dest;
uint src;
assembly {
// Memory address of the buffer datalet bufptr :=mload(buf)
// Length of existing buffer datalet buflen :=mload(bufptr)
// Start address = buffer address + offset + sizeof(buffer length)
dest :=add(add(bufptr, 32), off)
// Update buffer length if we're extending itifgt(add(len, off), buflen) {
mstore(bufptr, add(len, off))
}
src :=add(data, 32)
}
// Copy word-length chunks while possiblefor (; len >=32; len -=32) {
assembly {
mstore(dest, mload(src))
}
dest +=32;
src +=32;
}
// Copy remaining bytesuint mask =256**(32- len) -1;
assembly {
let srcpart :=and(mload(src), not(mask))
let destpart :=and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
}
return buf;
}
functionappend(buffer memory buf, bytesmemory data) internalpurereturns (buffer memory) {
return write(buf, buf.buf.length, data, data.length);
}
functionresize(buffer memory buf, uint capacity) privatepure{
bytesmemory oldbuf = buf.buf;
init(buf, capacity);
append(buf, oldbuf);
}
functionmax(uint a, uint b) privatepurereturns (uint) {
if (a > b) {
return a;
}
return b;
}
}
Contract Source Code
File 2 of 6: FPValidator.sol
// SPDX-License-Identifier: BUSL-1.1pragmasolidity 0.7.6;pragmaabicoderv2;import"./utility/LayerZeroPacket.sol";
import"../interfaces/ILayerZeroValidationLibrary.sol";
import"../interfaces/IValidationLibraryHelperV2.sol";
interfaceIStargate{
// Stargate objects for abi encoding / decodingstructSwapObj {
uint amount;
uint eqFee;
uint eqReward;
uint lpFee;
uint protocolFee;
uint lkbRemove;
}
structCreditObj {
uint credits;
uint idealBalance;
}
}
contractFPValidatorisILayerZeroValidationLibrary, IValidationLibraryHelperV2{
uint8public proofType =2;
uint8public utilsVersion =1;
addresspublicimmutable stargateBridgeAddress;
addresspublicimmutable stargateTokenAddress;
constructor(address _stargateBridgeAddress, address _stargateTokenAddress) {
stargateBridgeAddress = _stargateBridgeAddress;
stargateTokenAddress = _stargateTokenAddress;
}
functionvalidateProof(bytes32 _packetHash, bytescalldata _transactionProof, uint _remoteAddressSize) externalviewoverridereturns (LayerZeroPacket.Packet memory packet) {
require(_remoteAddressSize >0, "ProofLib: invalid address size");
// _transactionProof = srcUlnAddress (32 bytes) + lzPacketrequire(_transactionProof.length>32&&keccak256(_transactionProof) == _packetHash, "ProofLib: invalid transaction proof");
bytesmemory ulnAddressBytes =bytes(_transactionProof[0:32]);
bytes32 ulnAddress;
assembly {
ulnAddress :=mload(add(ulnAddressBytes, 32))
}
packet = LayerZeroPacket.getPacketV3(_transactionProof[32:], _remoteAddressSize, ulnAddress);
if (packet.dstAddress == stargateBridgeAddress) packet.payload = _secureStgPayload(packet.payload);
if (packet.dstAddress == stargateTokenAddress) packet.payload = _secureStgTokenPayload(packet.payload);
return packet;
}
function_secureStgTokenPayload(bytesmemory _payload) internalpurereturns (bytesmemory) {
(bytesmemory toAddressBytes, uint qty) =abi.decode(_payload, (bytes, uint));
address toAddress =address(0);
if (toAddressBytes.length>0) {
assembly {
toAddress :=mload(add(toAddressBytes, 20))
}
}
if (toAddress ==address(0)) {
address deadAddress =address(0x000000000000000000000000000000000000dEaD);
bytesmemory newToAddressBytes =abi.encodePacked(deadAddress);
returnabi.encode(newToAddressBytes, qty);
}
// default to return the original payloadreturn _payload;
}
function_secureStgPayload(bytesmemory _payload) internalviewreturns (bytesmemory) {
// functionType is uint8 even though the encoding will take up the side of uint256uint8 functionType;
assembly {
functionType :=mload(add(_payload, 32))
}
// TYPE_SWAP_REMOTE == 1 && only if the payload has a payload// only swapRemote inside of stargate can call sgReceive on an user supplied to address// thus we do not care about the other type functions even if the toAddress is overly long.if (functionType ==1) {
// decode the _payload with its types
(, uint srcPoolId, uint dstPoolId, uint dstGasForCall, IStargate.CreditObj memory c, IStargate.SwapObj memory s, bytesmemory toAddressBytes, bytesmemory contractCallPayload) =abi.decode(_payload, (uint8, uint, uint, uint, IStargate.CreditObj, IStargate.SwapObj, bytes, bytes));
// if contractCallPayload.length > 0 need to check if the to address is a contract or notif (contractCallPayload.length>0) {
// otherwise, need to check if the payload can be delivered to the toAddressaddress toAddress =address(0);
if (toAddressBytes.length>0) {
assembly {
toAddress :=mload(add(toAddressBytes, 20))
}
}
// check if the toAddress is a contract. We are not concerned about addresses that pretend to be wallets. because worst case we just delete their payload if being malicious// we can guarantee that if a size > 0, then the contract is definitely a contract address in this contextuint size;
assembly {
size :=extcodesize(toAddress)
}
if (size ==0) {
// size == 0 indicates its not a contract, payload wont be delivered// secure the _payload to make sure funds can be delivered to the toAddressbytesmemory newToAddressBytes =abi.encodePacked(toAddress);
bytesmemory securePayload =abi.encode(functionType, srcPoolId, dstPoolId, dstGasForCall, c, s, newToAddressBytes, bytes(""));
return securePayload;
}
}
}
// default to return the original payloadreturn _payload;
}
functionsecureStgTokenPayload(bytesmemory _payload) externalpurereturns (bytesmemory) {
return _secureStgTokenPayload(_payload);
}
functionsecureStgPayload(bytesmemory _payload) externalviewreturns (bytesmemory) {
return _secureStgPayload(_payload);
}
functiongetUtilsVersion() externalviewoverridereturns (uint8) {
return utilsVersion;
}
functiongetProofType() externalviewoverridereturns (uint8) {
return proofType;
}
functiongetVerifyLog(bytes32, uint[] calldata, uint, bytes[] calldata proof) externalpureoverridereturns (ULNLog memory log) {}
functiongetPacket(bytesmemory data, uint sizeOfSrcAddress, bytes32 ulnAddress) externalpureoverridereturns (LayerZeroPacket.Packet memory) {
return LayerZeroPacket.getPacketV3(data, sizeOfSrcAddress, ulnAddress);
}
}
// SPDX-License-Identifier: MITpragmasolidity ^0.7.0;/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/librarySafeMath{
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryAdd(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontrySub(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryMul(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryDiv(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b ==0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryMod(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b ==0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/functionadd(uint256 a, uint256 b) internalpurereturns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/functionmul(uint256 a, uint256 b) internalpurereturns (uint256) {
if (a ==0) return0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b >0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b >0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b >0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b >0, errorMessage);
return a % b;
}
}