Merge pull request #140 from jodobear/master

minor grammar edits
This commit is contained in:
Shannon Appelcline 2020-07-07 07:51:29 -10:00 committed by GitHub
commit bc34a1ced5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,7 +47,7 @@ We are going to install the `python-bitcoinrpc` library, like so:
## Build Your Connection
We are now ready to start interacting with `boitcond`, but first we need to establish connection. Create a file called `btcrpc.py` and type the following:
We are now ready to start interacting with `bitcoind`, but first we need to establish a connection. Create a file called `btcrpc.py` and type the following:
```py
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
@ -63,11 +63,11 @@ rpc_host = "127.0.0.1"
rpc_client = AuthServiceProxy(f"http://{rpc_user}:{rpc_pass}@{rpc_host}:18332", timeout=120)
```
`pprint` is nifty library to pretty print the `json` response from `bitcoind`.
`pprint` is a nifty library to pretty print the `json` response from `bitcoind`.
`logging` will print out the call we make to `bitcoind` and `bitcoind`'s respose, which is useful when we make a bunch of calls together. If you don't want the excessive output in the terminal just comment out the `logging` blok.
`logging` will print out the call we make to `bitcoind` and `bitcoind`'s respose, which is useful when we make a bunch of calls together. If you don't want the excessive output in the terminal just comment out the `logging` block.
The arguments in the URL are `<rpc_username>:<rpc_password>@<host_IP_address>:<port>`. We covered the information in our work with `curl` . The IP address `127.0.0.1` and port `18332` should be correct for the standard testnet setup described in these documents, while you can extract the user and password from `~/.bitcoin/bitcoin.conf`. We specify the `timeout` argument since socket timeouts under heavy load on the mainnet. If you get `socket.timeout: timed out` response, be patient and increase the `timeout`.
The arguments in the URL are `<rpc_username>:<rpc_password>@<host_IP_address>:<port>`. We covered the relevant information in our work with `curl` . The IP address `127.0.0.1` and port `18332` should be correct for the standard testnet setup described in these documents, while you can extract the user and password from `~/.bitcoin/bitcoin.conf`. We specify the `timeout` argument since socket timeouts under heavy load on the mainnet. If you get `socket.timeout: timed out` response, be patient and increase the `timeout`.
> MAINNET VS TESTNET: The port would be 8332 for a mainnet setup.
@ -209,11 +209,11 @@ pprint(address_tx_list)
## Exploring a UTXO
To get details of a utxo, we first need to get it's `hex` id, which we can then decode using `decoderawtransaction`.
To get details of a UTXO, we first need to get its `hex` id, which we can then decode using `decoderawtransaction`.
```py
print("Exploring UTXOs")
## List Utxos
## List UTXOs
utxos = rpc_client.listunspent()
print("Utxos: ")
print("-----")
@ -235,7 +235,7 @@ print("---------------------------------------------------------------\n")
## Sending Transactions
Now that we have established comfort between our python and bitcoind interface, lets create and broadcast a transaction. For the purposes of this course we will send test bitcoins to an address generated by us in our testnet wallet. Here's how we will do it:
Now that we have established comfort between our python and bitcoind interface, lets create and broadcast a transaction. For the purposes of this course, we will send test bitcoins to an address generated by us in our testnet wallet. Here's how we will do it:
1. Create 2 addresses, one that will act as recipient and the other for change.
2. Select a UTXO and set transaction details.
@ -245,7 +245,7 @@ Now that we have established comfort between our python and bitcoind interface,
### 1. Create New Addresses
For the purposes of learning, we will send bitcoins to an address generated by our node. We also generate a change address to receive back change from the difference between input and recipient amounts less the miner fees. For this we use `getrawchangeaddress`. It's largely the same as `getnewaddress` but is optimized for use as a change address in a raw transaction, so it doesn't do things like make entries in your address book.
For the purposes of learning, we will send bitcoins to an address generated by our node. We also generate a change address to receive back change from the difference between input and recipient amounts (less the miner fees). For this we use `getrawchangeaddress`. It's largely the same as `getnewaddress` but is optimized for use as a change address in a raw transaction, so it doesn't do things like make entries in your address book.
```py
print("Creating a Transaction")
@ -256,7 +256,7 @@ new_change_address = rpc_client.getrawchangeaddress()
### 2. Select UTXO & Set Transaction Details
In the folowing code snippet we first select the UTXO which we want to spend. Then we get it's address, transaction id, vector index of the output. Then we add the recipent address to which we want to send the bitcoins, enter the amount of bitcoins we want to send, set miner fee and calculate the change amount.
In the folowing code snippet we first select the UTXO which we want to spend. Then we get its address, transaction id, and the vector index of the output. Finally, we add the recipient address to which we want to send the bitcoins, enter the amount of bitcoins we want to send, set the miner fee, and calculate the change amount.
```py
selected_utxo = utxos[0] # we select the first utxo here
@ -305,7 +305,7 @@ Change Amount......: 0.004998
### 3. Create Raw Transacion
Now we have all the information to send a transaction, but before we can send one, we have to create a transaction, we do that by creating a raw transaction, like so:
Now we have all the information to send a transaction, but before we can send one, we have to create a transaction. We do that by creating a raw transaction, like so:
```py
txids_vouts = [{"txid": utxo_txid, "vout": utxo_vout}]
@ -335,7 +335,7 @@ Try checking the details of the transaction, hint: use `decoderawtransaction`.
### 4. Sign Raw Transaction
Now that we have a raw transaction, we use it's hex and the private key of the utxos we are spending to sign the transaction like so:
Now that we have a raw transaction, we use its hex and the private key of the UTXOs we are spending, in order to sign the transaction like so:
```py
address_priv_key = [] # list of priv keys of each utxo
@ -347,7 +347,7 @@ print("----------------------")
pprint(signed_tx)
print("---------------------------------------------------------------\n")
```
This returns a json object with the signed transaction's hex and whether it was signed completely or not:
This returns a JSON object with the signed transaction's hex, and whether it was signed completely or not:
```sh
---------------------------------------------------------------
@ -369,7 +369,7 @@ print("TXID of sent transaction: ", send_tx)
print("---------------------------------------------------------------\n")
```
Once the transaction has been broadcasted, bitcoind will return it's txid:
Once the transaction has been broadcasted, bitcoind will return its TXID:
```sh
---------------------------------------------------------------
@ -379,6 +379,8 @@ TXID of sent transaction: 'k9db702lsd512e2421915dc53clkj28f39a987c9a91cc0514faac
## Summary
In this chapter we learnt how to connect to a node, get some basic information about our node and even sent a transaction over testnet. Accessing Bitcoind with Python is very easy while using the `python-bitcoinrpc` library. The first thing to always do is to establish connection with your bitcoind instance then, you can basically call all the bitcoin API calls as described in the bitcoin-core documentation. This makes it really easy to create small or large scripts to manage your own node, check balances or create cool applications on top as you get the full power of `bitcoin-cli`.
In this chapter we learned how to connect to a node, get some basic information about our node, and even sent a transaction over testnet.
Accessing Bitcoind with Python is very easy while using the `python-bitcoinrpc` library. The first thing to always do is to establish connection with your bitcoind instance, then you can basically call all the bitcoin API calls as described in the bitcoin-core documentation. This makes it really easy to create small or large scripts to manage your own node, check balances, or create cool applications on top as you get the full power of `bitcoin-cli`.
All the source code for this chapter is available in the [src](./src/18_4_accessing_bitcoind_with_python.py) directory of the repo.