COMP4142
E-Payment and Cryptocurrency
Lab 3: Bitcoin
Rui SONG
[email protected]
Objective
• Be able to install Docker on Windows, MacOS, or Linux operating systems.
• Be able to run Bitcoin Core and CLI images in Docker containers.
• Be able to get test coins from the public faucet.
• Be able to browse the status of the Bitcoin TestNet using public web services.
• Be able to query Bitcoin TestNet using the Bitcoin CLI tool.
• Be able to initiate transactions on Bitcoin TestNet using the Bitcoin CLI tool.
Outline
• Install Docker
• Run Bitcoin Core and CLI
• Query Bitcoin TestNet
• Issue Bitcoin Transactions
Install Docker
Docker Installation
Microsoft Windows
• Install Docker Desktop on Windows
• Click on this link
• Click on the button 'Docker Desktop for Windows'
Docker Installation
Microsoft Windows
• Install Docker Desktop on Windows
• Open the downloaded installer
• Tick the above checkboxs as appropriate
• Reboot your device when finishing the installation
• Click on the Docker Desktop icon to activate the Docker Engine
Docker Installation
Apple MacOS
• Install Docker Desktop on Windows
• Click on this link
• Click on one of the two buttons according to your Mac
Docker Installation
Apple MacOS
• Install Docker Desktop on Windows
• Double-click Docer.img to open the installer, drag the Docker icon to the Applications folder
• Click on the Docker.app icon on the launchpad to start Docker, and enter your password to install it
Docker Installation
Canonical Ubuntu
• Install Docker using Apt repository
• Add Docker’s GPG key
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
• Add the repository to apt sources
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
* For installing Docker on other Linux distributions, see this link.
Docker Installation
Canonical Ubuntu
• Install Docker using Apt repository
• Install the Docker packages
sudo apt-get install \
docker-ce docker-ce-cli \
containerd.io docker-buildx-plugin \
docker-compose-plugin
• Verify that the Docker Engine has been installed
sudo docker run hello-world
Run Bitcoin Core and CLI
Introduction
Bitcoin
• Bitcoin Core
• Open-source software that serves as a Bitcoin node …
• And Bitcoin wallet.
• First published by Satoshi Nakamoto along with the Bitcoin Blockchain.
• Two implementations:
• Bitcoin Core GUI (Graphic User Interface)
• Bitcoin Core CLI (Command Line Interface)
Bitcoin Core
Pull the Image
• Pull the image from Docker Hub
docker pull crumblejon/bitcoin-core
* For Linux distributions (e.g., Ubuntu), the command should be: sudo docker pull crumblejon/bitcoin-core
Bitcoin Core
Run the Image
• Run the image in Docker
• Execute the image to generate a Docker container
docker run -d --name bitcoin-core \
-it crumblejon/bitcoin-core \
-testnet \
-prune=1000
• The -testnet argument indicates that we are in the test network, as opposed to the main network.
* If you use Powershell or CMD on Microsoft Windows, please use backquote to break lines.
Bitcoin Core
Run the Image
• Prunning to shrink disk storage
• Bitcoin Core will download ALL blocks to your device, from the very beginning till now.
• There is an option that deletes verified blocks from your local copy, called pruning.
• You can enable pruning at initialization by adding an argument:
-prune=1000
• The number after -prune= indicates the disk space to be used in Megabytes (MB) and should be
higher than 550. Typically, a setting of 1000 to 4000 is reasonable.
Bitcoin Core
Run the Image
• Check blockchain status
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet getblockchaininfo
• Contents
• Chain, block number, block header number, current difficulty, etc.
Query Bitcoin TestNet
Preparations
Generate Wallet and Addresses
• Create a Bitcoin wallet
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet createwallet "<your_wallet_name>"
• Create a new address
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=<your_wallet_name> getnewaddress
* You should replace the <your_wallet_name> with your own wallet name created above.
Preparations
Get TestNet Bitcoins
• Get your own TestNet coins for free from faucet
• https://bitcoinfaucet.uo1.net/
Input your address here
• Click on the button "Send testnet bitcoins"
• You can find the corresponding transaction in the following list:
Preparations
Check the Balance
• Check your balance on Block Cypher
• https://live.blockcypher.com/btc-testnet
Input your address here
• View the transaction details
Query TestNet
Check the Balance
• Check your balance1,2 on your local Bitcoin node
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=<your_wallet_name> getwalletinfo
1. If your local node has not finished synchronizing, your balance will be 0.
2. A transaction will be finalized after 6 confirmations by subsequent blocks, So you may find your balance unconfirmed.
Query TestNet
Query UTxOs
• List your Unspent Transaction Outputs, UTxOs
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=<your_wallet_name> listunspent
Issue Bitcoin Transactions
Preparations
Query Transactions
• Query transactions using your local node
• You should provide the corresponding transaction hash and block hash
• You can find one from Block Cypher
Transaction Hash
Block Hash
Preparations
Query Transactions
• Query raw transactions
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet getrawtransaction \
<tx_hash> false <block_hash>
The transaction raw data in hexadecimal format
Preparations
Query Transactions
• Query decoded transactions
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet getrawtransaction \
<tx_hash> true <block_hash>
decoded transaction in JSON format
Issue Transactions
By Transferring
• Initial a transaction by sending coins to others
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=COMP4142 \
sendtoaddress <address> <amount>
Transaction ID
• View the transaction on Block Cypher
your address
receiver’s address
Issue Transactions
By Raw Transactions
• Create a raw transaction
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet createrawtransaction \
<tx_inputs> <tx_outputs>
• Should provide transaction inputs and outputs
Issue Transactions
By Raw Transactions
• Data Structures
• Transaction inputs:
[
{
"txid": "04315324..", // ID of a previous transaction of yours
"vout": 0, // The index of the unspent output in its transaction
},
...
]
• Transaction outputs:
[
{
"tb1qwc..": 0.0004 // The key is the bitcoin address, and the value is the amount in BTC
},
...
]
Issue Transactions
By Raw Transactions
• Construct transaction inputs
• By retrieving your unspent transaction outputs (UTxOs):
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=<your_wallet_name> listunspent
Issue Transactions
By Raw Transactions
• Create a raw transaction
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet createrawtransaction \
<tx_inputs> <tx_outputs>
The unsigned raw transaction
1. The quotes appearing within strings in the command should be escaped with backslashes.
2. If you use Powershell or CMD on Microsoft Windows, you should use single quotes for outer quotes of strings, e.g.,
docker exec --user bitcoin bitcoin-core `
bitcoin-cli -testnet createrawtransaction `
'[{\"txid\": \"28248cb986162773d58c961fff98201098bd0fdcdde9dfa2295706f88a121ce3\", \"vout\": 0}]' `
'[{\"tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6\": 0.00001}]'
Issue Transactions
By Raw Transactions
• Sign the raw transaction
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet -rpcwallet=<your_wallet_name> signrawtransactionwithwallet \
<unsigned_raw_transaction>
The signed raw transaction
Issue Transactions
By Raw Transactions
• Publish the signed raw transaction
docker exec --user bitcoin bitcoin-core \
bitcoin-cli -testnet sendrawtransaction \
<signed_raw_transaction>
• Broadcast the new transaction to peer nodes
• Will be packed into a block by a miner afterward
The ID of the new transaction
Issue Transactions
By Raw Transactions
• View the new transaction on Block Cypher
• The difference between transaction inputs and outputs is automatically counted as the transaction
fee.
Thank you!