Skip to content

Commit

Permalink
watch only: update client blob xpubs when creating a new subaccount
Browse files Browse the repository at this point in the history
  • Loading branch information
jgriffiths committed May 13, 2022
1 parent e110b17 commit 83f2d53
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/client_blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ namespace sdk {

uint64_t client_blob::get_user_version() const { return m_data[USER_VERSION]; }

bool client_blob::set_subaccount_name(uint32_t subaccount, const std::string& name)
bool client_blob::set_subaccount_name(uint32_t subaccount, const std::string& name, const nlohmann::json& xpubs)
{
if (is_key_encrypted(SA_NAMES)) {
// This gdk version does not support encrypted subaccount names
throw user_error("Client too old. Please upgrade your app!"); // TODO: i18n
}
const std::string subaccount_str(std::to_string(subaccount));
const bool changed = json_add_non_default(m_data[SA_NAMES], subaccount_str, name);
bool changed = json_add_non_default(m_data[SA_NAMES], subaccount_str, name);
if (!xpubs.empty() && m_data[WATCHONLY].contains("username")) {
// Client blob watch only is enabled, update the subaccount xpubs
json_add_non_default(m_data[WATCHONLY], "xpubs", xpubs);
changed = true;
}
return changed ? increment_version(m_data) : changed;
}

Expand Down
2 changes: 1 addition & 1 deletion src/client_blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace sdk {
void set_user_version(uint64_t version);
uint64_t get_user_version() const;

bool set_subaccount_name(uint32_t subaccount, const std::string& name);
bool set_subaccount_name(uint32_t subaccount, const std::string& name, const nlohmann::json& xpubs);
std::string get_subaccount_name(uint32_t subaccount) const;

bool set_subaccount_hidden(uint32_t subaccount, bool is_hidden);
Expand Down
19 changes: 10 additions & 9 deletions src/ga_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,9 @@ namespace sdk {
// No client blob: create one, save it to the server and cache it,
// but only if the wallet isn't locked for a two factor reset.
// Subaccount names
const auto signer_xpubs = get_signer_xpubs_json(m_signer);
for (const auto& sa : login_data["subaccounts"]) {
m_blob.set_subaccount_name(sa["pointer"], json_get_value(sa, "name"));
m_blob.set_subaccount_name(sa["pointer"], json_get_value(sa, "name"), signer_xpubs);
}
// Tx memos
nlohmann::json tx_memos = wamp_cast_json(m_wamp->call(locker, "txs.get_memos"));
Expand Down Expand Up @@ -1613,8 +1614,8 @@ namespace sdk {
}

// Set watch only data in the client blob. Blanks the username if disabling.
const auto xpubs = get_signer_xpubs_json(m_signer);
update_blob(locker, std::bind(&client_blob::set_wo_data, &m_blob, username, xpubs));
const auto signer_xpubs = get_signer_xpubs_json(m_signer);
update_blob(locker, std::bind(&client_blob::set_wo_data, &m_blob, username, signer_xpubs));

std::pair<std::string, std::string> u_p{ username, password };
std::string wo_blob_key_hex;
Expand Down Expand Up @@ -1696,7 +1697,8 @@ namespace sdk {
GDK_RUNTIME_ASSERT_MSG(p != m_subaccounts.end(), "Unknown subaccount");
const std::string old_name = json_get_value(p->second, "name");
if (old_name != new_name) {
update_blob(locker, std::bind(&client_blob::set_subaccount_name, &m_blob, subaccount, new_name));
nlohmann::json empty;
update_blob(locker, std::bind(&client_blob::set_subaccount_name, &m_blob, subaccount, new_name, empty));
// Look up our subaccount again as iterators may have been invalidated
m_subaccounts.find(subaccount)->second["name"] = new_name;
}
Expand Down Expand Up @@ -1800,9 +1802,8 @@ namespace sdk {
if (type == "2of3") {
subaccount_details["recovery_xpub"] = recovery_bip32_xpub;
}
if (!name.empty()) {
update_blob(locker, std::bind(&client_blob::set_subaccount_name, &m_blob, subaccount, name));
}
const auto signer_xpubs = get_signer_xpubs_json(m_signer);
update_blob(locker, std::bind(&client_blob::set_subaccount_name, &m_blob, subaccount, name, signer_xpubs));
return subaccount_details;
}

Expand Down Expand Up @@ -1846,8 +1847,8 @@ namespace sdk {
void ga_session::encache_signer_xpubs(std::shared_ptr<signer> signer)
{
locker_t locker(m_mutex);
const auto cached_xpubs = get_signer_xpubs_json(signer);
m_cache->upsert_key_value("xpubs", nlohmann::json::to_msgpack(cached_xpubs));
const auto signer_xpubs = get_signer_xpubs_json(signer);
m_cache->upsert_key_value("xpubs", nlohmann::json::to_msgpack(signer_xpubs));
m_cache->save_db();
}

Expand Down

0 comments on commit 83f2d53

Please sign in to comment.