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

Jl/caip25 permission migration/move request grant hooks into mmc #29213

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Dec 13, 2024

Description

  • Adds requestPermittedChainsPermission, requestPermittedChainsPermissionIncremental, and requestCaip25Permission methods to MetamaskController
  • Uses these new hooks in wallet_requestPermissions, eth_requestAccounts, wallet_switchEthereumChain, wallet_addEthereumChain
  • Removes wallet_requestPermissions update caveat flow
  • Make wallet_requestPermissions no longer support anything but eth_requestAccounts and endowment:permittedChains

Open in GitHub Codespaces

Related issues

See: #27847 (comment)

Manual testing steps

Only user facing change is that wallet_requestPermissions should correctly replace instead update existing permissions again

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

…on. Use in wallet_requestPermissions and eth_requestAccounts. Remove updateCaveat and support of unexpected permissions in wallet_requestPermissions
…ermission requestPermittedChainsPermissionIncremental to MMC. Use downstream.
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [036a418]
Page Load Metrics (1518 ± 27 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1397161615185627
domContentLoaded1390159915005225
load1398161415185527
domInteractive236436147
backgroundConnect85325168
firstReactRender1695372713
getState440884
initialActions01000
loadScripts1018119211095124
setupStore643984
uiStartup16022206175314368
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 15.58 KiB (0.28%)
  • ui: 590 Bytes (0.01%)
  • common: 132.75 KiB (1.66%)

Comment on lines 176 to 177
* @param hooks.requestPermittedChainsPermissionForOrigin
* @param hooks.requestPermittedChainsPermissionIncrementalForOrigin
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a jsdoc descriptor for these hooks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 5408 to 5412
async requestPermittedChainsPermissionIncremental(
origin,
chainId,
autoApprove,
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - I find it easier to understand these functions inline when the args are passed in as object rather than by position - like in this case where this is used in ethereum-chain-utils you can see that the isAddFlow boolean is used for the autoApprove arg without having toggle between the declaration to understand ... I suppose the docs also help with this. Anyways I know we do it both ways throughout the codebase. Just putting in for my preference 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

*
* @param {string} origin - The origin to request approval for.
* @param {Hex} chainId - The chainId to add incrementally.
* @param {boolean} autoApprove - If the chain should be granted without prompting for user approval.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we include an explanation of why we might want to auto approve in some cases: i.e. currently chainPermissions may be granted as part of a wallet_addEthereumChain request in which the permissions and addition of the network are combined into one confirmation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@metamaskbot
Copy link
Collaborator

Builds ready [c70f7b7]
Page Load Metrics (1507 ± 29 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1387159815115828
domContentLoaded1377157714865627
load1387160115076029
domInteractive206930126
backgroundConnect105927167
firstReactRender1693282211
getState45716189
initialActions01000
loadScripts996119311095527
setupStore679202411
uiStartup15872146177917082
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 15.69 KiB (0.29%)
  • ui: 590 Bytes (0.01%)
  • common: 132.75 KiB (1.63%)

expect(mocks.requestPermissionApprovalForOrigin).toHaveBeenCalled();
expect(
mocks.requestPermittedChainsPermissionIncrementalForOrigin,
).toHaveBeenCalled();
expect(mocks.setActiveNetwork).not.toHaveBeenCalled();
expect(end).toHaveBeenCalledWith();
});
});

describe('with an existing CAIP-25 permission granted from the multichain flow (isMultichainOrigin: true) and the chainId is not already permissioned', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 is it weird that we have tests for the multichain API in here already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah. To fix this we could just drop/move all of the isMultichainOrigin checks

Comment on lines -190 to -277
it('sets the approved chainIds on an empty CAIP-25 caveat with isMultichainOrigin: false if origin is not snapId', async () => {
const { handler } = createMockedHandler();

await handler(baseRequest);
expect(MockMultichain.setPermittedEthChainIds).toHaveBeenCalledWith(
{
requiredScopes: {},
optionalScopes: {},
isMultichainOrigin: false,
},
['0x1', '0x5'],
);
});

it('sets the approved accounts on the CAIP-25 caveat after the approved chainIds if origin is not snapId', async () => {
const { handler } = createMockedHandler();

MockMultichain.setPermittedEthChainIds.mockReturnValue({
requiredScopes: {},
optionalScopes: {},
sessionProperties: { caveatValueWithEthChainIdsSet: true },
isMultichainOrigin: false,
});

await handler(baseRequest);
expect(MockMultichain.setEthAccounts).toHaveBeenCalledWith(
{
requiredScopes: {},
optionalScopes: {},
sessionProperties: { caveatValueWithEthChainIdsSet: true },
isMultichainOrigin: false,
},
['0xdeadbeef'],
);
});

it('does not set the approved chainIds on an empty CAIP-25 caveat if origin is snapId', async () => {
const { handler } = createMockedHandler();

await handler({ ...baseRequest, origin: 'npm:snap' });
expect(MockMultichain.setPermittedEthChainIds).not.toHaveBeenCalled();
});

it('sets the approved accounts for the `wallet:eip155` scope with isMultichainOrigin: false if origin is snapId', async () => {
const { handler } = createMockedHandler();

await handler({ ...baseRequest, origin: 'npm:snap' });
expect(MockMultichain.setEthAccounts).toHaveBeenCalledWith(
{
requiredScopes: {},
optionalScopes: {
'wallet:eip155': {
accounts: [],
},
},
isMultichainOrigin: false,
},
['0xdeadbeef'],
);
});

it('grants a CAIP-25 permission', async () => {
const { handler, grantPermissions } = createMockedHandler();

MockMultichain.setEthAccounts.mockReturnValue({
requiredScopes: {},
optionalScopes: {},
sessionProperties: { caveatValueWithEthAccountsSet: true },
isMultichainOrigin: false,
});

await handler(baseRequest);
expect(grantPermissions).toHaveBeenCalledWith({
[Caip25EndowmentPermissionName]: {
caveats: [
{
type: Caip25CaveatType,
value: {
requiredScopes: {},
optionalScopes: {},
sessionProperties: { caveatValueWithEthAccountsSet: true },
isMultichainOrigin: false,
},
},
],
},
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Forgive me if its obvious but why aren't these tests relevant anymore? I see they would need to be adjusted but aren't they still test cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they've been moved/adjusted into MetaMaskController

@adonesky1
Copy link
Contributor

Screen.Recording.2024-12-16.at.3.21.33.PM.mov

@jiexi
Copy link
Contributor Author

jiexi commented Dec 16, 2024

Screen.Recording.2024-12-16.at.3.21.33.PM.mov

ticketed here https://github.com/MetaMask/MetaMask-planning/issues/3814

'addAndShowApprovalRequest',
)
.mockResolvedValue({
approvedChainIds: ['0x1', '0x5'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
approvedChainIds: ['0x1', '0x5'],
approvedChainIds: ['0x1', '0x5'],

presumably this isn't currently possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, the UI should not showing an Approval veiw that can return approvedChainIds, but I also explicitly filter out / ignore them in code. Is it worth leaving this in to be doubly sure or do you think it just causes confusion?

Copy link
Contributor

Choose a reason for hiding this comment

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

No its okay!

);
});

it('gets the CAIP-25 caveat', async () => {
Copy link
Contributor

@adonesky1 adonesky1 Dec 16, 2024

Choose a reason for hiding this comment

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

🤔 not sure I follow

oh I see. Hmmm, one of those cases where I see you're trying to avoid overly specific implementation detail language but kindof confuses without looking closer

@metamaskbot
Copy link
Collaborator

❌ API Spec Test Failed. View the report here.

jiexi and others added 6 commits December 16, 2024 14:03
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

…ove-request-grant-hooks-into-mmc' into jl/caip25-permission-migration/move-request-grant-hooks-into-mmc
@jiexi jiexi force-pushed the jl/caip25-permission-migration/move-request-grant-hooks-into-mmc branch from a65eec9 to 9ac2b92 Compare December 16, 2024 23:09
@jiexi
Copy link
Contributor Author

jiexi commented Dec 16, 2024

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

No policy changes

@metamaskbot
Copy link
Collaborator

Builds ready [e3a903b]
Page Load Metrics (1572 ± 63 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint22119081450408196
domContentLoaded13801889154912761
load13931909157213263
domInteractive22171373216
backgroundConnect774272211
firstReactRender1688382713
getState3559115
initialActions01000
loadScripts9751441113211455
setupStore683202311
uiStartup16442358189620397
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 15.69 KiB (0.29%)
  • ui: 590 Bytes (0.01%)
  • common: 132.94 KiB (1.64%)

@jiexi jiexi marked this pull request as ready for review December 17, 2024 00:09
@jiexi jiexi merged commit ff3b272 into caip25-permission-migration Dec 17, 2024
71 checks passed
@jiexi jiexi deleted the jl/caip25-permission-migration/move-request-grant-hooks-into-mmc branch December 17, 2024 00:09
@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants