public class WalletAppKit extends AbstractIdleService
Utility class that wraps the boilerplate needed to set up a new SPV bitcoinj app. Instantiate it with a directory
and file prefix, optionally configure a few things, then use startAsync and optionally awaitRunning. The object will
construct and configure a BlockChain
, SPVBlockStore
, Wallet
and PeerGroup
. Depending
on the value of the blockingStartup property, startup will be considered complete once the block chain has fully
synchronized, so it can take a while.
To add listeners and modify the objects that are constructed, you can either do that by overriding the
onSetupCompleted()
method (which will run on a background thread) and make your changes there,
or by waiting for the service to start and then accessing the objects from wherever you want. However, you cannot
access the objects this class creates until startup is complete.
The asynchronous design of this class may seem puzzling (just use AbstractIdleService.awaitRunning()
if you don't want that).
It is to make it easier to fit bitcoinj into GUI apps, which require a high degree of responsiveness on their main
thread which handles all the animation and user interaction. Even when blockingStart is false, initializing bitcoinj
means doing potentially blocking file IO, generating keys and other potentially intensive operations. By running it
on a background thread, there's no risk of accidentally causing UI lag.
Note that AbstractIdleService.awaitRunning()
can throw an unchecked IllegalStateException
if anything goes wrong during startup - you should probably handle it and use Throwable.getCause()
to figure
out what went wrong more precisely. Same thing if you just use the AbstractIdleService.startAsync()
method.
Service.Listener, Service.State
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoStop |
protected boolean |
blockingStartup |
protected InputStream |
checkpoints |
protected Context |
context |
protected File |
directory |
protected PeerDiscovery |
discovery |
protected DownloadProgressTracker |
downloadListener |
protected String |
filePrefix |
protected static org.slf4j.Logger |
log |
protected NetworkParameters |
params |
protected PeerAddress[] |
peerAddresses |
protected DeterministicSeed |
restoreFromSeed |
protected boolean |
useAutoSave |
protected String |
userAgent |
protected boolean |
useTor |
protected BlockChain |
vChain |
protected String |
version |
protected PeerGroup |
vPeerGroup |
protected BlockStore |
vStore |
protected Wallet |
vWallet |
protected File |
vWalletFile |
protected WalletProtobufSerializer.WalletFactory |
walletFactory |
Constructor and Description |
---|
WalletAppKit(Context context,
File directory,
String filePrefix)
Creates a new WalletAppKit, with the given
Context . |
WalletAppKit(NetworkParameters params,
File directory,
String filePrefix)
Creates a new WalletAppKit, with a newly created
Context . |
Modifier and Type | Method and Description |
---|---|
BlockChain |
chain() |
WalletAppKit |
connectToLocalHost()
Will only connect to localhost.
|
protected PeerGroup |
createPeerGroup() |
protected Wallet |
createWallet() |
File |
directory() |
boolean |
isChainFileLocked()
Tests to see if the spvchain file has an operating system file lock on it.
|
protected void |
onSetupCompleted()
This method is invoked on a background thread after all objects are initialised, but before the peer group
or block chain download is started.
|
NetworkParameters |
params() |
PeerGroup |
peerGroup() |
protected BlockStore |
provideBlockStore(File file)
Override this to use a
BlockStore that isn't the default of SPVBlockStore . |
protected List<WalletExtension> |
provideWalletExtensions()
Override this to return wallet extensions if any are necessary.
|
WalletAppKit |
restoreWalletFromSeed(DeterministicSeed seed)
If a seed is set here then any existing wallet that matches the file name will be renamed to a backup name,
the chain file will be deleted, and the wallet object will be instantiated with the given seed instead of
a fresh one being created.
|
WalletAppKit |
setAutoSave(boolean value)
If true, the wallet will save itself to disk automatically whenever it changes.
|
WalletAppKit |
setAutoStop(boolean autoStop)
If true, will register a shutdown hook to stop the library.
|
WalletAppKit |
setBlockingStartup(boolean blockingStartup)
If true (the default) then the startup of this service won't be considered complete until the network has been
brought up, peer connections established and the block chain synchronised.
|
WalletAppKit |
setCheckpoints(InputStream checkpoints)
If set, the file is expected to contain a checkpoints file calculated with BuildCheckpoints.
|
WalletAppKit |
setDiscovery(PeerDiscovery discovery)
Sets the peer discovery class to use.
|
WalletAppKit |
setDownloadListener(DownloadProgressTracker listener)
If you want to learn about the sync process, you can provide a listener here.
|
WalletAppKit |
setPeerNodes(PeerAddress... addresses)
Will only connect to the given addresses.
|
protected void |
setupAutoSave(Wallet wallet) |
WalletAppKit |
setUserAgent(String userAgent,
String version)
Sets the string that will appear in the subver field of the version message.
|
protected void |
shutDown() |
protected void |
startUp() |
BlockStore |
store() |
WalletAppKit |
useTor()
If called, then an embedded Tor client library will be used to connect to the P2P network.
|
Wallet |
wallet() |
addListener, awaitRunning, awaitRunning, awaitTerminated, awaitTerminated, executor, failureCause, isRunning, serviceName, startAsync, state, stopAsync, toString
protected static final org.slf4j.Logger log
protected final String filePrefix
protected final NetworkParameters params
protected volatile BlockChain vChain
protected volatile BlockStore vStore
protected volatile Wallet vWallet
protected volatile PeerGroup vPeerGroup
protected final File directory
protected volatile File vWalletFile
protected boolean useAutoSave
protected PeerAddress[] peerAddresses
protected DownloadProgressTracker downloadListener
protected boolean autoStop
protected InputStream checkpoints
protected boolean blockingStartup
protected boolean useTor
protected String userAgent
protected String version
protected WalletProtobufSerializer.WalletFactory walletFactory
@Nullable protected DeterministicSeed restoreFromSeed
@Nullable protected PeerDiscovery discovery
protected volatile Context context
public WalletAppKit(NetworkParameters params, File directory, String filePrefix)
Context
. Files will be stored in the given directory.public WalletAppKit setPeerNodes(PeerAddress... addresses)
public WalletAppKit connectToLocalHost()
public WalletAppKit setAutoSave(boolean value)
public WalletAppKit setDownloadListener(DownloadProgressTracker listener)
org.bitcoinj.core.DownloadProgressTracker
is a good choice. This has no effect unless setBlockingStartup(false) has been called
too, due to some missing implementation code.public WalletAppKit setAutoStop(boolean autoStop)
public WalletAppKit setCheckpoints(InputStream checkpoints)
public WalletAppKit setBlockingStartup(boolean blockingStartup)
AbstractIdleService.awaitRunning()
can
potentially take a very long time. If false, then startup is considered complete once the network activity
begins and peer connections/block chain sync will continue in the background.public WalletAppKit setUserAgent(String userAgent, String version)
userAgent
- A short string that should be the name of your app, e.g. "My Wallet"version
- A short string that contains the version number, e.g. "1.0-BETA"public WalletAppKit useTor()
public WalletAppKit restoreWalletFromSeed(DeterministicSeed seed)
public WalletAppKit setDiscovery(@Nullable PeerDiscovery discovery)
protected List<WalletExtension> provideWalletExtensions() throws Exception
Override this to return wallet extensions if any are necessary.
When this is called, chain(), store(), and peerGroup() will return the created objects, however they are not initialized/started.
Exception
protected BlockStore provideBlockStore(File file) throws BlockStoreException
BlockStore
that isn't the default of SPVBlockStore
.BlockStoreException
protected void onSetupCompleted()
public boolean isChainFileLocked() throws IOException
IOException
protected void startUp() throws Exception
startUp
in class AbstractIdleService
Exception
protected void setupAutoSave(Wallet wallet)
protected Wallet createWallet()
protected PeerGroup createPeerGroup() throws TimeoutException
TimeoutException
protected void shutDown() throws Exception
shutDown
in class AbstractIdleService
Exception
public NetworkParameters params()
public BlockChain chain()
public BlockStore store()
public Wallet wallet()
public PeerGroup peerGroup()
public File directory()
Copyright © 2016. All rights reserved.