1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-19 00:29:03 +00:00

style(python): use new-type annotations

This commit is contained in:
Ondřej Vejpustek 2025-04-02 19:28:30 +02:00
parent 5176d5950c
commit c0660ef34e
3 changed files with 49 additions and 46 deletions

View File

@ -1,7 +1,8 @@
from __future__ import annotations
import secrets
from dataclasses import dataclass
from hashlib import sha256
from typing import Optional
import ecdsa
@ -9,8 +10,8 @@ import ecdsa
@dataclass
class AntiExfilSignature:
signature: bytes
entropy: Optional[bytes]
nonce_commitment: Optional[bytes]
entropy: bytes | None
nonce_commitment: bytes | None
def generate_entropy() -> bytes:
@ -43,9 +44,9 @@ def tweak_nonce(nonce_commitment: bytes, entropy: bytes) -> bytes:
def verify(
public_key: Optional[bytes],
public_key: bytes | None,
signature: bytes,
digest: Optional[bytes],
digest: bytes | None,
entropy: bytes,
nonce_commitment: bytes,
) -> bool:

View File

@ -193,15 +193,15 @@ def get_ownership_proof_common(
client: "TrezorClient",
coin_name: str,
n: "Address",
multisig: Optional[messages.MultisigRedeemScriptType],
multisig: messages.MultisigRedeemScriptType | None,
script_type: messages.InputScriptType,
user_confirmation: bool,
ownership_ids: Optional[List[bytes]],
commitment_data: Optional[bytes],
ownership_ids: List[bytes] | None,
commitment_data: bytes | None,
preauthorized: bool,
use_anti_exfil: bool,
entropy: Optional[bytes],
) -> Tuple[Optional[bytes], AntiExfilSignature]:
entropy: bytes | None,
) -> Tuple[bytes | None, AntiExfilSignature]:
if preauthorized:
client.call(messages.DoPreauthorized(), expect=messages.PreauthorizedRequest)
@ -297,14 +297,14 @@ def get_ownership_proof_new(
client: "TrezorClient",
coin_name: str,
n: "Address",
multisig: Optional[messages.MultisigRedeemScriptType] = None,
multisig: messages.MultisigRedeemScriptType | None = None,
script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS,
user_confirmation: bool = False,
ownership_ids: Optional[List[bytes]] = None,
commitment_data: Optional[bytes] = None,
ownership_ids: List[bytes] | None = None,
commitment_data: bytes | None = None,
preauthorized: bool = False,
use_anti_exfil: bool = True,
entropy: Optional[bytes] = None,
entropy: bytes | None = None,
) -> AntiExfilSignature:
"""
If `use_anti_exfil` is set to `True`, the anti-exfilitration protocol will be
@ -384,16 +384,16 @@ def sign_tx_common(
coin_name: str,
inputs: Sequence[messages.TxInputType],
outputs: Sequence[messages.TxOutputType],
details: Optional[messages.SignTx],
prev_txes: Optional["TxCacheType"],
details: messages.SignTx | None,
prev_txes: TxCacheType | None,
payment_reqs: Sequence[messages.TxAckPaymentRequest],
preauthorized: bool,
unlock_path: Optional[List[int]],
unlock_path_mac: Optional[bytes],
unlock_path: List[int] | None,
unlock_path_mac: bytes | None,
use_anti_exfil: bool,
entropy_list: Optional[List[bytes]],
entropy_list: List[bytes] | None,
**kwargs: Any,
) -> Tuple[Sequence[Optional[AntiExfilSignature]], bytes]:
) -> Tuple[Sequence[AntiExfilSignature | None], bytes]:
if prev_txes is None:
prev_txes = {}
@ -455,7 +455,7 @@ def sign_tx_common(
R = messages.RequestType
nonce_commitment_list: List[Optional[bytes]] = [None for _ in inputs]
nonce_commitment_list: List[bytes | None] = [None for _ in inputs]
if use_anti_exfil and entropy_list is None:
entropy_list = [generate_entropy() for _ in inputs]
@ -638,16 +638,16 @@ def sign_tx_new(
coin_name: str,
inputs: Sequence[messages.TxInputType],
outputs: Sequence[messages.TxOutputType],
details: Optional[messages.SignTx] = None,
prev_txes: Optional["TxCacheType"] = None,
details: messages.SignTx | None = None,
prev_txes: TxCacheType | None = None,
payment_reqs: Sequence[messages.TxAckPaymentRequest] = (),
preauthorized: bool = False,
unlock_path: Optional[List[int]] = None,
unlock_path_mac: Optional[bytes] = None,
unlock_path: List[int] | None = None,
unlock_path_mac: bytes | None = None,
use_anti_exfil: bool = True,
entropy_list: Optional[List[bytes]] = None,
entropy_list: List[bytes] | None = None,
**kwargs: Any,
) -> Sequence[Optional[AntiExfilSignature]]:
) -> Sequence[AntiExfilSignature | None]:
"""Sign a Bitcoin-like transaction.
Returns a list of `AntiExfilSignature` objects (one for each provided input).

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import re
from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Tuple
@ -200,14 +202,14 @@ def sign_tx_common(
gas_limit: int,
to: str,
value: int,
data: Optional[bytes],
chain_id: Optional[int],
tx_type: Optional[int],
definitions: Optional[messages.EthereumDefinitions],
data: bytes | None,
chain_id: int | None,
tx_type: int | None,
definitions: messages.EthereumDefinitions | None,
chunkify: bool,
use_anti_exfil: bool,
entropy: Optional[bytes],
) -> Tuple[Optional[int], bytes, bytes, Optional[bytes], Optional[bytes]]:
entropy: bytes | None,
) -> Tuple[int | None, bytes, bytes, bytes | None, bytes | None]:
if chain_id is None:
raise exceptions.TrezorException("Chain ID cannot be undefined")
@ -332,13 +334,13 @@ def sign_tx_new(
gas_limit: int,
to: str,
value: int,
data: Optional[bytes] = None,
chain_id: Optional[int] = None,
tx_type: Optional[int] = None,
definitions: Optional[messages.EthereumDefinitions] = None,
data: bytes | None = None,
chain_id: int | None = None,
tx_type: int | None = None,
definitions: messages.EthereumDefinitions | None = None,
chunkify: bool = False,
use_anti_exfil: bool = True,
entropy: Optional[bytes] = None,
entropy: bytes | None = None,
) -> AntiExfilSignature:
"""
If `use_anti_exfil` is set to `True`, the anti-exfilitration protocol will be
@ -385,12 +387,12 @@ def sign_tx_eip1559_common(
chain_id: int,
max_gas_fee: int,
max_priority_fee: int,
access_list: Optional[List[messages.EthereumAccessList]],
definitions: Optional[messages.EthereumDefinitions],
access_list: List[messages.EthereumAccessList] | None,
definitions: messages.EthereumDefinitions | None,
chunkify: bool,
use_anti_exfil: bool,
entropy: Optional[bytes] = None,
) -> Tuple[Optional[int], bytes, bytes, Optional[bytes], Optional[bytes]]:
entropy: bytes | None = None,
) -> Tuple[int | None, bytes, bytes, bytes | None, bytes | None]:
if use_anti_exfil:
if entropy is None:
entropy = generate_entropy()
@ -419,7 +421,7 @@ def sign_tx_eip1559_common(
response = client.call(msg)
assert isinstance(response, messages.EthereumTxRequest)
nonce_commitment: Optional[bytes] = None
nonce_commitment: bytes | None = None
while True:
if response.data_length is not None:
@ -517,11 +519,11 @@ def sign_tx_eip1559_new(
chain_id: int,
max_gas_fee: int,
max_priority_fee: int,
access_list: Optional[List[messages.EthereumAccessList]] = None,
definitions: Optional[messages.EthereumDefinitions] = None,
access_list: List[messages.EthereumAccessList] | None = None,
definitions: messages.EthereumDefinitions | None = None,
chunkify: bool = False,
use_anti_exfil: bool = True,
entropy: Optional[bytes] = None,
entropy: bytes | None = None,
) -> AntiExfilSignature:
"""
If `use_anti_exfil` is set to `True`, the anti-exfilitration protocol will be