22"""
33This script checks files for potential Web3 private keys.
44"""
5+ from __future__ import annotations
56
67import argparse
78import os
89import re
910import sys
10- from typing import Sequence
11+ from collections . abc import Sequence
1112
1213from eth_account import Account
1314from eth_utils import decode_hex
1415
1516# Regular expression to match Ethereum private keys
16- KEY_PATTERN = re .compile (r" \b(0x)?[a-fA-F0-9]{64}\b" )
17- IGNORE_COMMENT = " # web3-private-key-ok"
17+ KEY_PATTERN = re .compile (r' \b(0x)?[a-fA-F0-9]{64}\b' )
18+ IGNORE_COMMENT = ' # web3-private-key-ok'
1819
1920
2021def is_private_key_valid (private_key_hex : str ) -> bool :
2122 try :
2223 # Remove hex prefix if present
23- if private_key_hex .startswith ("0x" ):
24+ if private_key_hex .startswith ('0x' ):
2425 private_key_hex = private_key_hex [2 :]
2526
2627 # Decode the hex string to bytes
@@ -41,14 +42,14 @@ def scan_file(file_path: str) -> bool:
4142 """
4243 detected = False
4344 try :
44- with open (file_path , "r" , encoding = " utf-8" , errors = " ignore" ) as f :
45+ with open (file_path , encoding = ' utf-8' , errors = ' ignore' ) as f :
4546 for idx , line in enumerate (f ):
4647 match = KEY_PATTERN .search (line )
4748 if match and IGNORE_COMMENT not in line :
4849 private_key_hex = match .group (0 )
4950 if is_private_key_valid (private_key_hex ):
5051 print (
51- f"Error: Valid Web3 private key detected in { file_path } :{ idx + 1 } "
52+ f"Error: Valid Web3 private key detected in { file_path } :{ idx + 1 } " ,
5253 )
5354 detected = True
5455 except Exception as e :
@@ -58,7 +59,7 @@ def scan_file(file_path: str) -> bool:
5859
5960def main (argv : Sequence [str ] | None = None ) -> None :
6061 parser = argparse .ArgumentParser ()
61- parser .add_argument (" filenames" , nargs = "*" , help = " Filenames to check" )
62+ parser .add_argument (' filenames' , nargs = '*' , help = ' Filenames to check' )
6263 args = parser .parse_args (argv )
6364
6465 files_with_keys = []
@@ -75,5 +76,5 @@ def main(argv: Sequence[str] | None = None) -> None:
7576 sys .exit (0 )
7677
7778
78- if __name__ == " __main__" :
79+ if __name__ == ' __main__' :
7980 main ()
0 commit comments