From e7a56899c69938cbb7963600cfe09ed5b2c6e29f Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 19 Jun 2017 00:00:26 +0200 Subject: [PATCH] more python3 compatibility --- tests/device_tests/test_msg_ethereum_getaddress.py | 10 +++++----- trezorlib/client.py | 4 ++-- trezorlib/tx_api.py | 13 +++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/device_tests/test_msg_ethereum_getaddress.py b/tests/device_tests/test_msg_ethereum_getaddress.py index a8ceec09aa..55ad039eaa 100644 --- a/tests/device_tests/test_msg_ethereum_getaddress.py +++ b/tests/device_tests/test_msg_ethereum_getaddress.py @@ -25,11 +25,11 @@ class TestMsgEthereumGetaddress(common.TrezorTest): def test_ethereum_getaddress(self): self.setup_mnemonic_nopin_nopassphrase() - self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([])), '1d1c328764a41bda0492b66baa30c4a339ff85ef') - self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([1])), '437207ca3cf43bf2e47dea0756d736c5df4f597a') - self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([0, -1])), 'e5d96dfa07bcf1a3ae43677840c31394258861bf') - self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([-9, 0])), 'f68804ac9eca9483ab4241d3e4751590d2c05102') - self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([0, 9999999])), '7a6366ecfcaf0d5dcc1539c171696c6cdd1eb8ed') + self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([])), b'1d1c328764a41bda0492b66baa30c4a339ff85ef') + self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([1])), b'437207ca3cf43bf2e47dea0756d736c5df4f597a') + self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([0, -1])), b'e5d96dfa07bcf1a3ae43677840c31394258861bf') + self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([-9, 0])), b'f68804ac9eca9483ab4241d3e4751590d2c05102') + self.assertEqual(binascii.hexlify(self.client.ethereum_get_address([0, 9999999])), b'7a6366ecfcaf0d5dcc1539c171696c6cdd1eb8ed') if __name__ == '__main__': unittest.main() diff --git a/trezorlib/client.py b/trezorlib/client.py index 003bc2cc09..4b68b969ba 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -691,7 +691,7 @@ class ProtocolMixin(object): tx = msg.transactions.add() if self.tx_api: - tx.CopyFrom(self.tx_api.get_tx(binascii.hexlify(inp.prev_hash))) + tx.CopyFrom(self.tx_api.get_tx(binascii.hexlify(inp.prev_hash).decode('utf-8'))) else: raise Exception('TX_API not defined') known_hashes.append(inp.prev_hash) @@ -716,7 +716,7 @@ class ProtocolMixin(object): continue if self.tx_api: - txes[inp.prev_hash] = self.tx_api.get_tx(binascii.hexlify(inp.prev_hash)) + txes[inp.prev_hash] = self.tx_api.get_tx(binascii.hexlify(inp.prev_hash).decode('utf-8')) else: raise Exception('TX_API not defined') known_hashes.append(inp.prev_hash) diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index 6f406b48f1..6c12451489 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -31,7 +31,7 @@ class TxApi(object): self.network = network self.url = url - def fetch_json(self, url, resource, resourceid): + def fetch_json(self, resource, resourceid): global cache_dir if cache_dir: cache_file = '%s/%s_%s_%s.json' % (cache_dir, self.network, resource, resourceid) @@ -41,7 +41,8 @@ class TxApi(object): except: pass try: - r = requests.get('%s%s/%s' % (self.url, resource, resourceid), headers={'User-agent': 'Mozilla/5.0'}) + url = '%s%s/%s' % (self.url, resource, resourceid) + r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'}) j = r.json() except: raise Exception('URL error: %s' % url) @@ -64,7 +65,7 @@ class TxApiInsight(TxApi): def get_tx(self, txhash): - data = self.fetch_json(self.url, 'tx', txhash) + data = self.fetch_json('tx', txhash) t = proto_types.TransactionType() t.version = data['version'] @@ -99,7 +100,7 @@ class TxApiInsight(TxApi): # we assume cnt < 253, so we can treat varIntLen(cnt) as 1 raise ValueError('Too many joinsplits') extra_data_len = 1 + joinsplit_cnt * 1802 + 32 + 64 - raw = fetch_json(self.url, 'rawtx', txhash) + raw = self.fetch_json('rawtx', txhash) raw = binascii.unhexlify(raw['rawtx']) t.extra_data = raw[-extra_data_len:] @@ -110,7 +111,7 @@ class TxApiSmartbit(TxApi): def get_tx(self, txhash): - data = self.fetch_json(self.url, 'tx', txhash) + data = self.fetch_json('tx', txhash) data = data['transaction'] @@ -144,7 +145,7 @@ class TxApiBlockCypher(TxApi): def get_tx(self, txhash): - data = self.fetch_json(self.url, 'txs', txhash) + data = self.fetch_json('txs', txhash) t = proto_types.TransactionType() t.version = data['ver']