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);
}
}
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();
}
}
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);
}
}
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);
}
}
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;
}
Aggregations