mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 12:28:09 +00:00
feat(python): extend sign_message to also sign bytes, not only string
This commit is contained in:
parent
a532bf0737
commit
3c0cb4d763
1
python/.changelog.d/2126.changed
Normal file
1
python/.changelog.d/2126.changed
Normal file
@ -0,0 +1 @@
|
|||||||
|
Rename `normalize_nfc` to `prepare_message_bytes` in tools.py
|
@ -23,7 +23,7 @@ from typing import TYPE_CHECKING, Any, AnyStr, List, Optional, Sequence, Tuple
|
|||||||
from typing_extensions import Protocol, TypedDict
|
from typing_extensions import Protocol, TypedDict
|
||||||
|
|
||||||
from . import exceptions, messages
|
from . import exceptions, messages
|
||||||
from .tools import expect, normalize_nfc, session
|
from .tools import expect, prepare_message_bytes, session
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .client import TrezorClient
|
from .client import TrezorClient
|
||||||
@ -219,7 +219,7 @@ def sign_message(
|
|||||||
messages.SignMessage(
|
messages.SignMessage(
|
||||||
coin_name=coin_name,
|
coin_name=coin_name,
|
||||||
address_n=n,
|
address_n=n,
|
||||||
message=normalize_nfc(message),
|
message=prepare_message_bytes(message),
|
||||||
script_type=script_type,
|
script_type=script_type,
|
||||||
no_script_type=no_script_type,
|
no_script_type=no_script_type,
|
||||||
)
|
)
|
||||||
@ -238,7 +238,7 @@ def verify_message(
|
|||||||
messages.VerifyMessage(
|
messages.VerifyMessage(
|
||||||
address=address,
|
address=address,
|
||||||
signature=signature,
|
signature=signature,
|
||||||
message=normalize_nfc(message),
|
message=prepare_message_bytes(message),
|
||||||
coin_name=coin_name,
|
coin_name=coin_name,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -18,7 +18,7 @@ import re
|
|||||||
from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Tuple
|
from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
from . import exceptions, messages
|
from . import exceptions, messages
|
||||||
from .tools import expect, normalize_nfc, session
|
from .tools import expect, prepare_message_bytes, session
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .client import TrezorClient
|
from .client import TrezorClient
|
||||||
@ -268,7 +268,9 @@ def sign_message(
|
|||||||
client: "TrezorClient", n: "Address", message: AnyStr
|
client: "TrezorClient", n: "Address", message: AnyStr
|
||||||
) -> "MessageType":
|
) -> "MessageType":
|
||||||
return client.call(
|
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:
|
try:
|
||||||
resp = client.call(
|
resp = client.call(
|
||||||
messages.EthereumVerifyMessage(
|
messages.EthereumVerifyMessage(
|
||||||
address=address, signature=signature, message=normalize_nfc(message)
|
address=address,
|
||||||
|
signature=signature,
|
||||||
|
message=prepare_message_bytes(message),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except exceptions.TrezorFailure:
|
except exceptions.TrezorFailure:
|
||||||
|
@ -211,13 +211,15 @@ def parse_path(nstr: str) -> Address:
|
|||||||
raise ValueError("Invalid BIP32 path", nstr) from e
|
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.
|
Make message suitable for protobuf.
|
||||||
This seems to be bitcoin-qt standard of doing things.
|
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
|
if isinstance(txt, bytes):
|
||||||
return unicodedata.normalize("NFC", str_txt).encode()
|
return txt
|
||||||
|
return unicodedata.normalize("NFC", txt).encode()
|
||||||
|
|
||||||
|
|
||||||
# NOTE for type tests (mypy/pyright):
|
# NOTE for type tests (mypy/pyright):
|
||||||
|
Loading…
Reference in New Issue
Block a user