[fix] bandit assert warnings (#3243)

Co-authored-by: dni  <office@dnilabs.com>
This commit is contained in:
Vlad Stan
2025-07-15 11:14:23 +02:00
committed by dni ⚡
co-authored by dni ⚡
parent 76ecf113c3
commit fa89313e6f
28 changed files with 196 additions and 220 deletions
+2 -1
View File
@@ -66,7 +66,8 @@ class AESCipher:
self, salt: bytes, output_len: int = 32 + 16
) -> tuple[bytes, bytes]:
# extended from https://gist.github.com/gsakkis/4546068
assert len(salt) == 8, "Salt must be 8 bytes"
if len(salt) != 8:
raise ValueError("Salt must be 8 bytes")
data = self.key + salt
key = md5(data).digest()
final_key = key
+8 -4
View File
@@ -175,14 +175,17 @@ def normalize_private_key(key: str) -> str:
def normalize_bech32_key(hrp: str, key: str) -> str:
if key.startswith(hrp):
_, decoded_data = bech32_decode(key)
assert decoded_data, f"Key is not valid {hrp}."
if not decoded_data:
raise ValueError(f"Key is not valid {hrp}.")
decoded_data_bits = convertbits(decoded_data, 5, 8, False)
assert decoded_data_bits, f"Key is not valid {hrp}."
if not decoded_data_bits:
raise ValueError(f"Key is not valid {hrp}.")
return bytes(decoded_data_bits).hex()
assert len(key) == 64, "Key has wrong length."
if len(key) != 64:
raise ValueError("Key has wrong length.")
try:
int(key, 16)
except Exception as exc:
@@ -203,7 +206,8 @@ def hex_to_npub(hex_pubkey: str) -> str:
normalize_public_key(hex_pubkey)
pubkey_bytes = bytes.fromhex(hex_pubkey)
bits = convertbits(pubkey_bytes, 8, 5, True)
assert bits
if not bits:
raise ValueError("Invalid Nostr public key.")
return bech32_encode("npub", bits)