@cowprotocol/iframe-transport 2.0.7 → 2.0.9
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 +23 -0
- package/package.json +25 -3
- package/src/IframeTransport.d.ts +11 -0
- package/src/IframeTransport.js +42 -0
- package/src/IframeTransport.js.map +1 -0
- package/src/iframeRpcProvider/IframeRpcProviderBridge.d.ts +45 -0
- package/src/iframeRpcProvider/IframeRpcProviderBridge.js +143 -0
- package/src/iframeRpcProvider/IframeRpcProviderBridge.js.map +1 -0
- package/src/iframeRpcProvider/WidgetEthereumProvider.d.ts +134 -0
- package/src/iframeRpcProvider/WidgetEthereumProvider.js +300 -0
- package/src/iframeRpcProvider/WidgetEthereumProvider.js.map +1 -0
- package/src/iframeRpcProvider/iframeRpcProviderEvents.d.ts +31 -0
- package/src/iframeRpcProvider/iframeRpcProviderEvents.js +11 -0
- package/src/iframeRpcProvider/iframeRpcProviderEvents.js.map +1 -0
- package/src/iframeRpcProvider/utils.d.ts +4 -0
- package/src/iframeRpcProvider/utils.js +11 -0
- package/src/iframeRpcProvider/utils.js.map +1 -0
- package/src/index.d.ts +5 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/types.d.ts +59 -0
- package/src/types.js +2 -0
- package/src/types.js.map +1 -0
- package/.babelrc +0 -20
- package/jest.config.ts +0 -11
- package/project.json +0 -42
- package/src/IframeTransport.ts +0 -73
- package/src/iframeRpcProvider/IframeRpcProviderBridge.ts +0 -192
- package/src/iframeRpcProvider/WidgetEthereumProvider.ts +0 -489
- package/src/iframeRpcProvider/iframeRpcProviderEvents.ts +0 -40
- package/src/iframeRpcProvider/utils.ts +0 -18
- package/src/index.ts +0 -5
- package/src/types.ts +0 -75
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -20
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ===========================================================================
|
|
3
|
+
* This is a modified version of the original iframe-provider library.
|
|
4
|
+
*
|
|
5
|
+
* The original had some deprecated methods and events, and it was hard to debug for the format used in the messages.
|
|
6
|
+
* This modified version makes use of the Widget library typed subscriptions
|
|
7
|
+
*
|
|
8
|
+
* https://github.com/ethvault/iframe-provider/blob/master/src/index.ts
|
|
9
|
+
*
|
|
10
|
+
* ===========================================================================
|
|
11
|
+
*/
|
|
12
|
+
import { EventEmitter } from 'eventemitter3';
|
|
13
|
+
import { IframeRpcProviderEvents, iframeRpcProviderTransport, } from './iframeRpcProviderEvents';
|
|
14
|
+
// By default timeout is 10 minutes
|
|
15
|
+
const DEFAULT_TIMEOUT_MILLISECONDS = 600000;
|
|
16
|
+
const JSON_RPC_VERSION = '2.0';
|
|
17
|
+
/**
|
|
18
|
+
* We return a random number between the 0 and the maximum safe integer so that we always generate a unique identifier,
|
|
19
|
+
* across all communication channels.
|
|
20
|
+
*/
|
|
21
|
+
function getUniqueId() {
|
|
22
|
+
return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Represents an error in an RPC returned from the event source. Always contains a code and a reason. The message
|
|
26
|
+
* is constructed from both.
|
|
27
|
+
*/
|
|
28
|
+
export class RpcError extends Error {
|
|
29
|
+
isRpcError = true;
|
|
30
|
+
code;
|
|
31
|
+
reason;
|
|
32
|
+
constructor(code, reason) {
|
|
33
|
+
super(`${code}: ${reason}`);
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.reason = reason;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* This is the primary artifact of this library.
|
|
40
|
+
*/
|
|
41
|
+
export class WidgetEthereumProvider extends EventEmitter {
|
|
42
|
+
// TODO: Add proper return type annotation
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
44
|
+
request({ method, params }) {
|
|
45
|
+
return this.send(method, params);
|
|
46
|
+
}
|
|
47
|
+
isConnected() {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Differentiate this provider from other providers by providing an isIFrame property that always returns true.
|
|
52
|
+
*/
|
|
53
|
+
get isIFrame() {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Always return this for currentProvider.
|
|
58
|
+
*/
|
|
59
|
+
get currentProvider() {
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
enabled = null;
|
|
63
|
+
timeoutMilliseconds;
|
|
64
|
+
eventSource;
|
|
65
|
+
eventTarget;
|
|
66
|
+
completers = {};
|
|
67
|
+
providerMetaInfo = null;
|
|
68
|
+
providerMetaInfoCallback;
|
|
69
|
+
constructor({ timeoutMilliseconds = DEFAULT_TIMEOUT_MILLISECONDS, eventSource = window, eventTarget = window.parent, } = {}) {
|
|
70
|
+
// Call super for `this` to be defined
|
|
71
|
+
super();
|
|
72
|
+
this.timeoutMilliseconds = timeoutMilliseconds;
|
|
73
|
+
this.eventSource = eventSource;
|
|
74
|
+
this.eventTarget = eventTarget;
|
|
75
|
+
iframeRpcProviderTransport.listenToMessageFromWindow(this.eventSource, IframeRpcProviderEvents.PROVIDER_RPC_RESPONSE, (message) => {
|
|
76
|
+
this.handleRpcRequests(message);
|
|
77
|
+
});
|
|
78
|
+
iframeRpcProviderTransport.listenToMessageFromWindow(this.eventSource, IframeRpcProviderEvents.PROVIDER_ON_EVENT, (message) => {
|
|
79
|
+
this.handleOnEvent(message);
|
|
80
|
+
});
|
|
81
|
+
iframeRpcProviderTransport.listenToMessageFromWindow(this.eventSource, IframeRpcProviderEvents.SEND_PROVIDER_META_INFO, (message) => {
|
|
82
|
+
this.providerMetaInfo = message;
|
|
83
|
+
if (this.providerMetaInfoCallback) {
|
|
84
|
+
this.providerMetaInfoCallback(this.providerMetaInfo);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Helper method that handles transport and request wrapping
|
|
90
|
+
* @param method method to execute
|
|
91
|
+
* @param params params to pass the method
|
|
92
|
+
*/
|
|
93
|
+
async execute(method, params) {
|
|
94
|
+
const id = getUniqueId();
|
|
95
|
+
const rpcRequest = {
|
|
96
|
+
jsonrpc: JSON_RPC_VERSION,
|
|
97
|
+
id,
|
|
98
|
+
method,
|
|
99
|
+
params,
|
|
100
|
+
};
|
|
101
|
+
const promise = new Promise((resolve, reject) => (this.completers[id] = { resolve, reject }));
|
|
102
|
+
// Send the JSON RPC to the event source.
|
|
103
|
+
iframeRpcProviderTransport.postMessageToWindow(this.eventTarget, IframeRpcProviderEvents.PROVIDER_RPC_REQUEST, {
|
|
104
|
+
rpcRequest,
|
|
105
|
+
});
|
|
106
|
+
// Delete the completer within the timeout and reject the promise.
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
if (this.completers[id]) {
|
|
109
|
+
this.completers[id].reject(new Error('Request timed out. Please try again and submit it faster.'));
|
|
110
|
+
delete this.completers[id];
|
|
111
|
+
}
|
|
112
|
+
}, this.timeoutMilliseconds);
|
|
113
|
+
return promise;
|
|
114
|
+
}
|
|
115
|
+
// TODO: Replace any with proper type definitions
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
+
async send(methodOrRequest, paramsOrCallback) {
|
|
118
|
+
const { method, params } = typeof methodOrRequest === 'string'
|
|
119
|
+
? { method: methodOrRequest, params: paramsOrCallback }
|
|
120
|
+
: { method: methodOrRequest.method, params: methodOrRequest.params };
|
|
121
|
+
// TODO: Replace any with proper type definitions
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
123
|
+
const response = await this.execute(method, params);
|
|
124
|
+
if ('error' in response) {
|
|
125
|
+
throw new RpcError(response.error.code, response.error.message);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return response.result;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Request the parent window to enable access to the user's web3 provider. Return accounts list immediately if already enabled.
|
|
133
|
+
* Keep the legacy method for backward compatibility.
|
|
134
|
+
*/
|
|
135
|
+
async enable() {
|
|
136
|
+
if (this.enabled === null) {
|
|
137
|
+
const promise = (this.enabled = this.send('eth_requestAccounts', []).catch((error) => {
|
|
138
|
+
// Clear this.enabled if it's this promise so we try again next call.
|
|
139
|
+
// this.enabled might be set from elsewhere if, e.g. the accounts changed event is emitted
|
|
140
|
+
if (this.enabled === promise) {
|
|
141
|
+
this.enabled = null;
|
|
142
|
+
}
|
|
143
|
+
// Rethrow the error.
|
|
144
|
+
throw error;
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
return this.enabled;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Backwards compatibility method for web3.
|
|
151
|
+
* @param payload payload to send to the provider
|
|
152
|
+
* @param callback callback to be called when the provider resolves
|
|
153
|
+
*/
|
|
154
|
+
async sendAsync(payload,
|
|
155
|
+
// TODO: Replace any with proper type definitions
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
157
|
+
callback) {
|
|
158
|
+
try {
|
|
159
|
+
const result = await this.execute(payload.method, payload.params);
|
|
160
|
+
callback(null, result);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
callback(error, null);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Subscribe to provider meta info
|
|
168
|
+
* @param callback
|
|
169
|
+
*/
|
|
170
|
+
// TODO: Add proper return type annotation
|
|
171
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
172
|
+
onProviderMetaInfo(callback) {
|
|
173
|
+
if (this.providerMetaInfo) {
|
|
174
|
+
callback(this.providerMetaInfo);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
this.providerMetaInfoCallback = callback;
|
|
178
|
+
iframeRpcProviderTransport.postMessageToWindow(this.eventTarget, IframeRpcProviderEvents.REQUEST_PROVIDER_META_INFO, null);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Handle a Rpc Request
|
|
183
|
+
*/
|
|
184
|
+
// TODO: Add proper return type annotation
|
|
185
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
186
|
+
handleRpcRequests = ({ rpcResponse }) => {
|
|
187
|
+
if (rpcResponse.id === undefined || rpcResponse.id === null) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const completer = this.completers['' + rpcResponse.id];
|
|
191
|
+
// True if we haven't timed out and this is a response to a message we sent.
|
|
192
|
+
if (completer) {
|
|
193
|
+
// Handle pending promise
|
|
194
|
+
if ('error' in rpcResponse || 'result' in rpcResponse) {
|
|
195
|
+
completer.resolve(rpcResponse);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
completer.reject(new Error('Response from provider did not have error or result key'));
|
|
199
|
+
}
|
|
200
|
+
delete this.completers[rpcResponse.id];
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
// TODO: Add proper return type annotation
|
|
204
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
205
|
+
handleOnEvent(message) {
|
|
206
|
+
// TODO: Replace any with proper type definitions
|
|
207
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
208
|
+
const params = message.params;
|
|
209
|
+
switch (message.event) {
|
|
210
|
+
case 'notification':
|
|
211
|
+
this.emitNotification(params);
|
|
212
|
+
break;
|
|
213
|
+
case 'connect':
|
|
214
|
+
this.emitConnect(params);
|
|
215
|
+
break;
|
|
216
|
+
case 'disconnect':
|
|
217
|
+
this.emitDisconnect(params);
|
|
218
|
+
break;
|
|
219
|
+
case 'chainChanged':
|
|
220
|
+
this.emitChainChanged(params);
|
|
221
|
+
break;
|
|
222
|
+
case 'accountsChanged':
|
|
223
|
+
this.emitAccountsChanged(params);
|
|
224
|
+
break;
|
|
225
|
+
case 'message':
|
|
226
|
+
this.emitMessage(params);
|
|
227
|
+
break;
|
|
228
|
+
// Deprecated events below 👇 --------------------------------------
|
|
229
|
+
case 'networkChanged':
|
|
230
|
+
this.emitNetworkChanged(params);
|
|
231
|
+
break;
|
|
232
|
+
case 'close':
|
|
233
|
+
this.emitClose(params);
|
|
234
|
+
break;
|
|
235
|
+
// END OF: Deprecated events --------------------------------------
|
|
236
|
+
default:
|
|
237
|
+
console.warn(`[WidgetEthereumProvider] Unknown event type: ${message.event}`);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// TODO: Add proper return type annotation
|
|
242
|
+
// TODO: Replace any with proper type definitions
|
|
243
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
|
|
244
|
+
emitNotification(result) {
|
|
245
|
+
this.emit('notification', result);
|
|
246
|
+
}
|
|
247
|
+
// TODO: Add proper return type annotation
|
|
248
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
249
|
+
emitConnect(connectInfo) {
|
|
250
|
+
// If the provider isn't enabled but it emits a connect event, assume that it's enabled and initialize
|
|
251
|
+
// with an empty list of accounts.
|
|
252
|
+
if (this.enabled === null) {
|
|
253
|
+
this.enabled = Promise.resolve([]);
|
|
254
|
+
}
|
|
255
|
+
this.emit('connect', connectInfo);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @deprecated See https://eips.ethereum.org/EIPS/eip-1193
|
|
259
|
+
*/
|
|
260
|
+
// TODO: Add proper return type annotation
|
|
261
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
262
|
+
emitClose(params) {
|
|
263
|
+
if (Array.isArray(params) && params.length === 2) {
|
|
264
|
+
this.emit('close', params[0], params[1]);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
this.emit('close');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// TODO: Add proper return type annotation
|
|
271
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
272
|
+
emitDisconnect(error) {
|
|
273
|
+
this.emit('disconnect', error);
|
|
274
|
+
}
|
|
275
|
+
// TODO: Add proper return type annotation
|
|
276
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
277
|
+
emitChainChanged(chainId) {
|
|
278
|
+
this.emit('chainChanged', chainId);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* @deprecated See https://eips.ethereum.org/EIPS/eip-1193
|
|
282
|
+
*/
|
|
283
|
+
// TODO: Add proper return type annotation
|
|
284
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
285
|
+
emitNetworkChanged(networkId) {
|
|
286
|
+
this.emit('networkChanged', networkId);
|
|
287
|
+
}
|
|
288
|
+
// TODO: Add proper return type annotation
|
|
289
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
290
|
+
emitAccountsChanged(accounts) {
|
|
291
|
+
this.enabled = Promise.resolve(accounts);
|
|
292
|
+
this.emit('accountsChanged', accounts);
|
|
293
|
+
}
|
|
294
|
+
// TODO: Add proper return type annotation
|
|
295
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
296
|
+
emitMessage(message) {
|
|
297
|
+
this.emit('message', message);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
//# sourceMappingURL=WidgetEthereumProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WidgetEthereumProvider.js","sourceRoot":"","sources":["../../../../../libs/iframe-transport/src/iframeRpcProvider/WidgetEthereumProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAE5C,OAAO,EACL,uBAAuB,EACvB,0BAA0B,GAI3B,MAAM,2BAA2B,CAAA;AA4BlC,mCAAmC;AACnC,MAAM,4BAA4B,GAAG,MAAM,CAAA;AAE3C,MAAM,gBAAgB,GAAG,KAAK,CA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AAA;IAChC,CAAC;IAED,0CAA0C;IAC1C,4EAA4E;IACpE,gBAAgB,CAAC,OAAe;QACtC,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;IACpC,CAAC;IAED;;OAEG;IACH,0CAA0C;IAC1C,4EAA4E;IACpE,kBAAkB,CAAC,SAAiB;QAC1C,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAA;IACxC,CAAC;IAED,0CAA0C;IAC1C,4EAA4E;IACpE,mBAAmB,CAAC,QAAkB;QAC5C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;QACxC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAA;IACxC,CAAC;IAED,0CAA0C;IAC1C,4EAA4E;IACpE,WAAW,CAAC,OAAwB;QAC1C,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;IAC/B,CAAC;CACF"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IframeTransport } from '../IframeTransport';
|
|
2
|
+
import type { EIP6963ProviderInfo, JsonRpcRequestMessage, JsonRpcResponse, ProviderWcMetadata } from '../types';
|
|
3
|
+
export interface ProviderRpcRequestPayload {
|
|
4
|
+
rpcRequest: JsonRpcRequestMessage;
|
|
5
|
+
}
|
|
6
|
+
export type ProviderRpcResponsePayload = {
|
|
7
|
+
rpcResponse: JsonRpcResponse;
|
|
8
|
+
};
|
|
9
|
+
export interface ProviderOnEventPayload {
|
|
10
|
+
event: string;
|
|
11
|
+
params: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface ProviderMetaInfoPayload {
|
|
14
|
+
providerEip6963Info?: EIP6963ProviderInfo;
|
|
15
|
+
providerWcMetadata?: ProviderWcMetadata;
|
|
16
|
+
}
|
|
17
|
+
export declare enum IframeRpcProviderEvents {
|
|
18
|
+
PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST",
|
|
19
|
+
PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE",
|
|
20
|
+
PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT",
|
|
21
|
+
SEND_PROVIDER_META_INFO = "SEND_PROVIDER_META_INFO",
|
|
22
|
+
REQUEST_PROVIDER_META_INFO = "REQUEST_PROVIDER_META_INFO"
|
|
23
|
+
}
|
|
24
|
+
export interface IframeEventsPayloadMap {
|
|
25
|
+
[IframeRpcProviderEvents.PROVIDER_RPC_REQUEST]: ProviderRpcRequestPayload;
|
|
26
|
+
[IframeRpcProviderEvents.PROVIDER_RPC_RESPONSE]: ProviderRpcResponsePayload;
|
|
27
|
+
[IframeRpcProviderEvents.PROVIDER_ON_EVENT]: ProviderOnEventPayload;
|
|
28
|
+
[IframeRpcProviderEvents.SEND_PROVIDER_META_INFO]: ProviderMetaInfoPayload;
|
|
29
|
+
[IframeRpcProviderEvents.REQUEST_PROVIDER_META_INFO]: null;
|
|
30
|
+
}
|
|
31
|
+
export declare const iframeRpcProviderTransport: IframeTransport<IframeEventsPayloadMap>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IframeTransport } from '../IframeTransport';
|
|
2
|
+
export var IframeRpcProviderEvents;
|
|
3
|
+
(function (IframeRpcProviderEvents) {
|
|
4
|
+
IframeRpcProviderEvents["PROVIDER_RPC_REQUEST"] = "PROVIDER_RPC_REQUEST";
|
|
5
|
+
IframeRpcProviderEvents["PROVIDER_RPC_RESPONSE"] = "PROVIDER_RPC_RESPONSE";
|
|
6
|
+
IframeRpcProviderEvents["PROVIDER_ON_EVENT"] = "PROVIDER_ON_EVENT";
|
|
7
|
+
IframeRpcProviderEvents["SEND_PROVIDER_META_INFO"] = "SEND_PROVIDER_META_INFO";
|
|
8
|
+
IframeRpcProviderEvents["REQUEST_PROVIDER_META_INFO"] = "REQUEST_PROVIDER_META_INFO";
|
|
9
|
+
})(IframeRpcProviderEvents || (IframeRpcProviderEvents = {}));
|
|
10
|
+
export const iframeRpcProviderTransport = new IframeTransport('cowSwapIframeRpcProviderTransport');
|
|
11
|
+
//# sourceMappingURL=iframeRpcProviderEvents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iframeRpcProviderEvents.js","sourceRoot":"","sources":["../../../../../libs/iframe-transport/src/iframeRpcProvider/iframeRpcProviderEvents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAqBpD,MAAM,CAAN,IAAY,uBAMX;AAND,WAAY,uBAAuB;IACjC,wEAA6C,CAAA;IAC7C,0EAA+C,CAAA;IAC/C,kEAAuC,CAAA;IACvC,8EAAmD,CAAA;IACnD,oFAAyD,CAAA;AAC3D,CAAC,EANW,uBAAuB,KAAvB,uBAAuB,QAMlC;AAUD,MAAM,CAAC,MAAM,0BAA0B,GAAG,IAAI,eAAe,CAC3D,mCAAmC,CACpC,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EIP6963ProviderDetail } from '@cowprotocol/types';
|
|
2
|
+
import { EIP6963ProviderInfo, ProviderWcMetadata } from '../types';
|
|
3
|
+
export declare function getProviderWcMetadata(provider: any): ProviderWcMetadata | undefined;
|
|
4
|
+
export declare function getEip6963ProviderInfo(provider: unknown, eip6963Providers: EIP6963ProviderDetail[]): EIP6963ProviderInfo | undefined;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// TODO: Replace any with proper type definitions
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
export function getProviderWcMetadata(provider) {
|
|
4
|
+
if (!provider.isWalletConnect)
|
|
5
|
+
return;
|
|
6
|
+
return provider?.session?.peer?.metadata;
|
|
7
|
+
}
|
|
8
|
+
export function getEip6963ProviderInfo(provider, eip6963Providers) {
|
|
9
|
+
return eip6963Providers.find((p) => p.provider === provider)?.info;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../../libs/iframe-transport/src/iframeRpcProvider/utils.ts"],"names":[],"mappings":"AAIA,iDAAiD;AACjD,8DAA8D;AAC9D,MAAM,UAAU,qBAAqB,CAAC,QAAa;IACjD,IAAI,CAAC,QAAQ,CAAC,eAAe;QAAE,OAAM;IAErC,OAAO,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAA;AAC1C,CAAC;AAED,MAAM,UAAU,sBAAsB,CACpC,QAAiB,EACjB,gBAAyC;IAEzC,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,EAAE,IAAI,CAAA;AACpE,CAAC"}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { IframeRpcProviderBridge } from './iframeRpcProvider/IframeRpcProviderBridge';
|
|
2
|
+
export { WidgetEthereumProvider } from './iframeRpcProvider/WidgetEthereumProvider';
|
|
3
|
+
export { IframeTransport } from './IframeTransport';
|
|
4
|
+
export * from './iframeRpcProvider/iframeRpcProviderEvents';
|
|
5
|
+
export * from './types';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { IframeRpcProviderBridge } from './iframeRpcProvider/IframeRpcProviderBridge';
|
|
2
|
+
export { WidgetEthereumProvider } from './iframeRpcProvider/WidgetEthereumProvider';
|
|
3
|
+
export { IframeTransport } from './IframeTransport';
|
|
4
|
+
export * from './iframeRpcProvider/iframeRpcProviderEvents';
|
|
5
|
+
export * from './types';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/iframe-transport/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAA;AACrF,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,6CAA6C,CAAA;AAC3D,cAAc,SAAS,CAAA"}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export interface JsonRpcRequest {
|
|
2
|
+
id?: number;
|
|
3
|
+
method: string;
|
|
4
|
+
params?: any[];
|
|
5
|
+
}
|
|
6
|
+
export interface JsonRpcRequestMessage {
|
|
7
|
+
jsonrpc: '2.0';
|
|
8
|
+
id?: number;
|
|
9
|
+
method: string;
|
|
10
|
+
params: unknown[] | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface BaseJsonRpcResponseMessage {
|
|
13
|
+
id: number;
|
|
14
|
+
jsonrpc: '2.0';
|
|
15
|
+
}
|
|
16
|
+
export interface JsonRpcSucessfulResponseMessage<TResult = unknown> extends BaseJsonRpcResponseMessage {
|
|
17
|
+
result: TResult;
|
|
18
|
+
}
|
|
19
|
+
export interface JsonRpcError<TData = unknown> {
|
|
20
|
+
code: number;
|
|
21
|
+
message: string;
|
|
22
|
+
data?: TData;
|
|
23
|
+
}
|
|
24
|
+
export interface JsonRpcErrorResponseMessage<TErrorData = unknown> extends BaseJsonRpcResponseMessage {
|
|
25
|
+
error: JsonRpcError<TErrorData>;
|
|
26
|
+
}
|
|
27
|
+
export type JsonRpcResponse = JsonRpcRequestMessage | JsonRpcErrorResponseMessage | JsonRpcSucessfulResponseMessage;
|
|
28
|
+
export interface EthereumProvider {
|
|
29
|
+
/**
|
|
30
|
+
* Subscribes to Ethereum-related events.
|
|
31
|
+
* @param event - The event to subscribe to.
|
|
32
|
+
* @param args - Arguments for the event.
|
|
33
|
+
*/
|
|
34
|
+
on(event: string, args: unknown): void;
|
|
35
|
+
/**
|
|
36
|
+
* Sends a JSON-RPC request to the Ethereum provider and returns the response.
|
|
37
|
+
* @param params - JSON-RPC request parameters.
|
|
38
|
+
* @returns A promise that resolves with the response.
|
|
39
|
+
*/
|
|
40
|
+
request<T>(params: JsonRpcRequest): Promise<T>;
|
|
41
|
+
}
|
|
42
|
+
export type WindowListener = (event: MessageEvent<unknown>) => void;
|
|
43
|
+
export interface ProviderWcMetadata {
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
url: string;
|
|
47
|
+
icons: string[];
|
|
48
|
+
verifyUrl?: string;
|
|
49
|
+
redirect?: {
|
|
50
|
+
native?: string;
|
|
51
|
+
universal?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface EIP6963ProviderInfo {
|
|
55
|
+
uuid: string;
|
|
56
|
+
name: string;
|
|
57
|
+
icon: string;
|
|
58
|
+
rdns: string;
|
|
59
|
+
}
|
package/src/types.js
ADDED
package/src/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../libs/iframe-transport/src/types.ts"],"names":[],"mappings":""}
|
package/.babelrc
DELETED
package/jest.config.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
displayName: 'iframe-transport',
|
|
3
|
-
preset: '../../jest.preset.js',
|
|
4
|
-
transform: {
|
|
5
|
-
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
|
|
6
|
-
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
|
|
7
|
-
},
|
|
8
|
-
setupFilesAfterEnv: ['../../jest.setup.ts'],
|
|
9
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
10
|
-
coverageDirectory: '../../coverage/libs/iframe-transport',
|
|
11
|
-
}
|
package/project.json
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "iframe-transport",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "libs/iframe-transport/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"tags": [],
|
|
7
|
-
"targets": {
|
|
8
|
-
"lint": {
|
|
9
|
-
"executor": "@nx/eslint:lint",
|
|
10
|
-
"outputs": ["{options.outputFile}"],
|
|
11
|
-
"options": {
|
|
12
|
-
"lintFilePatterns": ["libs/iframe-transport/**/*.{ts,tsx,js,jsx}"]
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
"build": {
|
|
16
|
-
"executor": "@nx/js:tsc",
|
|
17
|
-
"outputs": ["{options.outputPath}"],
|
|
18
|
-
"options": {
|
|
19
|
-
"outputPath": "dist/libs/iframe-transport",
|
|
20
|
-
"main": "libs/iframe-transport/src/index.ts",
|
|
21
|
-
"tsConfig": "libs/iframe-transport/tsconfig.lib.json",
|
|
22
|
-
"assets": ["libs/iframe-transport/*.md"],
|
|
23
|
-
"updateBuildableProjectDepsInPackageJson": true,
|
|
24
|
-
"buildableProjectDepsInPackageJsonType": "dependencies"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"test": {
|
|
28
|
-
"executor": "@nx/jest:jest",
|
|
29
|
-
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
30
|
-
"options": {
|
|
31
|
-
"jestConfig": "libs/iframe-transport/jest.config.ts",
|
|
32
|
-
"passWithNoTests": true
|
|
33
|
-
},
|
|
34
|
-
"configurations": {
|
|
35
|
-
"ci": {
|
|
36
|
-
"ci": true,
|
|
37
|
-
"codeCoverage": true
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
package/src/IframeTransport.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Yes, this is a hack. For some reason I can't specify other types as a key
|
|
2
|
-
import { WindowListener } from './types'
|
|
3
|
-
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
type AbstractRecord = Record<unknown, unknown>
|
|
6
|
-
|
|
7
|
-
export class IframeTransport<MethodsEmitPayloadMap extends AbstractRecord> {
|
|
8
|
-
constructor(public readonly key: string) {}
|
|
9
|
-
|
|
10
|
-
postMessageToWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
11
|
-
contentWindow: Window,
|
|
12
|
-
method: T,
|
|
13
|
-
payload: MethodsEmitPayloadMap[T],
|
|
14
|
-
): void {
|
|
15
|
-
const data = typeof payload === 'object' ? payload : {}
|
|
16
|
-
const postPayload = {
|
|
17
|
-
key: this.key,
|
|
18
|
-
method,
|
|
19
|
-
...data,
|
|
20
|
-
}
|
|
21
|
-
contentWindow.postMessage(
|
|
22
|
-
postPayload,
|
|
23
|
-
'*', // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
listenToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
28
|
-
contentWindow: Window,
|
|
29
|
-
method: T,
|
|
30
|
-
callback: (payload: MethodsEmitPayloadMap[T]) => void,
|
|
31
|
-
): (payload: MessageEvent<unknown>) => void {
|
|
32
|
-
const listener = (event: MessageEvent<unknown>): void => {
|
|
33
|
-
if (!isEventData(event.data) || event.data.key !== this.key || event.data.method !== method) {
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
callback(event.data as MethodsEmitPayloadMap[T])
|
|
38
|
-
}
|
|
39
|
-
contentWindow.addEventListener('message', listener)
|
|
40
|
-
|
|
41
|
-
return listener
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
stopListeningToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
45
|
-
contentWindow: Window,
|
|
46
|
-
_method: T,
|
|
47
|
-
callback: (payload: MethodsEmitPayloadMap[T]) => void,
|
|
48
|
-
): void {
|
|
49
|
-
// TODO: Replace any with proper type definitions
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
-
contentWindow.removeEventListener('message', callback as any)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
stopListeningWindowListener(contentWindow: Window, callback: WindowListener): void {
|
|
55
|
-
contentWindow.removeEventListener('message', callback)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
interface EventData {
|
|
60
|
-
key: string
|
|
61
|
-
method: string
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function isEventData(obj: unknown): obj is EventData {
|
|
65
|
-
return (
|
|
66
|
-
typeof obj === 'object' &&
|
|
67
|
-
obj !== null &&
|
|
68
|
-
'key' in obj &&
|
|
69
|
-
'method' in obj &&
|
|
70
|
-
typeof obj.key === 'string' &&
|
|
71
|
-
typeof obj.method === 'string'
|
|
72
|
-
)
|
|
73
|
-
}
|