MrChrisJ / fullnode

How to setup a Bitcoin Fullnode on a Raspberry Pi 2
Creative Commons Zero v1.0 Universal
92 stars 50 forks source link

#Fullnode - Scripting Bitcoin with Python #14

Open ghost opened 8 years ago

ghost commented 8 years ago

Getting Started - Bitcoin Fullnode and Python Programming

OK so you have your #fullnode but now what? You want to be able to program with it of course.

You can use the Bitcoin RPC directly however it can be a bit slow on the Rasberry Pi 2 (http://bitcoinupdates.net/bitcoin-cli-very-slow-to-respond-to-rpc-calls-raspberry-pi/) but with some additional libraries interfacing with bitcoin becomes faster and quite simple.

Step 1

Assuming you have complied and installed bitcoin, use the following commands to download and install the latest python-bitcoinlib from Peter Todds repository on Github

git clone https://github.com/petertodd/python-bitcoinlib

Then run the following commands as root to install

cd python-bitcoinlib python setup.py build python setup.py install

Congratulatons, you are now able to interface with the bitcoin RPC at higher speed, using the simple commands from this python library.

Here are two very basic example scripts you are able to run on your #fullnode

balance.py

import bitcoin
#bitcoin.SelectParams("testnet")

import bitcoin.rpc
myproxy = bitcoin.rpc.Proxy()

bal = myproxy.getbalance()

print "Your balance is ...", bal, " satoshis."
print "Thank you for using bitcoin, have a nice day!"

exit()

send.py

import bitcoin
#bitcoin.SelectParams("testnet")

import bitcoin.rpc
import bitcoin.wallet

myproxy = bitcoin.rpc.Proxy()
bal = myproxy.getbalance()

print ("Your balance is ...")
print (bal)

address = raw_input("Please enter the address to send to: ")
if len(address) == 0 :
    print ("No address supplied, have a nice day!")
else :
    satoshis = raw_input("Please enter the number of satoshis to send: ")
    if len(satoshis) == 0 :
        print("We can not send zero satoshis, please enter only numbers, have a nice day!")
    else :
        myproxy.sendtoaddress(address, satoshis)
        print("Coins have been sent!")
exit()
ralphtheninja commented 8 years ago

This is great! We should script this as well and also put the example code inside this repository. That way when we clone and install the fullnode, everything is there for the user.

ghost commented 8 years ago

More example scripts can be found in https://github.com/OpenProvenance/python-bitcoinlib-scripting so if we could script the installation of the python-bitcoinlibs then the repo could also be cloned