@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
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { PermissionSpecificationBuilder, RestrictedMethodOptions, PermissionValidatorConstraint, PermissionSideEffect } from '@metamask/permission-controller';
|
|
2
|
+
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { Snap, SnapId, SnapRpcHookArgs, RequestedSnapPermissions, InstallSnapsResult } from '@metamask/snaps-utils';
|
|
4
|
+
import type { Json, NonEmptyArray } from '@metamask/utils';
|
|
5
|
+
import type { MethodHooksObject } from '../utils';
|
|
6
|
+
export declare const WALLET_SNAP_PERMISSION_KEY = "wallet_snap";
|
|
7
|
+
export declare type InstallSnaps = {
|
|
8
|
+
type: `SnapController:install`;
|
|
9
|
+
handler: (origin: string, requestedSnaps: RequestedSnapPermissions) => Promise<InstallSnapsResult>;
|
|
10
|
+
};
|
|
11
|
+
export declare type GetPermittedSnaps = {
|
|
12
|
+
type: `SnapController:getPermitted`;
|
|
13
|
+
handler: (origin: string) => InstallSnapsResult;
|
|
14
|
+
};
|
|
15
|
+
declare type AllowedActions = InstallSnaps | GetPermittedSnaps;
|
|
16
|
+
export declare type InvokeSnapMethodHooks = {
|
|
17
|
+
getSnap: (snapId: SnapId) => Snap | undefined;
|
|
18
|
+
handleSnapRpcRequest: ({ snapId, origin, handler, request, }: SnapRpcHookArgs & {
|
|
19
|
+
snapId: SnapId;
|
|
20
|
+
}) => Promise<unknown>;
|
|
21
|
+
};
|
|
22
|
+
declare type InvokeSnapSpecificationBuilderOptions = {
|
|
23
|
+
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
24
|
+
methodHooks: InvokeSnapMethodHooks;
|
|
25
|
+
};
|
|
26
|
+
export declare type InvokeSnapParams = {
|
|
27
|
+
snapId: string;
|
|
28
|
+
request: Record<string, unknown>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The side-effect method to handle the snap install.
|
|
32
|
+
*
|
|
33
|
+
* @param params - The side-effect params.
|
|
34
|
+
* @param params.requestData - The request data associated to the requested permission.
|
|
35
|
+
* @param params.messagingSystem - The messenger to call an action.
|
|
36
|
+
*/
|
|
37
|
+
export declare const handleSnapInstall: PermissionSideEffect<AllowedActions, never>['onPermitted'];
|
|
38
|
+
export declare const invokeSnapBuilder: Readonly<{
|
|
39
|
+
readonly targetName: "wallet_snap";
|
|
40
|
+
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, InvokeSnapSpecificationBuilderOptions, {
|
|
41
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
42
|
+
targetName: typeof WALLET_SNAP_PERMISSION_KEY;
|
|
43
|
+
methodImplementation: ReturnType<typeof getInvokeSnapImplementation>;
|
|
44
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
45
|
+
validator: PermissionValidatorConstraint;
|
|
46
|
+
sideEffect: {
|
|
47
|
+
onPermitted: PermissionSideEffect<AllowedActions, never>['onPermitted'];
|
|
48
|
+
};
|
|
49
|
+
}>;
|
|
50
|
+
readonly methodHooks: MethodHooksObject<InvokeSnapMethodHooks>;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Builds the method implementation for `wallet_snap_*`.
|
|
54
|
+
*
|
|
55
|
+
* @param hooks - The RPC method hooks.
|
|
56
|
+
* @param hooks.getSnap - A function that retrieves all information stored about a snap.
|
|
57
|
+
* @param hooks.handleSnapRpcRequest - A function that sends an RPC request to a snap's RPC handler or throws if that fails.
|
|
58
|
+
* @returns The method implementation which returns the result of `handleSnapRpcRequest`.
|
|
59
|
+
* @throws If the params are invalid.
|
|
60
|
+
*/
|
|
61
|
+
export declare function getInvokeSnapImplementation({ getSnap, handleSnapRpcRequest, }: InvokeSnapMethodHooks): (options: RestrictedMethodOptions<Record<string, Json>>) => Promise<Json>;
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { RestrictedMethodOptions, ValidPermissionSpecification, PermissionSpecificationBuilder } from '@metamask/permission-controller';
|
|
2
|
+
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { Json, NonEmptyArray } from '@metamask/utils';
|
|
4
|
+
import type { Infer } from 'superstruct';
|
|
5
|
+
declare const SnapMessageStruct: import("superstruct").Struct<{
|
|
6
|
+
method: string;
|
|
7
|
+
} | {
|
|
8
|
+
params: Record<string, Json> | Json[];
|
|
9
|
+
method: string;
|
|
10
|
+
}, null>;
|
|
11
|
+
declare type Message = Infer<typeof SnapMessageStruct>;
|
|
12
|
+
export declare const methodName = "snap_manageAccounts";
|
|
13
|
+
export declare type ManageAccountsMethodHooks = {
|
|
14
|
+
/**
|
|
15
|
+
* Gets the snap keyring implementation.
|
|
16
|
+
*/
|
|
17
|
+
getSnapKeyring: (snapOrigin: string) => Promise<{
|
|
18
|
+
handleKeyringSnapMessage: (snapId: string, message: Message) => Promise<Json>;
|
|
19
|
+
}>;
|
|
20
|
+
};
|
|
21
|
+
declare type ManageAccountsSpecificationBuilderOptions = {
|
|
22
|
+
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
23
|
+
methodHooks: ManageAccountsMethodHooks;
|
|
24
|
+
};
|
|
25
|
+
declare type ManageAccountsSpecification = ValidPermissionSpecification<{
|
|
26
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
27
|
+
targetName: typeof methodName;
|
|
28
|
+
methodImplementation: ReturnType<typeof manageAccountsImplementation>;
|
|
29
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* The specification builder for the `snap_manageAccounts` permission.
|
|
33
|
+
* `snap_manageAccounts` lets the Snap manage a set of accounts via a custom keyring.
|
|
34
|
+
*
|
|
35
|
+
* @param options - The specification builder options.
|
|
36
|
+
* @param options.allowedCaveats - The optional allowed caveats for the permission.
|
|
37
|
+
* @param options.methodHooks - The RPC method hooks needed by the method implementation.
|
|
38
|
+
* @returns The specification for the `snap_manageAccounts` permission.
|
|
39
|
+
*/
|
|
40
|
+
export declare const specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, ManageAccountsSpecificationBuilderOptions, ManageAccountsSpecification>;
|
|
41
|
+
/**
|
|
42
|
+
* Builds the method implementation for `snap_manageAccounts`.
|
|
43
|
+
*
|
|
44
|
+
* @param hooks - The RPC method hooks.
|
|
45
|
+
* @param hooks.getSnapKeyring - A function to get the snap keyring.
|
|
46
|
+
* @returns The method implementation which either returns `null` for a
|
|
47
|
+
* successful state update/deletion or returns the decrypted state.
|
|
48
|
+
* @throws If the params are invalid.
|
|
49
|
+
*/
|
|
50
|
+
export declare function manageAccountsImplementation({ getSnapKeyring, }: ManageAccountsMethodHooks): (options: RestrictedMethodOptions<Message>) => Promise<Json>;
|
|
51
|
+
export declare const manageAccountsBuilder: Readonly<{
|
|
52
|
+
readonly targetName: "snap_manageAccounts";
|
|
53
|
+
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, ManageAccountsSpecificationBuilderOptions, {
|
|
54
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
55
|
+
targetName: typeof methodName;
|
|
56
|
+
methodImplementation: ReturnType<typeof manageAccountsImplementation>;
|
|
57
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
58
|
+
}>;
|
|
59
|
+
readonly methodHooks: {
|
|
60
|
+
readonly getSnapKeyring: true;
|
|
61
|
+
};
|
|
62
|
+
}>;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { PermissionSpecificationBuilder, RestrictedMethodOptions, ValidPermissionSpecification } from '@metamask/permission-controller';
|
|
2
|
+
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { EnumToUnion } from '@metamask/snaps-utils';
|
|
4
|
+
import type { Json, NonEmptyArray, Hex } from '@metamask/utils';
|
|
5
|
+
import type { MethodHooksObject } from '../utils';
|
|
6
|
+
export declare const STATE_ENCRYPTION_SALT = "snap_manageState encryption";
|
|
7
|
+
declare const methodName = "snap_manageState";
|
|
8
|
+
export declare type ManageStateMethodHooks = {
|
|
9
|
+
/**
|
|
10
|
+
* @returns The mnemonic of the user's primary keyring.
|
|
11
|
+
*/
|
|
12
|
+
getMnemonic: () => Promise<Uint8Array>;
|
|
13
|
+
/**
|
|
14
|
+
* Waits for the extension to be unlocked.
|
|
15
|
+
*
|
|
16
|
+
* @returns A promise that resolves once the extension is unlocked.
|
|
17
|
+
*/
|
|
18
|
+
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* A function that clears the state of the requesting Snap.
|
|
21
|
+
*/
|
|
22
|
+
clearSnapState: (snapId: string) => Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* A function that gets the encrypted state of the requesting Snap.
|
|
25
|
+
*
|
|
26
|
+
* @returns The current state of the Snap.
|
|
27
|
+
*/
|
|
28
|
+
getSnapState: (snapId: string) => Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* A function that updates the state of the requesting Snap.
|
|
31
|
+
*
|
|
32
|
+
* @param newState - The new state of the Snap.
|
|
33
|
+
*/
|
|
34
|
+
updateSnapState: (snapId: string, newState: string) => Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Encrypts data with a key. This is assumed to perform symmetric encryption.
|
|
37
|
+
*
|
|
38
|
+
* @param key - The key to use for encryption, in hexadecimal format.
|
|
39
|
+
* @param data - The JSON data to encrypt.
|
|
40
|
+
* @returns The ciphertext as a string. The format for this string is
|
|
41
|
+
* dependent on the implementation, but MUST be a string.
|
|
42
|
+
*/
|
|
43
|
+
encrypt: (key: string, data: Json) => Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Decrypts data with a key. This is assumed to perform symmetric decryption.
|
|
46
|
+
*
|
|
47
|
+
* @param key - The key to use for decryption, in hexadecimal format.
|
|
48
|
+
* @param cipherText - The ciphertext to decrypt. The format for this string
|
|
49
|
+
* is dependent on the implementation, but MUST be a string.
|
|
50
|
+
* @returns The decrypted data as a JSON object.
|
|
51
|
+
*/
|
|
52
|
+
decrypt: (key: Hex, cipherText: string) => Promise<unknown>;
|
|
53
|
+
};
|
|
54
|
+
declare type ManageStateSpecificationBuilderOptions = {
|
|
55
|
+
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
56
|
+
methodHooks: ManageStateMethodHooks;
|
|
57
|
+
};
|
|
58
|
+
declare type ManageStateSpecification = ValidPermissionSpecification<{
|
|
59
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
60
|
+
targetName: typeof methodName;
|
|
61
|
+
methodImplementation: ReturnType<typeof getManageStateImplementation>;
|
|
62
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* The specification builder for the `snap_manageState` permission.
|
|
66
|
+
* `snap_manageState` lets the Snap store and manage some of its state on
|
|
67
|
+
* your device.
|
|
68
|
+
*
|
|
69
|
+
* @param options - The specification builder options.
|
|
70
|
+
* @param options.allowedCaveats - The optional allowed caveats for the permission.
|
|
71
|
+
* @param options.methodHooks - The RPC method hooks needed by the method implementation.
|
|
72
|
+
* @returns The specification for the `snap_manageState` permission.
|
|
73
|
+
*/
|
|
74
|
+
export declare const specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, ManageStateSpecificationBuilderOptions, ManageStateSpecification>;
|
|
75
|
+
export declare const manageStateBuilder: Readonly<{
|
|
76
|
+
readonly targetName: "snap_manageState";
|
|
77
|
+
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, ManageStateSpecificationBuilderOptions, {
|
|
78
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
79
|
+
targetName: typeof methodName;
|
|
80
|
+
methodImplementation: ReturnType<typeof getManageStateImplementation>;
|
|
81
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
82
|
+
}>;
|
|
83
|
+
readonly methodHooks: MethodHooksObject<ManageStateMethodHooks>;
|
|
84
|
+
}>;
|
|
85
|
+
export declare enum ManageStateOperation {
|
|
86
|
+
ClearState = "clear",
|
|
87
|
+
GetState = "get",
|
|
88
|
+
UpdateState = "update"
|
|
89
|
+
}
|
|
90
|
+
export declare type ManageStateArgs = {
|
|
91
|
+
operation: EnumToUnion<ManageStateOperation>;
|
|
92
|
+
newState?: Record<string, Json>;
|
|
93
|
+
};
|
|
94
|
+
export declare const STORAGE_SIZE_LIMIT = 104857600;
|
|
95
|
+
/**
|
|
96
|
+
* Builds the method implementation for `snap_manageState`.
|
|
97
|
+
*
|
|
98
|
+
* @param hooks - The RPC method hooks.
|
|
99
|
+
* @param hooks.clearSnapState - A function that clears the state stored for a
|
|
100
|
+
* snap.
|
|
101
|
+
* @param hooks.getSnapState - A function that fetches the persisted decrypted
|
|
102
|
+
* state for a snap.
|
|
103
|
+
* @param hooks.updateSnapState - A function that updates the state stored for a
|
|
104
|
+
* snap.
|
|
105
|
+
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase
|
|
106
|
+
* of the user.
|
|
107
|
+
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask
|
|
108
|
+
* extension is unlocked and prompts the user to unlock their MetaMask if it is
|
|
109
|
+
* locked.
|
|
110
|
+
* @param hooks.encrypt - A function that encrypts the given state.
|
|
111
|
+
* @param hooks.decrypt - A function that decrypts the given state.
|
|
112
|
+
* @returns The method implementation which either returns `null` for a
|
|
113
|
+
* successful state update/deletion or returns the decrypted state.
|
|
114
|
+
* @throws If the params are invalid.
|
|
115
|
+
*/
|
|
116
|
+
export declare function getManageStateImplementation({ getMnemonic, getUnlockPromise, clearSnapState, getSnapState, updateSnapState, encrypt, decrypt, }: ManageStateMethodHooks): (options: RestrictedMethodOptions<ManageStateArgs>) => Promise<null | Record<string, Json>>;
|
|
117
|
+
/**
|
|
118
|
+
* Validates the manageState method `params` and returns them cast to the correct
|
|
119
|
+
* type. Throws if validation fails.
|
|
120
|
+
*
|
|
121
|
+
* @param params - The unvalidated params object from the method request.
|
|
122
|
+
* @param method - RPC method name used for debugging errors.
|
|
123
|
+
* @param storageSizeLimit - Maximum allowed size (in bytes) of a new state object.
|
|
124
|
+
* @returns The validated method parameter object.
|
|
125
|
+
*/
|
|
126
|
+
export declare function getValidatedParams(params: unknown, method: string, storageSizeLimit?: number): ManageStateArgs;
|
|
127
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { PermissionSpecificationBuilder, RestrictedMethodOptions, ValidPermissionSpecification } from '@metamask/permission-controller';
|
|
2
|
+
import { PermissionType } from '@metamask/permission-controller';
|
|
3
|
+
import type { EnumToUnion } from '@metamask/snaps-utils';
|
|
4
|
+
import type { NonEmptyArray } from '@metamask/utils';
|
|
5
|
+
import type { MethodHooksObject } from '../utils';
|
|
6
|
+
declare const methodName = "snap_notify";
|
|
7
|
+
export declare enum NotificationType {
|
|
8
|
+
InApp = "inApp",
|
|
9
|
+
Native = "native"
|
|
10
|
+
}
|
|
11
|
+
export declare type NotificationArgs = {
|
|
12
|
+
/**
|
|
13
|
+
* Enum type to determine notification type.
|
|
14
|
+
*/
|
|
15
|
+
type: EnumToUnion<NotificationType>;
|
|
16
|
+
/**
|
|
17
|
+
* A message to show on the notification.
|
|
18
|
+
*/
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
export declare type NotifyMethodHooks = {
|
|
22
|
+
/**
|
|
23
|
+
* @param snapId - The ID of the Snap that created the notification.
|
|
24
|
+
* @param args - The notification arguments.
|
|
25
|
+
*/
|
|
26
|
+
showNativeNotification: (snapId: string, args: NotificationArgs) => Promise<null>;
|
|
27
|
+
/**
|
|
28
|
+
* @param snapId - The ID of the Snap that created the notification.
|
|
29
|
+
* @param args - The notification arguments.
|
|
30
|
+
*/
|
|
31
|
+
showInAppNotification: (snapId: string, args: NotificationArgs) => Promise<null>;
|
|
32
|
+
};
|
|
33
|
+
declare type SpecificationBuilderOptions = {
|
|
34
|
+
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
35
|
+
methodHooks: NotifyMethodHooks;
|
|
36
|
+
};
|
|
37
|
+
declare type Specification = ValidPermissionSpecification<{
|
|
38
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
39
|
+
targetName: typeof methodName;
|
|
40
|
+
methodImplementation: ReturnType<typeof getImplementation>;
|
|
41
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* The specification builder for the `snap_notify` permission.
|
|
45
|
+
* `snap_notify` allows snaps to send multiple types of notifications to its users.
|
|
46
|
+
*
|
|
47
|
+
* @param options - The specification builder options.
|
|
48
|
+
* @param options.allowedCaveats - The optional allowed caveats for the permission.
|
|
49
|
+
* @param options.methodHooks - The RPC method hooks needed by the method implementation.
|
|
50
|
+
* @returns The specification for the `snap_notify` permission.
|
|
51
|
+
*/
|
|
52
|
+
export declare const specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, SpecificationBuilderOptions, Specification>;
|
|
53
|
+
export declare const notifyBuilder: Readonly<{
|
|
54
|
+
readonly targetName: "snap_notify";
|
|
55
|
+
readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, SpecificationBuilderOptions, {
|
|
56
|
+
permissionType: PermissionType.RestrictedMethod;
|
|
57
|
+
targetName: typeof methodName;
|
|
58
|
+
methodImplementation: ReturnType<typeof getImplementation>;
|
|
59
|
+
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
|
|
60
|
+
}>;
|
|
61
|
+
readonly methodHooks: MethodHooksObject<NotifyMethodHooks>;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* Builds the method implementation for `snap_notify`.
|
|
65
|
+
*
|
|
66
|
+
* @param hooks - The RPC method hooks.
|
|
67
|
+
* @param hooks.showNativeNotification - A function that shows a native browser notification.
|
|
68
|
+
* @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.
|
|
69
|
+
* @returns The method implementation which returns `null` on success.
|
|
70
|
+
* @throws If the params are invalid.
|
|
71
|
+
*/
|
|
72
|
+
export declare function getImplementation({ showNativeNotification, showInAppNotification, }: NotifyMethodHooks): (args: RestrictedMethodOptions<NotificationArgs>) => Promise<null>;
|
|
73
|
+
/**
|
|
74
|
+
* Validates the notify method `params` and returns them cast to the correct
|
|
75
|
+
* type. Throws if validation fails.
|
|
76
|
+
*
|
|
77
|
+
* @param params - The unvalidated params object from the method request.
|
|
78
|
+
* @returns The validated method parameter object.
|
|
79
|
+
*/
|
|
80
|
+
export declare function getValidatedParams(params: unknown): NotificationArgs;
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { SLIP10Node } from '@metamask/key-tree';
|
|
2
|
+
import type { MagicValue } from '@metamask/snaps-utils';
|
|
3
|
+
import type { Hex } from '@metamask/utils';
|
|
4
|
+
/**
|
|
5
|
+
* Maps an interface with method hooks to an object, using the keys of the
|
|
6
|
+
* interface, and `true` as value. This ensures that the `methodHooks` object
|
|
7
|
+
* has the same values as the interface.
|
|
8
|
+
*/
|
|
9
|
+
export declare type MethodHooksObject<HooksType extends Record<string, unknown>> = {
|
|
10
|
+
[Key in keyof HooksType]: true;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Returns the subset of the specified `hooks` that are included in the
|
|
14
|
+
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
15
|
+
* to ensure that each RPC method implementation only has access to the
|
|
16
|
+
* API "hooks" it needs to do its job.
|
|
17
|
+
*
|
|
18
|
+
* @param hooks - The hooks to select from.
|
|
19
|
+
* @param hookNames - The names of the hooks to select.
|
|
20
|
+
* @returns The selected hooks.
|
|
21
|
+
* @template Hooks - The hooks to select from.
|
|
22
|
+
* @template HookName - The names of the hooks to select.
|
|
23
|
+
*/
|
|
24
|
+
export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
|
|
25
|
+
declare type DeriveEntropyOptions = {
|
|
26
|
+
/**
|
|
27
|
+
* The input value to derive entropy from.
|
|
28
|
+
*/
|
|
29
|
+
input: string;
|
|
30
|
+
/**
|
|
31
|
+
* An optional salt to use when deriving entropy.
|
|
32
|
+
*/
|
|
33
|
+
salt?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The mnemonic phrase to use for entropy derivation.
|
|
36
|
+
*/
|
|
37
|
+
mnemonicPhrase: Uint8Array;
|
|
38
|
+
/**
|
|
39
|
+
* A hardened BIP-32 index, which is used to derive the root key from the
|
|
40
|
+
* mnemonic phrase.
|
|
41
|
+
*/
|
|
42
|
+
magic: MagicValue;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Derive entropy from the given mnemonic phrase and salt.
|
|
46
|
+
*
|
|
47
|
+
* This is based on the reference implementation of
|
|
48
|
+
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
|
|
49
|
+
*
|
|
50
|
+
* @param options - The options for entropy derivation.
|
|
51
|
+
* @param options.input - The input value to derive entropy from.
|
|
52
|
+
* @param options.salt - An optional salt to use when deriving entropy.
|
|
53
|
+
* @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
|
|
54
|
+
* derivation.
|
|
55
|
+
* @param options.magic - A hardened BIP-32 index, which is used to derive the
|
|
56
|
+
* root key from the mnemonic phrase.
|
|
57
|
+
* @returns The derived entropy.
|
|
58
|
+
*/
|
|
59
|
+
export declare function deriveEntropy({ input, salt, mnemonicPhrase, magic, }: DeriveEntropyOptions): Promise<Hex>;
|
|
60
|
+
/**
|
|
61
|
+
* Get the path prefix to use for key derivation in `key-tree`. This assumes the
|
|
62
|
+
* following:
|
|
63
|
+
*
|
|
64
|
+
* - The Secp256k1 curve always use the BIP-32 specification.
|
|
65
|
+
* - The Ed25519 curve always use the SLIP-10 specification.
|
|
66
|
+
*
|
|
67
|
+
* While this does not matter in most situations (no known case at the time of
|
|
68
|
+
* writing), `key-tree` requires a specific specification to be used.
|
|
69
|
+
*
|
|
70
|
+
* @param curve - The curve to get the path prefix for. The curve is NOT
|
|
71
|
+
* validated by this function.
|
|
72
|
+
* @returns The path prefix, i.e., `secp256k1` or `ed25519`.
|
|
73
|
+
*/
|
|
74
|
+
export declare function getPathPrefix(curve: 'secp256k1' | 'ed25519'): 'bip32' | 'slip10';
|
|
75
|
+
declare type GetNodeArgs = {
|
|
76
|
+
curve: 'secp256k1' | 'ed25519';
|
|
77
|
+
secretRecoveryPhrase: Uint8Array;
|
|
78
|
+
path: string[];
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Get a `key-tree`-compatible node.
|
|
82
|
+
*
|
|
83
|
+
* Note: This function assumes that all the parameters have been validated
|
|
84
|
+
* beforehand.
|
|
85
|
+
*
|
|
86
|
+
* @param options - The derivation options.
|
|
87
|
+
* @param options.curve - The curve to use for derivation.
|
|
88
|
+
* @param options.secretRecoveryPhrase - The secret recovery phrase to use for
|
|
89
|
+
* derivation.
|
|
90
|
+
* @param options.path - The derivation path to use as array, starting with an
|
|
91
|
+
* "m" as the first item.
|
|
92
|
+
*/
|
|
93
|
+
export declare function getNode({ curve, secretRecoveryPhrase, path, }: GetNodeArgs): Promise<SLIP10Node>;
|
|
94
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask/snaps-rpc-methods",
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"description": "MetaMask Snaps JSON-RPC method implementations.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/MetaMask/snaps.git"
|
|
8
|
+
},
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"main": "./dist/cjs/index.js",
|
|
11
|
+
"module": "./dist/esm/index.js",
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/cjs/**",
|
|
15
|
+
"dist/esm/**",
|
|
16
|
+
"dist/types/**"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "jest && yarn posttest",
|
|
20
|
+
"posttest": "jest-it-up --margin 0.25",
|
|
21
|
+
"test:ci": "yarn test",
|
|
22
|
+
"lint:eslint": "eslint . --cache --ext js,ts,jsx,tsx",
|
|
23
|
+
"lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
|
|
24
|
+
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies",
|
|
25
|
+
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
26
|
+
"lint:changelog": "../../scripts/validate-changelog.sh @metamask/snaps-rpc-methods",
|
|
27
|
+
"build": "yarn build:source && yarn build:types",
|
|
28
|
+
"build:source": "yarn build:esm && yarn build:cjs",
|
|
29
|
+
"build:types": "tsc --project tsconfig.build.json",
|
|
30
|
+
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
|
|
31
|
+
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
|
|
32
|
+
"build:clean": "yarn clean && yarn build",
|
|
33
|
+
"clean": "rimraf '*.tsbuildinfo' 'dist'",
|
|
34
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
35
|
+
"lint:ci": "yarn lint",
|
|
36
|
+
"lint:dependencies": "depcheck"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@metamask/key-tree": "^9.0.0",
|
|
40
|
+
"@metamask/permission-controller": "^5.0.0",
|
|
41
|
+
"@metamask/rpc-errors": "^6.1.0",
|
|
42
|
+
"@metamask/snaps-ui": "^3.0.1",
|
|
43
|
+
"@metamask/snaps-utils": "^3.1.0",
|
|
44
|
+
"@metamask/utils": "^8.1.0",
|
|
45
|
+
"@noble/hashes": "^1.3.1",
|
|
46
|
+
"superstruct": "^1.0.3"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@lavamoat/allow-scripts": "^2.5.1",
|
|
50
|
+
"@metamask/auto-changelog": "^3.3.0",
|
|
51
|
+
"@metamask/browser-passworder": "^4.1.0",
|
|
52
|
+
"@metamask/eslint-config": "^12.1.0",
|
|
53
|
+
"@metamask/eslint-config-jest": "^12.1.0",
|
|
54
|
+
"@metamask/eslint-config-nodejs": "^12.1.0",
|
|
55
|
+
"@metamask/eslint-config-typescript": "^12.1.0",
|
|
56
|
+
"@metamask/json-rpc-engine": "^7.1.1",
|
|
57
|
+
"@swc/cli": "^0.1.62",
|
|
58
|
+
"@swc/core": "1.3.78",
|
|
59
|
+
"@swc/jest": "^0.2.26",
|
|
60
|
+
"@types/node": "18.14.2",
|
|
61
|
+
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
62
|
+
"@typescript-eslint/parser": "^5.42.1",
|
|
63
|
+
"deepmerge": "^4.2.2",
|
|
64
|
+
"depcheck": "^1.4.7",
|
|
65
|
+
"eslint": "^8.27.0",
|
|
66
|
+
"eslint-config-prettier": "^8.5.0",
|
|
67
|
+
"eslint-plugin-import": "^2.26.0",
|
|
68
|
+
"eslint-plugin-jest": "^27.1.5",
|
|
69
|
+
"eslint-plugin-jsdoc": "^39.6.2",
|
|
70
|
+
"eslint-plugin-n": "^15.7.0",
|
|
71
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
72
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
73
|
+
"expect-type": "^0.15.0",
|
|
74
|
+
"jest": "^29.0.2",
|
|
75
|
+
"jest-it-up": "^2.0.0",
|
|
76
|
+
"prettier": "^2.7.1",
|
|
77
|
+
"prettier-plugin-packagejson": "^2.2.11",
|
|
78
|
+
"rimraf": "^4.1.2",
|
|
79
|
+
"typescript": "~4.8.4"
|
|
80
|
+
},
|
|
81
|
+
"engines": {
|
|
82
|
+
"node": "^18.16 || >=20"
|
|
83
|
+
},
|
|
84
|
+
"publishConfig": {
|
|
85
|
+
"access": "public",
|
|
86
|
+
"registry": "https://registry.npmjs.org/"
|
|
87
|
+
}
|
|
88
|
+
}
|