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

feat: Add Multichain API to @metamask/multichain #4813

Open
wants to merge 202 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
202 commits
Select commit Hold shift + click to select a range
bc56104
Initial package
jiexi Oct 10, 2024
a4b52ad
Fix caip25Permission
jiexi Oct 10, 2024
86ab58a
scopes
jiexi Oct 10, 2024
186d4b3
Fix middlewares
jiexi Oct 10, 2024
6a93d1d
Fix adapters except permission middleware
jiexi Oct 10, 2024
231bcaa
Rename handlers js to ts
jiexi Oct 10, 2024
4e52fc0
permission middleware test js ts rename
jiexi Oct 10, 2024
151eaac
fix: typescript + linting (#4788)
shanejonas Oct 11, 2024
85a723e
Added exports for multichain package (#4789)
shanejonas Oct 11, 2024
a0bb278
Add requires
jiexi Oct 11, 2024
1cd8ef2
add permittedChains adapter to exports
jiexi Oct 11, 2024
c636def
kill scope index barrel
jiexi Oct 11, 2024
a6aa7c1
barrel
jiexi Oct 14, 2024
42134f7
Merge branch 'main' into caip-multichain
jiexi Oct 14, 2024
a13b9c7
remove subjectTypes from CAIP-25 permission
jiexi Oct 14, 2024
81db877
update eth accounts adapter with empty wallet and wallet:eip155 Scope…
jiexi Oct 14, 2024
a3fc263
fix caip25permission spec
jiexi Oct 14, 2024
afe5cb9
upsert empty wallet:eip155 when setting permittedChains
jiexi Oct 14, 2024
9ea78be
lint
jiexi Oct 14, 2024
5650d31
Rename scope.test.ts to types.test.ts
jiexi Oct 14, 2024
df5167e
add networkClientId type to permission-adapter-middleware
jiexi Oct 15, 2024
9446b0b
fix snapshot
jiexi Oct 15, 2024
f65ca24
Merge branch 'main' into caip-multichain
jiexi Oct 15, 2024
941849d
add KnownWalletScopeString enum
jiexi Oct 15, 2024
a250de2
stop upserting wallet scope in setEthAccounts()
jiexi Oct 15, 2024
aa7ba39
lint
jiexi Oct 15, 2024
8415be6
update removeScope mutator to not revoke permission if requiredScope …
jiexi Oct 15, 2024
c7f510e
Fix specificationBuilder jsdoc and typing
jiexi Oct 15, 2024
4afa8f8
Fix caip25permission type
jiexi Oct 15, 2024
597e837
Fix caip25permission type
jiexi Oct 15, 2024
1872188
small dry todo fix
adonesky1 Oct 15, 2024
c5d8005
another small dry
adonesky1 Oct 15, 2024
54f7c49
Added handler wrappers to better integrate with existing middleware (…
shanejonas Oct 15, 2024
ada451d
change subscriptionManager require to import
jiexi Oct 15, 2024
2322e39
lint
jiexi Oct 15, 2024
9b21677
Merge branch 'main' into caip-multichain
jiexi Oct 16, 2024
00a41cb
Jl/caip multichain/update scope object account types (#4803)
jiexi Oct 16, 2024
f30dd8b
update @metamask/rpc-errors version
adonesky1 Oct 17, 2024
71085a0
fix merge
adonesky1 Oct 17, 2024
f2be8f0
Merge branch 'main' into caip-multichain
jiexi Oct 17, 2024
644f4d0
Add isEqualCaseInsensitive to controller-utils
jiexi Oct 17, 2024
1b78bbc
create initial multichain package
jiexi Oct 17, 2024
fc28c89
remove API related logic
jiexi Oct 17, 2024
ba25ce5
Merge branch 'initialize-caip-multichain' into caip-multichain
jiexi Oct 17, 2024
4ceb118
remove api related deps
jiexi Oct 17, 2024
a36ec4d
Revert "remove API related logic"
jiexi Oct 17, 2024
95dfbc9
Revert "remove api related deps"
jiexi Oct 17, 2024
9bcf869
Fix package.json
jiexi Oct 17, 2024
633485c
Merge branch 'initialize-caip-multichain' into caip-multichain
jiexi Oct 17, 2024
609518a
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 17, 2024
afb2e2c
fix bad merge
jiexi Oct 17, 2024
7c97afe
fix bad merge
jiexi Oct 17, 2024
ba210ac
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 17, 2024
109a7bf
Update packages/controller-utils/src/util.test.ts
jiexi Oct 17, 2024
1e67641
Merge branch 'controller-utils-isEqualCaseInsensitive' into initializ…
jiexi Oct 17, 2024
32b213a
Merge branch 'initialize-caip-multichain' into caip-multichain
jiexi Oct 17, 2024
dae4f73
add account support check in validator (#4816)
jiexi Oct 17, 2024
b673f62
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 17, 2024
d7ed999
Merge branch 'main' into initialize-caip-multichain
jiexi Oct 21, 2024
b4b65e2
Merge branch 'initialize-caip-multichain' into caip-multichain
jiexi Oct 21, 2024
4d752b7
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
fad338d
remove caip-permission-adapter-middleware
jiexi Oct 21, 2024
5bf15a2
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
8208931
Revert "remove caip-permission-adapter-middleware"
jiexi Oct 21, 2024
c5980c6
remove caip-permission-adapter-middleware from exports
jiexi Oct 21, 2024
106e986
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
5c6bb8f
Revert "remove caip-permission-adapter-middleware from exports"
jiexi Oct 21, 2024
a7a8e6e
remove types/@metamask/eth-json-rpc-filters.d.ts
jiexi Oct 21, 2024
0962fb2
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
54829e0
Revert "remove types/@metamask/eth-json-rpc-filters.d.ts"
jiexi Oct 21, 2024
6b367f0
Merge branch 'main' into caip-multichain
adonesky1 Oct 21, 2024
42297f9
add back readme content
adonesky1 Oct 21, 2024
dc31704
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
56176d1
yarn lock
jiexi Oct 21, 2024
26acfcd
Merge branch 'main' into initialize-caip-multichain
jiexi Oct 21, 2024
2e3bfb4
Merge branch 'initialize-caip-multichain' into caip-multichain
jiexi Oct 21, 2024
89d6101
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Oct 21, 2024
b7ea5d5
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 21, 2024
7203358
bump network-controller dep
jiexi Oct 22, 2024
046133a
add Scoped Properties to Caip25Authorization type
jiexi Oct 22, 2024
eea9f46
Merge branch 'main' into caip-multichain
jiexi Oct 22, 2024
257618a
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 22, 2024
67b222e
Fix ScopedProperties type
jiexi Oct 22, 2024
ead2f43
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 22, 2024
009fb4a
Loosen get adapter param types (#4835)
jiexi Oct 22, 2024
e34032b
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 22, 2024
0631c48
Seperate types and constants
jiexi Oct 23, 2024
4685547
Merge branch 'main' into caip-multichain
jiexi Oct 23, 2024
18d1112
remove jest.resetAllMocks
jiexi Oct 23, 2024
1ba77c7
yarn lock
jiexi Oct 23, 2024
bb50a19
yarn
jiexi Oct 23, 2024
7d32a0b
100% coverage
jiexi Oct 23, 2024
f095370
Remove accountsChanged and chainChanged from KnownNotifications (sinc…
jiexi Oct 23, 2024
62929a4
Merge branch 'main' into caip-multichain
jiexi Oct 23, 2024
491552f
Update packages/multichain/src/scope/transform.test.ts
jiexi Oct 24, 2024
bd0f50f
Merge branch 'main' into caip-multichain
jiexi Oct 24, 2024
41dcc83
Merge branch 'caip-multichain' into caip-multichain-api
jiexi Oct 24, 2024
d80c9ce
Merge branch 'main' into caip-multichain-api
jiexi Nov 21, 2024
6eb3e87
Merge branch 'main' into caip-multichain-api
jiexi Nov 21, 2024
8374f37
Merge branch 'main' into caip-multichain-api
jiexi Nov 21, 2024
d6dfc71
Merge branch 'main' into caip-multichain-api
jiexi Nov 21, 2024
0d167a3
CAIP Multichain API with permission refactor changes (#4961)
jiexi Nov 21, 2024
17af395
export caipPermissionAdapterMiddleware
jiexi Nov 21, 2024
62975b6
Caip multichain api normalized to internal (#4964)
jiexi Nov 21, 2024
19a5983
Filter out eip1193 only methods from Eip155Methods
jiexi Nov 22, 2024
439ef9b
Merge branch 'main' into caip-multichain-api
jiexi Nov 22, 2024
7d61af6
export Eip1193OnlyMethods
jiexi Nov 22, 2024
b5d0cd7
Merge branch 'main' into caip-multichain-api
jiexi Nov 22, 2024
97b4a09
Revert "export Eip1193OnlyMethods"
jiexi Nov 22, 2024
6732cd9
use Eip1193OnlyMethods in adapter middleware
jiexi Nov 22, 2024
e03033c
expand Eip1193OnlyMethods to include meta methods
jiexi Nov 22, 2024
e1793ff
cleanup + add tests for MultichainSubscriptionManager
adonesky1 Nov 22, 2024
4c9be0e
adding tests for multichainMethodCallValidator
adonesky1 Nov 22, 2024
a25c05d
cleanup
adonesky1 Nov 22, 2024
b35bdc3
lint
adonesky1 Nov 22, 2024
2ad05a3
question
adonesky1 Nov 22, 2024
79acd26
small cleanup
adonesky1 Nov 22, 2024
32aa807
more cleanup
adonesky1 Nov 22, 2024
121ba94
remove lingering console
adonesky1 Nov 25, 2024
6d4bd04
this seems like a reasonable tweak (#4980)
adonesky1 Nov 26, 2024
3f4c297
Merge branch 'main' into caip-multichain-api
adonesky1 Nov 26, 2024
5a621b3
100% test coverage
adonesky1 Nov 26, 2024
aef155f
Merge branch 'main' into caip-multichain-api
adonesky1 Nov 26, 2024
944be53
Merge branch 'main' into caip-multichain-api
adonesky1 Nov 26, 2024
5f98801
Merge branch 'main' into caip-multichain-api
jiexi Nov 26, 2024
b79f827
Merge branch 'main' into caip-multichain-api
jiexi Nov 27, 2024
242629d
Merge branch 'main' into caip-multichain-api
jiexi Dec 2, 2024
57e6ab5
Override subscriptionManager.middleware.destroy to also remove sub en…
jiexi Dec 2, 2024
0007fa5
Merge branch 'main' into caip-multichain-api
jiexi Dec 5, 2024
1ec9503
stop exporting unused helpers
jiexi Dec 5, 2024
02144d1
remove more unused exports
jiexi Dec 5, 2024
78737cb
remove more unused exports
jiexi Dec 5, 2024
f8ca898
fix snapshot
jiexi Dec 5, 2024
e8b2487
align json-schema version
jiexi Dec 5, 2024
9cfbf21
remove caip lifecycle method param types (#5031)
jiexi Dec 5, 2024
ca2b343
Add jsdoc
jiexi Dec 5, 2024
507093e
Merge branch 'main' into caip-multichain-api
jiexi Dec 6, 2024
5f96567
Jl/caip multichain api/drop unsupported methods notifications (#5039)
jiexi Dec 6, 2024
40b7757
Merge branch 'main' into caip-multichain-api
jiexi Dec 10, 2024
2a983d2
Merge branch 'main' into caip-multichain-api
jiexi Dec 16, 2024
4542942
Merge branch 'main' into caip-multichain-api
jiexi Dec 17, 2024
b6765a4
Update packages/multichain/src/adapters/caip-permission-adapter-middl…
jiexi Dec 17, 2024
e6a3346
Update packages/multichain/src/middlewares/multichainMethodCallValida…
jiexi Dec 17, 2024
c0637f0
Update packages/multichain/src/handlers/wallet-getSession.test.ts
jiexi Dec 17, 2024
d8d90ce
Update packages/multichain/src/handlers/wallet-invokeMethod.test.ts
jiexi Dec 17, 2024
c102e37
Update packages/multichain/src/scope/filter.test.ts
jiexi Dec 17, 2024
3d68f83
Update packages/multichain/src/adapters/caip-permission-adapter-middl…
jiexi Dec 17, 2024
d997b82
Update packages/multichain/src/handlers/wallet-getSession.ts
jiexi Dec 17, 2024
a3e9d8f
Update packages/multichain/src/handlers/wallet-invokeMethod.ts
jiexi Dec 17, 2024
7f11b8a
Update packages/multichain/src/handlers/wallet-revokeSession.ts
jiexi Dec 17, 2024
a551ba2
Merge branch 'main' into caip-multichain-api
jiexi Dec 17, 2024
6f61047
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Dec 17, 2024
f94c4f6
Improve wallet_getSession, invokeSession, revokeSession jsdoc
jiexi Dec 17, 2024
d1aad26
Update packages/multichain/src/handlers/wallet-invokeMethod.ts
jiexi Dec 17, 2024
da3fb04
Fix walletInvokeMethodHandler type
jiexi Dec 17, 2024
4a7ae5d
Update packages/multichain/src/handlers/wallet-invokeMethod.ts
jiexi Dec 17, 2024
c54714f
Update packages/multichain/src/middlewares/MultichainMiddlewareManage…
jiexi Dec 17, 2024
840d27d
add MultichainMiddlewareManager desc
jiexi Dec 17, 2024
8874ebc
add MultichainSubscriptionManager desc
jiexi Dec 17, 2024
c344d3d
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Dec 17, 2024
ff57ba3
rename onNotification to notify
jiexi Dec 17, 2024
dc0283e
rename filter helpers
jiexi Dec 17, 2024
b439461
Update packages/multichain/src/middlewares/multichainMethodCallValida…
jiexi Dec 17, 2024
e77e87f
cleanup multichainMethodCallValidator
jiexi Dec 17, 2024
5a2c8a0
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Dec 17, 2024
3f65e22
Add walletInvokeMethodRequest type
jiexi Dec 17, 2024
1399f97
Add MultichainMiddlewareManager.test.ts to get back to 100%
jiexi Dec 17, 2024
0fd56ce
fix type
jiexi Dec 17, 2024
bf4e136
fix types. Ignore linter prefer-spy-on
jiexi Dec 17, 2024
5c79a54
Merge branch 'main' into caip-multichain-api
jiexi Jan 6, 2025
7c4aa65
align package versions
jiexi Jan 6, 2025
4ad37a9
fix cross-spawn
jiexi Jan 8, 2025
c760400
Merge branch 'main' into caip-multichain-api
jiexi Jan 8, 2025
43d1dcd
hexToBigInt
jiexi Jan 8, 2025
61c654a
lint
jiexi Jan 8, 2025
c1dd11f
add to MultichainMiddlewareManager description
adonesky1 Jan 10, 2025
2c085fe
Merge branch 'main' into caip-multichain-api
jiexi Jan 10, 2025
f550ee4
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Jan 10, 2025
0353672
remove getSupportedScopes
jiexi Jan 10, 2025
fa5b27f
improve testing of nested errors in multichainMethodCallValidator (#5…
adonesky1 Jan 10, 2025
cf554f8
yarn dedupe
jiexi Jan 10, 2025
298fc54
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Jan 10, 2025
d07f372
remove cross-spawn resolution
jiexi Jan 13, 2025
b0b350f
Merge branch 'main' into caip-multichain-api
jiexi Jan 13, 2025
1c0cc44
Merge branch 'main' into caip-multichain-api
jiexi Jan 14, 2025
07d969c
Replace revokePermission hook with revokePermissionForOrigin bound hook
jiexi Jan 14, 2025
e3def86
Merge branch 'main' into caip-multichain-api
jiexi Jan 14, 2025
e05b7d3
replace getCaveat with getCaveatForOrigin
jiexi Jan 14, 2025
10158bd
replace getCaveat with getCaveatForOrigin (missed one)
jiexi Jan 14, 2025
f371565
Merge branch 'main' into caip-multichain-api
jiexi Jan 14, 2025
b57d706
Fix wallet-revokeSession hookNames
jiexi Jan 14, 2025
6244b7b
Merge branch 'main' into caip-multichain-api
jiexi Jan 14, 2025
93683d1
update thresholds
jiexi Jan 15, 2025
1a87b4d
remove adapter middleware
jiexi Jan 15, 2025
96f372c
Update packages/multichain/src/scope/authorization.test.ts
jiexi Jan 15, 2025
8f02d61
Fix snapshot
jiexi Jan 15, 2025
da7aed7
Merge branch 'main' into caip-multichain-api
jiexi Jan 15, 2025
0cc5cc6
Merge remote-tracking branch 'origin/caip-multichain-api' into caip-m…
jiexi Jan 15, 2025
20588ef
yarn lock
jiexi Jan 15, 2025
e595d48
Merge branch 'main' into caip-multichain-api
jiexi Jan 15, 2025
a32a95b
eslint thresholds
jiexi Jan 15, 2025
36449e9
fix coverage
jiexi Jan 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
"@typescript-eslint/no-base-to-string": 3,
"@typescript-eslint/no-duplicate-enum-values": 2,
"@typescript-eslint/no-unsafe-enum-comparison": 34,
"@typescript-eslint/no-unused-vars": 36,
"@typescript-eslint/prefer-promise-reject-errors": 13,
"@typescript-eslint/prefer-readonly": 145,
"@typescript-eslint/no-unused-vars": 41,
"@typescript-eslint/prefer-promise-reject-errors": 33,
"@typescript-eslint/prefer-readonly": 147,
"@typescript-eslint/switch-exhaustiveness-check": 0,
"import-x/namespace": 189,
"import-x/no-named-as-default": 1,
"import-x/no-named-as-default-member": 8,
"import-x/order": 205,
"jest/no-conditional-in-test": 129,
"import-x/order": 211,
"jest/no-conditional-in-test": 138,
"jest/prefer-lowercase-title": 2,
"jest/prefer-strict-equal": 2,
"jsdoc/check-tag-names": 375,
"jsdoc/require-returns": 22,
"jsdoc/tag-lines": 328,
"jsdoc/require-returns": 25,
"jsdoc/tag-lines": 335,
"n/no-unsupported-features/node-builtins": 18,
"n/prefer-global/text-encoder": 4,
"n/prefer-global/text-decoder": 4,
"prettier/prettier": 115,
"prettier/prettier": 118,
"promise/always-return": 3,
"promise/catch-or-return": 2,
"promise/param-names": 8,
Expand Down
5 changes: 5 additions & 0 deletions packages/multichain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@
"@metamask/controller-utils": "^11.4.5",
"@metamask/eth-json-rpc-filters": "^9.0.0",
"@metamask/rpc-errors": "^7.0.2",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^11.0.1",
"@open-rpc/schema-utils-js": "^2.0.5",
"jsonschema": "^1.4.1",
"lodash": "^4.17.21"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@metamask/json-rpc-engine": "^10.0.2",
"@metamask/network-controller": "^22.1.1",
"@metamask/permission-controller": "^11.0.5",
"@open-rpc/meta-schema": "^1.14.6",
"@types/jest": "^27.4.1",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toHex } from '@metamask/controller-utils';
import type { Hex } from '@metamask/utils';
import { KnownCaipNamespace } from '@metamask/utils';
import { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';

import type { Caip25CaveatValue } from '../caip25Permission';
import { getUniqueArrayItems } from '../scope/transform';
Expand Down Expand Up @@ -57,7 +57,7 @@ export const addPermittedEthChainId = (
caip25CaveatValue: Caip25CaveatValue,
chainId: Hex,
): Caip25CaveatValue => {
const scopeString = `eip155:${parseInt(chainId, 16)}`;
const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;
if (
Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||
Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
KnownNotifications,
KnownRpcMethods,
KnownWalletNamespaceRpcMethods,
KnownWalletRpcMethods,
} from '../scope/constants';
import {
getInternalScopesObject,
getSessionScopes,
} from './caip-permission-adapter-session-scopes';

describe('CAIP-25 session scopes adapters', () => {
describe('getInternalScopesObject', () => {
it('returns an InternalScopesObject with only the accounts from each NormalizedScopeObject', () => {
const result = getInternalScopesObject({
'wallet:eip155': {
methods: ['foo', 'bar'],
notifications: ['baz'],
accounts: ['wallet:eip155:0xdead'],
},
'eip155:1': {
methods: ['eth_call'],
notifications: ['eth_subscription'],
accounts: ['eip155:1:0xdead', 'eip155:1:0xbeef'],
},
});

expect(result).toStrictEqual({
'wallet:eip155': {
accounts: ['wallet:eip155:0xdead'],
},
'eip155:1': {
accounts: ['eip155:1:0xdead', 'eip155:1:0xbeef'],
},
});
});
});

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 with empty methods and notifications for scope not wallet namespace and unknown reference', () => {
const result = getSessionScopes({
requiredScopes: {},
optionalScopes: {
'foo:1': {
accounts: ['foo:1:0xdeadbeef'],
},
},
});

expect(result).toStrictEqual({
'foo:1': {
methods: [],
notifications: [],
accounts: ['foo:1: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'],
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 NormalizedScopesObject to a InternalScopesObject.
* @param normalizedScopesObject - The NormalizedScopesObject to convert.
* @returns An InternalScopesObject.
*/
export const getInternalScopesObject = (
normalizedScopesObject: NormalizedScopesObject,
) => {
const internalScopes: InternalScopesObject = {};

Object.entries(normalizedScopesObject).forEach(
([_scopeString, { accounts }]) => {
const scopeString = _scopeString as keyof typeof normalizedScopesObject;

internalScopes[scopeString] = {
accounts,
Copy link
Member

Choose a reason for hiding this comment

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

Wait, so we are throwing away all of the methods etc? What is the plan then if we want to do more granular permissioning down the line, then we will require another potentially tricky migration?

Copy link
Contributor

Choose a reason for hiding this comment

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

So we originally were including the methods in the serialized permission itself (and granularly enforcing each metho), but after discussions w/ @Gudahtt we realized that approach would

  1. actually require a migration for each and any new methods added.
  2. doesn't really match how we represent permissions (in connections flows) to users. We don't represent good "buckets" of types of permissions currently. We considered creating these kinds of buckets as we removed the individual methods but decided we probably weren't going to get it exactly right the first time and so would probably need a later migration no matter what. Getting these "buckets" right requires more in depth discussion and we didn't want to make that a blocker for this work

};
},
);

return internalScopes;
};

/**
* 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] ?? [];
Copy link
Member

Choose a reason for hiding this comment

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

Seems incompatible with non-EVM as well? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

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),
);
};
Loading
Loading