From 36c177a9fb5588a419b5495858049858c2d78ee3 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Mon, 2 Dec 2024 15:46:51 +0100 Subject: [PATCH] chore(python): update python tools [no changelog] --- python/tools/encfs_aes_getpass.py | 14 +++++++------- python/tools/helloworld.py | 5 +++-- python/tools/pwd_reader.py | 18 ++++++++++-------- python/tools/pybridge.py | 18 ++++++++++++------ python/tools/rng_entropy_collector.py | 9 ++++----- python/tools/trezor-otp.py | 13 ++++++------- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/python/tools/encfs_aes_getpass.py b/python/tools/encfs_aes_getpass.py index 82773e50fa..37a221154c 100755 --- a/python/tools/encfs_aes_getpass.py +++ b/python/tools/encfs_aes_getpass.py @@ -35,7 +35,6 @@ import trezorlib.misc from trezorlib.client import TrezorClient from trezorlib.tools import Address from trezorlib.transport import enumerate_devices -from trezorlib.ui import ClickUI version_tuple = tuple(map(int, trezorlib.__version__.split("."))) if not (0, 11) <= version_tuple < (0, 14): @@ -71,7 +70,7 @@ def choose_device(devices: Sequence["Transport"]) -> "Transport": sys.stderr.write("Available devices:\n") for d in devices: try: - client = TrezorClient(d, ui=ClickUI()) + client = TrezorClient(d) except IOError: sys.stderr.write("[-] \n") continue @@ -80,7 +79,7 @@ def choose_device(devices: Sequence["Transport"]) -> "Transport": sys.stderr.write(f"[{i}] {client.features.label}\n") else: sys.stderr.write(f"[{i}] \n") - client.close() + # TODO client.close() i += 1 sys.stderr.write("----------------------------\n") @@ -106,7 +105,8 @@ def main() -> None: devices = wait_for_devices() transport = choose_device(devices) - client = TrezorClient(transport, ui=ClickUI()) + client = TrezorClient(transport) + session = client.get_management_session() rootdir = os.environ["encfs_root"] # Read "man encfs" for more passw_file = os.path.join(rootdir, "password.dat") @@ -120,7 +120,7 @@ def main() -> None: sys.stderr.write("Computer asked Trezor for new strong password.\n") # 32 bytes, good for AES - trezor_entropy = trezorlib.misc.get_entropy(client, 32) + trezor_entropy = trezorlib.misc.get_entropy(session, 32) urandom_entropy = os.urandom(32) passw = hashlib.sha256(trezor_entropy + urandom_entropy).digest() @@ -129,7 +129,7 @@ def main() -> None: bip32_path = Address([10, 0]) passw_encrypted = trezorlib.misc.encrypt_keyvalue( - client, bip32_path, label, passw, False, True + session, bip32_path, label, passw, False, True ) data = { @@ -144,7 +144,7 @@ def main() -> None: data = json.load(open(passw_file, "r")) passw = trezorlib.misc.decrypt_keyvalue( - client, + session, data["bip32_path"], data["label"], bytes.fromhex(data["password_encrypted_hex"]), diff --git a/python/tools/helloworld.py b/python/tools/helloworld.py index 76b4502da2..b8711dbb00 100755 --- a/python/tools/helloworld.py +++ b/python/tools/helloworld.py @@ -24,13 +24,14 @@ from trezorlib.tools import parse_path def main() -> None: # Use first connected device client = get_default_client() + session = client.get_session(derive_cardano=True) # Print out Trezor's features and settings - print(client.features) + print(session.features) # Get the first address of first BIP44 account bip32_path = parse_path("44h/0h/0h/0/0") - address = btc.get_address(client, "Bitcoin", bip32_path, True) + address = btc.get_address(session, "Bitcoin", bip32_path, False) print("Bitcoin address:", address) diff --git a/python/tools/pwd_reader.py b/python/tools/pwd_reader.py index afd405e164..1c012c7abf 100755 --- a/python/tools/pwd_reader.py +++ b/python/tools/pwd_reader.py @@ -26,23 +26,24 @@ from urllib.parse import urlparse from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes -from trezorlib import misc, ui +from trezorlib import misc from trezorlib.client import TrezorClient from trezorlib.tools import parse_path from trezorlib.transport import get_transport +from trezorlib.transport.session import Session # Return path by BIP-32 BIP32_PATH = parse_path("10016h/0") # Deriving master key -def getMasterKey(client: TrezorClient) -> str: +def getMasterKey(session: Session) -> str: bip32_path = BIP32_PATH ENC_KEY = "Activate TREZOR Password Manager?" ENC_VALUE = bytes.fromhex( "2d650551248d792eabf628f451200d7f51cb63e46aadcbb1038aacb05e8c8aee2d650551248d792eabf628f451200d7f51cb63e46aadcbb1038aacb05e8c8aee" ) - key = misc.encrypt_keyvalue(client, bip32_path, ENC_KEY, ENC_VALUE, True, True) + key = misc.encrypt_keyvalue(session, bip32_path, ENC_KEY, ENC_VALUE, True, True) return key.hex() @@ -101,7 +102,7 @@ def decryptEntryValue(nonce: str, val: bytes) -> dict: # Decrypt give entry nonce -def getDecryptedNonce(client: TrezorClient, entry: dict) -> str: +def getDecryptedNonce(session: Session, entry: dict) -> str: print() print("Waiting for Trezor input ...") print() @@ -117,7 +118,7 @@ def getDecryptedNonce(client: TrezorClient, entry: dict) -> str: ENC_KEY = f"Unlock {item} for user {entry['username']}?" ENC_VALUE = entry["nonce"] decrypted_nonce = misc.decrypt_keyvalue( - client, BIP32_PATH, ENC_KEY, bytes.fromhex(ENC_VALUE), False, True + session, BIP32_PATH, ENC_KEY, bytes.fromhex(ENC_VALUE), False, True ) return decrypted_nonce.hex() @@ -144,13 +145,14 @@ def main() -> None: print(e) return - client = TrezorClient(transport=transport, ui=ui.ClickUI()) + client = TrezorClient(transport=transport) + session = client.get_management_session() print() print("Confirm operation on Trezor") print() - masterKey = getMasterKey(client) + masterKey = getMasterKey(session) # print('master key:', masterKey) fileName = getFileEncKey(masterKey)[0] @@ -173,7 +175,7 @@ def main() -> None: entry_id = input("Select entry number to decrypt: ") entry_id = str(entry_id) - plain_nonce = getDecryptedNonce(client, entries[entry_id]) + plain_nonce = getDecryptedNonce(session, entries[entry_id]) pwdArr = entries[entry_id]["password"]["data"] pwdHex = "".join([hex(x)[2:].zfill(2) for x in pwdArr]) diff --git a/python/tools/pybridge.py b/python/tools/pybridge.py index 30d69bbc9b..d94ec121d0 100644 --- a/python/tools/pybridge.py +++ b/python/tools/pybridge.py @@ -24,6 +24,8 @@ from __future__ import annotations from gevent import monkey +import trezorlib.transport + monkey.patch_all() import json @@ -103,11 +105,11 @@ class Transport: self.session: Session | None = None self.transport = transport - client = TrezorClient(transport, ui=SilentUI()) + client = TrezorClient(transport) # TODO add silent UI? self.model = ( trezorlib.models.by_name(client.features.model) or trezorlib.models.TREZOR_T ) - client.end_session() + # TODO client.end_session() def acquire(self, sid: str) -> str: if self.session_id() != sid: @@ -116,11 +118,11 @@ class Transport: self.session.release() self.session = Session(self) - self.transport.begin_session() + # TODO self.transport.deprecated_begin_session() return self.session.id def release(self) -> None: - self.transport.end_session() + # TODO self.transport.deprecated_end_session() self.session = None def session_id(self) -> str | None: @@ -141,10 +143,14 @@ class Transport: } def write(self, msg_id: int, data: bytes) -> None: - self.transport.write(msg_id, data) + raise NotImplementedError + # TODO + # self.transport.write(msg_id, data) def read(self) -> tuple[int, bytes]: - return self.transport.read() + raise NotImplementedError + # TODO + # return self.transport.read() @classmethod def find(cls, path: str) -> Transport | None: diff --git a/python/tools/rng_entropy_collector.py b/python/tools/rng_entropy_collector.py index 2b0a5b80d7..437561b154 100755 --- a/python/tools/rng_entropy_collector.py +++ b/python/tools/rng_entropy_collector.py @@ -7,14 +7,15 @@ import io import sys -from trezorlib import misc, ui +from trezorlib import misc from trezorlib.client import TrezorClient from trezorlib.transport import get_transport def main() -> None: try: - client = TrezorClient(get_transport(), ui=ui.ClickUI()) + client = TrezorClient(get_transport()) + session = client.get_management_session() except Exception as e: print(e) return @@ -25,11 +26,9 @@ def main() -> None: with io.open(arg1, "wb") as f: for _ in range(0, arg2, step): - entropy = misc.get_entropy(client, step) + entropy = misc.get_entropy(session, step) f.write(entropy) - client.close() - if __name__ == "__main__": main() diff --git a/python/tools/trezor-otp.py b/python/tools/trezor-otp.py index bc0b66daa9..a88f745b41 100755 --- a/python/tools/trezor-otp.py +++ b/python/tools/trezor-otp.py @@ -27,26 +27,25 @@ from trezorlib.client import TrezorClient from trezorlib.misc import decrypt_keyvalue, encrypt_keyvalue from trezorlib.tools import parse_path from trezorlib.transport import get_transport -from trezorlib.ui import ClickUI BIP32_PATH = parse_path("10016h/0") def encrypt(type: str, domain: str, secret: str) -> str: transport = get_transport() - client = TrezorClient(transport, ClickUI()) + client = TrezorClient(transport) + session = client.get_management_session() dom = type.upper() + ": " + domain - enc = encrypt_keyvalue(client, BIP32_PATH, dom, secret.encode(), False, True) - client.close() + enc = encrypt_keyvalue(session, BIP32_PATH, dom, secret.encode(), False, True) return enc.hex() def decrypt(type: str, domain: str, secret: bytes) -> bytes: transport = get_transport() - client = TrezorClient(transport, ClickUI()) + client = TrezorClient(transport) + session = client.get_management_session() dom = type.upper() + ": " + domain - dec = decrypt_keyvalue(client, BIP32_PATH, dom, secret, False, True) - client.close() + dec = decrypt_keyvalue(session, BIP32_PATH, dom, secret, False, True) return dec