Skip to content

Commit 523d1fb

Browse files
committed
Make account chooser auto-select existing keys in Key Ring
1 parent b8a7968 commit 523d1fb

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

convex-core/src/main/java/convex/core/Constants.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,16 @@ public class Constants {
171171
*/
172172
public static final int LOOKUP_DEPTH = 16;
173173

174+
/**
175+
* SLIP-44 Chain code for Convex CVM
176+
*
177+
* 8 = maturity, mastery, power, and ambition
178+
* 7 = knowledge and wisdom through observation, analysis, and awareness
179+
* 6 = harmony, compassion, and completeness
180+
*
181+
* Hex is 36c i.e. 36c = 3 * 16^2 + 6 * 16^1 + 12 * 16^0
182+
*/
183+
public static final int CHAIN_CODE = 876;
184+
174185

175186
}

convex-gui/src/main/java/convex/gui/components/account/AccountChooserPanel.java

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
import convex.core.crypto.AKeyPair;
1414
import convex.core.crypto.wallet.AWalletEntry;
1515
import convex.core.cvm.ops.Special;
16+
import convex.core.data.AccountKey;
1617
import convex.core.data.Address;
18+
import convex.core.data.Symbols;
1719
import convex.core.exceptions.ResultException;
1820
import convex.gui.components.BalanceLabel;
1921
import convex.gui.components.DropdownMenu;
22+
import convex.gui.keys.KeyRingPanel;
2023
import convex.gui.utils.Toolkit;
2124
import net.miginfocom.swing.MigLayout;
2225

@@ -50,6 +53,9 @@ public AccountChooserPanel(Convex convex) {
5053
Address address=convex.getAddress();
5154
addressCombo = new AddressCombo(address);
5255
addressCombo.setToolTipText("Select Account for use");
56+
addressCombo.addItemListener(e -> {
57+
updateAddress(addressCombo.getAddress());
58+
});
5359
mp.add(addressCombo);
5460

5561
keyCombo=KeyPairCombo.forConvex(convex);
@@ -61,34 +67,10 @@ public AccountChooserPanel(Convex convex) {
6167
return;
6268
};
6369
AWalletEntry we=(AWalletEntry)e.getItem();
64-
if (we==null) {
65-
convex.setKeyPair(null);
66-
} else {
67-
AKeyPair kp;
68-
if (we.isLocked()) {
69-
String s=JOptionPane.showInputDialog(AccountChooserPanel.this,"Enter password to unlock wallet:\n"+we.getPublicKey());
70-
if (s==null) {
71-
return;
72-
}
73-
char[] pass=s.toCharArray();
74-
boolean unlock=we.tryUnlock(s.toCharArray());
75-
if (!unlock) {
76-
return;
77-
}
78-
kp=we.getKeyPair();
79-
we.lock(pass);
80-
} else {
81-
kp=we.getKeyPair();
82-
}
83-
convex.setKeyPair(kp);
84-
}
70+
setKeyPair(we);
8571
});
8672
mp.add(keyCombo);
8773

88-
89-
addressCombo.addItemListener(e -> {
90-
updateAddress(addressCombo.getAddress());
91-
});
9274

9375
// Balance Info
9476
mp.add(new JLabel("Balance: "));
@@ -165,10 +147,49 @@ public Address getAddress() {
165147

166148
public void updateAddress(Address a) {
167149
convex.setAddress(a);
150+
convex.query(Special.forSymbol(Symbols.STAR_KEY)).thenAcceptAsync(r-> {
151+
if (r.isError()) {
152+
// ignore?
153+
System.err.println("Account key query failed: "+r);
154+
setKeyPair(null);
155+
} else {
156+
AccountKey ak=AccountKey.parse(r.getValue()); // might be null, will clear key
157+
AWalletEntry we=KeyRingPanel.getKeyRingEntry(ak);
158+
setKeyPair(we);
159+
}
160+
});
168161

169162
updateBalance();
170163
}
171164

165+
public void setKeyPair(AWalletEntry we) {
166+
if (we==keyCombo.getSelectedItem()) return; // no change
167+
168+
if (we==null) {
169+
convex.setKeyPair(null);
170+
} else {
171+
AKeyPair kp;
172+
if (we.isLocked()) {
173+
String s=JOptionPane.showInputDialog(AccountChooserPanel.this,"Enter password to unlock wallet:\n"+we.getPublicKey());
174+
if (s==null) {
175+
convex.setKeyPair(null);
176+
return;
177+
}
178+
char[] pass=s.toCharArray();
179+
boolean unlock=we.tryUnlock(s.toCharArray());
180+
if (!unlock) {
181+
return;
182+
}
183+
kp=we.getKeyPair();
184+
we.lock(pass);
185+
} else {
186+
kp=we.getKeyPair();
187+
}
188+
convex.setKeyPair(kp);
189+
}
190+
keyCombo.setSelectedItem(we);
191+
}
192+
172193
public void updateBalance() {
173194
updateBalance(getAddress());
174195
}

convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.swing.JTextArea;
1515
import javax.swing.SpinnerNumberModel;
1616

17+
import convex.core.Constants;
1718
import convex.core.crypto.AKeyPair;
1819
import convex.core.crypto.BIP39;
1920
import convex.core.crypto.Passwords;
@@ -307,15 +308,16 @@ public KeyGenPanel(PeerGUI manager) {
307308
}
308309

309310
{
310-
addLabel("BIP32 Path","This is the hierarchical path for key generation as defined in BIP32. 'm' just specifies the master key.");
311+
int CC = Constants.CHAIN_CODE;
312+
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.");
311313
derivationArea = makeTextArea();
312314

313315
derivationArea.setLineWrap(true);
314316
derivationArea.setWrapStyleWord(false);
315317
derivationArea.setBackground(Color.BLACK);
316318

317319
formPanel.add(derivationArea,TEXTAREA_CONSTRAINT);
318-
derivationArea.setText("m");
320+
derivationArea.setText("m/"+CC+"/0/0/0");
319321
derivationArea.getDocument().addDocumentListener(Toolkit.createDocumentListener(() -> {
320322
if (!derivationArea.isFocusOwner()) return;
321323
updatePath();

convex-gui/src/main/java/convex/gui/wallet/SettingsPanel.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.swing.JPanel;
44

55
import convex.api.Convex;
6+
import convex.gui.client.ConvexClient;
7+
import convex.gui.components.ActionButton;
68
import convex.gui.components.account.AccountChooserPanel;
79
import convex.gui.utils.Toolkit;
810
import net.miginfocom.swing.MigLayout;
@@ -18,6 +20,13 @@ public SettingsPanel(Convex convex) {
1820
this.setLayout(new MigLayout("wrap 1","[grow]"));
1921

2022
AccountChooserPanel chooser=new AccountChooserPanel(convex);
21-
add(Toolkit.withTitledBorder("Account Selection", chooser),"dock north");
23+
add(Toolkit.withTitledBorder("Account Selection", chooser));
24+
25+
ActionButton replButton=new ActionButton("Terminal REPL", 0xeb8e, e->{
26+
new ConvexClient(convex).run();
27+
});
28+
JPanel toolPanel=new JPanel();
29+
toolPanel.add(replButton);
30+
add(Toolkit.withTitledBorder("Developer Tools", toolPanel));
2231
}
2332
}

0 commit comments

Comments
 (0)