From af9e5c4c32b09a019534f354bf3c337d254d382f Mon Sep 17 00:00:00 2001 From: kafka2000 Date: Fri, 7 Jan 2022 08:12:06 +0300 Subject: [PATCH] Update getrawtransaction to work with block hash argument --- bitcoin/rpc.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bitcoin/rpc.py b/bitcoin/rpc.py index 42c2012c..5907740a 100644 --- a/bitcoin/rpc.py +++ b/bitcoin/rpc.py @@ -566,7 +566,7 @@ def getrawmempool(self, verbose=False): r = [lx(txid) for txid in r] return r - def getrawtransaction(self, txid, verbose=False): + def getrawtransaction(self, txid, verbose=False, block_hash=None): """Return transaction with hash txid Raises IndexError if transaction not found. @@ -574,16 +574,22 @@ def getrawtransaction(self, txid, verbose=False): verbose - If true a dict is returned instead with additional information on the transaction. + block_hash - Hash of the block containing the transaction + (required when transaction not currently indexed by Bitcoin Core) + Note that if all txouts are spent and the transaction index is not enabled the transaction may not be available. """ try: - r = self._call('getrawtransaction', b2lx(txid), 1 if verbose else 0) + if block_hash is None: + r = self._call('getrawtransaction', b2lx(txid), 1 if verbose else 0) + else: + r = self._call('getrawtransaction', b2lx(txid), 1 if verbose else 0, b2lx(block_hash)) except InvalidAddressOrKeyError as ex: raise IndexError('%s.getrawtransaction(): %s (%d)' % (self.__class__.__name__, ex.error['message'], ex.error['code'])) if verbose: - r['tx'] = CTransaction.deserialize(unhexlify_str(r['hex'])) + r['tx'] = CTransaction.deserialize(unhexlify(r['hex'])) del r['hex'] del r['txid'] del r['version'] @@ -592,8 +598,7 @@ def getrawtransaction(self, txid, verbose=False): del r['vout'] r['blockhash'] = lx(r['blockhash']) if 'blockhash' in r else None else: - r = CTransaction.deserialize(unhexlify_str(r)) - + r = CTransaction.deserialize(unhexlify(r)) return r def getreceivedbyaddress(self, addr, minconf=1):