Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def gettransaction(self, tx_id):
res = self.compose_request('dashboards/transaction/', data=tx_id)
tx = res['data'][tx_id]['transaction']
confirmations = res['context']['state'] - tx['block_id']
status = 'unconfirmed'
if confirmations:
status = 'confirmed'
witness_type = 'legacy'
if tx['has_witness']:
witness_type = 'segwit'
input_total = tx['input_total']
if tx['is_coinbase']:
input_total = tx['output_total']
t = Transaction(locktime=tx['lock_time'], version=tx['version'], network=self.network,
fee=tx['fee'], size=tx['size'], hash=tx['hash'],
date=datetime.strptime(tx['time'], "%Y-%m-%d %H:%M:%S"),
confirmations=confirmations, block_height=tx['block_id'], status=status,
input_total=input_total, coinbase=tx['is_coinbase'],
output_total=tx['output_total'], witness_type=witness_type)
index_n = 0
if not res['data'][tx_id]['inputs']:
# This is a coinbase transaction, add input
t.add_input(prev_hash=b'\00' * 32, output_n=0, value=input_total)
for ti in res['data'][tx_id]['inputs']:
if ti['spending_witness']:
witnesses = b"".join([varstr(to_bytes(x)) for x in ti['spending_witness'].split(",")])
t.add_input(prev_hash=ti['transaction_hash'], output_n=ti['index'],
unlocking_script=witnesses, index_n=index_n, value=ti['value'],
address=ti['recipient'], witness_type='segwit')
else:
def transaction_import(self, t):
"""
Import a Transaction into this wallet. Link inputs to wallet keys if possible and return HDWalletTransaction
object. Only imports Transaction objects or dictionaries, use transaction_import_raw method to import a
raw transaction.
:param t: A Transaction object or dictionary
:type t: Transaction, dict
:return HDWalletTransaction:
"""
if isinstance(t, Transaction):
rt = self.transaction_create(t.outputs, t.inputs, transaction_fee=t.fee, network=t.network.network_name)
elif isinstance(t, dict):
output_arr = []
for o in t['outputs']:
output_arr.append((o['address'], int(o['value'])))
input_arr = []
for i in t['inputs']:
signatures = [to_bytes(sig) for sig in i['signatures']]
script = b'' if 'script' not in i else i['script']
address = '' if 'address' not in i else i['address']
input_arr.append((i['prev_hash'], i['output_n'], None, int(i['value']), signatures, script,
address))
rt = self.transaction_create(output_arr, input_arr, transaction_fee=t['fee'], network=t['network'])
else:
raise WalletError("Import transaction must be of type Transaction or dict")
def gettransaction(self, tx_id):
tx = self.compose_request('transaction', tx_id)
rawtx = tx['raw']
t = Transaction.import_raw(rawtx, network=self.network)
if tx['confirmations']:
t.status = 'confirmed'
else:
t.status = 'unconfirmed'
if t.coinbase:
t.input_total = t.output_total
else:
t.input_total = tx['total_input_value']
t.output_total = tx['total_output_value']
t.fee = tx['total_fee']
t.hash = tx['hash']
t.block_hash = tx['block_hash']
t.block_height = tx['block_height']
t.confirmations = tx['confirmations']
t.date = datetime.strptime(tx['block_time'], "%Y-%m-%dT%H:%M:%S+%f")
def __init__(self, hdwallet, *args, **kwargs):
"""
Initialize HDWalletTransaction object with reference to a HDWallet object
:param hdwallet: HDWallet object, wallet name or ID
:type hdWallet: HDwallet, str, int
:param args: Arguments for HDWallet parent class
:type args: args
:param kwargs: Keyword arguments for HDWallet parent class
:type kwargs: kwargs
"""
assert isinstance(hdwallet, HDWallet)
self.hdwallet = hdwallet
self.pushed = False
self.error = None
Transaction.__init__(self, *args, **kwargs)
def _parse_transaction(self, tx):
status = 'unconfirmed'
if tx['confirmations']:
status = 'confirmed'
witness_type = 'legacy'
if 'inputs' in tx and [ti['witness'] for ti in tx['inputs'] if ti['witness'] and ti['witness'] != ['NULL']]:
witness_type = 'segwit'
input_total = tx['input_amount_int']
t_time = None
if tx['time']:
t_time = datetime.fromtimestamp(tx['time'])
if tx['coinbase']:
input_total = tx['output_amount_int']
t = Transaction(locktime=tx['locktime'], version=int(tx['version']), network=self.network, fee=tx['fee_int'],
size=tx['size'], hash=tx['txid'], date=t_time,
confirmations=tx['confirmations'], block_height=tx['block'], status=status,
input_total=input_total, coinbase=tx['coinbase'],
output_total=tx['output_amount_int'], witness_type=witness_type)
index_n = 0
if tx['coinbase']:
t.add_input(prev_hash=b'\00' * 32, output_n=0, value=input_total)
else:
for ti in tx['inputs']:
unlocking_script = ti['script_sig']['hex']
witness_type = 'legacy'
if ti['witness'] and ti['witness'] != ['NULL']:
witness_type = 'segwit'
unlocking_script = b"".join([varstr(to_bytes(x)) for x in ti['witness']])
# if tx['inputs']['witness']
def _convert_to_transaction(self, tx):
if tx['confirmations']:
status = 'confirmed'
else:
status = 'unconfirmed'
fees = None if 'fees' not in tx else int(round(float(tx['fees']) * self.units, 0))
value_in = 0 if 'valueIn' not in tx else tx['valueIn']
isCoinbase = False
if 'isCoinBase' in tx and tx['isCoinBase']:
value_in = tx['valueOut']
isCoinbase = True
t = Transaction(locktime=tx['locktime'], version=tx['version'], network=self.network,
fee=fees, size=tx['size'], hash=tx['txid'],
date=datetime.fromtimestamp(tx['blocktime']), confirmations=tx['confirmations'],
block_height=tx['blockheight'], block_hash=tx['blockhash'], status=status,
input_total=int(round(float(value_in) * self.units, 0)), coinbase=isCoinbase,
output_total=int(round(float(tx['valueOut']) * self.units, 0)))
for ti in tx['vin']:
if isCoinbase:
t.add_input(prev_hash=32 * b'\0', output_n=4*b'\xff', unlocking_script=ti['coinbase'], index_n=ti['n'],
script_type='coinbase', sequence=ti['sequence'])
else:
value = int(round(float(ti['value']) * self.units, 0))
t.add_input(prev_hash=ti['txid'], output_n=ti['vout'], unlocking_script=ti['scriptSig']['hex'],
index_n=ti['n'], value=value, sequence=ti['sequence'],
double_spend=False if ti['doubleSpentTxID'] is None else ti['doubleSpentTxID'])
for to in tx['vout']:
value = int(round(float(to['value']) * self.units, 0))
script_type = 'p2sh_p2wpkh'
elif usd['script_type'] == "p2wsh" and witness_script_type == 'p2sh':
inp_witness_type = 'p2sh-segwit'
script_type = 'p2sh_p2wsh'
inputs[n] = Input(prev_hash=inputs[n].prev_hash, output_n=inputs[n].output_n, keys=keys,
unlocking_script_unsigned=inputs[n].unlocking_script_unsigned,
unlocking_script=inputs[n].unlocking_script, sigs_required=sigs_required,
signatures=signatures, witness_type=inp_witness_type, script_type=script_type,
sequence=inputs[n].sequence, index_n=inputs[n].index_n, public_hash=public_hash,
network=inputs[n].network)
if len(rawtx[cursor:]) != 4:
raise TransactionError("Error when deserializing raw transaction, bytes left for locktime must be 4 not %d" %
len(rawtx[cursor:]))
locktime = change_base(rawtx[cursor:cursor + 4][::-1], 256, 10)
return Transaction(inputs, outputs, locktime, version, network, size=len(rawtx), output_total=output_total,
coinbase=coinbase, flag=flag, witness_type=witness_type, rawtx=to_hexstring(rawtx))
def _parse_transaction(self, tx, blockcount=None):
if not blockcount:
blockcount = self.blockcount()
confirmations = 0
block_height = None
if 'block_height' in tx['status']:
block_height = tx['status']['block_height']
confirmations = blockcount - block_height
status = 'unconfirmed'
if tx['status']['confirmed']:
status = 'confirmed'
fee = None if 'fee' not in tx else tx['fee']
t = Transaction(locktime=tx['locktime'], version=tx['version'], network=self.network,
fee=fee, size=tx['size'], hash=tx['txid'],
date=None if 'block_time' not in tx['status'] else datetime.fromtimestamp(tx['status']['block_time']),
confirmations=confirmations, block_height=block_height, status=status,
coinbase=tx['vin'][0]['is_coinbase'])
index_n = 0
for ti in tx['vin']:
if tx['vin'][0]['is_coinbase']:
t.add_input(prev_hash=ti['txid'], output_n=ti['vout'], index_n=index_n,
unlocking_script=ti['scriptsig'], value=sum([o['value'] for o in tx['vout']]))
else:
t.add_input(prev_hash=ti['txid'], output_n=ti['vout'],
unlocking_script_unsigned=ti['prevout']['scriptpubkey'], index_n=index_n,
value=ti['prevout']['value'], address=ti['prevout']['scriptpubkey_address'],
unlocking_script=ti['scriptsig'])
index_n += 1
index_n = 0
def gettransaction(self, tx_id):
tx = self.compose_request('txs', tx_id, variables={'includeHex': 'true'})
t = Transaction.import_raw(tx['hex'], network=self.network)
t.hash = tx_id
if tx['confirmations']:
t.status = 'confirmed'
t.date = datetime.strptime(tx['confirmed'][:19], "%Y-%m-%dT%H:%M:%S")
else:
t.status = 'unconfirmed'
t.confirmations = tx['confirmations']
t.block_height = tx['block_height']
t.block_hash = tx.get('block_hash')
t.fee = tx['fees']
t.rawtx = tx['hex']
t.size = int(len(tx['hex']) / 2)
t.network = self.network
t.input_total = 0
if t.coinbase:
t.input_total = t.output_total