Skip to content

Commit

Permalink
Merge pull request #115 from orogvany/update_wallet_import
Browse files Browse the repository at this point in the history
GUI: Wallet import/recover of HD keys does not replace existing seed
  • Loading branch information
orogvany authored Dec 18, 2018
2 parents 6283cc4 + 663c914 commit 885dbed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/main/java/org/semux/gui/dialog/RecoverHdWalletDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import com.github.orogvany.bip39.Language;
import com.github.orogvany.bip39.MnemonicGenerator;
import org.semux.core.Wallet;
import org.semux.crypto.Key;
import org.semux.gui.Action;
import org.semux.gui.SemuxGui;
import org.semux.gui.SwingUtil;
import org.semux.message.GuiMessages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.GroupLayout;
import javax.swing.JButton;
Expand All @@ -25,9 +28,12 @@
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class RecoverHdWalletDialog extends JDialog implements ActionListener {
private final transient SemuxGui gui;
private static final Logger logger = LoggerFactory.getLogger(RecoverHdWalletDialog.class);

private final JTextArea phraseField;
private final JPasswordField passwordField;
Expand Down Expand Up @@ -105,8 +111,6 @@ public void actionPerformed(ActionEvent e) {
String phrase = phraseField.getText();
String password = new String(passwordField.getPassword());

Wallet wallet = gui.getKernel().getWallet();

MnemonicGenerator generator = new MnemonicGenerator();
byte[] seed;
try {
Expand All @@ -116,9 +120,31 @@ public void actionPerformed(ActionEvent e) {
break;
}

wallet.setHdSeed(seed);
int found = wallet.scanForHdKeys(gui.getKernel().getBlockchain().getAccountState());
wallet.flush();
Wallet wallet = gui.getKernel().getWallet();
int found = 0;
try {
Wallet importedWallet = new Wallet(File.createTempFile("wallet", ".data"),
wallet.getNetwork());
importedWallet.setHdSeed(seed);
found = importedWallet.scanForHdKeys(gui.getKernel().getBlockchain().getAccountState());

if (found > 0) {
wallet.addAccounts(importedWallet.getAccounts());

for (Key key : importedWallet.getAccounts()) {
wallet.addAccount(key);
if (!wallet.getAddressAlias(key.toAddress()).isPresent()) {
wallet.setAddressAlias(key.toAddress(),
GuiMessages.get("Imported") + importedWallet.getAddressAlias(key.toAddress()));
}
}
wallet.flush();
}

} catch (IOException e1) {
logger.error(e1.getMessage(), e);
}

JOptionPane.showMessageDialog(this, GuiMessages.get("ImportSuccess", found));
this.dispose();
break;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/org/semux/gui/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ RenameAccount = Rename Account
AccountDeleted = Account deleted!
ConfirmDeleteAccount = Do you want to delete this account?
ComputerNotQualified = Your computer is not qualified for running a validator node, continue?
Imported = Imported:

# HD wallet
WalletRecoveryPhrase = Wallet recovery phrase
Expand Down

0 comments on commit 885dbed

Please sign in to comment.