Skip to content

Instantly share code, notes, and snippets.

@suhailvs
Last active September 21, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suhailvs/281b8471f6136b58946dc49c92230e4d to your computer and use it in GitHub Desktop.
Save suhailvs/281b8471f6136b58946dc49c92230e4d to your computer and use it in GitHub Desktop.
Bitcoin Work

Installation

$ sudo apt-add-repository ppa:bitcoin/bitcoin
$ sudo apt-get update  

To install the Bitcoin Core Graphical User Interface (GUI):

$ sudo apt-get install bitcoin-qt

To install the Bitcoin Core daemon (bitcoind):

$ sudo apt-get install bitcoind

Note that you can’t run both the GUI and the daemon at the same time using the same configuration directory.

All three programs get settings from bitcoin.conf in the Bitcoin application directory:

$HOME/.bitcoin/

To use bitcoind and bitcoin-cli, you will need to add a RPC password to your bitcoin.conf file.

rpcpassword=change_this_to_a_long_random_password

Starting Bitcoin Core Daemon

$ bitcoind -daemon

To interact with Bitcoin Core daemon, you will use the command bitcoin-cli (Bitcoin command line interface)

To safely stop your node, run the following command:

$ bitcoin-cli stop

Testing Applications

Testnet

When run with no arguments, all Bitcoin Core programs default to Bitcoin’s main network (mainnet). However, for development, it’s safer and cheaper to use Bitcoin’s test network (testnet) where the satoshis spent have no real-world value.

To use testnet, use the argument -testnet with bitcoin-cli, bitcoind or bitcoin-qt or add testnet=1 to your bitcoin.conf file.

Regtest Mode

For situations where interaction with random peers and blocks is unnecessary or unwanted, Bitcoin Core’s regression test mode (regtest mode) lets you instantly create a brand-new private block chain with the same basic rules as testnet, but one major difference: you choose when to create new blocks, so you have complete control over the environment.

The following example will let you create a regtest environment after you first configure bitcoind.

$ bitcoind -regtest -daemon
Bitcoin server starting

Start bitcoind in regtest mode to create a private block chain.

$ bitcoin-cli -regtest generate 101

A block must have 100 confirmations before that reward can be spent, so we generate 101 blocks(There is 5000BTC unconfirmed).

$ bitcoin-cli -regtest getbalance
50.00000000

Verify that we now have 50 bitcoins available to spend. You can now use Bitcoin Core RPCs prefixed with bitcoin-cli -regtest.

You can stop bitcoind and view the balance in bitcoin-qt:

$ bitcoin-cli -regtest stop
Bitcoin server stopping
$ bitcoin-qt -regtest

Regtest wallets and block chain state (chainstate) are saved in the regtest subdirectory of the Bitcoin Core configuration directory($HOME/.bitcoin/). You can safely delete the regtest subdirectory and restart Bitcoin Core to start a new regtest.

Transactions

Simple Spending

Bitcoin Core provides several RPCs which handle all the details of spending, including creating change outputs and paying appropriate fees. Even advanced users should use these RPCs whenever possible to decrease the chance that satoshis will be lost by mistake.

$ bitcoind -regtest -daemon
Bitcoin server starting
$ bitcoin-cli -regtest getnewaddress
mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou
$ NEW_ADDRESS=mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou

Get a new Bitcoin address and save it in the shell variable $NEW_ADDRESS.

$ bitcoin-cli -regtest sendtoaddress $NEW_ADDRESS 10.00
263c018582731ff54dc72c7d67e858c002ae298835501d80200f05753de0edf0

Send 10 bitcoins to the address using the sendtoaddress RPC. The returned hex string is the transaction identifier (txid).

The sendtoaddress RPC automatically selects an unspent transaction output (UTXO) from which to spend the satoshis. In this case, it withdrew the satoshis from our only available UTXO, the coinbase transaction for block #1 which matured with the creation of block #101. To spend a specific UTXO, you could use the sendfrom RPC instead.

$ bitcoin-cli -regtest listunspent
[
]

Use the listunspent RPC to display the UTXOs belonging to this wallet. The list is empty because it defaults to only showing confirmed UTXOs and we just spent our only confirmed UTXO.

$ bitcoin-cli -regtest listunspent 0
[
    {
        "txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d\
                  80200f05753de0edf0",
        "vout" : 0,
        "address" : "muhtvdmsnbQEPFuEmxcChX58fGvXaaUoVt",
        "scriptPubKey" : "76a9149ba386253ea698158b6d34802bb9b550\
                          f5ce36dd88ac",
        "amount" : 40.00000000,
        "confirmations" : 0,
        "spendable" : true,
        "solvable" : true
    },
    {
        "txid" : "263c018582731ff54dc72c7d67e858c002ae298835501d\
                  80200f05753de0edf0",
        "vout" : 1,
        "address" : "mvbnrCX3bg1cDRUu8pkecrvP6vQkSLDSou",
        "account" : "",
        "scriptPubKey" : "76a914a57414e5ffae9ef5074bacbe10a320bb\
                          2614e1f388ac",
        "amount" : 10.00000000,
        "confirmations" : 0,
        "spendable" : true,
        "solvable" : true
    }
]

Re-running the listunspent RPC with the argument 0 to also display unconfirmed transactions shows that we have two UTXOs, both with the same txid. The first UTXO shown is a change output that sendtoaddress created using a new address from the key pool. The second UTXO shown is the spend to the address we provided. If we had spent those satoshis to someone else, that second transaction would not be displayed in our list of UTXOs.

$ bitcoin-cli -regtest generate 1
$ unset NEW_ADDRESS

Create a new block to confirm the transaction above (takes less than a second) and clear the shell variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment