@cowprotocol/iframe-transport 2.0.12 → 2.1.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/IframeTransport.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ type AbstractRecord = Record<unknown, unknown>;
|
|
|
3
3
|
export declare class IframeTransport<MethodsEmitPayloadMap extends AbstractRecord> {
|
|
4
4
|
readonly key: string;
|
|
5
5
|
constructor(key: string);
|
|
6
|
-
postMessageToWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, method: T, payload: MethodsEmitPayloadMap[T]): void;
|
|
7
|
-
listenToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, method: T, callback: (payload: MethodsEmitPayloadMap[T]) => void): (payload: MessageEvent<unknown>) => void;
|
|
6
|
+
postMessageToWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, method: T, payload: MethodsEmitPayloadMap[T], targetOrigin?: string): void;
|
|
7
|
+
listenToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, method: T, callback: (payload: MethodsEmitPayloadMap[T]) => void, trustedOrigin?: string): (payload: MessageEvent<unknown>) => void;
|
|
8
8
|
stopListeningToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, _method: T, callback: (payload: MethodsEmitPayloadMap[T]) => void): void;
|
|
9
9
|
stopListeningWindowListener(contentWindow: Window, callback: WindowListener): void;
|
|
10
10
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getParentOrigin(): string | undefined;
|
|
@@ -7,6 +7,7 @@ import { EthereumProvider, JsonRpcRequestMessage } from '../types';
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class IframeRpcProviderBridge {
|
|
9
9
|
private iframeWidow;
|
|
10
|
+
private iframeOrigin?;
|
|
10
11
|
/**
|
|
11
12
|
* The Ethereum provider instance.
|
|
12
13
|
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
@@ -19,7 +20,7 @@ export declare class IframeRpcProviderBridge {
|
|
|
19
20
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
20
21
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
21
22
|
*/
|
|
22
|
-
constructor(iframeWidow: Window);
|
|
23
|
+
constructor(iframeWidow: Window, iframeOrigin?: string | undefined);
|
|
23
24
|
/**
|
|
24
25
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
25
26
|
*/
|
|
@@ -65,10 +65,11 @@ export declare class WidgetEthereumProvider extends EventEmitter<IFrameEthereumP
|
|
|
65
65
|
private readonly timeoutMilliseconds;
|
|
66
66
|
private readonly eventSource;
|
|
67
67
|
private readonly eventTarget;
|
|
68
|
+
private readonly targetOrigin;
|
|
68
69
|
private readonly completers;
|
|
69
70
|
private providerMetaInfo;
|
|
70
71
|
private providerMetaInfoCallback?;
|
|
71
|
-
constructor({ timeoutMilliseconds, eventSource, eventTarget, }?: IFrameEthereumProviderOptions);
|
|
72
|
+
constructor({ targetOrigin, timeoutMilliseconds, eventSource, eventTarget, }?: IFrameEthereumProviderOptions);
|
|
72
73
|
/**
|
|
73
74
|
* Helper method that handles transport and request wrapping
|
|
74
75
|
* @param method method to execute
|
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("eventemitter3"),d="https://swap.cow.finance";function l(...i){console.debug("%c [COW][Widget]","font-weight: bold; color: #ff0000",...i)}class h{constructor(e){this.key=e}postMessageToWindow(e,t,r,c=d){const n=typeof r=="object"?r:{},s={key:this.key,method:t,...n};e.postMessage(s,c)}listenToMessageFromWindow(e,t,r,c=d){const n=s=>{if(!(!E(s.data)||s.data.key!==this.key||s.data.method!==t)){if(s.origin!==c){l("Rejected message due to origin mismatch",{key:this.key,method:t,actualOrigin:s.origin,trustedOrigin:c});return}r(s.data)}};return e.addEventListener("message",n),n}stopListeningToMessageFromWindow(e,t,r){e.removeEventListener("message",r)}stopListeningWindowListener(e,t){e.removeEventListener("message",t)}}function E(i){return typeof i=="object"&&i!==null&&"key"in i&&"method"in i&&typeof i.key=="string"&&typeof i.method=="string"}var o=(i=>(i.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",i.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",i.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",i.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",i.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",i))(o||{});const a=new h("cowSwapIframeRpcProviderTransport");function R(i){if(i.isWalletConnect)return i?.session?.peer?.metadata}function p(i,e){return e.find(t=>t.provider===i)?.info}const w=["connect","disconnect","close","chainChanged","accountsChanged"],u=[];typeof window<"u"&&window.addEventListener("eip6963:announceProvider",i=>{const e=i;u.push(e.detail)});class P{constructor(e,t){this.iframeWidow=e,this.iframeOrigin=t}ethereumProvider=null;requestWaitingForConnection={};disconnect(){typeof window>"u"||(this.ethereumProvider=null,a.stopListeningToMessageFromWindow(window,o.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),a.stopListeningToMessageFromWindow(window,o.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}onConnect(e){typeof window>"u"||(this.ethereumProvider?this.disconnect():a.listenToMessageFromWindow(window,o.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow,this.iframeOrigin),this.ethereumProvider=e,this.processPendingRequests(),w.forEach(t=>{e.on(t,r=>this.onProviderEvent(t,r))}),a.listenToMessageFromWindow(window,o.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest,this.iframeOrigin))}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(e=>{this.processRpcRequest(this.requestWaitingForConnection[e])}),this.requestWaitingForConnection={}}processRpcRequest(e){const{id:t,jsonrpc:r,method:c}=e;if(!this.ethereumProvider||!t)return;(c==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:t}):this.ethereumProvider.request({...e,id:t})).then(s=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,result:s}})).catch(s=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,error:s}}))}processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const e=p(this.ethereumProvider,u),t=R(this.ethereumProvider);a.postMessageToWindow(this.iframeWidow,o.SEND_PROVIDER_META_INFO,{providerEip6963Info:e,providerWcMetadata:t},this.iframeOrigin)};onProviderEvent(e,t){a.postMessageToWindow(this.iframeWidow,o.PROVIDER_ON_EVENT,{event:e,params:t},this.iframeOrigin)}forwardRpcResponseToIframe(e){a.postMessageToWindow(this.iframeWidow,o.PROVIDER_RPC_RESPONSE,e,this.iframeOrigin)}}function m(){return O()||v()||_()}function O(){if(typeof window>"u")return;const i=window.location.ancestorOrigins;if(!(!i||i.length===0))return i[0]}function v(){if(!(typeof document>"u"||!document.referrer))try{return new URL(document.referrer).origin}catch{return}}function _(){if(!(typeof window>"u"||!window.parent||window.parent===window))try{return window.parent.location.origin}catch{return}}const I=6e5,T="2.0",M="https://swap.cow.finance";class C extends Error{isRpcError=!0;code;reason;constructor(e,t){super(`${e}: ${t}`),this.code=e,this.reason=t}}class y extends f{request({method:e,params:t}){return this.send(e,t)}isConnected(){return!0}get isIFrame(){return!0}get currentProvider(){return this}enabled=null;timeoutMilliseconds;eventSource;eventTarget;targetOrigin;completers={};providerMetaInfo=null;providerMetaInfoCallback;constructor({targetOrigin:e,timeoutMilliseconds:t=I,eventSource:r=window,eventTarget:c=window.parent}={}){super(),this.timeoutMilliseconds=t,this.eventSource=r,this.eventTarget=c,this.targetOrigin=e||m()||M,a.listenToMessageFromWindow(this.eventSource,o.PROVIDER_RPC_RESPONSE,n=>{this.handleRpcRequests(n)},this.targetOrigin),a.listenToMessageFromWindow(this.eventSource,o.PROVIDER_ON_EVENT,n=>{this.handleOnEvent(n)},this.targetOrigin),a.listenToMessageFromWindow(this.eventSource,o.SEND_PROVIDER_META_INFO,n=>{this.providerMetaInfo=n,this.providerMetaInfoCallback&&this.providerMetaInfoCallback(this.providerMetaInfo)},this.targetOrigin)}async execute(e,t){const r=W(),c={jsonrpc:T,id:r,method:e,params:t},n=new Promise((s,g)=>this.completers[r]={resolve:s,reject:g});return a.postMessageToWindow(this.eventTarget,o.PROVIDER_RPC_REQUEST,{rpcRequest:c},this.targetOrigin),setTimeout(()=>{this.completers[r]&&(this.completers[r].reject(new Error("Request timed out. Please try again and submit it faster.")),delete this.completers[r])},this.timeoutMilliseconds),n}async send(e,t){const{method:r,params:c}=typeof e=="string"?{method:e,params:t}:{method:e.method,params:e.params},n=await this.execute(r,c);if("error"in n)throw new C(n.error.code,n.error.message);return n.result}async enable(){if(this.enabled===null){const e=this.enabled=this.send("eth_requestAccounts",[]).catch(t=>{throw this.enabled===e&&(this.enabled=null),t})}return this.enabled}async sendAsync(e,t){try{const r=await this.execute(e.method,e.params);t(null,r)}catch(r){t(N(r),null)}}onProviderMetaInfo(e){this.providerMetaInfo?e(this.providerMetaInfo):(this.providerMetaInfoCallback=e,a.postMessageToWindow(this.eventTarget,o.REQUEST_PROVIDER_META_INFO,null,this.targetOrigin))}handleRpcRequests=({rpcResponse:e})=>{if(e.id===void 0||e.id===null)return;const t=this.completers[""+e.id];t&&("error"in e||"result"in e?t.resolve(e):t.reject(new Error("Response from provider did not have error or result key")),delete this.completers[e.id])};handleOnEvent(e){const t=e.params;switch(e.event){case"notification":this.emitNotification(t);break;case"connect":this.emitConnect(t);break;case"disconnect":this.emitDisconnect(t);break;case"chainChanged":this.emitChainChanged(t);break;case"accountsChanged":this.emitAccountsChanged(t);break;case"message":this.emitMessage(t);break;case"networkChanged":this.emitNetworkChanged(t);break;case"close":this.emitClose(t);break;default:console.warn(`[WidgetEthereumProvider] Unknown event type: ${e.event}`);break}}emitNotification(e){this.emit("notification",e)}emitConnect(e){this.enabled===null&&(this.enabled=Promise.resolve([])),this.emit("connect",e)}emitClose(e){Array.isArray(e)&&e.length===2?this.emit("close",e[0],e[1]):this.emit("close")}emitDisconnect(e){this.emit("disconnect",e)}emitChainChanged(e){this.emit("chainChanged",e)}emitNetworkChanged(e){this.emit("networkChanged",e)}emitAccountsChanged(e){this.enabled=Promise.resolve(e),this.emit("accountsChanged",e)}emitMessage(e){this.emit("message",e)}}function W(){return Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)}function N(i){if(!i)return null;if(typeof i=="string")return i;if(i instanceof Error||i.message)return i.message;try{return JSON.stringify(i)}catch{const e="Unknown WidgetEthereumProvider error";return console.error(e,i),e}}exports.IframeRpcProviderBridge=P;exports.IframeRpcProviderEvents=o;exports.IframeTransport=h;exports.WidgetEthereumProvider=y;exports.getParentOrigin=m;exports.iframeRpcProviderTransport=a;
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { IframeRpcProviderBridge } from './iframeRpcProvider/IframeRpcProviderBridge';
|
|
2
2
|
export { WidgetEthereumProvider } from './iframeRpcProvider/WidgetEthereumProvider';
|
|
3
3
|
export { IframeTransport } from './IframeTransport';
|
|
4
|
+
export { getParentOrigin } from './getParentOrigin';
|
|
4
5
|
export * from './iframeRpcProvider/iframeRpcProviderEvents';
|
|
5
6
|
export * from './types';
|
package/index.js
CHANGED
|
@@ -1,58 +1,69 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import m from "eventemitter3";
|
|
2
|
+
const d = "https://swap.cow.finance";
|
|
3
|
+
function g(...i) {
|
|
4
|
+
console.debug("%c [COW][Widget]", "font-weight: bold; color: #ff0000", ...i);
|
|
5
|
+
}
|
|
6
|
+
class f {
|
|
3
7
|
constructor(e) {
|
|
4
8
|
this.key = e;
|
|
5
9
|
}
|
|
6
|
-
postMessageToWindow(e, t,
|
|
7
|
-
const
|
|
10
|
+
postMessageToWindow(e, t, n, a = d) {
|
|
11
|
+
const r = typeof n == "object" ? n : {}, s = {
|
|
8
12
|
key: this.key,
|
|
9
13
|
method: t,
|
|
10
|
-
...
|
|
14
|
+
...r
|
|
11
15
|
};
|
|
12
|
-
e.postMessage(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
e.postMessage(s, a);
|
|
17
|
+
}
|
|
18
|
+
listenToMessageFromWindow(e, t, n, a = d) {
|
|
19
|
+
const r = (s) => {
|
|
20
|
+
if (!(!l(s.data) || s.data.key !== this.key || s.data.method !== t)) {
|
|
21
|
+
if (s.origin !== a) {
|
|
22
|
+
g("Rejected message due to origin mismatch", {
|
|
23
|
+
key: this.key,
|
|
24
|
+
method: t,
|
|
25
|
+
actualOrigin: s.origin,
|
|
26
|
+
trustedOrigin: a
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
n(s.data);
|
|
31
|
+
}
|
|
21
32
|
};
|
|
22
|
-
return e.addEventListener("message",
|
|
33
|
+
return e.addEventListener("message", r), r;
|
|
23
34
|
}
|
|
24
|
-
stopListeningToMessageFromWindow(e, t,
|
|
25
|
-
e.removeEventListener("message",
|
|
35
|
+
stopListeningToMessageFromWindow(e, t, n) {
|
|
36
|
+
e.removeEventListener("message", n);
|
|
26
37
|
}
|
|
27
38
|
stopListeningWindowListener(e, t) {
|
|
28
39
|
e.removeEventListener("message", t);
|
|
29
40
|
}
|
|
30
41
|
}
|
|
31
|
-
function l(
|
|
32
|
-
return typeof
|
|
42
|
+
function l(i) {
|
|
43
|
+
return typeof i == "object" && i !== null && "key" in i && "method" in i && typeof i.key == "string" && typeof i.method == "string";
|
|
33
44
|
}
|
|
34
|
-
var
|
|
35
|
-
const
|
|
36
|
-
function E(
|
|
37
|
-
if (
|
|
38
|
-
return
|
|
45
|
+
var o = /* @__PURE__ */ ((i) => (i.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", i.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", i.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", i.SEND_PROVIDER_META_INFO = "SEND_PROVIDER_META_INFO", i.REQUEST_PROVIDER_META_INFO = "REQUEST_PROVIDER_META_INFO", i))(o || {});
|
|
46
|
+
const c = new f("cowSwapIframeRpcProviderTransport");
|
|
47
|
+
function E(i) {
|
|
48
|
+
if (i.isWalletConnect)
|
|
49
|
+
return i?.session?.peer?.metadata;
|
|
39
50
|
}
|
|
40
|
-
function R(
|
|
41
|
-
return e.find((t) => t.provider ===
|
|
51
|
+
function R(i, e) {
|
|
52
|
+
return e.find((t) => t.provider === i)?.info;
|
|
42
53
|
}
|
|
43
|
-
const
|
|
44
|
-
typeof window < "u" && window.addEventListener("eip6963:announceProvider", (
|
|
45
|
-
const e =
|
|
46
|
-
|
|
54
|
+
const p = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"], h = [];
|
|
55
|
+
typeof window < "u" && window.addEventListener("eip6963:announceProvider", (i) => {
|
|
56
|
+
const e = i;
|
|
57
|
+
h.push(e.detail);
|
|
47
58
|
});
|
|
48
|
-
class
|
|
59
|
+
class y {
|
|
49
60
|
/**
|
|
50
61
|
* Creates an instance of IframeRpcProviderBridge.
|
|
51
62
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
52
63
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
53
64
|
*/
|
|
54
|
-
constructor(e) {
|
|
55
|
-
this.iframeWidow = e;
|
|
65
|
+
constructor(e, t) {
|
|
66
|
+
this.iframeWidow = e, this.iframeOrigin = t;
|
|
56
67
|
}
|
|
57
68
|
/**
|
|
58
69
|
* The Ethereum provider instance.
|
|
@@ -65,16 +76,16 @@ class I {
|
|
|
65
76
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
66
77
|
*/
|
|
67
78
|
disconnect() {
|
|
68
|
-
typeof window > "u" || (this.ethereumProvider = null,
|
|
79
|
+
typeof window > "u" || (this.ethereumProvider = null, c.stopListeningToMessageFromWindow(window, o.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), c.stopListeningToMessageFromWindow(window, o.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest));
|
|
69
80
|
}
|
|
70
81
|
/**
|
|
71
82
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
72
83
|
* @param newProvider - The Ethereum provider to connect.
|
|
73
84
|
*/
|
|
74
85
|
onConnect(e) {
|
|
75
|
-
typeof window > "u" || (this.ethereumProvider ? this.disconnect() :
|
|
76
|
-
e.on(t, (
|
|
77
|
-
}),
|
|
86
|
+
typeof window > "u" || (this.ethereumProvider ? this.disconnect() : c.listenToMessageFromWindow(window, o.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow, this.iframeOrigin), this.ethereumProvider = e, this.processPendingRequests(), p.forEach((t) => {
|
|
87
|
+
e.on(t, (n) => this.onProviderEvent(t, n));
|
|
88
|
+
}), c.listenToMessageFromWindow(window, o.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest, this.iframeOrigin));
|
|
78
89
|
}
|
|
79
90
|
// TODO: Add proper return type annotation
|
|
80
91
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -92,29 +103,29 @@ class I {
|
|
|
92
103
|
processRpcRequest(e) {
|
|
93
104
|
const {
|
|
94
105
|
id: t,
|
|
95
|
-
jsonrpc:
|
|
96
|
-
method:
|
|
106
|
+
jsonrpc: n,
|
|
107
|
+
method: a
|
|
97
108
|
} = e;
|
|
98
109
|
if (!this.ethereumProvider || !t)
|
|
99
110
|
return;
|
|
100
111
|
// Keep the legacy "enable" method for backward compatibility
|
|
101
|
-
(
|
|
112
|
+
(a === "enable" ? this.ethereumProvider.request({
|
|
102
113
|
method: "eth_requestAccounts",
|
|
103
114
|
id: t
|
|
104
115
|
}) : this.ethereumProvider.request({
|
|
105
116
|
...e,
|
|
106
117
|
id: t
|
|
107
|
-
})).then((
|
|
118
|
+
})).then((s) => this.forwardRpcResponseToIframe({
|
|
108
119
|
rpcResponse: {
|
|
109
|
-
jsonrpc:
|
|
120
|
+
jsonrpc: n,
|
|
110
121
|
id: t,
|
|
111
|
-
result:
|
|
122
|
+
result: s
|
|
112
123
|
}
|
|
113
|
-
})).catch((
|
|
124
|
+
})).catch((s) => this.forwardRpcResponseToIframe({
|
|
114
125
|
rpcResponse: {
|
|
115
|
-
jsonrpc:
|
|
126
|
+
jsonrpc: n,
|
|
116
127
|
id: t,
|
|
117
|
-
error:
|
|
128
|
+
error: s
|
|
118
129
|
}
|
|
119
130
|
}));
|
|
120
131
|
}
|
|
@@ -129,17 +140,17 @@ class I {
|
|
|
129
140
|
};
|
|
130
141
|
processProviderMetaInfoRequest = () => {
|
|
131
142
|
if (!this.ethereumProvider) return;
|
|
132
|
-
const e = R(this.ethereumProvider,
|
|
133
|
-
|
|
143
|
+
const e = R(this.ethereumProvider, h), t = E(this.ethereumProvider);
|
|
144
|
+
c.postMessageToWindow(this.iframeWidow, o.SEND_PROVIDER_META_INFO, {
|
|
134
145
|
providerEip6963Info: e,
|
|
135
146
|
providerWcMetadata: t
|
|
136
|
-
});
|
|
147
|
+
}, this.iframeOrigin);
|
|
137
148
|
};
|
|
138
149
|
onProviderEvent(e, t) {
|
|
139
|
-
|
|
150
|
+
c.postMessageToWindow(this.iframeWidow, o.PROVIDER_ON_EVENT, {
|
|
140
151
|
event: e,
|
|
141
152
|
params: t
|
|
142
|
-
});
|
|
153
|
+
}, this.iframeOrigin);
|
|
143
154
|
}
|
|
144
155
|
/**
|
|
145
156
|
* Forward a JSON-RPC message to the content window.
|
|
@@ -147,11 +158,37 @@ class I {
|
|
|
147
158
|
// TODO: Add proper return type annotation
|
|
148
159
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
149
160
|
forwardRpcResponseToIframe(e) {
|
|
150
|
-
|
|
161
|
+
c.postMessageToWindow(this.iframeWidow, o.PROVIDER_RPC_RESPONSE, e, this.iframeOrigin);
|
|
151
162
|
}
|
|
152
163
|
}
|
|
153
|
-
|
|
154
|
-
|
|
164
|
+
function w() {
|
|
165
|
+
return P() || O() || _();
|
|
166
|
+
}
|
|
167
|
+
function P() {
|
|
168
|
+
if (typeof window > "u")
|
|
169
|
+
return;
|
|
170
|
+
const i = window.location.ancestorOrigins;
|
|
171
|
+
if (!(!i || i.length === 0))
|
|
172
|
+
return i[0];
|
|
173
|
+
}
|
|
174
|
+
function O() {
|
|
175
|
+
if (!(typeof document > "u" || !document.referrer))
|
|
176
|
+
try {
|
|
177
|
+
return new URL(document.referrer).origin;
|
|
178
|
+
} catch {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function _() {
|
|
183
|
+
if (!(typeof window > "u" || !window.parent || window.parent === window))
|
|
184
|
+
try {
|
|
185
|
+
return window.parent.location.origin;
|
|
186
|
+
} catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const v = 6e5, I = "2.0", T = "https://swap.cow.finance";
|
|
191
|
+
class M extends Error {
|
|
155
192
|
isRpcError = !0;
|
|
156
193
|
code;
|
|
157
194
|
reason;
|
|
@@ -159,7 +196,7 @@ class P extends Error {
|
|
|
159
196
|
super(`${e}: ${t}`), this.code = e, this.reason = t;
|
|
160
197
|
}
|
|
161
198
|
}
|
|
162
|
-
class
|
|
199
|
+
class S extends m {
|
|
163
200
|
// TODO: Add proper return type annotation
|
|
164
201
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
165
202
|
request({
|
|
@@ -187,21 +224,23 @@ class T extends u {
|
|
|
187
224
|
timeoutMilliseconds;
|
|
188
225
|
eventSource;
|
|
189
226
|
eventTarget;
|
|
227
|
+
targetOrigin;
|
|
190
228
|
completers = {};
|
|
191
229
|
providerMetaInfo = null;
|
|
192
230
|
providerMetaInfoCallback;
|
|
193
231
|
constructor({
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
232
|
+
targetOrigin: e,
|
|
233
|
+
timeoutMilliseconds: t = v,
|
|
234
|
+
eventSource: n = window,
|
|
235
|
+
eventTarget: a = window.parent
|
|
197
236
|
} = {}) {
|
|
198
|
-
super(), this.timeoutMilliseconds =
|
|
199
|
-
this.handleRpcRequests(
|
|
200
|
-
}),
|
|
201
|
-
this.handleOnEvent(
|
|
202
|
-
}),
|
|
203
|
-
this.providerMetaInfo =
|
|
204
|
-
});
|
|
237
|
+
super(), this.timeoutMilliseconds = t, this.eventSource = n, this.eventTarget = a, this.targetOrigin = e || w() || T, c.listenToMessageFromWindow(this.eventSource, o.PROVIDER_RPC_RESPONSE, (r) => {
|
|
238
|
+
this.handleRpcRequests(r);
|
|
239
|
+
}, this.targetOrigin), c.listenToMessageFromWindow(this.eventSource, o.PROVIDER_ON_EVENT, (r) => {
|
|
240
|
+
this.handleOnEvent(r);
|
|
241
|
+
}, this.targetOrigin), c.listenToMessageFromWindow(this.eventSource, o.SEND_PROVIDER_META_INFO, (r) => {
|
|
242
|
+
this.providerMetaInfo = r, this.providerMetaInfoCallback && this.providerMetaInfoCallback(this.providerMetaInfo);
|
|
243
|
+
}, this.targetOrigin);
|
|
205
244
|
}
|
|
206
245
|
/**
|
|
207
246
|
* Helper method that handles transport and request wrapping
|
|
@@ -209,37 +248,37 @@ class T extends u {
|
|
|
209
248
|
* @param params params to pass the method
|
|
210
249
|
*/
|
|
211
250
|
async execute(e, t) {
|
|
212
|
-
const
|
|
213
|
-
jsonrpc:
|
|
214
|
-
id:
|
|
251
|
+
const n = C(), a = {
|
|
252
|
+
jsonrpc: I,
|
|
253
|
+
id: n,
|
|
215
254
|
method: e,
|
|
216
255
|
params: t
|
|
217
|
-
},
|
|
218
|
-
resolve:
|
|
219
|
-
reject:
|
|
256
|
+
}, r = new Promise((s, u) => this.completers[n] = {
|
|
257
|
+
resolve: s,
|
|
258
|
+
reject: u
|
|
220
259
|
});
|
|
221
|
-
return
|
|
222
|
-
rpcRequest:
|
|
223
|
-
}), setTimeout(() => {
|
|
224
|
-
this.completers[
|
|
225
|
-
}, this.timeoutMilliseconds),
|
|
260
|
+
return c.postMessageToWindow(this.eventTarget, o.PROVIDER_RPC_REQUEST, {
|
|
261
|
+
rpcRequest: a
|
|
262
|
+
}, this.targetOrigin), setTimeout(() => {
|
|
263
|
+
this.completers[n] && (this.completers[n].reject(new Error("Request timed out. Please try again and submit it faster.")), delete this.completers[n]);
|
|
264
|
+
}, this.timeoutMilliseconds), r;
|
|
226
265
|
}
|
|
227
266
|
// TODO: Replace any with proper type definitions
|
|
228
267
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
229
268
|
async send(e, t) {
|
|
230
269
|
const {
|
|
231
|
-
method:
|
|
232
|
-
params:
|
|
270
|
+
method: n,
|
|
271
|
+
params: a
|
|
233
272
|
} = typeof e == "string" ? {
|
|
234
273
|
method: e,
|
|
235
274
|
params: t
|
|
236
275
|
} : {
|
|
237
276
|
method: e.method,
|
|
238
277
|
params: e.params
|
|
239
|
-
},
|
|
240
|
-
if ("error" in
|
|
241
|
-
throw new
|
|
242
|
-
return
|
|
278
|
+
}, r = await this.execute(n, a);
|
|
279
|
+
if ("error" in r)
|
|
280
|
+
throw new M(r.error.code, r.error.message);
|
|
281
|
+
return r.result;
|
|
243
282
|
}
|
|
244
283
|
/**
|
|
245
284
|
* Request the parent window to enable access to the user's web3 provider. Return accounts list immediately if already enabled.
|
|
@@ -260,10 +299,10 @@ class T extends u {
|
|
|
260
299
|
*/
|
|
261
300
|
async sendAsync(e, t) {
|
|
262
301
|
try {
|
|
263
|
-
const
|
|
264
|
-
t(null,
|
|
265
|
-
} catch (
|
|
266
|
-
t(
|
|
302
|
+
const n = await this.execute(e.method, e.params);
|
|
303
|
+
t(null, n);
|
|
304
|
+
} catch (n) {
|
|
305
|
+
t(N(n), null);
|
|
267
306
|
}
|
|
268
307
|
}
|
|
269
308
|
/**
|
|
@@ -273,7 +312,7 @@ class T extends u {
|
|
|
273
312
|
// TODO: Add proper return type annotation
|
|
274
313
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
275
314
|
onProviderMetaInfo(e) {
|
|
276
|
-
this.providerMetaInfo ? e(this.providerMetaInfo) : (this.providerMetaInfoCallback = e,
|
|
315
|
+
this.providerMetaInfo ? e(this.providerMetaInfo) : (this.providerMetaInfoCallback = e, c.postMessageToWindow(this.eventTarget, o.REQUEST_PROVIDER_META_INFO, null, this.targetOrigin));
|
|
277
316
|
}
|
|
278
317
|
/**
|
|
279
318
|
* Handle a Rpc Request
|
|
@@ -372,24 +411,25 @@ class T extends u {
|
|
|
372
411
|
this.emit("message", e);
|
|
373
412
|
}
|
|
374
413
|
}
|
|
375
|
-
function
|
|
414
|
+
function C() {
|
|
376
415
|
return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
377
416
|
}
|
|
378
|
-
function
|
|
379
|
-
if (!
|
|
380
|
-
if (typeof
|
|
381
|
-
if (
|
|
417
|
+
function N(i) {
|
|
418
|
+
if (!i) return null;
|
|
419
|
+
if (typeof i == "string") return i;
|
|
420
|
+
if (i instanceof Error || i.message) return i.message;
|
|
382
421
|
try {
|
|
383
|
-
return JSON.stringify(
|
|
422
|
+
return JSON.stringify(i);
|
|
384
423
|
} catch {
|
|
385
424
|
const e = "Unknown WidgetEthereumProvider error";
|
|
386
|
-
return console.error(e,
|
|
425
|
+
return console.error(e, i), e;
|
|
387
426
|
}
|
|
388
427
|
}
|
|
389
428
|
export {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
429
|
+
y as IframeRpcProviderBridge,
|
|
430
|
+
o as IframeRpcProviderEvents,
|
|
431
|
+
f as IframeTransport,
|
|
432
|
+
S as WidgetEthereumProvider,
|
|
433
|
+
w as getParentOrigin,
|
|
434
|
+
c as iframeRpcProviderTransport
|
|
395
435
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/iframe-transport",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "CoW Swap iframe-transport",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@cowprotocol/types": "^4.
|
|
18
|
+
"@cowprotocol/types": "^4.2.0",
|
|
19
19
|
"eventemitter3": "^4.0.0"
|
|
20
20
|
},
|
|
21
21
|
"exports": {
|