[fix] bandit assert warnings (#3243)
Co-authored-by: dni ⚡ <office@dnilabs.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user