@metamask-previews/multichain-api-middleware 0.1.0-preview-b69c669
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 +17 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/handlers/wallet-getSession.cjs +47 -0
- package/dist/handlers/wallet-getSession.cjs.map +1 -0
- package/dist/handlers/wallet-getSession.d.cts +37 -0
- package/dist/handlers/wallet-getSession.d.cts.map +1 -0
- package/dist/handlers/wallet-getSession.d.mts +37 -0
- package/dist/handlers/wallet-getSession.d.mts.map +1 -0
- package/dist/handlers/wallet-getSession.mjs +44 -0
- package/dist/handlers/wallet-getSession.mjs.map +1 -0
- package/dist/handlers/wallet-invokeMethod.cjs +98 -0
- package/dist/handlers/wallet-invokeMethod.cjs.map +1 -0
- package/dist/handlers/wallet-invokeMethod.d.cts +53 -0
- package/dist/handlers/wallet-invokeMethod.d.cts.map +1 -0
- package/dist/handlers/wallet-invokeMethod.d.mts +53 -0
- package/dist/handlers/wallet-invokeMethod.d.mts.map +1 -0
- package/dist/handlers/wallet-invokeMethod.mjs +95 -0
- package/dist/handlers/wallet-invokeMethod.mjs.map +1 -0
- package/dist/handlers/wallet-revokeSession.cjs +43 -0
- package/dist/handlers/wallet-revokeSession.cjs.map +1 -0
- package/dist/handlers/wallet-revokeSession.d.cts +31 -0
- package/dist/handlers/wallet-revokeSession.d.cts.map +1 -0
- package/dist/handlers/wallet-revokeSession.d.mts +31 -0
- package/dist/handlers/wallet-revokeSession.d.mts.map +1 -0
- package/dist/handlers/wallet-revokeSession.mjs +40 -0
- package/dist/handlers/wallet-revokeSession.mjs.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/dist/middlewares/MultichainMiddlewareManager.cjs +110 -0
- package/dist/middlewares/MultichainMiddlewareManager.cjs.map +1 -0
- package/dist/middlewares/MultichainMiddlewareManager.d.cts +35 -0
- package/dist/middlewares/MultichainMiddlewareManager.d.cts.map +1 -0
- package/dist/middlewares/MultichainMiddlewareManager.d.mts +35 -0
- package/dist/middlewares/MultichainMiddlewareManager.d.mts.map +1 -0
- package/dist/middlewares/MultichainMiddlewareManager.mjs +106 -0
- package/dist/middlewares/MultichainMiddlewareManager.mjs.map +1 -0
- package/dist/middlewares/MultichainSubscriptionManager.cjs +117 -0
- package/dist/middlewares/MultichainSubscriptionManager.cjs.map +1 -0
- package/dist/middlewares/MultichainSubscriptionManager.d.cts +49 -0
- package/dist/middlewares/MultichainSubscriptionManager.d.cts.map +1 -0
- package/dist/middlewares/MultichainSubscriptionManager.d.mts +49 -0
- package/dist/middlewares/MultichainSubscriptionManager.d.mts.map +1 -0
- package/dist/middlewares/MultichainSubscriptionManager.mjs +117 -0
- package/dist/middlewares/MultichainSubscriptionManager.mjs.map +1 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.cjs +74 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.cjs.map +1 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.cts +7 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.cts.map +1 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.mts +7 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.d.mts.map +1 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.mjs +75 -0
- package/dist/middlewares/multichainMethodCallValidatorMiddleware.mjs.map +1 -0
- package/package.json +80 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Initial release
|
|
15
|
+
|
|
16
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/multichain-api-middleware@0.1.0...HEAD
|
|
17
|
+
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/multichain-api-middleware@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/multichain-api-middleware`
|
|
2
|
+
|
|
3
|
+
JSON-RPC methods and middleware to support the the [MetaMask Multichain API](https://github.com/MetaMask/metamask-improvement-proposals/blob/main/MIPs/mip-5.md).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/multichain-api-middleware`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/multichain-api-middleware`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.walletGetSession = void 0;
|
|
4
|
+
const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission");
|
|
5
|
+
/**
|
|
6
|
+
* Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
|
|
7
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
|
|
8
|
+
* and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there
|
|
9
|
+
* is no active session for the origin.
|
|
10
|
+
*
|
|
11
|
+
* @param _request - The request object.
|
|
12
|
+
* @param response - The response object.
|
|
13
|
+
* @param _next - The next middleware function. Unused.
|
|
14
|
+
* @param end - The end function.
|
|
15
|
+
* @param hooks - The hooks object.
|
|
16
|
+
* @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.
|
|
17
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
18
|
+
* @returns Nothing.
|
|
19
|
+
*/
|
|
20
|
+
async function walletGetSessionHandler(_request, response, _next, end, hooks) {
|
|
21
|
+
let caveat;
|
|
22
|
+
try {
|
|
23
|
+
caveat = hooks.getCaveatForOrigin(chain_agnostic_permission_1.Caip25EndowmentPermissionName, chain_agnostic_permission_1.Caip25CaveatType);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// noop
|
|
27
|
+
}
|
|
28
|
+
if (!caveat) {
|
|
29
|
+
response.result = { sessionScopes: {} };
|
|
30
|
+
return end();
|
|
31
|
+
}
|
|
32
|
+
response.result = {
|
|
33
|
+
sessionScopes: (0, chain_agnostic_permission_1.getSessionScopes)(caveat.value, {
|
|
34
|
+
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
|
|
35
|
+
}),
|
|
36
|
+
};
|
|
37
|
+
return end();
|
|
38
|
+
}
|
|
39
|
+
exports.walletGetSession = {
|
|
40
|
+
methodNames: ['wallet_getSession'],
|
|
41
|
+
implementation: walletGetSessionHandler,
|
|
42
|
+
hookNames: {
|
|
43
|
+
getCaveatForOrigin: true,
|
|
44
|
+
getNonEvmSupportedMethods: true,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=wallet-getSession.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":";;;AAIA,mFAI6C;AAQ7C;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,uBAAuB,CACpC,QAA6C,EAC7C,QAAmE,EACnE,KAAiB,EACjB,GAAe,EACf,KAMC;IAED,IAAI,MAAM,CAAC;IACX,IAAI;QACF,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,yDAA6B,EAC7B,4CAAgB,CACjB,CAAC;KACH;IAAC,MAAM;QACN,OAAO;KACR;IAED,IAAI,CAAC,MAAM,EAAE;QACX,QAAQ,CAAC,MAAM,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC;QACxC,OAAO,GAAG,EAAE,CAAC;KACd;IAED,QAAQ,CAAC,MAAM,GAAG;QAChB,aAAa,EAAE,IAAA,4CAAgB,EAAC,MAAM,CAAC,KAAK,EAAE;YAC5C,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;SAC3D,CAAC;KACH,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAEY,QAAA,gBAAgB,GAAG;IAC9B,WAAW,EAAE,CAAC,mBAAmB,CAAC;IAClC,cAAc,EAAE,uBAAuB;IACvC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,yBAAyB,EAAE,IAAI;KAChC;CACF,CAAC","sourcesContent":["import type {\n Caip25CaveatValue,\n NormalizedScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getSessionScopes,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caveat } from '@metamask/permission-controller';\nimport type {\n CaipChainId,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\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 * @returns Nothing.\n */\nasync function walletGetSessionHandler(\n _request: JsonRpcRequest & { origin: string },\n response: JsonRpcSuccess<{ sessionScopes: NormalizedScopesObject }>,\n _next: () => void,\n end: () => void,\n hooks: {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) {\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n\n if (!caveat) {\n response.result = { sessionScopes: {} };\n return end();\n }\n\n response.result = {\n sessionScopes: getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n }),\n };\n return end();\n}\n\nexport const walletGetSession = {\n methodNames: ['wallet_getSession'],\n implementation: walletGetSessionHandler,\n hookNames: {\n getCaveatForOrigin: true,\n getNonEvmSupportedMethods: true,\n },\n};\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Caip25CaveatValue, NormalizedScopesObject } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
4
|
+
import type { CaipChainId, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
|
|
5
|
+
/**
|
|
6
|
+
* Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
|
|
7
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
|
|
8
|
+
* and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there
|
|
9
|
+
* is no active session for the origin.
|
|
10
|
+
*
|
|
11
|
+
* @param _request - The request object.
|
|
12
|
+
* @param response - The response object.
|
|
13
|
+
* @param _next - The next middleware function. Unused.
|
|
14
|
+
* @param end - The end function.
|
|
15
|
+
* @param hooks - The hooks object.
|
|
16
|
+
* @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.
|
|
17
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
18
|
+
* @returns Nothing.
|
|
19
|
+
*/
|
|
20
|
+
declare function walletGetSessionHandler(_request: JsonRpcRequest & {
|
|
21
|
+
origin: string;
|
|
22
|
+
}, response: JsonRpcSuccess<{
|
|
23
|
+
sessionScopes: NormalizedScopesObject;
|
|
24
|
+
}>, _next: () => void, end: () => void, hooks: {
|
|
25
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
26
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
export declare const walletGetSession: {
|
|
29
|
+
methodNames: string[];
|
|
30
|
+
implementation: typeof walletGetSessionHandler;
|
|
31
|
+
hookNames: {
|
|
32
|
+
getCaveatForOrigin: boolean;
|
|
33
|
+
getNonEvmSupportedMethods: boolean;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=wallet-getSession.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,sBAAsB,EACvB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAGjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAC9D,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACf,wBAAwB;AAEzB;;;;;;;;;;;;;;GAcG;AACH,iBAAe,uBAAuB,CACpC,QAAQ,EAAE,cAAc,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,QAAQ,EAAE,cAAc,CAAC;IAAE,aAAa,EAAE,sBAAsB,CAAA;CAAE,CAAC,EACnE,KAAK,EAAE,MAAM,IAAI,EACjB,GAAG,EAAE,MAAM,IAAI,EACf,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;CAC7D,iBAuBF;AAED,eAAO,MAAM,gBAAgB;;;;;;;CAO5B,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Caip25CaveatValue, NormalizedScopesObject } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
4
|
+
import type { CaipChainId, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
|
|
5
|
+
/**
|
|
6
|
+
* Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
|
|
7
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
|
|
8
|
+
* and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there
|
|
9
|
+
* is no active session for the origin.
|
|
10
|
+
*
|
|
11
|
+
* @param _request - The request object.
|
|
12
|
+
* @param response - The response object.
|
|
13
|
+
* @param _next - The next middleware function. Unused.
|
|
14
|
+
* @param end - The end function.
|
|
15
|
+
* @param hooks - The hooks object.
|
|
16
|
+
* @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.
|
|
17
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
18
|
+
* @returns Nothing.
|
|
19
|
+
*/
|
|
20
|
+
declare function walletGetSessionHandler(_request: JsonRpcRequest & {
|
|
21
|
+
origin: string;
|
|
22
|
+
}, response: JsonRpcSuccess<{
|
|
23
|
+
sessionScopes: NormalizedScopesObject;
|
|
24
|
+
}>, _next: () => void, end: () => void, hooks: {
|
|
25
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
26
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
export declare const walletGetSession: {
|
|
29
|
+
methodNames: string[];
|
|
30
|
+
implementation: typeof walletGetSessionHandler;
|
|
31
|
+
hookNames: {
|
|
32
|
+
getCaveatForOrigin: boolean;
|
|
33
|
+
getNonEvmSupportedMethods: boolean;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=wallet-getSession.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,sBAAsB,EACvB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAGjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAC9D,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACf,wBAAwB;AAEzB;;;;;;;;;;;;;;GAcG;AACH,iBAAe,uBAAuB,CACpC,QAAQ,EAAE,cAAc,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,QAAQ,EAAE,cAAc,CAAC;IAAE,aAAa,EAAE,sBAAsB,CAAA;CAAE,CAAC,EACnE,KAAK,EAAE,MAAM,IAAI,EACjB,GAAG,EAAE,MAAM,IAAI,EACf,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;CAC7D,iBAuBF;AAED,eAAO,MAAM,gBAAgB;;;;;;;CAO5B,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Caip25CaveatType, Caip25EndowmentPermissionName, getSessionScopes } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
/**
|
|
3
|
+
* Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
|
|
4
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
|
|
5
|
+
* and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there
|
|
6
|
+
* is no active session for the origin.
|
|
7
|
+
*
|
|
8
|
+
* @param _request - The request object.
|
|
9
|
+
* @param response - The response object.
|
|
10
|
+
* @param _next - The next middleware function. Unused.
|
|
11
|
+
* @param end - The end function.
|
|
12
|
+
* @param hooks - The hooks object.
|
|
13
|
+
* @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.
|
|
14
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
15
|
+
* @returns Nothing.
|
|
16
|
+
*/
|
|
17
|
+
async function walletGetSessionHandler(_request, response, _next, end, hooks) {
|
|
18
|
+
let caveat;
|
|
19
|
+
try {
|
|
20
|
+
caveat = hooks.getCaveatForOrigin(Caip25EndowmentPermissionName, Caip25CaveatType);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// noop
|
|
24
|
+
}
|
|
25
|
+
if (!caveat) {
|
|
26
|
+
response.result = { sessionScopes: {} };
|
|
27
|
+
return end();
|
|
28
|
+
}
|
|
29
|
+
response.result = {
|
|
30
|
+
sessionScopes: getSessionScopes(caveat.value, {
|
|
31
|
+
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
return end();
|
|
35
|
+
}
|
|
36
|
+
export const walletGetSession = {
|
|
37
|
+
methodNames: ['wallet_getSession'],
|
|
38
|
+
implementation: walletGetSessionHandler,
|
|
39
|
+
hookNames: {
|
|
40
|
+
getCaveatForOrigin: true,
|
|
41
|
+
getNonEvmSupportedMethods: true,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=wallet-getSession.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-getSession.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,gBAAgB,EACjB,4CAA4C;AAQ7C;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,uBAAuB,CACpC,QAA6C,EAC7C,QAAmE,EACnE,KAAiB,EACjB,GAAe,EACf,KAMC;IAED,IAAI,MAAM,CAAC;IACX,IAAI;QACF,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,6BAA6B,EAC7B,gBAAgB,CACjB,CAAC;KACH;IAAC,MAAM;QACN,OAAO;KACR;IAED,IAAI,CAAC,MAAM,EAAE;QACX,QAAQ,CAAC,MAAM,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC;QACxC,OAAO,GAAG,EAAE,CAAC;KACd;IAED,QAAQ,CAAC,MAAM,GAAG;QAChB,aAAa,EAAE,gBAAgB,CAAC,MAAM,CAAC,KAAK,EAAE;YAC5C,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;SAC3D,CAAC;KACH,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,WAAW,EAAE,CAAC,mBAAmB,CAAC;IAClC,cAAc,EAAE,uBAAuB;IACvC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,yBAAyB,EAAE,IAAI;KAChC;CACF,CAAC","sourcesContent":["import type {\n Caip25CaveatValue,\n NormalizedScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getSessionScopes,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caveat } from '@metamask/permission-controller';\nimport type {\n CaipChainId,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\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 * @returns Nothing.\n */\nasync function walletGetSessionHandler(\n _request: JsonRpcRequest & { origin: string },\n response: JsonRpcSuccess<{ sessionScopes: NormalizedScopesObject }>,\n _next: () => void,\n end: () => void,\n hooks: {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) {\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n\n if (!caveat) {\n response.result = { sessionScopes: {} };\n return end();\n }\n\n response.result = {\n sessionScopes: getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n }),\n };\n return end();\n}\n\nexport const walletGetSession = {\n methodNames: ['wallet_getSession'],\n implementation: walletGetSessionHandler,\n hookNames: {\n getCaveatForOrigin: true,\n getNonEvmSupportedMethods: true,\n },\n};\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.walletInvokeMethod = void 0;
|
|
4
|
+
const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission");
|
|
5
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
6
|
+
const utils_1 = require("@metamask/utils");
|
|
7
|
+
/**
|
|
8
|
+
* Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
|
|
9
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
10
|
+
* and instead uses the singular session for the origin if available.
|
|
11
|
+
*
|
|
12
|
+
* @param request - The request object.
|
|
13
|
+
* @param response - The response object. Unused.
|
|
14
|
+
* @param next - The next middleware function.
|
|
15
|
+
* @param end - The end function.
|
|
16
|
+
* @param hooks - The hooks object.
|
|
17
|
+
* @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.
|
|
18
|
+
* @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
|
|
19
|
+
* @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
|
|
20
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
21
|
+
* @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
|
|
22
|
+
* @returns Nothing.
|
|
23
|
+
*/
|
|
24
|
+
async function walletInvokeMethodHandler(request, response, next, end, hooks) {
|
|
25
|
+
const { scope, request: wrappedRequest } = request.params;
|
|
26
|
+
(0, chain_agnostic_permission_1.assertIsInternalScopeString)(scope);
|
|
27
|
+
let caveat;
|
|
28
|
+
try {
|
|
29
|
+
caveat = hooks.getCaveatForOrigin(chain_agnostic_permission_1.Caip25EndowmentPermissionName, chain_agnostic_permission_1.Caip25CaveatType);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// noop
|
|
33
|
+
}
|
|
34
|
+
if (!caveat?.value?.isMultichainOrigin) {
|
|
35
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
36
|
+
}
|
|
37
|
+
const scopeObject = (0, chain_agnostic_permission_1.getSessionScopes)(caveat.value, {
|
|
38
|
+
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
|
|
39
|
+
})[scope];
|
|
40
|
+
if (!scopeObject?.methods?.includes(wrappedRequest.method)) {
|
|
41
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
42
|
+
}
|
|
43
|
+
const { namespace, reference } = (0, chain_agnostic_permission_1.parseScopeString)(scope);
|
|
44
|
+
const isEvmRequest = (namespace === utils_1.KnownCaipNamespace.Wallet &&
|
|
45
|
+
(!reference || reference === utils_1.KnownCaipNamespace.Eip155)) ||
|
|
46
|
+
namespace === utils_1.KnownCaipNamespace.Eip155;
|
|
47
|
+
const unwrappedRequest = {
|
|
48
|
+
...request,
|
|
49
|
+
scope,
|
|
50
|
+
method: wrappedRequest.method,
|
|
51
|
+
params: wrappedRequest.params,
|
|
52
|
+
};
|
|
53
|
+
if (isEvmRequest) {
|
|
54
|
+
let networkClientId;
|
|
55
|
+
if (namespace === utils_1.KnownCaipNamespace.Wallet) {
|
|
56
|
+
networkClientId = hooks.getSelectedNetworkClientId();
|
|
57
|
+
}
|
|
58
|
+
else if (namespace === utils_1.KnownCaipNamespace.Eip155) {
|
|
59
|
+
if (reference) {
|
|
60
|
+
networkClientId = hooks.findNetworkClientIdByChainId((0, utils_1.numberToHex)(parseInt(reference, 10)));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!networkClientId) {
|
|
64
|
+
console.error('failed to resolve network client for wallet_invokeMethod', request);
|
|
65
|
+
return end(rpc_errors_1.rpcErrors.internal());
|
|
66
|
+
}
|
|
67
|
+
Object.assign(request, {
|
|
68
|
+
...unwrappedRequest,
|
|
69
|
+
networkClientId,
|
|
70
|
+
});
|
|
71
|
+
return next();
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
response.result = await hooks.handleNonEvmRequestForOrigin({
|
|
75
|
+
connectedAddresses: scopeObject.accounts,
|
|
76
|
+
// Type assertion: We know that scope is not "wallet" by now because it
|
|
77
|
+
// is already being handled above.
|
|
78
|
+
scope: scope,
|
|
79
|
+
request: unwrappedRequest,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
return end(err);
|
|
84
|
+
}
|
|
85
|
+
return end();
|
|
86
|
+
}
|
|
87
|
+
exports.walletInvokeMethod = {
|
|
88
|
+
methodNames: ['wallet_invokeMethod'],
|
|
89
|
+
implementation: walletInvokeMethodHandler,
|
|
90
|
+
hookNames: {
|
|
91
|
+
getCaveatForOrigin: true,
|
|
92
|
+
findNetworkClientIdByChainId: true,
|
|
93
|
+
getSelectedNetworkClientId: true,
|
|
94
|
+
getNonEvmSupportedMethods: true,
|
|
95
|
+
handleNonEvmRequestForOrigin: true,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=wallet-invokeMethod.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"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","sourcesContent":["import type {\n Caip25CaveatValue,\n ExternalScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n assertIsInternalScopeString,\n getSessionScopes,\n parseScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { KnownCaipNamespace, numberToHex } from '@metamask/utils';\n\nexport type WalletInvokeMethodRequest = JsonRpcRequest & {\n origin: string;\n params: {\n scope: ExternalScopeString;\n request: Pick<JsonRpcRequest, 'method' | 'params'>;\n };\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.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.\n * @returns Nothing.\n */\nasync function walletInvokeMethodHandler(\n request: WalletInvokeMethodRequest,\n response: PendingJsonRpcResponse<Json>,\n next: () => void,\n end: (error?: Error) => void,\n hooks: {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n getSelectedNetworkClientId: () => NetworkClientId;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => Promise<Json>;\n },\n) {\n const { scope, request: wrappedRequest } = request.params;\n\n assertIsInternalScopeString(scope);\n\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n if (!caveat?.value?.isMultichainOrigin) {\n return end(providerErrors.unauthorized());\n }\n\n const scopeObject = getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\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}\nexport const walletInvokeMethod = {\n methodNames: ['wallet_invokeMethod'],\n implementation: walletInvokeMethodHandler,\n hookNames: {\n getCaveatForOrigin: true,\n findNetworkClientIdByChainId: true,\n getSelectedNetworkClientId: true,\n getNonEvmSupportedMethods: true,\n handleNonEvmRequestForOrigin: true,\n },\n};\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Caip25CaveatValue, ExternalScopeString } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { NetworkClientId } from "@metamask/network-controller";
|
|
4
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
5
|
+
import type { CaipAccountId, CaipChainId, Hex, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
export type WalletInvokeMethodRequest = JsonRpcRequest & {
|
|
7
|
+
origin: string;
|
|
8
|
+
params: {
|
|
9
|
+
scope: ExternalScopeString;
|
|
10
|
+
request: Pick<JsonRpcRequest, 'method' | 'params'>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
|
|
15
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
16
|
+
* and instead uses the singular session for the origin if available.
|
|
17
|
+
*
|
|
18
|
+
* @param request - The request object.
|
|
19
|
+
* @param response - The response object. Unused.
|
|
20
|
+
* @param next - The next middleware function.
|
|
21
|
+
* @param end - The end function.
|
|
22
|
+
* @param hooks - The hooks object.
|
|
23
|
+
* @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.
|
|
24
|
+
* @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
|
|
25
|
+
* @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
|
|
26
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
27
|
+
* @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
|
|
28
|
+
* @returns Nothing.
|
|
29
|
+
*/
|
|
30
|
+
declare function walletInvokeMethodHandler(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse<Json>, next: () => void, end: (error?: Error) => void, hooks: {
|
|
31
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
32
|
+
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
|
|
33
|
+
getSelectedNetworkClientId: () => NetworkClientId;
|
|
34
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
|
35
|
+
handleNonEvmRequestForOrigin: (params: {
|
|
36
|
+
connectedAddresses: CaipAccountId[];
|
|
37
|
+
scope: CaipChainId;
|
|
38
|
+
request: JsonRpcRequest;
|
|
39
|
+
}) => Promise<Json>;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
export declare const walletInvokeMethod: {
|
|
42
|
+
methodNames: string[];
|
|
43
|
+
implementation: typeof walletInvokeMethodHandler;
|
|
44
|
+
hookNames: {
|
|
45
|
+
getCaveatForOrigin: boolean;
|
|
46
|
+
findNetworkClientIdByChainId: boolean;
|
|
47
|
+
getSelectedNetworkClientId: boolean;
|
|
48
|
+
getNonEvmSupportedMethods: boolean;
|
|
49
|
+
handleNonEvmRequestForOrigin: boolean;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export {};
|
|
53
|
+
//# sourceMappingURL=wallet-invokeMethod.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACpB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAKjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAE9D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,GAAG,EACH,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAGzB,MAAM,MAAM,yBAAyB,GAAG,cAAc,GAAG;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QACN,KAAK,EAAE,mBAAmB,CAAC;QAC3B,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,QAAQ,CAAC,CAAC;KACpD,CAAC;CACH,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,yBAAyB,EAClC,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,IAAI,EAAE,MAAM,IAAI,EAChB,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,EAC5B,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,4BAA4B,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,eAAe,GAAG,SAAS,CAAC;IAC5E,0BAA0B,EAAE,MAAM,eAAe,CAAC;IAClD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,4BAA4B,EAAE,CAAC,MAAM,EAAE;QACrC,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACrB,iBAgFF;AACD,eAAO,MAAM,kBAAkB;;;;;;;;;;CAU9B,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Caip25CaveatValue, ExternalScopeString } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { NetworkClientId } from "@metamask/network-controller";
|
|
4
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
5
|
+
import type { CaipAccountId, CaipChainId, Hex, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
export type WalletInvokeMethodRequest = JsonRpcRequest & {
|
|
7
|
+
origin: string;
|
|
8
|
+
params: {
|
|
9
|
+
scope: ExternalScopeString;
|
|
10
|
+
request: Pick<JsonRpcRequest, 'method' | 'params'>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
|
|
15
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
16
|
+
* and instead uses the singular session for the origin if available.
|
|
17
|
+
*
|
|
18
|
+
* @param request - The request object.
|
|
19
|
+
* @param response - The response object. Unused.
|
|
20
|
+
* @param next - The next middleware function.
|
|
21
|
+
* @param end - The end function.
|
|
22
|
+
* @param hooks - The hooks object.
|
|
23
|
+
* @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.
|
|
24
|
+
* @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
|
|
25
|
+
* @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
|
|
26
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
27
|
+
* @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
|
|
28
|
+
* @returns Nothing.
|
|
29
|
+
*/
|
|
30
|
+
declare function walletInvokeMethodHandler(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse<Json>, next: () => void, end: (error?: Error) => void, hooks: {
|
|
31
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
32
|
+
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
|
|
33
|
+
getSelectedNetworkClientId: () => NetworkClientId;
|
|
34
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
|
35
|
+
handleNonEvmRequestForOrigin: (params: {
|
|
36
|
+
connectedAddresses: CaipAccountId[];
|
|
37
|
+
scope: CaipChainId;
|
|
38
|
+
request: JsonRpcRequest;
|
|
39
|
+
}) => Promise<Json>;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
export declare const walletInvokeMethod: {
|
|
42
|
+
methodNames: string[];
|
|
43
|
+
implementation: typeof walletInvokeMethodHandler;
|
|
44
|
+
hookNames: {
|
|
45
|
+
getCaveatForOrigin: boolean;
|
|
46
|
+
findNetworkClientIdByChainId: boolean;
|
|
47
|
+
getSelectedNetworkClientId: boolean;
|
|
48
|
+
getNonEvmSupportedMethods: boolean;
|
|
49
|
+
handleNonEvmRequestForOrigin: boolean;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export {};
|
|
53
|
+
//# sourceMappingURL=wallet-invokeMethod.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACpB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAKjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAE9D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,GAAG,EACH,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAGzB,MAAM,MAAM,yBAAyB,GAAG,cAAc,GAAG;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QACN,KAAK,EAAE,mBAAmB,CAAC;QAC3B,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,QAAQ,CAAC,CAAC;KACpD,CAAC;CACH,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,yBAAyB,EAClC,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,IAAI,EAAE,MAAM,IAAI,EAChB,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,EAC5B,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,4BAA4B,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,eAAe,GAAG,SAAS,CAAC;IAC5E,0BAA0B,EAAE,MAAM,eAAe,CAAC;IAClD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,4BAA4B,EAAE,CAAC,MAAM,EAAE;QACrC,kBAAkB,EAAE,aAAa,EAAE,CAAC;QACpC,KAAK,EAAE,WAAW,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;KACzB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACrB,iBAgFF;AACD,eAAO,MAAM,kBAAkB;;;;;;;;;;CAU9B,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Caip25CaveatType, Caip25EndowmentPermissionName, assertIsInternalScopeString, getSessionScopes, parseScopeString } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
3
|
+
import { KnownCaipNamespace, numberToHex } from "@metamask/utils";
|
|
4
|
+
/**
|
|
5
|
+
* Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
|
|
6
|
+
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
7
|
+
* and instead uses the singular session for the origin if available.
|
|
8
|
+
*
|
|
9
|
+
* @param request - The request object.
|
|
10
|
+
* @param response - The response object. Unused.
|
|
11
|
+
* @param next - The next middleware function.
|
|
12
|
+
* @param end - The end function.
|
|
13
|
+
* @param hooks - The hooks object.
|
|
14
|
+
* @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.
|
|
15
|
+
* @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
|
|
16
|
+
* @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
|
|
17
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
|
18
|
+
* @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
|
|
19
|
+
* @returns Nothing.
|
|
20
|
+
*/
|
|
21
|
+
async function walletInvokeMethodHandler(request, response, next, end, hooks) {
|
|
22
|
+
const { scope, request: wrappedRequest } = request.params;
|
|
23
|
+
assertIsInternalScopeString(scope);
|
|
24
|
+
let caveat;
|
|
25
|
+
try {
|
|
26
|
+
caveat = hooks.getCaveatForOrigin(Caip25EndowmentPermissionName, Caip25CaveatType);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// noop
|
|
30
|
+
}
|
|
31
|
+
if (!caveat?.value?.isMultichainOrigin) {
|
|
32
|
+
return end(providerErrors.unauthorized());
|
|
33
|
+
}
|
|
34
|
+
const scopeObject = getSessionScopes(caveat.value, {
|
|
35
|
+
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
|
|
36
|
+
})[scope];
|
|
37
|
+
if (!scopeObject?.methods?.includes(wrappedRequest.method)) {
|
|
38
|
+
return end(providerErrors.unauthorized());
|
|
39
|
+
}
|
|
40
|
+
const { namespace, reference } = parseScopeString(scope);
|
|
41
|
+
const isEvmRequest = (namespace === KnownCaipNamespace.Wallet &&
|
|
42
|
+
(!reference || reference === KnownCaipNamespace.Eip155)) ||
|
|
43
|
+
namespace === KnownCaipNamespace.Eip155;
|
|
44
|
+
const unwrappedRequest = {
|
|
45
|
+
...request,
|
|
46
|
+
scope,
|
|
47
|
+
method: wrappedRequest.method,
|
|
48
|
+
params: wrappedRequest.params,
|
|
49
|
+
};
|
|
50
|
+
if (isEvmRequest) {
|
|
51
|
+
let networkClientId;
|
|
52
|
+
if (namespace === KnownCaipNamespace.Wallet) {
|
|
53
|
+
networkClientId = hooks.getSelectedNetworkClientId();
|
|
54
|
+
}
|
|
55
|
+
else if (namespace === KnownCaipNamespace.Eip155) {
|
|
56
|
+
if (reference) {
|
|
57
|
+
networkClientId = hooks.findNetworkClientIdByChainId(numberToHex(parseInt(reference, 10)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!networkClientId) {
|
|
61
|
+
console.error('failed to resolve network client for wallet_invokeMethod', request);
|
|
62
|
+
return end(rpcErrors.internal());
|
|
63
|
+
}
|
|
64
|
+
Object.assign(request, {
|
|
65
|
+
...unwrappedRequest,
|
|
66
|
+
networkClientId,
|
|
67
|
+
});
|
|
68
|
+
return next();
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
response.result = await hooks.handleNonEvmRequestForOrigin({
|
|
72
|
+
connectedAddresses: scopeObject.accounts,
|
|
73
|
+
// Type assertion: We know that scope is not "wallet" by now because it
|
|
74
|
+
// is already being handled above.
|
|
75
|
+
scope: scope,
|
|
76
|
+
request: unwrappedRequest,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
return end(err);
|
|
81
|
+
}
|
|
82
|
+
return end();
|
|
83
|
+
}
|
|
84
|
+
export const walletInvokeMethod = {
|
|
85
|
+
methodNames: ['wallet_invokeMethod'],
|
|
86
|
+
implementation: walletInvokeMethodHandler,
|
|
87
|
+
hookNames: {
|
|
88
|
+
getCaveatForOrigin: true,
|
|
89
|
+
findNetworkClientIdByChainId: true,
|
|
90
|
+
getSelectedNetworkClientId: true,
|
|
91
|
+
getNonEvmSupportedMethods: true,
|
|
92
|
+
handleNonEvmRequestForOrigin: true,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=wallet-invokeMethod.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"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","sourcesContent":["import type {\n Caip25CaveatValue,\n ExternalScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n assertIsInternalScopeString,\n getSessionScopes,\n parseScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { KnownCaipNamespace, numberToHex } from '@metamask/utils';\n\nexport type WalletInvokeMethodRequest = JsonRpcRequest & {\n origin: string;\n params: {\n scope: ExternalScopeString;\n request: Pick<JsonRpcRequest, 'method' | 'params'>;\n };\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.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.\n * @returns Nothing.\n */\nasync function walletInvokeMethodHandler(\n request: WalletInvokeMethodRequest,\n response: PendingJsonRpcResponse<Json>,\n next: () => void,\n end: (error?: Error) => void,\n hooks: {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n getSelectedNetworkClientId: () => NetworkClientId;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => Promise<Json>;\n },\n) {\n const { scope, request: wrappedRequest } = request.params;\n\n assertIsInternalScopeString(scope);\n\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n if (!caveat?.value?.isMultichainOrigin) {\n return end(providerErrors.unauthorized());\n }\n\n const scopeObject = getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\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}\nexport const walletInvokeMethod = {\n methodNames: ['wallet_invokeMethod'],\n implementation: walletInvokeMethodHandler,\n hookNames: {\n getCaveatForOrigin: true,\n findNetworkClientIdByChainId: true,\n getSelectedNetworkClientId: true,\n getNonEvmSupportedMethods: true,\n handleNonEvmRequestForOrigin: true,\n },\n};\n"]}
|