Skip to content

Commit

Permalink
Make account chooser auto-select existing keys in Key Ring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Nov 18, 2024
1 parent b8a7968 commit 523d1fb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
11 changes: 11 additions & 0 deletions convex-core/src/main/java/convex/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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: "));
Expand Down Expand Up @@ -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());
}
Expand Down
6 changes: 4 additions & 2 deletions convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -307,15 +308,16 @@ 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);
derivationArea.setWrapStyleWord(false);
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();
Expand Down
11 changes: 10 additions & 1 deletion convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}

0 comments on commit 523d1fb

Please sign in to comment.