-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jl/caip multichain/lifecycle methods (#25842)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** * Add `wallet_getSession` * Add `wallet_revokeSession` * Emit `wallet_sessionChanged` on authorization change * Note this does not include specs. Seems we are not currently testing accountChanged and chainChanged events and should probably get those covered first [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25842?quickstart=1) ## **Related issues** See: MetaMask/MetaMask-planning#2821 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **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.
- Loading branch information
Showing
9 changed files
with
428 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { EthereumRpcError } from 'eth-rpc-errors'; | ||
import { | ||
Caip25CaveatType, | ||
Caip25EndowmentPermissionName, | ||
} from './caip25permissions'; | ||
import { mergeScopes } from './scope'; | ||
|
||
export async function walletGetSessionHandler( | ||
request, | ||
response, | ||
_next, | ||
end, | ||
hooks, | ||
) { | ||
if (request.params?.sessionId) { | ||
return end( | ||
new EthereumRpcError(5500, 'SessionId not recognized'), // we aren't currently storing a sessionId to check this against | ||
); | ||
} | ||
|
||
const caveat = hooks.getCaveat( | ||
request.origin, | ||
Caip25EndowmentPermissionName, | ||
Caip25CaveatType, | ||
); | ||
if (!caveat) { | ||
return end(new EthereumRpcError(5501, 'No active sessions')); | ||
} | ||
|
||
response.result = { | ||
sessionScopes: mergeScopes( | ||
caveat.value.requiredScopes, | ||
caveat.value.optionalScopes, | ||
), | ||
}; | ||
return end(); | ||
} |
111 changes: 111 additions & 0 deletions
111
app/scripts/lib/multichain-api/wallet-getSession.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { EthereumRpcError } from 'eth-rpc-errors'; | ||
import { | ||
Caip25CaveatType, | ||
Caip25EndowmentPermissionName, | ||
} from './caip25permissions'; | ||
import { walletGetSessionHandler } from './wallet-getSession'; | ||
|
||
const baseRequest = { | ||
origin: 'http://test.com', | ||
params: {}, | ||
}; | ||
|
||
const createMockedHandler = () => { | ||
const next = jest.fn(); | ||
const end = jest.fn(); | ||
const getCaveat = jest.fn().mockReturnValue({ | ||
value: { | ||
requiredScopes: { | ||
'eip155:1': { | ||
methods: ['eth_call'], | ||
notifications: [], | ||
}, | ||
'eip155:5': { | ||
methods: ['eth_chainId'], | ||
notifications: [], | ||
}, | ||
}, | ||
optionalScopes: { | ||
'eip155:1': { | ||
methods: ['net_version'], | ||
notifications: ['chainChanged'], | ||
}, | ||
wallet: { | ||
methods: ['wallet_watchAsset'], | ||
notifications: [], | ||
}, | ||
}, | ||
}, | ||
}); | ||
const response = {}; | ||
const handler = (request) => | ||
walletGetSessionHandler(request, response, next, end, { | ||
getCaveat, | ||
}); | ||
|
||
return { | ||
next, | ||
response, | ||
end, | ||
getCaveat, | ||
handler, | ||
}; | ||
}; | ||
|
||
describe('wallet_getSession', () => { | ||
it('throws an error when sessionId param is specified', async () => { | ||
const { handler, end } = createMockedHandler(); | ||
await handler({ | ||
...baseRequest, | ||
params: { | ||
sessionId: '0xdeadbeef', | ||
}, | ||
}); | ||
expect(end).toHaveBeenCalledWith( | ||
new EthereumRpcError(5500, 'SessionId not recognized'), | ||
); | ||
}); | ||
|
||
it('gets the authorized scopes from the CAIP-25 endowement permission', async () => { | ||
const { handler, getCaveat } = createMockedHandler(); | ||
|
||
await handler(baseRequest); | ||
expect(getCaveat).toHaveBeenCalledWith( | ||
'http://test.com', | ||
Caip25EndowmentPermissionName, | ||
Caip25CaveatType, | ||
); | ||
}); | ||
|
||
it('throws an error if the CAIP-25 endowement permission does not exist', async () => { | ||
const { handler, getCaveat, end } = createMockedHandler(); | ||
getCaveat.mockReturnValue(null); | ||
|
||
await handler(baseRequest); | ||
expect(end).toHaveBeenCalledWith( | ||
new EthereumRpcError(5501, 'No active sessions'), | ||
); | ||
}); | ||
|
||
it('returns the merged scopes', async () => { | ||
const { handler, response } = createMockedHandler(); | ||
|
||
await handler(baseRequest); | ||
expect(response.result).toStrictEqual({ | ||
sessionScopes: { | ||
'eip155:1': { | ||
methods: ['eth_call', 'net_version'], | ||
notifications: ['chainChanged'], | ||
}, | ||
'eip155:5': { | ||
methods: ['eth_chainId'], | ||
notifications: [], | ||
}, | ||
wallet: { | ||
methods: ['wallet_watchAsset'], | ||
notifications: [], | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
PermissionDoesNotExistError, | ||
UnrecognizedSubjectError, | ||
} from '@metamask/permission-controller'; | ||
import { EthereumRpcError } from 'eth-rpc-errors'; | ||
import { Caip25EndowmentPermissionName } from './caip25permissions'; | ||
|
||
export async function walletRevokeSessionHandler( | ||
request, | ||
response, | ||
_next, | ||
end, | ||
hooks, | ||
) { | ||
if (request.params?.sessionId) { | ||
return end( | ||
new EthereumRpcError(5500, 'SessionId not recognized'), // we aren't currently storing a sessionId to check this against | ||
); | ||
} | ||
|
||
try { | ||
hooks.revokePermission(request.origin, Caip25EndowmentPermissionName); | ||
} catch (err) { | ||
if ( | ||
err instanceof UnrecognizedSubjectError || | ||
err instanceof PermissionDoesNotExistError | ||
) { | ||
return end(new EthereumRpcError(5501, 'No active sessions')); | ||
} | ||
|
||
return end(err); // TODO: handle this better | ||
} | ||
|
||
response.result = true; | ||
return end(); | ||
} |
Oops, something went wrong.