-
Notifications
You must be signed in to change notification settings - Fork 542
/
notification.ts
64 lines (56 loc) · 1.64 KB
/
notification.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Infer, is, literal, object, string, unknown } from 'superstruct';
import { assertStruct } from '@metamask/utils';
import { ChainIdStruct } from './namespace';
export const EventStruct = object({
name: string(),
data: unknown(),
});
export type Event = Infer<typeof EventStruct>;
/**
* Check if a value is a SIP-2 event.
*
* @param value - The value to check.
* @returns Whether the value is a SIP-2 event.
*/
export function isEvent(value: unknown): value is Event {
return is(value, EventStruct);
}
/**
* Assert that a value is a SIP-2 event.
*
* @param value - The value to check.
* @throws If the value is not a SIP-2 event.
*/
export function assertIsEvent(value: unknown): asserts value is Event {
assertStruct(value, EventStruct, 'Invalid event');
}
export const MetaMaskNotificationStruct = object({
method: literal('multichainHack_metamask_event'),
params: object({
chainId: ChainIdStruct,
event: EventStruct,
}),
});
export type MetaMaskNotification = Infer<typeof MetaMaskNotificationStruct>;
/**
* Check if a value is a SIP-2 notification.
*
* @param value - The value to check.
* @returns Whether the value is a SIP-2 notification.
*/
export function isMetaMaskNotification(
value: unknown,
): value is MetaMaskNotification {
return is(value, MetaMaskNotificationStruct);
}
/**
* Assert that a value is a SIP-2 notification.
*
* @param value - The value to check.
* @throws If the value is not a SIP-2 notification.
*/
export function assertIsMetaMaskNotification(
value: unknown,
): asserts value is MetaMaskNotification {
assertStruct(value, MetaMaskNotificationStruct, 'Invalid notification');
}