Search in sources :

Example 1 with Storage

use of com.sparrowwallet.sparrow.io.Storage in project sparrow by sparrowwallet.

the class MessageSignDialog method openWallets.

@Subscribe
public void openWallets(OpenWalletsEvent event) {
    Storage storage = event.getStorage(wallet);
    if (storage == null) {
        // Another window, ignore
        return;
    }
    WalletPasswordDialog dlg = new WalletPasswordDialog(wallet.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD);
    Optional<SecureString> password = dlg.showAndWait();
    if (password.isPresent()) {
        Storage.DecryptWalletService decryptWalletService = new Storage.DecryptWalletService(walletNode.getWallet().copy(), password.get());
        decryptWalletService.setOnSucceeded(workerStateEvent -> {
            EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.END, "Done"));
            Wallet decryptedWallet = decryptWalletService.getValue();
            signUnencryptedKeystore(decryptedWallet);
            decryptedWallet.clearPrivate();
        });
        decryptWalletService.setOnFailed(workerStateEvent -> {
            EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.END, "Failed"));
            AppServices.showErrorDialog("Incorrect Password", decryptWalletService.getException().getMessage());
        });
        EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.START, "Decrypting wallet..."));
        decryptWalletService.start();
    }
}
Also used : Storage(com.sparrowwallet.sparrow.io.Storage) StorageEvent(com.sparrowwallet.sparrow.event.StorageEvent) Wallet(com.sparrowwallet.drongo.wallet.Wallet) SecureString(com.sparrowwallet.drongo.SecureString) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with Storage

use of com.sparrowwallet.sparrow.io.Storage in project sparrow by sparrowwallet.

the class PayNymController method addWalletIfNotificationTransactionPresent.

private void addWalletIfNotificationTransactionPresent(Wallet decryptedWallet, Map<BlockTransaction, PayNym> unlinkedPayNyms, Map<BlockTransaction, WalletNode> unlinkedNotifications) {
    List<Wallet> addedWallets = new ArrayList<>();
    for (BlockTransaction blockTransaction : unlinkedNotifications.keySet()) {
        try {
            PayNym payNym = unlinkedPayNyms.get(blockTransaction);
            PaymentCode externalPaymentCode = payNym.paymentCode();
            WalletNode input0Node = unlinkedNotifications.get(blockTransaction);
            Keystore keystore = input0Node.getWallet().isNested() ? decryptedWallet.getChildWallet(input0Node.getWallet().getName()).getKeystores().get(0) : decryptedWallet.getKeystores().get(0);
            ECKey input0Key = keystore.getKey(input0Node);
            TransactionOutPoint input0Outpoint = PaymentCode.getDesignatedInput(blockTransaction.getTransaction()).getOutpoint();
            SecretPoint secretPoint = new SecretPoint(input0Key.getPrivKeyBytes(), externalPaymentCode.getNotificationKey().getPubKey());
            byte[] blindingMask = PaymentCode.getMask(secretPoint.ECDHSecretAsBytes(), input0Outpoint.bitcoinSerialize());
            byte[] blindedPaymentCode = PaymentCode.blind(getMasterWallet().getPaymentCode().getPayload(), blindingMask);
            byte[] opReturnData = PaymentCode.getOpReturnData(blockTransaction.getTransaction());
            if (Arrays.equals(opReturnData, blindedPaymentCode)) {
                addedWallets.addAll(addChildWallets(payNym, externalPaymentCode));
            } else {
                blockTransaction.setLabel(INVALID_PAYMENT_CODE_LABEL);
                EventManager.get().post(new WalletEntryLabelsChangedEvent(input0Node.getWallet(), new TransactionEntry(input0Node.getWallet(), blockTransaction, Collections.emptyMap(), Collections.emptyMap())));
            }
        } catch (Exception e) {
            log.error("Error adding linked contact from notification transaction", e);
        }
    }
    if (!addedWallets.isEmpty()) {
        Wallet masterWallet = getMasterWallet();
        Storage storage = AppServices.get().getOpenWallets().get(masterWallet);
        EventManager.get().post(new ChildWalletsAddedEvent(storage, masterWallet, addedWallets));
        followingList.refresh();
    }
}
Also used : PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) ECKey(com.sparrowwallet.drongo.crypto.ECKey) Storage(com.sparrowwallet.sparrow.io.Storage) TransactionEntry(com.sparrowwallet.sparrow.wallet.TransactionEntry) SecretPoint(com.sparrowwallet.drongo.bip47.SecretPoint)

Example 3 with Storage

use of com.sparrowwallet.sparrow.io.Storage in project sparrow by sparrowwallet.

the class PayNymController method addWalletIfNotificationTransactionPresent.

private void addWalletIfNotificationTransactionPresent(List<PayNym> following) {
    Map<BlockTransaction, PayNym> unlinkedPayNyms = new HashMap<>();
    Map<BlockTransaction, WalletNode> unlinkedNotifications = new HashMap<>();
    for (PayNym payNym : following) {
        if (!isLinked(payNym)) {
            PaymentCode externalPaymentCode = payNym.paymentCode();
            Map<BlockTransaction, WalletNode> unlinkedNotification = getMasterWallet().getNotificationTransaction(externalPaymentCode);
            if (!unlinkedNotification.isEmpty() && !INVALID_PAYMENT_CODE_LABEL.equals(unlinkedNotification.keySet().iterator().next().getLabel())) {
                unlinkedNotifications.putAll(unlinkedNotification);
                unlinkedPayNyms.put(unlinkedNotification.keySet().iterator().next(), payNym);
            }
        }
    }
    Wallet wallet = getMasterWallet();
    if (!unlinkedNotifications.isEmpty()) {
        if (wallet.isEncrypted()) {
            Storage storage = AppServices.get().getOpenWallets().get(wallet);
            Optional<ButtonType> optButtonType = AppServices.showAlertDialog("Link contacts?", "Some contacts were found that may be already linked. Link these contacts? Your password is required to check.", Alert.AlertType.CONFIRMATION, ButtonType.NO, ButtonType.YES);
            if (optButtonType.isPresent() && optButtonType.get() == ButtonType.YES) {
                WalletPasswordDialog dlg = new WalletPasswordDialog(wallet.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD);
                Optional<SecureString> password = dlg.showAndWait();
                if (password.isPresent()) {
                    Storage.DecryptWalletService decryptWalletService = new Storage.DecryptWalletService(wallet.copy(), password.get());
                    decryptWalletService.setOnSucceeded(workerStateEvent -> {
                        EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.END, "Done"));
                        Wallet decryptedWallet = decryptWalletService.getValue();
                        addWalletIfNotificationTransactionPresent(decryptedWallet, unlinkedPayNyms, unlinkedNotifications);
                        decryptedWallet.clearPrivate();
                    });
                    decryptWalletService.setOnFailed(workerStateEvent -> {
                        EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.END, "Failed"));
                        AppServices.showErrorDialog("Incorrect Password", decryptWalletService.getException().getMessage());
                    });
                    EventManager.get().post(new StorageEvent(storage.getWalletId(wallet), TimedEvent.Action.START, "Decrypting wallet..."));
                    decryptWalletService.start();
                }
            }
        } else {
            addWalletIfNotificationTransactionPresent(wallet, unlinkedPayNyms, unlinkedNotifications);
        }
    }
}
Also used : PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) Storage(com.sparrowwallet.sparrow.io.Storage) SecureString(com.sparrowwallet.drongo.SecureString)

Example 4 with Storage

use of com.sparrowwallet.sparrow.io.Storage in project sparrow by sparrowwallet.

the class InitiatorDialog method acceptAndBroadcast.

private void acceptAndBroadcast(InitiatorController initiatorController, String walletId, Wallet wallet) {
    if (confirmationRequired && wallet.isEncrypted()) {
        WalletPasswordDialog dlg = new WalletPasswordDialog(wallet.getMasterName(), WalletPasswordDialog.PasswordRequirement.LOAD);
        Optional<SecureString> password = dlg.showAndWait();
        if (password.isPresent()) {
            Storage storage = AppServices.get().getOpenWallets().get(wallet);
            Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true);
            keyDerivationService.setOnSucceeded(workerStateEvent -> {
                EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Done"));
                initiatorController.accept();
                password.get().clear();
            });
            keyDerivationService.setOnFailed(workerStateEvent -> {
                EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Failed"));
                if (keyDerivationService.getException() instanceof InvalidPasswordException) {
                    Optional<ButtonType> optResponse = showErrorDialog("Invalid Password", "The wallet password was invalid. Try again?", ButtonType.CANCEL, ButtonType.OK);
                    if (optResponse.isPresent() && optResponse.get().equals(ButtonType.OK)) {
                        Platform.runLater(() -> acceptAndBroadcast(initiatorController, walletId, wallet));
                    }
                } else {
                    log.error("Error deriving wallet key", keyDerivationService.getException());
                }
            });
            EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.START, "Decrypting wallet..."));
            keyDerivationService.start();
        }
    } else {
        initiatorController.accept();
    }
}
Also used : WalletPasswordDialog(com.sparrowwallet.sparrow.control.WalletPasswordDialog) Storage(com.sparrowwallet.sparrow.io.Storage) StorageEvent(com.sparrowwallet.sparrow.event.StorageEvent) InvalidPasswordException(com.sparrowwallet.drongo.crypto.InvalidPasswordException) SecureString(com.sparrowwallet.drongo.SecureString)

Example 5 with Storage

use of com.sparrowwallet.sparrow.io.Storage in project sparrow by sparrowwallet.

the class PayNymController method addChildWallets.

public List<Wallet> addChildWallets(PayNym payNym, PaymentCode externalPaymentCode) {
    List<Wallet> addedWallets = new ArrayList<>();
    Wallet masterWallet = getMasterWallet();
    Storage storage = AppServices.get().getOpenWallets().get(masterWallet);
    List<ScriptType> scriptTypes = masterWallet.getScriptType() != ScriptType.P2PKH ? PayNym.getSegwitScriptTypes() : payNym.getScriptTypes();
    for (ScriptType childScriptType : scriptTypes) {
        String label = payNym.nymName() + " " + childScriptType.getName();
        Wallet addedWallet = masterWallet.addChildWallet(externalPaymentCode, childScriptType, label);
        if (!storage.isPersisted(addedWallet)) {
            try {
                storage.saveWallet(addedWallet);
            } catch (Exception e) {
                log.error("Error saving wallet", e);
                AppServices.showErrorDialog("Error saving wallet " + addedWallet.getName(), e.getMessage());
            }
        }
        addedWallets.add(addedWallet);
    }
    return addedWallets;
}
Also used : Storage(com.sparrowwallet.sparrow.io.Storage) SecureString(com.sparrowwallet.drongo.SecureString)

Aggregations

Storage (com.sparrowwallet.sparrow.io.Storage)12 SecureString (com.sparrowwallet.drongo.SecureString)8 PaymentCode (com.sparrowwallet.drongo.bip47.PaymentCode)4 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)2 SecretPoint (com.sparrowwallet.drongo.bip47.SecretPoint)2 ECKey (com.sparrowwallet.drongo.crypto.ECKey)2 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 StorageEvent (com.sparrowwallet.sparrow.event.StorageEvent)2 TransactionEntry (com.sparrowwallet.sparrow.wallet.TransactionEntry)2 IOException (java.io.IOException)2 Subscribe (com.google.common.eventbus.Subscribe)1 InvalidPasswordException (com.sparrowwallet.drongo.crypto.InvalidPasswordException)1 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)1 Sha256Hash (com.sparrowwallet.drongo.protocol.Sha256Hash)1 PSBT (com.sparrowwallet.drongo.psbt.PSBT)1 WalletPasswordDialog (com.sparrowwallet.sparrow.control.WalletPasswordDialog)1 WalletDataChangedEvent (com.sparrowwallet.sparrow.event.WalletDataChangedEvent)1 WalletHistoryClearedEvent (com.sparrowwallet.sparrow.event.WalletHistoryClearedEvent)1 ElectrumServer (com.sparrowwallet.sparrow.net.ElectrumServer)1 Whirlpool (com.sparrowwallet.sparrow.whirlpool.Whirlpool)1