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

Fix SelectedNetworkController default metamask networkClientId #1757

Merged
merged 11 commits into from
Oct 4, 2023

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Oct 2, 2023

Explanation

Currently, the SelectedNetworkController does not set a value for the default metamask domain on initial NetworkController:stateChange event because it does not count selectedNetworkId as actually changed in the patch yet. This manifests itself in extension with the SelectedNetworkMiddleware setting undefined for the req.networkClientId attribute. This PR adds one more check to the event listener to ensure that the first NetworkController:stateChange event is used to populate the networkClientId for the default metamask domain. See resolution in comments

References

Changelog

@metamask/selected-network-controller

  • FIXED: setNetworkClientIdForDomain() will now ignore the passed in domain value and set the networkClientId for the metamask domain instead when the state.perDomainNetwork flag is false (default)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@jiexi jiexi requested a review from a team as a code owner October 2, 2023 20:30
@jiexi
Copy link
Contributor Author

jiexi commented Oct 2, 2023

Note that this requires SelectedNetworkController to be instantiated before NetworkController.

An alternative solution is to change setNetworkClientIdForDomain

from

  setNetworkClientIdForDomain(
    domain: Domain,
    networkClientId: NetworkClientId,
  ) {
    this.update((state) => {
      state.domains[domain] = networkClientId;
    });
  }

to

  setNetworkClientIdForDomain(
    domain: Domain,
    networkClientId: NetworkClientId,
  ) {
    this.update((state) => {
      if (!state.perDomainNetwork) {
        state.domains[METAMASK_DOMAIN] = networkClientId
        return
      }
      state.domains[domain] = networkClientId;
    });
  }

@jiexi
Copy link
Contributor Author

jiexi commented Oct 2, 2023

Another alternative is to instantiate SelectedNetworkController after NetworkController and then to call selectedNetworkController.setNetworkClientIdForMetamask(networkController.state.selectedNetworkId) afterwards

@adonesky1
Copy link
Contributor

setNetworkClientIdForDomain(
domain: Domain,
networkClientId: NetworkClientId,
) {
this.update((state) => {
if (state.perDomainNetwork) {
state.domains[METAMASK_DOMAIN] = networkClientId
return
}
state.domains[domain] = networkClientId;
});
}

Would this work? Wouldn't we be updating the the metamask domain every time then and skipping setting other domains? Did you mean if (!state.perDomainNetwork)? Maybe I'm missing something here?

adonesky1
adonesky1 previously approved these changes Oct 4, 2023
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM

@jiexi
Copy link
Contributor Author

jiexi commented Oct 4, 2023

Did you mean if (!state.perDomainNetwork)? Maybe I'm missing something here?

sorry, you're correct. The if statement should be negated like you suggested

@adonesky1 what approach do you think is preferable? The setNetworkClientIdForDomain approach would make it so controller instantiation order doesn't matter again

@adonesky1
Copy link
Contributor

Hey @jiexi sorry for the delay but

The setNetworkClientIdForDomain approach would make it so controller instantiation order doesn't matter again

I think this ^ approach is probably better

adonesky1
adonesky1 previously approved these changes Oct 4, 2023
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

Small nit otherwise LGTM

@jiexi jiexi merged commit 55546df into main Oct 4, 2023
107 checks passed
@jiexi jiexi deleted the jl/mmp-1370/fix-selected-network-controller-init-bug branch October 4, 2023 22:47
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
## Explanation

Currently, the SelectedNetworkController does not set a value for the
default `metamask` domain on initial `NetworkController:stateChange`
event because it does not count `selectedNetworkId` as actually changed
in the patch yet. This manifests itself in extension with the
SelectedNetworkMiddleware setting undefined for the
`req.networkClientId` attribute. ~~This PR adds one more check to the
event listener to ensure that the first `NetworkController:stateChange`
event is used to populate the networkClientId for the default `metamask`
domain.~~ See resolution in comments

## References

* Fixes MetaMask/MetaMask-planning#1370

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/selected-network-controller`

- **FIXED**: `setNetworkClientIdForDomain()` will now ignore the passed
in domain value and set the networkClientId for the `metamask` domain
instead when the `state.perDomainNetwork` flag is false (default)

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
## Explanation

Currently, the SelectedNetworkController does not set a value for the
default `metamask` domain on initial `NetworkController:stateChange`
event because it does not count `selectedNetworkId` as actually changed
in the patch yet. This manifests itself in extension with the
SelectedNetworkMiddleware setting undefined for the
`req.networkClientId` attribute. ~~This PR adds one more check to the
event listener to ensure that the first `NetworkController:stateChange`
event is used to populate the networkClientId for the default `metamask`
domain.~~ See resolution in comments

## References

* Fixes MetaMask/MetaMask-planning#1370

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/selected-network-controller`

- **FIXED**: `setNetworkClientIdForDomain()` will now ignore the passed
in domain value and set the networkClientId for the `metamask` domain
instead when the `state.perDomainNetwork` flag is false (default)

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
MajorLift pushed a commit that referenced this pull request Oct 12, 2023
## Explanation

Currently, the SelectedNetworkController does not set a value for the
default `metamask` domain on initial `NetworkController:stateChange`
event because it does not count `selectedNetworkId` as actually changed
in the patch yet. This manifests itself in extension with the
SelectedNetworkMiddleware setting undefined for the
`req.networkClientId` attribute. ~~This PR adds one more check to the
event listener to ensure that the first `NetworkController:stateChange`
event is used to populate the networkClientId for the default `metamask`
domain.~~ See resolution in comments

## References

* Fixes MetaMask/MetaMask-planning#1370

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/selected-network-controller`

- **FIXED**: `setNetworkClientIdForDomain()` will now ignore the passed
in domain value and set the networkClientId for the `metamask` domain
instead when the `state.perDomainNetwork` flag is false (default)

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate

---------

Co-authored-by: Alex Donesky <adonesky@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants