ਟੈਕਸਟ ਆਕਾਰ
Init code hash ਮੂਲ ਰੂਪ ਵਿੱਚ ਕੰਟਰੈਕਟ ਦੇ ਬਾਈਟਕੋਡ ਦਾ keccak256 ਹੈਸ਼ ਹੁੰਦਾ ਹੈ।
Init Code ਦੀ ਵਰਤੋਂ
Init code hash ਦੀ ਲੋੜ ਹੁੰਦੀ ਹੈ "ਕਿਉਂਕਿ ਐਡਰੈੱਸ ਗਣਨਾ init code ਨੂੰ ਹੈਸ਼ ਕਰਨ 'ਤੇ ਨਿਰਭਰ ਕਰਦੀ ਹੈ। Init code ਉਹ ਕੋਡ ਹੁੰਦਾ ਹੈ ਜੋ, ਐਗਜ਼ੀਕਿਊਟ ਹੋਣ 'ਤੇ, ਉਹ ਰਨਟਾਈਮ ਬਾਈਟਕੋਡ ਪੈਦਾ ਕਰਦਾ ਹੈ ਜੋ ਸਟੇਟ ਵਿੱਚ ਰੱਖਿਆ ਜਾਵੇਗਾ, ਅਤੇ ਜੋ ਆਮ ਤੌਰ 'ਤੇ ਹਾਈ ਲੈਵਲ ਭਾਸ਼ਾਵਾਂ ਦੁਆਰਾ 'ਕੰਸਟ੍ਰਕਟਰ' ਲਾਗੂ ਕਰਨ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ।"
"EIP-1014: Skinny CREATE2," Ethereum Improvement Proposals, no. 1014, April 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1014.
Init Code ਦੀ ਵਰਤੋਂ
Init code ਦੀ ਮੁੱਖ ਵਰਤੋਂ ਐਡਰੈੱਸ ਮਾਈਨਿੰਗ ਲਈ ਹੈ। ਤੁਸੀਂ https://github.com/0age/create2crunch ਵੇਖ ਸਕਦੇ ਹੋ ਕਿ ਅਸੀਂ CREATE2 ਅਤੇ ਇੱਕ ਕੰਟਰੈਕਟ ਦੇ init code ਦੀ ਵਰਤੋਂ ਕਿਵੇਂ ਕਰ ਸਕਦੇ ਹਾਂ ਤਾਂ ਜੋ ਕੰਟਰੈਕਟ ਡਿਪਲੌਇਮੈਂਟ ਲਈ ਵਧੇਰੇ ਕੁਸ਼ਲ ਐਡਰੈੱਸ 'ਮਾਈਨ' ਕੀਤੇ ਜਾ ਸਕਣ।
ਇੱਥੇ ਇੱਕ ਸਧਾਰਨ bash ਸਕ੍ਰਿਪਟ ਹੈ ਜੋ ਤੁਹਾਡੇ ਲਈ forge ਦੀ ਵਰਤੋਂ ਕਰਕੇ code init hash ਪੈਦਾ ਕਰਦੀ ਹੈ:
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."# ਕਦਮ 4: init ਕੋਡ ਹੈਸ਼ ਨੂੰ ਇੱਕ ਫਾਈਲ ਵਿੱਚ ਲਿਖੋ
OUTPUT_FILE="${CONTRACT_NAME}_INIT_CODE_HASH.txt"
echo "$INIT_CODE_HASH" > "$OUTPUT_FILE" || exit_on_error "init ਕੋਡ ਹੈਸ਼ ਨੂੰ ਫਾਈਲ ਵਿੱਚ ਲਿਖਣ ਵਿੱਚ ਅਸਫਲ।"
# ਅਸਥਾਈ ਫਾਈਲਾਂ ਨੂੰ ਸਾਫ਼ ਕਰੋ (ਜੇ ਕੋਈ ਹੋਵੇ)
# (ਜੇ ਸਾਫ਼ ਕਰਨ ਲਈ ਕੋਈ ਅਸਥਾਈ ਫਾਈਲਾਂ ਹੋਣ ਤਾਂ ਇੱਥੇ ਕਮਾਂਡਾਂ ਸ਼ਾਮਲ ਕਰੋ)
# ਕਦਮ 5: ਨਤੀਜਾ ਆਊਟਪੁੱਟ ਕਰੋ
echo "Init ਕੋਡ ਹੈਸ਼ $OUTPUT_FILE ਵਿੱਚ ਲਿਖ ਦਿੱਤਾ ਗਿਆ ਹੈ"
if $VERBOSE; then
echo "Init ਕੋਡ: $INIT_CODE"
echo "Init ਕੋਡ ਹੈਸ਼: $INIT_CODE_HASH"
fi
# ਅਸਲ ਡਾਇਰੈਕਟਰੀ 'ਤੇ ਵਾਪਸ ਜਾਓ
cd - > /dev/null || exit