Merge pull request #30 from lbryio/get-transaction-json

fix `verbose` arg for `blockchain.transaction.get`
This commit is contained in:
Jack Robison 2022-05-02 18:07:02 -04:00 committed by GitHub
commit 95495aa786
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 4 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, # FIXME: add this
"locktime": self.locktime,
"vin": [
{
"txid": txin.prev_hash[::-1].hex(),
"vout": txin.prev_idx,
"scriptSig": {
"asm": None, # FIXME: add this
"hex": txin.script.hex()
},
"sequence": txin.sequence
} for txin in self.inputs
],
"vout": [
{
"value": txo.value / 1E8,
"n": txo.nout,
"scriptPubKey": {
"asm": None, # FIXME: add this
"hex": txo.pk_script.hex(),
"reqSigs": 1, # FIXME: what if it isn't 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

@ -284,7 +284,10 @@ class Env:
return self.PD_ON
def extract_peer_hubs(self):
return [hub.strip() for hub in self.default('PEER_HUBS', '').split(',')]
peer_hubs = self.default('PEER_HUBS', '')
if not peer_hubs:
return []
return [hub.strip() for hub in peer_hubs.split(',')]
@classmethod
def contribute_to_arg_parser(cls, parser):

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:
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):