-
Notifications
You must be signed in to change notification settings - Fork 542
/
snap.ts
39 lines (38 loc) · 1.07 KB
/
snap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { OnRpcRequestHandler } from '@metamask/snaps-types';
/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
*
* @param args - The request handler args as object.
* @param args.origin - The origin of the request, e.g., the website that
* invoked the snap.
* @param args.request - A validated JSON-RPC request object.
* @returns `null` if the request succeeded.
* @throws If the request method is not valid for this snap.
* @throws If the `snap_notify` call failed.
*/
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return snaps.request({
method: 'snap_notify',
params: [
{
type: 'inApp',
message: `Hello, ${origin}!`,
},
],
});
case 'native':
return snaps.request({
method: 'snap_notify',
params: [
{
type: 'native',
message: `Hello, ${origin}!`,
},
],
});
default:
throw new Error('Method not found.');
}
};