mirror of
https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
synced 2025-06-07 16:06:26 +00:00
Update and rename 3_Playing_with_Bitcoin.md to 3_0_Playing_with_Bitcoin-CLI.md
This commit is contained in:
parent
c270aab1eb
commit
8a84f8db71
@ -1,259 +1,11 @@
|
|||||||
# 3 - Playing with Bitcoin
|
# 3.0: Playing with Bitcoin Using bitcoin-cli
|
||||||
|
|
||||||
> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning.
|
This document explains how to begin using Bitcoin from the command line, using bitcoin-cli.
|
||||||
|
|
||||||
This document explains how to begin using Bitcoin from the command line. It presumes that you have a VPS that you installed bitcoin on and that is running bitcoind. It also presumes that you are connected to testnet, allowing for access to bitcoins without using real funds. We explained how to do this either by hand in [2A - Setting up a Bitcoin-Core VPS by Hand](./2A_Setting_Up_a_Bitcoin-Core_VPS_by_Hand.md) or by using a Linode StackScript at Linode.com in [2B - Setting up a Bitcoin-Core VPS with StackScript](./2B_Setting_Up_a_Bitcoin-Core_VPS_with_StackScript.md).
|
This section presumes that you have a VPS with bitcoin installed, running bitcoind. It also presumes that you are connected to testnet, allowing for access to bitcoins without using real funds. You can either do this by hand, per [2.1: Setting up a Bitcoin-Core VPS by Hand](./2_1_Setting_Up_a_Bitcoin-Core_VPS_by_Hand.md), or do it with a Linode StackScript at Linode.com, per [2.2: Setting up a Bitcoin-Core VPS with StackScript](./2_2_Setting_Up_a_Bitcoin-Core_VPS_with_StackScript.md).
|
||||||
|
|
||||||
## Part Zero: Getting Started with Bitcoin
|
* [Part One: Verifying Your bitcoin-cli Setup](3_1_Verifying_Your_Bitcoin-CLI_Setup.md)
|
||||||
|
|
||||||
Before you start playing with Bitcoin, you should ensure that everything is setup correctly.
|
|
||||||
|
|
||||||
### Check Your Aliases
|
|
||||||
|
|
||||||
The Bitcoin setup docs suggest creating a set of aliases. In case you didn't run through those docs, you can create them for your main Bitcoin user with the following command:
|
|
||||||
|
|
||||||
```
|
|
||||||
cat >> ~/.bash_profile <<EOF
|
|
||||||
alias btcdir="cd ~/.bitcoin/" #linux default bitcoind path
|
|
||||||
# alias btcdir="cd ~/Library/Application\ Support/Bitcoin/" #mac default bitcoind path
|
|
||||||
alias bc="bitcoin-cli"
|
|
||||||
alias bd="bitcoind"
|
|
||||||
alias btcinfo='bitcoin-cli getinfo | egrep "\"version\"|balance|blocks|connections|errors"'
|
|
||||||
alias btcblock="echo \\\`bitcoin-cli getblockcount 2>&1\\\`/\\\`wget -O - http://blockexplorer.com/testnet/q/getblockcount 2> /dev/null | cut -d : -f2 | rev | cut -c 2- | rev\\\`"
|
|
||||||
EOF
|
|
||||||
```
|
|
||||||
|
|
||||||
> **WARNING:** The btcblock alias will not work correctly if you try to place it in your .bash_profile by hand, rather than using the "cat" command as suggested. If you want to enter it by hand, you need to adjust the number of backslashes (usually from three each to one each), so make sure you know what you're doing if you aren't entering the commands exactly as shown.
|
|
||||||
|
|
||||||
Note that these aliases includes shortcuts for running `bitcoin-cli`, for running `bitcoind`, and for going to the Bitcoin directory. These aliases are mainly meant to make your life easier. We suggest you create other aliases to ease your use of frequent commands (and arguments) and to minimize errors. Aliases of this sort can even more useful if you have a complex setup where you regularly run commands associated with Mainnet, with Testnet, _and_ with Regtest.
|
|
||||||
|
|
||||||
With that said, use of these aliases in _this_ document might accidentally obscure the core lessons being taught about Bitcoin, so the only aliases directly used here are `btcinfo` and `btcblock`, because they encapsulate much longer and more complex commands. Otherwise, we show the full commands; adjust for your own use as appropriate.
|
|
||||||
|
|
||||||
> **TESTNET vs MAINNET:** Remember that this tutorial generally assumes that you are using testnet. Notes like this will comment on how things might be different over on Mainnet. In this case, the `btcblock` alias needs to be slightly different. On testnet, you can look up the current block count with the complex command "wget -O - http://blockexplorer.com/testnet/q/getblockcount 2> /dev/null | cut -d : -f2 | rev | cut -c 2- | rev"; on mainnet, you use the much simpler "wget -O - http://blockchain.info/q/getblockcount 2>/dev/null".
|
|
||||||
|
|
||||||
### Run Bitcoind
|
|
||||||
|
|
||||||
You'll be accessing the Bitcoin network with the `bitcoin-cli` command. However, bitcoind _must_ be running to use bitcoin-cli, as the bitcoin-cli sends JSON-RPC commands to the bitcoind. If you used our standard setup, bitcoind should already be up and running. You can double check by looking at the process table.
|
|
||||||
```
|
|
||||||
$ ps auxww | grep bitcoind
|
|
||||||
user1 29360 11.5 39.6 2676812 1601416 ? SLsl Feb23 163:42 /usr/local/bin/bitcoind -daemon
|
|
||||||
```
|
|
||||||
If it's not running, you'll want to run "/usr/local/bin/bitcoind -daemon" by hand and also place it in your crontab, as explained in [2A - Setting up a Bitcoin-Core VPS by Hand](./2A_Setting_Up_a_Bitcoin-Core_VPS_by_Hand.md).
|
|
||||||
|
|
||||||
### Verify Your Blocks
|
|
||||||
|
|
||||||
You should have the whole blockchain (or the whole pruned blockchain) ready before you start playing. Just run the `btcblock` alias to see if it's all loaded. You'll see two numbers, which tell you how many blocks have loaded out of how many total.
|
|
||||||
|
|
||||||
If the two numbers aren't the same, as shown in this testnet example, you should wait:
|
|
||||||
```
|
|
||||||
$ btcblock
|
|
||||||
973212/1090099
|
|
||||||
```
|
|
||||||
Total time can take from several hours to a day, depending on your setup.
|
|
||||||
|
|
||||||
If the two numbers are the same, as shown in this testnet example, you're fully loaded:
|
|
||||||
```
|
|
||||||
$ btcblock
|
|
||||||
1090099/1090099
|
|
||||||
```
|
|
||||||
|
|
||||||
### Check Your Directory
|
|
||||||
|
|
||||||
If your bitcoind is running and you've downloaded all the blocks, you ready to go. You could move right on to Part 1. However, you may want to be aware of other Bitcoin resources. To start with, take a look at your ~/.bitcoin directory.
|
|
||||||
|
|
||||||
The main directory just contains your config file and the testnet directory:
|
|
||||||
```
|
|
||||||
$ ls ~/.bitcoin
|
|
||||||
bitcoin.conf testnet3
|
|
||||||
```
|
|
||||||
The testnet3 directory then contains all of the guts:
|
|
||||||
```
|
|
||||||
$ ls ~/.bitcoin/testnet3
|
|
||||||
banlist.dat blocks database debug.log wallet.dat
|
|
||||||
bitcoind.pid chainstate db.log peers.dat
|
|
||||||
```
|
|
||||||
You shouldn't mess with most of these files and directories — particularly not the blocks and chainstate directories, which contain all of the blockchain data. However, do take careful note of the db.log and debug.log file, which you should refer to if you ever have problems with your setup.
|
|
||||||
|
|
||||||
> **TESTNET vs MAINNET:** If you're using mainnet, then _everything_ will instead be placed in the main ~/.bitcoin directory. These various setups _do_ elegantly stack, so if you are using mainnet, testnet, and regtest, you'll find that ~/.bitcoin contains your config file and your mainnet data, ~/.bitcoin/testnet3 contains your testnet data, and ~/.bitcoin/regtest contains your regtest data.
|
|
||||||
|
|
||||||
### Get Help
|
|
||||||
|
|
||||||
Most of your work will be done with the "bitcoin-cli" command. If you ever want more information on its usage, just run the help argument. Without any other arguments, it shows you ever possible command:
|
|
||||||
```
|
|
||||||
$ bitcoin-cli help
|
|
||||||
== Blockchain ==
|
|
||||||
getbestblockhash
|
|
||||||
getblock "hash" ( verbose )
|
|
||||||
getblockchaininfo
|
|
||||||
getblockcount
|
|
||||||
getblockhash index
|
|
||||||
getblockheader "hash" ( verbose )
|
|
||||||
getchaintips
|
|
||||||
getdifficulty
|
|
||||||
getmempoolancestors txid (verbose)
|
|
||||||
getmempooldescendants txid (verbose)
|
|
||||||
getmempoolentry txid
|
|
||||||
getmempoolinfo
|
|
||||||
getrawmempool ( verbose )
|
|
||||||
gettxout "txid" n ( includemempool )
|
|
||||||
gettxoutproof ["txid",...] ( blockhash )
|
|
||||||
gettxoutsetinfo
|
|
||||||
verifychain ( checklevel numblocks )
|
|
||||||
verifytxoutproof "proof"
|
|
||||||
|
|
||||||
== Control ==
|
|
||||||
getinfo
|
|
||||||
help ( "command" )
|
|
||||||
stop
|
|
||||||
|
|
||||||
== Generating ==
|
|
||||||
generate numblocks ( maxtries )
|
|
||||||
generatetoaddress numblocks address (maxtries)
|
|
||||||
|
|
||||||
== Mining ==
|
|
||||||
getblocktemplate ( TemplateRequest )
|
|
||||||
getmininginfo
|
|
||||||
getnetworkhashps ( blocks height )
|
|
||||||
prioritisetransaction <txid> <priority delta> <fee delta>
|
|
||||||
submitblock "hexdata" ( "jsonparametersobject" )
|
|
||||||
|
|
||||||
== Network ==
|
|
||||||
addnode "node" "add|remove|onetry"
|
|
||||||
clearbanned
|
|
||||||
disconnectnode "node"
|
|
||||||
getaddednodeinfo dummy ( "node" )
|
|
||||||
getconnectioncount
|
|
||||||
getnettotals
|
|
||||||
getnetworkinfo
|
|
||||||
getpeerinfo
|
|
||||||
listbanned
|
|
||||||
ping
|
|
||||||
setban "ip(/netmask)" "add|remove" (bantime) (absolute)
|
|
||||||
|
|
||||||
== Rawtransactions ==
|
|
||||||
createrawtransaction [{"txid":"id","vout":n},...] {"address":amount,"data":"hex",...} ( locktime )
|
|
||||||
decoderawtransaction "hexstring"
|
|
||||||
decodescript "hex"
|
|
||||||
fundrawtransaction "hexstring" ( options )
|
|
||||||
getrawtransaction "txid" ( verbose )
|
|
||||||
sendrawtransaction "hexstring" ( allowhighfees )
|
|
||||||
signrawtransaction "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},...] ["privatekey1",...] sighashtype )
|
|
||||||
|
|
||||||
== Util ==
|
|
||||||
createmultisig nrequired ["key",...]
|
|
||||||
estimatefee nblocks
|
|
||||||
estimatepriority nblocks
|
|
||||||
estimatesmartfee nblocks
|
|
||||||
estimatesmartpriority nblocks
|
|
||||||
signmessagewithprivkey "privkey" "message"
|
|
||||||
validateaddress "bitcoinaddress"
|
|
||||||
verifymessage "bitcoinaddress" "signature" "message"
|
|
||||||
|
|
||||||
== Wallet ==
|
|
||||||
abandontransaction "txid"
|
|
||||||
addmultisigaddress nrequired ["key",...] ( "account" )
|
|
||||||
addwitnessaddress "address"
|
|
||||||
backupwallet "destination"
|
|
||||||
dumpprivkey "bitcoinaddress"
|
|
||||||
dumpwallet "filename"
|
|
||||||
encryptwallet "passphrase"
|
|
||||||
getaccount "bitcoinaddress"
|
|
||||||
getaccountaddress "account"
|
|
||||||
getaddressesbyaccount "account"
|
|
||||||
getbalance ( "account" minconf includeWatchonly )
|
|
||||||
getnewaddress ( "account" )
|
|
||||||
getrawchangeaddress
|
|
||||||
getreceivedbyaccount "account" ( minconf )
|
|
||||||
getreceivedbyaddress "bitcoinaddress" ( minconf )
|
|
||||||
gettransaction "txid" ( includeWatchonly )
|
|
||||||
getunconfirmedbalance
|
|
||||||
getwalletinfo
|
|
||||||
importaddress "address" ( "label" rescan p2sh )
|
|
||||||
importprivkey "bitcoinprivkey" ( "label" rescan )
|
|
||||||
importprunedfunds
|
|
||||||
importpubkey "pubkey" ( "label" rescan )
|
|
||||||
importwallet "filename"
|
|
||||||
keypoolrefill ( newsize )
|
|
||||||
listaccounts ( minconf includeWatchonly)
|
|
||||||
listaddressgroupings
|
|
||||||
listlockunspent
|
|
||||||
listreceivedbyaccount ( minconf includeempty includeWatchonly)
|
|
||||||
listreceivedbyaddress ( minconf includeempty includeWatchonly)
|
|
||||||
listsinceblock ( "blockhash" target-confirmations includeWatchonly)
|
|
||||||
listtransactions ( "account" count from includeWatchonly)
|
|
||||||
listunspent ( minconf maxconf ["address",...] )
|
|
||||||
lockunspent unlock ([{"txid":"txid","vout":n},...])
|
|
||||||
move "fromaccount" "toaccount" amount ( minconf "comment" )
|
|
||||||
removeprunedfunds "txid"
|
|
||||||
sendfrom "fromaccount" "tobitcoinaddress" amount ( minconf "comment" "comment-to" )
|
|
||||||
sendmany "fromaccount" {"address":amount,...} ( minconf "comment" ["address",...] )
|
|
||||||
sendtoaddress "bitcoinaddress" amount ( "comment" "comment-to" subtractfeefromamount )
|
|
||||||
setaccount "bitcoinaddress" "account"
|
|
||||||
settxfee amount
|
|
||||||
signmessage "bitcoinaddress" "message"
|
|
||||||
```
|
|
||||||
You can also type "bitcoin help [command]" to get even more extensive info on that command. For example:
|
|
||||||
```
|
|
||||||
$ bitcoin-cli help getmininginfo
|
|
||||||
getmininginfo
|
|
||||||
|
|
||||||
Returns a json object containing mining-related information.
|
|
||||||
Result:
|
|
||||||
{
|
|
||||||
"blocks": nnn, (numeric) The current block
|
|
||||||
"currentblocksize": nnn, (numeric) The last block size
|
|
||||||
"currentblockweight": nnn, (numeric) The last block weight
|
|
||||||
"currentblocktx": nnn, (numeric) The last block transaction
|
|
||||||
"difficulty": xxx.xxxxx (numeric) The current difficulty
|
|
||||||
"errors": "..." (string) Current errors
|
|
||||||
"networkhashps": nnn, (numeric) The network hashes per second
|
|
||||||
"pooledtx": n (numeric) The size of the mempool
|
|
||||||
"testnet": true|false (boolean) If using testnet or not
|
|
||||||
"chain": "xxxx", (string) current network name as defined in BIP70 (main, test, regtest)
|
|
||||||
}
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
> bitcoin-cli getmininginfo
|
|
||||||
> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getmininginfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
|
|
||||||
```
|
|
||||||
|
|
||||||
### Optional: Know Your Server Types
|
|
||||||
|
|
||||||
> **TESTNET vs MAINNET:** When you set up your node, you choose to create it as either a Mainnet, Testnet, or Regtest node. Though this document presumes a testnet setup, it's worth understanding how you might access and use the other setup types — even all on the same machine!
|
|
||||||
|
|
||||||
The type of setup is mainly controlled through the ~/.bitcoin/bitcoin.conf file. If you're running testnet, it probably contains this line:
|
|
||||||
```
|
|
||||||
testnet=1
|
|
||||||
```
|
|
||||||
While if you're running regtest, it probably contains this line:
|
|
||||||
```
|
|
||||||
regtest=1
|
|
||||||
```
|
|
||||||
However, if you want to run several different sorts of nodes simultaneously, you should leave the testnet (or regtest) flag out of your configuration file. You can then choose whether you're using the mainnet, the testnet, or your regtest every time you run bitcoind or bitcoin-cli.
|
|
||||||
|
|
||||||
Here's a set of aliases that would make that easier by creating a specific alias for starting and stopping the bitcoind, for going to the bitcoin directory, and for running bitcoin-cli, for each of the mainnet (which has no extra flags), the testnet (which is -testnet), or your regtest (which is -regtest).
|
|
||||||
```
|
|
||||||
alias bcstart="bitcoind -daemon"
|
|
||||||
alias btstart="bitcoind -testnet -daemon"
|
|
||||||
alias brstart="bitcoind -regtest -daemon"
|
|
||||||
|
|
||||||
alias bcstop="bitcoin-cli stop"
|
|
||||||
alias btstop="bitcoin-cli -testnet stop"
|
|
||||||
alias brstop="bitcoin-cli -regtest -stop"
|
|
||||||
|
|
||||||
alias bcdir="cd ~/.bitcoin/" #linux default bitcoin path
|
|
||||||
alias btdir="cd ~/.bitcoin/testnet" #linux default bitcoin testnet path
|
|
||||||
alias brdir="cd ~/.bitcoin/regtest" #linux default bitcoin regtest path
|
|
||||||
|
|
||||||
alias bc="bitcoin-cli"
|
|
||||||
alias bt="bitcoin-cli -testnet"
|
|
||||||
alias br="bitcoin-cli -regtest"
|
|
||||||
```
|
|
||||||
For even more complexity, you could have each of your 'start' aliases use the -conf flag to load configuration from a different file. This goes far beyond the scope of this tutorial, but we offer it as a starting point for when your explorations of Bitcoin reach the next level.
|
|
||||||
|
|
||||||
### Summary: Getting Started
|
|
||||||
|
|
||||||
Before you start playing with bitcoin, you should make sure that the bitcoind is running and that all the blocks have been downloaded. You might get additional help from the `bitcoin-cli help` command or from files in the ~/.bitcoin directory.
|
|
||||||
|
|
||||||
## Part One: Setting Up Your Wallet
|
## Part One: Setting Up Your Wallet
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user