Ìwọ̀n ọ̀rọ̀
Init code hash ni basically keccak256 hash ti bytecode àwọn contract.
Lílò Init Code
A nílò init code hash "Nítorí pé ìṣirò àdírẹ́sì dá lórí híháṣì
init code. Init code ni kóòdù tí, nígbà tí a bá ṣe é, yóò mú
runtime bytecode jáde tí wọn yóò fi sínú state, àti èyí tí
àwọn èdè ìpele gíga sábà máa ń lò láti ṣe ìmúṣẹ
'constructor'."
"EIP-1014: Skinny CREATE2," Ethereum Improvement Proposals, no. 1014, April 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1014.
Lílò Init Code
Lílò àkọ́kọ́ ti init code ni fún wíwa àwọn àdírẹ́sì. O lè wo https://github.com/0age/create2crunch fún àpẹẹrẹ bí a ṣe lè lo CREATE2 àti init code contract kan láti 'wa' àwọn àdírẹ́sì tí ó dára jù fún ìgbékalẹ̀ contract.
Èyí ni bash script tí ó rọrùn tí ó ń lo forge láti ṣe code init hash fún ọ:
https://github.com/sambacha/foundry-scripts/tree/master/scripts/deploy
Script
#!/usr/bin/env bash
# Function to exit on error with a message
exit_on_error() {
echo "Error: $1"
exit 1
}
# Function to display usage information
usage() {
echo "Usage: $0 [-v]"
echo " : Path to the Solidity contract file"
echo " : Name of the Solidity contract"
echo " -v : Enable verbose output (optional)"
exit 1
}
# Check if foundry is installed
if ! command -v forge &> /dev/null; then
exit_on_error "Foundry is not installed. Please install it first."
fi
# Check if correct number of arguments is provided
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
usage
fi
# Variables from arguments
CONTRACT_PATH=$1
CONTRACT_NAME=$2
VERBOSE=false
# Check if verbose option is enabled
if [ "$#" -eq 3 ] && [ "$3" == "-v" ]; then
VERBOSE=true
fi
# Validate contract path
if [ ! -f "$CONTRACT_PATH" ]; then
exit_on_error "Contract path '$CONTRACT_PATH' does not exist or is not a file."
fi
# Get the directory of the contract path
CONTRACT_DIR=$(dirname "$CONTRACT_PATH")
# shellcheck disable=SC2034
CONTRACT_FILE=$(basename "$CONTRACT_PATH")
# Change to the contract directory
cd "$CONTRACT_DIR" || exit_on_error "Failed to change directory to '$CONTRACT_DIR'."
# Step 1: Compile the contract
echo "Compiling the contract..."
if $VERBOSE; then
forge build --contracts . || exit_on_error "Failed to compile the contract."
else
forge build --contracts . > /dev/null 2>&1 || exit_on_error "Failed to compile the contract."
fi
# Step 2: Generate the init code
echo "Generating init code..."
INIT_CODE=$(forge inspect "$CONTRACT_NAME" bytecode) || exit_on_error "Failed to generate init code."
# Step 3: Calculate the init code hash
echo "Calculating init code hash..."
INIT_CODE_HASH=$(echo -n "$INIT_CODE" | keccak-256sum | cut -d' ' -f1) || exit_on_error "Failed to calculate init code hash."# Ìgbésẹ̀ 4: Kọ hash kóòdù ìpilẹ̀sẹ̀ sínú fáìlì
OUTPUT_FILE="${CONTRACT_NAME}_INIT_CODE_HASH.txt"
echo "$INIT_CODE_HASH" > "$OUTPUT_FILE" || exit_on_error "Ó kùnà láti kọ hash kóòdù ìpilẹ̀sẹ̀ sínú fáìlì."
# Pàwọn àwọn fáìlì àdánidá (tí ó bá wà)
# (Fi àwọn àṣẹ kún ibí yìí tí àwọn fáìlì àdánidá bá wà láti pàwọn)
# Ìgbésẹ̀ 5: Ṣàfihàn èsì náà
echo "A ti kọ hash kóòdù ìpilẹ̀sẹ̀ sínú $OUTPUT_FILE"
if $VERBOSE; then
echo "Kóòdù ìpilẹ̀sẹ̀: $INIT_CODE"
echo "Hash kóòdù ìpilẹ̀sẹ̀: $INIT_CODE_HASH"
fi
# Padà sí àkójọpọ̀ ìpilẹ̀
cd - > /dev/null || exit