Search in sources :

Example 1 with WalletProtobufSerializer

use of org.bitcoinj.wallet.WalletProtobufSerializer in project bitcoin-wallet by bitcoin-wallet.

the class BackupWalletDialogFragment method onActivityResult.

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    if (requestCode == REQUEST_CODE_CREATE_DOCUMENT) {
        if (resultCode == Activity.RESULT_OK) {
            final Uri targetUri = intent.getData();
            final String password = passwordView.getText().toString().trim();
            checkState(!password.isEmpty());
            wipePasswords();
            dismiss();
            final Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
            try (final Writer cipherOut = new OutputStreamWriter(activity.getContentResolver().openOutputStream(targetUri), StandardCharsets.UTF_8)) {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                walletProto.writeTo(baos);
                baos.close();
                final byte[] plainBytes = baos.toByteArray();
                cipherOut.write(Crypto.encrypt(plainBytes, password.toCharArray()));
                cipherOut.flush();
                final String target = uriToTarget(targetUri);
                log.info("backed up wallet to: '" + targetUri + "'" + (target != null ? " (" + target + ")" : ""));
                final DialogBuilder dialog = new DialogBuilder(activity);
                dialog.setTitle(R.string.export_keys_dialog_title);
                dialog.setMessage(Html.fromHtml(getString(R.string.export_keys_dialog_success, target != null ? target : targetUri)));
                dialog.singleDismissButton(null);
                dialog.show();
                application.getConfiguration().disarmBackupReminder();
            } catch (final IOException x) {
                log.error("problem backing up wallet", x);
                final DialogBuilder dialog = DialogBuilder.warn(activity, R.string.import_export_keys_dialog_failure_title);
                dialog.setMessage(getString(R.string.export_keys_dialog_failure, x.getMessage()));
                dialog.singleDismissButton(null);
                dialog.show();
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            log.info("cancelled backing up wallet");
            passwordView.setEnabled(true);
            passwordAgainView.setEnabled(true);
        }
    } else {
        super.onActivityResult(requestCode, resultCode, intent);
    }
}
Also used : Protos(org.bitcoinj.wallet.Protos) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder) Uri(android.net.Uri) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) WalletProtobufSerializer(org.bitcoinj.wallet.WalletProtobufSerializer)

Example 2 with WalletProtobufSerializer

use of org.bitcoinj.wallet.WalletProtobufSerializer 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 3 with WalletProtobufSerializer

use of org.bitcoinj.wallet.WalletProtobufSerializer 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 4 with WalletProtobufSerializer

use of org.bitcoinj.wallet.WalletProtobufSerializer 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)

Example 5 with WalletProtobufSerializer

use of org.bitcoinj.wallet.WalletProtobufSerializer in project bisq-core by bisq-network.

the class WalletConfig method loadWallet.

private Wallet loadWallet(File walletFile, boolean shouldReplayWallet, boolean useBitcoinDeterministicKeyChain) throws Exception {
    Wallet wallet;
    try (FileInputStream walletStream = new FileInputStream(walletFile)) {
        List<WalletExtension> extensions = provideWalletExtensions();
        WalletExtension[] extArray = extensions.toArray(new WalletExtension[extensions.size()]);
        Protos.Wallet proto = WalletProtobufSerializer.parseToProto(walletStream);
        final WalletProtobufSerializer serializer;
        if (walletFactory != null)
            serializer = new WalletProtobufSerializer(walletFactory);
        else
            serializer = new WalletProtobufSerializer();
        serializer.setKeyChainFactory(new BisqKeyChainFactory(useBitcoinDeterministicKeyChain));
        wallet = serializer.readWallet(params, extArray, proto);
        if (shouldReplayWallet)
            wallet.reset();
    }
    return wallet;
}
Also used : WalletExtension(org.bitcoinj.wallet.WalletExtension) Protos(org.bitcoinj.wallet.Protos) Wallet(org.bitcoinj.wallet.Wallet) FileInputStream(java.io.FileInputStream) WalletProtobufSerializer(org.bitcoinj.wallet.WalletProtobufSerializer)

Aggregations

WalletProtobufSerializer (org.bitcoinj.wallet.WalletProtobufSerializer)5 IOException (java.io.IOException)4 Wallet (org.bitcoinj.wallet.Wallet)4 FileInputStream (java.io.FileInputStream)3 Protos (org.bitcoinj.wallet.Protos)3 Stopwatch (com.google.common.base.Stopwatch)2 UnreadableWalletException (org.bitcoinj.wallet.UnreadableWalletException)2 Uri (android.net.Uri)1 DialogBuilder (de.schildbach.wallet.ui.DialogBuilder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 WalletExtension (org.bitcoinj.wallet.WalletExtension)1