Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matching creation bytecode "perfect" when the creation bytecode does not have CBOR not at end #1839

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions packages/lib-sourcify/src/lib/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,25 @@ export async function matchWithCreationTx(
// The reason why this uses `startsWith` instead of `===` is that creationTxData may contain constructor arguments at the end part.
if (match.onchainCreationBytecode.startsWith(recompiledCreationBytecode)) {
// if the bytecode doesn't end with metadata then "partial" match
// endsWithMetadataHash checks the metadata hash, not only the CBOR auxdata
if (endsWithMetadataHash(recompiledCreationBytecode)) {
match.creationMatch = 'perfect';
} else {
match.creationMatch = 'partial';
// CBOR auxdata could be somewhere else in the bytecode
const cborAuxdataPositions = await generateCborAuxdataPositions();
const allCborAuxdataHaveMetadataHash = Object.values(
cborAuxdataPositions,
).every(({ offset, value }) => {
const cborAuxdataExtracted = recompiledCreationBytecode.slice(
offset * 2,
offset * 2 + value.length,
);
// REMEMBER! CBORAuxdata !== metadata hash. We need the metadata hash. endsWithMetadataHash checks the metadata hash
return endsWithMetadataHash(cborAuxdataExtracted);
});
match.creationMatch = allCborAuxdataHaveMetadataHash
? 'perfect'
: 'partial';
}
logDebug('Found creation match', {
chainId: match.chainId,
Expand All @@ -627,12 +642,7 @@ export async function matchWithCreationTx(

// We call generateCborAuxdataPositions only here because in the case of double auxdata it will
// trigger a second compilation. We don't want to run the compiler twice if not strictly needed
const cborAuxdataPositions = await generateCborAuxdataPositions().catch(
(error) => {
logError('cannot generate contract artifacts', error);
throw new Error('cannot generate contract artifacts');
},
);
const cborAuxdataPositions = await generateCborAuxdataPositions();

// We use normalizeBytecodesAuxdata to replace all the auxdatas in both bytecodes with zeros
const {
Expand Down
35 changes: 35 additions & 0 deletions packages/lib-sourcify/test/sources/CBORInTheMiddle/artifact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "getActiveConfig",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "vrf",
"type": "address"
},
{
"internalType": "bytes32",
"name": "key_hash",
"type": "bytes32"
}
],
"internalType": "struct Type.InitiazationObject",
"name": "",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "608060405234801561000f575f80fd5b504662066eee0361009e576040805180820182525f8082526020918201528151808301909252735ce8d5a2bc84beb22a398cca51996f7930313d6182527f1770bdc7eec7771f7ba4ffd640f34260d7f095b79c92d34a5b2551d6f6cfd2be908201525b80515f80546001600160a01b0319166001600160a01b039092169190911790556020015160015561013a565b4662aa36a7036100f1576040805180820182525f8082526020918201528151808301909252739ddfaca8183c41ad55329bdeed9f6a8d53168b1b82525f805160206101f383398151915290820152610072565b6040805180820182525f8082526020918201528151808301909252735ce8d5a2bc84beb22a398cca51996f7930313d6182525f805160206101f383398151915290820152610072565b60ad806101465f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c8063addfcae814602a575b5f80fd5b6040805180820182525f80825260209182018190528251808401845290546001600160a01b0316808252600154918301918252835190815290519181019190915281519081900390910190f3fea2646970667358221220f5303c4f93b1dff221b6f1cf6e7aa933e980fec92e0c1d743085a8c7353b752164736f6c634300081a0033787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"compiler":{"version":"0.8.26+commit.8a97fa7a"},"language":"Solidity","output":{"abi":[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"getActiveConfig","outputs":[{"components":[{"internalType":"address","name":"vrf","type":"address"},{"internalType":"bytes32","name":"key_hash","type":"bytes32"}],"internalType":"struct Type.InitiazationObject","name":"","type":"tuple"}],"stateMutability":"view","type":"function"}],"devdoc":{"kind":"dev","methods":{},"version":1},"userdoc":{"kind":"user","methods":{},"version":1}},"settings":{"compilationTarget":{"contracts/CBORInTheMiddle.sol":"DeploymentHelper"},"evmVersion":"cancun","libraries":{},"metadata":{"bytecodeHash":"ipfs"},"optimizer":{"enabled":true,"runs":200},"remappings":[]},"sources":{"contracts/CBORInTheMiddle.sol":{"keccak256":"0x2718b880b03105e97950bd1cbb1134720edc5187ebdb9440b2cb3753d929b295","license":"MIT","urls":["bzz-raw://f97208d403360b74cb5d7b2fb3f4f7ba74dc4a7d75aa938bc01bef3343454454","dweb:/ipfs/QmXzbTMxEBzmthqQ74XNjK7sqUVMP1YcJDrvhtzzpWU4Lg"]}},"version":1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

interface Type {
struct InitiazationObject{
address vrf;
bytes32 key_hash;
}
}

contract DeploymentHelper {

address constant ARB_VRF = 0x5CE8D5A2BC84beb22a398CCA51996F7930313D61;
address constant SEPOLIA_VRF = 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B;
bytes32 constant SEPOLIA_HASH = 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae;
bytes32 constant ARB_HASH = 0x1770bdc7eec7771f7ba4ffd640f34260d7f095b79c92d34a5b2551d6f6cfd2be;

Type.InitiazationObject private activeIntializationObJect;

constructor() {
if (block.chainid == 421614) {
activeIntializationObJect = getArbitriumConfig();
} else if (block.chainid == 11155111) {
activeIntializationObJect = getSepoliaConfig();
} else {
activeIntializationObJect = getAnvilConfig();
}
}

function getArbitriumConfig() internal pure returns (Type.InitiazationObject memory) {
return Type.InitiazationObject({vrf: ARB_VRF, key_hash: ARB_HASH});
}

function getSepoliaConfig() internal pure returns (Type.InitiazationObject memory) {
return Type.InitiazationObject({vrf: SEPOLIA_VRF, key_hash: SEPOLIA_HASH});
}

function getAnvilConfig() internal pure returns (Type.InitiazationObject memory) {
return Type.InitiazationObject({vrf: ARB_VRF, key_hash: SEPOLIA_HASH});
}

function getActiveConfig() external view returns (Type.InitiazationObject memory) {
return activeIntializationObJect;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"abi": [
{
"inputs": [
{
"internalType": "contract IEntryPoint",
"name": "_entryPoint",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "accountImplementation",
"outputs": [
{
"internalType": "contract SimpleAccount",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "salt",
"type": "uint256"
}
],
"name": "createAccount",
"outputs": [
{
"internalType": "contract SimpleAccount",
"name": "ret",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "salt",
"type": "uint256"
}
],
"name": "getAddress",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x60a060405234801561001057600080fd5b50604051610a0b380380610a0b83398101604081905261002f9161005d565b6040518060600160405280602f81526020016109dc602f91396100519061008d565b60601c608052506100c4565b60006020828403121561006f57600080fd5b81516001600160a01b038116811461008657600080fd5b9392505050565b805160208201516001600160601b031980821692919060148310156100bc5780818460140360031b1b83161693505b505050919050565b6080516108f06100ec60003960008181604b01528181610114015261025801526108f06000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806311464fbe146100465780635fbfb9cf146100965780638cb84e18146100a9575b600080fd5b61006d7f000000000000000000000000000000000000000000000000000000000000000081565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390f35b61006d6100a436600461039d565b6100bc565b61006d6100b736600461039d565b6101ee565b6000806100c984846101ee565b905073ffffffffffffffffffffffffffffffffffffffff81163b80156100f1575090506101e8565b60405173ffffffffffffffffffffffffffffffffffffffff8616602482015284907f000000000000000000000000000000000000000000000000000000000000000090604401604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529181526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fc4d66de800000000000000000000000000000000000000000000000000000000179052516101b790610390565b6101c2929190610406565b8190604051809103906000f59050801580156101e2573d6000803e3d6000fd5b50925050505b92915050565b60006103578260001b6040518060200161020790610390565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe082820381018352601f90910116604081905273ffffffffffffffffffffffffffffffffffffffff871660248201527f000000000000000000000000000000000000000000000000000000000000000090604401604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0818403018152918152602080830180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fc4d66de800000000000000000000000000000000000000000000000000000000179052905161030093929101610406565b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529082905261033c9291602001610474565b6040516020818303038152906040528051906020012061035e565b9392505050565b60006103578383306000604051836040820152846020820152828152600b8101905060ff815360559020949350505050565b610417806104a483390190565b600080604083850312156103b057600080fd5b823573ffffffffffffffffffffffffffffffffffffffff811681146103d457600080fd5b946020939093013593505050565b60005b838110156103fd5781810151838201526020016103e5565b50506000910152565b73ffffffffffffffffffffffffffffffffffffffff8316815260406020820152600082518060408401526104418160608501602087016103e2565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe016919091016060019392505050565b600083516104868184602088016103e2565b83519083019061049a8183602088016103e2565b0194935050505056fe608060405260405161041738038061041783398101604081905261002291610268565b61002c8282610033565b5050610352565b61003c82610092565b6040516001600160a01b038316907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a280511561008657610081828261010e565b505050565b61008e610185565b5050565b806001600160a01b03163b6000036100cd57604051634c9c8ce360e01b81526001600160a01b03821660048201526024015b60405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc80546001600160a01b0319166001600160a01b0392909216919091179055565b6060600080846001600160a01b03168460405161012b9190610336565b600060405180830381855af49150503d8060008114610166576040519150601f19603f3d011682016040523d82523d6000602084013e61016b565b606091505b50909250905061017c8583836101a6565b95945050505050565b34156101a45760405163b398979f60e01b815260040160405180910390fd5b565b6060826101bb576101b682610205565b6101fe565b81511580156101d257506001600160a01b0384163b155b156101fb57604051639996b31560e01b81526001600160a01b03851660048201526024016100c4565b50805b9392505050565b8051156102155780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b634e487b7160e01b600052604160045260246000fd5b60005b8381101561025f578181015183820152602001610247565b50506000910152565b6000806040838503121561027b57600080fd5b82516001600160a01b038116811461029257600080fd5b60208401519092506001600160401b03808211156102af57600080fd5b818501915085601f8301126102c357600080fd5b8151818111156102d5576102d561022e565b604051601f8201601f19908116603f011681019083821181831017156102fd576102fd61022e565b8160405282815288602084870101111561031657600080fd5b610327836020830160208801610244565b80955050505050509250929050565b60008251610348818460208701610244565b9190910192915050565b60b7806103606000396000f3fe6080604052600a600c565b005b60186014601a565b605e565b565b600060597f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5473ffffffffffffffffffffffffffffffffffffffff1690565b905090565b3660008037600080366000845af43d6000803e808015607c573d6000f35b3d6000fdfea2646970667358221220d7f23a80daebb5531c9e4a18d87e812fca112e5df7e56433218edcc12bbe415d64736f6c63430008170033a26469706673582212209976e0e7134780ae402f487f50b0281c3a9991c10a93856ecd91b30a7205784364736f6c6343000817003330783032393737366236353861424443633431653235313131304536466333366239643531374230343323636f6465"
}
Loading