From 3c0f778a85f2d6ef9504fcd488e7a4a05c60f75b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Feb 2025 10:21:12 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pre_commit_hooks/detect_web3_private_key.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pre_commit_hooks/detect_web3_private_key.py b/pre_commit_hooks/detect_web3_private_key.py index b8a19ac..1bd67b0 100644 --- a/pre_commit_hooks/detect_web3_private_key.py +++ b/pre_commit_hooks/detect_web3_private_key.py @@ -2,25 +2,26 @@ """ This script checks files for potential Web3 private keys. """ +from __future__ import annotations import argparse import os import re import sys -from typing import Sequence +from collections.abc import Sequence from eth_account import Account from eth_utils import decode_hex # Regular expression to match Ethereum private keys -KEY_PATTERN = re.compile(r"\b(0x)?[a-fA-F0-9]{64}\b") -IGNORE_COMMENT = "# web3-private-key-ok" +KEY_PATTERN = re.compile(r'\b(0x)?[a-fA-F0-9]{64}\b') +IGNORE_COMMENT = '# web3-private-key-ok' def is_private_key_valid(private_key_hex: str) -> bool: try: # Remove hex prefix if present - if private_key_hex.startswith("0x"): + if private_key_hex.startswith('0x'): private_key_hex = private_key_hex[2:] # Decode the hex string to bytes @@ -41,14 +42,14 @@ def scan_file(file_path: str) -> bool: """ detected = False 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): match = KEY_PATTERN.search(line) if match and IGNORE_COMMENT not in line: private_key_hex = match.group(0) if is_private_key_valid(private_key_hex): 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 except Exception as e: @@ -58,7 +59,7 @@ def scan_file(file_path: str) -> bool: def main(argv: Sequence[str] | None = None) -> None: 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) files_with_keys = [] @@ -75,5 +76,5 @@ def main(argv: Sequence[str] | None = None) -> None: sys.exit(0) -if __name__ == "__main__": +if __name__ == '__main__': main()