Skip to content

Commit

Permalink
support restoring wallet from private spend key
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Dec 29, 2021
1 parent 1cdaf20 commit 05b2ab8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion external/monero-cpp
2 changes: 1 addition & 1 deletion src/main/java/monero/wallet/MoneroWalletFull.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static MoneroWalletFull createWallet(MoneroWalletConfig config) {
if (config.getMnemonic() != null) {
if (config.getLanguage() != null) throw new MoneroError("Cannot specify language when creating wallet from mnemonic");
return createWalletFromMnemonic(config.getPath(), config.getPassword(), config.getNetworkType(), config.getMnemonic(), config.getServer(), config.getRestoreHeight(), config.getSeedOffset());
} else if (config.getPrimaryAddress() != null) {
} else if (config.getPrivateSpendKey() != null || config.getPrimaryAddress() != null) {
if (config.getSeedOffset() != null) throw new MoneroError("Cannot specify seed offset when creating wallet from keys");
return createWalletFromKeys(config.getPath(), config.getPassword(), config.getNetworkType(), config.getPrimaryAddress(), config.getPrivateViewKey(), config.getPrivateSpendKey(), config.getServer(), config.getRestoreHeight(), config.getLanguage());
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/monero/wallet/MoneroWalletRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public MoneroWalletRpc createWallet(MoneroWalletConfig config) {
// create wallet
if (config.getMnemonic() != null) {
createWalletFromMnemonic(config.getPath(), config.getPassword(), config.getMnemonic(), config.getRestoreHeight(), config.getLanguage(), config.getSeedOffset(), config.getSaveCurrent());
} else if (config.getPrimaryAddress() != null) {
} else if (config.getPrivateSpendKey() != null || config.getPrimaryAddress() != null) {
if (config.getSeedOffset() != null) throw new MoneroError("Cannot specify seed offset when creating wallet from keys");
createWalletFromKeys(config.getPath(), config.getPassword(), config.getPrimaryAddress(), config.getPrivateViewKey(), config.getPrivateSpendKey(), config.getRestoreHeight(), config.getLanguage(), config.getSaveCurrent());
} else {
Expand Down
27 changes: 24 additions & 3 deletions src/test/java/test/TestMoneroWalletCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public void testCreateWalletFromMnemonicWithOffset() {
@Test
public void testCreateWalletFromKeys() {
assumeTrue(TEST_NON_RELAYS);
Exception e1 = null; // emulating Java "finally" but compatible with other languages
Exception e1 = null; // emulating Java "finally" but compatible with other languages
try {

// save for comparison
Expand All @@ -329,14 +329,14 @@ public void testCreateWalletFromKeys() {
String privateSpendKey = wallet.getPrivateSpendKey();

// recreate test wallet from keys
MoneroWallet wallet = createWallet(new MoneroWalletConfig().setPrimaryAddress(primaryAddress).setPrivateViewKey(privateViewKey).setPrivateSpendKey(privateSpendKey).setRestoreHeight(TestUtils.FIRST_RECEIVE_HEIGHT));
MoneroWallet wallet = createWallet(new MoneroWalletConfig().setPrimaryAddress(primaryAddress).setPrivateViewKey(privateViewKey).setPrivateSpendKey(privateSpendKey).setRestoreHeight(daemon.getHeight()));
String path = wallet.getPath();
Exception e2 = null;
try {
assertEquals(primaryAddress, wallet.getPrimaryAddress());
assertEquals(privateViewKey, wallet.getPrivateViewKey());
assertEquals(privateSpendKey, wallet.getPrivateSpendKey());
if (!wallet.isConnectedToDaemon()) System.out.println("WARNING: wallet created from keys is not connected to authenticated daemon"); // TODO monero-project: keys wallets not connected
if (!wallet.isConnectedToDaemon()) System.out.println("WARNING: wallet created from keys is not connected to authenticated daemon"); // TODO monero-project: keys wallets not connected
assertTrue(wallet.isConnectedToDaemon(), "Wallet created from keys is not connected to authenticated daemon");
if (!(wallet instanceof MoneroWalletRpc)) {
MoneroUtils.validateMnemonic(wallet.getMnemonic()); // TODO monero-wallet-rpc: cannot get mnemonic from wallet created from keys?
Expand All @@ -348,6 +348,27 @@ public void testCreateWalletFromKeys() {
closeWallet(wallet);
if (e2 != null) throw e2;

// recreate test wallet from spend key
if (!(wallet instanceof MoneroWalletRpc)) { // TODO monero-wallet-rpc: cannot create wallet from spend key?
wallet = createWallet(new MoneroWalletConfig().setPrivateSpendKey(privateSpendKey).setRestoreHeight(daemon.getHeight()));
e2 = null;
try {
assertEquals(primaryAddress, wallet.getPrimaryAddress());
assertEquals(privateViewKey, wallet.getPrivateViewKey());
assertEquals(privateSpendKey, wallet.getPrivateSpendKey());
if (!wallet.isConnectedToDaemon()) System.out.println("WARNING: wallet created from keys is not connected to authenticated daemon"); // TODO monero-project: keys wallets not connected
assertTrue(wallet.isConnectedToDaemon(), "Wallet created from keys is not connected to authenticated daemon");
if (!(wallet instanceof MoneroWalletRpc)) {
MoneroUtils.validateMnemonic(wallet.getMnemonic()); // TODO monero-wallet-rpc: cannot get mnemonic from wallet created from keys?
assertEquals(MoneroWallet.DEFAULT_LANGUAGE, wallet.getMnemonicLanguage());
}
} catch (Exception e) {
e2 = e;
}
closeWallet(wallet);
if (e2 != null) throw e2;
}

// attempt to create wallet at same path
try {
createWallet(new MoneroWalletConfig().setPath(path));
Expand Down

0 comments on commit 05b2ab8

Please sign in to comment.