Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store last wallet unlocked YMD in preferences #9205

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions browser/extensions/api/brave_wallet_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
9 changes: 9 additions & 0 deletions browser/extensions/api/brave_wallet_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions common/extensions/api/brave_wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
]
}
]
}, {
"name": "notifyWalletUnlock",
"type": "function",
"description": "Called to notify Brave that the wallet was unlocked",
"parameters": []
}, {
"name": "getWalletSeed",
"type": "function",
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "brave/components/brave_wallet/browser/brave_wallet_types.h"

class PrefService;

namespace brave_wallet {

bool IsNativeWalletEnabled();
Expand Down Expand Up @@ -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_
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/keyring_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ bool KeyringController::Unlock(const std::string& password) {
return false;
}

UpdateLastUnlockPref(prefs_);
return true;
}

Expand Down Expand Up @@ -205,6 +206,7 @@ bool KeyringController::CreateDefaultKeyringInternal(
return false;
default_keyring_ = std::make_unique<HDKeyring>();
default_keyring_->ConstructRootHDKey(*seed, "m/44'/60'/0'/0");
UpdateLastUnlockPref(prefs_);

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
1 change: 1 addition & 0 deletions components/brave_wallet/browser/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
1 change: 1 addition & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions test/data/extensions/api_test/braveWallet/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down