Skip to content

Commit

Permalink
wallet mnemonic update
Browse files Browse the repository at this point in the history
  • Loading branch information
HashHound committed Jul 14, 2024
1 parent 5385942 commit 1b61625
Show file tree
Hide file tree
Showing 4 changed files with 1,750 additions and 21 deletions.
12 changes: 4 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ file(GLOB_RECURSE P2p P2p/*)
file(GLOB_RECURSE Rpc Rpc/*)
file(GLOB_RECURSE Serialization Serialization/*)
file(GLOB_RECURSE SimpleWallet SimpleWallet/*)
if(MSVC)
file(GLOB_RECURSE System System/* Platform/Windows/System/*)
elseif(APPLE)
file(GLOB_RECURSE System System/* Platform/OSX/System/*)
else()
file(GLOB_RECURSE System System/* Platform/Linux/System/*)
endif()
file(GLOB_RECURSE Transfers Transfers/*)
file(GLOB_RECURSE Wallet Wallet/*)
file(GLOB_RECURSE WalletLegacy WalletLegacy/*)
Expand All @@ -32,7 +26,9 @@ file(GLOB_RECURSE PaymentGate PaymentGate/*)
file(GLOB_RECURSE PaymentGateService PaymentGateService/*)
file(GLOB_RECURSE Miner Miner/*)

source_group("" FILES $${Common} ${ConnectivityTool} ${Crypto} ${CryptoNoteCore} ${CryptoNoteProtocol} ${Daemon} ${JsonRpcServer} ${Http} ${Logging} ${NodeRpcProxy} ${P2p} ${Rpc} ${Serialization} ${SimpleWallet} ${System} ${Transfers} ${Wallet} ${WalletLegacy})
file(GLOB_RECURSE Mnemonic src/mnemonic/*) # Add this line

source_group("" FILES ${Common} ${ConnectivityTool} ${Crypto} ${CryptoNoteCore} ${CryptoNoteProtocol} ${Daemon} ${JsonRpcServer} ${Http} ${Logging} ${NodeRpcProxy} ${P2p} ${Rpc} ${Serialization} ${SimpleWallet} ${System} ${Transfers} ${Wallet} ${WalletLegacy} ${Mnemonic})

add_library(BlockchainExplorer ${BlockchainExplorer})
add_library(Common ${Common})
Expand Down Expand Up @@ -79,5 +75,5 @@ set_property(TARGET ConnectivityTool PROPERTY OUTPUT_NAME "connectivity_tool")
set_property(TARGET SimpleWallet PROPERTY OUTPUT_NAME "simplewallet")
set_property(TARGET PaymentGateService PROPERTY OUTPUT_NAME "walletd")
set_property(TARGET Miner PROPERTY OUTPUT_NAME "miner")
#TODO Specify the name of daemon for your currency
# TODO Specify the name of daemon for your currency
set_property(TARGET Daemon PROPERTY OUTPUT_NAME "dogemoned")
76 changes: 63 additions & 13 deletions src/SimpleWallet/SimpleWallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,23 +732,73 @@ bool simple_wallet::restore_wallet(const std::string &wallet_file, const std::st
return true;
}

bool simple_wallet::restore_wallet_command(const std::vector<std::string> &args) {
if (args.size() < 2) {
fail_msg_writer() << "Usage: restore_wallet <file> <mnemonic>";
return false;
}
bool SimpleWallet::restoreWalletFromMnemonic(const std::string& mnemonic) {
Crypto::SecretKey spendSecretKey;
if (!Crypto::ElectrumWords::words_to_bytes(mnemonic, spendSecretKey)) {
fail_msg_writer() << "Invalid mnemonic phrase.";
return false;
}

std::string wallet_file = args[0];
std::string mnemonic = args[1];
m_wallet.reset(new WalletLegacy(m_currency, *m_node.get()));
m_node->addObserver(static_cast<INodeObserver*>(this));
m_wallet->addObserver(this);

Tools::PasswordContainer pwd_container;
if (!pwd_container.read_password()) {
fail_msg_writer() << "failed to read wallet password";
return false;
}
AccountKeys keys;
keys.spendSecretKey = spendSecretKey;
Crypto::secret_key_to_public_key(keys.spendSecretKey, keys.address.spendPublicKey);

Crypto::SecretKey viewSecretKey = Crypto::generate_keys(keys.address.viewPublicKey, keys.viewSecretKey);
keys.viewSecretKey = viewSecretKey;

return restore_wallet(wallet_file, mnemonic, pwd_container.password());
m_wallet->initWithKeys(keys, password);

try {
WalletHelper::storeWallet(*m_wallet, m_wallet_file);
} catch (std::exception& e) {
fail_msg_writer() << "failed to save new wallet: " << e.what();
throw;
}

success_msg_writer() << "Wallet restored successfully.";
return true;
}
void SimpleWallet::showMnemonic() {
Crypto::SecretKey spendSecretKey = m_wallet->getAccountKeys().spendSecretKey;
std::string mnemonic = generate_mnemonic(spendSecretKey);
std::cout << "Mnemonic: " << mnemonic << std::endl;
}

if (command == "restore_from_mnemonic") {
std::string mnemonic;
std::cout << "Enter mnemonic: ";
std::getline(std::cin, mnemonic);
if (!wallet.restoreWalletFromMnemonic(mnemonic)) {
std::cerr << "Failed to restore wallet from mnemonic." << std::endl;
} else {
std::cout << "Wallet restored successfully." << std::endl;
}
}

if (command == "show_mnemonic") {
wallet.showMnemonic();
}
void SimpleWallet::handleCommand(const std::string& command) {
if (command == "restore_from_mnemonic") {
std::string mnemonic;
std::cout << "Enter mnemonic: ";
std::getline(std::cin, mnemonic);
if (!restoreWalletFromMnemonic(mnemonic)) {
std::cerr << "Failed to restore wallet from mnemonic." << std::endl;
} else {
std::cout << "Wallet restored successfully." << std::endl;
}
} else if (command == "show_mnemonic") {
showMnemonic();
} else {
// Handle other commands
}
}

//----------------------------------------------------------------------------------------------------
bool simple_wallet::close_wallet()
{
Expand Down
Loading

0 comments on commit 1b61625

Please sign in to comment.