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:
parent
00628b5596
commit
4e8c8d4054
2 changed files with 50 additions and 3 deletions
|
@ -4,6 +4,7 @@ import typing
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from struct import Struct
|
from struct import Struct
|
||||||
from scribe.schema.claim import Claim
|
from scribe.schema.claim import Claim
|
||||||
|
from scribe.common import double_sha256
|
||||||
|
|
||||||
if (sys.version_info.major, sys.version_info.minor) > (3, 7):
|
if (sys.version_info.major, sys.version_info.minor) > (3, 7):
|
||||||
cachedproperty = functools.cached_property
|
cachedproperty = functools.cached_property
|
||||||
|
@ -84,6 +85,51 @@ class Tx(typing.NamedTuple):
|
||||||
flag: typing.Optional[int] = None
|
flag: typing.Optional[int] = None
|
||||||
witness: typing.Optional[typing.List[typing.List[bytes]]] = 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):
|
class TxInput(typing.NamedTuple):
|
||||||
prev_hash: bytes
|
prev_hash: bytes
|
||||||
|
|
|
@ -1668,13 +1668,14 @@ class LBRYElectrumX(asyncio.Protocol):
|
||||||
verbose: passed on to the daemon
|
verbose: passed on to the daemon
|
||||||
"""
|
"""
|
||||||
assert_tx_hash(txid)
|
assert_tx_hash(txid)
|
||||||
if verbose not in (True, False):
|
verbose = bool(verbose)
|
||||||
raise RPCError(BAD_REQUEST, f'"verbose" must be a boolean')
|
|
||||||
tx_hash_bytes = bytes.fromhex(txid)[::-1]
|
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)
|
raw_tx = await asyncio.get_event_loop().run_in_executor(None, self.db.get_raw_tx, tx_hash_bytes)
|
||||||
if raw_tx:
|
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.")
|
return RPCError("No such mempool or blockchain transaction.")
|
||||||
|
|
||||||
def _get_merkle_branch(self, tx_hashes, tx_pos):
|
def _get_merkle_branch(self, tx_hashes, tx_pos):
|
||||||
|
|
Loading…
Reference in a new issue