2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
2016-11-25 21:53:55 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
import json
|
2014-05-28 12:38:44 +00:00
|
|
|
from decimal import Decimal
|
2018-11-02 15:21:28 +00:00
|
|
|
import random
|
|
|
|
from typing import Mapping, Any
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2016-05-20 14:08:55 +00:00
|
|
|
import requests
|
2016-05-26 15:20:44 +00:00
|
|
|
|
2018-11-02 15:21:28 +00:00
|
|
|
from . import messages
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2016-11-28 15:01:45 +00:00
|
|
|
cache_dir = None
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2018-11-02 15:21:28 +00:00
|
|
|
def is_zcash(coin):
|
|
|
|
return coin["coin_name"].lower().startswith("zcash")
|
|
|
|
|
|
|
|
|
|
|
|
def is_capricoin(coin):
|
|
|
|
return coin["coin_name"].lower().startswith("capricoin")
|
|
|
|
|
|
|
|
|
|
|
|
def _json_to_input(coin, vin):
|
|
|
|
i = messages.TxInputType()
|
|
|
|
if "coinbase" in vin:
|
|
|
|
i.prev_hash = b"\0" * 32
|
|
|
|
i.prev_index = 0xFFFFFFFF # signed int -1
|
|
|
|
i.script_sig = bytes.fromhex(vin["coinbase"])
|
|
|
|
i.sequence = vin["sequence"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
i.prev_hash = bytes.fromhex(vin["txid"])
|
|
|
|
i.prev_index = vin["vout"]
|
|
|
|
i.script_sig = bytes.fromhex(vin["scriptSig"]["hex"])
|
|
|
|
i.sequence = vin["sequence"]
|
|
|
|
|
|
|
|
if coin["decred"]:
|
|
|
|
i.decred_tree = vin["tree"]
|
|
|
|
# TODO: support amountIn, blockHeight, blockIndex
|
|
|
|
|
|
|
|
return i
|
|
|
|
|
|
|
|
|
|
|
|
def _json_to_bin_output(coin, vout):
|
|
|
|
o = messages.TxOutputBinType()
|
|
|
|
o.amount = int(Decimal(vout["value"]) * 100000000)
|
|
|
|
o.script_pubkey = bytes.fromhex(vout["scriptPubKey"]["hex"])
|
|
|
|
if coin["bip115"] and o.script_pubkey[-1] == 0xB4:
|
|
|
|
# Verify if coin implements replay protection bip115 and script includes
|
|
|
|
# checkblockatheight opcode. 0xb4 - is op_code (OP_CHECKBLOCKATHEIGHT)
|
|
|
|
# <OP_32> <32-byte block hash> <OP_3> <3-byte block height> <OP_CHECKBLOCKATHEIGHT>
|
|
|
|
tail = o.script_pubkey[-38:]
|
|
|
|
o.block_hash = tail[1:33] # <32-byte block hash>
|
|
|
|
o.block_height = int.from_bytes(tail[34:37], "little") # <3-byte block height>
|
|
|
|
if coin["decred"]:
|
|
|
|
o.decred_script_version = vout["version"]
|
|
|
|
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
def json_to_tx(coin, data):
|
|
|
|
t = messages.TransactionType()
|
|
|
|
t.version = data["version"]
|
|
|
|
t.lock_time = data.get("locktime")
|
|
|
|
|
|
|
|
if is_capricoin(coin):
|
|
|
|
t.timestamp = data["time"]
|
|
|
|
|
|
|
|
if coin["decred"]:
|
|
|
|
t.expiry = data["expiry"]
|
|
|
|
|
|
|
|
if is_zcash(coin):
|
|
|
|
t.overwintered = data.get("fOverwintered", False)
|
|
|
|
t.expiry = data.get("nExpiryHeight", None)
|
|
|
|
t.version_group_id = data.get("nVersionGroupId", None)
|
|
|
|
|
|
|
|
t.inputs = [_json_to_input(coin, vin) for vin in data["vin"]]
|
|
|
|
t.bin_outputs = [_json_to_bin_output(coin, vout) for vout in data["vout"]]
|
|
|
|
|
|
|
|
# zcash extra data
|
|
|
|
if is_zcash(coin) and t.version >= 2:
|
|
|
|
joinsplit_cnt = len(data["vjoinsplit"])
|
|
|
|
if joinsplit_cnt == 0:
|
|
|
|
t.extra_data = b"\x00"
|
|
|
|
elif joinsplit_cnt >= 253:
|
|
|
|
# we assume cnt < 253, so we can treat varIntLen(cnt) as 1
|
|
|
|
raise ValueError("Too many joinsplits")
|
|
|
|
elif "hex" not in data:
|
|
|
|
raise ValueError("Raw TX data required for Zcash joinsplit transaction")
|
|
|
|
else:
|
|
|
|
rawtx = bytes.fromhex(data["hex"])
|
|
|
|
extra_data_len = 1 + joinsplit_cnt * 1802 + 32 + 64
|
|
|
|
t.extra_data = rawtx[-extra_data_len:]
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
class TxApi:
|
|
|
|
def __init__(self, coin_data):
|
|
|
|
self.coin_data = coin_data
|
|
|
|
if coin_data["blockbook"]:
|
|
|
|
self.url = random.choice(coin_data["blockbook"])
|
|
|
|
self.pushtx_url = self.url + "/sendtx"
|
|
|
|
elif coin_data["bitcore"]:
|
|
|
|
self.url = random.choice(coin_data["bitcore"])
|
2018-08-21 16:56:25 +00:00
|
|
|
self.pushtx_url = self.url + "/tx/send"
|
2018-11-02 15:21:28 +00:00
|
|
|
else:
|
|
|
|
raise ValueError("No API URL in coin data")
|
|
|
|
|
|
|
|
def fetch_json(self, *path, **params):
|
|
|
|
url = self.url + "/api/" + "/".join(map(str, path))
|
|
|
|
return requests.get(url, params=params).json(parse_float=Decimal)
|
2018-08-21 16:56:25 +00:00
|
|
|
|
|
|
|
def get_block_hash(self, block_number):
|
|
|
|
j = self.fetch_json("block-index", block_number)
|
2018-09-12 22:44:08 +00:00
|
|
|
return bytes.fromhex(j["blockHash"])
|
2018-08-21 16:56:25 +00:00
|
|
|
|
|
|
|
def current_height(self):
|
2018-11-02 15:21:28 +00:00
|
|
|
j = self.fetch_json("status", q="getBlockCount")
|
|
|
|
return j["info"]["blocks"]
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2018-11-02 15:21:28 +00:00
|
|
|
def __getitem__(self, txhash):
|
|
|
|
return self.get_tx(txhash.hex())
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2018-11-02 15:21:28 +00:00
|
|
|
def get_tx_data(self, txhash):
|
2018-08-13 16:21:24 +00:00
|
|
|
data = self.fetch_json("tx", txhash)
|
2018-11-02 15:21:28 +00:00
|
|
|
if is_zcash(self.coin_data) and data.get("vjoinsplit") and "hex" not in data:
|
|
|
|
j = self.fetch_json("rawtx", txhash)
|
|
|
|
data["hex"] = j["rawtx"]
|
|
|
|
return data
|
2016-05-01 12:21:20 +00:00
|
|
|
|
2018-11-02 15:21:28 +00:00
|
|
|
def get_tx(self, txhash):
|
|
|
|
data = self.get_tx_data(txhash)
|
|
|
|
return json_to_tx(self.coin_data, data)
|