org.bitcoinj.crypto.MnemonicCode Java Examples

The following examples show how to use org.bitcoinj.crypto.MnemonicCode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SeedUtil.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
public static MnemonicCode getMCode()
{
  StringBuilder sb = new StringBuilder();
  for(String s : SeedWordList.getWordList())
  {
    sb.append(s); sb.append('\n');
  }
  byte[] b = sb.toString().getBytes();

  try
  {
    return new MnemonicCode(new ByteArrayInputStream(b),"ad90bf3beb7b0eb7e5acd74727dc0da96e0a280a258354e7293fb7e211ac03db");
  }
  catch(java.io.IOException e)
  {
    throw new RuntimeException(e);
  }
}
 
Example #2
Source File: SeedWordsView.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
private LocalDate getWalletDate() {
    LocalDate walletDate = restoreDatePicker.getValue();
    // Even though no current Bisq wallet could have been created before the v0.5 release date (2017.06.28),
    // the user may want to import from a seed generated by another wallet.
    // So use when the BIP39 standard was finalised (2013.10.09) as the oldest possible wallet date.
    LocalDate oldestWalletDate = LocalDate.ofInstant(
            Instant.ofEpochMilli(MnemonicCode.BIP39_STANDARDISATION_TIME_SECS * 1000),
            TimeZone.getDefault().toZoneId());
    if (walletDate == null) {
        // No date was specified, perhaps the user doesn't know the wallet date
        walletDate = oldestWalletDate;
    } else if (walletDate.isBefore(oldestWalletDate)) {
        walletDate = oldestWalletDate;
    } else if (walletDate.isAfter(LocalDate.now())) {
        walletDate = LocalDate.now();
    }
    return walletDate;
}
 
Example #3
Source File: SPV.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public void startService(final Context ctx) throws Exception {
    GaService.createNotificationChannel(ctx);

    // Provide bitcoinj with Mnemonics. These are used if we need to create a fake
    // wallet during SPV_SYNCRONIZATION syncing to prevent an exception.
    final ArrayList<String> words = new ArrayList<>(Wally.BIP39_WORDLIST_LEN);
    MnemonicHelper.initWordList(words, null);
    MnemonicCode.INSTANCE = new MnemonicCode(words, null);

    Log.d(TAG, "onCreate: binding service");
    final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(final ComponentName className,
                                       final IBinder service) {
            Log.d(TAG, "onServiceConnected: dispatching onServiceAttached callbacks");
            mService = ((GaService.GaBinder)service).getService();
            mService.onBound(ctx);
            startAsync();
            onServiceAttached.set(null);

        }

        @Override
        public void onServiceDisconnected(final ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: dispatching onServiceAttached exception");
            onServiceAttached.setException(new GAException(name.toString()));
        }
    };
    final Intent intent = new Intent(ctx, GaService.class);
    ctx.bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
 
Example #4
Source File: WalletSettingsController.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public void initialize(@Nullable KeyParameter aesKey) {
    DeterministicSeed seed = Main.bitcoin.wallet().getKeyChainSeed();
    if (aesKey == null) {
        if (seed.isEncrypted()) {
            log.info("Wallet is encrypted, requesting password first.");
            // Delay execution of this until after we've finished initialising this screen.
            Platform.runLater(() -> askForPasswordAndRetry());
            return;
        }
    } else {
        this.aesKey = aesKey;
        seed = seed.decrypt(checkNotNull(Main.bitcoin.wallet().getKeyCrypter()), "", aesKey);
        // Now we can display the wallet seed as appropriate.
        passwordButton.setText("Remove password");
    }

    // Set the date picker to show the birthday of this wallet.
    Instant creationTime = Instant.ofEpochSecond(seed.getCreationTimeSeconds());
    LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
    datePicker.setValue(origDate);

    // Set the mnemonic seed words.
    final List<String> mnemonicCode = seed.getMnemonicCode();
    checkNotNull(mnemonicCode);    // Already checked for encryption.
    String origWords = Utils.join(mnemonicCode);
    wordsArea.setText(origWords);

    // Validate words as they are being typed.
    MnemonicCode codec = unchecked(MnemonicCode::new);
    TextFieldValidator validator = new TextFieldValidator(wordsArea, text ->
        !didThrow(() -> codec.check(Splitter.on(' ').splitToList(text)))
    );

    // Clear the date picker if the user starts editing the words, if it contained the current wallets date.
    // This forces them to set the birthday field when restoring.
    wordsArea.textProperty().addListener(o -> {
        if (origDate.equals(datePicker.getValue()))
            datePicker.setValue(null);
    });

    BooleanBinding datePickerIsInvalid = or(
            datePicker.valueProperty().isNull(),

            createBooleanBinding(() ->
                    datePicker.getValue().isAfter(LocalDate.now())
            , /* depends on */ datePicker.valueProperty())
    );

    // Don't let the user click restore if the words area contains the current wallet words, or are an invalid set,
    // or if the date field isn't set, or if it's in the future.
    restoreButton.disableProperty().bind(
            or(
                    or(
                            not(validator.valid),
                            equal(origWords, wordsArea.textProperty())
                    ),

                    datePickerIsInvalid
            )
    );

    // Highlight the date picker in red if it's empty or in the future, so the user knows why restore is disabled.
    datePickerIsInvalid.addListener((dp, old, cur) -> {
        if (cur) {
            datePicker.getStyleClass().add("validation_error");
        } else {
            datePicker.getStyleClass().remove("validation_error");
        }
    });
}
 
Example #5
Source File: WalletSettingsController.java    From thunder with GNU Affero General Public License v3.0 4 votes vote down vote up
public void initialize (@Nullable KeyParameter aesKey) {
    DeterministicSeed seed = Main.wallet.getKeyChainSeed();
    if (aesKey == null) {
        if (seed.isEncrypted()) {
            log.info("Wallet is encrypted, requesting password first.");
            // Delay execution of this until after we've finished initialising this screen.
            Platform.runLater(() -> askForPasswordAndRetry());
            return;
        }
    } else {
        this.aesKey = aesKey;
        seed = seed.decrypt(checkNotNull(Main.wallet.getKeyCrypter()), "", aesKey);
        // Now we can display the wallet seed as appropriate.
        passwordButton.setText("Remove password");
    }

    // Set the date picker to show the birthday of this wallet.
    Instant creationTime = Instant.ofEpochSecond(seed.getCreationTimeSeconds());
    LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
    datePicker.setValue(origDate);

    // Set the mnemonic seed words.
    final List<String> mnemonicCode = seed.getMnemonicCode();
    checkNotNull(mnemonicCode);    // Already checked for encryption.
    String origWords = Utils.join(mnemonicCode);
    wordsArea.setText(origWords);

    // Validate words as they are being typed.
    MnemonicCode codec = unchecked(MnemonicCode::new);
    TextFieldValidator validator = new TextFieldValidator(wordsArea, text ->
            !didThrow(() -> codec.check(Splitter.on(' ').splitToList(text)))
    );

    // Clear the date picker if the user starts editing the words, if it contained the current wallets date.
    // This forces them to set the birthday field when restoring.
    wordsArea.textProperty().addListener(o -> {
        if (origDate.equals(datePicker.getValue())) {
            datePicker.setValue(null);
        }
    });

    BooleanBinding datePickerIsInvalid = or(
            datePicker.valueProperty().isNull(),

            createBooleanBinding(() ->
                            datePicker.getValue().isAfter(LocalDate.now())
                    , /* depends on */ datePicker.valueProperty())
    );

    // Don't let the user click restore if the words area contains the current wallet words, or are an invalid set,
    // or if the date field isn't set, or if it's in the future.
    restoreButton.disableProperty().bind(
            or(
                    or(
                            not(validator.valid),
                            equal(origWords, wordsArea.textProperty())
                    ),

                    datePickerIsInvalid
            )
    );

    // Highlight the date picker in red if it's empty or in the future, so the user knows why restore is disabled.
    datePickerIsInvalid.addListener((dp, old, cur) -> {
        if (cur) {
            datePicker.getStyleClass().add("validation_error");
        } else {
            datePicker.getStyleClass().remove("validation_error");
        }
    });
}
 
Example #6
Source File: WalletSettingsController.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public void initialize(@Nullable KeyParameter aesKey) {
    DeterministicSeed seed = Main.bitcoin.wallet().getKeyChainSeed();
    if (aesKey == null) {
        if (seed.isEncrypted()) {
            log.info("Wallet is encrypted, requesting password first.");
            // Delay execution of this until after we've finished initialising this screen.
            Platform.runLater(() -> askForPasswordAndRetry());
            return;
        }
    } else {
        this.aesKey = aesKey;
        seed = seed.decrypt(checkNotNull(Main.bitcoin.wallet().getKeyCrypter()), "", aesKey);
        // Now we can display the wallet seed as appropriate.
        passwordButton.setText("Remove password");
    }

    // Set the date picker to show the birthday of this wallet.
    Instant creationTime = Instant.ofEpochSecond(seed.getCreationTimeSeconds());
    LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
    datePicker.setValue(origDate);

    // Set the mnemonic seed words.
    final List<String> mnemonicCode = seed.getMnemonicCode();
    checkNotNull(mnemonicCode);    // Already checked for encryption.
    String origWords = Utils.join(mnemonicCode);
    wordsArea.setText(origWords);

    // Validate words as they are being typed.
    MnemonicCode codec = unchecked(MnemonicCode::new);
    TextFieldValidator validator = new TextFieldValidator(wordsArea, text ->
        !didThrow(() -> codec.check(Splitter.on(' ').splitToList(text)))
    );

    // Clear the date picker if the user starts editing the words, if it contained the current wallets date.
    // This forces them to set the birthday field when restoring.
    wordsArea.textProperty().addListener(o -> {
        if (origDate.equals(datePicker.getValue()))
            datePicker.setValue(null);
    });

    BooleanBinding datePickerIsInvalid = or(
            datePicker.valueProperty().isNull(),

            createBooleanBinding(() ->
                    datePicker.getValue().isAfter(LocalDate.now())
            , /* depends on */ datePicker.valueProperty())
    );

    // Don't let the user click restore if the words area contains the current wallet words, or are an invalid set,
    // or if the date field isn't set, or if it's in the future.
    restoreButton.disableProperty().bind(
            or(
                    or(
                            not(validator.valid),
                            equal(origWords, wordsArea.textProperty())
                    ),

                    datePickerIsInvalid
            )
    );

    // Highlight the date picker in red if it's empty or in the future, so the user knows why restore is disabled.
    datePickerIsInvalid.addListener((dp, old, cur) -> {
        if (cur) {
            datePicker.getStyleClass().add("validation_error");
        } else {
            datePicker.getStyleClass().remove("validation_error");
        }
    });
}
 
Example #7
Source File: WalletSettingsController.java    From devcoretalk with GNU General Public License v2.0 4 votes vote down vote up
public void initialize(@Nullable KeyParameter aesKey) {
    DeterministicSeed seed = Main.bitcoin.wallet().getKeyChainSeed();
    if (aesKey == null) {
        if (seed.isEncrypted()) {
            log.info("Wallet is encrypted, requesting password first.");
            // Delay execution of this until after we've finished initialising this screen.
            Platform.runLater(() -> askForPasswordAndRetry());
            return;
        }
    } else {
        this.aesKey = aesKey;
        seed = seed.decrypt(checkNotNull(Main.bitcoin.wallet().getKeyCrypter()), "", aesKey);
        // Now we can display the wallet seed as appropriate.
        passwordButton.setText("Remove password");
    }

    // Set the date picker to show the birthday of this wallet.
    Instant creationTime = Instant.ofEpochSecond(seed.getCreationTimeSeconds());
    LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
    datePicker.setValue(origDate);

    // Set the mnemonic seed words.
    final List<String> mnemonicCode = seed.getMnemonicCode();
    checkNotNull(mnemonicCode);    // Already checked for encryption.
    String origWords = Joiner.on(" ").join(mnemonicCode);
    wordsArea.setText(origWords);

    // Validate words as they are being typed.
    MnemonicCode codec = unchecked(MnemonicCode::new);
    TextFieldValidator validator = new TextFieldValidator(wordsArea, text ->
        !didThrow(() -> codec.check(Splitter.on(' ').splitToList(text)))
    );

    // Clear the date picker if the user starts editing the words, if it contained the current wallets date.
    // This forces them to set the birthday field when restoring.
    wordsArea.textProperty().addListener(o -> {
        if (origDate.equals(datePicker.getValue()))
            datePicker.setValue(null);
    });

    BooleanBinding datePickerIsInvalid = or(
            datePicker.valueProperty().isNull(),

            createBooleanBinding(() ->
                    datePicker.getValue().isAfter(LocalDate.now())
            , /* depends on */ datePicker.valueProperty())
    );

    // Don't let the user click restore if the words area contains the current wallet words, or are an invalid set,
    // or if the date field isn't set, or if it's in the future.
    restoreButton.disableProperty().bind(
            or(
                    or(
                            not(validator.valid),
                            equal(origWords, wordsArea.textProperty())
                    ),

                    datePickerIsInvalid
            )
    );

    // Highlight the date picker in red if it's empty or in the future, so the user knows why restore is disabled.
    datePickerIsInvalid.addListener((dp, old, cur) -> {
        if (cur) {
            datePicker.getStyleClass().add("validation_error");
        } else {
            datePicker.getStyleClass().remove("validation_error");
        }
    });
}
 
Example #8
Source File: WalletSettingsController.java    From thundernetwork with GNU Affero General Public License v3.0 4 votes vote down vote up
public void initialize(@Nullable KeyParameter aesKey) {
    DeterministicSeed seed = Main.bitcoin.wallet().getKeyChainSeed();
    if (aesKey == null) {
        if (seed.isEncrypted()) {
            log.info("Wallet is encrypted, requesting password first.");
            // Delay execution of this until after we've finished initialising this screen.
            Platform.runLater(() -> askForPasswordAndRetry());
            return;
        }
    } else {
        this.aesKey = aesKey;
        seed = seed.decrypt(checkNotNull(Main.bitcoin.wallet().getKeyCrypter()), "", aesKey);
        // Now we can display the wallet seed as appropriate.
        passwordButton.setText("Remove password");
    }

    // Set the date picker to show the birthday of this wallet.
    Instant creationTime = Instant.ofEpochSecond(seed.getCreationTimeSeconds());
    LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
    datePicker.setValue(origDate);

    // Set the mnemonic seed words.
    final List<String> mnemonicCode = seed.getMnemonicCode();
    checkNotNull(mnemonicCode);    // Already checked for encryption.
    String origWords = Utils.join(mnemonicCode);
    wordsArea.setText(origWords);

    // Validate words as they are being typed.
    MnemonicCode codec = unchecked(MnemonicCode::new);
    TextFieldValidator validator = new TextFieldValidator(wordsArea, text ->
        !didThrow(() -> codec.check(Splitter.on(' ').splitToList(text)))
    );

    // Clear the date picker if the user starts editing the words, if it contained the current wallets date.
    // This forces them to set the birthday field when restoring.
    wordsArea.textProperty().addListener(o -> {
        if (origDate.equals(datePicker.getValue()))
            datePicker.setValue(null);
    });

    BooleanBinding datePickerIsInvalid = or(
            datePicker.valueProperty().isNull(),

            createBooleanBinding(() ->
                    datePicker.getValue().isAfter(LocalDate.now())
            , /* depends on */ datePicker.valueProperty())
    );

    // Don't let the user click restore if the words area contains the current wallet words, or are an invalid set,
    // or if the date field isn't set, or if it's in the future.
    restoreButton.disableProperty().bind(
            or(
                    or(
                            not(validator.valid),
                            equal(origWords, wordsArea.textProperty())
                    ),

                    datePickerIsInvalid
            )
    );

    // Highlight the date picker in red if it's empty or in the future, so the user knows why restore is disabled.
    datePickerIsInvalid.addListener((dp, old, cur) -> {
        if (cur) {
            datePicker.getStyleClass().add("validation_error");
        } else {
            datePicker.getStyleClass().remove("validation_error");
        }
    });
}
 
Example #9
Source File: WalletPasswordWindow.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
private void showRestoreScreen() {
    Label headLine2Label = new AutoTooltipLabel(Res.get("seed.restore.title"));
    headLine2Label.getStyleClass().add("popup-headline");
    headLine2Label.setMouseTransparent(true);
    GridPane.setHalignment(headLine2Label, HPos.LEFT);
    GridPane.setRowIndex(headLine2Label, ++rowIndex);
    GridPane.setMargin(headLine2Label, new Insets(30, 0, 0, 0));
    gridPane.getChildren().add(headLine2Label);

    seedWordsTextArea = addTextArea(gridPane, ++rowIndex, Res.get("seed.enterSeedWords"), 5);
    ;
    seedWordsTextArea.setPrefHeight(60);

    Tuple2<Label, DatePicker> labelDatePickerTuple2 = addTopLabelDatePicker(gridPane, ++rowIndex,
            Res.get("seed.creationDate"), 10);
    datePicker = labelDatePickerTuple2.second;
    restoreButton = addPrimaryActionButton(gridPane, ++rowIndex, Res.get("seed.restore"), 0);
    restoreButton.setDefaultButton(true);
    stage.setHeight(570);


    // wallet creation date is not encrypted
    LocalDate walletCreationDate = Instant.ofEpochSecond(walletsManager.getChainSeedCreationTimeSeconds()).atZone(ZoneId.systemDefault()).toLocalDate();
    log.info("walletCreationDate " + walletCreationDate);
    datePicker.setValue(walletCreationDate);
    restoreButton.disableProperty().bind(createBooleanBinding(() -> !seedWordsValid.get() || !seedWordsEdited.get(),
            seedWordsValid, seedWordsEdited));

    seedWordsValidChangeListener = (observable, oldValue, newValue) -> {
        if (newValue) {
            seedWordsTextArea.getStyleClass().remove("validation-error");
        } else {
            seedWordsTextArea.getStyleClass().add("validation-error");
        }
    };

    wordsTextAreaChangeListener = (observable, oldValue, newValue) -> {
        seedWordsEdited.set(true);
        try {
            MnemonicCode codec = new MnemonicCode();
            codec.check(Splitter.on(" ").splitToList(newValue));
            seedWordsValid.set(true);
        } catch (IOException | MnemonicException e) {
            seedWordsValid.set(false);
        }
    };

    seedWordsValid.addListener(seedWordsValidChangeListener);
    seedWordsTextArea.textProperty().addListener(wordsTextAreaChangeListener);
    restoreButton.disableProperty().bind(createBooleanBinding(() -> !seedWordsValid.get() || !seedWordsEdited.get(),
            seedWordsValid, seedWordsEdited));

    restoreButton.setOnAction(e -> onRestore());

    seedWordsTextArea.getStyleClass().remove("validation-error");
    datePicker.getStyleClass().remove("validation-error");

    layout();
}
 
Example #10
Source File: SeedWordsView.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void initialize() {
    addTitledGroupBg(root, gridRow, 2, Res.get("account.seed.backup.title"));
    displaySeedWordsTextArea = addTopLabelTextArea(root, gridRow, Res.get("seed.seedWords"), "", Layout.FIRST_ROW_DISTANCE).second;
    displaySeedWordsTextArea.getStyleClass().add("wallet-seed-words");
    displaySeedWordsTextArea.setPrefHeight(40);
    displaySeedWordsTextArea.setMaxHeight(40);
    displaySeedWordsTextArea.setEditable(false);

    datePicker = addTopLabelDatePicker(root, ++gridRow, Res.get("seed.date"), 10).second;
    datePicker.setMouseTransparent(true);

    addTitledGroupBg(root, ++gridRow, 3, Res.get("seed.restore.title"), Layout.GROUP_DISTANCE);
    seedWordsTextArea = addTopLabelTextArea(root, gridRow, Res.get("seed.seedWords"), "", Layout.FIRST_ROW_AND_GROUP_DISTANCE).second;
    seedWordsTextArea.getStyleClass().add("wallet-seed-words");
    seedWordsTextArea.setPrefHeight(40);
    seedWordsTextArea.setMaxHeight(40);

    restoreDatePicker = addTopLabelDatePicker(root, ++gridRow, Res.get("seed.date"), 10).second;
    restoreButton = addPrimaryActionButtonAFterGroup(root, ++gridRow, Res.get("seed.restore"));

    addTitledGroupBg(root, ++gridRow, 1, Res.get("shared.information"), Layout.GROUP_DISTANCE);
    addMultilineLabel(root, gridRow, Res.get("account.seed.info"),
            Layout.FIRST_ROW_AND_GROUP_DISTANCE);

    seedWordsValidChangeListener = (observable, oldValue, newValue) -> {
        if (newValue) {
            seedWordsTextArea.getStyleClass().remove("validation-error");
        } else {
            seedWordsTextArea.getStyleClass().add("validation-error");
        }
    };

    seedWordsTextAreaChangeListener = (observable, oldValue, newValue) -> {
        seedWordsEdited.set(true);
        try {
            MnemonicCode codec = new MnemonicCode();
            codec.check(Splitter.on(" ").splitToList(newValue));
            seedWordsValid.set(true);
        } catch (IOException | MnemonicException e) {
            seedWordsValid.set(false);
        }
    };
}