From 83f2d53d906ebc4a0b9d4b0d2dc12a000f1ad02d Mon Sep 17 00:00:00 2001 From: Jon Griffiths Date: Sat, 14 May 2022 00:27:18 +1200 Subject: [PATCH] watch only: update client blob xpubs when creating a new subaccount --- src/client_blob.cpp | 9 +++++++-- src/client_blob.hpp | 2 +- src/ga_session.cpp | 19 ++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/client_blob.cpp b/src/client_blob.cpp index 1abd29533..33efe96e9 100644 --- a/src/client_blob.cpp +++ b/src/client_blob.cpp @@ -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; } diff --git a/src/client_blob.hpp b/src/client_blob.hpp index d7253a523..6b2757cbe 100644 --- a/src/client_blob.hpp +++ b/src/client_blob.hpp @@ -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); diff --git a/src/ga_session.cpp b/src/ga_session.cpp index 4425c3a10..f39f7f455 100644 --- a/src/ga_session.cpp +++ b/src/ga_session.cpp @@ -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")); @@ -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 u_p{ username, password }; std::string wo_blob_key_hex; @@ -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; } @@ -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; } @@ -1846,8 +1847,8 @@ namespace sdk { void ga_session::encache_signer_xpubs(std::shared_ptr 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(); }