@metamask-previews/multichain-api-middleware 4.0.3-preview-c61613ed2 → 4.0.3-preview-3866c0ff1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/handlers/index.d.ts +25 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/{index.mjs → index.js} +5 -5
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/{types.d.cts → types.d.ts} +5 -5
- package/dist/handlers/types.d.ts.map +1 -0
- package/dist/handlers/{types.mjs → types.js} +1 -2
- package/dist/handlers/types.js.map +1 -0
- package/dist/handlers/{wallet-createSession.d.cts → wallet-createSession.d.ts} +9 -9
- package/dist/handlers/wallet-createSession.d.ts.map +1 -0
- package/dist/handlers/{wallet-createSession.mjs → wallet-createSession.js} +6 -6
- package/dist/handlers/wallet-createSession.js.map +1 -0
- package/dist/handlers/{wallet-getSession.d.mts → wallet-getSession.d.ts} +5 -5
- package/dist/handlers/wallet-getSession.d.ts.map +1 -0
- package/dist/handlers/{wallet-getSession.mjs → wallet-getSession.js} +2 -2
- package/dist/handlers/wallet-getSession.js.map +1 -0
- package/dist/handlers/{wallet-invokeMethod.d.mts → wallet-invokeMethod.d.ts} +7 -7
- package/dist/handlers/wallet-invokeMethod.d.ts.map +1 -0
- package/dist/handlers/{wallet-invokeMethod.mjs → wallet-invokeMethod.js} +4 -4
- package/dist/handlers/wallet-invokeMethod.js.map +1 -0
- package/dist/handlers/{wallet-revokeSession.d.mts → wallet-revokeSession.d.ts} +6 -6
- package/dist/handlers/wallet-revokeSession.d.ts.map +1 -0
- package/dist/handlers/{wallet-revokeSession.mjs → wallet-revokeSession.js} +5 -5
- package/dist/handlers/wallet-revokeSession.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares/{MultichainMiddlewareManager.d.cts → MultichainMiddlewareManager.d.ts} +4 -4
- package/dist/middlewares/MultichainMiddlewareManager.d.ts.map +1 -0
- package/dist/middlewares/MultichainMiddlewareManager.js +93 -0
- package/dist/middlewares/MultichainMiddlewareManager.js.map +1 -0
- package/dist/middlewares/{MultichainSubscriptionManager.d.cts → MultichainSubscriptionManager.d.ts} +6 -6
- package/dist/middlewares/MultichainSubscriptionManager.d.ts.map +1 -0
- package/dist/middlewares/MultichainSubscriptionManager.js +100 -0
- package/dist/middlewares/MultichainSubscriptionManager.js.map +1 -0
- package/dist/middlewares/{multichainMethodCallValidatorMiddleware.d.cts → multichainMethodCallValidatorMiddleware.d.ts} +2 -2
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.ts.map +1 -0
- package/dist/middlewares/{multichainMethodCallValidatorMiddleware.mjs → multichainMethodCallValidatorMiddleware.js} +8 -15
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.js.map +1 -0
- package/package.json +13 -17
- package/dist/handlers/index.cjs +0 -20
- package/dist/handlers/index.cjs.map +0 -1
- package/dist/handlers/index.d.cts +0 -25
- package/dist/handlers/index.d.cts.map +0 -1
- package/dist/handlers/index.d.mts +0 -25
- package/dist/handlers/index.d.mts.map +0 -1
- package/dist/handlers/index.mjs.map +0 -1
- package/dist/handlers/types.cjs +0 -13
- package/dist/handlers/types.cjs.map +0 -1
- package/dist/handlers/types.d.cts.map +0 -1
- package/dist/handlers/types.d.mts +0 -27
- package/dist/handlers/types.d.mts.map +0 -1
- package/dist/handlers/types.mjs.map +0 -1
- package/dist/handlers/wallet-createSession.cjs +0 -179
- package/dist/handlers/wallet-createSession.cjs.map +0 -1
- package/dist/handlers/wallet-createSession.d.cts.map +0 -1
- package/dist/handlers/wallet-createSession.d.mts +0 -70
- package/dist/handlers/wallet-createSession.d.mts.map +0 -1
- package/dist/handlers/wallet-createSession.mjs.map +0 -1
- package/dist/handlers/wallet-getSession.cjs +0 -54
- package/dist/handlers/wallet-getSession.cjs.map +0 -1
- package/dist/handlers/wallet-getSession.d.cts +0 -43
- package/dist/handlers/wallet-getSession.d.cts.map +0 -1
- package/dist/handlers/wallet-getSession.d.mts.map +0 -1
- package/dist/handlers/wallet-getSession.mjs.map +0 -1
- package/dist/handlers/wallet-invokeMethod.cjs +0 -103
- package/dist/handlers/wallet-invokeMethod.cjs.map +0 -1
- package/dist/handlers/wallet-invokeMethod.d.cts +0 -57
- package/dist/handlers/wallet-invokeMethod.d.cts.map +0 -1
- package/dist/handlers/wallet-invokeMethod.d.mts.map +0 -1
- package/dist/handlers/wallet-invokeMethod.mjs.map +0 -1
- package/dist/handlers/wallet-revokeSession.cjs +0 -106
- package/dist/handlers/wallet-revokeSession.cjs.map +0 -1
- package/dist/handlers/wallet-revokeSession.d.cts +0 -45
- package/dist/handlers/wallet-revokeSession.d.cts.map +0 -1
- package/dist/handlers/wallet-revokeSession.d.mts.map +0 -1
- package/dist/handlers/wallet-revokeSession.mjs.map +0 -1
- package/dist/index.cjs +0 -14
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -7
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -7
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -6
- package/dist/index.mjs.map +0 -1
- package/dist/middlewares/MultichainMiddlewareManager.cjs +0 -110
- package/dist/middlewares/MultichainMiddlewareManager.cjs.map +0 -1
- package/dist/middlewares/MultichainMiddlewareManager.d.cts.map +0 -1
- package/dist/middlewares/MultichainMiddlewareManager.d.mts +0 -35
- package/dist/middlewares/MultichainMiddlewareManager.d.mts.map +0 -1
- package/dist/middlewares/MultichainMiddlewareManager.mjs +0 -106
- package/dist/middlewares/MultichainMiddlewareManager.mjs.map +0 -1
- package/dist/middlewares/MultichainSubscriptionManager.cjs +0 -118
- package/dist/middlewares/MultichainSubscriptionManager.cjs.map +0 -1
- package/dist/middlewares/MultichainSubscriptionManager.d.cts.map +0 -1
- package/dist/middlewares/MultichainSubscriptionManager.d.mts +0 -49
- package/dist/middlewares/MultichainSubscriptionManager.d.mts.map +0 -1
- package/dist/middlewares/MultichainSubscriptionManager.mjs +0 -118
- package/dist/middlewares/MultichainSubscriptionManager.mjs.map +0 -1
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.cjs +0 -74
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.cjs.map +0 -1
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.cts.map +0 -1
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.mts +0 -7
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.mts.map +0 -1
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- **BREAKING:** Bump minimum Node.js version to 22 ([#9168](https://github.com/MetaMask/core/pull/9168))
|
|
13
|
+
|
|
10
14
|
## [4.0.3]
|
|
11
15
|
|
|
12
16
|
### Changed
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { UnionToIntersection } from '@metamask/json-rpc-engine/v2';
|
|
2
|
+
import type { WalletCreateSessionHooks } from './wallet-createSession.js';
|
|
3
|
+
import { walletCreateSessionHandler } from './wallet-createSession.js';
|
|
4
|
+
import type { WalletGetSessionHooks } from './wallet-getSession.js';
|
|
5
|
+
import { walletGetSessionHandler } from './wallet-getSession.js';
|
|
6
|
+
import type { WalletInvokeMethodHooks } from './wallet-invokeMethod.js';
|
|
7
|
+
import { walletInvokeMethodHandler } from './wallet-invokeMethod.js';
|
|
8
|
+
import type { WalletRevokeSessionHooks } from './wallet-revokeSession.js';
|
|
9
|
+
import { walletRevokeSessionHandler } from './wallet-revokeSession.js';
|
|
10
|
+
export type MethodHandlerHooks = UnionToIntersection<WalletCreateSessionHooks | WalletGetSessionHooks | WalletInvokeMethodHooks | WalletRevokeSessionHooks>;
|
|
11
|
+
declare const MethodNames: {
|
|
12
|
+
readonly WalletCreateSession: 'wallet_createSession';
|
|
13
|
+
readonly WalletGetSession: 'wallet_getSession';
|
|
14
|
+
readonly WalletInvokeMethod: 'wallet_invokeMethod';
|
|
15
|
+
readonly WalletRevokeSession: 'wallet_revokeSession';
|
|
16
|
+
};
|
|
17
|
+
type MethodHandlers = {
|
|
18
|
+
[MethodNames.WalletCreateSession]: typeof walletCreateSessionHandler;
|
|
19
|
+
[MethodNames.WalletGetSession]: typeof walletGetSessionHandler;
|
|
20
|
+
[MethodNames.WalletInvokeMethod]: typeof walletInvokeMethodHandler;
|
|
21
|
+
[MethodNames.WalletRevokeSession]: typeof walletRevokeSessionHandler;
|
|
22
|
+
};
|
|
23
|
+
export declare const methodHandlers: Readonly<MethodHandlers>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAExE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AAEvE,MAAM,MAAM,kBAAkB,GAAG,mBAAmB,CAChD,wBAAwB,GACxB,qBAAqB,GACrB,uBAAuB,GACvB,wBAAwB,CAC3B,CAAC;AAEF,QAAA,MAAM,WAAW;aACf,mBAAmB,EAAE,sBAAsB;aAC3C,gBAAgB,EAAE,mBAAmB;aACrC,kBAAkB,EAAE,qBAAqB;aACzC,mBAAmB,EAAE,sBAAsB;CACnC,CAAC;AAEX,KAAK,cAAc,GAAG;IACpB,CAAC,WAAW,CAAC,mBAAmB,CAAC,EAAE,OAAO,0BAA0B,CAAC;IACrE,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAAE,OAAO,uBAAuB,CAAC;IAC/D,CAAC,WAAW,CAAC,kBAAkB,CAAC,EAAE,OAAO,yBAAyB,CAAC;IACnE,CAAC,WAAW,CAAC,mBAAmB,CAAC,EAAE,OAAO,0BAA0B,CAAC;CACtE,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,cAAc,CAKnD,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { walletCreateSessionHandler } from
|
|
2
|
-
import { walletGetSessionHandler } from
|
|
3
|
-
import { walletInvokeMethodHandler } from
|
|
4
|
-
import { walletRevokeSessionHandler } from
|
|
1
|
+
import { walletCreateSessionHandler } from './wallet-createSession.js';
|
|
2
|
+
import { walletGetSessionHandler } from './wallet-getSession.js';
|
|
3
|
+
import { walletInvokeMethodHandler } from './wallet-invokeMethod.js';
|
|
4
|
+
import { walletRevokeSessionHandler } from './wallet-revokeSession.js';
|
|
5
5
|
const MethodNames = {
|
|
6
6
|
WalletCreateSession: 'wallet_createSession',
|
|
7
7
|
WalletGetSession: 'wallet_getSession',
|
|
@@ -14,4 +14,4 @@ export const methodHandlers = {
|
|
|
14
14
|
[MethodNames.WalletInvokeMethod]: walletInvokeMethodHandler,
|
|
15
15
|
[MethodNames.WalletRevokeSession]: walletRevokeSessionHandler,
|
|
16
16
|
};
|
|
17
|
-
//# sourceMappingURL=index.
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AAEvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEjE,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAErE,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AASvE,MAAM,WAAW,GAAG;IAClB,mBAAmB,EAAE,sBAAsB;IAC3C,gBAAgB,EAAE,mBAAmB;IACrC,kBAAkB,EAAE,qBAAqB;IACzC,mBAAmB,EAAE,sBAAsB;CACnC,CAAC;AASX,MAAM,CAAC,MAAM,cAAc,GAA6B;IACtD,CAAC,WAAW,CAAC,mBAAmB,CAAC,EAAE,0BAA0B;IAC7D,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAAE,uBAAuB;IACvD,CAAC,WAAW,CAAC,kBAAkB,CAAC,EAAE,yBAAyB;IAC3D,CAAC,WAAW,CAAC,mBAAmB,CAAC,EAAE,0BAA0B;CAC9D,CAAC","sourcesContent":["import type { UnionToIntersection } from '@metamask/json-rpc-engine/v2';\n\nimport type { WalletCreateSessionHooks } from './wallet-createSession.js';\nimport { walletCreateSessionHandler } from './wallet-createSession.js';\nimport type { WalletGetSessionHooks } from './wallet-getSession.js';\nimport { walletGetSessionHandler } from './wallet-getSession.js';\nimport type { WalletInvokeMethodHooks } from './wallet-invokeMethod.js';\nimport { walletInvokeMethodHandler } from './wallet-invokeMethod.js';\nimport type { WalletRevokeSessionHooks } from './wallet-revokeSession.js';\nimport { walletRevokeSessionHandler } from './wallet-revokeSession.js';\n\nexport type MethodHandlerHooks = UnionToIntersection<\n | WalletCreateSessionHooks\n | WalletGetSessionHooks\n | WalletInvokeMethodHooks\n | WalletRevokeSessionHooks\n>;\n\nconst MethodNames = {\n WalletCreateSession: 'wallet_createSession',\n WalletGetSession: 'wallet_getSession',\n WalletInvokeMethod: 'wallet_invokeMethod',\n WalletRevokeSession: 'wallet_revokeSession',\n} as const;\n\ntype MethodHandlers = {\n [MethodNames.WalletCreateSession]: typeof walletCreateSessionHandler;\n [MethodNames.WalletGetSession]: typeof walletGetSessionHandler;\n [MethodNames.WalletInvokeMethod]: typeof walletInvokeMethodHandler;\n [MethodNames.WalletRevokeSession]: typeof walletRevokeSessionHandler;\n};\n\nexport const methodHandlers: Readonly<MethodHandlers> = {\n [MethodNames.WalletCreateSession]: walletCreateSessionHandler,\n [MethodNames.WalletGetSession]: walletGetSessionHandler,\n [MethodNames.WalletInvokeMethod]: walletInvokeMethodHandler,\n [MethodNames.WalletRevokeSession]: walletRevokeSessionHandler,\n};\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Caip25CaveatType, Caip25CaveatValue } from
|
|
2
|
-
import type { GenericPermissionController, Caveat } from
|
|
3
|
-
import type { MultichainRoutingService } from
|
|
4
|
-
import type { CaipAccountId, Hex, Json } from
|
|
1
|
+
import { Caip25CaveatType, Caip25CaveatValue } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { GenericPermissionController, Caveat } from '@metamask/permission-controller';
|
|
3
|
+
import type { MultichainRoutingService } from '@metamask/snaps-controllers';
|
|
4
|
+
import type { CaipAccountId, Hex, Json } from '@metamask/utils';
|
|
5
5
|
/**
|
|
6
6
|
* Multichain API notifications currently supported by/known to the wallet.
|
|
7
7
|
*/
|
|
@@ -24,4 +24,4 @@ export type GetCapabilitiesHook = {
|
|
|
24
24
|
address: string;
|
|
25
25
|
}) => Promise<Record<Hex, Record<string, Json>>>;
|
|
26
26
|
};
|
|
27
|
-
//# sourceMappingURL=types.d.
|
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/handlers/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EACV,2BAA2B,EAC3B,MAAM,EACP,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEhE;;GAEG;AACH,oBAAY,0BAA0B;IACpC,cAAc,0BAA0B;IACxC,YAAY,kBAAkB;CAC/B;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;AAE9E,MAAM,MAAM,sBAAsB,GAAG;IACnC,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,UAAU,CAAC,2BAA2B,CAAC,WAAW,CAAC,CAAC,CAAC;CAC3D,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,yBAAyB,EAAE,wBAAwB,CAAC,qBAAqB,CAAC,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG;IAC7C,4BAA4B,EAAE,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,eAAe,EAAE,CAAC,MAAM,EAAE;QACxB,OAAO,EAAE,MAAM,CAAC;KACjB,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;CAClD,CAAC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
2
1
|
/**
|
|
3
2
|
* Multichain API notifications currently supported by/known to the wallet.
|
|
4
3
|
*/
|
|
@@ -7,4 +6,4 @@ export var MultichainApiNotifications;
|
|
|
7
6
|
MultichainApiNotifications["sessionChanged"] = "wallet_sessionChanged";
|
|
8
7
|
MultichainApiNotifications["walletNotify"] = "wallet_notify";
|
|
9
8
|
})(MultichainApiNotifications || (MultichainApiNotifications = {}));
|
|
10
|
-
//# sourceMappingURL=types.
|
|
9
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/handlers/types.ts"],"names":[],"mappings":"AAWA;;GAEG;AACH,MAAM,CAAN,IAAY,0BAGX;AAHD,WAAY,0BAA0B;IACpC,sEAAwC,CAAA;IACxC,4DAA8B,CAAA;AAChC,CAAC,EAHW,0BAA0B,KAA1B,0BAA0B,QAGrC","sourcesContent":["import {\n Caip25CaveatType,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n GenericPermissionController,\n Caveat,\n} from '@metamask/permission-controller';\nimport type { MultichainRoutingService } from '@metamask/snaps-controllers';\nimport type { CaipAccountId, Hex, Json } from '@metamask/utils';\n\n/**\n * Multichain API notifications currently supported by/known to the wallet.\n */\nexport enum MultichainApiNotifications {\n sessionChanged = 'wallet_sessionChanged',\n walletNotify = 'wallet_notify',\n}\n\nexport type Caip25Caveat = Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n\nexport type GetCaveatForOriginHook = {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => ReturnType<GenericPermissionController['getCaveat']>;\n};\n\nexport type GetNonEvmSupportedMethodsHook = {\n getNonEvmSupportedMethods: MultichainRoutingService['getSupportedMethods'];\n};\n\nexport type SortAccountIdsByLastSelectedHook = {\n sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];\n};\n\nexport type GetCapabilitiesHook = {\n getCapabilities: (params: {\n address: string;\n }) => Promise<Record<Hex, Record<string, Json>>>;\n};\n"]}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { AccountsController } from
|
|
2
|
-
import type { Caip25Authorization, NormalizedScopesObject, Caip25CaveatValue } from
|
|
3
|
-
import type { MethodHandler, JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from
|
|
4
|
-
import type { NetworkController } from
|
|
5
|
-
import type { GenericPermissionController, RequestedPermissions } from
|
|
6
|
-
import type { MultichainRoutingService } from
|
|
7
|
-
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from
|
|
8
|
-
import type { GetCapabilitiesHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from
|
|
1
|
+
import type { AccountsController } from '@metamask/accounts-controller';
|
|
2
|
+
import type { Caip25Authorization, NormalizedScopesObject, Caip25CaveatValue } from '@metamask/chain-agnostic-permission';
|
|
3
|
+
import type { MethodHandler, JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
|
|
4
|
+
import type { NetworkController } from '@metamask/network-controller';
|
|
5
|
+
import type { GenericPermissionController, RequestedPermissions } from '@metamask/permission-controller';
|
|
6
|
+
import type { MultichainRoutingService } from '@metamask/snaps-controllers';
|
|
7
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
|
|
8
|
+
import type { GetCapabilitiesHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from './types.js';
|
|
9
9
|
export type WalletCreateSessionHooks = GetNonEvmSupportedMethodsHook & SortAccountIdsByLastSelectedHook & GetCapabilitiesHook & {
|
|
10
10
|
listAccounts: AccountsController['listAccounts'];
|
|
11
11
|
findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
|
|
@@ -67,4 +67,4 @@ export declare const walletCreateSessionHandler: {
|
|
|
67
67
|
};
|
|
68
68
|
};
|
|
69
69
|
export {};
|
|
70
|
-
//# sourceMappingURL=wallet-createSession.d.
|
|
70
|
+
//# sourceMappingURL=wallet-createSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet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AAEJ,KAAK,MAAM,GAAG,mBAAmB,CAAC;AAElC,KAAK,MAAM,GAAG;IACZ,aAAa,EAAE,sBAAsB,CAAC;IACtC,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,iBAAe,yBAAyB,CACtC,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAChD,GAAG,EAAE,sBAAsB,CAAC,MAAM,CAAC,EACnC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAgMf;AAED,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,wBAAwB,EACxB,KAAK,EACL,MAAM,EACN,MAAM,EACN;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;CAaD,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Caip25CaveatType, Caip25EndowmentPermissionName, bucketScopes, validateAndNormalizeScopes, getInternalScopesObject, getSessionScopes, getSessionProperties, getSupportedScopeObjects, isKnownSessionPropertyValue, getCaipAccountIdsFromScopesObjects, getAllScopesFromScopesObjects, setNonSCACaipAccountIdsInCaip25CaveatValue, isNamespaceInScopesObject } from
|
|
2
|
-
import { isEqualCaseInsensitive } from
|
|
3
|
-
import { invalidParams } from
|
|
4
|
-
import { JsonRpcError, rpcErrors } from
|
|
5
|
-
import { isPlainObject, KnownCaipNamespace, parseCaipAccountId } from
|
|
1
|
+
import { Caip25CaveatType, Caip25EndowmentPermissionName, bucketScopes, validateAndNormalizeScopes, getInternalScopesObject, getSessionScopes, getSessionProperties, getSupportedScopeObjects, isKnownSessionPropertyValue, getCaipAccountIdsFromScopesObjects, getAllScopesFromScopesObjects, setNonSCACaipAccountIdsInCaip25CaveatValue, isNamespaceInScopesObject, } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import { isEqualCaseInsensitive } from '@metamask/controller-utils';
|
|
3
|
+
import { invalidParams } from '@metamask/permission-controller';
|
|
4
|
+
import { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';
|
|
5
|
+
import { isPlainObject, KnownCaipNamespace, parseCaipAccountId, } from '@metamask/utils';
|
|
6
6
|
const SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
|
|
7
7
|
/**
|
|
8
8
|
* Handler for the `wallet_createSession` RPC method which is responsible
|
|
@@ -173,4 +173,4 @@ export const walletCreateSessionHandler = {
|
|
|
173
173
|
trackSessionCreatedEvent: true,
|
|
174
174
|
},
|
|
175
175
|
};
|
|
176
|
-
//# sourceMappingURL=wallet-createSession.
|
|
176
|
+
//# sourceMappingURL=wallet-createSession.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-createSession.js","sourceRoot":"","sources":["../../src/handlers/wallet-createSession.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,YAAY,EACZ,0BAA0B,EAC1B,uBAAuB,EACvB,gBAAgB,EAChB,oBAAoB,EACpB,wBAAwB,EACxB,2BAA2B,EAC3B,kCAAkC,EAClC,6BAA6B,EAC7B,0CAA0C,EAC1C,yBAAyB,GAC1B,MAAM,qCAAqC,CAAC;AAM7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAOpE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAKhE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAezB,MAAM,oBAAoB,GAAG,yCAAyC,CAA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AC;QACF,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAUD,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,4BAA4B,EAAE,IAAI;QAClC,YAAY,EAAE,IAAI;QAClB,2BAA2B,EAAE,IAAI;QACjC,yBAAyB,EAAE,IAAI;QAC/B,sBAAsB,EAAE,IAAI;QAC5B,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,eAAe,EAAE,IAAI;QACrB,wBAAwB,EAAE,IAAI;KAC/B;CACmC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n bucketScopes,\n validateAndNormalizeScopes,\n getInternalScopesObject,\n getSessionScopes,\n getSessionProperties,\n getSupportedScopeObjects,\n isKnownSessionPropertyValue,\n getCaipAccountIdsFromScopesObjects,\n getAllScopesFromScopesObjects,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n isNamespaceInScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n Caip25Authorization,\n NormalizedScopesObject,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type {\n MethodHandler,\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkController } from '@metamask/network-controller';\nimport { invalidParams } from '@metamask/permission-controller';\nimport type {\n GenericPermissionController,\n RequestedPermissions,\n} from '@metamask/permission-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport type { MultichainRoutingService } from '@metamask/snaps-controllers';\nimport {\n isPlainObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport type {\n CaipAccountId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n GetCapabilitiesHook,\n GetNonEvmSupportedMethodsHook,\n SortAccountIdsByLastSelectedHook,\n} from './types.js';\n\nconst SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n\nexport type WalletCreateSessionHooks = GetNonEvmSupportedMethodsHook &\n SortAccountIdsByLastSelectedHook &\n GetCapabilitiesHook & {\n listAccounts: AccountsController['listAccounts'];\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n requestPermissionsForOrigin: (\n requestedPermissions: RequestedPermissions,\n metadata?: Record<string, Json>,\n ) => ReturnType<GenericPermissionController['requestPermissions']>;\n isNonEvmScopeSupported: MultichainRoutingService['isSupportedScope'];\n getNonEvmAccountAddresses: MultichainRoutingService['getSupportedAccounts'];\n trackSessionCreatedEvent:\n | ((approvedCaip25CaveatValue: Caip25CaveatValue) => void)\n | null;\n };\n\ntype Params = Caip25Authorization;\n\ntype Result = {\n sessionScopes: NormalizedScopesObject;\n sessionProperties?: Record<string, Json>;\n};\n\n/**\n * Handler for the `wallet_createSession` RPC method which is responsible\n * for prompting for approval and granting a CAIP-25 permission.\n *\n * This implementation primarily deviates from the CAIP-25 handler\n * specification by treating all scopes as optional regardless of\n * if they were specified in `requiredScopes` or `optionalScopes`.\n * Additionally, provided scopes, methods, notifications, and\n * account values that are invalid/malformed are ignored rather than\n * causing an error to be returned.\n *\n * @param req - The request object.\n * @param res - The response object.\n * @param _next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.\n * @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.\n * @param hooks.requestPermissionsForOrigin - The hook that approves and grants requested permissions.\n * @param hooks.getNonEvmSupportedMethods - The hook that returns the supported methods for a non EVM scope.\n * @param hooks.isNonEvmScopeSupported - The hook that returns true if a non EVM scope is supported.\n * @param hooks.getNonEvmAccountAddresses - The hook that returns a list of CaipAccountIds that are supported for a CaipChainId.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by last selected.\n * @param hooks.getCapabilities - A function that returns the capabilities for a given address.\n * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run.\n * @returns A promise with wallet_createSession handler\n */\nasync function handleWalletCreateSession(\n req: JsonRpcRequest<Params> & { origin: string },\n res: PendingJsonRpcResponse<Result>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletCreateSessionHooks,\n): Promise<void> {\n if (!isPlainObject(req.params)) {\n return end(invalidParams({ data: { request: req } }));\n }\n const { requiredScopes, optionalScopes, sessionProperties } = req.params;\n\n if (sessionProperties && Object.keys(sessionProperties).length === 0) {\n return end(new JsonRpcError(5302, 'Invalid sessionProperties requested'));\n }\n\n const filteredSessionProperties = Object.fromEntries(\n Object.entries(sessionProperties ?? {}).filter(([key]) =>\n isKnownSessionPropertyValue(key),\n ),\n );\n\n try {\n const { normalizedRequiredScopes, normalizedOptionalScopes } =\n validateAndNormalizeScopes(requiredScopes || {}, optionalScopes || {});\n\n const requiredScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedRequiredScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n const optionalScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedOptionalScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n\n const networkClientExistsForChainId = (chainId: Hex) => {\n try {\n hooks.findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n // if solana is a requested scope but not supported, we add a promptToCreateSolanaAccount flag to request\n const isSolanaRequested =\n isNamespaceInScopesObject(\n requiredScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n ) ||\n isNamespaceInScopesObject(\n optionalScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n );\n\n let promptToCreateSolanaAccount = false;\n if (isSolanaRequested) {\n const supportedSolanaAccounts =\n hooks.getNonEvmAccountAddresses(SOLANA_CAIP_CHAIN_ID);\n promptToCreateSolanaAccount = supportedSolanaAccounts.length === 0;\n }\n\n const { supportedScopes: supportedRequiredScopes } = bucketScopes(\n requiredScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const { supportedScopes: supportedOptionalScopes } = bucketScopes(\n optionalScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const allRequestedAccountAddresses = getCaipAccountIdsFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const allSupportedRequestedCaipChainIds = getAllScopesFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const existingEvmAddresses = hooks\n .listAccounts()\n .map((account) => account.address);\n\n const supportedRequestedAccountAddresses =\n allRequestedAccountAddresses.filter(\n (requestedAccountAddress: CaipAccountId) => {\n const {\n address,\n chain: { namespace },\n chainId: caipChainId,\n } = parseCaipAccountId(requestedAccountAddress);\n if (namespace === KnownCaipNamespace.Eip155.toString()) {\n return existingEvmAddresses.some((existingEvmAddress) => {\n return isEqualCaseInsensitive(address, existingEvmAddress);\n });\n }\n\n // If the namespace is not eip155 (EVM) we do a case sensitive check\n return hooks\n .getNonEvmAccountAddresses(caipChainId)\n .some((existingCaipAddress) => {\n return requestedAccountAddress === existingCaipAddress;\n });\n },\n );\n\n const requestedCaip25CaveatValue = {\n requiredScopes: getInternalScopesObject(supportedRequiredScopes),\n optionalScopes: getInternalScopesObject(supportedOptionalScopes),\n isMultichainOrigin: true,\n sessionProperties: filteredSessionProperties,\n };\n\n const requestedCaip25CaveatValueWithSupportedAccounts =\n setNonSCACaipAccountIdsInCaip25CaveatValue(\n requestedCaip25CaveatValue,\n supportedRequestedAccountAddresses,\n );\n\n // if `promptToCreateSolanaAccount` is true and there are no other valid scopes requested,\n // we add a `wallet` scope to the request in order to get passed the CAIP-25 caveat validator.\n // This is very hacky but is necessary because the solana opt-in flow breaks key assumptions\n // of the CAIP-25 permission specification - namely that we can have valid requests with no scopes.\n if (allSupportedRequestedCaipChainIds.length === 0) {\n if (promptToCreateSolanaAccount) {\n requestedCaip25CaveatValueWithSupportedAccounts.optionalScopes[\n KnownCaipNamespace.Wallet\n ] = {\n accounts: [],\n };\n } else {\n // if solana is not requested and there are no supported scopes, we return an error\n return end(\n new JsonRpcError(5100, 'Requested scopes are not supported'),\n );\n }\n }\n\n const [grantedPermissions] = await hooks.requestPermissionsForOrigin(\n {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: requestedCaip25CaveatValueWithSupportedAccounts,\n },\n ],\n },\n },\n {\n metadata: { promptToCreateSolanaAccount },\n },\n );\n\n const approvedCaip25Permission =\n grantedPermissions[Caip25EndowmentPermissionName];\n const approvedCaip25CaveatValue = approvedCaip25Permission?.caveats?.find(\n (caveat) => caveat.type === Caip25CaveatType,\n )?.value as Caip25CaveatValue;\n if (!approvedCaip25CaveatValue) {\n throw rpcErrors.internal();\n }\n\n const sessionScopes = getSessionScopes(approvedCaip25CaveatValue, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n });\n\n const approvedSessionProperties = await getSessionProperties(\n approvedCaip25CaveatValue,\n {\n getCapabilities: hooks.getCapabilities,\n },\n );\n\n hooks.trackSessionCreatedEvent?.(approvedCaip25CaveatValue);\n\n res.result = {\n sessionScopes,\n sessionProperties: approvedSessionProperties,\n };\n return end();\n } catch (err) {\n return end(err);\n }\n}\n\nexport type WalletCreateSessionHandler = MethodHandler<\n WalletCreateSessionHooks,\n never,\n Params,\n Result,\n { origin: string }\n>;\n\nexport const walletCreateSessionHandler = {\n implementation: handleWalletCreateSession,\n hookNames: {\n findNetworkClientIdByChainId: true,\n listAccounts: true,\n requestPermissionsForOrigin: true,\n getNonEvmSupportedMethods: true,\n isNonEvmScopeSupported: true,\n getNonEvmAccountAddresses: true,\n sortAccountIdsByLastSelected: true,\n getCapabilities: true,\n trackSessionCreatedEvent: true,\n },\n} satisfies WalletCreateSessionHandler;\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { NormalizedScopesObject } from
|
|
2
|
-
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from
|
|
3
|
-
import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from
|
|
4
|
-
import type { GetCapabilitiesHook, GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from
|
|
1
|
+
import type { NormalizedScopesObject } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from '@metamask/json-rpc-engine';
|
|
3
|
+
import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
|
|
4
|
+
import type { GetCapabilitiesHook, GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from './types.js';
|
|
5
5
|
type WalletGetSessionResult = {
|
|
6
6
|
sessionScopes: NormalizedScopesObject;
|
|
7
7
|
sessionProperties: Record<string, Json>;
|
|
@@ -40,4 +40,4 @@ export declare const walletGetSessionHandler: {
|
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
42
|
export {};
|
|
43
|
-
//# sourceMappingURL=wallet-getSession.d.
|
|
43
|
+
//# sourceMappingURL=wallet-getSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.d.ts","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAOlF,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAEV,mBAAmB,EACnB,sBAAsB,EACtB,6BAA6B,EAC7B,gCAAgC,EACjC,MAAM,YAAY,CAAC;AAEpB,KAAK,sBAAsB,GAAG;IAC5B,aAAa,EAAE,sBAAsB,CAAC;IACtC,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,sBAAsB,GACxD,6BAA6B,GAC7B,gCAAgC,GAChC,mBAAmB,CAAC;AAEtB;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,sBAAsB,CACnC,QAAQ,EAAE,cAAc,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,QAAQ,EAAE,sBAAsB,CAAC,sBAAsB,CAAC,EACxD,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,qBAAqB,iBA2B7B;AAED,MAAM,MAAM,uBAAuB,GAAG,aAAa,CACjD,qBAAqB,EACrB,KAAK,EACL,aAAa,EACb,sBAAsB,EACtB;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,uBAAuB;;;;;;;;CAQD,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Caip25CaveatType, Caip25EndowmentPermissionName, getSessionProperties, getSessionScopes } from
|
|
1
|
+
import { Caip25CaveatType, Caip25EndowmentPermissionName, getSessionProperties, getSessionScopes, } from '@metamask/chain-agnostic-permission';
|
|
2
2
|
/**
|
|
3
3
|
* Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
|
|
4
4
|
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
|
|
@@ -48,4 +48,4 @@ export const walletGetSessionHandler = {
|
|
|
48
48
|
getCapabilities: true,
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
|
-
//# sourceMappingURL=wallet-getSession.
|
|
51
|
+
//# sourceMappingURL=wallet-getSession.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.js","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,qCAAqC,CAAC;AA+B7C;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,sBAAsB,CACnC,QAA6C,EAC7C,QAAwD,EACxD,KAAgC,EAChC,GAA6B,EAC7B,KAA4B;IAE5B,IAAI,MAAgC,CAAC;IACrC,IAAI,CAAC;QACH,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,6BAA6B,EAC7B,gBAAgB,CACW,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;IACT,CAAC;IAED,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,QAAQ,CAAC,MAAM,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,iBAAiB,EAAE,EAAE,EAAE,CAAC;QAC/D,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG;QAChB,aAAa,EAAE,gBAAgB,CAAC,MAAM,CAAC,KAAK,EAAE;YAC5C,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;YAC1D,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;SACjE,CAAC;QACF,iBAAiB,EAAE,MAAM,oBAAoB,CAAC,MAAM,CAAC,KAAK,EAAE;YAC1D,eAAe,EAAE,KAAK,CAAC,eAAe;SACvC,CAAC;KACH,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUD,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,cAAc,EAAE,sBAAsB;IACtC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,eAAe,EAAE,IAAI;KACtB;CACgC,CAAC","sourcesContent":["import type { NormalizedScopesObject } from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getSessionProperties,\n getSessionScopes,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n Caip25Caveat,\n GetCapabilitiesHook,\n GetCaveatForOriginHook,\n GetNonEvmSupportedMethodsHook,\n SortAccountIdsByLastSelectedHook,\n} from './types.js';\n\ntype WalletGetSessionResult = {\n sessionScopes: NormalizedScopesObject;\n sessionProperties: Record<string, Json>;\n};\n\nexport type WalletGetSessionHooks = GetCaveatForOriginHook &\n GetNonEvmSupportedMethodsHook &\n SortAccountIdsByLastSelectedHook &\n GetCapabilitiesHook;\n\n/**\n * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,\n * and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there\n * is no active session for the origin.\n *\n * @param _request - The request object.\n * @param response - The response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.\n * @param hooks.getCapabilities - A function that returns the capabilities for a given address.\n * @returns Nothing.\n */\nasync function handleWalletGetSession(\n _request: JsonRpcRequest & { origin: string },\n response: PendingJsonRpcResponse<WalletGetSessionResult>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletGetSessionHooks,\n) {\n let caveat: Caip25Caveat | undefined;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\n } catch {\n // noop\n }\n\n if (!caveat) {\n response.result = { sessionScopes: {}, sessionProperties: {} };\n return end();\n }\n\n response.result = {\n sessionScopes: getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n }),\n sessionProperties: await getSessionProperties(caveat.value, {\n getCapabilities: hooks.getCapabilities,\n }),\n };\n return end();\n}\n\nexport type WalletGetSessionHandler = MethodHandler<\n WalletGetSessionHooks,\n never,\n JsonRpcParams,\n WalletGetSessionResult,\n { origin: string }\n>;\n\nexport const walletGetSessionHandler = {\n implementation: handleWalletGetSession,\n hookNames: {\n getCaveatForOrigin: true,\n getNonEvmSupportedMethods: true,\n sortAccountIdsByLastSelected: true,\n getCapabilities: true,\n },\n} satisfies WalletGetSessionHandler;\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { ExternalScopeString } from
|
|
2
|
-
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from
|
|
3
|
-
import type { NetworkClientId, NetworkController } from
|
|
4
|
-
import type { MultichainRoutingService } from
|
|
5
|
-
import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from
|
|
6
|
-
import type { GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from
|
|
1
|
+
import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from '@metamask/json-rpc-engine';
|
|
3
|
+
import type { NetworkClientId, NetworkController } from '@metamask/network-controller';
|
|
4
|
+
import type { MultichainRoutingService } from '@metamask/snaps-controllers';
|
|
5
|
+
import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
|
|
6
|
+
import type { GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from './types.js';
|
|
7
7
|
export type WalletInvokeMethodParams = {
|
|
8
8
|
scope: ExternalScopeString;
|
|
9
9
|
request: Pick<JsonRpcRequest, 'method' | 'params'>;
|
|
@@ -54,4 +54,4 @@ export declare const walletInvokeMethodHandler: {
|
|
|
54
54
|
};
|
|
55
55
|
};
|
|
56
56
|
export {};
|
|
57
|
-
//# sourceMappingURL=wallet-invokeMethod.d.
|
|
57
|
+
//# sourceMappingURL=wallet-invokeMethod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.d.ts","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAQ/E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,E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ACJ;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;CAUD,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Caip25CaveatType, Caip25EndowmentPermissionName, assertIsInternalScopeString, getSessionScopes, parseScopeString } from
|
|
2
|
-
import { providerErrors, rpcErrors } from
|
|
3
|
-
import { isObject, KnownCaipNamespace, numberToHex } from
|
|
1
|
+
import { Caip25CaveatType, Caip25EndowmentPermissionName, assertIsInternalScopeString, getSessionScopes, parseScopeString, } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
+
import { isObject, KnownCaipNamespace, numberToHex } from '@metamask/utils';
|
|
4
4
|
/**
|
|
5
5
|
* Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
|
|
6
6
|
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
@@ -97,4 +97,4 @@ export const walletInvokeMethodHandler = {
|
|
|
97
97
|
handleNonEvmRequestForOrigin: true,
|
|
98
98
|
},
|
|
99
99
|
};
|
|
100
|
-
//# sourceMappingURL=wallet-invokeMethod.
|
|
100
|
+
//# sourceMappingURL=wallet-invokeMethod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.js","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,2BAA2B,EAC3B,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,qCAAqC,CAAC;AAU7C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAsC5E;;;;;;;;;;;;;;;;;GAiB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AAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUD,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,cAAc,EAAE,wBAAwB;IACxC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,4BAA4B,EAAE,IAAI;QAClC,0BAA0B,EAAE,IAAI;QAChC,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,4BAA4B,EAAE,IAAI;KACnC;CACkC,CAAC","sourcesContent":["import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n assertIsInternalScopeString,\n getSessionScopes,\n parseScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type {\n NetworkClientId,\n NetworkController,\n} from '@metamask/network-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { MultichainRoutingService } from '@metamask/snaps-controllers';\nimport { isObject, KnownCaipNamespace, numberToHex } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n Caip25Caveat,\n GetCaveatForOriginHook,\n GetNonEvmSupportedMethodsHook,\n SortAccountIdsByLastSelectedHook,\n} from './types.js';\n\nexport type WalletInvokeMethodParams = {\n scope: ExternalScopeString;\n request: Pick<JsonRpcRequest, 'method' | 'params'>;\n};\n\nexport type WalletInvokeMethodRequest =\n JsonRpcRequest<WalletInvokeMethodParams> & {\n origin: string;\n };\n\nexport type WalletInvokeMethodHooks = GetCaveatForOriginHook &\n GetNonEvmSupportedMethodsHook &\n SortAccountIdsByLastSelectedHook & {\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n getSelectedNetworkClientId: () => NetworkClientId;\n handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => ReturnType<MultichainRoutingService['handleRequest']>;\n };\n\n/**\n * Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param\n * and instead uses the singular session for the origin if available.\n *\n * @param request - The request object.\n * @param response - The response object. Unused.\n * @param next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.\n * @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.\n * @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @param hooks.sortAccountIdsByLastSelected - A function that sorts accounts by their last selected order.\n * @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.\n * @returns Nothing.\n */\nasync function handleWalletInvokeMethod(\n request: WalletInvokeMethodRequest,\n response: PendingJsonRpcResponse<Json>,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletInvokeMethodHooks,\n) {\n if (!isObject(request.params)) {\n return end(rpcErrors.invalidParams({ data: { request } }));\n }\n\n const { scope, request: wrappedRequest } = request.params;\n assertIsInternalScopeString(scope);\n\n let caveat: Caip25Caveat | undefined;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\n } catch {\n // noop\n }\n if (!caveat) {\n return end(providerErrors.unauthorized());\n }\n\n const scopeObject = getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n })[scope];\n\n if (!scopeObject?.methods?.includes(wrappedRequest.method)) {\n return end(providerErrors.unauthorized());\n }\n\n const { namespace, reference } = parseScopeString(scope);\n\n const isEvmRequest =\n (namespace === KnownCaipNamespace.Wallet &&\n (!reference || reference === KnownCaipNamespace.Eip155)) ||\n namespace === KnownCaipNamespace.Eip155;\n\n const unwrappedRequest = {\n ...request,\n scope,\n method: wrappedRequest.method,\n params: wrappedRequest.params,\n };\n\n if (isEvmRequest) {\n let networkClientId;\n if (namespace === KnownCaipNamespace.Wallet) {\n networkClientId = hooks.getSelectedNetworkClientId();\n } else if (namespace === KnownCaipNamespace.Eip155) {\n if (reference) {\n networkClientId = hooks.findNetworkClientIdByChainId(\n numberToHex(parseInt(reference, 10)),\n );\n }\n }\n\n if (!networkClientId) {\n console.error(\n 'failed to resolve network client for wallet_invokeMethod',\n request,\n );\n return end(rpcErrors.internal());\n }\n\n Object.assign(request, {\n ...unwrappedRequest,\n networkClientId,\n });\n return next();\n }\n\n try {\n response.result = await hooks.handleNonEvmRequestForOrigin({\n connectedAddresses: scopeObject.accounts,\n // Type assertion: We know that scope is not \"wallet\" by now because it\n // is already being handled above.\n scope: scope as CaipChainId,\n request: unwrappedRequest,\n });\n } catch (err) {\n return end(err as Error);\n }\n return end();\n}\n\nexport type WalletInvokeMethodHandler = MethodHandler<\n WalletInvokeMethodHooks,\n never,\n WalletInvokeMethodParams,\n Json,\n { origin: string }\n>;\n\nexport const walletInvokeMethodHandler = {\n implementation: handleWalletInvokeMethod,\n hookNames: {\n getCaveatForOrigin: true,\n findNetworkClientIdByChainId: true,\n getSelectedNetworkClientId: true,\n getNonEvmSupportedMethods: true,\n sortAccountIdsByLastSelected: true,\n handleNonEvmRequestForOrigin: true,\n },\n} satisfies WalletInvokeMethodHandler;\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Caip25CaveatValue } from
|
|
2
|
-
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from
|
|
3
|
-
import { GenericPermissionController } from
|
|
4
|
-
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from
|
|
5
|
-
import type { GetCaveatForOriginHook } from
|
|
1
|
+
import type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from '@metamask/json-rpc-engine';
|
|
3
|
+
import { GenericPermissionController } from '@metamask/permission-controller';
|
|
4
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
|
|
5
|
+
import type { GetCaveatForOriginHook } from './types.js';
|
|
6
6
|
export type WalletRevokeSessionHooks = GetCaveatForOriginHook & {
|
|
7
7
|
revokePermissionForOrigin: (permissionName: string) => ReturnType<GenericPermissionController['revokePermissions']>;
|
|
8
8
|
updateCaveat: (target: string, caveatType: string, caveatValue: Caip25CaveatValue) => ReturnType<GenericPermissionController['updateCaveat']>;
|
|
@@ -42,4 +42,4 @@ export declare const walletRevokeSessionHandler: {
|
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
44
|
export {};
|
|
45
|
-
//# sourceMappingURL=wallet-revokeSession.d.
|
|
45
|
+
//# sourceMappingURL=wallet-revokeSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.d.ts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,2BAA2B,EAG5B,MAAM,iCAAiC,CAAC;AAGzC,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAgB,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEvE,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,GAAG;IAC9D,yBAAyB,EAAE,CACzB,cAAc,EAAE,MAAM,KACnB,UAAU,CAAC,2BAA2B,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAClE,YAAY,EAAE,CACZ,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,iBAAiB,KAC3B,UAAU,CAAC,2BAA2B,CAAC,cAAc,CAAC,CAAC,CAAC;CAC9D,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAiFvD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,cAAc,CAAC,yBAAyB,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EACvE,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,iBAiBhC;AAED,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,wBAAwB,EACxB,KAAK,EACL,yBAAyB,EACzB,IAAI,EACJ;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,0BAA0B;;;;;;;CAOD,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Caip25CaveatMutators, Caip25CaveatType, Caip25EndowmentPermissionName, getCaipAccountIdsFromCaip25CaveatValue } from
|
|
2
|
-
import { CaveatMutatorOperation, PermissionDoesNotExistError, UnrecognizedSubjectError } from
|
|
3
|
-
import { rpcErrors } from
|
|
4
|
-
import { isObject } from
|
|
1
|
+
import { Caip25CaveatMutators, Caip25CaveatType, Caip25EndowmentPermissionName, getCaipAccountIdsFromCaip25CaveatValue, } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import { CaveatMutatorOperation, PermissionDoesNotExistError, UnrecognizedSubjectError, } from '@metamask/permission-controller';
|
|
3
|
+
import { rpcErrors } from '@metamask/rpc-errors';
|
|
4
|
+
import { isObject } from '@metamask/utils';
|
|
5
5
|
/**
|
|
6
6
|
* Check whether the given error is a permission error.
|
|
7
7
|
*
|
|
@@ -100,4 +100,4 @@ export const walletRevokeSessionHandler = {
|
|
|
100
100
|
getCaveatForOrigin: true,
|
|
101
101
|
},
|
|
102
102
|
};
|
|
103
|
-
//# sourceMappingURL=wallet-revokeSession.
|
|
103
|
+
//# sourceMappingURL=wallet-revokeSession.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.js","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,6BAA6B,EAC7B,sCAAsC,GACvC,MAAM,qCAAqC,CAAC;AAO7C,OAAO,EACL,sBAAsB,EAEtB,2BAA2B,EAC3B,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAsB3C;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IACE,CAAC,QAAQ,CAAC,KAAK,CAAC;QAChB,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;QAClB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAC9B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO;QACL,wBAAwB,CAAC,IAAI;QAC7B,2BAA2B,CAAC,IAAI;KACjC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACzB,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,wBAAwB,CAC/B,MAAgB,EAChB,KAA+B;IAE/B,MAAM,MAAM,GAAG,KAAK,CAAC,kBAAkB,CACrC,6BAA6B,EAC7B,gBAAgB,CACW,CAAC;IAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;IACT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AAG,CAAC,EAAE,CAAC;YAC5B,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnB,OAAO,GAAG,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUD,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,yBAAyB,EAAE,IAAI;QAC/B,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,IAAI;KACzB;CACmC,CAAC","sourcesContent":["import {\n Caip25CaveatMutators,\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getCaipAccountIdsFromCaip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport {\n CaveatMutatorOperation,\n GenericPermissionController,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isObject } from '@metamask/utils';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { Caip25Caveat, GetCaveatForOriginHook } from './types.js';\n\nexport type WalletRevokeSessionHooks = GetCaveatForOriginHook & {\n revokePermissionForOrigin: (\n permissionName: string,\n ) => ReturnType<GenericPermissionController['revokePermissions']>;\n updateCaveat: (\n target: string,\n caveatType: string,\n caveatValue: Caip25CaveatValue,\n ) => ReturnType<GenericPermissionController['updateCaveat']>;\n};\n\ntype WalletRevokeSessionParams = { scopes?: string[] };\n\n/**\n * Check whether the given error is a permission error.\n *\n * @param error - The error to check.\n * @returns Whether the error is a permission error.\n */\nfunction isPermissionError(error: unknown) {\n if (\n !isObject(error) ||\n !('name' in error) ||\n typeof error.name !== 'string'\n ) {\n return false;\n }\n\n return [\n UnrecognizedSubjectError.name,\n PermissionDoesNotExistError.name,\n ].includes(error.name);\n}\n\n/**\n * Revokes specific session scopes from an existing caveat.\n * Fully revokes permission if no accounts remain permitted after iterating through scopes.\n *\n * @param scopes - Array of scope strings to remove from the caveat.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n */\nfunction partialRevokePermissions(\n scopes: string[],\n hooks: WalletRevokeSessionHooks,\n) {\n const caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\n if (!caveat) {\n return;\n }\n\n let updatedCaveatValue = caveat.value;\n\n for (const scopeString of scopes) {\n const result = Caip25CaveatMutators[Caip25CaveatType].removeScope(\n updatedCaveatValue,\n scopeString,\n );\n\n // If operation is a Noop, it means a scope was passed that was not present in the permission, so we proceed with the loop\n if (result.operation === CaveatMutatorOperation.Noop) {\n continue;\n }\n\n updatedCaveatValue = result?.value ?? {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: {},\n isMultichainOrigin: true,\n };\n }\n\n const caipAccountIds =\n getCaipAccountIdsFromCaip25CaveatValue(updatedCaveatValue);\n\n // We fully revoke permission if no accounts are left after scope removal loop.\n if (!caipAccountIds.length) {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n } else {\n hooks.updateCaveat(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n updatedCaveatValue,\n );\n }\n}\n\n/**\n * Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param\n * and instead revokes the singular session for the origin if available. Additionally,\n * the handler also does not return an error if there is currently no active session and instead\n * returns true which is the same result returned if an active session was actually revoked.\n *\n * @param request - The JSON-RPC request object. Unused.\n * @param response - The JSON-RPC response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end callback function.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n * @returns Nothing.\n */\nasync function handleWalletRevokeSession(\n request: JsonRpcRequest<WalletRevokeSessionParams> & { origin: string },\n response: PendingJsonRpcResponse<Json>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletRevokeSessionHooks,\n) {\n try {\n if (request.params?.scopes?.length) {\n partialRevokePermissions(request.params.scopes, hooks);\n } else {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n }\n } catch (err) {\n if (!isPermissionError(err)) {\n console.error(err);\n return end(rpcErrors.internal());\n }\n }\n\n response.result = true;\n return end();\n}\n\nexport type WalletRevokeSessionHandler = MethodHandler<\n WalletRevokeSessionHooks,\n never,\n WalletRevokeSessionParams,\n Json,\n { origin: string }\n>;\n\nexport const walletRevokeSessionHandler = {\n implementation: handleWalletRevokeSession,\n hookNames: {\n revokePermissionForOrigin: true,\n updateCaveat: true,\n getCaveatForOrigin: true,\n },\n} satisfies WalletRevokeSessionHandler;\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { methodHandlers } from './handlers/index.js';
|
|
2
|
+
export type { MethodHandlerHooks } from './handlers/index.js';
|
|
3
|
+
export { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware.js';
|
|
4
|
+
export { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager.js';
|
|
5
|
+
export { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager.js';
|
|
6
|
+
export { MultichainApiNotifications } from './handlers/types.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,YAAY,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EAAE,uCAAuC,EAAE,MAAM,0DAA0D,CAAC;AACnH,OAAO,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,6BAA6B,EAAE,MAAM,gDAAgD,CAAC;AAC/F,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { methodHandlers } from './handlers/index.js';
|
|
2
|
+
export { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware.js';
|
|
3
|
+
export { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager.js';
|
|
4
|
+
export { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager.js';
|
|
5
|
+
export { MultichainApiNotifications } from './handlers/types.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAGrD,OAAO,EAAE,uCAAuC,EAAE,MAAM,0DAA0D,CAAC;AACnH,OAAO,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,6BAA6B,EAAE,MAAM,gDAAgD,CAAC;AAC/F,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["export { methodHandlers } from './handlers/index.js';\nexport type { MethodHandlerHooks } from './handlers/index.js';\n\nexport { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware.js';\nexport { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager.js';\nexport { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager.js';\nexport { MultichainApiNotifications } from './handlers/types.js';\n"]}
|
package/dist/middlewares/{MultichainMiddlewareManager.d.cts → MultichainMiddlewareManager.d.ts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ExternalScopeString } from
|
|
2
|
-
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from
|
|
3
|
-
import type { JsonRpcRequest, PendingJsonRpcResponse } from
|
|
1
|
+
import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
|
|
3
|
+
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
|
|
4
4
|
export type ExtendedJsonRpcMiddleware = {
|
|
5
5
|
(req: JsonRpcRequest & {
|
|
6
6
|
scope: string;
|
|
@@ -32,4 +32,4 @@ export declare class MultichainMiddlewareManager {
|
|
|
32
32
|
generateMultichainMiddlewareForOriginAndTabId(origin: string, tabId?: number): ExtendedJsonRpcMiddleware;
|
|
33
33
|
}
|
|
34
34
|
export {};
|
|
35
|
-
//# sourceMappingURL=MultichainMiddlewareManager.d.
|
|
35
|
+
//# sourceMappingURL=MultichainMiddlewareManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultichainMiddlewareManager.d.ts","sourceRoot":"","sources":["../../src/middlewares/MultichainMiddlewareManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AAEnC,OAAO,KAAK,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAE9E,MAAM,MAAM,yBAAyB,GAAG;IACtC,CACE,GAAG,EAAE,cAAc,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,EACvC,GAAG,EAAE,sBAAsB,EAC3B,IAAI,EAAE,yBAAyB,EAC/B,GAAG,EAAE,wBAAwB,GAC5B,IAAI,CAAC;IACR,OAAO,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtC,CAAC;AAEF,KAAK,aAAa,GAAG;IACnB,KAAK,EAAE,mBAAmB,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AACF,KAAK,eAAe,GAAG,aAAa,GAAG;IACrC,UAAU,EAAE,yBAAyB,CAAC;CACvC,CAAC;AAKF;;;;;;;GAOG;AACH,qBAAa,2BAA2B;;IA2BtC,aAAa,CAAC,eAAe,EAAE,eAAe,QAK7C;IAYD,uBAAuB,CAAC,KAAK,EAAE,mBAAmB,QAMjD;IAED,gCAAgC,CAAC,KAAK,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAS1E;IAED,gCAAgC,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,QAS9D;IAED,6CAA6C,CAC3C,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,MAAM,6BA+Bf;CACF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { rpcErrors } from '@metamask/rpc-errors';
|
|
2
|
+
// Methods related to eth_subscriptions
|
|
3
|
+
const SubscriptionMethods = ['eth_subscribe', 'eth_unsubscribe'];
|
|
4
|
+
/**
|
|
5
|
+
* A helper that facilates registering and calling of provided middleware instances
|
|
6
|
+
* in the RPC pipeline based on the incoming request's scope, origin, and tabId.
|
|
7
|
+
* The core purpose of this class is to enable and manage multichain subscriptions
|
|
8
|
+
* (i.e. eth_subscribe called accross different chains and domains).
|
|
9
|
+
*
|
|
10
|
+
* Note that only one middleware instance can be registered per scope, origin, tabId key.
|
|
11
|
+
*/
|
|
12
|
+
export class MultichainMiddlewareManager {
|
|
13
|
+
#middlewares = [];
|
|
14
|
+
#getMiddlewareEntry({ scope, origin, tabId, }) {
|
|
15
|
+
return this.#middlewares.find((middlewareEntry) => {
|
|
16
|
+
return (middlewareEntry.scope === scope &&
|
|
17
|
+
middlewareEntry.origin === origin &&
|
|
18
|
+
middlewareEntry.tabId === tabId);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
#removeMiddlewareEntry({ scope, origin, tabId }) {
|
|
22
|
+
this.#middlewares = this.#middlewares.filter((middlewareEntry) => {
|
|
23
|
+
return (middlewareEntry.scope !== scope ||
|
|
24
|
+
middlewareEntry.origin !== origin ||
|
|
25
|
+
middlewareEntry.tabId !== tabId);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
addMiddleware(middlewareEntry) {
|
|
29
|
+
const { scope, origin, tabId } = middlewareEntry;
|
|
30
|
+
if (!this.#getMiddlewareEntry({ scope, origin, tabId })) {
|
|
31
|
+
this.#middlewares.push(middlewareEntry);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
#removeMiddleware(middlewareEntry) {
|
|
35
|
+
// When the destroy function on the middleware is async,
|
|
36
|
+
// we don't need to wait for it complete
|
|
37
|
+
Promise.resolve(middlewareEntry.middleware.destroy?.()).catch(() => {
|
|
38
|
+
// do nothing
|
|
39
|
+
});
|
|
40
|
+
this.#removeMiddlewareEntry(middlewareEntry);
|
|
41
|
+
}
|
|
42
|
+
removeMiddlewareByScope(scope) {
|
|
43
|
+
this.#middlewares.forEach((middlewareEntry) => {
|
|
44
|
+
if (middlewareEntry.scope === scope) {
|
|
45
|
+
this.#removeMiddleware(middlewareEntry);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
removeMiddlewareByScopeAndOrigin(scope, origin) {
|
|
50
|
+
this.#middlewares.forEach((middlewareEntry) => {
|
|
51
|
+
if (middlewareEntry.scope === scope &&
|
|
52
|
+
middlewareEntry.origin === origin) {
|
|
53
|
+
this.#removeMiddleware(middlewareEntry);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
removeMiddlewareByOriginAndTabId(origin, tabId) {
|
|
58
|
+
this.#middlewares.forEach((middlewareEntry) => {
|
|
59
|
+
if (middlewareEntry.origin === origin &&
|
|
60
|
+
middlewareEntry.tabId === tabId) {
|
|
61
|
+
this.#removeMiddleware(middlewareEntry);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
generateMultichainMiddlewareForOriginAndTabId(origin, tabId) {
|
|
66
|
+
const middleware = (req, res, next, end) => {
|
|
67
|
+
const { scope } = req;
|
|
68
|
+
const middlewareEntry = this.#getMiddlewareEntry({
|
|
69
|
+
scope,
|
|
70
|
+
origin,
|
|
71
|
+
tabId,
|
|
72
|
+
});
|
|
73
|
+
if (SubscriptionMethods.includes(req.method)) {
|
|
74
|
+
if (middlewareEntry) {
|
|
75
|
+
middlewareEntry.middleware(req, res, next, end);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// TODO: Temporary safety guard to prevent requests with these methods
|
|
79
|
+
// from being forwarded to the RPC endpoint even though this scenario
|
|
80
|
+
// should not be possible.
|
|
81
|
+
return end(rpcErrors.methodNotFound());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return next();
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
};
|
|
89
|
+
middleware.destroy = this.removeMiddlewareByOriginAndTabId.bind(this, origin, tabId);
|
|
90
|
+
return middleware;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=MultichainMiddlewareManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultichainMiddlewareManager.js","sourceRo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AG,IAAI,CAAC,mBAAmB,CAAC;gBAC/C,KAAK;gBACL,MAAM;gBACN,KAAK;aACN,CAAC,CAAC;YAEH,IAAI,mBAAmB,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC7C,IAAI,eAAe,EAAE,CAAC;oBACpB,eAAe,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;gBAClD,CAAC;qBAAM,CAAC;oBACN,sEAAsE;oBACtE,qEAAqE;oBACrE,0BAA0B;oBAC1B,OAAO,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC;gBACzC,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC,CAAC;QACF,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAC7D,IAAI,EACJ,MAAM,EACN,KAAK,CACN,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC;CACF","sourcesContent":["import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';\n\nexport type ExtendedJsonRpcMiddleware = {\n (\n req: JsonRpcRequest & { scope: string },\n res: PendingJsonRpcResponse,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n ): void;\n destroy?: () => void | Promise<void>;\n};\n\ntype MiddlewareKey = {\n scope: ExternalScopeString;\n origin: string;\n tabId?: number;\n};\ntype MiddlewareEntry = MiddlewareKey & {\n middleware: ExtendedJsonRpcMiddleware;\n};\n\n// Methods related to eth_subscriptions\nconst SubscriptionMethods = ['eth_subscribe', 'eth_unsubscribe'];\n\n/**\n * A helper that facilates registering and calling of provided middleware instances\n * in the RPC pipeline based on the incoming request's scope, origin, and tabId.\n * The core purpose of this class is to enable and manage multichain subscriptions\n * (i.e. eth_subscribe called accross different chains and domains).\n *\n * Note that only one middleware instance can be registered per scope, origin, tabId key.\n */\nexport class MultichainMiddlewareManager {\n #middlewares: MiddlewareEntry[] = [];\n\n #getMiddlewareEntry({\n scope,\n origin,\n tabId,\n }: MiddlewareKey): MiddlewareEntry | undefined {\n return this.#middlewares.find((middlewareEntry) => {\n return (\n middlewareEntry.scope === scope &&\n middlewareEntry.origin === origin &&\n middlewareEntry.tabId === tabId\n );\n });\n }\n\n #removeMiddlewareEntry({ scope, origin, tabId }: MiddlewareEntry) {\n this.#middlewares = this.#middlewares.filter((middlewareEntry) => {\n return (\n middlewareEntry.scope !== scope ||\n middlewareEntry.origin !== origin ||\n middlewareEntry.tabId !== tabId\n );\n });\n }\n\n addMiddleware(middlewareEntry: MiddlewareEntry) {\n const { scope, origin, tabId } = middlewareEntry;\n if (!this.#getMiddlewareEntry({ scope, origin, tabId })) {\n this.#middlewares.push(middlewareEntry);\n }\n }\n\n #removeMiddleware(middlewareEntry: MiddlewareEntry) {\n // When the destroy function on the middleware is async,\n // we don't need to wait for it complete\n Promise.resolve(middlewareEntry.middleware.destroy?.()).catch(() => {\n // do nothing\n });\n\n this.#removeMiddlewareEntry(middlewareEntry);\n }\n\n removeMiddlewareByScope(scope: ExternalScopeString) {\n this.#middlewares.forEach((middlewareEntry) => {\n if (middlewareEntry.scope === scope) {\n this.#removeMiddleware(middlewareEntry);\n }\n });\n }\n\n removeMiddlewareByScopeAndOrigin(scope: ExternalScopeString, origin: string) {\n this.#middlewares.forEach((middlewareEntry) => {\n if (\n middlewareEntry.scope === scope &&\n middlewareEntry.origin === origin\n ) {\n this.#removeMiddleware(middlewareEntry);\n }\n });\n }\n\n removeMiddlewareByOriginAndTabId(origin: string, tabId?: number) {\n this.#middlewares.forEach((middlewareEntry) => {\n if (\n middlewareEntry.origin === origin &&\n middlewareEntry.tabId === tabId\n ) {\n this.#removeMiddleware(middlewareEntry);\n }\n });\n }\n\n generateMultichainMiddlewareForOriginAndTabId(\n origin: string,\n tabId?: number,\n ) {\n const middleware: ExtendedJsonRpcMiddleware = (req, res, next, end) => {\n const { scope } = req;\n const middlewareEntry = this.#getMiddlewareEntry({\n scope,\n origin,\n tabId,\n });\n\n if (SubscriptionMethods.includes(req.method)) {\n if (middlewareEntry) {\n middlewareEntry.middleware(req, res, next, end);\n } else {\n // TODO: Temporary safety guard to prevent requests with these methods\n // from being forwarded to the RPC endpoint even though this scenario\n // should not be possible.\n return end(rpcErrors.methodNotFound());\n }\n } else {\n return next();\n }\n return undefined;\n };\n middleware.destroy = this.removeMiddlewareByOriginAndTabId.bind(\n this,\n origin,\n tabId,\n );\n\n return middleware;\n }\n}\n"]}
|
package/dist/middlewares/{MultichainSubscriptionManager.d.cts → MultichainSubscriptionManager.d.ts}
RENAMED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ExternalScopeString } from
|
|
2
|
-
import type { NetworkController } from
|
|
3
|
-
import SafeEventEmitter from
|
|
4
|
-
import type { Hex } from
|
|
5
|
-
import type { ExtendedJsonRpcMiddleware } from
|
|
1
|
+
import type { ExternalScopeString } from '@metamask/chain-agnostic-permission';
|
|
2
|
+
import type { NetworkController } from '@metamask/network-controller';
|
|
3
|
+
import SafeEventEmitter from '@metamask/safe-event-emitter';
|
|
4
|
+
import type { Hex } from '@metamask/utils';
|
|
5
|
+
import type { ExtendedJsonRpcMiddleware } from './MultichainMiddlewareManager.js';
|
|
6
6
|
export type SubscriptionManager = {
|
|
7
7
|
events: SafeEventEmitter;
|
|
8
8
|
destroy?: () => void;
|
|
@@ -46,4 +46,4 @@ export declare class MultichainSubscriptionManager extends SafeEventEmitter {
|
|
|
46
46
|
unsubscribeByOriginAndTabId(origin: string, tabId?: number): void;
|
|
47
47
|
}
|
|
48
48
|
export {};
|
|
49
|
-
//# sourceMappingURL=MultichainSubscriptionManager.d.
|
|
49
|
+
//# sourceMappingURL=MultichainSubscriptionManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultichainSubscriptionManager.d.ts","sourceRoot":"","sources":["../../src/middlewares/MultichainSubscriptionManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAG/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,gBAAgB,MAAM,8BAA8B,CAAC;AAC5D,OAAO,KAAK,EAAe,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAIxD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAElF,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,gBAAgB,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,EAAE,yBAAyB,CAAC;CACvC,CAAC;AAEF,KAAK,6BAA6B,GAAG;IACnC,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE,kBAAkB,CAAC;IAC3B,MAAM,EAAE;QACN,YAAY,EAAE,GAAG,CAAC;QAClB,MAAM,EAAE,OAAO,CAAC;KACjB,CAAC;CACH,CAAC;AAEF,KAAK,eAAe,GAAG;IACrB,KAAK,EAAE,mBAAmB,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAKF,KAAK,oCAAoC,GAAG;IAC1C,4BAA4B,EAAE,iBAAiB,CAAC,8BAA8B,CAAC,CAAC;IAChF,oBAAoB,EAAE,iBAAiB,CAAC,sBAAsB,CAAC,CAAC;CACjE,CAAC;AAEF;;;GAGG;AACH,qBAAa,6BAA8B,SAAQ,gBAAgB;;IAOjE;;;;;;OAMG;IACH,YAAY,OAAO,EAAE,oCAAoC,EAIxD;IAED,MAAM,CACJ,EAAE,KAAK,E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