@metamask/snaps-controllers 9.3.1 → 9.4.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 +9 -1
- package/dist/{chunk-3T6W5VI2.mjs → chunk-3WEWHK2I.mjs} +7 -3
- package/dist/chunk-3WEWHK2I.mjs.map +1 -0
- package/dist/{chunk-QYQLJCPR.js → chunk-5O4O4BI5.js} +2 -1
- package/dist/chunk-5O4O4BI5.js.map +1 -0
- package/dist/{chunk-23PVLQFV.mjs → chunk-CJY2LYEL.mjs} +2 -2
- package/dist/{chunk-H7TB7I6Z.js → chunk-EVKVZUYY.js} +7 -3
- package/dist/chunk-EVKVZUYY.js.map +1 -0
- package/dist/{chunk-JLIW4CNS.js → chunk-OJBUUQ5D.js} +7 -7
- package/dist/{chunk-G245VWEY.mjs → chunk-UMVUXQ3B.mjs} +2 -1
- package/dist/chunk-UMVUXQ3B.mjs.map +1 -0
- package/dist/cronjob/CronjobController.js +3 -3
- package/dist/cronjob/CronjobController.mjs +3 -3
- package/dist/cronjob/index.js +3 -3
- package/dist/cronjob/index.mjs +3 -3
- package/dist/index.js +4 -4
- package/dist/index.mjs +3 -3
- package/dist/insights/SnapInsightsController.js +2 -2
- package/dist/insights/SnapInsightsController.mjs +1 -1
- package/dist/insights/index.js +2 -2
- package/dist/insights/index.mjs +1 -1
- package/dist/interface/SnapInterfaceController.js +3 -3
- package/dist/interface/SnapInterfaceController.mjs +2 -2
- package/dist/interface/index.js +3 -3
- package/dist/interface/index.mjs +2 -2
- package/dist/interface/utils.js +2 -2
- package/dist/interface/utils.mjs +1 -1
- package/dist/node.js +4 -4
- package/dist/node.mjs +3 -3
- package/dist/react-native.js +4 -4
- package/dist/react-native.mjs +3 -3
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/cronjob/CronjobController.d.ts +7 -7
- package/dist/types/insights/SnapInsightsController.d.ts +8 -7
- package/dist/types/interface/SnapInterfaceController.d.ts +12 -12
- package/dist/types/services/AbstractExecutionService.d.ts +5 -5
- package/dist/types/services/ExecutionService.d.ts +16 -16
- package/dist/types/services/ProxyPostMessageStream.d.ts +2 -2
- package/dist/types/services/iframe/IframeExecutionService.d.ts +1 -1
- package/dist/types/services/offscreen/OffscreenExecutionService.d.ts +1 -1
- package/dist/types/services/proxy/ProxyExecutionService.d.ts +1 -1
- package/dist/types/services/webview/WebViewExecutionService.d.ts +1 -1
- package/dist/types/services/webview/WebViewMessageStream.d.ts +2 -2
- package/dist/types/services/webworker/WebWorkerExecutionService.d.ts +1 -1
- package/dist/types/snaps/SnapController.d.ts +46 -46
- package/dist/types/snaps/Timer.d.ts +1 -1
- package/dist/types/snaps/location/location.d.ts +1 -1
- package/dist/types/snaps/location/npm.d.ts +1 -1
- package/dist/types/snaps/registry/json.d.ts +10 -10
- package/dist/types/snaps/registry/registry.d.ts +5 -5
- package/dist/types/types/controllers.d.ts +8 -8
- package/dist/types/types/encryptor.d.ts +5 -5
- package/dist/types/utils.d.ts +13 -6
- package/package.json +5 -5
- package/dist/chunk-3T6W5VI2.mjs.map +0 -1
- package/dist/chunk-G245VWEY.mjs.map +0 -1
- package/dist/chunk-H7TB7I6Z.js.map +0 -1
- package/dist/chunk-QYQLJCPR.js.map +0 -1
- /package/dist/{chunk-23PVLQFV.mjs.map → chunk-CJY2LYEL.mjs.map} +0 -0
- /package/dist/{chunk-JLIW4CNS.js.map → chunk-OJBUUQ5D.js.map} +0 -0
|
@@ -3,11 +3,11 @@ import { BaseController } from '@metamask/base-controller';
|
|
|
3
3
|
import type { SnapsRegistryDatabase } from '@metamask/snaps-registry';
|
|
4
4
|
import type { Hex } from '@metamask/utils';
|
|
5
5
|
import type { SnapsRegistry } from './registry';
|
|
6
|
-
|
|
6
|
+
type JsonSnapsRegistryUrl = {
|
|
7
7
|
registry: string;
|
|
8
8
|
signature: string;
|
|
9
9
|
};
|
|
10
|
-
export
|
|
10
|
+
export type JsonSnapsRegistryArgs = {
|
|
11
11
|
messenger: SnapsRegistryMessenger;
|
|
12
12
|
state?: SnapsRegistryState;
|
|
13
13
|
fetchFunction?: typeof fetch;
|
|
@@ -16,26 +16,26 @@ export declare type JsonSnapsRegistryArgs = {
|
|
|
16
16
|
refetchOnAllowlistMiss?: boolean;
|
|
17
17
|
publicKey?: Hex;
|
|
18
18
|
};
|
|
19
|
-
export
|
|
19
|
+
export type GetResult = {
|
|
20
20
|
type: `${typeof controllerName}:get`;
|
|
21
21
|
handler: SnapsRegistry['get'];
|
|
22
22
|
};
|
|
23
|
-
export
|
|
23
|
+
export type ResolveVersion = {
|
|
24
24
|
type: `${typeof controllerName}:resolveVersion`;
|
|
25
25
|
handler: SnapsRegistry['resolveVersion'];
|
|
26
26
|
};
|
|
27
|
-
export
|
|
27
|
+
export type GetMetadata = {
|
|
28
28
|
type: `${typeof controllerName}:getMetadata`;
|
|
29
29
|
handler: SnapsRegistry['getMetadata'];
|
|
30
30
|
};
|
|
31
|
-
export
|
|
31
|
+
export type Update = {
|
|
32
32
|
type: `${typeof controllerName}:update`;
|
|
33
33
|
handler: SnapsRegistry['update'];
|
|
34
34
|
};
|
|
35
|
-
export
|
|
36
|
-
export
|
|
37
|
-
export
|
|
38
|
-
export
|
|
35
|
+
export type SnapsRegistryActions = GetResult | GetMetadata | Update | ResolveVersion;
|
|
36
|
+
export type SnapsRegistryEvents = never;
|
|
37
|
+
export type SnapsRegistryMessenger = RestrictedControllerMessenger<'SnapsRegistry', SnapsRegistryActions, SnapsRegistryEvents, SnapsRegistryActions['type'], SnapsRegistryEvents['type']>;
|
|
38
|
+
export type SnapsRegistryState = {
|
|
39
39
|
database: SnapsRegistryDatabase | null;
|
|
40
40
|
lastUpdated: number | null;
|
|
41
41
|
databaseUnavailable: boolean;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import type { BlockReason, SnapsRegistryDatabase } from '@metamask/snaps-registry';
|
|
2
2
|
import type { SnapId } from '@metamask/snaps-sdk';
|
|
3
3
|
import type { SemVerRange, SemVerVersion } from '@metamask/utils';
|
|
4
|
-
export
|
|
4
|
+
export type SnapsRegistryInfo = {
|
|
5
5
|
version: SemVerVersion;
|
|
6
6
|
checksum: string;
|
|
7
7
|
};
|
|
8
|
-
export
|
|
9
|
-
export
|
|
8
|
+
export type SnapsRegistryRequest = Record<SnapId, SnapsRegistryInfo>;
|
|
9
|
+
export type SnapsRegistryMetadata = SnapsRegistryDatabase['verifiedSnaps'][SnapId]['metadata'];
|
|
10
10
|
export declare enum SnapsRegistryStatus {
|
|
11
11
|
Unverified = 0,
|
|
12
12
|
Blocked = 1,
|
|
13
13
|
Verified = 2,
|
|
14
14
|
Unavailable = 3
|
|
15
15
|
}
|
|
16
|
-
export
|
|
16
|
+
export type SnapsRegistryResult = {
|
|
17
17
|
status: SnapsRegistryStatus;
|
|
18
18
|
reason?: BlockReason;
|
|
19
19
|
};
|
|
20
|
-
export
|
|
20
|
+
export type SnapsRegistry = {
|
|
21
21
|
get(snaps: SnapsRegistryRequest): Promise<Record<SnapId, SnapsRegistryResult>>;
|
|
22
22
|
update(): Promise<void>;
|
|
23
23
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ControllerStateChangeEvent } from '@metamask/base-controller';
|
|
2
2
|
import type { Hex, Json } from '@metamask/utils';
|
|
3
|
-
export
|
|
3
|
+
export type TransactionMeta = {
|
|
4
4
|
/**
|
|
5
5
|
* Generated UUID associated with this transaction.
|
|
6
6
|
*/
|
|
@@ -25,7 +25,7 @@ export declare type TransactionMeta = {
|
|
|
25
25
|
/**
|
|
26
26
|
* Standard data concerning a transaction to be processed by the blockchain.
|
|
27
27
|
*/
|
|
28
|
-
export
|
|
28
|
+
export type TransactionParams = {
|
|
29
29
|
/**
|
|
30
30
|
* Network ID as per EIP-155.
|
|
31
31
|
*/
|
|
@@ -97,11 +97,11 @@ export declare type TransactionParams = {
|
|
|
97
97
|
*/
|
|
98
98
|
type?: string;
|
|
99
99
|
};
|
|
100
|
-
export
|
|
100
|
+
export type TransactionControllerUnapprovedTransactionAddedEvent = {
|
|
101
101
|
type: `TransactionController:unapprovedTransactionAdded`;
|
|
102
102
|
payload: [transactionMeta: TransactionMeta];
|
|
103
103
|
};
|
|
104
|
-
export
|
|
104
|
+
export type TransactionControllerTransactionStatusUpdatedEvent = {
|
|
105
105
|
type: `TransactionController:transactionStatusUpdated`;
|
|
106
106
|
payload: [
|
|
107
107
|
{
|
|
@@ -109,23 +109,23 @@ export declare type TransactionControllerTransactionStatusUpdatedEvent = {
|
|
|
109
109
|
}
|
|
110
110
|
];
|
|
111
111
|
};
|
|
112
|
-
export
|
|
112
|
+
export type StateSignatureParams = {
|
|
113
113
|
from: string;
|
|
114
114
|
origin?: string;
|
|
115
115
|
deferSetAsSigned?: boolean;
|
|
116
116
|
data: string | Record<string, Json>;
|
|
117
117
|
signatureMethod: string;
|
|
118
118
|
};
|
|
119
|
-
export
|
|
119
|
+
export type StateSignature = {
|
|
120
120
|
id: string;
|
|
121
121
|
msgParams: StateSignatureParams;
|
|
122
122
|
};
|
|
123
|
-
export
|
|
123
|
+
export type SignatureControllerState = {
|
|
124
124
|
unapprovedPersonalMsgs: Record<string, StateSignature>;
|
|
125
125
|
unapprovedTypedMessages: Record<string, StateSignature>;
|
|
126
126
|
unapprovedPersonalMsgCount: number;
|
|
127
127
|
unapprovedTypedMessagesCount: number;
|
|
128
128
|
};
|
|
129
129
|
declare const signatureControllerName = "SignatureController";
|
|
130
|
-
export
|
|
130
|
+
export type SignatureStateChange = ControllerStateChangeEvent<typeof signatureControllerName, SignatureControllerState>;
|
|
131
131
|
export {};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Json } from '@metamask/utils';
|
|
2
|
-
export
|
|
2
|
+
export type PBKDF2Params = {
|
|
3
3
|
iterations: number;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export type KeyDerivationOptions = {
|
|
6
6
|
algorithm: 'PBKDF2';
|
|
7
7
|
params: PBKDF2Params;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type EncryptionResult = {
|
|
10
10
|
data: string;
|
|
11
11
|
iv: string;
|
|
12
12
|
salt: string;
|
|
@@ -16,7 +16,7 @@ export declare type EncryptionResult = {
|
|
|
16
16
|
* A generic encryptor interface that supports encrypting and decrypting
|
|
17
17
|
* serializable data with a password.
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
19
|
+
export type GenericEncryptor = {
|
|
20
20
|
/**
|
|
21
21
|
* Encrypt the given object with the given password.
|
|
22
22
|
*
|
|
@@ -47,7 +47,7 @@ export declare type GenericEncryptor = {
|
|
|
47
47
|
* An encryptor interface that supports encrypting and decrypting
|
|
48
48
|
* serializable data with a password, and exporting and importing keys.
|
|
49
49
|
*/
|
|
50
|
-
export
|
|
50
|
+
export type ExportableKeyEncryptor = GenericEncryptor & {
|
|
51
51
|
/**
|
|
52
52
|
* Encrypt the given object with the given encryption key.
|
|
53
53
|
*
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -86,7 +86,7 @@ export declare function withTimeout<PromiseValue = void>(promise: Promise<Promis
|
|
|
86
86
|
* // t5 = false;
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
type IsLiteral<Type> = Type extends string | number | boolean | symbol ? Extract<string | number | boolean | symbol, Type> extends never ? true : false : false;
|
|
90
90
|
/**
|
|
91
91
|
* Returns all keys of an object, that are literal, as an union
|
|
92
92
|
*
|
|
@@ -97,7 +97,7 @@ declare type IsLiteral<Type> = Type extends string | number | boolean | symbol ?
|
|
|
97
97
|
* ```
|
|
98
98
|
* @see [Literal types]{@link https://www.typescriptlang.org/docs/handbook/literal-types.html}
|
|
99
99
|
*/
|
|
100
|
-
|
|
100
|
+
type LiteralKeys<Type> = NonNullable<{
|
|
101
101
|
[Key in keyof Type]: IsLiteral<Key> extends true ? Key : never;
|
|
102
102
|
}[keyof Type]>;
|
|
103
103
|
/**
|
|
@@ -110,7 +110,7 @@ declare type LiteralKeys<Type> = NonNullable<{
|
|
|
110
110
|
* ```
|
|
111
111
|
* @see [Literal types]{@link https://www.typescriptlang.org/docs/handbook/literal-types.html}
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
type NonLiteralKeys<Type> = NonNullable<{
|
|
114
114
|
[Key in keyof Type]: IsLiteral<Key> extends false ? Key : never;
|
|
115
115
|
}[keyof Type]>;
|
|
116
116
|
/**
|
|
@@ -127,14 +127,14 @@ declare type NonLiteralKeys<Type> = NonNullable<{
|
|
|
127
127
|
* ```
|
|
128
128
|
* @see {@link setDiff} for the main use-case
|
|
129
129
|
*/
|
|
130
|
-
export
|
|
130
|
+
export type Diff<First, Second> = Omit<First, LiteralKeys<Second>> & Partial<Pick<First, Extract<keyof First, NonLiteralKeys<Second>>>>;
|
|
131
131
|
/**
|
|
132
132
|
* Makes every specified property of the specified object type mutable.
|
|
133
133
|
*
|
|
134
134
|
* @template Type - The object whose readonly properties to make mutable.
|
|
135
135
|
* @template TargetKey - The property key(s) to make mutable.
|
|
136
136
|
*/
|
|
137
|
-
export
|
|
137
|
+
export type Mutable<Type extends Record<string, unknown>, TargetKey extends string> = {
|
|
138
138
|
-readonly [Key in keyof Pick<Type, TargetKey>]: Type[Key];
|
|
139
139
|
} & {
|
|
140
140
|
[Key in keyof Omit<Type, TargetKey>]: Type[Key];
|
|
@@ -173,7 +173,14 @@ export declare function fetchSnap(snapId: SnapId, location: SnapLocation): Promi
|
|
|
173
173
|
files?: string[] | undefined;
|
|
174
174
|
locales?: string[] | undefined;
|
|
175
175
|
};
|
|
176
|
-
proposedName: string;
|
|
176
|
+
proposedName: string; /**
|
|
177
|
+
* A Promise that delays it's return by using a pausable Timer.
|
|
178
|
+
*
|
|
179
|
+
* @param timer - Timer used to control the delay.
|
|
180
|
+
* @param result - The result to return from the Promise after delay.
|
|
181
|
+
* @returns A promise that is void if no result provided, result otherwise.
|
|
182
|
+
* @template Result - The `result`.
|
|
183
|
+
*/
|
|
177
184
|
initialPermissions: Partial<{
|
|
178
185
|
'endowment:cronjob': {
|
|
179
186
|
jobs: import("@metamask/snaps-sdk").Cronjob[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-controllers",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.4.0",
|
|
4
4
|
"description": "Controllers for MetaMask Snaps.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -63,9 +63,9 @@
|
|
|
63
63
|
"@metamask/post-message-stream": "^8.1.0",
|
|
64
64
|
"@metamask/rpc-errors": "^6.3.1",
|
|
65
65
|
"@metamask/snaps-registry": "^3.2.1",
|
|
66
|
-
"@metamask/snaps-rpc-methods": "^
|
|
67
|
-
"@metamask/snaps-sdk": "^6.
|
|
68
|
-
"@metamask/snaps-utils": "^
|
|
66
|
+
"@metamask/snaps-rpc-methods": "^11.0.0",
|
|
67
|
+
"@metamask/snaps-sdk": "^6.2.0",
|
|
68
|
+
"@metamask/snaps-utils": "^8.0.0",
|
|
69
69
|
"@metamask/utils": "^9.1.0",
|
|
70
70
|
"@xstate/fsm": "^2.0.0",
|
|
71
71
|
"browserify-zlib": "^0.2.0",
|
|
@@ -130,7 +130,7 @@
|
|
|
130
130
|
"rimraf": "^4.1.2",
|
|
131
131
|
"ts-node": "^10.9.1",
|
|
132
132
|
"tsup": "^8.0.1",
|
|
133
|
-
"typescript": "~
|
|
133
|
+
"typescript": "~5.0.4",
|
|
134
134
|
"vite": "^4.3.9",
|
|
135
135
|
"vite-tsconfig-paths": "^4.0.5",
|
|
136
136
|
"wdio-chromedriver-service": "^8.1.1",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/interface/utils.ts"],"sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n State,\n} from '@metamask/snaps-sdk';\nimport type {\n DropdownElement,\n InputElement,\n JSXElement,\n OptionElement,\n FileInputElement,\n CheckboxElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n walkJsx,\n} from '@metamask/snaps-utils';\n\n/**\n * Get a JSX element from a component or JSX element. If the component is a\n * JSX element, it is returned as is. Otherwise, the component is converted to\n * a JSX element.\n *\n * @param component - The component to convert.\n * @returns The JSX element.\n */\nexport function getJsxInterface(component: ComponentOrElement): JSXElement {\n if (isJSXElementUnsafe(component)) {\n return component;\n }\n\n return getJsxElementFromComponent(component);\n}\n\n/**\n * Assert that the component name is unique in state.\n *\n * @param state - The interface state to verify against.\n * @param name - The component name to verify.\n */\nexport function assertNameIsUnique(state: InterfaceState, name: string) {\n assert(\n state[name] === undefined,\n `Duplicate component names are not allowed, found multiple instances of: \"${name}\".`,\n );\n}\n\n/**\n * Construct default state for a component.\n *\n * This function is meant to be used inside constructInputState to account\n * for component specific defaults and will not override the component value or existing form state.\n *\n * @param element - The input element.\n * @returns The default state for the specific component, if any.\n */\nfunction constructComponentSpecificDefaultState(\n element: InputElement | DropdownElement | CheckboxElement,\n) {\n switch (element.type) {\n case 'Dropdown': {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n case 'Checkbox':\n return false;\n\n default:\n return null;\n }\n}\n\n/**\n * Get the state value for a stateful component.\n *\n * Most components store the state value as a `value` prop.\n * This function exists to account for components where that isn't the case.\n *\n * @param element - The input element.\n * @returns The state value for a given component.\n */\nfunction getComponentStateValue(\n element: InputElement | DropdownElement | CheckboxElement,\n) {\n switch (element.type) {\n case 'Checkbox':\n return element.props.checked;\n\n default:\n return element.props.value;\n }\n}\n\n/**\n * Construct the state for an input field.\n *\n * @param oldState - The previous state.\n * @param element - The input element.\n * @param form - An optional form that the input is enclosed in.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element: InputElement | DropdownElement | FileInputElement | CheckboxElement,\n form?: string,\n) {\n const oldStateUnwrapped = form ? (oldState[form] as FormState) : oldState;\n const oldInputState = oldStateUnwrapped?.[element.props.name] as State;\n\n if (element.type === 'FileInput') {\n return oldInputState ?? null;\n }\n\n return (\n getComponentStateValue(element) ??\n oldInputState ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param rootComponent - The UI component to construct state from.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n rootComponent: JSXElement,\n): InterfaceState {\n const newState: InterfaceState = {};\n\n // Stack containing the forms we have visited and at which depth\n const formStack: { name: string; depth: number }[] = [];\n\n walkJsx(rootComponent, (component, depth) => {\n let currentForm = formStack[formStack.length - 1];\n\n // Pop the current form of the stack once we leave its depth.\n if (currentForm && depth <= currentForm.depth) {\n formStack.pop();\n currentForm = formStack[formStack.length - 1];\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n formStack.push({ name: component.props.name, depth });\n newState[component.props.name] = {};\n return;\n }\n\n // Stateful components inside a form\n if (\n currentForm &&\n (component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox')\n ) {\n const formState = newState[currentForm.name] as FormState;\n assertNameIsUnique(formState, component.props.name);\n formState[component.props.name] = constructInputState(\n oldState,\n component,\n currentForm.name,\n );\n return;\n }\n\n // Stateful components outside a form\n if (\n component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox'\n ) {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\n });\n\n return newState;\n}\n\nconst MAX_CONTEXT_SIZE = 1_000_000; // 1 mb\n\n/**\n * Validate a JSON blob to be used as the interface context.\n *\n * @param context - The JSON blob.\n * @throws If the JSON blob is too large.\n */\nexport function validateInterfaceContext(context?: InterfaceContext) {\n if (!context) {\n return;\n }\n\n // We assume the validity of this JSON to be validated by the caller.\n // E.g., in the RPC method implementation.\n const size = getJsonSizeUnsafe(context);\n assert(\n size <= MAX_CONTEXT_SIZE,\n `A Snap interface context may not be larger than ${\n MAX_CONTEXT_SIZE / 1000000\n } MB.`,\n );\n}\n"],"mappings":";AAAA,SAAS,cAAc;AAgBvB,SAAS,0BAA0B;AACnC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAUA,SAAS,gBAAgB,WAA2C;AACzE,MAAI,mBAAmB,SAAS,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,SAAO,2BAA2B,SAAS;AAC7C;AAQO,SAAS,mBAAmB,OAAuB,MAAc;AACtE;AAAA,IACE,MAAM,IAAI,MAAM;AAAA,IAChB,4EAA4E,IAAI;AAAA,EAClF;AACF;AAWA,SAAS,uCACP,SACA;AACA,UAAQ,QAAQ,MAAM;AAAA,IACpB,KAAK,YAAY;AACf,YAAM,WAAW,eAAe,OAAO;AACvC,aAAO,SAAS,CAAC,GAAG,MAAM;AAAA,IAC5B;AAAA,IAEA,KAAK;AACH,aAAO;AAAA,IAET;AACE,aAAO;AAAA,EACX;AACF;AAWA,SAAS,uBACP,SACA;AACA,UAAQ,QAAQ,MAAM;AAAA,IACpB,KAAK;AACH,aAAO,QAAQ,MAAM;AAAA,IAEvB;AACE,aAAO,QAAQ,MAAM;AAAA,EACzB;AACF;AAUA,SAAS,oBACP,UACA,SACA,MACA;AACA,QAAM,oBAAoB,OAAQ,SAAS,IAAI,IAAkB;AACjE,QAAM,gBAAgB,oBAAoB,QAAQ,MAAM,IAAI;AAE5D,MAAI,QAAQ,SAAS,aAAa;AAChC,WAAO,iBAAiB;AAAA,EAC1B;AAEA,SACE,uBAAuB,OAAO,KAC9B,iBACA,uCAAuC,OAAO,KAC9C;AAEJ;AASO,SAAS,eACd,UACA,eACgB;AAChB,QAAM,WAA2B,CAAC;AAGlC,QAAM,YAA+C,CAAC;AAEtD,UAAQ,eAAe,CAAC,WAAW,UAAU;AAC3C,QAAI,cAAc,UAAU,UAAU,SAAS,CAAC;AAGhD,QAAI,eAAe,SAAS,YAAY,OAAO;AAC7C,gBAAU,IAAI;AACd,oBAAc,UAAU,UAAU,SAAS,CAAC;AAAA,IAC9C;AAEA,QAAI,UAAU,SAAS,QAAQ;AAC7B,yBAAmB,UAAU,UAAU,MAAM,IAAI;AACjD,gBAAU,KAAK,EAAE,MAAM,UAAU,MAAM,MAAM,MAAM,CAAC;AACpD,eAAS,UAAU,MAAM,IAAI,IAAI,CAAC;AAClC;AAAA,IACF;AAGA,QACE,gBACC,UAAU,SAAS,WAClB,UAAU,SAAS,cACnB,UAAU,SAAS,eACnB,UAAU,SAAS,aACrB;AACA,YAAM,YAAY,SAAS,YAAY,IAAI;AAC3C,yBAAmB,WAAW,UAAU,MAAM,IAAI;AAClD,gBAAU,UAAU,MAAM,IAAI,IAAI;AAAA,QAChC;AAAA,QACA;AAAA,QACA,YAAY;AAAA,MACd;AACA;AAAA,IACF;AAGA,QACE,UAAU,SAAS,WACnB,UAAU,SAAS,cACnB,UAAU,SAAS,eACnB,UAAU,SAAS,YACnB;AACA,yBAAmB,UAAU,UAAU,MAAM,IAAI;AACjD,eAAS,UAAU,MAAM,IAAI,IAAI,oBAAoB,UAAU,SAAS;AAAA,IAC1E;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAEA,IAAM,mBAAmB;AAQlB,SAAS,yBAAyB,SAA4B;AACnE,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAIA,QAAM,OAAO,kBAAkB,OAAO;AACtC;AAAA,IACE,QAAQ;AAAA,IACR,mDACE,mBAAmB,GACrB;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/insights/SnapInsightsController.ts"],"sourcesContent":["import type { RestrictedControllerMessenger } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n Caveat,\n GetPermissions,\n ValidPermission,\n} from '@metamask/permission-controller';\nimport {\n getSignatureOriginCaveat,\n getTransactionOriginCaveat,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport type { DeleteInterface } from '../interface';\nimport type { GetAllSnaps, HandleSnapRequest } from '../snaps';\nimport { getRunnableSnaps } from '../snaps';\nimport type {\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionMeta,\n SignatureStateChange,\n SignatureControllerState,\n StateSignature,\n TransactionControllerTransactionStatusUpdatedEvent,\n} from '../types';\n\nconst controllerName = 'SnapInsightsController';\n\nexport type SnapInsightsControllerAllowedActions =\n | HandleSnapRequest\n | GetAllSnaps\n | GetPermissions\n | DeleteInterface;\n\nexport type SnapInsightsControllerActions = never;\n\nexport type SnapInsightsControllerAllowedEvents =\n | TransactionControllerUnapprovedTransactionAddedEvent\n | TransactionControllerTransactionStatusUpdatedEvent\n | SignatureStateChange;\n\nexport type SnapInsightsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInsightsControllerActions | SnapInsightsControllerAllowedActions,\n SnapInsightsControllerAllowedEvents,\n SnapInsightsControllerAllowedActions['type'],\n SnapInsightsControllerAllowedEvents['type']\n>;\n\nexport type SnapInsight = {\n snapId: SnapId;\n interfaceId?: string | null;\n error?: string;\n loading: boolean;\n};\n\nexport type SnapInsightsControllerState = {\n insights: Record<string, Record<SnapId, SnapInsight>>;\n};\n\nexport type SnapInsightsControllerArgs = {\n messenger: SnapInsightsControllerMessenger;\n state?: SnapInsightsControllerState;\n};\n\ntype SnapWithPermission = {\n snapId: SnapId;\n permission: ValidPermission<string, Caveat<string, Json>>;\n};\n\n/**\n * Controller for monitoring for new transactions and signatures to provide insight for.\n */\nexport class SnapInsightsController extends BaseController<\n typeof controllerName,\n SnapInsightsControllerState,\n SnapInsightsControllerMessenger\n> {\n constructor({ messenger, state }: SnapInsightsControllerArgs) {\n super({\n messenger,\n metadata: {\n insights: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { insights: {}, ...state },\n });\n\n this.messagingSystem.subscribe(\n 'TransactionController:unapprovedTransactionAdded',\n this.#handleTransaction.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'TransactionController:transactionStatusUpdated',\n this.#handleTransactionStatusUpdate.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'SignatureController:stateChange',\n this.#handleSignatureStateChange.bind(this),\n );\n }\n\n /**\n * Check if an insight already exists for a given ID.\n *\n * @param id - The ID.\n * @returns True if the insight already exists, otherwise false.\n */\n #hasInsight(id: string) {\n return hasProperty(this.state.insights, id);\n }\n\n /**\n * Get a list of runnable Snaps that have a given permission.\n * Also includes the permission object itself.\n *\n * @param permissionName - The permission name.\n * @returns A list of objects containing Snap IDs and the permission object.\n */\n #getSnapsWithPermission(permissionName: string) {\n const allSnaps = this.messagingSystem.call('SnapController:getAll');\n const filteredSnaps = getRunnableSnaps(allSnaps);\n\n return filteredSnaps.reduce<SnapWithPermission[]>((accumulator, snap) => {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n if (permissions && hasProperty(permissions, permissionName)) {\n accumulator.push({\n snapId: snap.id,\n permission: permissions[permissionName],\n });\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle a newly added unapproved transaction.\n * This function fetches insights from all available Snaps\n * and populates the insights state blob with the responses.\n *\n * @param transaction - The transaction object.\n */\n #handleTransaction(transaction: TransactionMeta) {\n const { id, txParams, chainId, origin } = transaction;\n\n // This assumes that the transactions are EVM-compatible for now.\n const caipChainId = `eip155:${parseInt(chainId, 16)}`;\n\n const snaps = this.#getSnapsWithPermission(\n SnapEndowments.TransactionInsight,\n );\n\n snaps.forEach(({ snapId, permission }) => {\n const hasTransactionOriginCaveat = getTransactionOriginCaveat(permission);\n const transactionOrigin =\n hasTransactionOriginCaveat && origin ? origin : null;\n\n this.update((state) => {\n state.insights[id] ??= {};\n state.insights[id][snapId] = { snapId, loading: true };\n });\n\n this.#handleSnapRequest({\n snapId,\n handler: HandlerType.OnTransaction,\n params: {\n transaction: txParams,\n chainId: caipChainId,\n transactionOrigin,\n },\n })\n .then((response) =>\n this.#handleSnapResponse({\n id,\n snapId,\n response: response as Record<string, Json>,\n }),\n )\n .catch((error) => this.#handleSnapResponse({ id, snapId, error }));\n });\n }\n\n /**\n * Handle the stateChange event emitted by the SignatureController.\n * This function will remove existing insights from the state when applicable, as well as\n * trigger insight fetching for newly added signatures.\n *\n * @param state - The SignatureController state blob.\n */\n #handleSignatureStateChange(state: SignatureControllerState) {\n // If any IDs have disappeared since the last state update, the insight may be cleaned up.\n for (const id of Object.keys(this.state.insights)) {\n if (\n !hasProperty(state.unapprovedTypedMessages, id) &&\n !hasProperty(state.unapprovedPersonalMsgs, id)\n ) {\n this.#handleInsightCleanup(id);\n }\n }\n\n if (\n state.unapprovedPersonalMsgCount > 0 ||\n state.unapprovedTypedMessagesCount > 0\n ) {\n const snaps = this.#getSnapsWithPermission(\n SnapEndowments.SignatureInsight,\n );\n\n // This isn't very efficient, but SignatureController doesn't expose a better event for us to use yet.\n for (const personalSignature of Object.values(\n state.unapprovedPersonalMsgs,\n )) {\n this.#handleSignature(snaps, personalSignature);\n }\n\n for (const typedMessage of Object.values(state.unapprovedTypedMessages)) {\n this.#handleSignature(snaps, typedMessage);\n }\n }\n }\n\n /**\n * Handle a newly added unapproved signature.\n * This function fetches insights from all available Snaps\n * and populates the insights state blob with the responses.\n *\n * @param snaps - A list of Snaps to invoke.\n * @param signature - The signature object.\n */\n #handleSignature(snaps: SnapWithPermission[], signature: StateSignature) {\n const { id, msgParams } = signature;\n\n if (this.#hasInsight(id)) {\n return;\n }\n\n const { from, data, signatureMethod, origin } = msgParams;\n\n /**\n * Both eth_signTypedData_v3 and eth_signTypedData_v4 methods\n * need to be parsed because their data is stringified. All other\n * signature methods do not, so they are ignored.\n */\n const shouldParse =\n signatureMethod === 'eth_signTypedData_v3' ||\n signatureMethod === 'eth_signTypedData_v4';\n\n const payload = {\n from,\n data: shouldParse ? JSON.parse(data as string) : data,\n signatureMethod,\n };\n\n snaps.forEach(({ snapId, permission }) => {\n const hasSignatureOriginCaveat = getSignatureOriginCaveat(permission);\n const signatureOrigin =\n origin && hasSignatureOriginCaveat ? origin : null;\n\n this.update((state) => {\n state.insights[id] ??= {};\n state.insights[id][snapId] = { snapId, loading: true };\n });\n\n this.#handleSnapRequest({\n snapId,\n handler: HandlerType.OnSignature,\n params: { signature: payload, signatureOrigin },\n })\n .then((response) =>\n this.#handleSnapResponse({\n id,\n snapId,\n response: response as Record<string, Json>,\n }),\n )\n .catch((error) => this.#handleSnapResponse({ id, snapId, error }));\n });\n }\n\n /**\n * Handle the transactionStatusUpdated event emitted by the TransactionController.\n * This function will remove insights for the transaction in question\n * once the transaction status has changed from unapproved.\n *\n * @param args - An options bag.\n * @param args.transactionMeta - The transaction.\n */\n #handleTransactionStatusUpdate({\n transactionMeta,\n }: {\n transactionMeta: TransactionMeta;\n }) {\n if (transactionMeta.status !== 'unapproved') {\n this.#handleInsightCleanup(transactionMeta.id);\n }\n }\n\n #handleInsightCleanup(id: string) {\n if (!this.#hasInsight(id)) {\n return;\n }\n\n // Delete interfaces from interface controller.\n Object.values(this.state.insights[id])\n .filter((insight) => insight.interfaceId)\n .forEach((insight) => {\n this.messagingSystem.call(\n 'SnapInterfaceController:deleteInterface',\n insight.interfaceId as string,\n );\n });\n\n this.update((state) => {\n delete state.insights[id];\n });\n }\n\n /**\n * Handle sending a request to a given Snap with a given payload.\n *\n * @param args - An options bag.\n * @param args.snapId - The Snap ID.\n * @param args.handler - The handler to invoke.\n * @param args.params - The JSON-RPC params to send.\n * @returns The response from the Snap.\n */\n async #handleSnapRequest({\n snapId,\n handler,\n params,\n }: {\n snapId: SnapId;\n handler: HandlerType.OnTransaction | HandlerType.OnSignature;\n params: Record<string, Json>;\n }) {\n return this.messagingSystem.call('SnapController:handleRequest', {\n snapId,\n origin: '',\n handler,\n request: {\n method: '',\n params,\n },\n });\n }\n\n /**\n * Handle response from a given Snap by persisting the response or error in state.\n *\n * @param args - An options bag.\n * @param args.id - The transaction or signature ID.\n * @param args.snapId - The Snap ID.\n * @param args.response - An optional response object returned by the Snap.\n * @param args.error - An optional error returned by the Snap.\n */\n #handleSnapResponse({\n id,\n snapId,\n response,\n error,\n }: {\n id: string;\n snapId: SnapId;\n response?: Record<string, Json>;\n error?: Error;\n }) {\n this.update((state) => {\n state.insights[id][snapId].loading = false;\n state.insights[id][snapId].interfaceId = response?.id as string;\n state.insights[id][snapId].error = error?.message;\n });\n }\n}\n"],"mappings":";;;;;;;;;AACA,SAAS,sBAAsB;AAM/B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB;AAc5B,IAAM,iBAAiB;AA5BvB;AA2EO,IAAM,yBAAN,cAAqC,eAI1C;AAAA,EACA,YAAY,EAAE,WAAW,MAAM,GAA+B;AAC5D,UAAM;AAAA,MACJ;AAAA,MACA,UAAU;AAAA,QACR,UAAU,EAAE,SAAS,OAAO,WAAW,MAAM;AAAA,MAC/C;AAAA,MACA,MAAM;AAAA,MACN,OAAO,EAAE,UAAU,CAAC,GAAG,GAAG,MAAM;AAAA,IAClC,CAAC;AAwBH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2BA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+CA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwCA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA0DA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA;AA6BA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAM;AA6B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mBAAmB;AAQlB,SAAS,yBAAyB,SAA4B;AACnE,MAAI,CAAC,SAAS;AACZ;AAAA,EACF;AAIA,QAAM,OAAO,kBAAkB,OAAO;AACtC;AAAA,IACE,QAAQ;AAAA,IACR,mDACE,mBAAmB,GACrB;AAAA,EACF;AACF","sourcesContent":["import { assert } from '@metamask/snaps-sdk';\nimport type {\n FormState,\n InterfaceState,\n ComponentOrElement,\n InterfaceContext,\n State,\n} from '@metamask/snaps-sdk';\nimport type {\n DropdownElement,\n InputElement,\n JSXElement,\n OptionElement,\n FileInputElement,\n CheckboxElement,\n} from '@metamask/snaps-sdk/jsx';\nimport { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n getJsxChildren,\n getJsxElementFromComponent,\n walkJsx,\n} from '@metamask/snaps-utils';\n\n/**\n * Get a JSX element from a component or JSX element. If the component is a\n * JSX element, it is returned as is. Otherwise, the component is converted to\n * a JSX element.\n *\n * @param component - The component to convert.\n * @returns The JSX element.\n */\nexport function getJsxInterface(component: ComponentOrElement): JSXElement {\n if (isJSXElementUnsafe(component)) {\n return component;\n }\n\n return getJsxElementFromComponent(component);\n}\n\n/**\n * Assert that the component name is unique in state.\n *\n * @param state - The interface state to verify against.\n * @param name - The component name to verify.\n */\nexport function assertNameIsUnique(state: InterfaceState, name: string) {\n assert(\n state[name] === undefined,\n `Duplicate component names are not allowed, found multiple instances of: \"${name}\".`,\n );\n}\n\n/**\n * Construct default state for a component.\n *\n * This function is meant to be used inside constructInputState to account\n * for component specific defaults and will not override the component value or existing form state.\n *\n * @param element - The input element.\n * @returns The default state for the specific component, if any.\n */\nfunction constructComponentSpecificDefaultState(\n element: InputElement | DropdownElement | CheckboxElement,\n) {\n switch (element.type) {\n case 'Dropdown': {\n const children = getJsxChildren(element) as OptionElement[];\n return children[0]?.props.value;\n }\n\n case 'Checkbox':\n return false;\n\n default:\n return null;\n }\n}\n\n/**\n * Get the state value for a stateful component.\n *\n * Most components store the state value as a `value` prop.\n * This function exists to account for components where that isn't the case.\n *\n * @param element - The input element.\n * @returns The state value for a given component.\n */\nfunction getComponentStateValue(\n element: InputElement | DropdownElement | CheckboxElement,\n) {\n switch (element.type) {\n case 'Checkbox':\n return element.props.checked;\n\n default:\n return element.props.value;\n }\n}\n\n/**\n * Construct the state for an input field.\n *\n * @param oldState - The previous state.\n * @param element - The input element.\n * @param form - An optional form that the input is enclosed in.\n * @returns The input state.\n */\nfunction constructInputState(\n oldState: InterfaceState,\n element: InputElement | DropdownElement | FileInputElement | CheckboxElement,\n form?: string,\n) {\n const oldStateUnwrapped = form ? (oldState[form] as FormState) : oldState;\n const oldInputState = oldStateUnwrapped?.[element.props.name] as State;\n\n if (element.type === 'FileInput') {\n return oldInputState ?? null;\n }\n\n return (\n getComponentStateValue(element) ??\n oldInputState ??\n constructComponentSpecificDefaultState(element) ??\n null\n );\n}\n\n/**\n * Construct the interface state for a given component tree.\n *\n * @param oldState - The previous state.\n * @param rootComponent - The UI component to construct state from.\n * @returns The interface state of the passed component.\n */\nexport function constructState(\n oldState: InterfaceState,\n rootComponent: JSXElement,\n): InterfaceState {\n const newState: InterfaceState = {};\n\n // Stack containing the forms we have visited and at which depth\n const formStack: { name: string; depth: number }[] = [];\n\n walkJsx(rootComponent, (component, depth) => {\n let currentForm = formStack[formStack.length - 1];\n\n // Pop the current form of the stack once we leave its depth.\n if (currentForm && depth <= currentForm.depth) {\n formStack.pop();\n currentForm = formStack[formStack.length - 1];\n }\n\n if (component.type === 'Form') {\n assertNameIsUnique(newState, component.props.name);\n formStack.push({ name: component.props.name, depth });\n newState[component.props.name] = {};\n return;\n }\n\n // Stateful components inside a form\n if (\n currentForm &&\n (component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox')\n ) {\n const formState = newState[currentForm.name] as FormState;\n assertNameIsUnique(formState, component.props.name);\n formState[component.props.name] = constructInputState(\n oldState,\n component,\n currentForm.name,\n );\n return;\n }\n\n // Stateful components outside a form\n if (\n component.type === 'Input' ||\n component.type === 'Dropdown' ||\n component.type === 'FileInput' ||\n component.type === 'Checkbox'\n ) {\n assertNameIsUnique(newState, component.props.name);\n newState[component.props.name] = constructInputState(oldState, component);\n }\n });\n\n return newState;\n}\n\nconst MAX_CONTEXT_SIZE = 1_000_000; // 1 mb\n\n/**\n * Validate a JSON blob to be used as the interface context.\n *\n * @param context - The JSON blob.\n * @throws If the JSON blob is too large.\n */\nexport function validateInterfaceContext(context?: InterfaceContext) {\n if (!context) {\n return;\n }\n\n // We assume the validity of this JSON to be validated by the caller.\n // E.g., in the RPC method implementation.\n const size = getJsonSizeUnsafe(context);\n assert(\n size <= MAX_CONTEXT_SIZE,\n `A Snap interface context may not be larger than ${\n MAX_CONTEXT_SIZE / 1000000\n } MB.`,\n );\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/insights/SnapInsightsController.ts"],"names":[],"mappings":";;;;;;;;;AACA,SAAS,sBAAsB;AAM/B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB;AAc5B,IAAM,iBAAiB;AA5BvB;AA2EO,IAAM,yBAAN,cAAqC,eAI1C;AAAA,EACA,YAAY,EAAE,WAAW,MAAM,GAA+B;AAC5D,UAAM;AAAA,MACJ;AAAA,MACA,UAAU;AAAA,QACR,UAAU,EAAE,SAAS,OAAO,WAAW,MAAM;AAAA,MAC/C;AAAA,MACA,MAAM;AAAA,MACN,OAAO,EAAE,UAAU,CAAC,GAAG,GAAG,MAAM;AAAA,IAClC,CAAC;AAwBH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2BA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+CA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwCA;AAAA;AAAA;AA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AAAA,IACV;AAAA,EACF,CAAC;AAEH,OAAK,OAAO,CAAC,UAAU;AACrB,WAAO,MAAM,SAAS,EAAE;AAAA,EAC1B,CAAC;AACH;AAWM;AAAA,uBAAkB,eAAC;AAAA,EACvB;AAAA,EACA;AAAA,EACA;AACF,GAIG;AACD,SAAO,KAAK,gBAAgB,KAAK,gCAAgC;AAAA,IAC/D;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA,SAAS;AAAA,MACP,QAAQ;AAAA,MACR;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAWA;AAAA,wBAAmB,SAAC;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAKG;AACD,OAAK,OAAO,CAAC,UAAU;AACrB,UAAM,SAAS,EAAE,EAAE,MAAM,EAAE,UAAU;AACrC,UAAM,SAAS,EAAE,EAAE,MAAM,EAAE,cAAc,UAAU;AACnD,UAAM,SAAS,EAAE,EAAE,MAAM,EAAE,QAAQ,OAAO;AAAA,EAC5C,CAAC;AACH","sourcesContent":["import type { RestrictedControllerMessenger } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n Caveat,\n GetPermissions,\n ValidPermission,\n} from '@metamask/permission-controller';\nimport {\n getSignatureOriginCaveat,\n getTransactionOriginCaveat,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { Json, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport type { DeleteInterface } from '../interface';\nimport type { GetAllSnaps, HandleSnapRequest } from '../snaps';\nimport { getRunnableSnaps } from '../snaps';\nimport type {\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionMeta,\n SignatureStateChange,\n SignatureControllerState,\n StateSignature,\n TransactionControllerTransactionStatusUpdatedEvent,\n} from '../types';\n\nconst controllerName = 'SnapInsightsController';\n\nexport type SnapInsightsControllerAllowedActions =\n | HandleSnapRequest\n | GetAllSnaps\n | GetPermissions\n | DeleteInterface;\n\nexport type SnapInsightsControllerActions = never;\n\nexport type SnapInsightsControllerAllowedEvents =\n | TransactionControllerUnapprovedTransactionAddedEvent\n | TransactionControllerTransactionStatusUpdatedEvent\n | SignatureStateChange;\n\nexport type SnapInsightsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n SnapInsightsControllerActions | SnapInsightsControllerAllowedActions,\n SnapInsightsControllerAllowedEvents,\n SnapInsightsControllerAllowedActions['type'],\n SnapInsightsControllerAllowedEvents['type']\n>;\n\nexport type SnapInsight = {\n snapId: SnapId;\n interfaceId?: string | null;\n error?: string;\n loading: boolean;\n};\n\nexport type SnapInsightsControllerState = {\n insights: Record<string, Record<SnapId, SnapInsight>>;\n};\n\nexport type SnapInsightsControllerArgs = {\n messenger: SnapInsightsControllerMessenger;\n state?: SnapInsightsControllerState;\n};\n\ntype SnapWithPermission = {\n snapId: SnapId;\n permission: ValidPermission<string, Caveat<string, Json>>;\n};\n\n/**\n * Controller for monitoring for new transactions and signatures to provide insight for.\n */\nexport class SnapInsightsController extends BaseController<\n typeof controllerName,\n SnapInsightsControllerState,\n SnapInsightsControllerMessenger\n> {\n constructor({ messenger, state }: SnapInsightsControllerArgs) {\n super({\n messenger,\n metadata: {\n insights: { persist: false, anonymous: false },\n },\n name: controllerName,\n state: { insights: {}, ...state },\n });\n\n this.messagingSystem.subscribe(\n 'TransactionController:unapprovedTransactionAdded',\n this.#handleTransaction.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'TransactionController:transactionStatusUpdated',\n this.#handleTransactionStatusUpdate.bind(this),\n );\n\n this.messagingSystem.subscribe(\n 'SignatureController:stateChange',\n this.#handleSignatureStateChange.bind(this),\n );\n }\n\n /**\n * Check if an insight already exists for a given ID.\n *\n * @param id - The ID.\n * @returns True if the insight already exists, otherwise false.\n */\n #hasInsight(id: string) {\n return hasProperty(this.state.insights, id);\n }\n\n /**\n * Get a list of runnable Snaps that have a given permission.\n * Also includes the permission object itself.\n *\n * @param permissionName - The permission name.\n * @returns A list of objects containing Snap IDs and the permission object.\n */\n #getSnapsWithPermission(permissionName: string) {\n const allSnaps = this.messagingSystem.call('SnapController:getAll');\n const filteredSnaps = getRunnableSnaps(allSnaps);\n\n return filteredSnaps.reduce<SnapWithPermission[]>((accumulator, snap) => {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snap.id,\n );\n if (permissions && hasProperty(permissions, permissionName)) {\n accumulator.push({\n snapId: snap.id,\n permission: permissions[permissionName],\n });\n }\n\n return accumulator;\n }, []);\n }\n\n /**\n * Handle a newly added unapproved transaction.\n * This function fetches insights from all available Snaps\n * and populates the insights state blob with the responses.\n *\n * @param transaction - The transaction object.\n */\n #handleTransaction(transaction: TransactionMeta) {\n const { id, txParams, chainId, origin } = transaction;\n\n // This assumes that the transactions are EVM-compatible for now.\n const caipChainId = `eip155:${parseInt(chainId, 16)}`;\n\n const snaps = this.#getSnapsWithPermission(\n SnapEndowments.TransactionInsight,\n );\n\n snaps.forEach(({ snapId, permission }) => {\n const hasTransactionOriginCaveat = getTransactionOriginCaveat(permission);\n const transactionOrigin =\n hasTransactionOriginCaveat && origin ? origin : null;\n\n this.update((state) => {\n state.insights[id] ??= {};\n state.insights[id][snapId] = { snapId, loading: true };\n });\n\n this.#handleSnapRequest({\n snapId,\n handler: HandlerType.OnTransaction,\n params: {\n transaction: txParams,\n chainId: caipChainId,\n transactionOrigin,\n },\n })\n .then((response) =>\n this.#handleSnapResponse({\n id,\n snapId,\n response: response as Record<string, Json>,\n }),\n )\n .catch((error) => this.#handleSnapResponse({ id, snapId, error }));\n });\n }\n\n /**\n * Handle the stateChange event emitted by the SignatureController.\n * This function will remove existing insights from the state when applicable, as well as\n * trigger insight fetching for newly added signatures.\n *\n * @param state - The SignatureController state blob.\n */\n #handleSignatureStateChange(state: SignatureControllerState) {\n // If any IDs have disappeared since the last state update, the insight may be cleaned up.\n for (const id of Object.keys(this.state.insights)) {\n if (\n !hasProperty(state.unapprovedTypedMessages, id) &&\n !hasProperty(state.unapprovedPersonalMsgs, id)\n ) {\n this.#handleInsightCleanup(id);\n }\n }\n\n if (\n state.unapprovedPersonalMsgCount > 0 ||\n state.unapprovedTypedMessagesCount > 0\n ) {\n const snaps = this.#getSnapsWithPermission(\n SnapEndowments.SignatureInsight,\n );\n\n // This isn't very efficient, but SignatureController doesn't expose a better event for us to use yet.\n for (const personalSignature of Object.values(\n state.unapprovedPersonalMsgs,\n )) {\n this.#handleSignature(snaps, personalSignature);\n }\n\n for (const typedMessage of Object.values(state.unapprovedTypedMessages)) {\n this.#handleSignature(snaps, typedMessage);\n }\n }\n }\n\n /**\n * Handle a newly added unapproved signature.\n * This function fetches insights from all available Snaps\n * and populates the insights state blob with the responses.\n *\n * @param snaps - A list of Snaps to invoke.\n * @param signature - The signature object.\n */\n #handleSignature(snaps: SnapWithPermission[], signature: StateSignature) {\n const { id, msgParams } = signature;\n\n if (this.#hasInsight(id)) {\n return;\n }\n\n const { from, data, signatureMethod, origin } = msgParams;\n\n /**\n * Both eth_signTypedData_v3 and eth_signTypedData_v4 methods\n * need to be parsed because their data is stringified. All other\n * signature methods do not, so they are ignored.\n */\n const shouldParse =\n signatureMethod === 'eth_signTypedData_v3' ||\n signatureMethod === 'eth_signTypedData_v4';\n\n const payload = {\n from,\n data: shouldParse ? JSON.parse(data as string) : data,\n signatureMethod,\n };\n\n snaps.forEach(({ snapId, permission }) => {\n const hasSignatureOriginCaveat = getSignatureOriginCaveat(permission);\n const signatureOrigin =\n origin && hasSignatureOriginCaveat ? origin : null;\n\n this.update((state) => {\n state.insights[id] ??= {};\n state.insights[id][snapId] = { snapId, loading: true };\n });\n\n this.#handleSnapRequest({\n snapId,\n handler: HandlerType.OnSignature,\n params: { signature: payload, signatureOrigin },\n })\n .then((response) =>\n this.#handleSnapResponse({\n id,\n snapId,\n response: response as Record<string, Json>,\n }),\n )\n .catch((error) => this.#handleSnapResponse({ id, snapId, error }));\n });\n }\n\n /**\n * Handle the transactionStatusUpdated event emitted by the TransactionController.\n * This function will remove insights for the transaction in question\n * once the transaction status has changed from unapproved.\n *\n * @param args - An options bag.\n * @param args.transactionMeta - The transaction.\n */\n #handleTransactionStatusUpdate({\n transactionMeta,\n }: {\n transactionMeta: TransactionMeta;\n }) {\n if (transactionMeta.status !== 'unapproved') {\n this.#handleInsightCleanup(transactionMeta.id);\n }\n }\n\n #handleInsightCleanup(id: string) {\n if (!this.#hasInsight(id)) {\n return;\n }\n\n // Delete interfaces from interface controller.\n Object.values(this.state.insights[id])\n .filter((insight) => insight.interfaceId)\n .forEach((insight) => {\n this.messagingSystem.call(\n 'SnapInterfaceController:deleteInterface',\n insight.interfaceId as string,\n );\n });\n\n this.update((state) => {\n delete state.insights[id];\n });\n }\n\n /**\n * Handle sending a request to a given Snap with a given payload.\n *\n * @param args - An options bag.\n * @param args.snapId - The Snap ID.\n * @param args.handler - The handler to invoke.\n * @param args.params - The JSON-RPC params to send.\n * @returns The response from the Snap.\n */\n async #handleSnapRequest({\n snapId,\n handler,\n params,\n }: {\n snapId: SnapId;\n handler: HandlerType.OnTransaction | HandlerType.OnSignature;\n params: Record<string, Json>;\n }) {\n return this.messagingSystem.call('SnapController:handleRequest', {\n snapId,\n origin: '',\n handler,\n request: {\n method: '',\n params,\n },\n });\n }\n\n /**\n * Handle response from a given Snap by persisting the response or error in state.\n *\n * @param args - An options bag.\n * @param args.id - The transaction or signature ID.\n * @param args.snapId - The Snap ID.\n * @param args.response - An optional response object returned by the Snap.\n * @param args.error - An optional error returned by the Snap.\n */\n #handleSnapResponse({\n id,\n snapId,\n response,\n error,\n }: {\n id: string;\n snapId: SnapId;\n response?: Record<string, Json>;\n error?: Error;\n }) {\n this.update((state) => {\n state.insights[id][snapId].loading = false;\n state.insights[id][snapId].interfaceId = response?.id as string;\n state.insights[id][snapId].error = error?.message;\n });\n }\n}\n"]}
|
|
File without changes
|
|
File without changes
|