Search in sources :

Example 1 with UnreadableWalletException

use of org.bitcoinj.wallet.UnreadableWalletException 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 2 with UnreadableWalletException

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

use of org.bitcoinj.wallet.UnreadableWalletException in project toshi-android-client by toshiapp.

the class HDWallet method initFromMasterSeed.

private Wallet initFromMasterSeed(final String masterSeed) {
    try {
        final DeterministicSeed seed = getSeed(masterSeed);
        seed.check();
        return constructFromSeed(seed);
    } catch (final UnreadableWalletException | MnemonicException e) {
        LogUtil.exception("Error while initiating from from master seed", e);
        throw new RuntimeException("Unable to create wallet. Seed is invalid");
    }
}
Also used : DeterministicSeed(org.bitcoinj.wallet.DeterministicSeed) UnreadableWalletException(org.bitcoinj.wallet.UnreadableWalletException) MnemonicException(org.bitcoinj.crypto.MnemonicException)

Example 4 with UnreadableWalletException

use of org.bitcoinj.wallet.UnreadableWalletException in project toshi-android-client by toshiapp.

the class HDWallet method createFromMasterSeed.

public Single<HDWallet> createFromMasterSeed(final String masterSeed) {
    return Single.fromCallable(() -> {
        try {
            final DeterministicSeed seed = getSeed(masterSeed);
            seed.check();
            final Wallet wallet = constructFromSeed(seed);
            deriveKeysFromWallet(wallet);
            saveMasterSeedToStorage(masterSeed);
            return this;
        } catch (final UnreadableWalletException | MnemonicException e) {
            LogUtil.exception("Error while creating wallet from master seed", e);
            throw new InvalidMasterSeedException(e);
        }
    });
}
Also used : DeterministicSeed(org.bitcoinj.wallet.DeterministicSeed) UnreadableWalletException(org.bitcoinj.wallet.UnreadableWalletException) MnemonicException(org.bitcoinj.crypto.MnemonicException) Wallet(org.bitcoinj.wallet.Wallet) InvalidMasterSeedException(com.toshi.exception.InvalidMasterSeedException)

Example 5 with UnreadableWalletException

use of org.bitcoinj.wallet.UnreadableWalletException in project cryptoputty by alokmenghrajani.

the class CryptoputtyApplication method initialize.

@Override
public void initialize(final Bootstrap<CryptoputtyConfiguration> bootstrap) {
    bootstrap.addBundle(new ViewBundle<>());
    NetworkParameters params = TestNet3Params.get();
    File tempDir = Files.createTempDir();
    tempDir.deleteOnExit();
    kit = new WalletAppKit(params, tempDir, "cryptoputty");
    try {
        kit.restoreWalletFromSeed(new DeterministicSeed("office suit release flame robust know depth truly swim bird quality reopen", null, "", 1522261414L));
        kit.setBlockingStartup(false);
        kit.setAutoSave(true);
        kit.startAsync();
        kit.awaitRunning();
        // Perhaps having bloom filters doesn't hurt since we don't plan to do anything
        // kit.peerGroup().setBloomFilteringEnabled(false);
        // I have no idea what I'm doing, but let's increase these max values.
        kit.peerGroup().setMaxPeersToDiscoverCount(10000);
        kit.peerGroup().setMaxConnections(5000);
        log.info(format("send money to: %s", kit.wallet().freshReceiveAddress().toString()));
        log.info("done initializing");
    } catch (UnreadableWalletException e) {
        e.printStackTrace();
    }
}
Also used : DeterministicSeed(org.bitcoinj.wallet.DeterministicSeed) UnreadableWalletException(org.bitcoinj.wallet.UnreadableWalletException) NetworkParameters(org.bitcoinj.core.NetworkParameters) WalletAppKit(org.bitcoinj.kits.WalletAppKit) File(java.io.File)

Aggregations

UnreadableWalletException (org.bitcoinj.wallet.UnreadableWalletException)5 DeterministicSeed (org.bitcoinj.wallet.DeterministicSeed)3 Wallet (org.bitcoinj.wallet.Wallet)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 MnemonicException (org.bitcoinj.crypto.MnemonicException)2 WalletProtobufSerializer (org.bitcoinj.wallet.WalletProtobufSerializer)2 Stopwatch (com.google.common.base.Stopwatch)1 InvalidMasterSeedException (com.toshi.exception.InvalidMasterSeedException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 NetworkParameters (org.bitcoinj.core.NetworkParameters)1 WalletAppKit (org.bitcoinj.kits.WalletAppKit)1