Unexpected packed abi encoding in solidity abi.encode operation
Description
Today we stumbled upon a bug and digging deeper I was surprised to see that in one of our smart contracts an abi.encode resulted in what looks like an abi.encodePacked , which if true and not intended by the compiler is of course quite problematic.
My suspicion is that as soon as multiple inputs are supplied to abi.encode it defaults to running abi.encodePacked, instead.
Environment
- Compiler version:
0.8.17(optimizer enabled) - Target EVM version (as per compiler settings): default
- Framework/IDE (e.g. Truffle or Remix): Hardhat (
"hardhat": "2.11.2",) - EVM execution environment / backend / blockchain client: Hardhat EVM
- Operating system: macOS
Steps to Reproduce
I created solidity and typescript example code to reproduce the findings.
Step 1: import files into your local hardhat environment Step2: build Step3: run the test with hardhat
The ts test code assumes that you use hardhat and hardhat-deploy and typechain locally, but you can easily reproduce on Remix as well with some modifications.
hardhat.config.ts
solidity: {
compilers: [
{
version: "0.8.17",
settings: {
optimizer: { enabled: true },
},
},
],
},
Mock Contract for Reproduction
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
struct Message {
address service;
bytes data;
uint256 salt;
uint256 deadline;
}
contract AbiEncode {
bytes32 public constant EXEC_WITH_SIGS_TYPEHASH =
keccak256(
bytes(
// solhint-disable-next-line max-line-length
"ExecWithSigs(address service,bytes data,uint256 salt,uint256 deadline)"
)
);
function normalEncoding(Message calldata _msg)
external
view
returns (bytes memory)
{
return abi.encode(_msg);
}
function unexpectedPackedEncoding(Message calldata _msg)
external
view
returns (bytes memory)
{
return
abi.encode(
EXEC_WITH_SIGS_TYPEHASH,
_msg.service,
keccak256(_msg.data),
_msg.salt,
_msg.deadline
);
}
}
Use this deploy file with hardhat-deploy and run the test file:
import { deployments, getNamedAccounts } from "hardhat";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
if (hre.network.name !== "hardhat") {
console.error("Only deploy AbiEncode locally for testing");
process.exit(1);
}
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy("AbiEncode", {
from: deployer,
log: hre.network.name != "hardhat" ? true : false,
});
};
export default func;
func.skip = async (hre: HardhatRuntimeEnvironment) => {
return hre.network.name !== "hardhat";
};
func.tags = ["AbiEncode"];
Test file:
import { expect } from "chai";
import hre, { ethers } from "hardhat";
import { AbiEncode } from "../typechain";
import { MessageStruct } from "../typechain/contracts/AbiEncode";
const service = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
const data =
"0x18cbafe50000000000000000000000000000000000000000000000878678326eac900000000000000000000000000000000000000000000000000000005ed6013cc864d700000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000143021f7aa26f577d02d02c037e79631406450e60000000000000000000000000000000000000000000000000000000061025b1700000000000000000000000000000000000000000000000000000000000000020000000000000000000000006369c3dadfc00054a42ba8b2c09c48131dd4aa38000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const salt = 420;
const deadline = 42069;
function typedAbiEncode(_typehash: string, _msg: MessageStruct): string {
return new ethers.utils.AbiCoder().encode(
["bytes", "address", "bytes32", "uint256", "uint256"],
[
_typehash,
_msg.service,
ethers.utils.keccak256(_msg.data as string),
_msg.salt,
_msg.deadline,
]
);
}
describe("AbiEncode Tests", function () {
beforeEach(async function () {
await hre.deployments.fixture();
});
it("normalEncoding", async function () {
const msg: MessageStruct = {
service,
data,
salt,
deadline,
};
const abiEncode = (await ethers.getContract("AbiEncode")) as AbiEncode;
const contractEncoding = await abiEncode.normalEncoding(msg);
console.log({ contractEncoding });
let ethersEncoding = abiEncode.interface.encodeFunctionData(
"normalEncoding",
[msg]
);
// remove function selector
ethersEncoding = `0x${ethersEncoding.substring(2 + 2 * 4)}`;
console.log({ ethersEncoding });
expect(contractEncoding).to.be.equal(ethersEncoding);
});
it("unexpectedPackedEncoding", async function () {
const msg: MessageStruct = {
service,
data,
salt,
deadline,
};
const abiEncode = (await ethers.getContract("AbiEncode")) as AbiEncode;
const contractEncoding = await abiEncode.unexpectedPackedEncoding(msg);
console.log({ contractEncoding });
const ethersEncoding = typedAbiEncode(
await abiEncode.EXEC_WITH_SIGS_TYPEHASH(),
msg
);
console.log({ ethersEncoding });
expect(contractEncoding).to.be.equal(ethersEncoding);
});
});
Test Results
normalEncoding
{
contractEncoding: '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000a455000000000000000000000000000000000000000000000000000000000000010418cbafe50000000000000000000000000000000000000000000000878678326eac900000000000000000000000000000000000000000000000000000005ed6013cc864d700000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000143021f7aa26f577d02d02c037e79631406450e60000000000000000000000000000000000000000000000000000000061025b1700000000000000000000000000000000000000000000000000000000000000020000000000000000000000006369c3dadfc00054a42ba8b2c09c48131dd4aa38000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000'
} ✅
{
ethersEncoding: '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000a455000000000000000000000000000000000000000000000000000000000000010418cbafe50000000000000000000000000000000000000000000000878678326eac900000000000000000000000000000000000000000000000000000005ed6013cc864d700000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000143021f7aa26f577d02d02c037e79631406450e60000000000000000000000000000000000000000000000000000000061025b1700000000000000000000000000000000000000000000000000000000000000020000000000000000000000006369c3dadfc00054a42ba8b2c09c48131dd4aa38000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000'
} ✅
unexpectedPackedEncoding
{
contractEncoding: '0xa60d3d5ef73882f5e7edb151ae707dcaf09cdc2a400201dafebbc6e5130013870000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488df4261ef29426d50260f88ff5f8cbdb55ecebaed02d09ac2919449660db896f1400000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000a455'
} 🔴 (looks like encoding was non-standard packed)
{
ethersEncoding: '0x00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488df4261ef29426d50260f88ff5f8cbdb55ecebaed02d09ac2919449660db896f1400000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000a4550000000000000000000000000000000000000000000000000000000000000020a60d3d5ef73882f5e7edb151ae707dcaf09cdc2a400201dafebbc6e513001387'
} ✅
I worked with @gitpusha as well on this, so I have further code snippets/environment information to share if needed.
Closing this as I found a mistake in the ethers.js encoding we used.
This should have been
function typedAbiEncode(_typehash: string, _msg: MessageStruct): string {
return new ethers.utils.AbiCoder().encode(
["bytes32", "address", "bytes32", "uint256", "uint256"],
[
_typehash,
_msg.service,
ethers.utils.keccak256(_msg.data as string),
_msg.salt,
_msg.deadline,
]
);
}
The _typehash is bytes32 not bytes
Fixing this also makes the test go green ✅