From 523d1fb66db4f175dcc0759fc75fdbc74069a9e0 Mon Sep 17 00:00:00 2001 From: mikera Date: Mon, 18 Nov 2024 11:01:15 +0000 Subject: [PATCH] Make account chooser auto-select existing keys in Key Ring --- .../src/main/java/convex/core/Constants.java | 11 +++ .../account/AccountChooserPanel.java | 71 ++++++++++++------- .../java/convex/gui/keys/KeyGenPanel.java | 6 +- .../java/convex/gui/wallet/SettingsPanel.java | 11 ++- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/convex-core/src/main/java/convex/core/Constants.java b/convex-core/src/main/java/convex/core/Constants.java index 9d51e8142..c99b824d6 100644 --- a/convex-core/src/main/java/convex/core/Constants.java +++ b/convex-core/src/main/java/convex/core/Constants.java @@ -171,5 +171,16 @@ public class Constants { */ public static final int LOOKUP_DEPTH = 16; + /** + * SLIP-44 Chain code for Convex CVM + * + * 8 = maturity, mastery, power, and ambition + * 7 = knowledge and wisdom through observation, analysis, and awareness + * 6 = harmony, compassion, and completeness + * + * Hex is 36c i.e. 36c = 3 * 16^2 + 6 * 16^1 + 12 * 16^0 + */ + public static final int CHAIN_CODE = 876; + } diff --git a/convex-gui/src/main/java/convex/gui/components/account/AccountChooserPanel.java b/convex-gui/src/main/java/convex/gui/components/account/AccountChooserPanel.java index c30ba8f8d..0b4d01181 100644 --- a/convex-gui/src/main/java/convex/gui/components/account/AccountChooserPanel.java +++ b/convex-gui/src/main/java/convex/gui/components/account/AccountChooserPanel.java @@ -13,10 +13,13 @@ import convex.core.crypto.AKeyPair; import convex.core.crypto.wallet.AWalletEntry; import convex.core.cvm.ops.Special; +import convex.core.data.AccountKey; import convex.core.data.Address; +import convex.core.data.Symbols; import convex.core.exceptions.ResultException; import convex.gui.components.BalanceLabel; import convex.gui.components.DropdownMenu; +import convex.gui.keys.KeyRingPanel; import convex.gui.utils.Toolkit; import net.miginfocom.swing.MigLayout; @@ -50,6 +53,9 @@ public AccountChooserPanel(Convex convex) { Address address=convex.getAddress(); addressCombo = new AddressCombo(address); addressCombo.setToolTipText("Select Account for use"); + addressCombo.addItemListener(e -> { + updateAddress(addressCombo.getAddress()); + }); mp.add(addressCombo); keyCombo=KeyPairCombo.forConvex(convex); @@ -61,34 +67,10 @@ public AccountChooserPanel(Convex convex) { return; }; AWalletEntry we=(AWalletEntry)e.getItem(); - if (we==null) { - convex.setKeyPair(null); - } else { - AKeyPair kp; - if (we.isLocked()) { - String s=JOptionPane.showInputDialog(AccountChooserPanel.this,"Enter password to unlock wallet:\n"+we.getPublicKey()); - if (s==null) { - return; - } - char[] pass=s.toCharArray(); - boolean unlock=we.tryUnlock(s.toCharArray()); - if (!unlock) { - return; - } - kp=we.getKeyPair(); - we.lock(pass); - } else { - kp=we.getKeyPair(); - } - convex.setKeyPair(kp); - } + setKeyPair(we); }); mp.add(keyCombo); - - addressCombo.addItemListener(e -> { - updateAddress(addressCombo.getAddress()); - }); // Balance Info mp.add(new JLabel("Balance: ")); @@ -165,10 +147,49 @@ public Address getAddress() { public void updateAddress(Address a) { convex.setAddress(a); + convex.query(Special.forSymbol(Symbols.STAR_KEY)).thenAcceptAsync(r-> { + if (r.isError()) { + // ignore? + System.err.println("Account key query failed: "+r); + setKeyPair(null); + } else { + AccountKey ak=AccountKey.parse(r.getValue()); // might be null, will clear key + AWalletEntry we=KeyRingPanel.getKeyRingEntry(ak); + setKeyPair(we); + } + }); updateBalance(); } + public void setKeyPair(AWalletEntry we) { + if (we==keyCombo.getSelectedItem()) return; // no change + + if (we==null) { + convex.setKeyPair(null); + } else { + AKeyPair kp; + if (we.isLocked()) { + String s=JOptionPane.showInputDialog(AccountChooserPanel.this,"Enter password to unlock wallet:\n"+we.getPublicKey()); + if (s==null) { + convex.setKeyPair(null); + return; + } + char[] pass=s.toCharArray(); + boolean unlock=we.tryUnlock(s.toCharArray()); + if (!unlock) { + return; + } + kp=we.getKeyPair(); + we.lock(pass); + } else { + kp=we.getKeyPair(); + } + convex.setKeyPair(kp); + } + keyCombo.setSelectedItem(we); + } + public void updateBalance() { updateBalance(getAddress()); } diff --git a/convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java b/convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java index 27042955b..a71aeb643 100644 --- a/convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java +++ b/convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java @@ -14,6 +14,7 @@ import javax.swing.JTextArea; import javax.swing.SpinnerNumberModel; +import convex.core.Constants; import convex.core.crypto.AKeyPair; import convex.core.crypto.BIP39; import convex.core.crypto.Passwords; @@ -307,7 +308,8 @@ public KeyGenPanel(PeerGUI manager) { } { - addLabel("BIP32 Path","This is the hierarchical path for key generation as defined in BIP32. 'm' just specifies the master key."); + int CC = Constants.CHAIN_CODE; + addLabel("BIP32 Path","This is the hierarchical path for key generation as defined in BIP32. 'm' specifies the master key. "+CC+" is the Convex SLIP-44 chain code."); derivationArea = makeTextArea(); derivationArea.setLineWrap(true); @@ -315,7 +317,7 @@ public KeyGenPanel(PeerGUI manager) { derivationArea.setBackground(Color.BLACK); formPanel.add(derivationArea,TEXTAREA_CONSTRAINT); - derivationArea.setText("m"); + derivationArea.setText("m/"+CC+"/0/0/0"); derivationArea.getDocument().addDocumentListener(Toolkit.createDocumentListener(() -> { if (!derivationArea.isFocusOwner()) return; updatePath(); diff --git a/convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java b/convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java index d01251208..527bd5549 100644 --- a/convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java +++ b/convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java @@ -3,6 +3,8 @@ import javax.swing.JPanel; import convex.api.Convex; +import convex.gui.client.ConvexClient; +import convex.gui.components.ActionButton; import convex.gui.components.account.AccountChooserPanel; import convex.gui.utils.Toolkit; import net.miginfocom.swing.MigLayout; @@ -18,6 +20,13 @@ public SettingsPanel(Convex convex) { this.setLayout(new MigLayout("wrap 1","[grow]")); AccountChooserPanel chooser=new AccountChooserPanel(convex); - add(Toolkit.withTitledBorder("Account Selection", chooser),"dock north"); + add(Toolkit.withTitledBorder("Account Selection", chooser)); + + ActionButton replButton=new ActionButton("Terminal REPL", 0xeb8e, e->{ + new ConvexClient(convex).run(); + }); + JPanel toolPanel=new JPanel(); + toolPanel.add(replButton); + add(Toolkit.withTitledBorder("Developer Tools", toolPanel)); } }