Slide 1

Slide 1 text

getting started coding with bitcoinj a how to build Bitcoin applications using bitcoinj

Slide 2

Slide 2 text

hello, nice to meet you! ! • @bumi on the internets • working at Railslove - a web consultancy building juicy web apps for you

Slide 3

Slide 3 text

DevHouseFriday 01/03/2009

Slide 4

Slide 4 text

assumptions • you have heard of Bitcoin and have a basic understanding of how it works • you can read some (Java) code • you do not ask questions about cryptography 
 - other people can explain that better

Slide 5

Slide 5 text

warnings • treat the code as an overview - not as something that you can use in production. (we silently ignore some things) • you are dealing with real value/money AND buggy software AND no safety net

Slide 6

Slide 6 text

bitcoinj • a Java implementation of the Bitcoin protocol • no need for a local copy of the Bitcoin core • supports a lot of Bitcoin improvement proposals (BIPs) • m-of-n multisig transactions • Mnemonic codes for representing private keys • Payment protocol • for example used by the Android wallet, Multibit, Hive, biteasy.com

Slide 7

Slide 7 text

more bitcoinj features • wallet class with encryption, fee calculation, event listeners • connect to the P2P network • simplified payment verification mode / full verification mode • micropayment channels / Bitcoin's contracts features. • support for TOR (through Orchid)

Slide 8

Slide 8 text

other tools • bitcoind RPC interface • http://bitcore.io/ • https://github.com/lian/bitcoin-ruby • https://insight.is/ • https://helloblock.io/ • https://blockchain.info/api

Slide 9

Slide 9 text

basic bitcoinj structure • BlockChain: ZOMG the BlockChain! • BlockStore: stores the BlockChain somewhere (disk, DB,..) • PeerGroup: manages the network connections to peers • Wallet: manages the Keys and transaction data • WalletEventListener: “callbacks” for wallet events • NetworkParameters: selects the bitcoin network (test/ main)

Slide 10

Slide 10 text

package com.fridaypay; ! import com.google.bitcoin.core.*; import com.google.bitcoin.params.*; import com.google.bitcoin.store.*; import com.google.bitcoin.wallet.*; ! public class DevHouseFriday { ! public static void main(String[] args) { ! ! NetworkParameters params = MainNetParams.get(); ! ! } }

Slide 11

Slide 11 text

package com.fridaypay; ! import com.google.bitcoin.core.*; import com.google.bitcoin.params.*; ! public class DevHouseFriday { ! public static void main(String[] args) { ! ! NetworkParameters params = MainNetParams.get(); Wallet wallet = new Wallet(params); System.out.println("send money to: "+ wallet.freshReceiveAddress().toString()); ! } ! }

Slide 12

Slide 12 text

package com.fridaypay; ! import java.io.File; ! import com.google.bitcoin.core.*; import com.google.bitcoin.params.*; import com.google.bitcoin.store.*; ! public class DevHouseFriday { ! public static void main(String[] args) { ! ! NetworkParameters params = MainNetParams.get(); Wallet wallet = new Wallet(params); System.out.println("send money to: "+ wallet.freshReceiveAddress().toString()); SPVBlockStore chainStore = new SPVBlockStore(params, new File("dh.spvchain")); BlockChain chain = new BlockChain(params, chainStore); PeerGroup peers = new PeerGroup(params, chain); ! ! } ! }

Slide 13

Slide 13 text

! public static void main(String[] args) { NetworkParameters params = MainNetParams.get(); Wallet wallet = new Wallet(params); System.out.println("send money to: "+ wallet.freshReceiveAddress().toString()); SPVBlockStore chainStore = new SPVBlockStore(params, new File("dh.spvchain")); BlockChain chain = new BlockChain(params, chainStore); PeerGroup peers = new PeerGroup(params, chain); // the wallet needs to get informed about new blocks and new transactions chain.addWallet(wallet); peers.addWallet(wallet); } !

Slide 14

Slide 14 text

public static void main(String[] args) { // … // ! chain.addWallet(wallet); peers.addWallet(wallet); DownloadListener bListener = new DownloadListener(){ @Override public void doneDownload() { System.out.println("blockchain downloaded"); } }; peers.startAsync(); peers.awaitRunning(); peers.startBlockChainDownload(bListener); bListener.await(); ! }

Slide 15

Slide 15 text

public class WalletListener extends AbstractWalletEventListener { ! @Override public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) { System.out.println("-----> coins resceived: " + tx.getHashAsString()); System.out.println("received: " + tx.getValue(wallet)); } ! @Override public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) { System.out.println("-----> confidence changed: " + tx.getHashAsString()); TransactionConfidence confidence = tx.getConfidence(); System.out.println("new block depth: " + confidence.getDepthInBlocks()); } ! @Override public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) { System.out.println("coins sent"); } ! @Override public void onReorganize(Wallet wallet) { } ! } WalletListener wListener = new WalletListener(); wallet.addEventListener(wListener); !

Slide 16

Slide 16 text

public static void main(String[] args) { ! ! NetworkParameters params = TestNet3Params.get(); ! WalletAppKit kit = new WalletAppKit(params, new File("."), "walletappkit-example"); ! kit.startAsync(); kit.awaitRunning(); ! WalletListener wListener = new WalletListener(); kit.wallet().addEventListener(wListener); ! System.out.println("send money to:"+kit.wallet().freshReceiveAddress().toString()); ! // shutting down //System.out.println("shutting down again"); //kit.stopAsync(); //kit.awaitTerminated(); }

Slide 17

Slide 17 text

how to test your app? MainNet vs. TestNet vs. RegTest

Slide 18

Slide 18 text

NetworkParameters params = TestNet3Params.get(); • alternative blockchain • different genesis block • coins are worth nothing • same rules as main net

Slide 19

Slide 19 text

NetworkParameters params = RegTestParams.get(); • you run bitcoind in regression test mode • your own local bitcoin network • your own blockchain • difficulty is zero - you decide when blocks are created

Slide 20

Slide 20 text

more information • https://bitcoin.org/en/developer-documentation • https://github.com/aantonop/bitcoinbook • https://github.com/bitcoinj/bitcoinj/tree/master/examples/ src/main/java/com/google/bitcoin/examples • https://bitcoinj.github.io/ • join on IRC: #bitcoinj

Slide 21

Slide 21 text

Questions? thanks! follow me: @bumi