From 3c0cb4d7635f14254bfc04b8b145583a40f2ebf1 Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Tue, 25 Aug 2020 19:02:57 +0200 Subject: [PATCH] feat(python): extend sign_message to also sign bytes, not only string --- python/.changelog.d/2126.changed | 1 + python/src/trezorlib/btc.py | 6 +++--- python/src/trezorlib/ethereum.py | 10 +++++++--- python/src/trezorlib/tools.py | 12 +++++++----- 4 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 python/.changelog.d/2126.changed diff --git a/python/.changelog.d/2126.changed b/python/.changelog.d/2126.changed new file mode 100644 index 000000000..6ea3e552a --- /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 29647bc4c..30e98aa05 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 098dbecc2..01a9d543c 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 961f65216..67f8115ff 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):