use of org.bitcoinj.wallet.Wallet in project bitcoin-wallet by bitcoin-wallet.
the class BlockchainService method onCreate.
@Override
public void onCreate() {
serviceCreatedAt = System.currentTimeMillis();
log.debug(".onCreate()");
super.onCreate();
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
application = (WalletApplication) getApplication();
config = application.getConfiguration();
final Wallet wallet = application.getWallet();
peerConnectivityListener = new PeerConnectivityListener();
broadcastPeerState(0);
blockChainFile = new File(getDir("blockstore", Context.MODE_PRIVATE), Constants.Files.BLOCKCHAIN_FILENAME);
final boolean blockChainFileExists = blockChainFile.exists();
if (!blockChainFileExists) {
log.info("blockchain does not exist, resetting wallet");
wallet.reset();
}
try {
blockStore = new SPVBlockStore(Constants.NETWORK_PARAMETERS, blockChainFile);
// detect corruptions as early as possible
blockStore.getChainHead();
final long earliestKeyCreationTime = wallet.getEarliestKeyCreationTime();
if (!blockChainFileExists && earliestKeyCreationTime > 0) {
try {
final Stopwatch watch = Stopwatch.createStarted();
final InputStream checkpointsInputStream = getAssets().open(Constants.Files.CHECKPOINTS_FILENAME);
CheckpointManager.checkpoint(Constants.NETWORK_PARAMETERS, checkpointsInputStream, blockStore, earliestKeyCreationTime);
watch.stop();
log.info("checkpoints loaded from '{}', took {}", Constants.Files.CHECKPOINTS_FILENAME, watch);
} catch (final IOException x) {
log.error("problem reading checkpoints, continuing without", x);
}
}
} catch (final BlockStoreException x) {
blockChainFile.delete();
final String msg = "blockstore cannot be created";
log.error(msg, x);
throw new Error(msg, x);
}
try {
blockChain = new BlockChain(Constants.NETWORK_PARAMETERS, wallet, blockStore);
} catch (final BlockStoreException x) {
throw new Error("blockchain cannot be created", x);
}
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
// implicitly start PeerGroup
registerReceiver(connectivityReceiver, intentFilter);
application.getWallet().addCoinsReceivedEventListener(Threading.SAME_THREAD, walletEventListener);
application.getWallet().addCoinsSentEventListener(Threading.SAME_THREAD, walletEventListener);
application.getWallet().addChangeEventListener(Threading.SAME_THREAD, walletEventListener);
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}
use of org.bitcoinj.wallet.Wallet in project bitcoin-wallet by bitcoin-wallet.
the class UpgradeWalletService method onHandleIntent.
@Override
protected void onHandleIntent(final Intent intent) {
org.bitcoinj.core.Context.propagate(Constants.CONTEXT);
final Wallet wallet = application.getWallet();
if (wallet.isDeterministicUpgradeRequired()) {
log.info("detected non-HD wallet, upgrading");
// upgrade wallet to HD
wallet.upgradeToDeterministic(null);
// let other service pre-generate look-ahead keys
BlockchainService.start(this, false);
}
maybeUpgradeToSecureChain(wallet);
}
use of org.bitcoinj.wallet.Wallet in project bitcoin-wallet by bitcoin-wallet.
the class WalletApplication method loadWalletFromProtobuf.
private void loadWalletFromProtobuf() {
if (walletFile.exists()) {
try (final FileInputStream walletStream = new FileInputStream(walletFile)) {
final Stopwatch watch = Stopwatch.createStarted();
wallet = new WalletProtobufSerializer().readWallet(walletStream);
watch.stop();
if (!wallet.getParams().equals(Constants.NETWORK_PARAMETERS))
throw new UnreadableWalletException("bad wallet network parameters: " + wallet.getParams().getId());
log.info("wallet loaded from: '{}', took {}", walletFile, watch);
} catch (final IOException | UnreadableWalletException x) {
log.error("problem loading wallet", x);
Toast.makeText(WalletApplication.this, x.getClass().getName(), Toast.LENGTH_LONG).show();
wallet = restoreWalletFromBackup();
}
if (!wallet.isConsistent()) {
Toast.makeText(this, "inconsistent wallet: " + walletFile, Toast.LENGTH_LONG).show();
wallet = restoreWalletFromBackup();
}
if (!wallet.getParams().equals(Constants.NETWORK_PARAMETERS))
throw new Error("bad wallet network parameters: " + wallet.getParams().getId());
} else {
final Stopwatch watch = Stopwatch.createStarted();
wallet = new Wallet(Constants.NETWORK_PARAMETERS);
saveWallet();
backupWallet();
watch.stop();
log.info("fresh wallet created, took {}", watch);
config.armBackupReminder();
}
}
use of org.bitcoinj.wallet.Wallet in project bitcoin-wallet by bitcoin-wallet.
the class WalletApplication method restoreWalletFromBackup.
private Wallet restoreWalletFromBackup() {
try (final InputStream is = openFileInput(Constants.Files.WALLET_KEY_BACKUP_PROTOBUF)) {
final Wallet wallet = new WalletProtobufSerializer().readWallet(is, true, null);
if (!wallet.isConsistent())
throw new Error("inconsistent backup");
BlockchainService.resetBlockchain(this);
Toast.makeText(this, R.string.toast_wallet_reset, Toast.LENGTH_LONG).show();
log.info("wallet restored from backup: '" + Constants.Files.WALLET_KEY_BACKUP_PROTOBUF + "'");
return wallet;
} catch (final IOException | UnreadableWalletException x) {
throw new Error("cannot read backup", x);
}
}
use of org.bitcoinj.wallet.Wallet in project bitcoin-wallet by bitcoin-wallet.
the class WalletApplication method backupWallet.
public void backupWallet() {
final Stopwatch watch = Stopwatch.createStarted();
final Protos.Wallet.Builder builder = new WalletProtobufSerializer().walletToProto(wallet).toBuilder();
// strip redundant
builder.clearTransaction();
builder.clearLastSeenBlockHash();
builder.setLastSeenBlockHeight(-1);
builder.clearLastSeenBlockTimeSecs();
final Protos.Wallet walletProto = builder.build();
try (final OutputStream os = openFileOutput(Constants.Files.WALLET_KEY_BACKUP_PROTOBUF, Context.MODE_PRIVATE)) {
walletProto.writeTo(os);
watch.stop();
log.info("wallet backed up to: '{}', took {}", Constants.Files.WALLET_KEY_BACKUP_PROTOBUF, watch);
} catch (final IOException x) {
log.error("problem writing wallet backup", x);
}
}
Aggregations