Merge pull request #1526

5eed5b05 Wallet API: functions for supporting/creating view only wallets (Jaquee)
This commit is contained in:
Riccardo Spagni 2017-01-13 14:32:26 -05:00
commit 36b84ded95
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
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;
}
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)
{
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());
}
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
{
return m_wallet->path();
@ -966,7 +1011,12 @@ bool WalletImpl::trustedDaemon() const
return m_trustedDaemon;
}
void WalletImpl::clearStatus()
bool WalletImpl::watchOnly() const
{
return m_wallet->watch_only();
}
void WalletImpl::clearStatus() const
{
m_status = Status_Ok;
m_errorString.clear();
@ -1020,6 +1070,8 @@ void WalletImpl::doRefresh()
if (m_history->count() == 0) {
m_history->refresh();
}
} else {
LOG_PRINT_L3(__FUNCTION__ << ": skipping refresh - daemon is not synced");
}
} catch (const std::exception &e) {
m_status = Status_Error;
@ -1068,7 +1120,8 @@ bool WalletImpl::isNewWallet() const
// 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).
// 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)
@ -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)
// If daemon isn't synced a calculated block height will be used instead
if (isNewWallet() && daemonSynced()) {
LOG_PRINT_L2(__FUNCTION__ << ":New Wallet - fast refresh until " << 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)) {
this->setTrustedDaemon(true);
}

View File

@ -53,6 +53,8 @@ public:
~WalletImpl();
bool create(const std::string &path, const std::string &password,
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 recover(const std::string &path, const std::string &seed);
bool close();
@ -65,6 +67,7 @@ public:
bool setPassword(const std::string &password);
std::string address() const;
std::string integratedAddress(const std::string &payment_id) const;
std::string privateViewKey() const;
std::string path() const;
bool store(const std::string &path);
std::string filename() const;
@ -88,6 +91,7 @@ public:
int autoRefreshInterval() const;
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height);
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);
private:
void clearStatus();
void clearStatus() const;
void refreshThreadFunc();
void doRefresh();
bool daemonSynced() const;

View File

@ -325,6 +325,7 @@ namespace tools
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;}
uint64_t get_refresh_from_block_height() const {return m_refresh_from_block_height;}
// upper_transaction_size_limit as defined below is set to
// 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;
/*!
* \brief privateViewKey - returns private view key
* \return - private view key
*/
virtual std::string privateViewKey() const = 0;
/*!
* \brief store - stores wallet to 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;
/*!
* \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
*
@ -324,6 +339,12 @@ struct Wallet
virtual uint64_t balance() 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
* @return