Free Java Tutorials >> Table of contents >> Bitcoin Tutorial Part1

1. Bitcoin Programming Tutorial, Part 1

Welcome to the Bitcoin Java Programming Tutorial

Click the run button at the bottom of this section! This interactive tutorial uses BitcoinJ. You can modify the code examples in your browser. This coding system has internet disabled, so do not use it to send or receive real money!

How do you make a key?

You should already be familiar with a bitcoin public address like 1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T. Public addresses allow users to receive funds, and private keys allow users to send funds. Here is the corresponding private key for the above public address: 5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS.

Some technical details: A bitcoin key is actually an ECDSA public private key pair, which uses the secp256k1 parameter. The curve equation is y^2 = x^3 + 7.

In BitcoinJ, a key is represented by the "ECKey". To generate a random key, use the default constructor. The standard toString() allows you to print the public key, and you can use toStringWithPrivate to print the private key:

import com.google.bitcoin.core.*;
public class MyProgram {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		ECKey key = new ECKey();
		System.out.println("We created key:\n" + key);
		System.out.println("\n\nHere with private:\n" + key.toStringWithPrivate());
	}
}

Notice that an ECKey key is actually a long hexadecimal string, such as 00956f37ff89dd854922d87b52fd9ec21144b5aa5162e293ebe3c157100dc4eaec . We will convert this into an address later. If you already already have a known private key, you can convert this into an ECKey. You will need to first create a BigInteger from hex. Try running this code:

2. Converting a key to an address

Converting a key to an address

There are multiple bitcoin networks and forks. You can use the same keys on multiple networks, but the addresses generated from those keys will be different. You can identify the main bitcoin network, "prodnet" with addresses that start with 1.

To convert an ECKey to an address, you first need to create a NetworkParameters. Then, use the key.toAddress(netParams) method:

3. Combining Keys into a Wallet

Creating a wallet and adding keys

A wallet is primarily an ArrayList of keys and a NetworkParams. Wallets can also contain information about transactions associated with the keys, and wallets can listen to events in order to automatically update the balances in the addresses.

You create a wallet with the network parameters given to the constructor. Then, you use Wallet.addKey for each ECKey.

Although our code box below does not allow you to save the wallet on our filesystem, you can use wallet.saveToFile(File walletFile) to serialize the wallet to disk. Important: for production environments, you should encrypt the private keys before saving.

Getting keys out of wallets

Because Wallets can be used live, the wallet.getKeys() returns a copy, not a reference, to the keys. Run this code as an example:

Previous: Twitter Beginner Problems

Next: Bitcoin Tutorial Part2