1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 20:38:10 +00:00

feat(python): add chunkify parameter to message signing/verification

This commit is contained in:
Martin Milata 2023-10-19 17:33:08 +02:00
parent 18d6b4d647
commit 14335e5371
5 changed files with 57 additions and 8 deletions

View File

@ -234,6 +234,7 @@ def sign_message(
message: AnyStr, message: AnyStr,
script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS, script_type: messages.InputScriptType = messages.InputScriptType.SPENDADDRESS,
no_script_type: bool = False, no_script_type: bool = False,
chunkify: bool = False,
) -> "MessageType": ) -> "MessageType":
return client.call( return client.call(
messages.SignMessage( messages.SignMessage(
@ -242,6 +243,7 @@ def sign_message(
message=prepare_message_bytes(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,
chunkify=chunkify,
) )
) )
@ -252,6 +254,7 @@ def verify_message(
address: str, address: str,
signature: bytes, signature: bytes,
message: AnyStr, message: AnyStr,
chunkify: bool = False,
) -> bool: ) -> bool:
try: try:
resp = client.call( resp = client.call(
@ -260,6 +263,7 @@ def verify_message(
signature=signature, signature=signature,
message=prepare_message_bytes(message), message=prepare_message_bytes(message),
coin_name=coin_name, coin_name=coin_name,
chunkify=chunkify,
) )
) )
except exceptions.TrezorFailure: except exceptions.TrezorFailure:

View File

@ -426,6 +426,7 @@ def sign_tx(client: "TrezorClient", json_file: TextIO, chunkify: bool) -> None:
is_flag=True, is_flag=True,
help="Generate Electrum-compatible signature", help="Generate Electrum-compatible signature",
) )
@click.option("-C", "--chunkify", is_flag=True)
@click.argument("message") @click.argument("message")
@with_client @with_client
def sign_message( def sign_message(
@ -435,13 +436,20 @@ def sign_message(
message: str, message: str,
script_type: Optional[messages.InputScriptType], script_type: Optional[messages.InputScriptType],
electrum_compat: bool, electrum_compat: bool,
chunkify: bool,
) -> Dict[str, str]: ) -> Dict[str, str]:
"""Sign message using address of given path.""" """Sign message using address of given path."""
address_n = tools.parse_path(address) address_n = tools.parse_path(address)
if script_type is None: if script_type is None:
script_type = guess_script_type_from_path(address_n) script_type = guess_script_type_from_path(address_n)
res = btc.sign_message( 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 { return {
"message": message, "message": message,
@ -452,16 +460,24 @@ def sign_message(
@cli.command() @cli.command()
@click.option("-c", "--coin", default=DEFAULT_COIN) @click.option("-c", "--coin", default=DEFAULT_COIN)
@click.option("-C", "--chunkify", is_flag=True)
@click.argument("address") @click.argument("address")
@click.argument("signature") @click.argument("signature")
@click.argument("message") @click.argument("message")
@with_client @with_client
def verify_message( 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: ) -> bool:
"""Verify message.""" """Verify message."""
signature_bytes = base64.b64decode(signature) 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
)
# #

View File

@ -524,13 +524,16 @@ def sign_tx(
@cli.command() @cli.command()
@click.option("-n", "--address", required=True, help=PATH_HELP) @click.option("-n", "--address", required=True, help=PATH_HELP)
@click.option("-C", "--chunkify", is_flag=True)
@click.argument("message") @click.argument("message")
@with_client @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.""" """Sign message with Ethereum address."""
address_n = tools.parse_path(address) address_n = tools.parse_path(address)
network = ethereum.network_from_address_n(address_n, DEFINITIONS_SOURCE) 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 = { output = {
"message": message, "message": message,
"address": ret.address, "address": ret.address,
@ -576,16 +579,23 @@ def sign_typed_data(
@cli.command() @cli.command()
@click.option("-C", "--chunkify", is_flag=True)
@click.argument("address") @click.argument("address")
@click.argument("signature") @click.argument("signature")
@click.argument("message") @click.argument("message")
@with_client @with_client
def verify_message( def verify_message(
client: "TrezorClient", address: str, signature: str, message: str client: "TrezorClient",
address: str,
signature: str,
message: str,
chunkify: bool,
) -> bool: ) -> bool:
"""Verify message signed with Ethereum address.""" """Verify message signed with Ethereum address."""
signature_bytes = ethereum.decode_hex(signature) 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() @cli.command()

View File

@ -303,12 +303,14 @@ def sign_message(
n: "Address", n: "Address",
message: AnyStr, message: AnyStr,
encoded_network: Optional[bytes] = None, encoded_network: Optional[bytes] = None,
chunkify: bool = False,
) -> "MessageType": ) -> "MessageType":
return client.call( return client.call(
messages.EthereumSignMessage( messages.EthereumSignMessage(
address_n=n, address_n=n,
message=prepare_message_bytes(message), message=prepare_message_bytes(message),
encoded_network=encoded_network, encoded_network=encoded_network,
chunkify=chunkify,
) )
) )
@ -389,7 +391,11 @@ def sign_typed_data(
def verify_message( def verify_message(
client: "TrezorClient", address: str, signature: bytes, message: AnyStr client: "TrezorClient",
address: str,
signature: bytes,
message: AnyStr,
chunkify: bool = False,
) -> bool: ) -> bool:
try: try:
resp = client.call( resp = client.call(
@ -397,6 +403,7 @@ def verify_message(
address=address, address=address,
signature=signature, signature=signature,
message=prepare_message_bytes(message), message=prepare_message_bytes(message),
chunkify=chunkify,
) )
) )
except exceptions.TrezorFailure: except exceptions.TrezorFailure:

View File

@ -1138,6 +1138,7 @@ class SignMessage(protobuf.MessageType):
3: protobuf.Field("coin_name", "string", repeated=False, required=False, default='Bitcoin'), 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), 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), 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__( def __init__(
@ -1148,12 +1149,14 @@ class SignMessage(protobuf.MessageType):
coin_name: Optional["str"] = 'Bitcoin', coin_name: Optional["str"] = 'Bitcoin',
script_type: Optional["InputScriptType"] = InputScriptType.SPENDADDRESS, script_type: Optional["InputScriptType"] = InputScriptType.SPENDADDRESS,
no_script_type: Optional["bool"] = None, no_script_type: Optional["bool"] = None,
chunkify: Optional["bool"] = None,
) -> None: ) -> None:
self.address_n: Sequence["int"] = address_n if address_n is not None else [] self.address_n: Sequence["int"] = address_n if address_n is not None else []
self.message = message self.message = message
self.coin_name = coin_name self.coin_name = coin_name
self.script_type = script_type self.script_type = script_type
self.no_script_type = no_script_type self.no_script_type = no_script_type
self.chunkify = chunkify
class MessageSignature(protobuf.MessageType): class MessageSignature(protobuf.MessageType):
@ -1180,6 +1183,7 @@ class VerifyMessage(protobuf.MessageType):
2: protobuf.Field("signature", "bytes", repeated=False, required=True), 2: protobuf.Field("signature", "bytes", repeated=False, required=True),
3: protobuf.Field("message", "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'), 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__( def __init__(
@ -1189,11 +1193,13 @@ class VerifyMessage(protobuf.MessageType):
signature: "bytes", signature: "bytes",
message: "bytes", message: "bytes",
coin_name: Optional["str"] = 'Bitcoin', coin_name: Optional["str"] = 'Bitcoin',
chunkify: Optional["bool"] = None,
) -> None: ) -> None:
self.address = address self.address = address
self.signature = signature self.signature = signature
self.message = message self.message = message
self.coin_name = coin_name self.coin_name = coin_name
self.chunkify = chunkify
class SignTx(protobuf.MessageType): class SignTx(protobuf.MessageType):
@ -5065,6 +5071,7 @@ class EthereumSignMessage(protobuf.MessageType):
1: protobuf.Field("address_n", "uint32", repeated=True, required=False, default=None), 1: protobuf.Field("address_n", "uint32", repeated=True, required=False, default=None),
2: protobuf.Field("message", "bytes", repeated=False, required=True), 2: protobuf.Field("message", "bytes", repeated=False, required=True),
3: protobuf.Field("encoded_network", "bytes", repeated=False, required=False, default=None), 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__( def __init__(
@ -5073,10 +5080,12 @@ class EthereumSignMessage(protobuf.MessageType):
message: "bytes", message: "bytes",
address_n: Optional[Sequence["int"]] = None, address_n: Optional[Sequence["int"]] = None,
encoded_network: Optional["bytes"] = None, encoded_network: Optional["bytes"] = None,
chunkify: Optional["bool"] = None,
) -> None: ) -> None:
self.address_n: Sequence["int"] = address_n if address_n is not None else [] self.address_n: Sequence["int"] = address_n if address_n is not None else []
self.message = message self.message = message
self.encoded_network = encoded_network self.encoded_network = encoded_network
self.chunkify = chunkify
class EthereumMessageSignature(protobuf.MessageType): class EthereumMessageSignature(protobuf.MessageType):
@ -5102,6 +5111,7 @@ class EthereumVerifyMessage(protobuf.MessageType):
2: protobuf.Field("signature", "bytes", repeated=False, required=True), 2: protobuf.Field("signature", "bytes", repeated=False, required=True),
3: protobuf.Field("message", "bytes", repeated=False, required=True), 3: protobuf.Field("message", "bytes", repeated=False, required=True),
4: protobuf.Field("address", "string", 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__( def __init__(
@ -5110,10 +5120,12 @@ class EthereumVerifyMessage(protobuf.MessageType):
signature: "bytes", signature: "bytes",
message: "bytes", message: "bytes",
address: "str", address: "str",
chunkify: Optional["bool"] = None,
) -> None: ) -> None:
self.signature = signature self.signature = signature
self.message = message self.message = message
self.address = address self.address = address
self.chunkify = chunkify
class EthereumSignTypedHash(protobuf.MessageType): class EthereumSignTypedHash(protobuf.MessageType):