Upgrade to Pro — share decks privately, control downloads, hide ads and more …

getting started coding with bitcoinj

getting started coding with bitcoinj

A short presentation on how to get started writing Bitcoin applications with the Bitcoinj Java library. I've talked about this at the last DevHouseFriday in Cologne.

Find coding examples here:
Basic usage: https://github.com/bumi/bitcoinj/blob/master/examples/src/main/java/com/google/bitcoin/examples/Kit.java
How to send coins to an address: https://github.com/bumi/bitcoinj/blob/master/examples/src/main/java/com/google/bitcoin/examples/SendRequest.java

more:
https://github.com/bumi/bitcoinj/tree/master/examples/src/main/java/com/google/bitcoin/examples

link collection: http://michaelbumann.com/post/97913171772/how-to-develop-bitcoin-applications-an-overview

Michael Bumann

September 05, 2014
Tweet

More Decks by Michael Bumann

Other Decks in Programming

Transcript

  1. hello, nice to meet you! ! • @bumi on the

    internets • working at Railslove - a web consultancy building juicy web apps for you
  2. 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
  3. 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
  4. 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
  5. 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)
  6. other tools • bitcoind RPC interface • http://bitcore.io/ • https://github.com/lian/bitcoin-ruby

    • https://insight.is/ • https://helloblock.io/ • https://blockchain.info/api
  7. 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)
  8. 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(); ! ! } }
  9. 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()); ! } ! }
  10. 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); ! ! } ! }
  11. ! 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); } !
  12. 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(); ! }
  13. 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); !
  14. 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(); }
  15. 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