Skip to content

Commit

Permalink
fix(ERC1271InputGenerator): fix code start with ef error by adding 0x…
Browse files Browse the repository at this point in the history
…ff as first byte
  • Loading branch information
xenoliss committed May 15, 2024
1 parent facf1a5 commit ad90817
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/utils/ERC1271InputGenerator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ contract ERC1271InputGenerator {
/// @param accountFactory The factory that will be used to deploy the account (if not already deployed).
/// @param factoryCalldata The calldata that will be used to deploy the account (if not already deployed).
constructor(CoinbaseSmartWallet account, bytes32 hash, address accountFactory, bytes memory factoryCalldata) {
// This allows us to get a replay-safe hash on any deployed or undeployed account
// in a single eth_call, i.e. without deploying the contract. We do this by calling replaySafeHash on a deployed
// account,
// or by simulating the deployment of an undeployed account and then calling replaySafeHash on it.
// This allows us to get a replay-safe hash on any deployed or undeployed account in a single eth_call, i.e.
// without deploying the contract. We do this by calling replaySafeHash on a deployed account, or by simulating
// the deployment of an undeployed account and then calling replaySafeHash on it.
bytes32 replaySafeHash = _coinbaseSmartWallet1271Input(account, hash, accountFactory, factoryCalldata);
assembly {
// store replay safe hash
mstore(0x80, replaySafeHash)
// return replay safe hash
return(0x80, 0x20)
// Ensure the contract code does not start with ef (see https://eips.ethereum.org/EIPS/eip-3541).
mstore8(0x80, 0xff)
// Store replay safe hash.
mstore(0x81, replaySafeHash)
// Return 33 bytes corresponding to 0xff | replaySafeHash.
// Off-chain caller must skip the first byte (0xff) when interpreting the result.
return(0x80, 0x21)
}
}

Expand Down
11 changes: 9 additions & 2 deletions test/ERC1271InputGenerator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract CoinbaseSmartWallet1271InputGeneratorTest is Test {
bytes32 hash = 0x15fa6f8c855db1dccbb8a42eef3a7b83f11d29758e84aed37312527165d5eec5;
bytes32 replaySafeHash = deployedAccount.replaySafeHash(hash);
ERC1271InputGenerator generator = new ERC1271InputGenerator(deployedAccount, hash, address(0), "");
assertEq(bytes32(address(generator).code), replaySafeHash);
assertEq(_bytes32FromByteCode(address(generator).code), replaySafeHash);
}

function testGetReplaySafeHashForUndeployedAccount() public {
Expand All @@ -42,6 +42,13 @@ contract CoinbaseSmartWallet1271InputGeneratorTest is Test {
// This is now deployed.
bytes32 replaySafeHash = undeployedAccount.replaySafeHash(hash);

assertEq(bytes32(address(generator).code), replaySafeHash);
assertEq(_bytes32FromByteCode(address(generator).code), replaySafeHash);
}

function _bytes32FromByteCode(bytes memory bytecode) private pure returns (bytes32) {
bytes32 a = abi.decode(bytecode, (bytes32));
uint8 b = uint8(bytecode[bytecode.length - 1]);

return (a << 8) | bytes32(uint256(b));
}
}

0 comments on commit ad90817

Please sign in to comment.