/* * * Copyright 2014 http://Bither.net * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * / */ package net.bither.viewsystem.dialogs; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.Spacer; import net.bither.bitherj.crypto.SecureCharSequence; import net.bither.bitherj.crypto.bip38.Bip38; import net.bither.bitherj.exception.AddressFormatException; import net.bither.bitherj.factory.ImportPrivateKey; import net.bither.factory.ImportPrivateKeyDesktop; import net.bither.utils.LocaliserUtils; import net.bither.viewsystem.base.Buttons; import net.bither.viewsystem.froms.PasswordPanel; import net.bither.viewsystem.listener.ICheckPasswordListener; import net.bither.viewsystem.listener.IDialogPasswordListener; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ResourceBundle; public class ImportBIP38PrivateTextDialog extends BitherDialog { private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; private JTextField tfBip38Key; private JLabel labError; private JLabel labBip38; private String bip38DecodeString; public ImportBIP38PrivateTextDialog() { showMsg(""); setContentPane(contentPane); setModal(true); initDialog(); buttonOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onOK(); } }); Buttons.modifCanelButton(buttonCancel); Buttons.modifOkButton(buttonOK); buttonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onCancel(); } }); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { onCancel(); } }); contentPane.registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { onCancel(); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); tfBip38Key.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); labError.setText(""); } }); } private void onOK() { importBip38Text(); } private void onCancel() { // add your code here if necessary dispose(); } private void importBip38Text() { final String result = tfBip38Key.getText().trim(); boolean isBIP38Key = false; try { isBIP38Key = Bip38.isBip38PrivateKey(result); } catch (AddressFormatException e) { e.printStackTrace(); } if (isBIP38Key) { dispose(); PasswordPanel dialogPassword = new PasswordPanel(new ImportPrivateKeyPasswordListenerI(result, true)); dialogPassword.setTitle(LocaliserUtils.getString("enter_bip38_key_password")); dialogPassword.setCheckPre(false); dialogPassword.setCheckPasswordListener(new ICheckPasswordListener() { @Override public boolean checkPassword(SecureCharSequence password) { try { bip38DecodeString = Bip38.decrypt(result, password).toString(); return bip38DecodeString != null; } catch (Exception e) { e.printStackTrace(); return false; } } }); dialogPassword.showPanel(); } else { showMsg(LocaliserUtils.getString("import_bip38_key_text_format_erro")); } } { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< // DO NOT EDIT OR ADD ANY CODE HERE! $$$setupUI$$$(); } /** * Method generated by IntelliJ IDEA GUI Designer * >>> IMPORTANT!! <<< * DO NOT edit this method OR call it in your code! * * @noinspection ALL */ private void $$$setupUI$$$() { contentPane = new JPanel(); contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1)); contentPane.setBackground(new Color(-328966)); final JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1)); panel1.setOpaque(false); contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false)); final Spacer spacer1 = new Spacer(); panel1.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); final JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1, true, false)); panel2.setOpaque(false); panel1.add(panel2, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); buttonOK = new JButton(); this.$$$loadButtonText$$$(buttonOK, ResourceBundle.getBundle("viewer").getString("ok")); panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); buttonCancel = new JButton(); this.$$$loadButtonText$$$(buttonCancel, ResourceBundle.getBundle("viewer").getString("close")); panel2.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); labError = new JLabel(); labError.setText("Label"); panel1.add(labError, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); final JPanel panel3 = new JPanel(); panel3.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1)); panel3.setOpaque(false); contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); tfBip38Key = new JTextField(); panel3.add(tfBip38Key, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false)); final Spacer spacer2 = new Spacer(); panel3.add(spacer2, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); labBip38 = new JLabel(); this.$$$loadLabelText$$$(labBip38, ResourceBundle.getBundle("viewer").getString("import_bip38_key_text_hint")); panel3.add(labBip38, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); } /** * @noinspection ALL */ private void $$$loadLabelText$$$(JLabel component, String text) { StringBuffer result = new StringBuffer(); boolean haveMnemonic = false; char mnemonic = '\0'; int mnemonicIndex = -1; for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '&') { i++; if (i == text.length()) break; if (!haveMnemonic && text.charAt(i) != '&') { haveMnemonic = true; mnemonic = text.charAt(i); mnemonicIndex = result.length(); } } result.append(text.charAt(i)); } component.setText(result.toString()); if (haveMnemonic) { component.setDisplayedMnemonic(mnemonic); component.setDisplayedMnemonicIndex(mnemonicIndex); } } /** * @noinspection ALL */ private void $$$loadButtonText$$$(AbstractButton component, String text) { StringBuffer result = new StringBuffer(); boolean haveMnemonic = false; char mnemonic = '\0'; int mnemonicIndex = -1; for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '&') { i++; if (i == text.length()) break; if (!haveMnemonic && text.charAt(i) != '&') { haveMnemonic = true; mnemonic = text.charAt(i); mnemonicIndex = result.length(); } } result.append(text.charAt(i)); } component.setText(result.toString()); if (haveMnemonic) { component.setMnemonic(mnemonic); component.setDisplayedMnemonicIndex(mnemonicIndex); } } /** * @noinspection ALL */ public JComponent $$$getRootComponent$$$() { return contentPane; } private class ImportPrivateKeyPasswordListenerI implements IDialogPasswordListener { private String content; private boolean isFromBip38; public ImportPrivateKeyPasswordListenerI(String content, boolean isFromBip38) { this.content = content; this.isFromBip38 = isFromBip38; } @Override public void onPasswordEntered(SecureCharSequence password) { if (isFromBip38) { PasswordPanel dialogPassword = new PasswordPanel(walletIDialogPasswordListener); dialogPassword.showPanel(); } else { ImportPrivateKeyDesktop importPrivateKey = new ImportPrivateKeyDesktop( ImportPrivateKey.ImportPrivateKeyType.BitherQrcode, content, password); importPrivateKey.importPrivateKey(); } } } private void showMsg(String str) { labError.setText(str); } private IDialogPasswordListener walletIDialogPasswordListener = new IDialogPasswordListener() { @Override public void onPasswordEntered(SecureCharSequence password) { ImportPrivateKeyDesktop importPrivateKey = new ImportPrivateKeyDesktop( ImportPrivateKey.ImportPrivateKeyType.Bip38, bip38DecodeString, password); importPrivateKey.importPrivateKey(); } }; }