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

Metadata hash extractor #212

Merged
merged 4 commits into from
Aug 16, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test:signatures": "./test/signatures/test-sigs.bash",
"test:e2e": "hardhat test test/e2e/*.ts",
"test:update": "yarn run test:signatures || yarn run test:storage",
"metadatahash": "yarn build:all && hardhat run scripts/printMetadataHashes.ts",
"upload-4bytes": "forge build && find ./out -type f -name \"*.json\" -exec cast upload-signature {} + | grep -v Duplicated:",
"postinstall": "patch-package",
"deploy-factory": "hardhat run scripts/deployment.ts",
Expand Down
90 changes: 90 additions & 0 deletions scripts/printMetadataHashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import path from 'path'
import fs from 'fs-extra'
import hre from 'hardhat'
import { execSync } from 'child_process'

main()
.then(() => process.exit(0))
.catch((error: Error) => {
console.error(error)
process.exit(1)
})

async function main() {
const contracts: string[] = [
'Inbox',
'Outbox',
'SequencerInbox',
'Bridge',
'ERC20Inbox',
'ERC20Outbox',
'SequencerInbox',
'ERC20Bridge',
'RollupProxy',
'RollupAdminLogic',
'RollupUserLogic',
'ChallengeManager',
]

// Print the current git tag
const gitTag = execSync('git describe --tags').toString().trim()
console.log(`Current tag: ${gitTag}`)

// Check if yarn packages match yarn.lock
try {
execSync('yarn install --check-files', { stdio: 'ignore' })
} catch (e) {
console.error('Yarn packages does not match yarn.lock')
process.exit(1)
}

// Check if the current working directory is clean
try {
execSync('git update-index --really-refresh', { stdio: 'ignore' })
if (execSync('git status --porcelain').toString().trim()) {
console.error('The current working directory have staged changes.')
process.exit(1)
}
} catch (e) {
console.error('The current working directory is not clean.')
process.exit(1)
}

console.log('HARDHAT:')
for (const contract of contracts) {
const hash = await _getHardhatMetadataHash(contract)
console.log(`${contract}: ${hash}`)
}

console.log('\nFOUNDRY:')
for (const contract of contracts) {
const hash = await _getFoundryMetadataHash(contract)
console.log(`${contract}: ${hash}`)
}
}

async function _getHardhatMetadataHash(contractName: string): Promise<string> {
const artifact = await hre.artifacts.readArtifact(contractName)
return _extractMetadataHash(artifact.bytecode)
}

async function _getFoundryMetadataHash(contractName: string): Promise<string> {
const artifactPath = path.join(
'out',
`${contractName}.sol`,
`${contractName}.json`
)
const artifact = await fs.readJson(artifactPath)
return _extractMetadataHash(artifact.bytecode.object)
}

function _extractMetadataHash(bytecode: string): string {
const metadataPattern = /a264697066735822([a-fA-F0-9]{64})/
const matches = bytecode.match(metadataPattern)

if (matches && matches.length > 1) {
return matches[1]
} else {
throw new Error('No metadata hash found in bytecode')
}
}
Loading