@metamask/snaps-rpc-methods 3.1.0
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 +60 -0
- package/LICENSE +18 -0
- package/README.md +3 -0
- package/dist/cjs/index.js +43 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/permitted/common/snapInstallation.js +33 -0
- package/dist/cjs/permitted/common/snapInstallation.js.map +1 -0
- package/dist/cjs/permitted/getFile.js +63 -0
- package/dist/cjs/permitted/getFile.js.map +1 -0
- package/dist/cjs/permitted/getSnaps.js +39 -0
- package/dist/cjs/permitted/getSnaps.js.map +1 -0
- package/dist/cjs/permitted/handlers.js +33 -0
- package/dist/cjs/permitted/handlers.js.map +1 -0
- package/dist/cjs/permitted/index.js +21 -0
- package/dist/cjs/permitted/index.js.map +1 -0
- package/dist/cjs/permitted/invokeKeyring.js +88 -0
- package/dist/cjs/permitted/invokeKeyring.js.map +1 -0
- package/dist/cjs/permitted/invokeSnapSugar.js +62 -0
- package/dist/cjs/permitted/invokeSnapSugar.js.map +1 -0
- package/dist/cjs/permitted/middleware.js +38 -0
- package/dist/cjs/permitted/middleware.js.map +1 -0
- package/dist/cjs/permitted/requestSnaps.js +122 -0
- package/dist/cjs/permitted/requestSnaps.js.map +1 -0
- package/dist/cjs/request.js +6 -0
- package/dist/cjs/request.js.map +1 -0
- package/dist/cjs/restricted/caveats/index.js +38 -0
- package/dist/cjs/restricted/caveats/index.js.map +1 -0
- package/dist/cjs/restricted/caveats/permittedCoinTypes.js +83 -0
- package/dist/cjs/restricted/caveats/permittedCoinTypes.js.map +1 -0
- package/dist/cjs/restricted/caveats/permittedDerivationPaths.js +67 -0
- package/dist/cjs/restricted/caveats/permittedDerivationPaths.js.map +1 -0
- package/dist/cjs/restricted/caveats/snapIds.js +59 -0
- package/dist/cjs/restricted/caveats/snapIds.js.map +1 -0
- package/dist/cjs/restricted/dialog.js +150 -0
- package/dist/cjs/restricted/dialog.js.map +1 -0
- package/dist/cjs/restricted/getBip32Entropy.js +76 -0
- package/dist/cjs/restricted/getBip32Entropy.js.map +1 -0
- package/dist/cjs/restricted/getBip32PublicKey.js +91 -0
- package/dist/cjs/restricted/getBip32PublicKey.js.map +1 -0
- package/dist/cjs/restricted/getBip44Entropy.js +77 -0
- package/dist/cjs/restricted/getBip44Entropy.js.map +1 -0
- package/dist/cjs/restricted/getEntropy.js +76 -0
- package/dist/cjs/restricted/getEntropy.js.map +1 -0
- package/dist/cjs/restricted/getLocale.js +49 -0
- package/dist/cjs/restricted/getLocale.js.map +1 -0
- package/dist/cjs/restricted/index.js +65 -0
- package/dist/cjs/restricted/index.js.map +1 -0
- package/dist/cjs/restricted/invokeSnap.js +99 -0
- package/dist/cjs/restricted/invokeSnap.js.map +1 -0
- package/dist/cjs/restricted/manageAccounts.js +68 -0
- package/dist/cjs/restricted/manageAccounts.js.map +1 -0
- package/dist/cjs/restricted/manageState.js +216 -0
- package/dist/cjs/restricted/manageState.js.map +1 -0
- package/dist/cjs/restricted/notify.js +94 -0
- package/dist/cjs/restricted/notify.js.map +1 -0
- package/dist/cjs/utils.js +98 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/permitted/common/snapInstallation.js +33 -0
- package/dist/esm/permitted/common/snapInstallation.js.map +1 -0
- package/dist/esm/permitted/getFile.js +45 -0
- package/dist/esm/permitted/getFile.js.map +1 -0
- package/dist/esm/permitted/getSnaps.js +31 -0
- package/dist/esm/permitted/getSnaps.js.map +1 -0
- package/dist/esm/permitted/handlers.js +15 -0
- package/dist/esm/permitted/handlers.js.map +1 -0
- package/dist/esm/permitted/index.js +4 -0
- package/dist/esm/permitted/index.js.map +1 -0
- package/dist/esm/permitted/invokeKeyring.js +80 -0
- package/dist/esm/permitted/invokeKeyring.js.map +1 -0
- package/dist/esm/permitted/invokeSnapSugar.js +60 -0
- package/dist/esm/permitted/invokeSnapSugar.js.map +1 -0
- package/dist/esm/permitted/middleware.js +34 -0
- package/dist/esm/permitted/middleware.js.map +1 -0
- package/dist/esm/permitted/requestSnaps.js +117 -0
- package/dist/esm/permitted/requestSnaps.js.map +1 -0
- package/dist/esm/request.js +3 -0
- package/dist/esm/request.js.map +1 -0
- package/dist/esm/restricted/caveats/index.js +20 -0
- package/dist/esm/restricted/caveats/index.js.map +1 -0
- package/dist/esm/restricted/caveats/permittedCoinTypes.js +77 -0
- package/dist/esm/restricted/caveats/permittedCoinTypes.js.map +1 -0
- package/dist/esm/restricted/caveats/permittedDerivationPaths.js +64 -0
- package/dist/esm/restricted/caveats/permittedDerivationPaths.js.map +1 -0
- package/dist/esm/restricted/caveats/snapIds.js +50 -0
- package/dist/esm/restricted/caveats/snapIds.js.map +1 -0
- package/dist/esm/restricted/dialog.js +137 -0
- package/dist/esm/restricted/dialog.js.map +1 -0
- package/dist/esm/restricted/getBip32Entropy.js +67 -0
- package/dist/esm/restricted/getBip32Entropy.js.map +1 -0
- package/dist/esm/restricted/getBip32PublicKey.js +79 -0
- package/dist/esm/restricted/getBip32PublicKey.js.map +1 -0
- package/dist/esm/restricted/getBip44Entropy.js +70 -0
- package/dist/esm/restricted/getBip44Entropy.js.map +1 -0
- package/dist/esm/restricted/getEntropy.js +58 -0
- package/dist/esm/restricted/getEntropy.js.map +1 -0
- package/dist/esm/restricted/getLocale.js +42 -0
- package/dist/esm/restricted/getLocale.js.map +1 -0
- package/dist/esm/restricted/index.js +29 -0
- package/dist/esm/restricted/index.js.map +1 -0
- package/dist/esm/restricted/invokeSnap.js +89 -0
- package/dist/esm/restricted/invokeSnap.js.map +1 -0
- package/dist/esm/restricted/manageAccounts.js +60 -0
- package/dist/esm/restricted/manageAccounts.js.map +1 -0
- package/dist/esm/restricted/manageState.js +221 -0
- package/dist/esm/restricted/manageState.js.map +1 -0
- package/dist/esm/restricted/notify.js +89 -0
- package/dist/esm/restricted/notify.js.map +1 -0
- package/dist/esm/utils.js +124 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/__fixtures__/entropy.d.ts +14 -0
- package/dist/types/__fixtures__/index.d.ts +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/permitted/common/snapInstallation.d.ts +16 -0
- package/dist/types/permitted/getFile.d.ts +14 -0
- package/dist/types/permitted/getSnaps.d.ts +13 -0
- package/dist/types/permitted/handlers.d.ts +14 -0
- package/dist/types/permitted/index.d.ts +5 -0
- package/dist/types/permitted/invokeKeyring.d.ts +17 -0
- package/dist/types/permitted/invokeSnapSugar.d.ts +32 -0
- package/dist/types/permitted/middleware.d.ts +10 -0
- package/dist/types/permitted/requestSnaps.d.ts +50 -0
- package/dist/types/request.d.ts +99 -0
- package/dist/types/restricted/caveats/index.d.ts +8 -0
- package/dist/types/restricted/caveats/permittedCoinTypes.d.ts +29 -0
- package/dist/types/restricted/caveats/permittedDerivationPaths.d.ts +32 -0
- package/dist/types/restricted/caveats/snapIds.d.ts +21 -0
- package/dist/types/restricted/dialog.d.ts +113 -0
- package/dist/types/restricted/getBip32Entropy.d.ts +45 -0
- package/dist/types/restricted/getBip32PublicKey.d.ts +60 -0
- package/dist/types/restricted/getBip44Entropy.d.ts +49 -0
- package/dist/types/restricted/getEntropy.d.ts +60 -0
- package/dist/types/restricted/getLocale.d.ts +47 -0
- package/dist/types/restricted/index.d.ts +232 -0
- package/dist/types/restricted/invokeSnap.d.ts +62 -0
- package/dist/types/restricted/manageAccounts.d.ts +63 -0
- package/dist/types/restricted/manageState.d.ts +127 -0
- package/dist/types/restricted/notify.d.ts +81 -0
- package/dist/types/utils.d.ts +94 -0
- package/package.json +88 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [3.1.0]
|
|
10
|
+
### Changed
|
|
11
|
+
- Rename package to `@metamask/snaps-rpc-methods` ([#1864](https://github.com/MetaMask/snaps/pull/1864))
|
|
12
|
+
- Update multiple MetaMask dependencies ([#1841](https://github.com/MetaMask/snaps/pull/1841))
|
|
13
|
+
|
|
14
|
+
## [3.0.0]
|
|
15
|
+
### Added
|
|
16
|
+
- Add keyring export and endowment ([#1787](https://github.com/MetaMask/snaps/pull/1787))
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
- **BREAKING:** Bump minimum Node.js version to `^18.16.0` ([#1741](https://github.com/MetaMask/snaps/pull/1741))
|
|
20
|
+
|
|
21
|
+
## [2.0.0]
|
|
22
|
+
### Changed
|
|
23
|
+
- Initial stable release from main branch ([#1757](https://github.com/MetaMask/snaps/pull/1757))
|
|
24
|
+
|
|
25
|
+
## [0.38.3-flask.1]
|
|
26
|
+
### Changed
|
|
27
|
+
- Bump `metamask/utils` and `metamask/snaps-registry` ([#1738](https://github.com/MetaMask/snaps/pull/1738))
|
|
28
|
+
|
|
29
|
+
## [0.38.2-flask.1]
|
|
30
|
+
### Changed
|
|
31
|
+
- Remove business-logic callbacks from `manageAccounts` ([#1725](https://github.com/MetaMask/snaps/pull/1725))
|
|
32
|
+
- Bump `@metamask/utils` and `@metamask/snaps-registry` ([#1694](https://github.com/MetaMask/snaps/pull/1694))
|
|
33
|
+
|
|
34
|
+
## [0.38.1-flask.1]
|
|
35
|
+
### Fixed
|
|
36
|
+
- Make `manageAccounts` arguments extend `RestrictedMethodParameters` ([#1687](https://github.com/MetaMask/snaps/pull/1687))
|
|
37
|
+
|
|
38
|
+
## [0.38.0-flask.1]
|
|
39
|
+
### Added
|
|
40
|
+
- Add `snap_getLocale` JSON-RPC method ([#1557](https://github.com/MetaMask/snaps/pull/1557))
|
|
41
|
+
- This will let snaps get the user locale from the client.
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
- Fix ed25519 public key derivation ([#1678](https://github.com/MetaMask/snaps/pull/1678))
|
|
45
|
+
|
|
46
|
+
## [0.37.2-flask.1]
|
|
47
|
+
### Changed
|
|
48
|
+
- Release package independently ([#1600](https://github.com/MetaMask/snaps/pull/1600))
|
|
49
|
+
- The version of the package no longer needs to match the version of all other
|
|
50
|
+
MetaMask Snaps packages.
|
|
51
|
+
|
|
52
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.1.0...HEAD
|
|
53
|
+
[3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.0.0...@metamask/snaps-rpc-methods@3.1.0
|
|
54
|
+
[3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@2.0.0...@metamask/snaps-rpc-methods@3.0.0
|
|
55
|
+
[2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.38.3-flask.1...@metamask/snaps-rpc-methods@2.0.0
|
|
56
|
+
[0.38.3-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.38.2-flask.1...@metamask/snaps-rpc-methods@0.38.3-flask.1
|
|
57
|
+
[0.38.2-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.38.1-flask.1...@metamask/snaps-rpc-methods@0.38.2-flask.1
|
|
58
|
+
[0.38.1-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.38.0-flask.1...@metamask/snaps-rpc-methods@0.38.1-flask.1
|
|
59
|
+
[0.38.0-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.37.2-flask.1...@metamask/snaps-rpc-methods@0.38.0-flask.1
|
|
60
|
+
[0.37.2-flask.1]: https://github.com/MetaMask/snaps/releases/tag/@metamask/snaps-rpc-methods@0.37.2-flask.1
|
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Copyright ConsenSys Software Inc. 2021. All rights reserved.
|
|
2
|
+
|
|
3
|
+
You acknowledge and agree that ConsenSys Software Inc. (“ConsenSys”) (or ConsenSys’s licensors) own all legal right, title and interest in and to the work, software, application, source code, documentation and any other documents in this repository (collectively, the “Program”), including any intellectual property rights which subsist in the Program (whether those rights happen to be registered or not, and wherever in the world those rights may exist), whether in source code or any other form.
|
|
4
|
+
|
|
5
|
+
Subject to the limited license below, you may not (and you may not permit anyone else to) distribute, publish, copy, modify, merge, combine with another program, create derivative works of, reverse engineer, decompile or otherwise attempt to extract the source code of, the Program or any part thereof, except that you may contribute to this repository.
|
|
6
|
+
|
|
7
|
+
You are granted a non-exclusive, non-transferable, non-sublicensable license to distribute, publish, copy, modify, merge, combine with another program or create derivative works of the Program (such resulting program, collectively, the “Resulting Program”) solely for Non-Commercial Use as long as you:
|
|
8
|
+
1. give prominent notice (“Notice”) with each copy of the Resulting Program that the Program is used in the Resulting Program and that the Program is the copyright of ConsenSys; and
|
|
9
|
+
2. subject the Resulting Program and any distribution, publication, copy, modification, merger therewith, combination with another program or derivative works thereof to the same Notice requirement and Non-Commercial Use restriction set forth herein.
|
|
10
|
+
|
|
11
|
+
“Non-Commercial Use” means each use as described in clauses (1)-(3) below, as reasonably determined by ConsenSys in its sole discretion:
|
|
12
|
+
1. personal use for research, personal study, private entertainment, hobby projects or amateur pursuits, in each case without any anticipated commercial application;
|
|
13
|
+
2. use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization or government institution; or
|
|
14
|
+
3. the number of monthly active users of the Resulting Program across all versions thereof and platforms globally do not exceed 10,000 at any time.
|
|
15
|
+
|
|
16
|
+
You will not use any trade mark, service mark, trade name, logo of ConsenSys or any other company or organization in a way that is likely or intended to cause confusion about the owner or authorized user of such marks, names or logos.
|
|
17
|
+
|
|
18
|
+
If you have any questions, comments or interest in pursuing any other use cases, please reach out to us at metamask.license@consensys.net.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
permittedMethods: function() {
|
|
13
|
+
return _permitted.handlers;
|
|
14
|
+
},
|
|
15
|
+
createSnapsMethodMiddleware: function() {
|
|
16
|
+
return _permitted.createSnapsMethodMiddleware;
|
|
17
|
+
},
|
|
18
|
+
SnapCaveatType: function() {
|
|
19
|
+
return _snapsutils.SnapCaveatType;
|
|
20
|
+
},
|
|
21
|
+
selectHooks: function() {
|
|
22
|
+
return _utils.selectHooks;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const _permitted = require("./permitted");
|
|
26
|
+
_export_star(require("./restricted"), exports);
|
|
27
|
+
const _snapsutils = require("@metamask/snaps-utils");
|
|
28
|
+
const _utils = require("./utils");
|
|
29
|
+
function _export_star(from, to) {
|
|
30
|
+
Object.keys(from).forEach(function(k) {
|
|
31
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
32
|
+
Object.defineProperty(to, k, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function() {
|
|
35
|
+
return from[k];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return from;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type { PermittedRpcMethodHooks } from './permitted';\nexport {\n handlers as permittedMethods,\n createSnapsMethodMiddleware,\n} from './permitted';\nexport * from './restricted';\nexport { SnapCaveatType } from '@metamask/snaps-utils';\nexport { selectHooks } from './utils';\nexport type { RequestFunction, SnapsGlobalObject } from './request';\n"],"names":["permittedMethods","handlers","createSnapsMethodMiddleware","SnapCaveatType","selectHooks"],"mappings":";;;;;;;;;;;IAEcA,gBAAgB;eAA5BC,mBAAQ;;IACRC,2BAA2B;eAA3BA,sCAA2B;;IAGpBC,cAAc;eAAdA,0BAAc;;IACdC,WAAW;eAAXA,kBAAW;;;2BAHb;qBACO;4BACiB;uBACH"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "handleInstallSnaps", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return handleInstallSnaps;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _rpcerrors = require("@metamask/rpc-errors");
|
|
12
|
+
const _utils = require("@metamask/utils");
|
|
13
|
+
async function handleInstallSnaps(requestedSnaps, installSnaps) {
|
|
14
|
+
if (!(0, _utils.isObject)(requestedSnaps)) {
|
|
15
|
+
throw _rpcerrors.rpcErrors.invalidParams({
|
|
16
|
+
message: `Invalid snap installation params.`,
|
|
17
|
+
data: {
|
|
18
|
+
requestedSnaps
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
} else if (Object.keys(requestedSnaps).length === 0) {
|
|
22
|
+
throw _rpcerrors.rpcErrors.invalidParams({
|
|
23
|
+
message: `Must specify at least one snap to install.`,
|
|
24
|
+
data: {
|
|
25
|
+
requestedSnaps
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
// installSnaps is bound to the origin
|
|
30
|
+
return await installSnaps(requestedSnaps);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=snapInstallation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/permitted/common/snapInstallation.ts"],"sourcesContent":["import type { RequestedPermissions } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { InstallSnapsResult } from '@metamask/snaps-utils';\nimport { isObject } from '@metamask/utils';\n\nexport type { InstallSnapsResult } from '@metamask/snaps-utils';\n\nexport type InstallSnapsHook = (\n requestedSnaps: RequestedPermissions,\n) => Promise<InstallSnapsResult>;\n\n/**\n * Typechecks the requested snaps and passes them to the permissions\n * controller for installation.\n *\n * @param requestedSnaps - An object containing the requested snaps to be installed. The key of the\n * object is the snap id and the value is potential extra data, i.e. version.\n * @param installSnaps - A function that tries to install a given snap, prompting the user if\n * necessary.\n * @returns An object containing the installed snaps.\n * @throws If the params are invalid or the snap installation fails.\n */\nexport async function handleInstallSnaps(\n requestedSnaps: RequestedPermissions,\n installSnaps: InstallSnapsHook,\n): Promise<InstallSnapsResult> {\n if (!isObject(requestedSnaps)) {\n throw rpcErrors.invalidParams({\n message: `Invalid snap installation params.`,\n data: { requestedSnaps },\n });\n } else if (Object.keys(requestedSnaps).length === 0) {\n throw rpcErrors.invalidParams({\n message: `Must specify at least one snap to install.`,\n data: { requestedSnaps },\n });\n }\n\n // installSnaps is bound to the origin\n return await installSnaps(requestedSnaps);\n}\n"],"names":["handleInstallSnaps","requestedSnaps","installSnaps","isObject","rpcErrors","invalidParams","message","data","Object","keys","length"],"mappings":";;;;+BAsBsBA;;;eAAAA;;;2BArBI;uBAED;AAmBlB,eAAeA,mBACpBC,cAAoC,EACpCC,YAA8B;IAE9B,IAAI,CAACC,IAAAA,eAAQ,EAACF,iBAAiB;QAC7B,MAAMG,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS,CAAC,iCAAiC,CAAC;YAC5CC,MAAM;gBAAEN;YAAe;QACzB;IACF,OAAO,IAAIO,OAAOC,IAAI,CAACR,gBAAgBS,MAAM,KAAK,GAAG;QACnD,MAAMN,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS,CAAC,0CAA0C,CAAC;YACrDC,MAAM;gBAAEN;YAAe;QACzB;IACF;IAEA,sCAAsC;IACtC,OAAO,MAAMC,aAAaD;AAC5B"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
GetFileArgsStruct: function() {
|
|
13
|
+
return GetFileArgsStruct;
|
|
14
|
+
},
|
|
15
|
+
getFileHandler: function() {
|
|
16
|
+
return getFileHandler;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _rpcerrors = require("@metamask/rpc-errors");
|
|
20
|
+
const _snapsutils = require("@metamask/snaps-utils");
|
|
21
|
+
const _utils = require("@metamask/utils");
|
|
22
|
+
const _superstruct = require("superstruct");
|
|
23
|
+
const GetFileArgsStruct = (0, _superstruct.object)({
|
|
24
|
+
path: (0, _superstruct.string)(),
|
|
25
|
+
encoding: (0, _superstruct.optional)((0, _superstruct.union)([
|
|
26
|
+
(0, _snapsutils.enumValue)(_snapsutils.AuxiliaryFileEncoding.Base64),
|
|
27
|
+
(0, _snapsutils.enumValue)(_snapsutils.AuxiliaryFileEncoding.Hex),
|
|
28
|
+
(0, _snapsutils.enumValue)(_snapsutils.AuxiliaryFileEncoding.Utf8)
|
|
29
|
+
]))
|
|
30
|
+
});
|
|
31
|
+
const hookNames = {
|
|
32
|
+
getSnapFile: true
|
|
33
|
+
};
|
|
34
|
+
const getFileHandler = {
|
|
35
|
+
methodNames: [
|
|
36
|
+
'snap_getFile'
|
|
37
|
+
],
|
|
38
|
+
implementation,
|
|
39
|
+
hookNames
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The `snap_getFile` method implementation.
|
|
43
|
+
*
|
|
44
|
+
* @param req - The JSON-RPC request object.
|
|
45
|
+
* @param res - The JSON-RPC response object.
|
|
46
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
47
|
+
* function.
|
|
48
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
49
|
+
* @param hooks - The RPC method hooks.
|
|
50
|
+
* @param hooks.getSnapFile - The funnction to load a static snap file.
|
|
51
|
+
* @returns Nothing.
|
|
52
|
+
*/ async function implementation(req, res, _next, end, { getSnapFile }) {
|
|
53
|
+
const { params } = req;
|
|
54
|
+
(0, _utils.assertStruct)(params, GetFileArgsStruct, 'Invalid "snap_getFile" parameters', _rpcerrors.rpcErrors.invalidParams);
|
|
55
|
+
try {
|
|
56
|
+
res.result = await getSnapFile(params.path, params.encoding ?? _snapsutils.AuxiliaryFileEncoding.Base64);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
return end(error);
|
|
59
|
+
}
|
|
60
|
+
return end();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=getFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/getFile.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { enumValue, AuxiliaryFileEncoding } from '@metamask/snaps-utils';\nimport type {\n PendingJsonRpcResponse,\n JsonRpcRequest,\n Json,\n} from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\nimport type { Infer } from 'superstruct';\nimport { object, optional, string, union } from 'superstruct';\n\nimport type { MethodHooksObject } from '../utils';\n\nexport const GetFileArgsStruct = object({\n path: string(),\n encoding: optional(\n union([\n enumValue(AuxiliaryFileEncoding.Base64),\n enumValue(AuxiliaryFileEncoding.Hex),\n enumValue(AuxiliaryFileEncoding.Utf8),\n ]),\n ),\n});\n\nexport type GetFileArgs = Infer<typeof GetFileArgsStruct>;\n\nconst hookNames: MethodHooksObject<GetFileHooks> = {\n getSnapFile: true,\n};\n\nexport const getFileHandler: PermittedHandlerExport<\n GetFileHooks,\n GetFileArgs,\n string\n> = {\n methodNames: ['snap_getFile'],\n implementation,\n hookNames,\n};\n\nexport type GetFileHooks = {\n getSnapFile: (\n path: GetFileArgs['path'],\n encoding: GetFileArgs['encoding'],\n ) => Promise<string>;\n};\n\n/**\n * The `snap_getFile` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getSnapFile - The funnction to load a static snap file.\n * @returns Nothing.\n */\nasync function implementation(\n req: JsonRpcRequest<GetFileArgs>,\n res: PendingJsonRpcResponse<Json>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getSnapFile }: GetFileHooks,\n): Promise<void> {\n const { params } = req;\n\n assertStruct(\n params,\n GetFileArgsStruct,\n 'Invalid \"snap_getFile\" parameters',\n rpcErrors.invalidParams,\n );\n\n try {\n res.result = await getSnapFile(\n params.path,\n params.encoding ?? AuxiliaryFileEncoding.Base64,\n );\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"],"names":["GetFileArgsStruct","getFileHandler","object","path","string","encoding","optional","union","enumValue","AuxiliaryFileEncoding","Base64","Hex","Utf8","hookNames","getSnapFile","methodNames","implementation","req","res","_next","end","params","assertStruct","rpcErrors","invalidParams","result","error"],"mappings":";;;;;;;;;;;IAeaA,iBAAiB;eAAjBA;;IAiBAC,cAAc;eAAdA;;;2BA9Ba;4BACuB;uBAMpB;6BAEmB;AAIzC,MAAMD,oBAAoBE,IAAAA,mBAAM,EAAC;IACtCC,MAAMC,IAAAA,mBAAM;IACZC,UAAUC,IAAAA,qBAAQ,EAChBC,IAAAA,kBAAK,EAAC;QACJC,IAAAA,qBAAS,EAACC,iCAAqB,CAACC,MAAM;QACtCF,IAAAA,qBAAS,EAACC,iCAAqB,CAACE,GAAG;QACnCH,IAAAA,qBAAS,EAACC,iCAAqB,CAACG,IAAI;KACrC;AAEL;AAIA,MAAMC,YAA6C;IACjDC,aAAa;AACf;AAEO,MAAMb,iBAIT;IACFc,aAAa;QAAC;KAAe;IAC7BC;IACAH;AACF;AASA;;;;;;;;;;;CAWC,GACD,eAAeG,eACbC,GAAgC,EAChCC,GAAiC,EACjCC,KAAc,EACdC,GAA6B,EAC7B,EAAEN,WAAW,EAAgB;IAE7B,MAAM,EAAEO,MAAM,EAAE,GAAGJ;IAEnBK,IAAAA,mBAAY,EACVD,QACArB,mBACA,qCACAuB,oBAAS,CAACC,aAAa;IAGzB,IAAI;QACFN,IAAIO,MAAM,GAAG,MAAMX,YACjBO,OAAOlB,IAAI,EACXkB,OAAOhB,QAAQ,IAAII,iCAAqB,CAACC,MAAM;IAEnD,EAAE,OAAOgB,OAAO;QACd,OAAON,IAAIM;IACb;IAEA,OAAON;AACT"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "getSnapsHandler", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return getSnapsHandler;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const hookNames = {
|
|
12
|
+
getSnaps: true
|
|
13
|
+
};
|
|
14
|
+
const getSnapsHandler = {
|
|
15
|
+
methodNames: [
|
|
16
|
+
'wallet_getSnaps'
|
|
17
|
+
],
|
|
18
|
+
implementation: getSnapsImplementation,
|
|
19
|
+
hookNames
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The `wallet_getSnaps` method implementation.
|
|
23
|
+
* Fetches available snaps for the requesting origin and adds them to the JSON-RPC response.
|
|
24
|
+
*
|
|
25
|
+
* @param _req - The JSON-RPC request object. Not used by this function.
|
|
26
|
+
* @param res - The JSON-RPC response object.
|
|
27
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
28
|
+
* function.
|
|
29
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
30
|
+
* @param hooks - The RPC method hooks.
|
|
31
|
+
* @param hooks.getSnaps - A function that returns the snaps available for the requesting origin.
|
|
32
|
+
* @returns Nothing.
|
|
33
|
+
*/ async function getSnapsImplementation(_req, res, _next, end, { getSnaps }) {
|
|
34
|
+
// getSnaps is already bound to the origin
|
|
35
|
+
res.result = await getSnaps();
|
|
36
|
+
return end();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=getSnaps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/getSnaps.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport type { InstallSnapsResult } from '@metamask/snaps-utils';\nimport type { JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetSnapsHooks> = {\n getSnaps: true,\n};\n\n/**\n * `wallet_getSnaps` gets the requester's permitted and installed Snaps.\n */\nexport const getSnapsHandler: PermittedHandlerExport<\n GetSnapsHooks,\n JsonRpcParams,\n InstallSnapsResult\n> = {\n methodNames: ['wallet_getSnaps'],\n implementation: getSnapsImplementation,\n hookNames,\n};\n\nexport type GetSnapsHooks = {\n /**\n * @returns The permitted and installed snaps for the requesting origin.\n */\n getSnaps: () => Promise<InstallSnapsResult>;\n};\n\n/**\n * The `wallet_getSnaps` method implementation.\n * Fetches available snaps for the requesting origin and adds them to the JSON-RPC response.\n *\n * @param _req - The JSON-RPC request object. Not used by this function.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getSnaps - A function that returns the snaps available for the requesting origin.\n * @returns Nothing.\n */\nasync function getSnapsImplementation(\n _req: unknown,\n res: PendingJsonRpcResponse<InstallSnapsResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getSnaps }: GetSnapsHooks,\n): Promise<void> {\n // getSnaps is already bound to the origin\n res.result = await getSnaps();\n return end();\n}\n"],"names":["getSnapsHandler","hookNames","getSnaps","methodNames","implementation","getSnapsImplementation","_req","res","_next","end","result"],"mappings":";;;;+BAcaA;;;eAAAA;;;AAPb,MAAMC,YAA8C;IAClDC,UAAU;AACZ;AAKO,MAAMF,kBAIT;IACFG,aAAa;QAAC;KAAkB;IAChCC,gBAAgBC;IAChBJ;AACF;AASA;;;;;;;;;;;;CAYC,GACD,eAAeI,uBACbC,IAAa,EACbC,GAA+C,EAC/CC,KAAc,EACdC,GAA6B,EAC7B,EAAEP,QAAQ,EAAiB;IAE3B,0CAA0C;IAC1CK,IAAIG,MAAM,GAAG,MAAMR;IACnB,OAAOO;AACT"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
methodHandlers: function() {
|
|
13
|
+
return methodHandlers;
|
|
14
|
+
},
|
|
15
|
+
handlers: function() {
|
|
16
|
+
return handlers;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _getFile = require("./getFile");
|
|
20
|
+
const _getSnaps = require("./getSnaps");
|
|
21
|
+
const _invokeKeyring = require("./invokeKeyring");
|
|
22
|
+
const _invokeSnapSugar = require("./invokeSnapSugar");
|
|
23
|
+
const _requestSnaps = require("./requestSnaps");
|
|
24
|
+
const methodHandlers = {
|
|
25
|
+
wallet_getSnaps: _getSnaps.getSnapsHandler,
|
|
26
|
+
wallet_requestSnaps: _requestSnaps.requestSnapsHandler,
|
|
27
|
+
wallet_invokeSnap: _invokeSnapSugar.invokeSnapSugarHandler,
|
|
28
|
+
wallet_invokeKeyring: _invokeKeyring.invokeKeyringHandler,
|
|
29
|
+
snap_getFile: _getFile.getFileHandler
|
|
30
|
+
};
|
|
31
|
+
const handlers = Object.values(methodHandlers);
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=handlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/handlers.ts"],"sourcesContent":["import { getFileHandler } from './getFile';\nimport { getSnapsHandler } from './getSnaps';\nimport { invokeKeyringHandler } from './invokeKeyring';\nimport { invokeSnapSugarHandler } from './invokeSnapSugar';\nimport { requestSnapsHandler } from './requestSnaps';\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const methodHandlers = {\n wallet_getSnaps: getSnapsHandler,\n wallet_requestSnaps: requestSnapsHandler,\n wallet_invokeSnap: invokeSnapSugarHandler,\n wallet_invokeKeyring: invokeKeyringHandler,\n snap_getFile: getFileHandler,\n};\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport const handlers = Object.values(methodHandlers);\n"],"names":["methodHandlers","handlers","wallet_getSnaps","getSnapsHandler","wallet_requestSnaps","requestSnapsHandler","wallet_invokeSnap","invokeSnapSugarHandler","wallet_invokeKeyring","invokeKeyringHandler","snap_getFile","getFileHandler","Object","values"],"mappings":";;;;;;;;;;;IAOaA,cAAc;eAAdA;;IASAC,QAAQ;eAARA;;;yBAhBkB;0BACC;+BACK;iCACE;8BACH;AAG7B,MAAMD,iBAAiB;IAC5BE,iBAAiBC,yBAAe;IAChCC,qBAAqBC,iCAAmB;IACxCC,mBAAmBC,uCAAsB;IACzCC,sBAAsBC,mCAAoB;IAC1CC,cAAcC,uBAAc;AAC9B;AAGO,MAAMV,WAAWW,OAAOC,MAAM,CAACb"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./handlers"), exports);
|
|
6
|
+
_export_star(require("./middleware"), exports);
|
|
7
|
+
function _export_star(from, to) {
|
|
8
|
+
Object.keys(from).forEach(function(k) {
|
|
9
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
10
|
+
Object.defineProperty(to, k, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function() {
|
|
13
|
+
return from[k];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return from;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/index.ts"],"sourcesContent":["import type { GetSnapsHooks } from './getSnaps';\nimport type { RequestSnapsHooks } from './requestSnaps';\n\nexport type PermittedRpcMethodHooks = GetSnapsHooks & RequestSnapsHooks;\n\nexport * from './handlers';\nexport * from './middleware';\n"],"names":[],"mappings":";;;;qBAKc;qBACA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "invokeKeyringHandler", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return invokeKeyringHandler;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _rpcerrors = require("@metamask/rpc-errors");
|
|
12
|
+
const _snapsutils = require("@metamask/snaps-utils");
|
|
13
|
+
const _utils = require("@metamask/utils");
|
|
14
|
+
const _invokeSnapSugar = require("./invokeSnapSugar");
|
|
15
|
+
const hookNames = {
|
|
16
|
+
hasPermission: true,
|
|
17
|
+
handleSnapRpcRequest: true,
|
|
18
|
+
getSnap: true,
|
|
19
|
+
getAllowedKeyringMethods: true
|
|
20
|
+
};
|
|
21
|
+
const invokeKeyringHandler = {
|
|
22
|
+
methodNames: [
|
|
23
|
+
'wallet_invokeKeyring'
|
|
24
|
+
],
|
|
25
|
+
implementation: invokeKeyringImplementation,
|
|
26
|
+
hookNames
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The `wallet_invokeKeyring` method implementation.
|
|
30
|
+
* Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.
|
|
31
|
+
*
|
|
32
|
+
* @param req - The JSON-RPC request object.
|
|
33
|
+
* @param res - The JSON-RPC response object.
|
|
34
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
35
|
+
* function.
|
|
36
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
37
|
+
* @param hooks - The RPC method hooks.
|
|
38
|
+
* @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.
|
|
39
|
+
* @param hooks.hasPermission - Checks whether a given origin has a given permission.
|
|
40
|
+
* @param hooks.getSnap - Gets information about a given snap.
|
|
41
|
+
* @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring
|
|
42
|
+
* methods for a given origin.
|
|
43
|
+
* @returns Nothing.
|
|
44
|
+
*/ async function invokeKeyringImplementation(req, res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods }) {
|
|
45
|
+
let params;
|
|
46
|
+
try {
|
|
47
|
+
params = (0, _invokeSnapSugar.getValidatedParams)(req.params);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return end(error);
|
|
50
|
+
}
|
|
51
|
+
// We expect the MM middleware stack to always add the origin to requests
|
|
52
|
+
const { origin } = req;
|
|
53
|
+
const { snapId, request } = params;
|
|
54
|
+
if (!origin || !hasPermission(origin, _snapsutils.WALLET_SNAP_PERMISSION_KEY)) {
|
|
55
|
+
return end(_rpcerrors.rpcErrors.invalidRequest({
|
|
56
|
+
message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
if (!getSnap(snapId)) {
|
|
60
|
+
return end(_rpcerrors.rpcErrors.invalidRequest({
|
|
61
|
+
message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
if (!(0, _utils.hasProperty)(request, 'method') || typeof request.method !== 'string') {
|
|
65
|
+
return end(_rpcerrors.rpcErrors.invalidRequest({
|
|
66
|
+
message: 'The request must have a method.'
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
const allowedMethods = getAllowedKeyringMethods(origin);
|
|
70
|
+
if (!allowedMethods.includes(request.method)) {
|
|
71
|
+
return end(_rpcerrors.rpcErrors.invalidRequest({
|
|
72
|
+
message: `The origin "${origin}" is not allowed to invoke the method "${request.method}".`
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
res.result = await handleSnapRpcRequest({
|
|
77
|
+
snapId,
|
|
78
|
+
origin,
|
|
79
|
+
request,
|
|
80
|
+
handler: _snapsutils.HandlerType.OnKeyringRequest
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return end(error);
|
|
84
|
+
}
|
|
85
|
+
return end();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=invokeKeyring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/invokeKeyring.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Snap } from '@metamask/snaps-utils';\nimport {\n HandlerType,\n WALLET_SNAP_PERMISSION_KEY,\n type SnapId,\n type SnapRpcHookArgs,\n} from '@metamask/snaps-utils';\nimport type { PendingJsonRpcResponse, JsonRpcRequest } from '@metamask/utils';\nimport { hasProperty, type Json } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport type { InvokeSnapSugarArgs } from './invokeSnapSugar';\nimport { getValidatedParams } from './invokeSnapSugar';\n\nconst hookNames: MethodHooksObject<InvokeKeyringHooks> = {\n hasPermission: true,\n handleSnapRpcRequest: true,\n getSnap: true,\n getAllowedKeyringMethods: true,\n};\n\n/**\n * `wallet_invokeKeyring` gets the requester's permitted and installed Snaps.\n */\nexport const invokeKeyringHandler: PermittedHandlerExport<\n InvokeKeyringHooks,\n JsonRpcRequest,\n Json\n> = {\n methodNames: ['wallet_invokeKeyring'],\n implementation: invokeKeyringImplementation,\n hookNames,\n};\n\nexport type InvokeKeyringHooks = {\n hasPermission: (origin: string, permissionName: string) => boolean;\n\n handleSnapRpcRequest: ({\n snapId,\n origin,\n handler,\n request,\n }: SnapRpcHookArgs & { snapId: SnapId }) => Promise<unknown>;\n\n getSnap: (snapId: SnapId) => Snap | undefined;\n\n getAllowedKeyringMethods: (origin: string) => string[];\n};\n\n/**\n * The `wallet_invokeKeyring` method implementation.\n * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request.\n * @param hooks.hasPermission - Checks whether a given origin has a given permission.\n * @param hooks.getSnap - Gets information about a given snap.\n * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring\n * methods for a given origin.\n * @returns Nothing.\n */\nasync function invokeKeyringImplementation(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse<Json>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n handleSnapRpcRequest,\n hasPermission,\n getSnap,\n getAllowedKeyringMethods,\n }: InvokeKeyringHooks,\n): Promise<void> {\n let params: InvokeSnapSugarArgs;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n // We expect the MM middleware stack to always add the origin to requests\n const { origin } = req as JsonRpcRequest & { origin: string };\n const { snapId, request } = params;\n\n if (!origin || !hasPermission(origin, WALLET_SNAP_PERMISSION_KEY)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not connected to \"${origin}\". Please connect before invoking the snap.`,\n }),\n );\n }\n\n if (!getSnap(snapId)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The snap \"${snapId}\" is not installed. Please install it first, before invoking the snap.`,\n }),\n );\n }\n\n if (!hasProperty(request, 'method') || typeof request.method !== 'string') {\n return end(\n rpcErrors.invalidRequest({\n message: 'The request must have a method.',\n }),\n );\n }\n\n const allowedMethods = getAllowedKeyringMethods(origin);\n if (!allowedMethods.includes(request.method)) {\n return end(\n rpcErrors.invalidRequest({\n message: `The origin \"${origin}\" is not allowed to invoke the method \"${request.method}\".`,\n }),\n );\n }\n\n try {\n res.result = (await handleSnapRpcRequest({\n snapId,\n origin,\n request,\n handler: HandlerType.OnKeyringRequest,\n })) as Json;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"],"names":["invokeKeyringHandler","hookNames","hasPermission","handleSnapRpcRequest","getSnap","getAllowedKeyringMethods","methodNames","implementation","invokeKeyringImplementation","req","res","_next","end","params","getValidatedParams","error","origin","snapId","request","WALLET_SNAP_PERMISSION_KEY","rpcErrors","invalidRequest","message","hasProperty","method","allowedMethods","includes","result","handler","HandlerType","OnKeyringRequest"],"mappings":";;;;+BA2BaA;;;eAAAA;;;2BAzBa;4BAOnB;uBAEgC;iCAIJ;AAEnC,MAAMC,YAAmD;IACvDC,eAAe;IACfC,sBAAsB;IACtBC,SAAS;IACTC,0BAA0B;AAC5B;AAKO,MAAML,uBAIT;IACFM,aAAa;QAAC;KAAuB;IACrCC,gBAAgBC;IAChBP;AACF;AAiBA;;;;;;;;;;;;;;;;CAgBC,GACD,eAAeO,4BACbC,GAAmB,EACnBC,GAAiC,EACjCC,KAAc,EACdC,GAA6B,EAC7B,EACET,oBAAoB,EACpBD,aAAa,EACbE,OAAO,EACPC,wBAAwB,EACL;IAErB,IAAIQ;IACJ,IAAI;QACFA,SAASC,IAAAA,mCAAkB,EAACL,IAAII,MAAM;IACxC,EAAE,OAAOE,OAAO;QACd,OAAOH,IAAIG;IACb;IAEA,yEAAyE;IACzE,MAAM,EAAEC,MAAM,EAAE,GAAGP;IACnB,MAAM,EAAEQ,MAAM,EAAEC,OAAO,EAAE,GAAGL;IAE5B,IAAI,CAACG,UAAU,CAACd,cAAcc,QAAQG,sCAA0B,GAAG;QACjE,OAAOP,IACLQ,oBAAS,CAACC,cAAc,CAAC;YACvBC,SAAS,CAAC,UAAU,EAAEL,OAAO,uBAAuB,EAAED,OAAO,2CAA2C,CAAC;QAC3G;IAEJ;IAEA,IAAI,CAACZ,QAAQa,SAAS;QACpB,OAAOL,IACLQ,oBAAS,CAACC,cAAc,CAAC;YACvBC,SAAS,CAAC,UAAU,EAAEL,OAAO,sEAAsE,CAAC;QACtG;IAEJ;IAEA,IAAI,CAACM,IAAAA,kBAAW,EAACL,SAAS,aAAa,OAAOA,QAAQM,MAAM,KAAK,UAAU;QACzE,OAAOZ,IACLQ,oBAAS,CAACC,cAAc,CAAC;YACvBC,SAAS;QACX;IAEJ;IAEA,MAAMG,iBAAiBpB,yBAAyBW;IAChD,IAAI,CAACS,eAAeC,QAAQ,CAACR,QAAQM,MAAM,GAAG;QAC5C,OAAOZ,IACLQ,oBAAS,CAACC,cAAc,CAAC;YACvBC,SAAS,CAAC,YAAY,EAAEN,OAAO,uCAAuC,EAAEE,QAAQM,MAAM,CAAC,EAAE,CAAC;QAC5F;IAEJ;IAEA,IAAI;QACFd,IAAIiB,MAAM,GAAI,MAAMxB,qBAAqB;YACvCc;YACAD;YACAE;YACAU,SAASC,uBAAW,CAACC,gBAAgB;QACvC;IACF,EAAE,OAAOf,OAAO;QACd,OAAOH,IAAIG;IACb;IAEA,OAAOH;AACT"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
invokeSnapSugarHandler: function() {
|
|
13
|
+
return invokeSnapSugarHandler;
|
|
14
|
+
},
|
|
15
|
+
invokeSnapSugar: function() {
|
|
16
|
+
return invokeSnapSugar;
|
|
17
|
+
},
|
|
18
|
+
getValidatedParams: function() {
|
|
19
|
+
return getValidatedParams;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _rpcerrors = require("@metamask/rpc-errors");
|
|
23
|
+
const _utils = require("@metamask/utils");
|
|
24
|
+
const invokeSnapSugarHandler = {
|
|
25
|
+
methodNames: [
|
|
26
|
+
'wallet_invokeSnap'
|
|
27
|
+
],
|
|
28
|
+
implementation: invokeSnapSugar,
|
|
29
|
+
hookNames: undefined
|
|
30
|
+
};
|
|
31
|
+
function invokeSnapSugar(req, _res, next, end) {
|
|
32
|
+
let params;
|
|
33
|
+
try {
|
|
34
|
+
params = getValidatedParams(req.params);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return end(error);
|
|
37
|
+
}
|
|
38
|
+
req.method = 'wallet_snap';
|
|
39
|
+
req.params = params;
|
|
40
|
+
return next();
|
|
41
|
+
}
|
|
42
|
+
function getValidatedParams(params) {
|
|
43
|
+
if (!(0, _utils.isObject)(params)) {
|
|
44
|
+
throw _rpcerrors.rpcErrors.invalidParams({
|
|
45
|
+
message: 'Expected params to be a single object.'
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const { snapId, request } = params;
|
|
49
|
+
if (!snapId || typeof snapId !== 'string' || snapId === '') {
|
|
50
|
+
throw _rpcerrors.rpcErrors.invalidParams({
|
|
51
|
+
message: 'Must specify a valid snap ID.'
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (!(0, _utils.isObject)(request)) {
|
|
55
|
+
throw _rpcerrors.rpcErrors.invalidParams({
|
|
56
|
+
message: 'Expected request to be a single object.'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return params;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=invokeSnapSugar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/invokeSnapSugar.ts"],"sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nexport type InvokeSnapSugarArgs = {\n snapId: string;\n request: Record<string, Json>;\n};\n\n/**\n * `wallet_invokeSnap` attempts to invoke an RPC method of the specified Snap.\n */\nexport const invokeSnapSugarHandler: PermittedHandlerExport<\n void,\n InvokeSnapSugarArgs,\n Json\n> = {\n methodNames: ['wallet_invokeSnap'],\n implementation: invokeSnapSugar,\n hookNames: undefined,\n};\n\n/**\n * The `wallet_invokeSnap` method implementation.\n * Reroutes incoming JSON-RPC requests that are targeting snaps, by modifying the method and params.\n *\n * @param req - The JSON-RPC request object.\n * @param _res - The JSON-RPC response object. Not used by this\n * function.\n * @param next - The `json-rpc-engine` \"next\" callback.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @returns Nothing.\n * @throws If the params are invalid.\n */\nexport function invokeSnapSugar(\n req: JsonRpcRequest<InvokeSnapSugarArgs>,\n _res: unknown,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n): void {\n let params: InvokeSnapSugarArgs;\n try {\n params = getValidatedParams(req.params);\n } catch (error) {\n return end(error);\n }\n\n req.method = 'wallet_snap';\n req.params = params;\n return next();\n}\n\n/**\n * Validates the wallet_invokeSnap method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): InvokeSnapSugarArgs {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { snapId, request } = params;\n\n if (!snapId || typeof snapId !== 'string' || snapId === '') {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid snap ID.',\n });\n }\n\n if (!isObject(request)) {\n throw rpcErrors.invalidParams({\n message: 'Expected request to be a single object.',\n });\n }\n\n return params as InvokeSnapSugarArgs;\n}\n"],"names":["invokeSnapSugarHandler","invokeSnapSugar","getValidatedParams","methodNames","implementation","hookNames","undefined","req","_res","next","end","params","error","method","isObject","rpcErrors","invalidParams","message","snapId","request"],"mappings":";;;;;;;;;;;IAiBaA,sBAAsB;eAAtBA;;IAsBGC,eAAe;eAAfA;;IAyBAC,kBAAkB;eAAlBA;;;2BA3DU;uBAED;AAUlB,MAAMF,yBAIT;IACFG,aAAa;QAAC;KAAoB;IAClCC,gBAAgBH;IAChBI,WAAWC;AACb;AAcO,SAASL,gBACdM,GAAwC,EACxCC,IAAa,EACbC,IAA+B,EAC/BC,GAA6B;IAE7B,IAAIC;IACJ,IAAI;QACFA,SAAST,mBAAmBK,IAAII,MAAM;IACxC,EAAE,OAAOC,OAAO;QACd,OAAOF,IAAIE;IACb;IAEAL,IAAIM,MAAM,GAAG;IACbN,IAAII,MAAM,GAAGA;IACb,OAAOF;AACT;AASO,SAASP,mBAAmBS,MAAe;IAChD,IAAI,CAACG,IAAAA,eAAQ,EAACH,SAAS;QACrB,MAAMI,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,MAAM,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGR;IAE5B,IAAI,CAACO,UAAU,OAAOA,WAAW,YAAYA,WAAW,IAAI;QAC1D,MAAMH,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,IAAI,CAACH,IAAAA,eAAQ,EAACK,UAAU;QACtB,MAAMJ,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,OAAON;AACT"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "createSnapsMethodMiddleware", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return createSnapsMethodMiddleware;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _rpcerrors = require("@metamask/rpc-errors");
|
|
12
|
+
const _snapsutils = require("@metamask/snaps-utils");
|
|
13
|
+
const _utils = require("../utils");
|
|
14
|
+
const _handlers = require("./handlers");
|
|
15
|
+
function createSnapsMethodMiddleware(isSnap, hooks) {
|
|
16
|
+
// This is not actually a misused promise, the type is just wrong
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
18
|
+
return async function methodMiddleware(request, response, next, end) {
|
|
19
|
+
const handler = _handlers.methodHandlers[request.method];
|
|
20
|
+
if (handler) {
|
|
21
|
+
if (request.method.startsWith('snap_') && !isSnap) {
|
|
22
|
+
return end(_rpcerrors.rpcErrors.methodNotFound());
|
|
23
|
+
}
|
|
24
|
+
// TODO: Once json-rpc-engine types are up to date, we should type this correctly
|
|
25
|
+
const { implementation, hookNames } = handler;
|
|
26
|
+
try {
|
|
27
|
+
// Implementations may or may not be async, so we must await them.
|
|
28
|
+
return await implementation(request, response, next, end, (0, _utils.selectHooks)(hooks, hookNames));
|
|
29
|
+
} catch (error) {
|
|
30
|
+
(0, _snapsutils.logError)(error);
|
|
31
|
+
return end(error);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return next();
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/permitted/middleware.ts"],"sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { selectHooks } from '../utils';\nimport { methodHandlers } from './handlers';\n\n/**\n * Creates a middleware that handles permitted snap RPC methods.\n *\n * @param isSnap - A flag that should indicate whether the requesting origin is a snap or not.\n * @param hooks - An object containing the hooks made available to the permitted RPC methods.\n * @returns The middleware.\n */\nexport function createSnapsMethodMiddleware(\n isSnap: boolean,\n hooks: Record<string, unknown>,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n // This is not actually a misused promise, the type is just wrong\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n return async function methodMiddleware(request, response, next, end) {\n const handler =\n methodHandlers[request.method as keyof typeof methodHandlers];\n if (handler) {\n if (request.method.startsWith('snap_') && !isSnap) {\n return end(rpcErrors.methodNotFound());\n }\n\n // TODO: Once json-rpc-engine types are up to date, we should type this correctly\n const { implementation, hookNames } = handler as any;\n try {\n // Implementations may or may not be async, so we must await them.\n return await implementation(\n request,\n response,\n next,\n end,\n selectHooks(hooks, hookNames),\n );\n } catch (error) {\n logError(error);\n return end(error);\n }\n }\n\n return next();\n };\n}\n"],"names":["createSnapsMethodMiddleware","isSnap","hooks","methodMiddleware","request","response","next","end","handler","methodHandlers","method","startsWith","rpcErrors","methodNotFound","implementation","hookNames","selectHooks","error","logError"],"mappings":";;;;+BAegBA;;;eAAAA;;;2BAdU;4BACD;uBAGG;0BACG;AASxB,SAASA,4BACdC,MAAe,EACfC,KAA8B;IAE9B,iEAAiE;IACjE,kEAAkE;IAClE,OAAO,eAAeC,iBAAiBC,OAAO,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,GAAG;QACjE,MAAMC,UACJC,wBAAc,CAACL,QAAQM,MAAM,CAAgC;QAC/D,IAAIF,SAAS;YACX,IAAIJ,QAAQM,MAAM,CAACC,UAAU,CAAC,YAAY,CAACV,QAAQ;gBACjD,OAAOM,IAAIK,oBAAS,CAACC,cAAc;YACrC;YAEA,iFAAiF;YACjF,MAAM,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGP;YACtC,IAAI;gBACF,kEAAkE;gBAClE,OAAO,MAAMM,eACXV,SACAC,UACAC,MACAC,KACAS,IAAAA,kBAAW,EAACd,OAAOa;YAEvB,EAAE,OAAOE,OAAO;gBACdC,IAAAA,oBAAQ,EAACD;gBACT,OAAOV,IAAIU;YACb;QACF;QAEA,OAAOX;IACT;AACF"}
|