From 9b55a0c8d31d8e95d7e06d9c948a9cf9f16155b2 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Mon, 21 Jun 2021 11:50:03 -0400 Subject: [PATCH] Store last wallet unlock time in preferences --- browser/extensions/api/brave_wallet_api.cc | 11 +++++++++++ browser/extensions/api/brave_wallet_api.h | 9 +++++++++ common/extensions/api/brave_wallet.json | 5 +++++ .../brave_wallet/browser/brave_wallet_service.cc | 1 + .../brave_wallet/browser/brave_wallet_utils.cc | 12 +++++++++++- components/brave_wallet/browser/brave_wallet_utils.h | 8 ++++++++ .../brave_wallet/browser/keyring_controller.cc | 2 ++ components/brave_wallet/browser/pref_names.cc | 2 ++ components/brave_wallet/browser/pref_names.h | 1 + components/definitions/chromel.d.ts | 1 + .../extensions/api_test/braveWallet/background.js | 1 + 11 files changed, 52 insertions(+), 1 deletion(-) diff --git a/browser/extensions/api/brave_wallet_api.cc b/browser/extensions/api/brave_wallet_api.cc index 0c21d08de627..e8bdf631fd67 100644 --- a/browser/extensions/api/brave_wallet_api.cc +++ b/browser/extensions/api/brave_wallet_api.cc @@ -104,6 +104,17 @@ BraveWalletReadyFunction::Run() { return RespondNow(NoArguments()); } +ExtensionFunction::ResponseAction BraveWalletNotifyWalletUnlockFunction::Run() { + if (browser_context()->IsTor()) { + return RespondNow(Error("Not available in Tor context")); + } + + Profile* profile = Profile::FromBrowserContext(browser_context()); + ::brave_wallet::UpdateLastUnlockPref(profile->GetPrefs()); + + return RespondNow(NoArguments()); +} + ExtensionFunction::ResponseAction BraveWalletLoadUIFunction::Run() { auto* service = GetEthereumRemoteClientService(browser_context()); diff --git a/browser/extensions/api/brave_wallet_api.h b/browser/extensions/api/brave_wallet_api.h index 6abff5c8023f..aa3aa7049931 100644 --- a/browser/extensions/api/brave_wallet_api.h +++ b/browser/extensions/api/brave_wallet_api.h @@ -35,6 +35,15 @@ class BraveWalletReadyFunction : ResponseAction Run() override; }; +class BraveWalletNotifyWalletUnlockFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("braveWallet.notifyWalletUnlock", UNKNOWN) + + protected: + ~BraveWalletNotifyWalletUnlockFunction() override {} + ResponseAction Run() override; +}; + class BraveWalletShouldCheckForDappsFunction : public ExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("braveWallet.shouldCheckForDapps", UNKNOWN) diff --git a/common/extensions/api/brave_wallet.json b/common/extensions/api/brave_wallet.json index fe724a0a6daf..d99f091cc04f 100644 --- a/common/extensions/api/brave_wallet.json +++ b/common/extensions/api/brave_wallet.json @@ -59,6 +59,11 @@ ] } ] + }, { + "name": "notifyWalletUnlock", + "type": "function", + "description": "Called to notify Brave that the wallet was unlocked", + "parameters": [] }, { "name": "getWalletSeed", "type": "function", diff --git a/components/brave_wallet/browser/brave_wallet_service.cc b/components/brave_wallet/browser/brave_wallet_service.cc index 1a131613ccff..bd67421cacf1 100644 --- a/components/brave_wallet/browser/brave_wallet_service.cc +++ b/components/brave_wallet/browser/brave_wallet_service.cc @@ -40,6 +40,7 @@ void BraveWalletService::RegisterProfilePrefs(PrefRegistrySimple* registry) { registry->RegisterIntegerPref(kBraveWalletDefaultKeyringAccountNum, 0); registry->RegisterBooleanPref(kShowWalletIconOnToolbar, true); registry->RegisterBooleanPref(kBraveWalletBackupComplete, false); + registry->RegisterTimePref(kBraveWalletLastUnlockTime, base::Time()); } brave_wallet::EthJsonRpcController* BraveWalletService::rpc_controller() const { diff --git a/components/brave_wallet/browser/brave_wallet_utils.cc b/components/brave_wallet/browser/brave_wallet_utils.cc index f0c6b2e450a8..5b588f8923c4 100644 --- a/components/brave_wallet/browser/brave_wallet_utils.cc +++ b/components/brave_wallet/browser/brave_wallet_utils.cc @@ -15,10 +15,12 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_split.h" #include "base/strings/string_util.h" -#include "base/strings/stringprintf.h" +#include "base/time/time.h" +#include "brave/components/brave_wallet/browser/pref_names.h" #include "brave/components/brave_wallet/common/features.h" #include "brave/third_party/ethash/src/include/ethash/keccak.h" #include "brave/vendor/bip39wally-core-native/include/wally_bip39.h" +#include "components/prefs/pref_service.h" #include "crypto/random.h" #include "third_party/boringssl/src/include/openssl/evp.h" @@ -375,4 +377,12 @@ void SecureZeroData(void* data, size_t size) { #endif } +// Updates preferences for when the wallet is unlocked. +// This is done in a utils function instead of in the KeyringController +// because we call it both from the old extension and the new wallet when +// it unlocks. +void UpdateLastUnlockPref(PrefService* prefs) { + prefs->SetTime(kBraveWalletLastUnlockTime, base::Time::Now()); +} + } // namespace brave_wallet diff --git a/components/brave_wallet/browser/brave_wallet_utils.h b/components/brave_wallet/browser/brave_wallet_utils.h index ab9e239aabd2..e43bad6e0042 100644 --- a/components/brave_wallet/browser/brave_wallet_utils.h +++ b/components/brave_wallet/browser/brave_wallet_utils.h @@ -12,6 +12,8 @@ #include "brave/components/brave_wallet/browser/brave_wallet_types.h" +class PrefService; + namespace brave_wallet { bool IsNativeWalletEnabled(); @@ -74,6 +76,12 @@ std::string Namehash(const std::string& name); // So we use our own function for this purpose. void SecureZeroData(void* data, size_t size); +// Updates preferences for when the wallet is unlocked. +// This is done in a utils function instead of in the KeyringController +// because we call it both from the old extension and the new wallet when +// it unlocks. +void UpdateLastUnlockPref(PrefService* prefs); + } // namespace brave_wallet #endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_UTILS_H_ diff --git a/components/brave_wallet/browser/keyring_controller.cc b/components/brave_wallet/browser/keyring_controller.cc index bbe0f4ac2332..d66e1e339574 100644 --- a/components/brave_wallet/browser/keyring_controller.cc +++ b/components/brave_wallet/browser/keyring_controller.cc @@ -130,6 +130,7 @@ bool KeyringController::Unlock(const std::string& password) { return false; } + UpdateLastUnlockPref(prefs_); return true; } @@ -205,6 +206,7 @@ bool KeyringController::CreateDefaultKeyringInternal( return false; default_keyring_ = std::make_unique(); default_keyring_->ConstructRootHDKey(*seed, "m/44'/60'/0'/0"); + UpdateLastUnlockPref(prefs_); return true; } diff --git a/components/brave_wallet/browser/pref_names.cc b/components/brave_wallet/browser/pref_names.cc index df0ec79f6221..23e82706c3e7 100644 --- a/components/brave_wallet/browser/pref_names.cc +++ b/components/brave_wallet/browser/pref_names.cc @@ -16,3 +16,5 @@ const char kBraveWalletDefaultKeyringAccountNum[] = const char kShowWalletIconOnToolbar[] = "brave.wallet.show_wallet_icon_on_toolbar"; const char kBraveWalletBackupComplete[] = "brave.wallet.wallet_backup_complete"; +const char kBraveWalletLastUnlockTime[] = + "brave.wallet.wallet_last_unlock_time"; diff --git a/components/brave_wallet/browser/pref_names.h b/components/brave_wallet/browser/pref_names.h index fe7adbdcefe2..39daf57dd4a7 100644 --- a/components/brave_wallet/browser/pref_names.h +++ b/components/brave_wallet/browser/pref_names.h @@ -13,5 +13,6 @@ extern const char kBraveWalletEncryptedMnemonic[]; extern const char kBraveWalletDefaultKeyringAccountNum[]; extern const char kShowWalletIconOnToolbar[]; extern const char kBraveWalletBackupComplete[]; +extern const char kBraveWalletLastUnlockTime[]; #endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_PREF_NAMES_H_ diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 57428e663288..27de1eb80766 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -351,6 +351,7 @@ declare namespace chrome.braveWallet { const shouldPromptForSetup: (callback: (dappDetection: boolean) => void) => void const loadUI: (callback: () => void) => void const isNativeWalletEnabled: (callback: (enabled: boolean) => void) => void + const notifyWalletUnlock: () => void } declare namespace chrome.ipfs { diff --git a/test/data/extensions/api_test/braveWallet/background.js b/test/data/extensions/api_test/braveWallet/background.js index d59d25f94e0c..b4d632b653f3 100644 --- a/test/data/extensions/api_test/braveWallet/background.js +++ b/test/data/extensions/api_test/braveWallet/background.js @@ -152,6 +152,7 @@ function testBasics() { function braveWalletExtensionHasAccess() { if (chrome.braveWallet && chrome.braveWallet.shouldPromptForSetup && + chrome.braveWallet.notifyWalletUnlock && chrome.braveWallet.loadUI && chrome.braveWallet.promptToEnableWallet) { chrome.test.succeed();