1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-28 03:38:46 +00:00

perf(core): remove imports from hot path

[no changelog]
This commit is contained in:
Roman Zeyde 2025-04-10 09:14:00 +03:00 committed by Roman Zeyde
parent 3c08ce62b3
commit 07ad01220c
8 changed files with 18 additions and 33 deletions

View File

@ -2,17 +2,18 @@ from typing import TYPE_CHECKING
from trezor.crypto import base58
from trezor.crypto.hashlib import sha256
from trezor.enums import InputScriptType
from trezor.utils import HashWriter
from trezor.wire import ProcessError
from apps.common import address_type
from .common import ecdsa_hash_pubkey, encode_bech32_address
from .multisig import multisig_get_pubkeys, multisig_pubkey_index
from .scripts import output_script_native_segwit, write_output_script_multisig
if TYPE_CHECKING:
from trezor.crypto import bip32
from trezor.enums import InputScriptType
from trezor.messages import MultisigRedeemScriptType
from apps.common.coininfo import CoinInfo
@ -24,10 +25,6 @@ def get_address(
node: bip32.HDNode,
multisig: MultisigRedeemScriptType | None = None,
) -> str:
from trezor.enums import InputScriptType
from .multisig import multisig_get_pubkeys, multisig_pubkey_index
node_public_key = node.public_key() # result_cache
if multisig:

View File

@ -1,7 +1,10 @@
from micropython import const
from typing import TYPE_CHECKING
from .common import BIP32_WALLET_DEPTH
from trezor import utils
from .common import BIP32_WALLET_DEPTH, CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
from .writers import write_bytes_prefixed
if TYPE_CHECKING:
from trezor.messages import (
@ -21,10 +24,6 @@ class CoinJoinAuthorization:
self.params = params
def check_get_ownership_proof(self, msg: GetOwnershipProof) -> bool:
from trezor import utils
from .writers import write_bytes_prefixed
params = self.params # local_cache_attribute
# Check whether the current params matches the parameters of the request.
@ -47,8 +46,6 @@ class CoinJoinAuthorization:
)
def check_internal_output(self, txo: TxOutput) -> bool:
from .common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
# Check whether the output matches the parameters of the request.
return (
len(txo.address_n) >= BIP32_WALLET_DEPTH

View File

@ -5,7 +5,7 @@ from trezor.wire import DataError, ProcessError
from apps.common import safety_checks
from ..common import input_is_external_unverified
from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES, input_is_external_unverified
from ..keychain import validate_path_against_script_type
from . import helpers, tx_weight
from .tx_info import OriginalTxInfo
@ -169,8 +169,6 @@ class BasicApprover(Approver):
raise ProcessError("Transaction has changed during signing")
async def _add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if txo.address_n and not validate_path_against_script_type(
self.coin,
address_n=txo.address_n,

View File

@ -1,9 +1,14 @@
from typing import TYPE_CHECKING
from trezor.enums import InputScriptType
from trezor.messages import TxOutput
from ..common import BIP32_WALLET_DEPTH, CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if TYPE_CHECKING:
from typing import Any, Generic, TypeVar
from trezor.messages import TxInput, TxOutput
from trezor.messages import TxInput
from apps.common.paths import Bip32Path
@ -86,8 +91,6 @@ class MatchChecker(Generic[T]):
class WalletPathChecker(MatchChecker):
def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any:
from ..common import BIP32_WALLET_DEPTH
if len(txio.address_n) <= BIP32_WALLET_DEPTH:
return None
return txio.address_n[:-BIP32_WALLET_DEPTH]
@ -113,11 +116,6 @@ class MultisigFingerprintChecker(MatchChecker):
class ScriptTypeChecker(MatchChecker):
def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any:
from trezor.enums import InputScriptType
from trezor.messages import TxOutput
from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if TxOutput.is_type_of(txio):
script_type = CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[txio.script_type]
else:

View File

@ -3,7 +3,9 @@ from typing import TYPE_CHECKING
from ..writers import (
TX_HASH_SIZE,
write_bytes_fixed,
write_bytes_prefixed,
write_bytes_reversed,
write_tx_output,
write_uint32,
write_uint64,
)
@ -59,8 +61,6 @@ class BitcoinSigHasher:
self.h_outputs = HashWriter(sha256())
def add_input(self, txi: TxInput, script_pubkey: bytes) -> None:
from ..writers import write_bytes_prefixed
write_bytes_reversed(self.h_prevouts, txi.prev_hash, TX_HASH_SIZE)
write_uint32(self.h_prevouts, txi.prev_index)
write_uint64(self.h_amounts, txi.amount)
@ -68,8 +68,6 @@ class BitcoinSigHasher:
write_uint32(self.h_sequences, txi.sequence)
def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
from ..writers import write_tx_output
write_tx_output(self.h_outputs, txo, script_pubkey)
def hash143(

View File

@ -14,6 +14,8 @@ from apps.common.writers import ( # noqa: F401
write_uint64_le,
)
from .multisig import multisig_fingerprint
if TYPE_CHECKING:
from trezor.messages import PrevInput, PrevOutput, TxInput, TxOutput
from trezor.utils import HashWriter
@ -40,8 +42,6 @@ def write_tx_input(w: Writer, i: TxInput | PrevInput, script: bytes) -> None:
def write_tx_input_check(w: Writer, i: TxInput) -> None:
from .multisig import multisig_fingerprint
write_uint32(w, len(i.address_n))
for n in i.address_n:
write_uint32(w, n)

View File

@ -128,8 +128,6 @@ def convertbits(
def decode(hrp: str, addr: str) -> OptionalTuple2[int, bytes]:
"""Decode a segwit address."""
from trezorcrypto import bech32
try:
hrpgot, data, spec = bech32.decode(addr)
decoded = bytes(convertbits(data[1:], 5, 8, False))

View File

@ -5,6 +5,7 @@ from storage.cache_common import DataCache, InvalidSessionError
from trezor import log, protobuf
from trezor.wire.codec import codec_v1
from trezor.wire.context import UnexpectedMessageException
from trezor.wire.message_handler import wrap_protobuf_load
from trezor.wire.protocol_common import Context, Message
if TYPE_CHECKING:
@ -69,8 +70,6 @@ class CodecContext(Context):
)
# look up the protobuf class and parse the message
from ..message_handler import wrap_protobuf_load
return wrap_protobuf_load(msg.data, expected_type)
async def write(self, msg: protobuf.MessageType) -> None: