From 8bd34d158eb7b2ebde4848b40ab5a741f5001d86 Mon Sep 17 00:00:00 2001 From: Daniel Edgecumbe Date: Sat, 30 Sep 2017 21:41:01 +0100 Subject: [PATCH] [transaction] Add TX_VERBOSE_MODE to show addresses in inputs --- macros.py | 4 +++- transaction.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/macros.py b/macros.py index 9e0f1fd..782470a 100644 --- a/macros.py +++ b/macros.py @@ -9,6 +9,8 @@ VERSION_STRING = "bitcoind-ncurses v0.2.0-dev" # "tx", "console", "net", "forks", # ] MODES = ["monitor", "peers", "block", "transaction"] -DEFAULT_MODE = "monitor" + +# TX_VERBOSE_MODE controls whether the prevouts for an input are fetched. +TX_VERBOSE_MODE = False MIN_WINDOW_SIZE = (10, 20) diff --git a/transaction.py b/transaction.py index 3fd1262..e08af4f 100644 --- a/transaction.py +++ b/transaction.py @@ -6,7 +6,7 @@ import datetime import curses import asyncio -from macros import MIN_WINDOW_SIZE +from macros import MIN_WINDOW_SIZE, TX_VERBOSE_MODE class TransactionStore(object): @@ -84,7 +84,7 @@ class TransactionView(object): # height and weight would be nice. # neither are directly accessible. - async def _draw_inputs(self, transaction): + async def _draw_inputs(self, transaction, inouts): CGREEN = curses.color_pair(1) CRED = curses.color_pair(3) CYELLOW = curses.color_pair(5) @@ -116,6 +116,24 @@ class TransactionView(object): # Sequence numbers, perhaps? if "coinbase" in inp: inputstr = inp["coinbase"][:76] + elif inouts is not None: # TX_VERBOSE_MODE + # Find the vout + inout = inouts[i] + inputstr = "{}".format(str(inout)[:40]) + spk = inout["scriptPubKey"] + if "addresses" in spk: + if len(spk["addresses"]) > 1: + inoutstring = "<{} addresses>".format(len(spk["addresses"])) + elif len(spk["addresses"]) == 1: + inoutstring = spk["addresses"][0].rjust(34) + else: + raise Exception("addresses present in scriptPubKey, but 0 addresses") + elif "asm" in spk: + inoutstring = spk["asm"][:34] + else: + inoutstring = "???" + + inputstr = "{:05d} {} {: 15.8f} BTC".format(i, inoutstring, inout["value"]) else: inputstr = "{:05d} {}:{:05d}".format(i, inp["txid"], inp["vout"]) @@ -160,7 +178,12 @@ class TransactionView(object): # A 1 million BTC output would be rather surprising. Pad to six. spk = out["scriptPubKey"] if "addresses" in spk: - outstring = " ".join(spk["addresses"])[:80] + if len(spk["addresses"]) > 1: + outstring = "<{} addresses>".format(len(spk["addresses"])) + elif len(spk["addresses"]) == 1: + outstring = spk["addresses"][0].rjust(34) + else: + raise Exception("addresses present in scriptPubKey, but 0 addresses") elif "asm" in spk: outstring = spk["asm"][:80] else: @@ -173,7 +196,7 @@ class TransactionView(object): self._pad.addstr(14+i-offset, 1, "{:05d} {} {: 15.8f} BTC".format(i, outstring, out["value"]), outputcolor) - async def _draw(self, transaction): + async def _draw(self, transaction, inouts): # TODO: figure out window width etc. if self._pad is not None: @@ -184,7 +207,7 @@ class TransactionView(object): if transaction: await self._draw_transaction(transaction) if "vin" in transaction: - await self._draw_inputs(transaction) + await self._draw_inputs(transaction, inouts) if "vout" in transaction: await self._draw_outputs(transaction) @@ -316,10 +339,20 @@ class TransactionView(object): return transaction = None + inouts = None if self._txid: transaction = await self._transactionstore.get_transaction(self._txid) - - await self._draw(transaction) + if TX_VERBOSE_MODE: + inouts = [] + for vin in transaction["vin"]: + if not "txid" in vin: + # It's a coinbase + inouts = None + break + prevtx = await self._transactionstore.get_transaction(vin["txid"]) + inouts.append(prevtx["vout"][vin["vout"]]) + + await self._draw(transaction, inouts) async def on_mode_change(self, newmode): if newmode != "transaction":