Wallet API: functions for supporting/creating view only wallets

This commit is contained in:
Jaquee 2017-01-10 22:34:15 +01:00
parent c6ec939626
commit 5eed5b056b
No known key found for this signature in database
GPG key ID: 384E52B09F45DC39
4 changed files with 88 additions and 5 deletions

View file

@ -277,6 +277,46 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
return true; return true;
} }
bool WalletImpl::createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const
{
clearStatus();
std::unique_ptr<tools::wallet2> view_wallet(new tools::wallet2(m_wallet->testnet()));
// Store same refresh height as original wallet
view_wallet->set_refresh_from_block_height(m_wallet->get_refresh_from_block_height());
bool keys_file_exists;
bool wallet_file_exists;
tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists);
LOG_PRINT_L3("wallet_path: " << path << "");
LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha
<< " wallet_file_exists: " << std::boolalpha << wallet_file_exists << std::noboolalpha);
// add logic to error out if new wallet requested but named wallet file exists
if (keys_file_exists || wallet_file_exists) {
m_errorString = "attempting to generate view only wallet, but specified file(s) exist. Exiting to not risk overwriting.";
LOG_ERROR(m_errorString);
m_status = Status_Error;
return false;
}
// TODO: validate language
view_wallet->set_seed_language(language);
const crypto::secret_key viewkey = m_wallet->get_account().get_keys().m_view_secret_key;
const cryptonote::account_public_address address = m_wallet->get_account().get_keys().m_account_address;
try {
view_wallet->generate(path, password, address, viewkey);
m_status = Status_Ok;
} catch (const std::exception &e) {
LOG_ERROR("Error creating view only wallet: " << e.what());
m_status = Status_Error;
m_errorString = e.what();
return false;
}
return true;
}
bool WalletImpl::open(const std::string &path, const std::string &password) bool WalletImpl::open(const std::string &path, const std::string &password)
{ {
clearStatus(); clearStatus();
@ -415,6 +455,11 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const
return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet());
} }
std::string WalletImpl::privateViewKey() const
{
return epee::string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key);
}
std::string WalletImpl::path() const std::string WalletImpl::path() const
{ {
return m_wallet->path(); return m_wallet->path();
@ -966,7 +1011,12 @@ bool WalletImpl::trustedDaemon() const
return m_trustedDaemon; return m_trustedDaemon;
} }
void WalletImpl::clearStatus() bool WalletImpl::watchOnly() const
{
return m_wallet->watch_only();
}
void WalletImpl::clearStatus() const
{ {
m_status = Status_Ok; m_status = Status_Ok;
m_errorString.clear(); m_errorString.clear();
@ -1020,7 +1070,9 @@ void WalletImpl::doRefresh()
if (m_history->count() == 0) { if (m_history->count() == 0) {
m_history->refresh(); m_history->refresh();
} }
} } else {
LOG_PRINT_L3(__FUNCTION__ << ": skipping refresh - daemon is not synced");
}
} catch (const std::exception &e) { } catch (const std::exception &e) {
m_status = Status_Error; m_status = Status_Error;
m_errorString = e.what(); m_errorString = e.what();
@ -1067,8 +1119,9 @@ bool WalletImpl::isNewWallet() const
// in case wallet created without daemon connection, closed and opened again, // in case wallet created without daemon connection, closed and opened again,
// it's the same case as if it created from scratch, i.e. we need "fast sync" // it's the same case as if it created from scratch, i.e. we need "fast sync"
// with the daemon (pull hashes instead of pull blocks). // with the daemon (pull hashes instead of pull blocks).
// If wallet cache is rebuilt, creation height stored in .keys is used. // If wallet cache is rebuilt, creation height stored in .keys is used.
return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache); // Watch only wallet is a copy of an existing wallet.
return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache) && !watchOnly();
} }
void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit) void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit)
@ -1078,9 +1131,13 @@ void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction
// in case new wallet, this will force fast-refresh (pulling hashes instead of blocks) // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks)
// If daemon isn't synced a calculated block height will be used instead // If daemon isn't synced a calculated block height will be used instead
if (isNewWallet() && daemonSynced()) { if (isNewWallet() && daemonSynced()) {
LOG_PRINT_L2(__FUNCTION__ << ":New Wallet - fast refresh until " << daemonBlockChainHeight());
m_wallet->set_refresh_from_block_height(daemonBlockChainHeight()); m_wallet->set_refresh_from_block_height(daemonBlockChainHeight());
} }
if (m_rebuildWalletCache)
LOG_PRINT_L2(__FUNCTION__ << ": Rebuilding wallet cache, fast refresh until block " << m_wallet->get_refresh_from_block_height());
if (Utils::isAddressLocal(daemon_address)) { if (Utils::isAddressLocal(daemon_address)) {
this->setTrustedDaemon(true); this->setTrustedDaemon(true);
} }

View file

@ -53,6 +53,8 @@ public:
~WalletImpl(); ~WalletImpl();
bool create(const std::string &path, const std::string &password, bool create(const std::string &path, const std::string &password,
const std::string &language); const std::string &language);
bool createWatchOnly(const std::string &path, const std::string &password,
const std::string &language) const;
bool open(const std::string &path, const std::string &password); bool open(const std::string &path, const std::string &password);
bool recover(const std::string &path, const std::string &seed); bool recover(const std::string &path, const std::string &seed);
bool close(); bool close();
@ -65,6 +67,7 @@ public:
bool setPassword(const std::string &password); bool setPassword(const std::string &password);
std::string address() const; std::string address() const;
std::string integratedAddress(const std::string &payment_id) const; std::string integratedAddress(const std::string &payment_id) const;
std::string privateViewKey() const;
std::string path() const; std::string path() const;
bool store(const std::string &path); bool store(const std::string &path);
std::string filename() const; std::string filename() const;
@ -88,6 +91,7 @@ public:
int autoRefreshInterval() const; int autoRefreshInterval() const;
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height); void setRefreshFromBlockHeight(uint64_t refresh_from_block_height);
void setRecoveringFromSeed(bool recoveringFromSeed); void setRecoveringFromSeed(bool recoveringFromSeed);
bool watchOnly() const;
@ -112,7 +116,7 @@ public:
virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error); virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error);
private: private:
void clearStatus(); void clearStatus() const;
void refreshThreadFunc(); void refreshThreadFunc();
void doRefresh(); void doRefresh();
bool daemonSynced() const; bool daemonSynced() const;

View file

@ -325,6 +325,7 @@ namespace tools
const cryptonote::account_base& get_account()const{return m_account;} const cryptonote::account_base& get_account()const{return m_account;}
void set_refresh_from_block_height(uint64_t height) {m_refresh_from_block_height = height;} void set_refresh_from_block_height(uint64_t height) {m_refresh_from_block_height = height;}
uint64_t get_refresh_from_block_height() const {return m_refresh_from_block_height;}
// upper_transaction_size_limit as defined below is set to // upper_transaction_size_limit as defined below is set to
// approximately 125% of the fixed minimum allowable penalty // approximately 125% of the fixed minimum allowable penalty

View file

@ -256,6 +256,12 @@ struct Wallet
*/ */
virtual std::string integratedAddress(const std::string &payment_id) const = 0; virtual std::string integratedAddress(const std::string &payment_id) const = 0;
/*!
* \brief privateViewKey - returns private view key
* \return - private view key
*/
virtual std::string privateViewKey() const = 0;
/*! /*!
* \brief store - stores wallet to file. * \brief store - stores wallet to file.
* \param path - main filename to store wallet to. additionally stores address file and keys file. * \param path - main filename to store wallet to. additionally stores address file and keys file.
@ -294,6 +300,15 @@ struct Wallet
*/ */
virtual void initAsync(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0; virtual void initAsync(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0;
/*!
* \brief createWatchOnly - Creates a watch only wallet
* \param path - where to store the wallet
* \param password
* \param language
* \return - true if created successfully
*/
virtual bool createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const = 0;
/*! /*!
* \brief setRefreshFromBlockHeight - start refresh from block height on recover * \brief setRefreshFromBlockHeight - start refresh from block height on recover
* *
@ -324,6 +339,12 @@ struct Wallet
virtual uint64_t balance() const = 0; virtual uint64_t balance() const = 0;
virtual uint64_t unlockedBalance() const = 0; virtual uint64_t unlockedBalance() const = 0;
/**
* @brief watchOnly - checks if wallet is watch only
* @return - true if watch only
*/
virtual bool watchOnly() const = 0;
/** /**
* @brief blockChainHeight - returns current blockchain height * @brief blockChainHeight - returns current blockchain height
* @return * @return