Skip to content

Commit

Permalink
Wallet API: functions for supporting/creating view only wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaqueeee committed Jan 9, 2017
1 parent 36e4435 commit d04aaed
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
65 changes: 61 additions & 4 deletions src/wallet/api/wallet.cpp
Expand Up @@ -280,6 +280,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();
Expand Down Expand Up @@ -418,6 +458,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();
Expand Down Expand Up @@ -1014,7 +1059,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();
Expand Down Expand Up @@ -1068,7 +1118,9 @@ 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;
m_errorString = e.what();
Expand Down Expand Up @@ -1115,8 +1167,9 @@ bool WalletImpl::isNewWallet() const
// 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"
// 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);
// If wallet cache is rebuilt, creation height stored in .keys is used.
// 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)
Expand All @@ -1126,9 +1179,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);
m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS;
Expand Down
6 changes: 5 additions & 1 deletion src/wallet/api/wallet.h
Expand Up @@ -54,6 +54,8 @@ class WalletImpl : public Wallet
~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();
Expand All @@ -66,6 +68,7 @@ class WalletImpl : public Wallet
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;
Expand All @@ -89,6 +92,7 @@ class WalletImpl : public Wallet
int autoRefreshInterval() const;
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height);
void setRecoveringFromSeed(bool recoveringFromSeed);
bool watchOnly() const;



Expand All @@ -115,7 +119,7 @@ class WalletImpl : public Wallet
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;
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet2.h
Expand Up @@ -327,6 +327,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
Expand Down
21 changes: 21 additions & 0 deletions src/wallet/wallet2_api.h
Expand Up @@ -298,6 +298,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.
Expand Down Expand Up @@ -336,6 +342,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
*
Expand Down Expand Up @@ -366,6 +381,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
Expand Down

0 comments on commit d04aaed

Please sign in to comment.