From 9907fa26bfdec30dbdf8585192ad4ae6103f97f2 Mon Sep 17 00:00:00 2001 From: Goran Vladika Date: Wed, 22 Nov 2023 13:54:15 +0100 Subject: [PATCH] Change _creationCodeFor so that it uses the same constructor as the one compiler would generate This will enable source code verification of the deployed token bridge contracts on the child chain --- .../arbitrum/L2AtomicTokenBridgeFactory.sol | 31 +++++++++---------- .../ethereum/L1AtomicTokenBridgeCreator.sol | 31 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/contracts/tokenbridge/arbitrum/L2AtomicTokenBridgeFactory.sol b/contracts/tokenbridge/arbitrum/L2AtomicTokenBridgeFactory.sol index 4b0c2b8703..cf9bd7d55e 100644 --- a/contracts/tokenbridge/arbitrum/L2AtomicTokenBridgeFactory.sol +++ b/contracts/tokenbridge/arbitrum/L2AtomicTokenBridgeFactory.sol @@ -290,24 +290,23 @@ contract L2AtomicTokenBridgeFactory { } /** - * @notice Generate a creation code that results on a contract with `code` as bytecode. - * Source - https://github.com/0xsequence/sstore2/blob/master/contracts/utils/Bytecode.sol - * @param code The returning value of the resulting `creationCode` - * @return creationCode (constructor) for new contract + * @notice Generate a creation code that results with a contract with `code` as deployed code. + * Generated creation code shall match the one generated by Solidity compiler. + * @dev Prepended constructor bytecode consists of: + * - 608060405234801561001057600080fd5b50 - store free memory pointer, then check no callvalue is provided + * - 61xxxx - push 2 bytes of `code` length + * - 806100206000396000f3fe - copy deployed code to memory and return the location of it + * @param code Deployed bytecode to which constructor bytecode will be prepended + * @return Creation code of a new contract */ function _creationCodeFor(bytes memory code) internal pure returns (bytes memory) { - /* - 0x00 0x63 0x63XXXXXX PUSH4 _code.length size - 0x01 0x80 0x80 DUP1 size size - 0x02 0x60 0x600e PUSH1 14 14 size size - 0x03 0x60 0x6000 PUSH1 00 0 14 size size - 0x04 0x39 0x39 CODECOPY size - 0x05 0x60 0x6000 PUSH1 00 0 size - 0x06 0xf3 0xf3 RETURN - - */ - - return abi.encodePacked(hex"63", uint32(code.length), hex"80600E6000396000F3", code); + return abi.encodePacked( + hex"608060405234801561001057600080fd5b50", + hex"61", + uint16(code.length), + hex"806100206000396000f3fe", + code + ); } } diff --git a/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol b/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol index 9a2fa9ddec..2a1dabfca0 100644 --- a/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol +++ b/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol @@ -662,24 +662,23 @@ contract L1AtomicTokenBridgeCreator is Initializable, OwnableUpgradeable { } /** - * @notice Generate a creation code that results on a contract with `code` as bytecode. - * Source - https://github.com/0xsequence/sstore2/blob/master/contracts/utils/Bytecode.sol - * @param code The returning value of the resulting `creationCode` - * @return creationCode (constructor) for new contract + * @notice Generate a creation code that results with a contract with `code` as deployed code. + * Generated creation code shall match the one generated by Solidity compiler. + * @dev Prepended constructor bytecode consists of: + * - 608060405234801561001057600080fd5b50 - store free memory pointer, then check no callvalue is provided + * - 61xxxx - push 2 bytes of `code` length + * - 806100206000396000f3fe - copy deployed code to memory and return the location of it + * @param code Deployed bytecode to which constructor bytecode will be prepended + * @return Creation code of a new contract */ function _creationCodeFor(bytes memory code) internal pure returns (bytes memory) { - /* - 0x00 0x63 0x63XXXXXX PUSH4 _code.length size - 0x01 0x80 0x80 DUP1 size size - 0x02 0x60 0x600e PUSH1 14 14 size size - 0x03 0x60 0x6000 PUSH1 00 0 14 size size - 0x04 0x39 0x39 CODECOPY size - 0x05 0x60 0x6000 PUSH1 00 0 size - 0x06 0xf3 0xf3 RETURN - - */ - - return abi.encodePacked(hex"63", uint32(code.length), hex"80600E6000396000F3", code); + return abi.encodePacked( + hex"608060405234801561001057600080fd5b50", + hex"61", + uint16(code.length), + hex"806100206000396000f3fe", + code + ); } /**