-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CAIP Multichain API with permission refactor changes (#4961)
## Explanation ~~This diff is a bit messy since it also brings in the [core PR that refactors the CAIP-25 permission](#4950) which the [caip-multichain-api](https://github.com/MetaMask/core/tree/caip-multichain-api) branch doesn't have yet.~~ The main thing this PR does is adds `getSessionScopes()` which massages the new CAIP-25 permission into a `NormalizedScopesObject` ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## 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/package-a` - **<CATEGORY>**: Your change here - **<CATEGORY>**: Your change here ### `@metamask/package-b` - **<CATEGORY>**: Your change here - **<CATEGORY>**: Your change here ## 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 - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes --------- Co-authored-by: Alex Donesky <adonesky@gmail.com>
- Loading branch information
Showing
17 changed files
with
393 additions
and
79 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
87 changes: 87 additions & 0 deletions
87
packages/multichain/src/adapters/caip-permission-adapter-session-scopes.test.ts
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,87 @@ | ||
import { | ||
KnownNotifications, | ||
KnownRpcMethods, | ||
KnownWalletNamespaceRpcMethods, | ||
KnownWalletRpcMethods, | ||
} from '../scope/constants'; | ||
import { getSessionScopes } from './caip-permission-adapter-session-scopes'; | ||
|
||
describe('CAIP-25 session scopes adapters', () => { | ||
describe('getSessionScopes', () => { | ||
it('returns a NormalizedScopesObject for the wallet scope', () => { | ||
const result = getSessionScopes({ | ||
requiredScopes: {}, | ||
optionalScopes: { | ||
wallet: { | ||
accounts: [], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result).toStrictEqual({ | ||
wallet: { | ||
methods: KnownWalletRpcMethods, | ||
notifications: [], | ||
accounts: [], | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a NormalizedScopesObject for the wallet:eip155 scope', () => { | ||
const result = getSessionScopes({ | ||
requiredScopes: {}, | ||
optionalScopes: { | ||
'wallet:eip155': { | ||
accounts: ['wallet:eip155:0xdeadbeef'], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result).toStrictEqual({ | ||
'wallet:eip155': { | ||
methods: KnownWalletNamespaceRpcMethods.eip155, | ||
notifications: [], | ||
accounts: ['wallet:eip155:0xdeadbeef'], | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a NormalizedScopesObject with empty methods and notifications for scope with wallet namespace and unknown reference', () => { | ||
const result = getSessionScopes({ | ||
requiredScopes: {}, | ||
optionalScopes: { | ||
'wallet:foobar': { | ||
accounts: ['wallet:foobar:0xdeadbeef'], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result).toStrictEqual({ | ||
'wallet:foobar': { | ||
methods: [], | ||
notifications: [], | ||
accounts: ['wallet:foobar:0xdeadbeef'], | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a NormalizedScopesObject for a eip155 namespaced scope', () => { | ||
const result = getSessionScopes({ | ||
requiredScopes: {}, | ||
optionalScopes: { | ||
'eip155:1': { | ||
accounts: ['eip155:1:0xdeadbeef'], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result).toStrictEqual({ | ||
'eip155:1': { | ||
methods: KnownRpcMethods.eip155, | ||
notifications: KnownNotifications.eip155, | ||
accounts: ['eip155:1:0xdeadbeef'], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
packages/multichain/src/adapters/caip-permission-adapter-session-scopes.ts
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,78 @@ | ||
import { KnownCaipNamespace } from '@metamask/utils'; | ||
|
||
import type { Caip25CaveatValue } from '../caip25Permission'; | ||
import { | ||
KnownNotifications, | ||
KnownRpcMethods, | ||
KnownWalletNamespaceRpcMethods, | ||
KnownWalletRpcMethods, | ||
} from '../scope/constants'; | ||
import { mergeScopes } from '../scope/transform'; | ||
import type { | ||
InternalScopesObject, | ||
NonWalletKnownCaipNamespace, | ||
NormalizedScopesObject, | ||
} from '../scope/types'; | ||
import { parseScopeString } from '../scope/types'; | ||
|
||
/** | ||
* Converts an InternalScopesObject to a NormalizedScopesObject. | ||
* @param internalScopesObject - The InternalScopesObject to convert. | ||
* @returns A NormalizedScopesObject. | ||
*/ | ||
const getNormalizedScopesObject = ( | ||
internalScopesObject: InternalScopesObject, | ||
) => { | ||
const normalizedScopes: NormalizedScopesObject = {}; | ||
|
||
Object.entries(internalScopesObject).forEach( | ||
([_scopeString, { accounts }]) => { | ||
const scopeString = _scopeString as keyof typeof internalScopesObject; | ||
const { namespace, reference } = parseScopeString(scopeString); | ||
let methods: string[] = []; | ||
let notifications: string[] = []; | ||
|
||
if (namespace === KnownCaipNamespace.Wallet) { | ||
if (reference) { | ||
methods = | ||
KnownWalletNamespaceRpcMethods[ | ||
reference as NonWalletKnownCaipNamespace | ||
] ?? []; | ||
} else { | ||
methods = KnownWalletRpcMethods; | ||
} | ||
} else { | ||
methods = | ||
KnownRpcMethods[namespace as NonWalletKnownCaipNamespace] ?? []; | ||
notifications = | ||
KnownNotifications[namespace as NonWalletKnownCaipNamespace] ?? []; | ||
} | ||
|
||
normalizedScopes[scopeString] = { | ||
methods, | ||
notifications, | ||
accounts, | ||
}; | ||
}, | ||
); | ||
|
||
return normalizedScopes; | ||
}; | ||
|
||
/** | ||
* Takes the scopes from an endowment:caip25 permission caveat value, | ||
* hydrates them with supported methods and notifications, and returns a NormalizedScopesObject. | ||
* @param caip25CaveatValue - The CAIP-25 CaveatValue to convert. | ||
* @returns A NormalizedScopesObject. | ||
*/ | ||
export const getSessionScopes = ( | ||
caip25CaveatValue: Pick< | ||
Caip25CaveatValue, | ||
'requiredScopes' | 'optionalScopes' | ||
>, | ||
) => { | ||
return mergeScopes( | ||
getNormalizedScopesObject(caip25CaveatValue.requiredScopes), | ||
getNormalizedScopesObject(caip25CaveatValue.optionalScopes), | ||
); | ||
}; |
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
Oops, something went wrong.