@cowprotocol/iframe-transport 2.1.0 → 2.2.1
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 +1 -1
- package/iframeRpcProvider/IframeRpcProviderBridge.d.ts +3 -3
- package/index.cjs +1 -1
- package/index.js +111 -72
- package/package.json +2 -2
package/IframeTransport.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare class IframeTransport<MethodsEmitPayloadMap extends AbstractRecor
|
|
|
4
4
|
readonly key: string;
|
|
5
5
|
constructor(key: string);
|
|
6
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;
|
|
7
|
+
listenToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(contentWindow: Window, targetWindow: 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
|
}
|
|
@@ -6,7 +6,7 @@ import { EthereumProvider, JsonRpcRequestMessage } from '../types';
|
|
|
6
6
|
* Additionally, it will forward some special events from the wallet, to the iFrame window, for example connect/disconnect/chainChanged
|
|
7
7
|
*/
|
|
8
8
|
export declare class IframeRpcProviderBridge {
|
|
9
|
-
private
|
|
9
|
+
private iframeWindow;
|
|
10
10
|
private iframeOrigin?;
|
|
11
11
|
/**
|
|
12
12
|
* The Ethereum provider instance.
|
|
@@ -17,10 +17,10 @@ export declare class IframeRpcProviderBridge {
|
|
|
17
17
|
private requestWaitingForConnection;
|
|
18
18
|
/**
|
|
19
19
|
* Creates an instance of IframeRpcProviderBridge.
|
|
20
|
-
* @param
|
|
20
|
+
* @param iframeWindow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
21
21
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
22
22
|
*/
|
|
23
|
-
constructor(
|
|
23
|
+
constructor(iframeWindow: Window, iframeOrigin?: string | undefined);
|
|
24
24
|
/**
|
|
25
25
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
26
26
|
*/
|
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 R=require("eventemitter3"),g="https://swap.cow.fi";function h(...i){process.env.NODE_ENV!=="test"&&console.debug("%c [COW][Widget]","font-weight: bold; color: #ff0000",...i)}function m(i){try{const{hostname:e}=new URL(i);return e==="localhost"||e==="127.0.0.1"||e==="::1"||e==="[::1]"}catch{return!1}}class l{constructor(e){this.key=e}postMessageToWindow(e,t,r,c=g){const n=typeof r=="object"?r:{},d={key:this.key,method:t,...n};e.postMessage(d,c)}listenToMessageFromWindow(e,t,r,c,n=g){const d=o=>{if(!(!p(o.data)||o.data.key!==this.key||o.data.method!==r)){if(!o.source||o.source!==t){if(!(m(o.origin)||m(n))){h("Rejected message due to source mismatch",{key:this.key,method:r,actualSource:o.source,expectedSource:t});return}h("Non-matching or missing message source. Continuing due to local env.",{key:this.key,method:r,actualSource:o.source,expectedSource:t})}if(o.origin!==n){h("Rejected message due to origin mismatch",{key:this.key,method:r,actualOrigin:o.origin,trustedOrigin:n});return}c(o.data)}};return e.addEventListener("message",d),d}stopListeningToMessageFromWindow(e,t,r){e.removeEventListener("message",r)}stopListeningWindowListener(e,t){e.removeEventListener("message",t)}}function p(i){return typeof i=="object"&&i!==null&&"key"in i&&"method"in i&&typeof i.key=="string"&&typeof i.method=="string"}var s=(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))(s||{});const a=new l("cowSwapIframeRpcProviderTransport");function w(i){if(i.isWalletConnect)return i?.session?.peer?.metadata}function P(i,e){return e.find(t=>t.provider===i)?.info}const v=["connect","disconnect","close","chainChanged","accountsChanged"],f=[];typeof window<"u"&&window.addEventListener("eip6963:announceProvider",i=>{const e=i;f.push(e.detail)});class O{constructor(e,t){this.iframeWindow=e,this.iframeOrigin=t}ethereumProvider=null;requestWaitingForConnection={};disconnect(){typeof window>"u"||(this.ethereumProvider=null,a.stopListeningToMessageFromWindow(window,s.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),a.stopListeningToMessageFromWindow(window,s.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}onConnect(e){typeof window>"u"||(this.ethereumProvider?this.disconnect():a.listenToMessageFromWindow(window,this.iframeWindow,s.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow,this.iframeOrigin),this.ethereumProvider=e,this.processPendingRequests(),v.forEach(t=>{e.on(t,r=>this.onProviderEvent(t,r))}),a.listenToMessageFromWindow(window,this.iframeWindow,s.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(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,result:d}})).catch(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,error:d}}))}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,f),t=w(this.ethereumProvider);a.postMessageToWindow(this.iframeWindow,s.SEND_PROVIDER_META_INFO,{providerEip6963Info:e,providerWcMetadata:t},this.iframeOrigin)};onProviderEvent(e,t){a.postMessageToWindow(this.iframeWindow,s.PROVIDER_ON_EVENT,{event:e,params:t},this.iframeOrigin)}forwardRpcResponseToIframe(e){a.postMessageToWindow(this.iframeWindow,s.PROVIDER_RPC_RESPONSE,e,this.iframeOrigin)}}function E(){const i=u(_())||u(T())||u(I());if(i)try{return new URL(i).origin}catch(e){console.error("[getParentOrigin] origin is invalid",e,{origin:i});return}}function u(i){return i&&i!=="null"?i:void 0}function _(){if(typeof window>"u")return;const i=window.location.ancestorOrigins;if(!(!i||i.length===0))return i[0]}function T(){if(!(typeof document>"u"||!document.referrer))try{return new URL(document.referrer).origin}catch{return}}function I(){if(!(typeof window>"u"||!window.parent||window.parent===window))try{return window.parent.location.origin}catch{return}}const M=6e5,C="2.0",y="https://swap.cow.fi";class N extends Error{isRpcError=!0;code;reason;constructor(e,t){super(`${e}: ${t}`),this.code=e,this.reason=t}}class S extends R{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=M,eventSource:r=window,eventTarget:c=window.parent}={}){super(),this.timeoutMilliseconds=t,this.eventSource=r,this.eventTarget=c,this.targetOrigin=e||E()||y,a.listenToMessageFromWindow(this.eventSource,this.eventTarget,s.PROVIDER_RPC_RESPONSE,n=>{this.handleRpcRequests(n)},this.targetOrigin),a.listenToMessageFromWindow(this.eventSource,this.eventTarget,s.PROVIDER_ON_EVENT,n=>{this.handleOnEvent(n)},this.targetOrigin),a.listenToMessageFromWindow(this.eventSource,this.eventTarget,s.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:C,id:r,method:e,params:t},n=new Promise((d,o)=>this.completers[r]={resolve:d,reject:o});return a.postMessageToWindow(this.eventTarget,s.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 N(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(D(r),null)}}onProviderMetaInfo(e){this.providerMetaInfo?e(this.providerMetaInfo):(this.providerMetaInfoCallback=e,a.postMessageToWindow(this.eventTarget,s.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 D(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=O;exports.IframeRpcProviderEvents=s;exports.IframeTransport=l;exports.WidgetEthereumProvider=S;exports.getParentOrigin=E;exports.iframeRpcProviderTransport=a;
|
package/index.js
CHANGED
|
@@ -1,36 +1,63 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
3
|
-
function
|
|
4
|
-
console.debug("%c [COW][Widget]", "font-weight: bold; color: #ff0000", ...i);
|
|
1
|
+
import f from "eventemitter3";
|
|
2
|
+
const l = "https://swap.cow.fi";
|
|
3
|
+
function h(...i) {
|
|
4
|
+
process.env.NODE_ENV !== "test" && console.debug("%c [COW][Widget]", "font-weight: bold; color: #ff0000", ...i);
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
function m(i) {
|
|
7
|
+
try {
|
|
8
|
+
const {
|
|
9
|
+
hostname: e
|
|
10
|
+
} = new URL(i);
|
|
11
|
+
return e === "localhost" || e === "127.0.0.1" || e === "::1" || e === "[::1]";
|
|
12
|
+
} catch {
|
|
13
|
+
return !1;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class E {
|
|
7
17
|
constructor(e) {
|
|
8
18
|
this.key = e;
|
|
9
19
|
}
|
|
10
|
-
postMessageToWindow(e, t, n,
|
|
11
|
-
const r = typeof n == "object" ? n : {},
|
|
20
|
+
postMessageToWindow(e, t, n, c = l) {
|
|
21
|
+
const r = typeof n == "object" ? n : {}, d = {
|
|
12
22
|
key: this.key,
|
|
13
23
|
method: t,
|
|
14
24
|
...r
|
|
15
25
|
};
|
|
16
|
-
e.postMessage(
|
|
17
|
-
}
|
|
18
|
-
listenToMessageFromWindow(e, t, n,
|
|
19
|
-
const
|
|
20
|
-
if (!(!
|
|
21
|
-
if (s.
|
|
22
|
-
|
|
26
|
+
e.postMessage(d, c);
|
|
27
|
+
}
|
|
28
|
+
listenToMessageFromWindow(e, t, n, c, r = l) {
|
|
29
|
+
const d = (s) => {
|
|
30
|
+
if (!(!R(s.data) || s.data.key !== this.key || s.data.method !== n)) {
|
|
31
|
+
if (!s.source || s.source !== t) {
|
|
32
|
+
if (!(m(s.origin) || m(r))) {
|
|
33
|
+
h("Rejected message due to source mismatch", {
|
|
34
|
+
key: this.key,
|
|
35
|
+
method: n,
|
|
36
|
+
actualSource: s.source,
|
|
37
|
+
expectedSource: t
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
h("Non-matching or missing message source. Continuing due to local env.", {
|
|
42
|
+
key: this.key,
|
|
43
|
+
method: n,
|
|
44
|
+
actualSource: s.source,
|
|
45
|
+
expectedSource: t
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (s.origin !== r) {
|
|
49
|
+
h("Rejected message due to origin mismatch", {
|
|
23
50
|
key: this.key,
|
|
24
|
-
method:
|
|
51
|
+
method: n,
|
|
25
52
|
actualOrigin: s.origin,
|
|
26
|
-
trustedOrigin:
|
|
53
|
+
trustedOrigin: r
|
|
27
54
|
});
|
|
28
55
|
return;
|
|
29
56
|
}
|
|
30
|
-
|
|
57
|
+
c(s.data);
|
|
31
58
|
}
|
|
32
59
|
};
|
|
33
|
-
return e.addEventListener("message",
|
|
60
|
+
return e.addEventListener("message", d), d;
|
|
34
61
|
}
|
|
35
62
|
stopListeningToMessageFromWindow(e, t, n) {
|
|
36
63
|
e.removeEventListener("message", n);
|
|
@@ -39,31 +66,31 @@ class f {
|
|
|
39
66
|
e.removeEventListener("message", t);
|
|
40
67
|
}
|
|
41
68
|
}
|
|
42
|
-
function
|
|
69
|
+
function R(i) {
|
|
43
70
|
return typeof i == "object" && i !== null && "key" in i && "method" in i && typeof i.key == "string" && typeof i.method == "string";
|
|
44
71
|
}
|
|
45
72
|
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
|
|
47
|
-
function
|
|
73
|
+
const a = new E("cowSwapIframeRpcProviderTransport");
|
|
74
|
+
function p(i) {
|
|
48
75
|
if (i.isWalletConnect)
|
|
49
76
|
return i?.session?.peer?.metadata;
|
|
50
77
|
}
|
|
51
|
-
function
|
|
78
|
+
function w(i, e) {
|
|
52
79
|
return e.find((t) => t.provider === i)?.info;
|
|
53
80
|
}
|
|
54
|
-
const
|
|
81
|
+
const O = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"], g = [];
|
|
55
82
|
typeof window < "u" && window.addEventListener("eip6963:announceProvider", (i) => {
|
|
56
83
|
const e = i;
|
|
57
|
-
|
|
84
|
+
g.push(e.detail);
|
|
58
85
|
});
|
|
59
|
-
class
|
|
86
|
+
class F {
|
|
60
87
|
/**
|
|
61
88
|
* Creates an instance of IframeRpcProviderBridge.
|
|
62
|
-
* @param
|
|
89
|
+
* @param iframeWindow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
63
90
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
64
91
|
*/
|
|
65
92
|
constructor(e, t) {
|
|
66
|
-
this.
|
|
93
|
+
this.iframeWindow = e, this.iframeOrigin = t;
|
|
67
94
|
}
|
|
68
95
|
/**
|
|
69
96
|
* The Ethereum provider instance.
|
|
@@ -76,16 +103,16 @@ class y {
|
|
|
76
103
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
77
104
|
*/
|
|
78
105
|
disconnect() {
|
|
79
|
-
typeof window > "u" || (this.ethereumProvider = null,
|
|
106
|
+
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));
|
|
80
107
|
}
|
|
81
108
|
/**
|
|
82
109
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
83
110
|
* @param newProvider - The Ethereum provider to connect.
|
|
84
111
|
*/
|
|
85
112
|
onConnect(e) {
|
|
86
|
-
typeof window > "u" || (this.ethereumProvider ? this.disconnect() :
|
|
113
|
+
typeof window > "u" || (this.ethereumProvider ? this.disconnect() : a.listenToMessageFromWindow(window, this.iframeWindow, o.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow, this.iframeOrigin), this.ethereumProvider = e, this.processPendingRequests(), O.forEach((t) => {
|
|
87
114
|
e.on(t, (n) => this.onProviderEvent(t, n));
|
|
88
|
-
}),
|
|
115
|
+
}), a.listenToMessageFromWindow(window, this.iframeWindow, o.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest, this.iframeOrigin));
|
|
89
116
|
}
|
|
90
117
|
// TODO: Add proper return type annotation
|
|
91
118
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -104,28 +131,28 @@ class y {
|
|
|
104
131
|
const {
|
|
105
132
|
id: t,
|
|
106
133
|
jsonrpc: n,
|
|
107
|
-
method:
|
|
134
|
+
method: c
|
|
108
135
|
} = e;
|
|
109
136
|
if (!this.ethereumProvider || !t)
|
|
110
137
|
return;
|
|
111
138
|
// Keep the legacy "enable" method for backward compatibility
|
|
112
|
-
(
|
|
139
|
+
(c === "enable" ? this.ethereumProvider.request({
|
|
113
140
|
method: "eth_requestAccounts",
|
|
114
141
|
id: t
|
|
115
142
|
}) : this.ethereumProvider.request({
|
|
116
143
|
...e,
|
|
117
144
|
id: t
|
|
118
|
-
})).then((
|
|
145
|
+
})).then((d) => this.forwardRpcResponseToIframe({
|
|
119
146
|
rpcResponse: {
|
|
120
147
|
jsonrpc: n,
|
|
121
148
|
id: t,
|
|
122
|
-
result:
|
|
149
|
+
result: d
|
|
123
150
|
}
|
|
124
|
-
})).catch((
|
|
151
|
+
})).catch((d) => this.forwardRpcResponseToIframe({
|
|
125
152
|
rpcResponse: {
|
|
126
153
|
jsonrpc: n,
|
|
127
154
|
id: t,
|
|
128
|
-
error:
|
|
155
|
+
error: d
|
|
129
156
|
}
|
|
130
157
|
}));
|
|
131
158
|
}
|
|
@@ -140,14 +167,14 @@ class y {
|
|
|
140
167
|
};
|
|
141
168
|
processProviderMetaInfoRequest = () => {
|
|
142
169
|
if (!this.ethereumProvider) return;
|
|
143
|
-
const e =
|
|
144
|
-
|
|
170
|
+
const e = w(this.ethereumProvider, g), t = p(this.ethereumProvider);
|
|
171
|
+
a.postMessageToWindow(this.iframeWindow, o.SEND_PROVIDER_META_INFO, {
|
|
145
172
|
providerEip6963Info: e,
|
|
146
173
|
providerWcMetadata: t
|
|
147
174
|
}, this.iframeOrigin);
|
|
148
175
|
};
|
|
149
176
|
onProviderEvent(e, t) {
|
|
150
|
-
|
|
177
|
+
a.postMessageToWindow(this.iframeWindow, o.PROVIDER_ON_EVENT, {
|
|
151
178
|
event: e,
|
|
152
179
|
params: t
|
|
153
180
|
}, this.iframeOrigin);
|
|
@@ -158,20 +185,32 @@ class y {
|
|
|
158
185
|
// TODO: Add proper return type annotation
|
|
159
186
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
160
187
|
forwardRpcResponseToIframe(e) {
|
|
161
|
-
|
|
188
|
+
a.postMessageToWindow(this.iframeWindow, o.PROVIDER_RPC_RESPONSE, e, this.iframeOrigin);
|
|
162
189
|
}
|
|
163
190
|
}
|
|
164
|
-
function w() {
|
|
165
|
-
return P() || O() || _();
|
|
166
|
-
}
|
|
167
191
|
function P() {
|
|
192
|
+
const i = u(v()) || u(_()) || u(T());
|
|
193
|
+
if (i)
|
|
194
|
+
try {
|
|
195
|
+
return new URL(i).origin;
|
|
196
|
+
} catch (e) {
|
|
197
|
+
console.error("[getParentOrigin] origin is invalid", e, {
|
|
198
|
+
origin: i
|
|
199
|
+
});
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function u(i) {
|
|
204
|
+
return i && i !== "null" ? i : void 0;
|
|
205
|
+
}
|
|
206
|
+
function v() {
|
|
168
207
|
if (typeof window > "u")
|
|
169
208
|
return;
|
|
170
209
|
const i = window.location.ancestorOrigins;
|
|
171
210
|
if (!(!i || i.length === 0))
|
|
172
211
|
return i[0];
|
|
173
212
|
}
|
|
174
|
-
function
|
|
213
|
+
function _() {
|
|
175
214
|
if (!(typeof document > "u" || !document.referrer))
|
|
176
215
|
try {
|
|
177
216
|
return new URL(document.referrer).origin;
|
|
@@ -179,7 +218,7 @@ function O() {
|
|
|
179
218
|
return;
|
|
180
219
|
}
|
|
181
220
|
}
|
|
182
|
-
function
|
|
221
|
+
function T() {
|
|
183
222
|
if (!(typeof window > "u" || !window.parent || window.parent === window))
|
|
184
223
|
try {
|
|
185
224
|
return window.parent.location.origin;
|
|
@@ -187,8 +226,8 @@ function _() {
|
|
|
187
226
|
return;
|
|
188
227
|
}
|
|
189
228
|
}
|
|
190
|
-
const
|
|
191
|
-
class
|
|
229
|
+
const I = 6e5, M = "2.0", C = "https://swap.cow.fi";
|
|
230
|
+
class y extends Error {
|
|
192
231
|
isRpcError = !0;
|
|
193
232
|
code;
|
|
194
233
|
reason;
|
|
@@ -196,7 +235,7 @@ class M extends Error {
|
|
|
196
235
|
super(`${e}: ${t}`), this.code = e, this.reason = t;
|
|
197
236
|
}
|
|
198
237
|
}
|
|
199
|
-
class
|
|
238
|
+
class k extends f {
|
|
200
239
|
// TODO: Add proper return type annotation
|
|
201
240
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
202
241
|
request({
|
|
@@ -230,15 +269,15 @@ class S extends m {
|
|
|
230
269
|
providerMetaInfoCallback;
|
|
231
270
|
constructor({
|
|
232
271
|
targetOrigin: e,
|
|
233
|
-
timeoutMilliseconds: t =
|
|
272
|
+
timeoutMilliseconds: t = I,
|
|
234
273
|
eventSource: n = window,
|
|
235
|
-
eventTarget:
|
|
274
|
+
eventTarget: c = window.parent
|
|
236
275
|
} = {}) {
|
|
237
|
-
super(), this.timeoutMilliseconds = t, this.eventSource = n, this.eventTarget =
|
|
276
|
+
super(), this.timeoutMilliseconds = t, this.eventSource = n, this.eventTarget = c, this.targetOrigin = e || P() || C, a.listenToMessageFromWindow(this.eventSource, this.eventTarget, o.PROVIDER_RPC_RESPONSE, (r) => {
|
|
238
277
|
this.handleRpcRequests(r);
|
|
239
|
-
}, this.targetOrigin),
|
|
278
|
+
}, this.targetOrigin), a.listenToMessageFromWindow(this.eventSource, this.eventTarget, o.PROVIDER_ON_EVENT, (r) => {
|
|
240
279
|
this.handleOnEvent(r);
|
|
241
|
-
}, this.targetOrigin),
|
|
280
|
+
}, this.targetOrigin), a.listenToMessageFromWindow(this.eventSource, this.eventTarget, o.SEND_PROVIDER_META_INFO, (r) => {
|
|
242
281
|
this.providerMetaInfo = r, this.providerMetaInfoCallback && this.providerMetaInfoCallback(this.providerMetaInfo);
|
|
243
282
|
}, this.targetOrigin);
|
|
244
283
|
}
|
|
@@ -248,17 +287,17 @@ class S extends m {
|
|
|
248
287
|
* @param params params to pass the method
|
|
249
288
|
*/
|
|
250
289
|
async execute(e, t) {
|
|
251
|
-
const n =
|
|
252
|
-
jsonrpc:
|
|
290
|
+
const n = N(), c = {
|
|
291
|
+
jsonrpc: M,
|
|
253
292
|
id: n,
|
|
254
293
|
method: e,
|
|
255
294
|
params: t
|
|
256
|
-
}, r = new Promise((
|
|
257
|
-
resolve:
|
|
258
|
-
reject:
|
|
295
|
+
}, r = new Promise((d, s) => this.completers[n] = {
|
|
296
|
+
resolve: d,
|
|
297
|
+
reject: s
|
|
259
298
|
});
|
|
260
|
-
return
|
|
261
|
-
rpcRequest:
|
|
299
|
+
return a.postMessageToWindow(this.eventTarget, o.PROVIDER_RPC_REQUEST, {
|
|
300
|
+
rpcRequest: c
|
|
262
301
|
}, this.targetOrigin), setTimeout(() => {
|
|
263
302
|
this.completers[n] && (this.completers[n].reject(new Error("Request timed out. Please try again and submit it faster.")), delete this.completers[n]);
|
|
264
303
|
}, this.timeoutMilliseconds), r;
|
|
@@ -268,16 +307,16 @@ class S extends m {
|
|
|
268
307
|
async send(e, t) {
|
|
269
308
|
const {
|
|
270
309
|
method: n,
|
|
271
|
-
params:
|
|
310
|
+
params: c
|
|
272
311
|
} = typeof e == "string" ? {
|
|
273
312
|
method: e,
|
|
274
313
|
params: t
|
|
275
314
|
} : {
|
|
276
315
|
method: e.method,
|
|
277
316
|
params: e.params
|
|
278
|
-
}, r = await this.execute(n,
|
|
317
|
+
}, r = await this.execute(n, c);
|
|
279
318
|
if ("error" in r)
|
|
280
|
-
throw new
|
|
319
|
+
throw new y(r.error.code, r.error.message);
|
|
281
320
|
return r.result;
|
|
282
321
|
}
|
|
283
322
|
/**
|
|
@@ -302,7 +341,7 @@ class S extends m {
|
|
|
302
341
|
const n = await this.execute(e.method, e.params);
|
|
303
342
|
t(null, n);
|
|
304
343
|
} catch (n) {
|
|
305
|
-
t(
|
|
344
|
+
t(S(n), null);
|
|
306
345
|
}
|
|
307
346
|
}
|
|
308
347
|
/**
|
|
@@ -312,7 +351,7 @@ class S extends m {
|
|
|
312
351
|
// TODO: Add proper return type annotation
|
|
313
352
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
314
353
|
onProviderMetaInfo(e) {
|
|
315
|
-
this.providerMetaInfo ? e(this.providerMetaInfo) : (this.providerMetaInfoCallback = e,
|
|
354
|
+
this.providerMetaInfo ? e(this.providerMetaInfo) : (this.providerMetaInfoCallback = e, a.postMessageToWindow(this.eventTarget, o.REQUEST_PROVIDER_META_INFO, null, this.targetOrigin));
|
|
316
355
|
}
|
|
317
356
|
/**
|
|
318
357
|
* Handle a Rpc Request
|
|
@@ -411,10 +450,10 @@ class S extends m {
|
|
|
411
450
|
this.emit("message", e);
|
|
412
451
|
}
|
|
413
452
|
}
|
|
414
|
-
function
|
|
453
|
+
function N() {
|
|
415
454
|
return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
416
455
|
}
|
|
417
|
-
function
|
|
456
|
+
function S(i) {
|
|
418
457
|
if (!i) return null;
|
|
419
458
|
if (typeof i == "string") return i;
|
|
420
459
|
if (i instanceof Error || i.message) return i.message;
|
|
@@ -426,10 +465,10 @@ function N(i) {
|
|
|
426
465
|
}
|
|
427
466
|
}
|
|
428
467
|
export {
|
|
429
|
-
|
|
468
|
+
F as IframeRpcProviderBridge,
|
|
430
469
|
o as IframeRpcProviderEvents,
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
470
|
+
E as IframeTransport,
|
|
471
|
+
k as WidgetEthereumProvider,
|
|
472
|
+
P as getParentOrigin,
|
|
473
|
+
a as iframeRpcProviderTransport
|
|
435
474
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/iframe-transport",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
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.2.
|
|
18
|
+
"@cowprotocol/types": "^4.2.1",
|
|
19
19
|
"eventemitter3": "^4.0.0"
|
|
20
20
|
},
|
|
21
21
|
"exports": {
|