2018-10-30 17:52:52 +01:00
|
|
|
import logging
|
2018-08-16 01:23:06 +02:00
|
|
|
from decimal import Decimal
|
|
|
|
from binascii import hexlify
|
|
|
|
from datetime import datetime
|
|
|
|
from json import JSONEncoder
|
2018-10-26 00:54:07 +02:00
|
|
|
from ecdsa import BadSignatureError
|
2018-08-16 17:24:22 +02:00
|
|
|
from lbrynet.wallet.transaction import Transaction, Output
|
2018-10-03 22:38:47 +02:00
|
|
|
from lbrynet.wallet.dewies import dewies_to_lbc
|
2018-10-12 15:49:13 +02:00
|
|
|
from lbrynet.wallet.ledger import MainNetLedger
|
2018-08-16 01:23:06 +02:00
|
|
|
|
|
|
|
|
2018-10-30 17:52:52 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-08-16 01:23:06 +02:00
|
|
|
class JSONResponseEncoder(JSONEncoder):
|
|
|
|
|
2018-10-12 15:49:13 +02:00
|
|
|
def __init__(self, *args, ledger: MainNetLedger, **kwargs):
|
2018-08-16 01:23:06 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.ledger = ledger
|
|
|
|
|
2018-08-16 17:24:22 +02:00
|
|
|
def default(self, obj): # pylint: disable=method-hidden
|
2018-08-16 01:23:06 +02:00
|
|
|
if isinstance(obj, Transaction):
|
|
|
|
return self.encode_transaction(obj)
|
|
|
|
if isinstance(obj, Output):
|
|
|
|
return self.encode_output(obj)
|
|
|
|
if isinstance(obj, datetime):
|
|
|
|
return obj.strftime("%Y%m%dT%H:%M:%S")
|
|
|
|
if isinstance(obj, Decimal):
|
|
|
|
return float(obj)
|
2018-08-16 21:55:33 +02:00
|
|
|
if isinstance(obj, bytes):
|
|
|
|
return obj.decode()
|
2018-08-16 01:23:06 +02:00
|
|
|
return super().default(obj)
|
|
|
|
|
|
|
|
def encode_transaction(self, tx):
|
|
|
|
return {
|
|
|
|
'txid': tx.id,
|
2018-09-22 04:19:32 +02:00
|
|
|
'height': tx.height,
|
2018-08-16 01:23:06 +02:00
|
|
|
'inputs': [self.encode_input(txo) for txo in tx.inputs],
|
|
|
|
'outputs': [self.encode_output(txo) for txo in tx.outputs],
|
2018-10-03 22:38:47 +02:00
|
|
|
'total_input': dewies_to_lbc(tx.input_sum),
|
|
|
|
'total_output': dewies_to_lbc(tx.input_sum - tx.fee),
|
|
|
|
'total_fee': dewies_to_lbc(tx.fee),
|
2018-08-16 01:23:06 +02:00
|
|
|
'hex': hexlify(tx.raw).decode(),
|
|
|
|
}
|
|
|
|
|
|
|
|
def encode_output(self, txo):
|
2018-10-12 15:49:13 +02:00
|
|
|
tx_height = txo.tx_ref.height
|
|
|
|
best_height = self.ledger.headers.height
|
2018-09-22 04:19:32 +02:00
|
|
|
output = {
|
2018-09-19 15:58:50 +02:00
|
|
|
'txid': txo.tx_ref.id,
|
2018-08-16 01:23:06 +02:00
|
|
|
'nout': txo.position,
|
2018-10-03 22:38:47 +02:00
|
|
|
'amount': dewies_to_lbc(txo.amount),
|
2018-09-19 15:58:50 +02:00
|
|
|
'address': txo.get_address(self.ledger),
|
2018-10-12 15:49:13 +02:00
|
|
|
'height': tx_height,
|
|
|
|
'confirmations': best_height - tx_height if tx_height > 0 else tx_height
|
2018-08-16 01:23:06 +02:00
|
|
|
}
|
2018-09-22 04:19:32 +02:00
|
|
|
if txo.is_change is not None:
|
|
|
|
output['is_change'] = txo.is_change
|
2018-10-05 15:02:02 +02:00
|
|
|
if txo.is_my_account is not None:
|
|
|
|
output['is_mine'] = txo.is_my_account
|
2018-10-08 16:41:07 +02:00
|
|
|
|
2018-10-05 15:02:02 +02:00
|
|
|
if txo.script.is_claim_involved:
|
|
|
|
output.update({
|
|
|
|
'name': txo.claim_name,
|
|
|
|
'claim_id': txo.claim_id,
|
|
|
|
'permanent_url': txo.permanent_url,
|
|
|
|
})
|
2018-10-08 16:41:07 +02:00
|
|
|
|
|
|
|
if txo.script.is_claim_name or txo.script.is_update_claim:
|
2018-10-12 15:49:13 +02:00
|
|
|
claim = txo.claim
|
|
|
|
output['value'] = claim.claim_dict
|
|
|
|
if claim.has_signature:
|
|
|
|
output['valid_signature'] = None
|
|
|
|
if txo.channel is not None:
|
2018-10-18 03:10:23 +02:00
|
|
|
output['channel_name'] = txo.channel.claim_name
|
2018-10-26 00:54:07 +02:00
|
|
|
try:
|
|
|
|
output['valid_signature'] = claim.validate_signature(
|
|
|
|
txo.get_address(self.ledger), txo.channel.claim
|
|
|
|
)
|
|
|
|
except BadSignatureError:
|
|
|
|
output['valid_signature'] = False
|
2018-10-30 17:52:52 +01:00
|
|
|
except ValueError:
|
|
|
|
log.exception(f'txo.id: {txo.id}, txo.channel.id:{txo.channel.id}, output: {output}')
|
|
|
|
output['valid_signature'] = False
|
2018-10-08 16:41:07 +02:00
|
|
|
|
2018-10-05 15:02:02 +02:00
|
|
|
if txo.script.is_claim_name:
|
2018-10-12 15:49:13 +02:00
|
|
|
output['type'] = 'claim'
|
2018-10-05 15:02:02 +02:00
|
|
|
elif txo.script.is_update_claim:
|
2018-10-12 15:49:13 +02:00
|
|
|
output['type'] = 'update'
|
2018-10-05 15:02:02 +02:00
|
|
|
elif txo.script.is_support_claim:
|
2018-10-12 15:49:13 +02:00
|
|
|
output['type'] = 'support'
|
|
|
|
else:
|
|
|
|
output['type'] = 'basic'
|
2018-10-08 16:41:07 +02:00
|
|
|
|
2018-09-22 04:19:32 +02:00
|
|
|
return output
|
2018-08-16 01:23:06 +02:00
|
|
|
|
|
|
|
def encode_input(self, txi):
|
2018-09-22 04:19:32 +02:00
|
|
|
return self.encode_output(txi.txo_ref.txo) if txi.txo_ref.txo is not None else {
|
|
|
|
'txid': txi.txo_ref.tx_ref.id,
|
|
|
|
'nout': txi.txo_ref.position
|
|
|
|
}
|