BitcoinJ

JAVA / Android Library


Andreas Petersson &
Harald Schilly

## What's Bitcoin?
Bitcoin is an experimental new digital currency that enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate with no central authority: managing transactions and issuing money are carried out collectively by the network.
-- bitcoin.org
## In Short *
You are your own bank!
* Money is stored in your machine. * worldwide * instant * ... and chargebacks are impossible!
## What's the differece? * No middleman * No contract * No TOS to care about.
## Scared? You can also rely on a third-party service: * [blockchain.info](https://blockchain.info/api): secure online wallet with an API * [BitPay](https://bitpay.com/): full blown merchant solution * [Paysius](http://paysius.com/): US only :( * [WalletBit](https://walletbit.com/businesstools): POS solutions
## Types of clients * [Classic "Satoshi" client](http://www.bitcoin.org/) * BitcoinJ * Forks / Alternative Implementations
## Resource Comparison
  • Satoshi client: 4800MB
  • Multibit: 21MB
  • BitcoinJ on Android: 5MB
## Projects using BitcoinJ * [Bitcoin Wallet for Android](https://play.google.com/store/apps/details?id=de.schildbach.wallet) * [Multibit](http://multibit.org/) * Satoshidice * Tons of lesser known applications
## BitcoinJ Project * Started as a 20% project at Google * First Rev: March 4th, 2011 * Main Devs: [Mike Hearn](https://plus.google.com/u/0/114798402540078632611), [Miron Cuperman](http://hyper.to/blog/), [Matt Corallo](https://github.com/TheBlueMatt) * Apache 2.0 Licensed * Still actively developed; alpha stage
BitcoinJ Website
## Goals * Java implementation of the Bitcoin protocol * Lightweight client for the Android ecosystem * Simple library for desktop applications * Suiteable for server-side processing
## Main Features
  • Manage a local Bitcoin wallet (no server!)
  • Connect and listen to other Bitcoin nodes directly
  • Send/recieve Bitcoins w/o storing the full blockchain (simplified payment verification)
## Ingredients * Public/Private Key in Wallet * Connection to Peers * Storage of past transactions (if necessary)
## Security BitcoinJ relies on the network to verify transactions. Only valid transactions are propagated and BitcoinJ checks if it receives its own transactions.
## BitcoinJ is not ... * a full Bitcoin node * fire-and-forget * 1.0 quality release
## The API

Show us the code!

Wallet

File walletFile = new File("coins.dat");
try {
  wallet = Wallet.loadFromFile(walletFile);
} catch (IOException e) {
  wallet = new Wallet(params);
  wallet.addKey(new ECKey());
  wallet.saveToFile(walletFile);
}

Network Type

For testing
NetworkParameters params = 
    NetworkParameters.testNet()
For production:
NetworkParameters params = 
    NetworkParameters.prodNet();

Address

Transform a public address to ...
Address a1 =
   new Address(params, "1HaSchNbFfLF8MJw41QNie7RPePPqdTozb");
1HaSchNbFfLF8MJw41QNie7RPePPqdTozb

Public/Private Key

ECKey key = new ECKey();
byte[] publicKey = key.getPubKey();
Gets the address corresponding to the public key.
Address addr = key.toAddress(params);
System.out.println(addr.toString());
1HwBZk111V4eEEC4BcN8fZd3RVifig2WjY

Connect to Network

final PeerGroup peerGroup =
  new PeerGroup(blockStore, params, chain);
peerGroup.setUserAgent("MyApp", "1.2");
peerGroup.addWallet(wallet);
peerGroup.addAddress(
  new PeerAddress(InetAddress.getLocalHost()));
peerGroup.start();

Payment Notification

wallet.addEventListener(
  new AbstractWalletEventListener() {
  public void onCoinsReceived(
      Wallet w,
      Transaction tx,
      BigInteger prevBalance,
      BigInteger newBalance) {
         // Running on a peer thread.
  }
});

Introspect Payment

// Wallet w, Transaction tx;
TransactionInput input = tx.getInputs().get(0);
Address from = input.getFromAddress();
BigInteger value = tx.getValueSentToMe(w);
println(Utils.bitcoinValueToFriendlyString(value));
Note: For each payment request, generate a new address.

Send a Payment

// Address to; Wallet w
Wallet.SendResult sendResult =
    w.sendCoins(peerGroup, to, value);
println("Sent coins! TX=" +
    sendResult.tx.getHashAsString());
Note: sendResult could be null.
## The Future of BitcoinJ * Full verification capability * Multisig support * External hardware wallets * Bloom filtering
## Bitcoin Network * No middleman: only *your* piece of software * No contract * No TOS to care about.

Talk

Bitcoin

BitcoinJ

© 2012 Harald Schilly & Andreas Petersson


devfest-bitcoinj@github

License: CC BY-SA

Based on work by Hakim El Hattab
Details: git blame ;-)