Skip to content

Commit

Permalink
Add BNB for an existing account.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjos2372 committed Apr 15, 2021
1 parent b2f31d1 commit 033f1cd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/main/java/btdex/core/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/btdex/ui/ExplorerButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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();
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/btdex/ui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/locale/i18n.btdex.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down

0 comments on commit 033f1cd

Please sign in to comment.