From 84230c921ac451ee0f140ac6ab80e1febac6aec3 Mon Sep 17 00:00:00 2001 From: Coline Date: Fri, 22 Jul 2022 10:55:32 +0200 Subject: [PATCH] fix: merge some similary code and clean code --- .../speculos/ethereum_client/ethereum_cmd.py | 7 +- .../ethereum_client/ethereum_cmd_builder.py | 112 ++---------------- 2 files changed, 10 insertions(+), 109 deletions(-) diff --git a/tests/speculos/ethereum_client/ethereum_cmd.py b/tests/speculos/ethereum_client/ethereum_cmd.py index aa51f79..1ee1e59 100644 --- a/tests/speculos/ethereum_client/ethereum_cmd.py +++ b/tests/speculos/ethereum_client/ethereum_cmd.py @@ -120,7 +120,6 @@ class EthereumCommand: data=chunk[5:]) as exchange: yield exchange response: bytes = exchange.receive() - print(response) except ApduException as error: raise DeviceException(error_code=error.sw, ins=InsType.INS_PERFORM_PRIVACY_OPERATION) @@ -148,8 +147,6 @@ class EthereumCommand: yield exchange result.append(exchange.receive()) - #response: bytes = exchange.receive() - except ApduException as error: raise DeviceException(error_code=error.sw, ins=InsType.INS_SIGN_TX) @@ -169,7 +166,7 @@ class EthereumCommand: except ApduException as error: raise DeviceException(error_code=error.sw, ins=InsType.INS_SIGN_TX) - # response = V (1) || R (32) || S (32) + # response = V (1) || R (32) || S (32) assert len(response) == 65 v, r, s = parse_sign_response(response) @@ -192,7 +189,7 @@ class EthereumCommand: except ApduException as error: raise DeviceException(error_code=error.sw, ins=InsType.INS_SIGN_EIP712) - # response = V (1) || R (32) || S (32) + # response = V (1) || R (32) || S (32) assert len(response) == 65 v, r, s = parse_sign_response(response) diff --git a/tests/speculos/ethereum_client/ethereum_cmd_builder.py b/tests/speculos/ethereum_client/ethereum_cmd_builder.py index d1e9434..1592309 100644 --- a/tests/speculos/ethereum_client/ethereum_cmd_builder.py +++ b/tests/speculos/ethereum_client/ethereum_cmd_builder.py @@ -124,71 +124,21 @@ class EthereumCommandBuilder: p2=0x00, cdata=b"") - def set_plugin(self, plugin: Plugin) -> bytes: - """Command builder for SET_PLUGIN. - - Parameters - ---------- - -> Check documentation of APDU - - Returns - ------- - bytes - APDU command for SET_PLUGIN. - - """ - - cdata: bytes = plugin.serialize() - + def _same_header_builder(self, data: Union[Plugin, ERC20_Information], ins: int) -> bytes: return self.serialize(cla=self.CLA, - ins=InsType.INS_SET_PLUGIN, + ins=ins, p1=0x00, p2=0x00, - cdata=cdata) + cdata=data.serialize()) + + def set_plugin(self, plugin: Plugin) -> bytes: + return self._same_header_builder(plugin, InsType.INS_SET_PLUGIN) def provide_nft_information(self, plugin: Plugin) -> bytes: - """Command builder for PROVIDE_NFT_INFORMATION. - - Parameters - ---------- - -> Check documentation of APDU - - Returns - ------- - bytes - APDU command for PROVIDE_NFT_INFORMATION. - - """ - - cdata: bytes = plugin.serialize() - - return self.serialize(cla=self.CLA, - ins=InsType.INS_PROVIDE_NFT_INFORMATION, - p1=0x00, - p2=0x00, - cdata=cdata) + return self._same_header_builder(plugin, InsType.INS_PROVIDE_NFT_INFORMATION) def provide_erc20_token_information(self, info: ERC20_Information): - """Command builder for PROVIDE_ERC20_INFORMATION. - - Parameters - ---------- - -> Check documentation of APDU - - Returns - ------- - bytes - APDU command for PROVIDE_ERC20_INFORMATION. - - """ - - cdata: bytes = info.serialize() - - return self.serialize(cla=self.CLA, - ins=InsType.INS_PROVIDE_ERC20, - p1=0x00, - p2=0x00, - cdata=cdata) + return self._same_header_builder(info, InsType.INS_PROVIDE_ERC20) def get_public_key(self, bip32_path: str, display: bool = False) -> bytes: """Command builder for GET_PUBLIC_KEY. @@ -243,52 +193,6 @@ class EthereumCommandBuilder: p2=0x01 if shared_secret else 0x00, cdata=cdata) - # Not use - def sign_tx(self, bip32_path: str, transaction: Transaction) -> Iterator[Tuple[bool, bytes]]: - """Command builder for INS_SIGN_TX. - - Parameters - ---------- - bip32_path : str - String representation of BIP32 path. - transaction : Transaction - Representation of the transaction to be signed. - - Yields - ------- - bytes - APDU command chunk for INS_SIGN_TX. - - """ - bip32_paths: List[bytes] = bip32_path_from_string(bip32_path) - - cdata: bytes = b"".join([ - len(bip32_paths).to_bytes(1, byteorder="big"), - *bip32_paths - ]) - - yield False, self.serialize(cla=self.CLA, - ins=InsType.INS_SIGN_TX, - p1=0x00, - p2=0x00, - cdata=cdata) - - tx: bytes = transaction.serialize() - - for i, (is_last, chunk) in enumerate(chunkify(tx, MAX_APDU_LEN)): - if is_last: - yield True, self.serialize(cla=self.CLA, - ins=InsType.INS_SIGN_TX, - p1=0x00, - p2=0x00, - cdata=chunk) - return - else: - yield False, self.serialize(cla=self.CLA, - ins=InsType.INS_SIGN_TX, - p1=0x00, - p2=0x00, - cdata=chunk) def simple_sign_tx(self, bip32_path: str, transaction: Transaction) -> bytes: """Command builder for INS_SIGN_TX.