// SPDX-License-Identifier: GPL-3.0-onlypragmasolidity 0.8.20;libraryHash{
functionhash(bytes32 left, bytes32 right) internalpurereturns (bytes32) {
if (left ==0x0&& right ==0x0) {
return0x0;
} elseif (left ==0x0) {
returnkeccak256(abi.encodePacked(right));
} elseif (right ==0x0) {
returnkeccak256(abi.encodePacked(left));
} else {
returnkeccak256(abi.encodePacked(left, right));
}
}
functionhashGtvBytes32Leaf(bytes32 value) internalpurereturns (bytes32) {
returnsha256(abi.encodePacked(
uint8(0x1), // Gtv merkle tree leaf prefixuint8(0xA1), // // Gtv ByteArray tag: CONTEXT_CLASS, CONSTRUCTED, 1uint8(32+2),
uint8(0x4), // DER ByteArray taguint8(32),
value
));
}
functionhashGtvBytes64Leaf(bytesmemory value) internalpurereturns (bytes32) {
if (value.length!=64) {
revert("Hash: value must be 64 bytes long");
}
returnsha256(abi.encodePacked(
uint8(0x1), // Gtv merkle tree leaf prefixuint8(0xA1), // // Gtv ByteArray tag: CONTEXT_CLASS, CONSTRUCTED, 1uint8(64+2),
uint8(0x4), // DER ByteArray taguint8(64),
value
));
}
functionhashGtvIntegerLeaf(uint value) internalpurereturns (bytes32) {
uint8 nbytes =1;
uint remainingValue = value >>8; // minimal length is 1 so we skip the first bytewhile (remainingValue >0) {
nbytes +=1;
remainingValue = remainingValue >>8;
}
bytesmemory b =newbytes(nbytes);
remainingValue = value;
for (uint8 i =1; i <= nbytes; i++) {
uint8 v =uint8(remainingValue &0xFF);
b[nbytes - i] =bytes1(v);
remainingValue = remainingValue >>8;
}
if (uint8(b[0]) &0x80>0) {
returnsha256(abi.encodePacked(
uint8(0x1), // Gtv merkle tree leaf prefixuint8(0xA3), // GtvInteger tag: CONTEXT_CLASS, CONSTRUCTED, 3uint8(nbytes +3),
uint8(0x2), // DER integer tag
nbytes+1,
uint8(0),
b
));
}
returnsha256(abi.encodePacked(
uint8(0x1), // Gtv merkle tree leaf prefixuint8(0xA3), // GtvInteger tag: CONTEXT_CLASS, CONSTRUCTED, 3uint8(nbytes +2),
uint8(0x2), // DER integer tag
nbytes,
b
));
}
}
Contract Source Code
File 4 of 7: IERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)pragmasolidity ^0.8.20;/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/interfaceIERC20{
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/eventApproval(addressindexed owner, addressindexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/functiontotalSupply() externalviewreturns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/functionbalanceOf(address account) externalviewreturns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransfer(address to, uint256 value) externalreturns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/functionallowance(address owner, address spender) externalviewreturns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/functionapprove(address spender, uint256 value) externalreturns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom, address to, uint256 value) externalreturns (bool);
}
// SPDX-License-Identifier: GPL-3.0-onlypragmasolidity 0.8.20;import"./Hash.sol";
libraryMerkleProof{
/**
* @dev verify merkle proof using keccak256
*/functionverify(bytes32[] memory proofs, bytes32 leaf, uint position, bytes32 rootHash) internalpurereturns (bool) {
if (leaf ==0x0|| position >= (1<< proofs.length)) {
returnfalse;
}
bytes32 r = leaf;
for (uint i =0; i < proofs.length; i++) {
uint b = position & (1<< i);
if (b ==0) {
r = Hash.hash(r, proofs[i]);
} else {
r = Hash.hash(proofs[i], r);
}
}
return (r == rootHash);
}
/**
* @dev verify merkle proof using sha256
* specific for postchain block header extra data in dictionary data format
*/functionverifySHA256(bytes32[] memory proofs, bytes32 leaf, uint position, bytes32 rootHash) internalpurereturns (bool) {
if (position >= (1<< proofs.length)) {
returnfalse;
}
bytes32 r = leaf; // hashed leafuint last = proofs.length-1;
for (uint i =0; i < last; i++) {
uint b = position & (1<< i);
if (b ==0) {
r =sha256(abi.encodePacked(uint8(0x00), r, proofs[i]));
} else {
r =sha256(abi.encodePacked(uint8(0x00), proofs[i], r));
}
}
// the last node is fixed in dictionary format, prefix = 0x8uint p = position & (1<< last);
if (p ==0) {
r =sha256(abi.encodePacked(uint8(0x08), r, proofs[last]));
} else {
r =sha256(abi.encodePacked(uint8(0x08), proofs[last], r));
}
return (r == rootHash);
}
}