Post
Topic
Board Project Development
Merits 2 from 1 user
Topic OP
[BASH] Script to get and process transactions by hash
by
naufragus
on 01/10/2020, 01:46:52 UTC
⭐ Merited by NotATether (2)
Hello guys!
It has been some months my working on some bash scripts
to get information of a transaction by its hash through bitcoin-cli
and parse the json data for that transaction in a more readable way.
(And also to learn some about bitcoin hashing).

As i wrote some wrappers for blockchain.com APIs, I wanted to get
more or less the same information for when quering for a transaction
or a block's worth of transaction.. I used functions from grondilu's
bitcoin-bash-tools..

Before trying the scripts, one needs have bitcoin-cli, openssl and jq
packages installed. Also, that is a very good idea to have bitcoin-cli
set with txindex=1 (otherwise you would need supply the block hash
for the transactions).

The scripts are at https://github.com/mountaineerbr/scripts/graphs/traffic

There are two scripts, one for getting block information and transaction
hashes in that block, and another to get transaction information by
transaction hash. For example:

Get info of a transaction by its hash id:
Code:
$ bitcoin.tx.sh a8bb9571a0667d63eaaaa36f9de87675f0d430e13c916248ded1d13093a77561

The output:

Code:
--------
Transaction information
TxId____: a8bb9571a0667d63eaaaa36f9de87675f0d430e13c916248ded1d13093a77561
Hash____: eac5391d9f7b411c7d6cbadb59a38bfa13399c3ecb440573f5f52b8a34001be1
BlkHash_: 0000000000000000000fb6a4d6f5dc7438f91a1bc3988c4f32b4bb8284eed0ec
Time____: 1594156611 2020-07-07T21:16:51Z
BlkTime_: 1594156611 2020-07-07T21:16:51Z
LockTime: 0
Version_: 1
Vsize___: 224
Size____: 414
Weight__: 894
Confirma: 12546

Vins
  TxIndex: 44f672f301772cf3e3fc15d424818aae8ed43468deb0cc56550dd9374578b816
  Sequenc: 4294967295
  VoutNum: 1
  Addresses
  Number_: 1 Value__: 4.68295
    type: witness_v0_scripthash
    bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej


Vouts
  Number_: 0 Value__: 0.26139693
  Addresses
    type: scripthash
    3KLwdgGf5QLHEW8qjxyv7wFpPMdVCDqu55

  Number_: 1 Value__: 0.19883951
  Addresses
    type: pubkeyhash
    1Lv9hU7h86REpDakYy9kpeTfT929b9twM5

  Number_: 2 Value__: 4.22231356
  Addresses
    type: witness_v0_scripthash
    bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej

One can parse all transactions from a block using my two scripts, such as:
Code:
$ bitcoin.blk.sh -ii 400000 | bitcoin.tx.sh

The problem is this script is slow to parse all transactions from a block, such
as in the previous example, because each transaction may require more
than a single RPC call to get extra information from previous UTXOs..

It took about 10 minutes to completely parsing all 1660 transactions from
block 400000 with my i7 (blockchain data  is in an external USD HDD)..
However, for few dozen transactions I reckon using that is quite feasible,
or maybe I just think that is an interesting script..

The scripts and more examples are at https://github.com/mountaineerbr/scripts/graphs/traffic

Cheers,The scripts are at https://github.com/mountaineerbr/scripts/graphs/traffic