From 14335e537118d4b588427ad5c7eb3071091a876d Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Thu, 19 Oct 2023 17:33:08 +0200 Subject: [PATCH] feat(python): add chunkify parameter to message signing/verification --- python/src/trezorlib/btc.py | 4 ++++ python/src/trezorlib/cli/btc.py | 22 +++++++++++++++++++--- python/src/trezorlib/cli/ethereum.py | 18 ++++++++++++++---- python/src/trezorlib/ethereum.py | 9 ++++++++- python/src/trezorlib/messages.py | 12 ++++++++++++ 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/python/src/trezorlib/btc.py b/python/src/trezorlib/btc.py index ec5e83307a..59e39de9b8 100644 --- a/python/src/trezorlib/btc.py +++ b/python/src/trezorlib/btc.py @@ -234,6 +234,7 @@ def sign_message( message: AnyStr, script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS, no_script_type: bool = False, + chunkify: bool = False, ) -> "MessageType": return client.call( messages.SignMessage( @@ -242,6 +243,7 @@ def sign_message( message=prepare_message_bytes(message), script_type=script_type, no_script_type=no_script_type, + chunkify=chunkify, ) ) @@ -252,6 +254,7 @@ def verify_message( address: str, signature: bytes, message: AnyStr, + chunkify: bool = False, ) -> bool: try: resp = client.call( @@ -260,6 +263,7 @@ def verify_message( signature=signature, message=prepare_message_bytes(message), coin_name=coin_name, + chunkify=chunkify, ) ) except exceptions.TrezorFailure: diff --git a/python/src/trezorlib/cli/btc.py b/python/src/trezorlib/cli/btc.py index 6776409b4f..cbf2082669 100644 --- a/python/src/trezorlib/cli/btc.py +++ b/python/src/trezorlib/cli/btc.py @@ -426,6 +426,7 @@ def sign_tx(client: "TrezorClient", json_file: TextIO, chunkify: bool) -> None: is_flag=True, help="Generate Electrum-compatible signature", ) +@click.option("-C", "--chunkify", is_flag=True) @click.argument("message") @with_client def sign_message( @@ -435,13 +436,20 @@ def sign_message( message: str, script_type: Optional[messages.InputScriptType], electrum_compat: bool, + chunkify: bool, ) -> Dict[str, str]: """Sign message using address of given path.""" address_n = tools.parse_path(address) if script_type is None: script_type = guess_script_type_from_path(address_n) res = btc.sign_message( - client, coin, address_n, message, script_type, electrum_compat + client, + coin, + address_n, + message, + script_type, + electrum_compat, + chunkify=chunkify, ) return { "message": message, @@ -452,16 +460,24 @@ def sign_message( @cli.command() @click.option("-c", "--coin", default=DEFAULT_COIN) +@click.option("-C", "--chunkify", is_flag=True) @click.argument("address") @click.argument("signature") @click.argument("message") @with_client def verify_message( - client: "TrezorClient", coin: str, address: str, signature: str, message: str + client: "TrezorClient", + coin: str, + address: str, + signature: str, + message: str, + chunkify: bool, ) -> bool: """Verify message.""" signature_bytes = base64.b64decode(signature) - return btc.verify_message(client, coin, address, signature_bytes, message) + return btc.verify_message( + client, coin, address, signature_bytes, message, chunkify=chunkify + ) # diff --git a/python/src/trezorlib/cli/ethereum.py b/python/src/trezorlib/cli/ethereum.py index 10b72fad59..38b536bceb 100644 --- a/python/src/trezorlib/cli/ethereum.py +++ b/python/src/trezorlib/cli/ethereum.py @@ -524,13 +524,16 @@ def sign_tx( @cli.command() @click.option("-n", "--address", required=True, help=PATH_HELP) +@click.option("-C", "--chunkify", is_flag=True) @click.argument("message") @with_client -def sign_message(client: "TrezorClient", address: str, message: str) -> Dict[str, str]: +def sign_message( + client: "TrezorClient", address: str, message: str, chunkify: bool +) -> Dict[str, str]: """Sign message with Ethereum address.""" address_n = tools.parse_path(address) network = ethereum.network_from_address_n(address_n, DEFINITIONS_SOURCE) - ret = ethereum.sign_message(client, address_n, message, network) + ret = ethereum.sign_message(client, address_n, message, network, chunkify=chunkify) output = { "message": message, "address": ret.address, @@ -576,16 +579,23 @@ def sign_typed_data( @cli.command() +@click.option("-C", "--chunkify", is_flag=True) @click.argument("address") @click.argument("signature") @click.argument("message") @with_client def verify_message( - client: "TrezorClient", address: str, signature: str, message: str + client: "TrezorClient", + address: str, + signature: str, + message: str, + chunkify: bool, ) -> bool: """Verify message signed with Ethereum address.""" signature_bytes = ethereum.decode_hex(signature) - return ethereum.verify_message(client, address, signature_bytes, message) + return ethereum.verify_message( + client, address, signature_bytes, message, chunkify=chunkify + ) @cli.command() diff --git a/python/src/trezorlib/ethereum.py b/python/src/trezorlib/ethereum.py index 2dc9d9151e..1cf2eeeaed 100644 --- a/python/src/trezorlib/ethereum.py +++ b/python/src/trezorlib/ethereum.py @@ -303,12 +303,14 @@ def sign_message( n: "Address", message: AnyStr, encoded_network: Optional[bytes] = None, + chunkify: bool = False, ) -> "MessageType": return client.call( messages.EthereumSignMessage( address_n=n, message=prepare_message_bytes(message), encoded_network=encoded_network, + chunkify=chunkify, ) ) @@ -389,7 +391,11 @@ def sign_typed_data( def verify_message( - client: "TrezorClient", address: str, signature: bytes, message: AnyStr + client: "TrezorClient", + address: str, + signature: bytes, + message: AnyStr, + chunkify: bool = False, ) -> bool: try: resp = client.call( @@ -397,6 +403,7 @@ def verify_message( address=address, signature=signature, message=prepare_message_bytes(message), + chunkify=chunkify, ) ) except exceptions.TrezorFailure: diff --git a/python/src/trezorlib/messages.py b/python/src/trezorlib/messages.py index adabfc2be6..4076577562 100644 --- a/python/src/trezorlib/messages.py +++ b/python/src/trezorlib/messages.py @@ -1138,6 +1138,7 @@ class SignMessage(protobuf.MessageType): 3: protobuf.Field("coin_name", "string", repeated=False, required=False, default='Bitcoin'), 4: protobuf.Field("script_type", "InputScriptType", repeated=False, required=False, default=InputScriptType.SPENDADDRESS), 5: protobuf.Field("no_script_type", "bool", repeated=False, required=False, default=None), + 6: protobuf.Field("chunkify", "bool", repeated=False, required=False, default=None), } def __init__( @@ -1148,12 +1149,14 @@ class SignMessage(protobuf.MessageType): coin_name: Optional["str"] = 'Bitcoin', script_type: Optional["InputScriptType"] = InputScriptType.SPENDADDRESS, no_script_type: Optional["bool"] = None, + chunkify: Optional["bool"] = None, ) -> None: self.address_n: Sequence["int"] = address_n if address_n is not None else [] self.message = message self.coin_name = coin_name self.script_type = script_type self.no_script_type = no_script_type + self.chunkify = chunkify class MessageSignature(protobuf.MessageType): @@ -1180,6 +1183,7 @@ class VerifyMessage(protobuf.MessageType): 2: protobuf.Field("signature", "bytes", repeated=False, required=True), 3: protobuf.Field("message", "bytes", repeated=False, required=True), 4: protobuf.Field("coin_name", "string", repeated=False, required=False, default='Bitcoin'), + 5: protobuf.Field("chunkify", "bool", repeated=False, required=False, default=None), } def __init__( @@ -1189,11 +1193,13 @@ class VerifyMessage(protobuf.MessageType): signature: "bytes", message: "bytes", coin_name: Optional["str"] = 'Bitcoin', + chunkify: Optional["bool"] = None, ) -> None: self.address = address self.signature = signature self.message = message self.coin_name = coin_name + self.chunkify = chunkify class SignTx(protobuf.MessageType): @@ -5065,6 +5071,7 @@ class EthereumSignMessage(protobuf.MessageType): 1: protobuf.Field("address_n", "uint32", repeated=True, required=False, default=None), 2: protobuf.Field("message", "bytes", repeated=False, required=True), 3: protobuf.Field("encoded_network", "bytes", repeated=False, required=False, default=None), + 4: protobuf.Field("chunkify", "bool", repeated=False, required=False, default=None), } def __init__( @@ -5073,10 +5080,12 @@ class EthereumSignMessage(protobuf.MessageType): message: "bytes", address_n: Optional[Sequence["int"]] = None, encoded_network: Optional["bytes"] = None, + chunkify: Optional["bool"] = None, ) -> None: self.address_n: Sequence["int"] = address_n if address_n is not None else [] self.message = message self.encoded_network = encoded_network + self.chunkify = chunkify class EthereumMessageSignature(protobuf.MessageType): @@ -5102,6 +5111,7 @@ class EthereumVerifyMessage(protobuf.MessageType): 2: protobuf.Field("signature", "bytes", repeated=False, required=True), 3: protobuf.Field("message", "bytes", repeated=False, required=True), 4: protobuf.Field("address", "string", repeated=False, required=True), + 5: protobuf.Field("chunkify", "bool", repeated=False, required=False, default=None), } def __init__( @@ -5110,10 +5120,12 @@ class EthereumVerifyMessage(protobuf.MessageType): signature: "bytes", message: "bytes", address: "str", + chunkify: Optional["bool"] = None, ) -> None: self.signature = signature self.message = message self.address = address + self.chunkify = chunkify class EthereumSignTypedHash(protobuf.MessageType):