Skip to content

Commit

Permalink
chore: Add pre-commit hook for detecting private keys in staged files
Browse files Browse the repository at this point in the history
  • Loading branch information
Big-Aaron committed Aug 19, 2024
1 parent e801987 commit 5c0d1c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- repo: local
hooks:
- id: detect-private-key
name: Detect Private Key
entry: script/detect_private_key.sh
language: script
types: [text]
25 changes: 25 additions & 0 deletions script/detect_private_key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# https://gist.github.com/suhailkakar/1f29265dbe843bfa85227c34ed063e86
# https://x.com/SuhailKakar/status/1825142639085109397

chmod +x script/detect_private_key.sh

# Define patterns for Ethereum and Solana private keys
ETH_PATTERN="(0x)?[A-Fa-f0-9]{64}"
SOL_PATTERN="^[1-9A-HJ-NP-Za-km-z]{88}$"

# Check for private keys in staged files
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|sol|py|sh|txt|json)$')
if [ -z "$FILES" ]; then
exit 0
fi

for FILE in $FILES; do
if grep -Eq "$ETH_PATTERN" "$FILE" || grep -Eq "$SOL_PATTERN" "$FILE"; then
echo "Error: Detected a potential private key in $FILE"
exit 1
fi
done

exit 0
33 changes: 33 additions & 0 deletions script/generate_abi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
chmod +x script/generate_abi.sh
if command -v forge > /dev/null; then
echo "forge command has found"
else
curl -L https://foundry.paradigm.xyz | bash
echo "forge command has installed"
source ~/.bashrc
foundryup
echo "forge command has started"
fi

forge build --force

if [ ! -d "abi" ]; then
mkdir abi
fi

if [ ! -d "flattenContracts" ]; then
mkdir flattenContracts
fi

# Loop through all .sol files in the src directory
for solfile in src/*.sol; do
# Get the base name of the .sol file (without directory and extension)
base=$(basename "$solfile" .sol)

# Flatten the .sol file and generate the ABI
forge flatten "$solfile" -o "flattenContracts/${base}_flattened.sol"
forge inspect "flattenContracts/${base}_flattened.sol:$base" abi > "abi/${base}.json"
done

echo "ABI files have generated"

0 comments on commit 5c0d1c3

Please sign in to comment.