From 4e8c8d405416787476d08059b09ab78c3315c01d Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 2 May 2022 14:59:39 -0400 Subject: [PATCH] return transaction as json if `verbose` is given to `blockchain.transaction.get` -fixes https://github.com/lbryio/scribe/issues/27 --- scribe/blockchain/transaction/__init__.py | 46 +++++++++++++++++++++++ scribe/hub/session.py | 7 ++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/scribe/blockchain/transaction/__init__.py b/scribe/blockchain/transaction/__init__.py index 6064314..07cb2d8 100644 --- a/scribe/blockchain/transaction/__init__.py +++ b/scribe/blockchain/transaction/__init__.py @@ -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 diff --git a/scribe/hub/session.py b/scribe/hub/session.py index 3ffce6d..cb4a45a 100644 --- a/scribe/hub/session.py +++ b/scribe/hub/session.py @@ -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):