from struct import unpack import json from .tools import * from .address import PrivateKey from binascii import hexlify, unhexlify class Transaction(dict): def __init__(self, raw_tx=None): self["format"] = "raw" self["txId"] = None self["hash"] = None self["version"] = None self["size"] = 0 self["vSize"] = 0 self["bSize"] = 0 self["lockTime"] = None self["vIn"] = dict() self["vOut"] = dict() self["rawTx"] = None self["blockHash"] = None self["confirmations"] = None self["time"] = None self["blockTime"] = None self["blockIndex"] = None self["coinbase"] = False self["fee"] = None self["data"] = None self["amount"] = 0 if raw_tx is None: return stream = self.get_stream(raw_tx) start = stream.tell() (self["version"],) = unpack('= 0 # assert type(sequence) == int # assert sequence <= 0xffffffff and sequence >= 0 # if type(script_sig) == str: # script_sig = unhexlify(script_sig) # else: # assert type(script_sig) == bytes # assert len(script_sig) <= 520 # if private_key: # if type(private_key) != PrivateKey: # private_key = PrivateKey(private_key) # if amount: # assert type(amount) == int # assert amount >= 0 and amount <= MAX_AMOUNT # if tx_in_witness: # assert type(tx_in_witness) == list # l = 0 # witness = [] # for w in tx_in_witness: # if type(w) == str: # witness.append(unhexlify(w) if self["format"] == "raw" else w) # else: # witness.append(w if self["format"] == "raw" else unhexlify(w)) # l += 1 + len(w) # if len(w) >= 0x4c: # l += 1 # if len(w) > 0xff: # l += 1 # # witness script limit # assert l <= 10000 # if tx_id == b"\x00" * 32: # assert v_out == 0 and sequence == 0xffffffff and len(script_sig) <= 100 # self["coinbase"] = True # # k = len(self["vIn"]) # self["vIn"][k] = dict() # self["vIn"][k]["vOut"] = v_out # self["vIn"][k]["sequence"] = sequence # if self["format"] == "raw": # self["vIn"][k]["txId"] = tx_id # self["vIn"][k]["scriptSig"] = script_sig # if tx_in_witness: # self["segwit"] = True # self["vIn"][k]["txInWitness"] = witness # else: # self["vIn"][k]["txId"] = rh2s(tx_id) # self["vIn"][k]["scriptSig"] = script_sig # self["vIn"][i]["scriptSigOpcodes"] = decode_script(script_sig) # self["vIn"][i]["scriptSigAsm"] = decode_script(script_sig, 1) # if tx_in_witness: # self["segwit"] = True # self["vIn"][k]["txInWitness"] = witness # if amount: # self["value"] = amount # if private_key: # self["privateKey"] = private_key # # # todo # # if self["vOut"]: # # self.__refresh_tx__() # # """ # написать сценарии использования # """