mcfx's blog

题解、Writeup、游记和碎碎念

"Minimal" Arbitrary Proxy

Update: you can use CALLDATALOAD instead of CALLDATACOPY and MLOAD, I forgot about that.

Inspired by https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract/, we can build a similar one, but allows arbitrary delegatecall target.

In order to save gas, we only store the target address in input data (which we can deploy one for each airdrop target), don't allow call data for the delegatecall, and ignore the return value.

Here's the main code:

CodeInstructionStackMemory / Comments
363d3d37Similar to part 1 in the link above-[0, cds] = calldata (target addr)
3d3d3d3d3dRETURNDATASIZE * 50 0 0 0 0[0, cds] = addr
51MLOADaddr 0 0 0 0(no longer used)
5aGASgas addr 0 0 0 0
32ORIGINorigin ...
73 originPUSH20 allowed originallowed origin ...
18XOR[zero if allowed] ...
3dRETURNDATASIZE0 [zero if allowed] ...
57JUMPIgas addr 0 0 0 0If the origin is not allowed, it will jump to zero, which is not a valid jump destination
f4DELEGATECALLsuccess
3d3dRETURNDATASIZE0 0 successThe call target should make rds=0
f3RETURNsuccess

Then the runtime code is:

363d3d373d3d3d3d3d515a3273
[origin]
183d57f43d3df3

We can also make a call in the creation code. The analysis is similar, and omitted. Here is the final code:

3d60288060263d398180808073
[first delegatecall target]
5af45081f3
363d3d373d3d3d3d3d515a3273
[origin]
183d57f43d3df3

日期: 2022-10-11

标签: Blockchain