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

Refactor how eth_requestAccounts works to make it more aligned with MetaMask #11459

Merged
merged 7 commits into from
Dec 9, 2021

Conversation

bbondy
Copy link
Member

@bbondy bbondy commented Dec 6, 2021

This work starts with a small refactor to pass error codes for provider instead of only a bool success code.
It then moves the provider errors into mojo.

If an account is selected and it is connected, eth_request and eth_accounts will return only that account to be consistent with MetaMask.

If an account is connected but the wallet is locked, we'll now ask to unlock the wallet instead of showing a permission prompt with the other accounts you can connect to. Douglas is doing some UI changes to make it easier to switch which account is connected in a separate PR.

Finally we return request already in progress now instead of stacking requests.

Resolves brave/brave-browser#19750
Resolves brave/brave-browser#19401
Resolves brave/brave-browser#19641

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@bbondy bbondy requested a review from a team as a code owner December 6, 2021 23:57
@bbondy bbondy changed the title Permissions Refactor how eth_requestAccounts works to make it more aligned with MetaMask Dec 7, 2021
UpdateKnownAccounts();
if (pending_unlock_callback_) {
RequestEthereumPermissions(std::move(pending_unlock_callback_));
pending_unlock_callback_ = RequestEthereumPermissionsCallback();
Copy link
Member

@yrliou yrliou Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pending_unlock_callback_ naming is confusing, I was a bit confused at first and didn't realize it's the callback for RequestEthereumPermissions, maybe pending_request_ethereum_permissions_callback_ and a comment in the header to describe it's pending due to wallet is locked and we need to unlock first?
And could you explain to me why do we need to construct a callback and assign to this member when the stored callback is used?

Copy link
Member Author

@bbondy bbondy Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4e506546f50e86fabd1d77c3ff2fc231a965f2f8

Comment on lines 545 to 560
// If the call was successful but the keyring is locked, then request an
// unlock. After the unlock happens a new request will be made.
if (error == mojom::ProviderError::kSuccess &&
keyring_controller_->IsLocked()) {
if (pending_unlock_callback_) {
std::move(callback).Run(
std::vector<std::string>(),
mojom::ProviderError::kUserRejectedRequest,
l10n_util::GetStringUTF8(IDS_WALLET_ALREADY_IN_PROGRESS_ERROR));
return;
}
pending_unlock_callback_ = std::move(callback);
keyring_controller_->RequestUnlock();
delegate_->ShowPanel();
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to run RequestEthereumPermissions once and handle the unlock logic here? Can this block be moved to RequestEthereumPermissions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that originally but there was some test failures that way. For example when no wallet is setup we want to open the onboarding. Basically we only want to do this request unlock logic when things would be successful but the keyring is locked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the explanation.

// Obtains if there's a pending unlock request
// Note that there is no need for an API to notify unlocked
// because the KeyringControllerObserver Unlocked event can be used for that.
GetPendingUnlockRequest() => (bool pending);
Copy link
Member

@yrliou yrliou Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: How about HasPendingUnlockRequest?

Copy link
Member Author

@bbondy bbondy Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4e506546f50e86fabd1d77c3ff2fc231a965f2f8

@bbondy bbondy force-pushed the permissions branch 4 times, most recently from 1efc7c9 to 83611a2 Compare December 8, 2021 13:59
Comment on lines 671 to 672
pending_request_ethereum_permissions_callback_ =
RequestEthereumPermissionsCallback();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'll prefer this to be taken out if it turns out to be unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in rebase

bbondy and others added 2 commits December 8, 2021 22:17
…thJsonRpcController

So we no longer need to manually create controller instances just for setting
the url loader in tests.
@bbondy bbondy merged commit 22f952b into master Dec 9, 2021
@bbondy bbondy deleted the permissions branch December 9, 2021 20:46
@bbondy bbondy added this to the 1.35.x - Nightly milestone Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants