diff --git a/python/.changelog.d/2126.changed b/python/.changelog.d/2126.changed new file mode 100644 index 0000000000..6ea3e552a2 --- /dev/null +++ b/python/.changelog.d/2126.changed @@ -0,0 +1 @@ +Rename `normalize_nfc` to `prepare_message_bytes` in tools.py diff --git a/python/src/trezorlib/btc.py b/python/src/trezorlib/btc.py index 29647bc4cf..30e98aa05f 100644 --- a/python/src/trezorlib/btc.py +++ b/python/src/trezorlib/btc.py @@ -23,7 +23,7 @@ from typing import TYPE_CHECKING, Any, AnyStr, List, Optional, Sequence, Tuple from typing_extensions import Protocol, TypedDict from . import exceptions, messages -from .tools import expect, normalize_nfc, session +from .tools import expect, prepare_message_bytes, session if TYPE_CHECKING: from .client import TrezorClient @@ -219,7 +219,7 @@ def sign_message( messages.SignMessage( coin_name=coin_name, address_n=n, - message=normalize_nfc(message), + message=prepare_message_bytes(message), script_type=script_type, no_script_type=no_script_type, ) @@ -238,7 +238,7 @@ def verify_message( messages.VerifyMessage( address=address, signature=signature, - message=normalize_nfc(message), + message=prepare_message_bytes(message), coin_name=coin_name, ) ) diff --git a/python/src/trezorlib/ethereum.py b/python/src/trezorlib/ethereum.py index 098dbecc2c..01a9d543cc 100644 --- a/python/src/trezorlib/ethereum.py +++ b/python/src/trezorlib/ethereum.py @@ -18,7 +18,7 @@ import re from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Tuple from . import exceptions, messages -from .tools import expect, normalize_nfc, session +from .tools import expect, prepare_message_bytes, session if TYPE_CHECKING: from .client import TrezorClient @@ -268,7 +268,9 @@ def sign_message( client: "TrezorClient", n: "Address", message: AnyStr ) -> "MessageType": return client.call( - messages.EthereumSignMessage(address_n=n, message=normalize_nfc(message)) + messages.EthereumSignMessage( + address_n=n, message=prepare_message_bytes(message) + ) ) @@ -351,7 +353,9 @@ def verify_message( try: resp = client.call( messages.EthereumVerifyMessage( - address=address, signature=signature, message=normalize_nfc(message) + address=address, + signature=signature, + message=prepare_message_bytes(message), ) ) except exceptions.TrezorFailure: diff --git a/python/src/trezorlib/tools.py b/python/src/trezorlib/tools.py index 961f652161..67f8115ff4 100644 --- a/python/src/trezorlib/tools.py +++ b/python/src/trezorlib/tools.py @@ -211,13 +211,15 @@ def parse_path(nstr: str) -> Address: raise ValueError("Invalid BIP32 path", nstr) from e -def normalize_nfc(txt: AnyStr) -> bytes: +def prepare_message_bytes(txt: AnyStr) -> bytes: """ - Normalize message to NFC and return bytes suitable for protobuf. - This seems to be bitcoin-qt standard of doing things. + Make message suitable for protobuf. + If the message is a Unicode string, normalize it. + If it's bytes, return the raw bytes. """ - str_txt = txt.decode() if isinstance(txt, bytes) else txt - return unicodedata.normalize("NFC", str_txt).encode() + if isinstance(txt, bytes): + return txt + return unicodedata.normalize("NFC", txt).encode() # NOTE for type tests (mypy/pyright):