From 033f1cd46f934ce0e755d0855ec01ce0236e11a7 Mon Sep 17 00:00:00 2001 From: jjos2372 Date: Thu, 15 Apr 2021 11:10:16 -0300 Subject: [PATCH] Add BNB for an existing account. --- src/main/java/btdex/core/Globals.java | 23 ++++++++++++ src/main/java/btdex/ui/ExplorerButton.java | 9 +++++ src/main/java/btdex/ui/Main.java | 36 ++++++++++++++++++- .../resources/locale/i18n.btdex.properties | 1 + 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/java/btdex/core/Globals.java b/src/main/java/btdex/core/Globals.java index aae358bb..893c3245 100644 --- a/src/main/java/btdex/core/Globals.java +++ b/src/main/java/btdex/core/Globals.java @@ -221,6 +221,29 @@ public void setKeys(byte []pubKey, byte []privKey, char []pin) throws Exception bnbAddress = bnbWallet.getAddress(); logger.debug("Keys set. for address {}", bnbAddress); } + + public boolean addBnb(char []pin) { + boolean result = false; + try { + byte[] pinKey = BC.getSha256().digest(new String(pin).getBytes("UTF-8")); + String encPrivKey = conf.getProperty(Constants.PROP_ENC_PRIVKEY); + byte []privKey = BC.aesDecrypt(BC.parseHexString(encPrivKey), pinKey); + String pubKey = BC.toHexString(BC.getPublicKey(privKey)); + result = pubKey.equals(conf.getProperty(Constants.PROP_PUBKEY)); + logger.debug("PIN checked, result: {}", result); + + if(result) { + Wallet bnbWallet = new Wallet(BC.toHexString(privKey), isTestnet() ? + BinanceDexEnvironment.TEST_NET : BinanceDexEnvironment.PROD); + conf.setProperty(Constants.PROP_ECKEY_PUB, bnbWallet.getEcKey().getPublicKeyAsHex()); + bnbAddress = bnbWallet.getAddress(); + logger.debug("Keys set. for address {}", bnbAddress); + } + } catch (Exception e) { + logger.error("Error: {}", e.getLocalizedMessage()); + } + return result; + } /** * @param pin diff --git a/src/main/java/btdex/ui/ExplorerButton.java b/src/main/java/btdex/ui/ExplorerButton.java index 0c59f355..11bfcab9 100644 --- a/src/main/java/btdex/ui/ExplorerButton.java +++ b/src/main/java/btdex/ui/ExplorerButton.java @@ -44,6 +44,10 @@ public void setBinance(boolean b) { this.isBinance = b; } + public String getId() { + return id; + } + public ExplorerButton(String text, Icon icon, Icon icon2, int type, String id, String addressRS, String tooltipText) { super(new BorderLayout(0, 0)); @@ -63,6 +67,8 @@ public ExplorerButton(String text, Icon icon, Icon icon2, int type, String id, S @Override public void actionPerformed(ActionEvent e) { String t = ExplorerButton.this.type == TYPE_ADDRESS ? ExplorerButton.this.addressRS : ExplorerButton.this.id; + if(t == null || t.length() == 0) + return; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection stringSelection = new StringSelection(t); @@ -75,6 +81,9 @@ public void actionPerformed(ActionEvent e) { explorerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(ExplorerButton.this.id == null || ExplorerButton.this.id.length() == 0) + return; + ExplorerWrapper exp = Main.getInstance().getExplorer(); if(isBinance) exp = ExplorerWrapper.binanceExplorer(); diff --git a/src/main/java/btdex/ui/Main.java b/src/main/java/btdex/ui/Main.java index 26480f88..9cb9fa4e 100644 --- a/src/main/java/btdex/ui/Main.java +++ b/src/main/java/btdex/ui/Main.java @@ -38,6 +38,7 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JPasswordField; import javax.swing.JPopupMenu; import javax.swing.JTabbedPane; import javax.swing.SwingConstants; @@ -100,7 +101,6 @@ public class Main extends JFrame implements ActionListener { private JButton nodeSelector, explorerSelector; private ExplorerWrapper explorer; - private ExplorerWrapper bnbExplorer; private ExplorerButton copyAddressButton; private ExplorerButton copyAddressButtonBinance; @@ -342,8 +342,42 @@ public void stateChanged(ChangeEvent evt) { String bnbAddress = g.getBinanceAddress(); copyAddressButtonBinance.setAddress(bnbAddress, bnbAddress); + copyAddressButtonBinance.getMainButton().addActionListener(e -> { + if(copyAddressButtonBinance.getId() == null || copyAddressButtonBinance.getId().length() == 0) { + // not yet available, lets add + JPanel panel = new JPanel(); + JLabel label = new JLabel(tr("dlg_pin")); + JPasswordField pass = new JPasswordField(10); + panel.add(label); + panel.add(pass); + String[] options = new String[]{tr("dlg_ok"), tr("dlg_cancel")}; + int option = JOptionPane.showOptionDialog(null, panel, tr("dlg_add_binance"), + JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, + null, options, options[1]); + if(option == 0) { + // pressing OK button + char[] pin = pass.getPassword(); + if(g.addBnb(pin)) { + String newBnbAddress = g.getBinanceAddress(); + copyAddressButtonBinance.setAddress(newBnbAddress, newBnbAddress); + newBnbAddress = newBnbAddress.substring(0, 5) + "..." + newBnbAddress.substring(newBnbAddress.length()-5); + copyAddressButtonBinance.getMainButton().setText(newBnbAddress); + try { + g.saveConfs(); + } catch (Exception e1) { + Toast.makeText(Main.this, e1.getLocalizedMessage(), Toast.Style.ERROR).display(); + } + } + else { + Toast.makeText(Main.this, tr("dlg_invalid_pin"), Toast.Style.ERROR).display(); + } + } + } + }); if(bnbAddress!=null) bnbAddress = bnbAddress.substring(0, 5) + "..." + bnbAddress.substring(bnbAddress.length()-5); + else + bnbAddress = tr("acc_add_button"); copyAddressButtonBinance.getMainButton().setText(bnbAddress); // Fire the node updating thread BinanceNode.getInstance(); diff --git a/src/main/resources/locale/i18n.btdex.properties b/src/main/resources/locale/i18n.btdex.properties index cd62673e..891f7bdc 100644 --- a/src/main/resources/locale/i18n.btdex.properties +++ b/src/main/resources/locale/i18n.btdex.properties @@ -112,6 +112,7 @@ dlg_support = Support dlg_support_reddit = Ask for support on Reddit dlg_support_discord = Ask for support on Discord dlg_not_enough_balance = Not enough balance +dlg_add_binance = Add Binance Chain btn_copy_to_clipboard = Copy to clipboard btn_show_qr = Show as a QR code...