From 0f71cbcc44acb2983794e11bf7e5ad010f4492fa Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 21 Aug 2018 18:56:25 +0200 Subject: [PATCH] tx_api: update to assume "/api" path again, let blockHash request be cached --- .../insight_zencash_block-index_110000.json | 1 + trezorlib/tx_api.py | 30 ++++++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) create mode 100644 trezorlib/tests/txcache/insight_zencash_block-index_110000.json diff --git a/trezorlib/tests/txcache/insight_zencash_block-index_110000.json b/trezorlib/tests/txcache/insight_zencash_block-index_110000.json new file mode 100644 index 000000000..bac570dad --- /dev/null +++ b/trezorlib/tests/txcache/insight_zencash_block-index_110000.json @@ -0,0 +1 @@ +{"blockHash": "000000003f5d6ba1385c6cd2d4f836dfc5adf7f98834309ad67e26faef462454"} \ No newline at end of file diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index 4d29bf29c..949b68fb6 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -30,21 +30,8 @@ class TxApi(object): self.network = network self.url = url - def get_url(self, resource, resourceid): - url = "%s%s/%s" % (self.url, resource, resourceid) - return url - - def current_height(self): - r = requests.get(self.url + "/status?q=getBlockCount") - j = r.json(parse_float=str) - block_height = j["info"]["blocks"] - return block_height - - def get_block_hash(self, block_number): - r = requests.get(self.url + "/block-index/" + str(block_number)) - j = r.json(parse_float=str) - block_hash = binascii.unhexlify(j["blockHash"]) - return block_hash + def get_url(self, *args): + return "/".join(map(str, [self.url, "api", *args])) def fetch_json(self, resource, resourceid): global cache_dir @@ -87,8 +74,17 @@ class TxApiInsight(TxApi): self.zcash = zcash self.bip115 = bip115 if url: - prefix, suffix = url.rsplit("/", maxsplit=1) - self.pushtx_url = prefix + "/tx/send" + self.pushtx_url = self.url + "/tx/send" + + def get_block_hash(self, block_number): + j = self.fetch_json("block-index", block_number) + return binascii.unhexlify(j["blockHash"]) + + def current_height(self): + r = requests.get(self.get_url("status?q=getBlockCount")) + j = r.json(parse_float=str) + block_height = j["info"]["blocks"] + return block_height def get_tx(self, txhash):