Bitcoin or blockchain is crazy hot on internet today, and I’m curious what’s the best way for a programmer to get start with it. After read some books, I found that you can setup a test bitcoin testnet and play with it, so in this post I will share how it works.
Get a ubuntu server
First of all, you need to get a server or in your local environment. I’m running with latest ubuntu on a VPS server.
Install docker-ce in ubuntu
I’m using docker-ce here. To install docker-ce in ubuntu, you can follow the steps:
docker run -t -i -p 19001:19001 -p 19011:19011 freewil/bitcoin-testnet-box
Then you will see something similar with:
1
tester@84074da951a2:~/bitcoin-testnet-box$
Check the info
1
make getinfo
Generate blocks
Must generate 200 blocks to get balance
1
make generate BLOCKS=200
Install ruby
I’m using ruby to call the rpc-json server, so ruby have to be installed in server first.
1
apt-get install ruby
Run ruby script to bitcoin rpc-json
I got the code sample from bitcoin.it wiki, but replace the server with the username/password/post from the configuration in bitcoin-testnet-box. Save following code outside the docker instance as test.rb
require'net/http'require'uri'require'json'classBitcoinRPCdefinitialize(service_url)@uri=URI.parse(service_url)enddefmethod_missing(name,*args)post_body={'method'=>name,'params'=>args,'id'=>'jsonrpc'}.to_jsonresp=JSON.parse(http_post_request(post_body))raiseJSONRPCError,resp['error']ifresp['error']resp['result']enddefhttp_post_request(post_body)http=Net::HTTP.new(@uri.host,@uri.port)request=Net::HTTP::Post.new(@uri.request_uri)request.basic_auth@uri.user,@uri.passwordrequest.content_type='application/json'request.body=post_bodyhttp.request(request).bodyendclassJSONRPCError<RuntimeError;endendif$0==__FILE__h=BitcoinRPC.new('http://admin1:123@127.0.0.1:19001')ph.getbalanceph.getinfoph.getnewaddressph.dumpprivkey(h.getnewaddress)# also see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_listend