com.google.bitcoin.core
Class Wallet

java.lang.Object
  extended by com.google.bitcoin.core.Wallet
All Implemented Interfaces:
Serializable

public class Wallet
extends Object
implements Serializable

A Wallet stores keys and a record of transactions that have not yet been spent. Thus, it is capable of providing transactions on demand that meet a given combined value.

The Wallet is read and written from disk, so be sure to follow the Java serialization versioning rules here. We use the built in Java serialization to avoid the need to pull in a potentially large (code-size) third party serialization library.

See Also:
Serialized Form

Nested Class Summary
static class Wallet.BalanceType
          It's possible to calculate a wallets balance from multiple points of view.
 
Field Summary
 ArrayList<ECKey> keychain
          A list of public/private EC keys owned by this user.
 
Constructor Summary
Wallet(NetworkParameters params)
          Creates a new, empty wallet with no keys and no transactions.
 
Method Summary
 void addEventListener(WalletEventListener listener)
          Adds an event listener object.
 void addKey(ECKey key)
          Adds the given ECKey to the wallet.
 ECKey findKeyFromPubHash(byte[] pubkeyHash)
          Locates a keypair from the keychain given the hash of the public key.
 ECKey findKeyFromPubKey(byte[] pubkey)
          Locates a keypair from the keychain given the raw public key bytes.
 BigInteger getBalance()
          Returns the AVAILABLE balance of this wallet.
 BigInteger getBalance(Wallet.BalanceType balanceType)
          Returns the balance of this wallet as calculated by the provided balanceType.
 Collection<Transaction> getPendingTransactions()
          Returns an immutable view of the transactions currently waiting for network confirmations.
 boolean isPubKeyHashMine(byte[] pubkeyHash)
          Returns true if this wallet contains a public key which hashes to the given hash.
 boolean isPubKeyMine(byte[] pubkey)
          Returns true if this wallet contains a keypair with the given public key.
static Wallet loadFromFile(File f)
          Returns a wallet deserialized from the given file.
static Wallet loadFromFileStream(FileInputStream f)
          Returns a wallet deserialied from the given file input stream.
 void saveToFile(File f)
          Uses Java serialization to save the wallet to the given file.
 void saveToFileStream(FileOutputStream f)
          Uses Java serialization to save the wallet to the given file stream.
 Transaction sendCoins(Peer peer, Address to, BigInteger nanocoins)
          Sends coins to the given address, via the given Peer.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

keychain

public final ArrayList<ECKey> keychain
A list of public/private EC keys owned by this user.

Constructor Detail

Wallet

public Wallet(NetworkParameters params)
Creates a new, empty wallet with no keys and no transactions. If you want to restore a wallet from disk instead, see loadFromFile.

Method Detail

saveToFile

public void saveToFile(File f)
                throws IOException
Uses Java serialization to save the wallet to the given file.

Throws:
IOException

saveToFileStream

public void saveToFileStream(FileOutputStream f)
                      throws IOException
Uses Java serialization to save the wallet to the given file stream.

Throws:
IOException

loadFromFile

public static Wallet loadFromFile(File f)
                           throws IOException
Returns a wallet deserialized from the given file.

Throws:
IOException

loadFromFileStream

public static Wallet loadFromFileStream(FileInputStream f)
                                 throws IOException
Returns a wallet deserialied from the given file input stream.

Throws:
IOException

addEventListener

public void addEventListener(WalletEventListener listener)
Adds an event listener object. Methods on this object are called when something interesting happens, like receiving money.

Threading: Event listener methods are dispatched on library provided threads and the both the wallet and the listener objects are locked during dispatch, so your listeners do not have to be thread safe. However they should not block as the Peer will be unresponsive to network traffic whilst your listener is running.


sendCoins

public Transaction sendCoins(Peer peer,
                             Address to,
                             BigInteger nanocoins)
                      throws IOException
Sends coins to the given address, via the given Peer. Change is returned to the first key in the wallet.

Parameters:
to - Which address to send coins to.
nanocoins - How many nanocoins to send. You can use Utils.toNanoCoins() to calculate this.
Returns:
The Transaction that was created or null if there was insufficient balance to send the coins.
Throws:
IOException - if there was a problem broadcasting the transaction

addKey

public void addKey(ECKey key)
Adds the given ECKey to the wallet. There is currently no way to delete keys (that would result in coin loss).


findKeyFromPubHash

public ECKey findKeyFromPubHash(byte[] pubkeyHash)
Locates a keypair from the keychain given the hash of the public key. This is needed when finding out which key we need to use to redeem a transaction output.

Returns:
ECKey object or null if no such key was found.

isPubKeyHashMine

public boolean isPubKeyHashMine(byte[] pubkeyHash)
Returns true if this wallet contains a public key which hashes to the given hash.


findKeyFromPubKey

public ECKey findKeyFromPubKey(byte[] pubkey)
Locates a keypair from the keychain given the raw public key bytes.

Returns:
ECKey or null if no such key was found.

isPubKeyMine

public boolean isPubKeyMine(byte[] pubkey)
Returns true if this wallet contains a keypair with the given public key.


getBalance

public BigInteger getBalance()
Returns the AVAILABLE balance of this wallet. See Wallet.BalanceType.AVAILABLE for details on what this means.

Note: the estimated balance is usually the one you want to show to the end user - however attempting to actually spend these coins may result in temporary failure. This method returns how much you can safely provide to createSend(Address, java.math.BigInteger).


getBalance

public BigInteger getBalance(Wallet.BalanceType balanceType)
Returns the balance of this wallet as calculated by the provided balanceType.


toString

public String toString()
Overrides:
toString in class Object

getPendingTransactions

public Collection<Transaction> getPendingTransactions()
Returns an immutable view of the transactions currently waiting for network confirmations.



Copyright © 2011. All Rights Reserved.