Libbitcoin-Explorer: Bitcoin Transaction via Command Line

Libbitcoin-Explorer: Bitcoin Transaction via Command Line

Libbitcoin Explorer provides a useful toolkit of command-line tools for performing bitcoin functions.

Libbitcoin Explorer exposes commands for specific bitcoin hashing functions as well as encoding. It also provides for a rich set of commands for building, signing and broadcasting transactions to the bitcoin network. In addition, it allows the user to make blockchain calls from the command line to query information such as block height, address balance and unspent transaction output.

This makes constructing a transaction from the command line a valuable learning experience as well as a hardcore bitcoin spartan pastime.

To start with, we are going to need to make sure we have libbitcoin-explorer installed, which luckily can be done by building the source files or install a simple executable.

After that we’re going to need to make sure we are utilizing the testnet, unless you want to practice spending real bitcoins in which case ignore the following.

If you have bx installed you can use the settings command to check what current settings are being used. With the default configuration this should return standard mainnet settings.

alpha$ bx settings

We can check the current height of the blockchain with the command:

alpha$ bx fetch-height

Now the number returned should start with a 4, or 5, depending on when you read this post. However, that is the height of the actual, legitamate multi-billion dollar blockchain. We would like to inspect and work with, explore if you will, the testnet blockchain.

To do this we can pass an argument with the specific server we want to query. The following example fetches the height from the address of a testnet server we pass as an argument.

alpha$ bx fetch-height tcp://

This should return a much larger block-height probably starting with a 1. If we want to use this configuration consistently we are going to need a config file. The default one provided by the developers works fairly well so I  simply copy and pasted everything from this config file into a new file call “bx-testnet.cfg” and saved that file on my desktop.

Then, I opened it up in sublime and scrolled down to the server section, where I uncommented the testnet server address and put a hashtag(comment-symbol #) in front of the previously used mainnet server. After I edited it, the server portion looked like this:

# The URL of the default hidden testnet libbitcoin query service.
#url = tcp://rmrai2ifbed2bf55.onion:19091
# The URL of the default testnet libbitcoin query service.
url = tcp://
# The URL of the default hidden mainnet libbitcoin query service.
#url = tcp://sqax52n5enkw4dsj.onion:9091
# The URL of the default mainnet libbitcoin query service.
#url = tcp://

Once I have these settings set and the file saved on my desktop I can begin using the -c command line argument to pass the configuration file to calls that I want to use on the testnet. By dragging the file from the desktop to the terminal window, I can get the exact path of the file.

alpha$ bx fetch-balance n2ge1S4bLDvJKx8AGXrK5JHY2D5cReVytu -c /Users/alpha/Desktop/bx-testnet.cfg

The previous command should return a JSON formatted response with testnet balances for this address.

Now, in order to create my transaction I’m going to need to collect a UTXO from my wallet to spend. Luckily BX has a command for this purpose. All I need to do is pass the address I want to spend from along with the amount I want to spend, as well as my config file.

I’ll look for a 1 bitcoin utxo, which inputted as satoshi’s(a one followed by eight zeros).

alpha$ bx fetch-utxo 100000000 n2ge1S4bLDvJKx8AGXrK5JHY2D5cReVytu --config /Users/alpha/Desktop/bx-testnet.cfg

Again, a nicely formatted JSON response should be displayed:

     hash ce7f741625dfa86a50a1f18e3664e927441e27ef2f1c526e3aff8ea6c7a650fd
     index 0
     value 148292700

Now I can encode the first part of my transaction, the inputs and outputs. My address came up with a UTXO that is slightly bigger than the 1 btc I want to send so I’ll have to make two outputs for this transaction. BX has a tx-encode command that will allow use to pass inputs and outputs for encoding in the format: BX -i UTXOhash:index -o destinationAddress:satoshiAmount

alpha$ bx tx-encode -i ce7f741625dfa86a50a1f18e3664e927441e27ef2f1c526e3aff8ea6c7a650fd:0 -o mnrnjVFimDFrNkszzMtecr4yrMKmEuMRbv:100000000 -o n2ge1S4bLDvJKx8AGXrK5JHY2D5cReVytu:386772661 

Note the two outputs, one with the change output minus 2000 satoshis for the implicit fee and the other with the 1 BTC payment. My output looked like this:


In order for this transaction to be valid we will need to sign it. It may be helpful here to open a new terminal window to perform some helper commands. BX’s input-sign command is going to need to take a private key, the previous output script and the encoded transaction from the last step.

Now, we can use a mnemonic seed from our wallet to get the public/private key pair that corresponds to this address. We can now pass all these arguments to our command.

alpha$ bx input-sign 2362a8YOUR-PRIVATE-KEY-HERE86f654bab5 "dup hash160 [02e6705c1dfbaadb92254bc5958820feedd98de6859a321fdb4ea335db0a2b2c97] equalverify checksig" 0100000001fd50a6c7a68eff3a6e521c2fef271e4427e964368ef1a1506aa8df2516747fce0000000000ffffffff0200e1f505000000001976a914508937dbc842526b26a66b6a3a7f80172713135f88acb5ae0d17000000001976a914e82f89457f9efaab09b3222b5f7f82b4ab82683288ac00000000

The output should be an endorsement for the transaction.


Now, using the input-set we can assign this endorsement along with the public key and the previously serialized transaction to create the transactions final form.

alpha$ bx input-set "[3044022055967cd2c32aeb12d09be138f218f6837968fe56aa2a811b7285d7d4eec82497022048cd87ff7a2b50cda55c2871a1fbf722bbc0a5855b91d445a5c912cd900bf66701] [02e6705c1dfbaadb92254bc5958820feedd98de6859a321fdb4ea335db0a2b2c97]" 0100000001fd50a6c7a68eff3a6e521c2fef271e4427e964368ef1a1506aa8df2516747fce0000000000ffffffff0200e1f505000000001976a914508937dbc842526b26a66b6a3a7f80172713135f88acb5ae0d17000000001976a914e82f89457f9efaab09b3222b5f7f82b4ab82683288ac00000000

The resulting transaction should be our fully signed and serialized transaction


the transaction can now be inspected and validated using various libbitcoin-explorer commands.

alpha$ bx tx-decode 0100000001fd50a6c7a68eff3a6e521c2fef271e4427e964368ef1a1506aa8df2516747fce000000006a473044022055967cd2c32aeb12d09be138f218f6837968fe56aa2a811b7285d7d4eec82497022048cd87ff7a2b50cda55c2871a1fbf722bbc0a5855b91d445a5c912cd900bf667012102e6705c1dfbaadb92254bc5958820feedd98de6859a321fdb4ea335db0a2b2c97ffffffff0200e1f505000000001976a914508937dbc842526b26a66b6a3a7f80172713135f88acb5ae0d17000000001976a914e82f89457f9efaab09b3222b5f7f82b4ab82683288ac00000000 -c /Users/alpha/Desktop/bx-testnet.cfg

Or push the Transaction to the network:

alpha$ bx send-tx 0100000001fd50a6c7a68eff3a6e521c2fef271e4427e964368ef1a1506aa8df2516747fce000000006a473044022055967cd2c32aeb12d09be138f218f6837968fe56aa2a811b7285d7d4eec82497022048cd87ff7a2b50cda55c2871a1fbf722bbc0a5855b91d445a5c912cd900bf667012102e6705c1dfbaadb92254bc5958820feedd98de6859a321fdb4ea335db0a2b2c97ffffffff0200e1f505000000001976a914508937dbc842526b26a66b6a3a7f80172713135f88acb5ae0d17000000001976a914e82f89457f9efaab09b3222b5f7f82b4ab82683288ac00000000 -c /Users/alpha/Desktop/bx-testnet.cfg

Leave a Reply

Your email address will not be published.