return transaction as json if verbose is given to blockchain.transaction.get

-fixes https://github.com/lbryio/scribe/issues/27
This commit is contained in:
Jack Robison 2022-05-02 14:59:39 -04:00
parent 00628b5596
commit 4e8c8d4054
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 50 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import typing
from dataclasses import dataclass
from struct import Struct
from scribe.schema.claim import Claim
from scribe.common import double_sha256
if (sys.version_info.major, sys.version_info.minor) > (3, 7):
cachedproperty = functools.cached_property
@ -84,6 +85,51 @@ class Tx(typing.NamedTuple):
flag: typing.Optional[int] = None
witness: typing.Optional[typing.List[typing.List[bytes]]] = None
def as_dict(self, coin):
txid = double_sha256(self.raw)[::-1].hex()
result = {
"txid": txid,
"hash": txid,
"version": self.version,
"size": len(self.raw),
"vsize": len(self.raw),
"weight": None,
"locktime": self.locktime,
"vin": [
{
"txid": txin.prev_hash[::-1].hex(),
"vout": txin.prev_idx,
"scriptSig": {
"asm": None,
"hex": txin.script.hex()
},
"sequence": txin.sequence
} for txin in self.inputs
],
"vout": [
{
"value": txo.value / 1E8,
"n": txo.nout,
"scriptPubKey": {
"asm": None,
"hex": txo.pk_script.hex(),
"reqSigs": 1,
"type": "nonstandard" if (txo.is_support or txo.is_claim or txo.is_update) else "pubkeyhash" if txo.pubkey_hash else "scripthash",
"addresses": [
coin.claim_address_handler(txo)
]
}
} for txo in self.outputs
],
"hex": self.raw.hex()
}
for n, txo in enumerate(self.outputs):
if txo.is_support or txo.is_claim or txo.is_update:
result['vout'][n]["scriptPubKey"]["isclaim"] = txo.is_claim or txo.is_update
result['vout'][n]["scriptPubKey"]["issupport"] = txo.is_support
result['vout'][n]["scriptPubKey"]["subtype"] = "pubkeyhash" if txo.pubkey_hash else "scripthash"
return result
class TxInput(typing.NamedTuple):
prev_hash: bytes

View file

@ -1668,13 +1668,14 @@ class LBRYElectrumX(asyncio.Protocol):
verbose: passed on to the daemon
"""
assert_tx_hash(txid)
if verbose not in (True, False):
raise RPCError(BAD_REQUEST, f'"verbose" must be a boolean')
verbose = bool(verbose)
tx_hash_bytes = bytes.fromhex(txid)[::-1]
raw_tx = await asyncio.get_event_loop().run_in_executor(None, self.db.get_raw_tx, tx_hash_bytes)
if raw_tx:
return raw_tx.hex()
if not verbose:
return raw_tx.hex()
return self.coin.transaction(raw_tx).as_dict(self.coin)
return RPCError("No such mempool or blockchain transaction.")
def _get_merkle_branch(self, tx_hashes, tx_pos):