@cowprotocol/widget-lib 4.0.5 → 4.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/allowedWindowOpenUrl.d.ts +1 -0
- package/index.cjs +1 -1
- package/index.js +184 -155
- package/package.json +4 -4
- package/types.d.ts +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isAllowedWindowOpenUrl(url: string): boolean;
|
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 N=require("@cowprotocol/iframe-transport"),$=require("@cowprotocol/events"),x=new Set(["wc:","metamask:","imtokenv2:","trustassetapp:","okex:","ledgerlive:","rainbow:","argent:","exodus:","zerion:","okxwallet:","uniswap:","robinhood-wallet:","krakenwallet:","bitpay:","oneinch:","safe:","backpack:","tangem:","mathwallet:","keplrwallet:","coin98:","tpoutside:","bncus:","bnc:"]),j=new Set(["http:","https:"]);function K(e){try{const t=new URL(e).protocol;return j.has(t)||x.has(t)}catch{return!1}}const v="allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox",U="strict-origin-when-cross-origin",b="clipboard-read; clipboard-write";var c=(e=>(e.ACTIVATE="ACTIVATE",e.READY="READY",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e.PROCESS_HOOK="PROCESS_HOOK",e))(c||{}),T=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.HOOK_RESULT="HOOK_RESULT",e))(T||{}),E=(e=>(e.ON_BEFORE_APPROVAL="ON_BEFORE_APPROVAL",e.ON_BEFORE_TRADE="ON_BEFORE_TRADE",e.ON_BEFORE_WRAP_UNWRAP="ON_BEFORE_WRAP_UNWRAP",e.ON_BEFORE_ORDER_CANCEL="ON_BEFORE_ORDER_CANCEL",e.ON_BEFORE_ORDERS_CANCEL="ON_BEFORE_ORDERS_CANCEL",e))(E||{}),g=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(g||{});const Q=["primary","background","paper","text","danger","warning","alert","info","success"],a=new N.IframeTransport("cowSwapWidget");class Y{constructor(t,n,o,r=[]){this.contentWindow=t,this.updateListeners(r),this.widgetListener=a.listenToMessageFromWindow(this.contentWindow,o,c.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload),n)}eventEmitter=new $.SimpleCowEventEmitter;listeners=[];widgetListener;stopListeningIframe(){a.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class X{constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=o=>{z(o.data)&&(typeof window<"u"&&o.origin===window.location.origin||(J(o.data)?this.appWindow.parent.postMessage(o.data,"*"):Z(o.data)&&this.iframeWidow.postMessage(o.data,"*")))},this.startListening()}forwardSdkMessage;startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function z(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function J(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function q(e="Default",t="#1c5dbf",...n){console.debug(`%c [COW][${e}]`,`font-weight: bold; color: ${t}`,...n)}function ee(...e){process.env.NODE_ENV!=="test"&&q("Widget","#47aba8",...e)}const F="https://swap.cow.fi";function H(e){return!!(e&&typeof e=="object")}const C="_",te="CoW Swap widget: invalid baseUrl.",y=!1;function ne(e){return e==="localhost"||e==="127.0.0.1"||e==="::1"||e.endsWith(".localhost")}function P(e,t){const n=`${te} ${e}`;if(t)throw new Error(n);return console.error("[CoW Widget]",n),F}function B(e,t=y){const n=typeof e=="string"?e.trim():"";if(!n)return F;let o;try{o=new URL(n)}catch{return P(`Not a valid URL: ${JSON.stringify(n)}`,t)}if(o.username||o.password)return P("Userinfo (username/password) in URL is not allowed.",t);const r=o.protocol==="https:",i=o.protocol==="http:"&&ne(o.hostname);if(!r&&!i)return P(`Use https, or http only on localhost / 127.0.0.1 / ::1 / *.localhost (got ${o.protocol}//${o.hostname}).`,t);const w=o.pathname==="/"||o.pathname===""?"":o.pathname.replace(/\/+$/,"");return`${o.origin}${w}`}function oe(e){const t=B(e.baseUrl),n=k(e);return t+"/#"+n+"?"+M(e)}function k(e){const{chainId:t=1,sell:n,buy:o,tradeType:r=g.SWAP}=e,i=[n?.asset||C,o?.asset||C].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${i}`}function M(e){const t=new URLSearchParams;return de(ae(se(ie(re(t,e),e),e),e),e)}function re(e,t){const{sell:n,buy:o}=t;return n?.amount&&e.append("sellAmount",n.amount),o?.amount&&e.append("buyAmount",o.amount),e}function ie(e,t){const n=t.theme;return n?(H(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function ae(e,t){return t.locale&&e.append("lng",t.locale),e}function de(e,t){return le(t.hooks)?e.append("hooksEnabled","true"):e.delete("hooksEnabled"),e}function le(e){return!!e&&Object.values(e).some(Boolean)}const V="640px",ce="450px",ue=20,Ee={iframe:null,updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function pe(e,t){const{params:n,provider:o,listeners:r,onReady:i,enableSafeSdkBridge:w=!0}=t;let _=o,p=n;if(typeof window>"u")return Ee;const l=fe(n),d=we(l);ee("Resolved trusted iframe origin",{iframeOrigin:d});const f=[];e.innerHTML="",e.appendChild(l);const{contentWindow:s}=l;if(!s)throw console.error("Iframe does not contain a window",l),new Error("Iframe does not contain a window!");i&&f.push(Oe(s,d,i)),f.push(_e(s,d,n.appCode)),f.push(...he(l,d,n.height,n.maxHeight));let h=null;function I(){s&&(h&&window.removeEventListener("message",h),!p.disableWindowOpen&&(h=Re(d,s),f.push(h)))}let A=null;function m(){s&&(A&&window.removeEventListener("message",A),A=Le(s,d,p.hooks),f.push(A))}I(),m();const W=new Y(window,d,s,r);let L=S(s,d,null,_);l.addEventListener("load",()=>{D(s,d,p,_)});const G=w?new X(window,s):null;return{iframe:l,updateParams:u=>{p=u,D(s,d,p,_),I(),m()},updateListeners:u=>W.updateListeners(u),updateProvider:u=>{_=u,L=S(s,d,L,u)},destroy:()=>{L.disconnect(),W.stopListeningIframe(),f.forEach(u=>window.removeEventListener("message",u)),G?.stopListening(),e.removeChild(l)}}}function S(e,t,n,o){n&&n.disconnect();const r=n||new N.IframeRpcProviderBridge(e,t);return o&&r.onConnect(o),r}function fe(e){const{width:t=ce,height:n=V}=e,o=document.createElement("iframe");return o.src=oe(e),o.width=t,o.height=n,o.style.border="0",o.setAttribute("sandbox",v),o.referrerPolicy=U,o.allow=b,o}function we(e){return new URL(e.src).origin}function D(e,t,n,o){const r=!!o,i=k(n),w=M(n).toString(),{theme:_,hooks:p,...l}=n;a.postMessageToWindow(e,T.UPDATE_PARAMS,{urlParams:{pathname:i,search:w},appParams:l,hasProvider:r},t)}function _e(e,t,n){return a.listenToMessageFromWindow(window,e,c.ACTIVATE,()=>{a.postMessageToWindow(e,T.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0},t)},t)}function Oe(e,t,n){let o=!1;return a.listenToMessageFromWindow(window,e,c.READY,()=>{o||(o=!0,n())},t)}function Re(e,t){return a.listenToMessageFromWindow(window,t,c.INTERCEPT_WINDOW_OPEN,({href:n,rel:o,target:r})=>{const i=Te(n.toString(),e);i&&K(i)&&window.open(i,r,o)},e)}function Te(e,t){const n=e.trim();if(!n)return null;try{return new URL(n,t).toString()}catch{return null}}function he(e,t,n=V,o){return e.contentWindow?[a.listenToMessageFromWindow(window,e.contentWindow,c.UPDATE_HEIGHT,r=>{const i=r.height?r.height+ue:void 0;e.style.height=i?`${o?Math.min(i,o):i}px`:n},t),a.listenToMessageFromWindow(window,e.contentWindow,c.SET_FULL_HEIGHT,({isUpToSmall:r})=>{e.style.height=r?n:`${o||document.body.offsetHeight}px`},t)]:[]}const Ae={[E.ON_BEFORE_APPROVAL]:(e,t)=>t?.onBeforeApproval?t.onBeforeApproval(e):!0,[E.ON_BEFORE_TRADE]:(e,t)=>t?.onBeforeTrade?t.onBeforeTrade(e):!0,[E.ON_BEFORE_WRAP_UNWRAP]:(e,t)=>t?.onBeforeWrapOrUnwrap?t.onBeforeWrapOrUnwrap(e):!0,[E.ON_BEFORE_ORDER_CANCEL]:(e,t)=>t?.onBeforeOrderCancel?t.onBeforeOrderCancel(e):!0,[E.ON_BEFORE_ORDERS_CANCEL]:(e,t)=>t?.onBeforeOrdersCancel?t.onBeforeOrdersCancel(e):!0};function ge(e,t){return Ae[e.event](e.payload,t)}function Le(e,t,n){return a.listenToMessageFromWindow(window,e,c.PROCESS_HOOK,async o=>{let r=!1;try{r=await ge(o,n)}catch{}a.postMessageToWindow(e,T.HOOK_RESULT,{id:o.id,result:r},t)},t)}const Pe=Object.values(g);function Ie(e,t,n){if(O(e)){const o=e[n];return R(o)?o[t]:o}if(R(e)){const o=e[t];return O(o)?o[n]:o}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>Pe.includes(t))}const me=/^\d+$/;function R(e){return typeof e!="object"?!1:Object.keys(e).every(t=>me.test(t))}function We(e){return O(e)?Object.values(e).flatMap(t=>R(t)?Object.values(t):[t]):R(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}exports.SHOULD_THROW_IF_INVALID_URL=y;exports.TradeType=g;exports.WIDGET_IFRAME_ALLOW=b;exports.WIDGET_IFRAME_REFERRER_POLICY=U;exports.WIDGET_IFRAME_SANDBOX=v;exports.WIDGET_PALETTE_COLORS=Q;exports.WidgetHookEvents=E;exports.WidgetMethodsEmit=c;exports.WidgetMethodsListen=T;exports.createCowSwapWidget=pe;exports.isCowSwapWidgetPalette=H;exports.isPerNetworkConfig=R;exports.isPerTradeTypeConfig=O;exports.resolveFlexibleConfig=Ie;exports.resolveFlexibleConfigValues=We;exports.sanitizeWidgetBaseUrl=B;exports.widgetIframeTransport=a;
|
package/index.js
CHANGED
|
@@ -1,13 +1,49 @@
|
|
|
1
|
-
import { IframeTransport as
|
|
2
|
-
import { SimpleCowEventEmitter as
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { IframeTransport as y, IframeRpcProviderBridge as B } from "@cowprotocol/iframe-transport";
|
|
2
|
+
import { SimpleCowEventEmitter as F } from "@cowprotocol/events";
|
|
3
|
+
const k = /* @__PURE__ */ new Set([
|
|
4
|
+
"wc:",
|
|
5
|
+
// default Wallet-connect protocol
|
|
6
|
+
"metamask:",
|
|
7
|
+
"imtokenv2:",
|
|
8
|
+
"trustassetapp:",
|
|
9
|
+
"okex:",
|
|
10
|
+
"ledgerlive:",
|
|
11
|
+
"rainbow:",
|
|
12
|
+
"argent:",
|
|
13
|
+
"exodus:",
|
|
14
|
+
"zerion:",
|
|
15
|
+
"okxwallet:",
|
|
16
|
+
"uniswap:",
|
|
17
|
+
"robinhood-wallet:",
|
|
18
|
+
"krakenwallet:",
|
|
19
|
+
"bitpay:",
|
|
20
|
+
"oneinch:",
|
|
21
|
+
"safe:",
|
|
22
|
+
"backpack:",
|
|
23
|
+
"tangem:",
|
|
24
|
+
"mathwallet:",
|
|
25
|
+
"keplrwallet:",
|
|
26
|
+
"coin98:",
|
|
27
|
+
"tpoutside:",
|
|
28
|
+
"bncus:",
|
|
29
|
+
"bnc:"
|
|
30
|
+
]), M = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
31
|
+
function V(e) {
|
|
32
|
+
try {
|
|
33
|
+
const t = new URL(e).protocol;
|
|
34
|
+
return M.has(t) || k.has(t);
|
|
35
|
+
} catch {
|
|
36
|
+
return !1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const $ = "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox", x = "strict-origin-when-cross-origin", G = "clipboard-read; clipboard-write";
|
|
40
|
+
var u = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.READY = "READY", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.SET_FULL_HEIGHT = "SET_FULL_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.INTERCEPT_WINDOW_OPEN = "INTERCEPT_WINDOW_OPEN", e.PROCESS_HOOK = "PROCESS_HOOK", e))(u || {}), A = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e.HOOK_RESULT = "HOOK_RESULT", e))(A || {}), f = /* @__PURE__ */ ((e) => (e.ON_BEFORE_APPROVAL = "ON_BEFORE_APPROVAL", e.ON_BEFORE_TRADE = "ON_BEFORE_TRADE", e.ON_BEFORE_WRAP_UNWRAP = "ON_BEFORE_WRAP_UNWRAP", e.ON_BEFORE_ORDER_CANCEL = "ON_BEFORE_ORDER_CANCEL", e.ON_BEFORE_ORDERS_CANCEL = "ON_BEFORE_ORDERS_CANCEL", e))(f || {}), m = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(m || {});
|
|
41
|
+
const Se = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], d = new y("cowSwapWidget");
|
|
42
|
+
class j {
|
|
7
43
|
constructor(t, n, o, r = []) {
|
|
8
|
-
this.contentWindow = t, this.updateListeners(r), this.widgetListener = d.listenToMessageFromWindow(this.contentWindow, o,
|
|
44
|
+
this.contentWindow = t, this.updateListeners(r), this.widgetListener = d.listenToMessageFromWindow(this.contentWindow, o, u.EMIT_COW_EVENT, (i) => this.eventEmitter.emit(i.event, i.payload), n);
|
|
9
45
|
}
|
|
10
|
-
eventEmitter = new
|
|
46
|
+
eventEmitter = new F();
|
|
11
47
|
listeners = [];
|
|
12
48
|
widgetListener;
|
|
13
49
|
stopListeningIframe() {
|
|
@@ -21,10 +57,10 @@ class k {
|
|
|
21
57
|
this.eventEmitter.on(n);
|
|
22
58
|
}
|
|
23
59
|
}
|
|
24
|
-
class
|
|
60
|
+
class K {
|
|
25
61
|
constructor(t, n) {
|
|
26
62
|
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (o) => {
|
|
27
|
-
|
|
63
|
+
Q(o.data) && (typeof window < "u" && o.origin === window.location.origin || (Y(o.data) ? this.appWindow.parent.postMessage(o.data, "*") : X(o.data) && this.iframeWidow.postMessage(o.data, "*")));
|
|
28
64
|
}, this.startListening();
|
|
29
65
|
}
|
|
30
66
|
forwardSdkMessage;
|
|
@@ -35,58 +71,58 @@ class $ {
|
|
|
35
71
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
36
72
|
}
|
|
37
73
|
}
|
|
38
|
-
function
|
|
74
|
+
function Q(e) {
|
|
39
75
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
40
76
|
}
|
|
41
|
-
function
|
|
77
|
+
function Y(e) {
|
|
42
78
|
return "method" in e && typeof e.method == "string" && "params" in e && "env" in e && typeof e.env == "object" && e.env !== null && "sdkVersion" in e.env;
|
|
43
79
|
}
|
|
44
|
-
function
|
|
80
|
+
function X(e) {
|
|
45
81
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
46
82
|
}
|
|
47
|
-
function
|
|
83
|
+
function z(e = "Default", t = "#1c5dbf", ...n) {
|
|
48
84
|
console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${t}`, ...n);
|
|
49
85
|
}
|
|
50
|
-
function
|
|
51
|
-
process.env.NODE_ENV !== "test" &&
|
|
86
|
+
function J(...e) {
|
|
87
|
+
process.env.NODE_ENV !== "test" && z("Widget", "#47aba8", ...e);
|
|
52
88
|
}
|
|
53
|
-
const
|
|
54
|
-
function
|
|
89
|
+
const N = "https://swap.cow.fi";
|
|
90
|
+
function Z(e) {
|
|
55
91
|
return !!(e && typeof e == "object");
|
|
56
92
|
}
|
|
57
|
-
const C = "_",
|
|
58
|
-
function
|
|
93
|
+
const C = "_", q = "CoW Swap widget: invalid baseUrl.", ee = !1;
|
|
94
|
+
function te(e) {
|
|
59
95
|
return e === "localhost" || e === "127.0.0.1" || e === "::1" || e.endsWith(".localhost");
|
|
60
96
|
}
|
|
61
|
-
function
|
|
62
|
-
const n = `${
|
|
97
|
+
function L(e, t) {
|
|
98
|
+
const n = `${q} ${e}`;
|
|
63
99
|
if (t)
|
|
64
100
|
throw new Error(n);
|
|
65
|
-
return console.error("[CoW Widget]", n),
|
|
101
|
+
return console.error("[CoW Widget]", n), N;
|
|
66
102
|
}
|
|
67
|
-
function
|
|
103
|
+
function ne(e, t = ee) {
|
|
68
104
|
const n = typeof e == "string" ? e.trim() : "";
|
|
69
105
|
if (!n)
|
|
70
|
-
return
|
|
106
|
+
return N;
|
|
71
107
|
let o;
|
|
72
108
|
try {
|
|
73
109
|
o = new URL(n);
|
|
74
110
|
} catch {
|
|
75
|
-
return
|
|
111
|
+
return L(`Not a valid URL: ${JSON.stringify(n)}`, t);
|
|
76
112
|
}
|
|
77
113
|
if (o.username || o.password)
|
|
78
|
-
return
|
|
79
|
-
const r = o.protocol === "https:", i = o.protocol === "http:" &&
|
|
114
|
+
return L("Userinfo (username/password) in URL is not allowed.", t);
|
|
115
|
+
const r = o.protocol === "https:", i = o.protocol === "http:" && te(o.hostname);
|
|
80
116
|
if (!r && !i)
|
|
81
|
-
return
|
|
82
|
-
const
|
|
83
|
-
return `${o.origin}${
|
|
117
|
+
return L(`Use https, or http only on localhost / 127.0.0.1 / ::1 / *.localhost (got ${o.protocol}//${o.hostname}).`, t);
|
|
118
|
+
const w = o.pathname === "/" || o.pathname === "" ? "" : o.pathname.replace(/\/+$/, "");
|
|
119
|
+
return `${o.origin}${w}`;
|
|
84
120
|
}
|
|
85
|
-
function
|
|
86
|
-
const t =
|
|
87
|
-
return t + "/#" + n + "?" +
|
|
121
|
+
function oe(e) {
|
|
122
|
+
const t = ne(e.baseUrl), n = v(e);
|
|
123
|
+
return t + "/#" + n + "?" + U(e);
|
|
88
124
|
}
|
|
89
|
-
function
|
|
125
|
+
function v(e) {
|
|
90
126
|
const {
|
|
91
127
|
chainId: t = 1,
|
|
92
128
|
sell: n,
|
|
@@ -95,34 +131,34 @@ function N(e) {
|
|
|
95
131
|
} = e, i = [n?.asset || C, o?.asset || C].map(encodeURIComponent).join("/");
|
|
96
132
|
return `/${t}/widget/${r}/${i}`;
|
|
97
133
|
}
|
|
98
|
-
function
|
|
134
|
+
function U(e) {
|
|
99
135
|
const t = new URLSearchParams();
|
|
100
|
-
return
|
|
136
|
+
return de(ae(se(ie(re(t, e), e), e), e), e);
|
|
101
137
|
}
|
|
102
|
-
function
|
|
138
|
+
function re(e, t) {
|
|
103
139
|
const {
|
|
104
140
|
sell: n,
|
|
105
141
|
buy: o
|
|
106
142
|
} = t;
|
|
107
143
|
return n?.amount && e.append("sellAmount", n.amount), o?.amount && e.append("buyAmount", o.amount), e;
|
|
108
144
|
}
|
|
109
|
-
function
|
|
145
|
+
function ie(e, t) {
|
|
110
146
|
const n = t.theme;
|
|
111
|
-
return n ? (
|
|
147
|
+
return n ? (Z(n) ? (e.append("palette", encodeURIComponent(JSON.stringify(n))), e.append("theme", n.baseTheme)) : (e.append("palette", "null"), e.append("theme", n)), e) : (e.append("palette", "null"), e);
|
|
112
148
|
}
|
|
113
|
-
function
|
|
149
|
+
function se(e, t) {
|
|
114
150
|
return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
|
|
115
151
|
}
|
|
116
|
-
function
|
|
152
|
+
function ae(e, t) {
|
|
117
153
|
return t.locale && e.append("lng", t.locale), e;
|
|
118
154
|
}
|
|
119
|
-
function
|
|
120
|
-
return
|
|
155
|
+
function de(e, t) {
|
|
156
|
+
return le(t.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
|
|
121
157
|
}
|
|
122
|
-
function
|
|
158
|
+
function le(e) {
|
|
123
159
|
return !!e && Object.values(e).some(Boolean);
|
|
124
160
|
}
|
|
125
|
-
const
|
|
161
|
+
const b = "640px", ce = "450px", ue = 20, pe = {
|
|
126
162
|
iframe: null,
|
|
127
163
|
updateParams: () => {
|
|
128
164
|
},
|
|
@@ -133,112 +169,113 @@ const U = "640px", se = "450px", ae = 20, de = {
|
|
|
133
169
|
destroy: () => {
|
|
134
170
|
}
|
|
135
171
|
};
|
|
136
|
-
function
|
|
172
|
+
function Ce(e, t) {
|
|
137
173
|
const {
|
|
138
174
|
params: n,
|
|
139
175
|
provider: o,
|
|
140
176
|
listeners: r,
|
|
141
|
-
onReady: i
|
|
177
|
+
onReady: i,
|
|
178
|
+
enableSafeSdkBridge: w = !0
|
|
142
179
|
} = t;
|
|
143
|
-
let
|
|
144
|
-
if (typeof window > "u") return
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
iframeOrigin:
|
|
180
|
+
let _ = o, p = n;
|
|
181
|
+
if (typeof window > "u") return pe;
|
|
182
|
+
const l = Ee(n), a = fe(l);
|
|
183
|
+
J("Resolved trusted iframe origin", {
|
|
184
|
+
iframeOrigin: a
|
|
148
185
|
});
|
|
149
|
-
const
|
|
150
|
-
e.innerHTML = "", e.appendChild(
|
|
186
|
+
const E = [];
|
|
187
|
+
e.innerHTML = "", e.appendChild(l);
|
|
151
188
|
const {
|
|
152
|
-
contentWindow:
|
|
153
|
-
} =
|
|
154
|
-
if (!
|
|
155
|
-
throw console.error("Iframe does not contain a window",
|
|
156
|
-
i &&
|
|
157
|
-
let
|
|
189
|
+
contentWindow: s
|
|
190
|
+
} = l;
|
|
191
|
+
if (!s)
|
|
192
|
+
throw console.error("Iframe does not contain a window", l), new Error("Iframe does not contain a window!");
|
|
193
|
+
i && E.push(_e(s, a, i)), E.push(we(s, a, n.appCode)), E.push(...Te(l, a, n.height, n.maxHeight));
|
|
194
|
+
let O = null;
|
|
158
195
|
function g() {
|
|
159
|
-
|
|
196
|
+
s && (O && window.removeEventListener("message", O), !p.disableWindowOpen && (O = Oe(a, s), E.push(O)));
|
|
160
197
|
}
|
|
161
|
-
let
|
|
162
|
-
function
|
|
163
|
-
|
|
198
|
+
let R = null;
|
|
199
|
+
function I() {
|
|
200
|
+
s && (R && window.removeEventListener("message", R), R = Pe(s, a, p.hooks), E.push(R));
|
|
164
201
|
}
|
|
165
|
-
g(),
|
|
166
|
-
const
|
|
167
|
-
let
|
|
168
|
-
|
|
169
|
-
|
|
202
|
+
g(), I();
|
|
203
|
+
const S = new j(window, a, s, r);
|
|
204
|
+
let P = W(s, a, null, _);
|
|
205
|
+
l.addEventListener("load", () => {
|
|
206
|
+
D(s, a, p, _);
|
|
170
207
|
});
|
|
171
|
-
const H = new
|
|
208
|
+
const H = w ? new K(window, s) : null;
|
|
172
209
|
return {
|
|
173
|
-
iframe:
|
|
174
|
-
updateParams: (
|
|
175
|
-
|
|
210
|
+
iframe: l,
|
|
211
|
+
updateParams: (c) => {
|
|
212
|
+
p = c, D(s, a, p, _), g(), I();
|
|
176
213
|
},
|
|
177
|
-
updateListeners: (
|
|
178
|
-
updateProvider: (
|
|
179
|
-
|
|
214
|
+
updateListeners: (c) => S.updateListeners(c),
|
|
215
|
+
updateProvider: (c) => {
|
|
216
|
+
_ = c, P = W(s, a, P, c);
|
|
180
217
|
},
|
|
181
218
|
destroy: () => {
|
|
182
|
-
|
|
219
|
+
P.disconnect(), S.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), H?.stopListening(), e.removeChild(l);
|
|
183
220
|
}
|
|
184
221
|
};
|
|
185
222
|
}
|
|
186
|
-
function
|
|
223
|
+
function W(e, t, n, o) {
|
|
187
224
|
n && n.disconnect();
|
|
188
|
-
const r = n || new
|
|
225
|
+
const r = n || new B(e, t);
|
|
189
226
|
return o && r.onConnect(o), r;
|
|
190
227
|
}
|
|
191
|
-
function
|
|
228
|
+
function Ee(e) {
|
|
192
229
|
const {
|
|
193
|
-
width: t =
|
|
194
|
-
height: n =
|
|
230
|
+
width: t = ce,
|
|
231
|
+
height: n = b
|
|
195
232
|
} = e, o = document.createElement("iframe");
|
|
196
|
-
return o.src =
|
|
233
|
+
return o.src = oe(e), o.width = t, o.height = n, o.style.border = "0", o.setAttribute("sandbox", $), o.referrerPolicy = x, o.allow = G, o;
|
|
197
234
|
}
|
|
198
|
-
function
|
|
235
|
+
function fe(e) {
|
|
199
236
|
return new URL(e.src).origin;
|
|
200
237
|
}
|
|
201
|
-
function
|
|
202
|
-
const r = !!o, i =
|
|
203
|
-
theme:
|
|
204
|
-
hooks:
|
|
205
|
-
...
|
|
238
|
+
function D(e, t, n, o) {
|
|
239
|
+
const r = !!o, i = v(n), w = U(n).toString(), {
|
|
240
|
+
theme: _,
|
|
241
|
+
hooks: p,
|
|
242
|
+
...l
|
|
206
243
|
} = n;
|
|
207
|
-
d.postMessageToWindow(e,
|
|
244
|
+
d.postMessageToWindow(e, A.UPDATE_PARAMS, {
|
|
208
245
|
urlParams: {
|
|
209
246
|
pathname: i,
|
|
210
|
-
search:
|
|
247
|
+
search: w
|
|
211
248
|
},
|
|
212
|
-
appParams:
|
|
249
|
+
appParams: l,
|
|
213
250
|
hasProvider: r
|
|
214
251
|
}, t);
|
|
215
252
|
}
|
|
216
|
-
function
|
|
217
|
-
return d.listenToMessageFromWindow(window, e,
|
|
218
|
-
d.postMessageToWindow(e,
|
|
253
|
+
function we(e, t, n) {
|
|
254
|
+
return d.listenToMessageFromWindow(window, e, u.ACTIVATE, () => {
|
|
255
|
+
d.postMessageToWindow(e, A.UPDATE_APP_DATA, {
|
|
219
256
|
metaData: n ? {
|
|
220
257
|
appCode: n
|
|
221
258
|
} : void 0
|
|
222
259
|
}, t);
|
|
223
260
|
}, t);
|
|
224
261
|
}
|
|
225
|
-
function
|
|
262
|
+
function _e(e, t, n) {
|
|
226
263
|
let o = !1;
|
|
227
|
-
return d.listenToMessageFromWindow(window, e,
|
|
264
|
+
return d.listenToMessageFromWindow(window, e, u.READY, () => {
|
|
228
265
|
o || (o = !0, n());
|
|
229
266
|
}, t);
|
|
230
267
|
}
|
|
231
|
-
function
|
|
232
|
-
return d.listenToMessageFromWindow(window, t,
|
|
268
|
+
function Oe(e, t) {
|
|
269
|
+
return d.listenToMessageFromWindow(window, t, u.INTERCEPT_WINDOW_OPEN, ({
|
|
233
270
|
href: n,
|
|
234
271
|
rel: o,
|
|
235
272
|
target: r
|
|
236
273
|
}) => {
|
|
237
|
-
const i =
|
|
238
|
-
i &&
|
|
274
|
+
const i = Re(n.toString(), e);
|
|
275
|
+
i && V(i) && window.open(i, r, o);
|
|
239
276
|
}, e);
|
|
240
277
|
}
|
|
241
|
-
function
|
|
278
|
+
function Re(e, t) {
|
|
242
279
|
const n = e.trim();
|
|
243
280
|
if (!n)
|
|
244
281
|
return null;
|
|
@@ -248,85 +285,77 @@ function fe(e, t) {
|
|
|
248
285
|
return null;
|
|
249
286
|
}
|
|
250
287
|
}
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
return t === "http:" || t === "https:";
|
|
255
|
-
} catch {
|
|
256
|
-
return !1;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
function _e(e, t, n = U, o) {
|
|
260
|
-
return e.contentWindow ? [d.listenToMessageFromWindow(window, e.contentWindow, p.UPDATE_HEIGHT, (r) => {
|
|
261
|
-
const i = r.height ? r.height + ae : void 0;
|
|
288
|
+
function Te(e, t, n = b, o) {
|
|
289
|
+
return e.contentWindow ? [d.listenToMessageFromWindow(window, e.contentWindow, u.UPDATE_HEIGHT, (r) => {
|
|
290
|
+
const i = r.height ? r.height + ue : void 0;
|
|
262
291
|
e.style.height = i ? `${o ? Math.min(i, o) : i}px` : n;
|
|
263
|
-
}, t), d.listenToMessageFromWindow(window, e.contentWindow,
|
|
292
|
+
}, t), d.listenToMessageFromWindow(window, e.contentWindow, u.SET_FULL_HEIGHT, ({
|
|
264
293
|
isUpToSmall: r
|
|
265
294
|
}) => {
|
|
266
295
|
e.style.height = r ? n : `${o || document.body.offsetHeight}px`;
|
|
267
296
|
}, t)] : [];
|
|
268
297
|
}
|
|
269
|
-
const
|
|
270
|
-
[
|
|
271
|
-
[
|
|
272
|
-
[
|
|
273
|
-
[
|
|
274
|
-
[
|
|
298
|
+
const he = {
|
|
299
|
+
[f.ON_BEFORE_APPROVAL]: (e, t) => t?.onBeforeApproval ? t.onBeforeApproval(e) : !0,
|
|
300
|
+
[f.ON_BEFORE_TRADE]: (e, t) => t?.onBeforeTrade ? t.onBeforeTrade(e) : !0,
|
|
301
|
+
[f.ON_BEFORE_WRAP_UNWRAP]: (e, t) => t?.onBeforeWrapOrUnwrap ? t.onBeforeWrapOrUnwrap(e) : !0,
|
|
302
|
+
[f.ON_BEFORE_ORDER_CANCEL]: (e, t) => t?.onBeforeOrderCancel ? t.onBeforeOrderCancel(e) : !0,
|
|
303
|
+
[f.ON_BEFORE_ORDERS_CANCEL]: (e, t) => t?.onBeforeOrdersCancel ? t.onBeforeOrdersCancel(e) : !0
|
|
275
304
|
};
|
|
276
|
-
function
|
|
277
|
-
return
|
|
305
|
+
function Ae(e, t) {
|
|
306
|
+
return he[e.event](e.payload, t);
|
|
278
307
|
}
|
|
279
|
-
function
|
|
280
|
-
return d.listenToMessageFromWindow(window, e,
|
|
308
|
+
function Pe(e, t, n) {
|
|
309
|
+
return d.listenToMessageFromWindow(window, e, u.PROCESS_HOOK, async (o) => {
|
|
281
310
|
let r = !1;
|
|
282
311
|
try {
|
|
283
|
-
r = await
|
|
312
|
+
r = await Ae(o, n);
|
|
284
313
|
} catch {
|
|
285
314
|
}
|
|
286
|
-
d.postMessageToWindow(e,
|
|
315
|
+
d.postMessageToWindow(e, A.HOOK_RESULT, {
|
|
287
316
|
id: o.id,
|
|
288
317
|
result: r
|
|
289
318
|
}, t);
|
|
290
319
|
}, t);
|
|
291
320
|
}
|
|
292
|
-
const
|
|
293
|
-
function
|
|
294
|
-
if (
|
|
321
|
+
const Le = Object.values(m);
|
|
322
|
+
function We(e, t, n) {
|
|
323
|
+
if (T(e)) {
|
|
295
324
|
const o = e[n];
|
|
296
|
-
return
|
|
325
|
+
return h(o) ? o[t] : o;
|
|
297
326
|
}
|
|
298
|
-
if (
|
|
327
|
+
if (h(e)) {
|
|
299
328
|
const o = e[t];
|
|
300
|
-
return
|
|
329
|
+
return T(o) ? o[n] : o;
|
|
301
330
|
}
|
|
302
331
|
return e;
|
|
303
332
|
}
|
|
304
|
-
function R(e) {
|
|
305
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) => he.includes(t));
|
|
306
|
-
}
|
|
307
|
-
const Ae = /^\d+$/;
|
|
308
333
|
function T(e) {
|
|
309
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
334
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => Le.includes(t));
|
|
335
|
+
}
|
|
336
|
+
const me = /^\d+$/;
|
|
337
|
+
function h(e) {
|
|
338
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => me.test(t));
|
|
310
339
|
}
|
|
311
|
-
function
|
|
312
|
-
return
|
|
340
|
+
function De(e) {
|
|
341
|
+
return T(e) ? Object.values(e).flatMap((t) => h(t) ? Object.values(t) : [t]) : h(e) ? Object.values(e).flatMap((t) => T(t) ? Object.values(t) : [t]) : [e];
|
|
313
342
|
}
|
|
314
343
|
export {
|
|
315
|
-
|
|
344
|
+
ee as SHOULD_THROW_IF_INVALID_URL,
|
|
316
345
|
m as TradeType,
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
346
|
+
G as WIDGET_IFRAME_ALLOW,
|
|
347
|
+
x as WIDGET_IFRAME_REFERRER_POLICY,
|
|
348
|
+
$ as WIDGET_IFRAME_SANDBOX,
|
|
349
|
+
Se as WIDGET_PALETTE_COLORS,
|
|
350
|
+
f as WidgetHookEvents,
|
|
351
|
+
u as WidgetMethodsEmit,
|
|
352
|
+
A as WidgetMethodsListen,
|
|
353
|
+
Ce as createCowSwapWidget,
|
|
354
|
+
Z as isCowSwapWidgetPalette,
|
|
355
|
+
h as isPerNetworkConfig,
|
|
356
|
+
T as isPerTradeTypeConfig,
|
|
357
|
+
We as resolveFlexibleConfig,
|
|
358
|
+
De as resolveFlexibleConfigValues,
|
|
359
|
+
ne as sanitizeWidgetBaseUrl,
|
|
331
360
|
d as widgetIframeTransport
|
|
332
361
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@cowprotocol/cow-sdk": "9.1.
|
|
19
|
-
"@cowprotocol/events": "^4.
|
|
20
|
-
"@cowprotocol/iframe-transport": "^2.
|
|
18
|
+
"@cowprotocol/cow-sdk": "9.1.2",
|
|
19
|
+
"@cowprotocol/events": "^4.5.0",
|
|
20
|
+
"@cowprotocol/iframe-transport": "^2.3.0"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|