mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +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.
|
||||
"""
|
||||
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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue