-
Notifications
You must be signed in to change notification settings - Fork 542
/
MultiChainProvider.ts
173 lines (153 loc) · 4.55 KB
/
MultiChainProvider.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import SafeEventEmitter from '@metamask/safe-event-emitter';
import {
assertIsConnectArguments,
assertIsMetaMaskNotification,
assertIsMultiChainRequest,
assertIsSession,
ChainId,
ConnectArguments,
NamespaceId,
RequestArguments,
RequestNamespace,
Session,
} from '@metamask/snaps-utils';
import { JsonRpcRequest, Json } from '@metamask/utils';
import { MetaMaskInpageProvider } from '@metamask/providers';
import { nanoid } from 'nanoid';
import { Provider } from './Provider';
declare global {
// Declaration merging doesn't work with types, so we have to use an interface
// here.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
ethereum: MetaMaskInpageProvider;
}
}
export class MultiChainProvider extends SafeEventEmitter implements Provider {
#isConnected = false;
constructor() {
super();
const provider = this.#getProvider();
provider.on('multichainHack_metamask_disconnect', () => {
this.#isConnected = false;
this.emit('session_delete');
});
provider.on('multichainHack_metamask_event', (notification) => {
assertIsMetaMaskNotification(notification);
this.emit('session_event', {
params: {
chainId: notification.params.chainId,
event: notification.params.event,
},
});
});
}
/**
* Get whether the provider is connected to the upstream wallet.
*
* @returns Whether the provider is connected to the upstream wallet.
*/
get isConnected() {
return this.#isConnected;
}
/**
* Connect to MetaMask and request a session with the given namespace(s).
*
* @param args - The connection arguments.
* @param args.requiredNamespaces - The namespaces to request.
* @returns An object containing an `approval` function, which can be called
* to connect to the wallet.
*/
async connect(
args: ConnectArguments,
): Promise<{ approval(): Promise<Session> }> {
assertIsConnectArguments(args);
// We're injected, we don't need to establish connection to the wallet
// and can just return approval straight away.
return {
approval: async () => {
const requiredNamespaces = Object.entries(
args.requiredNamespaces,
).reduce<Record<NamespaceId, RequestNamespace>>(
(acc, [id, definition]) => {
acc[id] = {
chains: definition.chains,
methods: definition.methods ?? [],
events: definition.events ?? [],
};
return acc;
},
{},
);
this.#isConnected = false;
const session = await this.#rpcRequest({
method: 'metamask_handshake',
params: { requiredNamespaces },
});
assertIsSession(session);
this.#isConnected = true;
this.emit('session_update', { params: session });
return session;
},
};
}
/**
* Send a multichain request to the wallet. The provider must be connected to
* the wallet using {@link MultiChainProvider#connect} before this method can
* be called.
*
* @param args - The multichain request arguments.
* @param args.chainId - The chain ID to use for the request.
* @param args.request - The JSON-RPC request to send.
* @returns The JSON-RPC response.
*/
async request(args: {
chainId: ChainId;
request: RequestArguments;
}): Promise<unknown> {
if (!this.#isConnected) {
throw new Error('No session connected.');
}
assertIsMultiChainRequest(args);
// We're doing it this way to avoid sentRequest.params = undefined.
const sentRequest: Json = { method: args.request.method };
if (args.request.params !== undefined) {
sentRequest.params = args.request.params;
}
return this.#rpcRequest({
method: 'caip_request',
params: {
chainId: args.chainId,
request: sentRequest,
},
});
}
/**
* Get the provider that is injected by the wallet, i.e., `window.ethereum`.
*
* @returns The injected provider.
*/
#getProvider() {
return window.ethereum;
}
/**
* Send an RPC request to the wallet.
*
* @param payload - The JSON-RPC request to send.
* @returns The JSON-RPC response.
*/
async #rpcRequest(
payload: { method: string } & Partial<
JsonRpcRequest<Record<string, Json> | Json[] | undefined>
>,
) {
return await this.#getProvider().request({
method: 'wallet_multiChainRequestHack',
params: {
jsonrpc: '2.0',
id: nanoid(),
...payload,
},
});
}
}