Search in sources :

Example 1 with Wallet

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));
}
Also used : IntentFilter(android.content.IntentFilter) BlockStoreException(org.bitcoinj.store.BlockStoreException) BlockChain(org.bitcoinj.core.BlockChain) Wallet(org.bitcoinj.wallet.Wallet) InputStream(java.io.InputStream) SPVBlockStore(org.bitcoinj.store.SPVBlockStore) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) PowerManager(android.os.PowerManager) File(java.io.File)

Example 2 with Wallet

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);
}
Also used : Wallet(org.bitcoinj.wallet.Wallet)

Example 3 with 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();
    }
}
Also used : UnreadableWalletException(org.bitcoinj.wallet.UnreadableWalletException) Wallet(org.bitcoinj.wallet.Wallet) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) WalletProtobufSerializer(org.bitcoinj.wallet.WalletProtobufSerializer)

Example 4 with Wallet

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);
    }
}
Also used : UnreadableWalletException(org.bitcoinj.wallet.UnreadableWalletException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Wallet(org.bitcoinj.wallet.Wallet) IOException(java.io.IOException) WalletProtobufSerializer(org.bitcoinj.wallet.WalletProtobufSerializer)

Example 5 with Wallet

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);
    }
}
Also used : Protos(org.bitcoinj.wallet.Protos) Wallet(org.bitcoinj.wallet.Wallet) OutputStream(java.io.OutputStream) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) WalletProtobufSerializer(org.bitcoinj.wallet.WalletProtobufSerializer)

Aggregations

Wallet (org.bitcoinj.wallet.Wallet)22 Transaction (org.bitcoinj.core.Transaction)9 Preferences (bisq.core.user.Preferences)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Coin (org.bitcoinj.core.Coin)6 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 Nullable (javax.annotation.Nullable)5 Address (org.bitcoinj.core.Address)5 TransactionConfidence (org.bitcoinj.core.TransactionConfidence)5 AddressEntry (bisq.core.btc.AddressEntry)4 AddressEntryList (bisq.core.btc.AddressEntryList)4 Restrictions (bisq.core.btc.Restrictions)4 TransactionVerificationException (bisq.core.btc.exceptions.TransactionVerificationException)4 WalletException (bisq.core.btc.exceptions.WalletException)4 FeeService (bisq.core.provider.fee.FeeService)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4