mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 12:34:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
67129881ce
commit
3c0f778a85
1 changed files with 9 additions and 8 deletions
|
|
@ -2,25 +2,26 @@
|
||||||
"""
|
"""
|
||||||
This script checks files for potential Web3 private keys.
|
This script checks files for potential Web3 private keys.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from typing import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
from eth_account import Account
|
from eth_account import Account
|
||||||
from eth_utils import decode_hex
|
from eth_utils import decode_hex
|
||||||
|
|
||||||
# Regular expression to match Ethereum private keys
|
# Regular expression to match Ethereum private keys
|
||||||
KEY_PATTERN = re.compile(r"\b(0x)?[a-fA-F0-9]{64}\b")
|
KEY_PATTERN = re.compile(r'\b(0x)?[a-fA-F0-9]{64}\b')
|
||||||
IGNORE_COMMENT = "# web3-private-key-ok"
|
IGNORE_COMMENT = '# web3-private-key-ok'
|
||||||
|
|
||||||
|
|
||||||
def is_private_key_valid(private_key_hex: str) -> bool:
|
def is_private_key_valid(private_key_hex: str) -> bool:
|
||||||
try:
|
try:
|
||||||
# Remove hex prefix if present
|
# Remove hex prefix if present
|
||||||
if private_key_hex.startswith("0x"):
|
if private_key_hex.startswith('0x'):
|
||||||
private_key_hex = private_key_hex[2:]
|
private_key_hex = private_key_hex[2:]
|
||||||
|
|
||||||
# Decode the hex string to bytes
|
# Decode the hex string to bytes
|
||||||
|
|
@ -41,14 +42,14 @@ def scan_file(file_path: str) -> bool:
|
||||||
"""
|
"""
|
||||||
detected = False
|
detected = False
|
||||||
try:
|
try:
|
||||||
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
with open(file_path, encoding='utf-8', errors='ignore') as f:
|
||||||
for idx, line in enumerate(f):
|
for idx, line in enumerate(f):
|
||||||
match = KEY_PATTERN.search(line)
|
match = KEY_PATTERN.search(line)
|
||||||
if match and IGNORE_COMMENT not in line:
|
if match and IGNORE_COMMENT not in line:
|
||||||
private_key_hex = match.group(0)
|
private_key_hex = match.group(0)
|
||||||
if is_private_key_valid(private_key_hex):
|
if is_private_key_valid(private_key_hex):
|
||||||
print(
|
print(
|
||||||
f"Error: Valid Web3 private key detected in {file_path}:{idx + 1}"
|
f"Error: Valid Web3 private key detected in {file_path}:{idx + 1}",
|
||||||
)
|
)
|
||||||
detected = True
|
detected = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -58,7 +59,7 @@ def scan_file(file_path: str) -> bool:
|
||||||
|
|
||||||
def main(argv: Sequence[str] | None = None) -> None:
|
def main(argv: Sequence[str] | None = None) -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("filenames", nargs="*", help="Filenames to check")
|
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
files_with_keys = []
|
files_with_keys = []
|
||||||
|
|
@ -75,5 +76,5 @@ def main(argv: Sequence[str] | None = None) -> None:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue