1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-21 23:18:13 +00:00

style(python): extend style checks to more targets

[no changelog]
This commit is contained in:
matejcik 2023-08-15 17:46:31 +02:00 committed by matejcik
parent 821d70dd8d
commit b5c541d8fd
11 changed files with 53 additions and 39 deletions

View File

@ -2,7 +2,7 @@ PYTHON=python3
SETUP=$(PYTHON) setup.py SETUP=$(PYTHON) setup.py
EXCLUDES=.vscode EXCLUDES=.vscode
STYLE_TARGETS=src/trezorlib setup.py STYLE_TARGETS=src tests tools helper-scripts setup.py
all: build all: build
@ -45,7 +45,7 @@ git-clean:
style: style:
black $(STYLE_TARGETS) black $(STYLE_TARGETS)
isort --apply --recursive $(STYLE_TARGETS) isort $(STYLE_TARGETS)
autoflake -i --remove-all-unused-imports -r $(STYLE_TARGETS) autoflake -i --remove-all-unused-imports -r $(STYLE_TARGETS)
flake8 flake8
make pyright make pyright

View File

@ -19,7 +19,6 @@ from decimal import Decimal
from trezorlib import btc from trezorlib import btc
# https://btc1.trezor.io/api/tx-specific/f5e735549daeb480d4348f2574b8967a4f149715edb220a742d8bb654d668348 # https://btc1.trezor.io/api/tx-specific/f5e735549daeb480d4348f2574b8967a4f149715edb220a742d8bb654d668348
TX_JSON_BIG = """ TX_JSON_BIG = """
{ {
@ -217,7 +216,7 @@ def test_from_json():
assert i.sequence == v["sequence"] assert i.sequence == v["sequence"]
for v, o in zip(tx_dict["vout"], tx.bin_outputs): for v, o in zip(tx_dict["vout"], tx.bin_outputs):
assert o.amount == int(Decimal(v["value"]) * (10 ** 8)) assert o.amount == int(Decimal(v["value"]) * (10**8))
assert o.script_pubkey.hex() == v["scriptPubKey"]["hex"] assert o.script_pubkey.hex() == v["scriptPubKey"]["hex"]
@ -233,5 +232,5 @@ def test_coinbase_from_json():
coinbase = tx.inputs[0] coinbase = tx.inputs[0]
assert coinbase.prev_hash == b"\x00" * 32 assert coinbase.prev_hash == b"\x00" * 32
assert coinbase.prev_index == 2 ** 32 - 1 assert coinbase.prev_index == 2**32 - 1
assert coinbase.script_sig.hex() == tx_dict["vin"][0]["coinbase"] assert coinbase.script_sig.hex() == tx_dict["vin"][0]["coinbase"]

View File

@ -6,9 +6,9 @@ import requests
from trezorlib import firmware from trezorlib import firmware
from trezorlib.firmware import ( from trezorlib.firmware import (
VendorFirmware,
LegacyFirmware, LegacyFirmware,
LegacyV2Firmware, LegacyV2Firmware,
VendorFirmware,
VendorHeader, VendorHeader,
) )

View File

@ -14,19 +14,18 @@
# You should have received a copy of the License along with this library. # 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>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import pytest
import typing as t import typing as t
from trezorlib.merkle_tree import ( import pytest
MerkleTree,
Leaf,
Node,
leaf_hash,
internal_hash,
evaluate_proof,
)
from trezorlib.merkle_tree import (
Leaf,
MerkleTree,
Node,
evaluate_proof,
internal_hash,
leaf_hash,
)
NODE_VECTORS = ( # node, expected_hash NODE_VECTORS = ( # node, expected_hash
( # leaf node ( # leaf node

View File

@ -14,9 +14,9 @@
# You should have received a copy of the License along with this library. # 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>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import logging
from enum import IntEnum from enum import IntEnum
from io import BytesIO from io import BytesIO
import logging
import pytest import pytest
@ -90,7 +90,7 @@ class DefaultFields(protobuf.MessageType):
class RecursiveMessage(protobuf.MessageType): class RecursiveMessage(protobuf.MessageType):
FIELDS = { FIELDS = {
1: protobuf.Field("uvarint", "uint64"), 1: protobuf.Field("uvarint", "uint64"),
2: protobuf.Field("recursivefield", "RecursiveMessage", required=False) 2: protobuf.Field("recursivefield", "RecursiveMessage", required=False),
} }
@ -170,7 +170,7 @@ def test_sint_uint():
# roundtrip: # roundtrip:
assert protobuf.uint_to_sint(protobuf.sint_to_uint(1234567891011)) == 1234567891011 assert protobuf.uint_to_sint(protobuf.sint_to_uint(1234567891011)) == 1234567891011
assert protobuf.uint_to_sint(protobuf.sint_to_uint(-(2 ** 32))) == -(2 ** 32) assert protobuf.uint_to_sint(protobuf.sint_to_uint(-(2**32))) == -(2**32)
def test_simple_message(): def test_simple_message():
@ -314,11 +314,8 @@ def test_recursive():
msg = RecursiveMessage( msg = RecursiveMessage(
uvarint=1, uvarint=1,
recursivefield=RecursiveMessage( recursivefield=RecursiveMessage(
uvarint=2, uvarint=2, recursivefield=RecursiveMessage(uvarint=3)
recursivefield=RecursiveMessage( ),
uvarint=3
)
)
) )
buf = dump_message(msg) buf = dump_message(msg)

View File

@ -20,10 +20,10 @@ try:
from stellar_sdk import ( from stellar_sdk import (
Account, Account,
Asset, Asset,
MuxedAccount,
Network, Network,
TransactionBuilder, TransactionBuilder,
TrustLineEntryFlag, TrustLineEntryFlag,
MuxedAccount,
) )
from stellar_sdk.strkey import StrKey from stellar_sdk.strkey import StrKey
except ImportError: except ImportError:

View File

@ -19,12 +19,30 @@ from trezorlib import tools
def test_descriptor_checksum(): def test_descriptor_checksum():
VECTOR = [ VECTOR = [
("pkh([5c9e228d/44'/0'/0']xpub6BiVtCpG9fQPxnPmHXG8PhtzQdWC2Su4qWu6XW9tpWFYhxydCLJGrWBJZ5H6qTAHdPQ7pQhtpjiYZVZARo14qHiay2fvrX996oEP42u8wZy/0/*)", "vzuemqzv"), (
("pkh([5c9e228d/44'/0'/0']xpub6BiVtCpG9fQPxnPmHXG8PhtzQdWC2Su4qWu6XW9tpWFYhxydCLJGrWBJZ5H6qTAHdPQ7pQhtpjiYZVZARo14qHiay2fvrX996oEP42u8wZy/1/*)", "akecx4j5"), "pkh([5c9e228d/44'/0'/0']xpub6BiVtCpG9fQPxnPmHXG8PhtzQdWC2Su4qWu6XW9tpWFYhxydCLJGrWBJZ5H6qTAHdPQ7pQhtpjiYZVZARo14qHiay2fvrX996oEP42u8wZy/0/*)",
("sh(wpkh([5c9e228d/49'/0'/0']xpub6CVKsQYXc9awxgV1tWbG4foDvdcnieK2JkbpPEBKB5WwAPKBZ1mstLbKVB4ov7QzxzjaxNK6EfmNY5Jsk2cG26EVcEkycGW4tchT2dyUhrx/0/*))", "jkfqtdfw"), "vzuemqzv",
("sh(wpkh([5c9e228d/49'/0'/0']xpub6CVKsQYXc9awxgV1tWbG4foDvdcnieK2JkbpPEBKB5WwAPKBZ1mstLbKVB4ov7QzxzjaxNK6EfmNY5Jsk2cG26EVcEkycGW4tchT2dyUhrx/1/*))", "8h8knju3"), ),
("wpkh([5c9e228d/84'/0'/0']xpub6DDUPHpUo4pcy43iJeZjbSVWGav1SMMmuWdMHiGtkK8rhKmfbomtkwW6GKs1GGAKehT6QRocrmda3WWxXawpjmwaUHfFRXuKrXSapdckEYF/0/*)", "l4dc6ccr"), (
("wpkh([5c9e228d/84'/0'/0']xpub6DDUPHpUo4pcy43iJeZjbSVWGav1SMMmuWdMHiGtkK8rhKmfbomtkwW6GKs1GGAKehT6QRocrmda3WWxXawpjmwaUHfFRXuKrXSapdckEYF/1/*)", "wpge8dgm"), "pkh([5c9e228d/44'/0'/0']xpub6BiVtCpG9fQPxnPmHXG8PhtzQdWC2Su4qWu6XW9tpWFYhxydCLJGrWBJZ5H6qTAHdPQ7pQhtpjiYZVZARo14qHiay2fvrX996oEP42u8wZy/1/*)",
"akecx4j5",
),
(
"sh(wpkh([5c9e228d/49'/0'/0']xpub6CVKsQYXc9awxgV1tWbG4foDvdcnieK2JkbpPEBKB5WwAPKBZ1mstLbKVB4ov7QzxzjaxNK6EfmNY5Jsk2cG26EVcEkycGW4tchT2dyUhrx/0/*))",
"jkfqtdfw",
),
(
"sh(wpkh([5c9e228d/49'/0'/0']xpub6CVKsQYXc9awxgV1tWbG4foDvdcnieK2JkbpPEBKB5WwAPKBZ1mstLbKVB4ov7QzxzjaxNK6EfmNY5Jsk2cG26EVcEkycGW4tchT2dyUhrx/1/*))",
"8h8knju3",
),
(
"wpkh([5c9e228d/84'/0'/0']xpub6DDUPHpUo4pcy43iJeZjbSVWGav1SMMmuWdMHiGtkK8rhKmfbomtkwW6GKs1GGAKehT6QRocrmda3WWxXawpjmwaUHfFRXuKrXSapdckEYF/0/*)",
"l4dc6ccr",
),
(
"wpkh([5c9e228d/84'/0'/0']xpub6DDUPHpUo4pcy43iJeZjbSVWGav1SMMmuWdMHiGtkK8rhKmfbomtkwW6GKs1GGAKehT6QRocrmda3WWxXawpjmwaUHfFRXuKrXSapdckEYF/1/*)",
"wpge8dgm",
),
] ]
for d, c in VECTOR: for d, c in VECTOR:
assert tools.descriptor_checksum(d) == c assert tools.descriptor_checksum(d) == c

View File

@ -34,8 +34,8 @@ def test_disabled_transport():
def test_import_all_transports(): def test_import_all_transports():
from trezorlib.transport.bridge import BridgeTransport from trezorlib.transport.bridge import BridgeTransport
from trezorlib.transport.hid import HidTransport from trezorlib.transport.hid import HidTransport
from trezorlib.transport.webusb import WebUsbTransport
from trezorlib.transport.udp import UdpTransport from trezorlib.transport.udp import UdpTransport
from trezorlib.transport.webusb import WebUsbTransport
assert BridgeTransport assert BridgeTransport
assert HidTransport assert HidTransport

View File

@ -48,11 +48,11 @@ class CompactUintAdapter(c.Adapter):
def _encode(self, obj: int, context: Any, path: Any) -> dict: def _encode(self, obj: int, context: Any, path: Any) -> dict:
if obj < 0xFD: if obj < 0xFD:
return {"base": obj} return {"base": obj}
if obj < 2 ** 16: if obj < 2**16:
return {"base": 0xFD, "ext": obj} return {"base": 0xFD, "ext": obj}
if obj < 2 ** 32: if obj < 2**32:
return {"base": 0xFE, "ext": obj} return {"base": 0xFE, "ext": obj}
if obj < 2 ** 64: if obj < 2**64:
return {"base": 0xFF, "ext": obj} return {"base": 0xFF, "ext": obj}
raise ValueError("Value too big for compact uint") raise ValueError("Value too big for compact uint")

View File

@ -27,21 +27,21 @@ from gevent import monkey
monkey.patch_all() monkey.patch_all()
import json import json
import logging
import struct import struct
import time import time
import typing as t import typing as t
import logging
import click import click
from bottle import run, post, request, response from bottle import post, request, response, run
import trezorlib.transport
import trezorlib.mapping import trezorlib.mapping
import trezorlib.models import trezorlib.models
import trezorlib.transport
from trezorlib.client import TrezorClient from trezorlib.client import TrezorClient
from trezorlib.ui import TrezorClientUI
from trezorlib.protobuf import format_message from trezorlib.protobuf import format_message
from trezorlib.transport.bridge import BridgeTransport from trezorlib.transport.bridge import BridgeTransport
from trezorlib.ui import TrezorClientUI
# ignore bridge. we are the bridge # ignore bridge. we are the bridge
BridgeTransport.ENABLED = False BridgeTransport.ENABLED = False

View File

@ -22,6 +22,7 @@ import re
import sys import sys
import pyotp import pyotp
from trezorlib.client import TrezorClient from trezorlib.client import TrezorClient
from trezorlib.misc import decrypt_keyvalue, encrypt_keyvalue from trezorlib.misc import decrypt_keyvalue, encrypt_keyvalue
from trezorlib.tools import parse_path from trezorlib.tools import parse_path