Search in sources :

Example 1 with KeyCrypterScrypt

use of org.bitcoinj.crypto.KeyCrypterScrypt in project bitsquare by bitsquare.

the class PasswordView method initialize.

@Override
public void initialize() {
    headline = addTitledGroupBg(root, gridRow, 2, "");
    passwordField = addLabelPasswordTextField(root, gridRow, "Enter password:", Layout.FIRST_ROW_DISTANCE).second;
    passwordField.setValidator(passwordValidator);
    passwordFieldChangeListener = (observable, oldValue, newValue) -> validatePasswords();
    Tuple2<Label, PasswordTextField> tuple2 = addLabelPasswordTextField(root, ++gridRow, "Repeat password:");
    repeatedPasswordLabel = tuple2.first;
    repeatedPasswordField = tuple2.second;
    repeatedPasswordField.setValidator(passwordValidator);
    repeatedPasswordFieldChangeListener = (observable, oldValue, newValue) -> validatePasswords();
    Tuple3<Button, BusyAnimation, Label> tuple = addButtonBusyAnimationLabel(root, ++gridRow, "", 15);
    pwButton = tuple.first;
    BusyAnimation busyAnimation = tuple.second;
    Label deriveStatusLabel = tuple.third;
    pwButton.setDisable(true);
    setText();
    pwButton.setOnAction(e -> {
        String password = passwordField.getText();
        checkArgument(password.length() < 50, "Password must be less then 50 characters.");
        pwButton.setDisable(true);
        deriveStatusLabel.setText("Derive key from password");
        busyAnimation.play();
        KeyCrypterScrypt keyCrypterScrypt;
        Wallet wallet = walletService.getWallet();
        if (wallet.isEncrypted())
            keyCrypterScrypt = (KeyCrypterScrypt) wallet.getKeyCrypter();
        else
            keyCrypterScrypt = ScryptUtil.getKeyCrypterScrypt();
        ScryptUtil.deriveKeyWithScrypt(keyCrypterScrypt, password, aesKey -> {
            deriveStatusLabel.setText("");
            busyAnimation.stop();
            if (wallet.isEncrypted()) {
                if (wallet.checkAESKey(aesKey)) {
                    walletService.decryptWallet(aesKey);
                    tradeWalletService.setAesKey(null);
                    new Popup().feedback("Wallet successfully decrypted and password protection removed.").show();
                    passwordField.setText("");
                    repeatedPasswordField.setText("");
                    walletService.backupWallet();
                } else {
                    pwButton.setDisable(false);
                    new Popup().warning("You entered the wrong password.\n\n" + "Please try entering your password again, carefully checking for typos or spelling errors.").show();
                }
            } else {
                walletService.encryptWallet(keyCrypterScrypt, aesKey);
                tradeWalletService.setAesKey(aesKey);
                new Popup().feedback("Wallet successfully encrypted and password protection enabled.").show();
                passwordField.setText("");
                repeatedPasswordField.setText("");
                walletService.clearBackup();
                walletService.backupWallet();
            }
            setText();
        });
    });
    addTitledGroupBg(root, ++gridRow, 1, "Information", Layout.GROUP_DISTANCE);
    addMultilineLabel(root, gridRow, "With password protection you need to enter your password when" + " withdrawing bitcoin out of your wallet or " + "if you want to view or restore a wallet from seed words as well as at application startup.", Layout.FIRST_ROW_AND_GROUP_DISTANCE);
}
Also used : BusyAnimation(io.bitsquare.gui.components.BusyAnimation) Button(javafx.scene.control.Button) Wallet(org.bitcoinj.core.Wallet) Popup(io.bitsquare.gui.main.overlays.popups.Popup) Label(javafx.scene.control.Label) PasswordTextField(io.bitsquare.gui.components.PasswordTextField) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt)

Example 2 with KeyCrypterScrypt

use of org.bitcoinj.crypto.KeyCrypterScrypt in project bitsquare by bitsquare.

the class WalletPasswordWindow method addButtons.

private void addButtons() {
    BusyAnimation busyAnimation = new BusyAnimation(false);
    Label deriveStatusLabel = new Label();
    unlockButton = new Button("Unlock");
    unlockButton.setDefaultButton(true);
    unlockButton.setDisable(true);
    unlockButton.setOnAction(e -> {
        String password = passwordTextField.getText();
        checkArgument(password.length() < 50, "Password must be less then 50 characters.");
        Wallet wallet = walletService.getWallet();
        KeyCrypterScrypt keyCrypterScrypt = (KeyCrypterScrypt) wallet.getKeyCrypter();
        if (keyCrypterScrypt != null) {
            busyAnimation.play();
            deriveStatusLabel.setText("Derive key from password");
            ScryptUtil.deriveKeyWithScrypt(keyCrypterScrypt, password, aesKey -> {
                if (wallet.checkAESKey(aesKey)) {
                    if (aesKeyHandler != null)
                        aesKeyHandler.onAesKey(aesKey);
                    hide();
                } else {
                    busyAnimation.stop();
                    deriveStatusLabel.setText("");
                    UserThread.runAfter(() -> new Popup().warning("You entered the wrong password.\n\n" + "Please try entering your password again, carefully checking for typos or spelling errors.").onClose(this::blurAgain).show(), Transitions.DEFAULT_DURATION, TimeUnit.MILLISECONDS);
                }
            });
        } else {
            log.error("wallet.getKeyCrypter() is null, that must not happen.");
        }
    });
    forgotPasswordButton = new Button("Forgot password?");
    forgotPasswordButton.setOnAction(e -> {
        forgotPasswordButton.setDisable(true);
        unlockButton.setDefaultButton(false);
        showRestoreScreen();
    });
    Button cancelButton = new Button("Cancel");
    cancelButton.setOnAction(event -> {
        hide();
        closeHandlerOptional.ifPresent(closeHandler -> closeHandler.run());
    });
    HBox hBox = new HBox();
    hBox.setMinWidth(560);
    hBox.setSpacing(10);
    GridPane.setRowIndex(hBox, ++rowIndex);
    GridPane.setColumnIndex(hBox, 1);
    hBox.setAlignment(Pos.CENTER_LEFT);
    if (hideCloseButton)
        hBox.getChildren().addAll(unlockButton, forgotPasswordButton, busyAnimation, deriveStatusLabel);
    else
        hBox.getChildren().addAll(unlockButton, cancelButton);
    gridPane.getChildren().add(hBox);
    ColumnConstraints columnConstraints1 = new ColumnConstraints();
    columnConstraints1.setHalignment(HPos.RIGHT);
    columnConstraints1.setHgrow(Priority.SOMETIMES);
    ColumnConstraints columnConstraints2 = new ColumnConstraints();
    columnConstraints2.setHgrow(Priority.ALWAYS);
    gridPane.getColumnConstraints().addAll(columnConstraints1, columnConstraints2);
}
Also used : HBox(javafx.scene.layout.HBox) BusyAnimation(io.bitsquare.gui.components.BusyAnimation) Wallet(org.bitcoinj.core.Wallet) ColumnConstraints(javafx.scene.layout.ColumnConstraints) Popup(io.bitsquare.gui.main.overlays.popups.Popup) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt)

Aggregations

BusyAnimation (io.bitsquare.gui.components.BusyAnimation)2 Popup (io.bitsquare.gui.main.overlays.popups.Popup)2 Wallet (org.bitcoinj.core.Wallet)2 KeyCrypterScrypt (org.bitcoinj.crypto.KeyCrypterScrypt)2 PasswordTextField (io.bitsquare.gui.components.PasswordTextField)1 Button (javafx.scene.control.Button)1 Label (javafx.scene.control.Label)1 ColumnConstraints (javafx.scene.layout.ColumnConstraints)1 HBox (javafx.scene.layout.HBox)1