-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into jl/fix-caip25-remove-scope-mutator
- Loading branch information
Showing
30 changed files
with
3,293 additions
and
30 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
135 changes: 135 additions & 0 deletions
135
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,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'], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
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,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, | ||
}; | ||
}, | ||
); | ||
|
||
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] ?? []; | ||
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), | ||
); | ||
}; |
Oops, something went wrong.