@arcblock/did-connect-react 3.3.9 → 3.3.10

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.
@@ -1,5 +1,5 @@
1
- import { useRef as I, useEffect as S } from "react";
2
- import W from "lodash/get";
1
+ import { useRef as I, useEffect as C } from "react";
2
+ import T from "lodash/get";
3
3
  import ue from "js-cookie";
4
4
  import B from "lodash/omitBy";
5
5
  import M from "lodash/isNil";
@@ -9,71 +9,63 @@ import { useReactive as H, useCreation as pe, useMemoizedFn as E } from "ahooks"
9
9
  import ke from "@arcblock/react-hooks/lib/useInterval";
10
10
  import me from "@arcblock/react-hooks/lib/useBrowser";
11
11
  import { getVisitorId as he, stringifyQuery as $, isUrl as ge } from "@arcblock/ux/lib/Util";
12
- import { WsClient as we } from "@arcblock/ws";
13
- import { sleep as q, getConnectedInfo as be, updateConnectedInfo as Ce, parseNextWorkflow as Q, createAxios as z, decodeConnectUrl as Se, parseTokenFromConnectUrl as xe } from "../../utils.js";
12
+ import { WsClient as be } from "@arcblock/ws";
13
+ import { sleep as q, getExtraHeaders as x, getConnectedInfo as we, updateConnectedInfo as Ce, parseNextWorkflow as Q, createAxios as z, decodeConnectUrl as Se, parseTokenFromConnectUrl as Ie } from "../../utils.js";
14
14
  import V from "../assets/locale.js";
15
- import Ie from "./security.js";
16
- import $e from "./page-show.js";
15
+ import $e from "./security.js";
16
+ import xe from "./page-show.js";
17
17
  import { RELAY_SOCKET_PREFIX as G } from "../../constant.js";
18
18
  function ve(o) {
19
- const a = o || window.location.href;
20
- return new URL(a).host;
19
+ const l = o || window.location.href;
20
+ return new URL(l).host;
21
21
  }
22
- function v(o) {
23
- const a = {}, d = o || window.location.href;
24
- if (d) {
25
- const { hostname: w, protocol: i, port: p } = new URL(d);
26
- a["x-real-hostname"] = w, a["x-real-port"] = p, a["x-real-protocol"] = i.endsWith(":") ? i.substring(0, i.length - 1) : i;
27
- }
28
- return a;
29
- }
30
- const R = {};
31
- function Ue({ action: o, prefix: a, checkFn: d, extraParams: w, baseUrl: i }) {
32
- const p = `${a}/token?${$(w)}`;
33
- return R[p] || (R[p] = async function() {
34
- const k = +/* @__PURE__ */ new Date(), h = await d(p, { headers: v(i) }), g = +/* @__PURE__ */ new Date();
35
- if (g - k < 500 && await q(500 - (g - k)), h?.data?.token)
22
+ const P = {};
23
+ function Ue({ action: o, prefix: l, checkFn: k, extraParams: v, baseUrl: m }) {
24
+ const d = `${l}/token?${$(v)}`;
25
+ return P[d] || (P[d] = async function() {
26
+ const f = +/* @__PURE__ */ new Date(), h = await k(d, { headers: x(m) }), g = +/* @__PURE__ */ new Date();
27
+ if (g - f < 500 && await q(500 - (g - f)), h?.data?.token)
36
28
  return h.data;
37
29
  if (h?.data?.error)
38
30
  throw new Error(h.data.error);
39
- const U = ge(a) ? a : `${i}${a}`;
31
+ const U = ge(l) ? l : `${m}${l}`;
40
32
  throw new Error(`Error generating ${o} QR Code from: ${U}`);
41
- }), R[p];
33
+ }), P[d];
42
34
  }
43
35
  const Ae = () => {
44
36
  try {
45
- const a = new URL(window.location.href).searchParams.get("__connect_url__");
46
- if (!a)
37
+ const l = new URL(window.location.href).searchParams.get("__connect_url__");
38
+ if (!l)
47
39
  return null;
48
- const d = Se(a);
40
+ const k = Se(l);
49
41
  return {
50
- token: xe(d),
51
- url: d
42
+ token: Ie(k),
43
+ url: k
52
44
  };
53
45
  } catch (o) {
54
46
  return {
55
47
  error: o
56
48
  };
57
49
  }
58
- }, J = () => W(globalThis, "blocklet.appPid") || W(globalThis, "blocklet.appId") || W(globalThis, "env.appId") || "", ye = () => (W(globalThis, "env.apiPrefix") || "/").replace(/\/$/, "").replace(G, ""), P = (o) => `relay:${J()}:${o}`, Ee = () => window.location.protocol === "https:" ? "wss:" : "ws:", X = (o) => !o || new URL(o).origin === window.location.origin, Y = (o) => ["succeed", "error", "timeout", "busy"].includes(o);
50
+ }, J = () => T(globalThis, "blocklet.appPid") || T(globalThis, "blocklet.appId") || T(globalThis, "env.appId") || "", ye = () => (T(globalThis, "env.apiPrefix") || "/").replace(/\/$/, "").replace(G, ""), R = (o) => `relay:${J()}:${o}`, Ee = () => window.location.protocol === "https:" ? "wss:" : "ws:", X = (o) => !o || new URL(o).origin === window.location.origin, Y = (o) => ["succeed", "error", "timeout", "busy"].includes(o);
59
51
  function Xe({
60
52
  action: o,
61
- checkFn: a,
62
- checkInterval: d,
63
- checkTimeout: w,
64
- extraParams: i,
65
- locale: p,
53
+ checkFn: l,
54
+ checkInterval: k,
55
+ checkTimeout: v,
56
+ extraParams: m,
57
+ locale: d,
66
58
  prefix: _,
67
- tokenKey: k,
59
+ tokenKey: f,
68
60
  encKey: h,
69
61
  onError: g,
70
62
  onSuccess: U,
71
63
  baseUrl: Z,
72
64
  autoConnect: K = !0,
73
65
  forceConnected: ee = !0,
74
- saveConnect: te = !0,
66
+ saveConnect: re = !0,
75
67
  // FIXME: @zhanghan 将来需要设置为默认 false,仅在需要的时候进行保存 login, switch/role/profile
76
- useSocket: re = !0,
68
+ useSocket: te = !0,
77
69
  provider: ne = "wallet"
78
70
  }) {
79
71
  const e = H({
@@ -94,41 +86,41 @@ function Xe({
94
86
  nextWorkflow: "",
95
87
  baseUrl: Z,
96
88
  prefix: `${_}/${o}`,
97
- extraParams: B(i, M),
98
- checkFn: a,
89
+ extraParams: B(m, M),
90
+ checkFn: l,
99
91
  results: {},
100
92
  action: o,
101
93
  provider: ne,
102
94
  reset() {
103
95
  this.error = "", this.status = "created";
104
96
  }
105
- }), F = I(null), x = V[p] || V.en, f = pe(() => Ae(), []), oe = Math.ceil(w / d), l = H({
97
+ }), F = I(null), S = V[d] || V.en, u = pe(() => Ae(), []), oe = Math.ceil(v / k), c = H({
106
98
  onSuccessCalled: !1,
107
99
  cancelWhenScannedCounter: 0,
108
100
  isSocketAvailable: !1
109
- }), { encryptKey: se, decrypt: A } = Ie(), D = me(), O = he(), c = I(null), m = I(null), b = {
101
+ }), { encryptKey: se, decrypt: A } = $e(), D = me(), O = he(), a = I(null), p = I(null), b = {
110
102
  ...e.extraParams,
111
- locale: p,
112
- [k]: e.token || f?.token,
103
+ locale: d,
104
+ [f]: e.token || u?.token,
113
105
  provider: e.provider
114
106
  }, L = async () => {
115
- if (!(f?.token && f?.token === e.token)) {
107
+ if (!(u?.token && u?.token === e.token)) {
116
108
  try {
117
- b[k] && e.status === "created" && await e.checkFn(`${e.prefix}/timeout?${$(b)}`, {
118
- headers: v(e.baseUrl)
109
+ b[f] && e.status === "created" && await e.checkFn(`${e.prefix}/timeout?${$(b)}`, {
110
+ headers: x(e.baseUrl)
119
111
  });
120
112
  } catch {
121
113
  }
122
114
  try {
123
- e.token && m.current && (await c.current.unsubscribe(P(e.token)), m.current = null);
115
+ e.token && p.current && (await a.current.unsubscribe(R(e.token)), p.current = null);
124
116
  } catch {
125
117
  }
126
118
  }
127
- }, ae = E((...r) => {
128
- const t = {
119
+ }, ae = E((...t) => {
120
+ const r = {
129
121
  [h]: se,
130
122
  ...e.extraParams,
131
- locale: p,
123
+ locale: d,
132
124
  forceConnected: ee,
133
125
  // - autoConnect 请求参数用于控制服务端是否给钱包应用发送自动连接通知
134
126
  // - 使用 connect 组件时明确传入了 autoConnect = false, 则 autoConnect 请求参数 为 false
@@ -136,97 +128,97 @@ function Xe({
136
128
  // (避免 "无限自动连接问题")
137
129
  // - 如果上次连接设备 (connected_wallet_os) 不是 ios/android, 则禁用自动连接
138
130
  autoConnect: K && // 如果是 wallet webview 环境, 不发送通知, 避免 wallet 连续弹出 auth 窗口 2 次 (#341)
139
- !(D.wallet || D.arcSphere) && !l.cancelWhenScannedCounter && ["ios", "android"].includes(ue.get("connected_wallet_os")),
131
+ !(D.wallet || D.arcSphere) && !c.cancelWhenScannedCounter && ["ios", "android"].includes(ue.get("connected_wallet_os")),
140
132
  provider: e.provider
141
133
  };
142
- O && (t.visitorId = O);
134
+ O && (r.visitorId = O);
143
135
  const n = fe(F.current || {}, ["sourceAppPid", "verbose"]);
144
136
  return Object.keys(n).forEach((s) => {
145
- (!t[s] || s === h && n[s]) && (t[s] = n[s]);
146
- }), f?.token && !t.sourceToken && (t.sourceToken = f?.token), Ue({
137
+ (!r[s] || s === h && n[s]) && (r[s] = n[s]);
138
+ }), u?.token && !r.sourceToken && (r.sourceToken = u?.token), Ue({
147
139
  action: o,
148
140
  prefix: e.prefix,
149
141
  baseUrl: e.baseUrl,
150
142
  checkFn: e.checkFn,
151
- extraParams: t
152
- })(...r);
153
- }), y = E(async (r = !0) => {
154
- r && L();
143
+ extraParams: r
144
+ })(...t);
145
+ }), y = E(async (t = !0) => {
146
+ t && L();
155
147
  try {
156
- e.loading = !0, e.token = "", e.url = "", e.store = null, e.extraParams = B(i, M), await q();
157
- const t = await ae(), n = t.extra || {};
158
- e.loading = !1, e.token = t.token, e.url = t.url, e.status = "created", e.error = "", e.checkCount = 0, e.appInfo = t.appInfo, e.memberAppInfo = t.memberAppInfo, e.connectedDid = n.connectedDid, e.saveConnect = n.saveConnect;
159
- } catch (t) {
160
- e.loading = !1, e.status = "error", e.error = `${x.generateError}: ${t.message}`;
148
+ e.loading = !0, e.token = "", e.url = "", e.store = null, e.extraParams = B(m, M), await q();
149
+ const r = await ae(), n = r.extra || {};
150
+ e.loading = !1, e.token = r.token, e.url = r.url, e.status = "created", e.error = "", e.checkCount = 0, e.appInfo = r.appInfo, e.memberAppInfo = r.memberAppInfo, e.connectedDid = n.connectedDid, e.saveConnect = n.saveConnect;
151
+ } catch (r) {
152
+ e.loading = !1, e.status = "error", e.error = `${S.generateError}: ${r.message}`;
161
153
  }
162
154
  }), ce = E(() => {
163
- l.cancelWhenScannedCounter++;
155
+ c.cancelWhenScannedCounter++;
164
156
  });
165
- S(() => {
166
- l.cancelWhenScannedCounter > 0 && (i.nw ? g(new Error(x.retryForbidden)) : y());
167
- }, [l.cancelWhenScannedCounter]), S(() => (!(/^((?!chrome|android).)*safari/i.test(globalThis.navigator.userAgent) || Object.hasOwn(globalThis?.blocklet || {}, "DID_CONNECT_DISABLE_SOCKET")) && re && X(e.baseUrl) && J() ? (!c.current || c.current.isConnected() === !1 || c.current.baseUrl !== e.baseUrl) && (c.current = new we(
157
+ C(() => {
158
+ c.cancelWhenScannedCounter > 0 && (m.nw ? g(new Error(S.retryForbidden)) : y());
159
+ }, [c.cancelWhenScannedCounter]), C(() => (!(/^((?!chrome|android).)*safari/i.test(globalThis.navigator.userAgent) || Object.hasOwn(globalThis?.blocklet || {}, "DID_CONNECT_DISABLE_SOCKET")) && te && X(e.baseUrl) && J() ? (!a.current || a.current.isConnected() === !1 || a.current.baseUrl !== e.baseUrl) && (a.current = new be(
168
160
  `${Ee()}//${ve(e.baseUrl)}${ye()}${G}/relay`,
169
161
  {
170
162
  longpollerTimeout: 5e3,
171
163
  // connection timeout
172
164
  heartbeatIntervalMs: 30 * 1e3
173
165
  }
174
- ), c.current.baseUrl = e.baseUrl, c.current.onOpen(() => {
175
- l.isSocketAvailable = !0;
176
- }), c.current.connect()) : l.isSocketAvailable && (l.isSocketAvailable = !1), () => {
177
- c.current && (c.current.disconnect(), c.current = null);
166
+ ), a.current.baseUrl = e.baseUrl, a.current.onOpen(() => {
167
+ c.isSocketAvailable = !0;
168
+ }), a.current.connect()) : c.isSocketAvailable && (c.isSocketAvailable = !1), () => {
169
+ a.current && (a.current.disconnect(), a.current = null);
178
170
  }), [e.baseUrl]);
179
- const ie = () => !e.token || e.loading || Y(e.status) ? null : d, j = I(null);
180
- S(() => {
171
+ const ie = () => !e.token || e.loading || Y(e.status) ? null : k, j = I(null);
172
+ C(() => {
181
173
  j.current = { state: e, params: b };
182
- }), S(() => () => {
183
- const { state: r, params: t } = j.current;
184
- r.status === "created" && t[k] && r.checkFn(`${r.prefix}/timeout?${$(t)}`, {
185
- headers: v(r.baseUrl)
174
+ }), C(() => () => {
175
+ const { state: t, params: r } = j.current;
176
+ t.status === "created" && r[f] && t.checkFn(`${t.prefix}/timeout?${$(r)}`, {
177
+ headers: x(t.baseUrl)
186
178
  });
187
179
  }, []);
188
- const C = E(async (r = !1) => {
189
- if ((e.checking || document.hidden) && !r || (e.status === "created" && e.checkCount++, l.isSocketAvailable && !r))
180
+ const w = E(async (t = !1) => {
181
+ if ((e.checking || document.hidden) && !t || (e.status === "created" && e.checkCount++, c.isSocketAvailable && !t))
190
182
  return null;
191
183
  try {
192
184
  e.checking = !0;
193
- const t = await e.checkFn(`${e.prefix}/status?${$(b)}`, {
194
- headers: v(e.baseUrl)
195
- }), { status: n, error: u, mfaCode: s = 0 } = t.data;
196
- if (e.store = t.data, e.mfaCode = s, e.checking = !1, e.status === "scanned" && n === "created" || (e.status = n), n === "error" && u) {
197
- const N = new Error(u);
198
- throw N.response = t, N;
185
+ const r = await e.checkFn(`${e.prefix}/status?${$(b)}`, {
186
+ headers: x(e.baseUrl)
187
+ }), { status: n, error: i, mfaCode: s = 0 } = r.data;
188
+ if (e.store = r.data, e.mfaCode = s, e.checking = !1, e.status === "scanned" && n === "created" || (e.status = n), n === "error" && i) {
189
+ const N = new Error(i);
190
+ throw N.response = r, N;
199
191
  }
200
- return t.data;
201
- } catch (t) {
202
- const { response: n } = t;
192
+ return r.data;
193
+ } catch (r) {
194
+ const { response: n } = r;
203
195
  if (n?.status) {
204
- b[k] && e.checkFn(`${e.prefix}/timeout?${$(b)}`, {
205
- headers: v(e.baseUrl)
196
+ b[f] && e.checkFn(`${e.prefix}/timeout?${$(b)}`, {
197
+ headers: x(e.baseUrl)
206
198
  });
207
- const u = n?.data?.error ? n.data.error : t.message, s = new Error(u);
208
- s.code = n.status, e.status = "error", e.checking = !1, e.error = u, g(s, e?.store, A);
199
+ const i = n?.data?.error ? n.data.error : r.message, s = new Error(i);
200
+ s.code = n.status, e.status = "error", e.checking = !1, e.error = i, g(s, e?.store, A);
209
201
  } else
210
- e.status = "error", e.checking = !1, e.error = x.generateError;
202
+ e.status = "error", e.checking = !1, e.error = S.generateError;
211
203
  }
212
204
  return null;
213
205
  }), le = async () => {
214
206
  try {
215
- if (f.error)
216
- throw f.error;
217
- e.loading = !0, e.token = f.token, e.url = f.url, e.inExistingSession = !0;
218
- let r = await C();
219
- if ((["scanned"].includes(r?.status) || r?.sourceToken === f?.token) && (await y(!1), r = await C()), F.current = r?.extraParams || null, r?.status && !["created"].includes(r?.status))
220
- throw new Error(`${x.invalidSessionStatus} [${r.status}]`);
221
- } catch (r) {
222
- e.status = "error", e.error = r.message, g(r, e?.store, A);
207
+ if (u.error)
208
+ throw u.error;
209
+ e.loading = !0, e.token = u.token, e.url = u.url, e.inExistingSession = !0;
210
+ let t = await w();
211
+ if ((["scanned"].includes(t?.status) || t?.sourceToken === u?.token) && (await y(!1), t = await w()), F.current = t?.extraParams || null, t?.status && !["created"].includes(t?.status))
212
+ throw new Error(`${S.invalidSessionStatus} [${t.status}]`);
213
+ } catch (t) {
214
+ e.status = "error", e.error = t.message, g(t, e?.store, A);
223
215
  } finally {
224
216
  e.loading = !1;
225
217
  }
226
218
  };
227
- S(() => {
219
+ C(() => {
228
220
  if (!e.token && !e.store && !e.loading && !e.error)
229
- if (f)
221
+ if (u)
230
222
  le();
231
223
  else {
232
224
  y(!1);
@@ -236,67 +228,67 @@ function Xe({
236
228
  e.status = "timeout", L();
237
229
  return;
238
230
  }
239
- if (e.status === "succeed" && !l.onSuccessCalled) {
240
- const r = be({
231
+ if (e.status === "succeed" && !c.onSuccessCalled) {
232
+ const t = we({
241
233
  ...e.store,
242
234
  appInfo: e.appInfo,
243
235
  memberAppInfo: e.memberAppInfo
244
236
  });
245
- if (X(e.baseUrl) && te && e.saveConnect && e.store.did && Ce(r, !0), e.store) {
246
- const { nextWorkflow: t } = e.store;
247
- if (t)
237
+ if (X(e.baseUrl) && re && e.saveConnect && e.store.did && Ce(t, !0), e.store) {
238
+ const { nextWorkflow: r } = e.store;
239
+ if (r)
248
240
  try {
249
- const n = Q(t, k);
250
- e.nextWorkflow = n?.nextWorkflow || t, Object.assign(e, n), e.store = null, e.error = "", e.url = "", e.appInfo = null, e.memberAppInfo = null, e.checkCount = 0, e.status = "scanned", e.results = {
241
+ const n = Q(r, f);
242
+ e.nextWorkflow = n?.nextWorkflow || r, Object.assign(e, n), e.store = null, e.error = "", e.url = "", e.appInfo = null, e.memberAppInfo = null, e.checkCount = 0, e.status = "scanned", e.results = {
251
243
  ...e.results,
252
244
  [e.token]: e.store
253
- }, e.checkFn = z({ baseURL: n.baseUrl, timeout: 8e3 }).get, C();
245
+ }, e.checkFn = z({ baseURL: n.baseUrl, timeout: 8e3 }).get, w();
254
246
  } catch (n) {
255
- console.error(`Invalid nextWorkflow: ${t}`, n), e.status = "error", e.error = `Invalid nextWorkflow: ${t}: ${n.message}`;
247
+ console.error(`Invalid nextWorkflow: ${r}`, n), e.status = "error", e.error = `Invalid nextWorkflow: ${r}: ${n.message}`;
256
248
  }
257
249
  else if (e.nextWorkflow && (e.nextWorkflow = "", e.url = ""), typeof U == "function") {
258
250
  const n = Object.values({ ...e.results, [e.token]: e.store });
259
- l.onSuccessCalled = !0;
260
- const u = n.length > 1 ? n : n[0], s = de(u);
251
+ c.onSuccessCalled = !0;
252
+ const i = n.length > 1 ? n : n[0], s = de(i);
261
253
  U(s, A, {
262
- sourceAppPid: i?.sourceAppPid,
263
- ...r
254
+ sourceAppPid: m?.sourceAppPid,
255
+ ...t
264
256
  });
265
257
  }
266
258
  }
267
259
  return;
268
260
  }
269
261
  if (e.store?.nextWorkflow) {
270
- const { nextWorkflow: r } = e.store;
262
+ const { nextWorkflow: t } = e.store;
271
263
  try {
272
- const t = Q(r, k);
273
- e.nextWorkflow = t?.nextWorkflow || r, Object.assign(e, t), e.store = null, e.error = "", e.url = "", e.appInfo = null, e.memberAppInfo = null, e.checkCount = 0, e.status = "scanned", e.results = {
264
+ const r = Q(t, f);
265
+ e.nextWorkflow = r?.nextWorkflow || t, Object.assign(e, r), e.store = null, e.error = "", e.url = "", e.appInfo = null, e.memberAppInfo = null, e.checkCount = 0, e.status = "scanned", e.results = {
274
266
  ...e.results,
275
267
  [e.token]: e.store
276
- }, e.checkFn = z({ baseURL: t.baseUrl, timeout: 8e3 }).get, C();
277
- } catch (t) {
278
- console.error(`Invalid nextWorkflow: ${r}`, t), e.status = "error", e.error = `Invalid nextWorkflow: ${r}: ${t.message}`;
268
+ }, e.checkFn = z({ baseURL: r.baseUrl, timeout: 8e3 }).get, w();
269
+ } catch (r) {
270
+ console.error(`Invalid nextWorkflow: ${t}`, r), e.status = "error", e.error = `Invalid nextWorkflow: ${t}: ${r.message}`;
279
271
  }
280
272
  }
281
273
  });
282
- const T = I(0);
283
- return S(() => {
284
- if (e.token && l.isSocketAvailable && c.current) {
285
- let r = !1;
286
- m.current ? m.current.token !== e.token && (c.current.unsubscribe(P(m.current.token)), r = !0) : r = !0, r && (m.current = c.current.subscribe(P(e.token)), T.current = 0, m.current.token = e.token, m.current.on("updated", ({ response: t }) => {
287
- const n = +new Date(t.updatedAt);
288
- if (n <= T.current) {
289
- console.warn("Ignore outdated message", t);
274
+ const W = I(0);
275
+ return C(() => {
276
+ if (e.token && c.isSocketAvailable && a.current) {
277
+ let t = !1;
278
+ p.current ? p.current.token !== e.token && (a.current.unsubscribe(R(p.current.token)), t = !0) : t = !0, t && (p.current = a.current.subscribe(R(e.token)), W.current = 0, p.current.token = e.token, p.current.on("updated", ({ response: r }) => {
279
+ const n = +new Date(r.updatedAt);
280
+ if (n <= W.current) {
281
+ console.warn("Ignore outdated message", r);
290
282
  return;
291
283
  }
292
- T.current = n;
293
- let { status: u, error: s } = t;
294
- u === "forbidden" && (s = x.forbidden, u = "error"), e.status = u, e.mfaCode = t.mfaCode || 0, e.store = t, e.error = s, s && g(new Error(s), e?.store, A);
284
+ W.current = n;
285
+ let { status: i, error: s } = r;
286
+ i === "forbidden" && (s = S.forbidden, i = "error"), e.status = i, e.mfaCode = r.mfaCode || 0, e.store = r, e.error = s, s && g(new Error(s), e?.store, A);
295
287
  }));
296
288
  }
297
- }, [e.token, l.isSocketAvailable, c.current]), ke(C, ie()), $e(() => {
289
+ }, [e.token, c.isSocketAvailable, a.current]), ke(w, ie()), xe(() => {
298
290
  e.token && Y(e.status) === !1 && (e.checking = !1, setTimeout(() => {
299
- C(!0);
291
+ w(!0);
300
292
  }, 100));
301
293
  }), { state: e, generate: y, cancelWhenScanned: ce };
302
294
  }
package/lib/index.js CHANGED
@@ -1,41 +1,41 @@
1
1
  import { default as t, default as r, default as s } from "./Connect/index.js";
2
2
  import { default as n, default as d } from "./Button/index.js";
3
3
  import { default as f, default as p } from "@arcblock/ux/lib/DIDLogo";
4
- import { createSessionContext as _, createAuthServiceSessionContext as m, createSessionContext as x } from "./Session/index.js";
4
+ import { createSessionContext as _, createAuthServiceSessionContext as x, createSessionContext as m } from "./Session/index.js";
5
5
  import { OAuthConsumer as C, OAuthContext as T, OAuthProvider as i, useOAuth as I } from "./OAuth/context.js";
6
- import { PasskeyConsumer as S, PasskeyContext as c, PasskeyProvider as O, usePasskey as D } from "./Passkey/context.js";
6
+ import { PasskeyConsumer as S, PasskeyContext as c, PasskeyProvider as O, usePasskey as g } from "./Passkey/context.js";
7
7
  import { default as R } from "./Passkey/actions.js";
8
8
  import { default as N } from "./User/use-did.js";
9
9
  import { default as F } from "./User/wrap-did.js";
10
10
  import { FederatedConsumer as K, FederatedContext as M, FederatedProvider as y, useFederatedContext as h } from "./Federated/context.js";
11
- import { API_DID_PREFIX as V, BLOCKLET_SERVICE_PATH_PREFIX as v, BUSY_STATUS as b, CHECK_INTERVAL_TIME as B, CHECK_STATUS_INTERVAL as W, CSRF_TOKEN_STORAGE_KEY as Y, DEFAULT_NAME as G, DEFAULT_TIMEOUT as H, DEFAULT_WINDOW_TIMEOUT as X, DID_CONNECT_URL_PARAMS_NAME as w, DID_SPACES_BASE_URL as q, EXT_DOWNLOAD_URL as j, LANG_COOKIE_NAME as z, REFRESH_TOKEN_STORAGE_KEY as J, RELAY_SOCKET_PREFIX as Q, SESSION_TOKEN_STORAGE_KEY as Z, VERIFY_CODE_LENGTH as $ } from "./constant.js";
12
- import { createAxios as oe, debug as te, debugTmp as re, debugTmpInterval as se, decodeConnectUrl as ae, decodeKey as ne, decodeUrlParams as de, decrypt as Ee, encodeConnectUrl as fe, encodeKey as pe, encrypt as ue, formatCacheTtl as _e, getApiErrorMessage as me, getAppId as xe, getBrowserLang as Ae, getConnectedInfo as Ce, getWebAuthnErrorMessage as Te, logger as ie, openPopup as Ie, parseNextWorkflow as le, parseTokenFromConnectUrl as Se, runPopup as ce, sleep as Oe, updateConnectedInfo as De, version as ge } from "./utils.js";
13
- import { NotOpenError as Le } from "./error.js";
14
- import { default as Pe } from "@arcblock/ux/lib/SessionManager";
15
- import { default as Ue } from "@arcblock/ux/lib/Address";
16
- import { default as Me } from "@arcblock/ux/lib/Avatar";
17
- import { default as he } from "./Connect/use-connect.js";
18
- import { EVENTS as Ve } from "./Session/libs/constants.js";
19
- import { didSpacesIsRequired as be } from "./Session/libs/did-spaces.js";
11
+ import { API_DID_PREFIX as V, BLOCKLET_SERVICE_PATH_PREFIX as v, BUSY_STATUS as b, CHECK_INTERVAL_TIME as B, CHECK_STATUS_INTERVAL as H, CSRF_TOKEN_STORAGE_KEY as W, DEFAULT_NAME as Y, DEFAULT_TIMEOUT as G, DEFAULT_WINDOW_TIMEOUT as X, DID_CONNECT_URL_PARAMS_NAME as w, DID_SPACES_BASE_URL as q, EXT_DOWNLOAD_URL as j, LANG_COOKIE_NAME as z, REFRESH_TOKEN_STORAGE_KEY as J, RELAY_SOCKET_PREFIX as Q, SESSION_TOKEN_STORAGE_KEY as Z, VERIFY_CODE_LENGTH as $ } from "./constant.js";
12
+ import { createAxios as oe, debug as te, debugTmp as re, debugTmpInterval as se, decodeConnectUrl as ae, decodeKey as ne, decodeUrlParams as de, decrypt as Ee, encodeConnectUrl as fe, encodeKey as pe, encrypt as ue, formatCacheTtl as _e, getApiErrorMessage as xe, getAppId as me, getBrowserLang as Ae, getConnectedInfo as Ce, getExtraHeaders as Te, getWebAuthnErrorMessage as ie, logger as Ie, openPopup as le, parseNextWorkflow as Se, parseTokenFromConnectUrl as ce, runPopup as Oe, sleep as ge, updateConnectedInfo as De, version as Re } from "./utils.js";
13
+ import { NotOpenError as Ne } from "./error.js";
14
+ import { default as Fe } from "@arcblock/ux/lib/SessionManager";
15
+ import { default as Ke } from "@arcblock/ux/lib/Address";
16
+ import { default as ye } from "@arcblock/ux/lib/Avatar";
17
+ import { default as ke } from "./Connect/use-connect.js";
18
+ import { EVENTS as ve } from "./Session/libs/constants.js";
19
+ import { didSpacesIsRequired as Be } from "./Session/libs/did-spaces.js";
20
20
  import { loginFederatedMaster as We, mountFederatedIframe as Ye } from "./Session/libs/federated.js";
21
- import { translations as He } from "./Session/libs/locales.js";
22
- import { checkEnableAutoLogin as we, getMobileVisitorId as qe, login as je } from "./Session/libs/login-mobile.js";
23
- import { SessionContext as Je } from "./Session/context.js";
21
+ import { translations as Xe } from "./Session/libs/locales.js";
22
+ import { checkEnableAutoLogin as qe, getMobileVisitorId as je, login as ze } from "./Session/libs/login-mobile.js";
23
+ import { SessionContext as Qe } from "./Session/context.js";
24
24
  export * from "@arcblock/ux/lib/Util/wallet";
25
- import { getVisitorId as $e, setVisitorId as eo } from "@arcblock/ux/lib/Util";
25
+ import { getVisitorId as eo, setVisitorId as oo } from "@arcblock/ux/lib/Util";
26
26
  export {
27
27
  V as API_DID_PREFIX,
28
- Ue as Address,
29
- Me as Avatar,
28
+ Ke as Address,
29
+ ye as Avatar,
30
30
  v as BLOCKLET_SERVICE_PATH_PREFIX,
31
31
  b as BUSY_STATUS,
32
32
  n as Button,
33
33
  B as CHECK_INTERVAL_TIME,
34
- W as CHECK_STATUS_INTERVAL,
35
- Y as CSRF_TOKEN_STORAGE_KEY,
34
+ H as CHECK_STATUS_INTERVAL,
35
+ W as CSRF_TOKEN_STORAGE_KEY,
36
36
  t as Connect,
37
- G as DEFAULT_NAME,
38
- H as DEFAULT_TIMEOUT,
37
+ Y as DEFAULT_NAME,
38
+ G as DEFAULT_TIMEOUT,
39
39
  X as DEFAULT_WINDOW_TIMEOUT,
40
40
  d as DIDButton,
41
41
  r as DIDConnect,
@@ -43,14 +43,14 @@ export {
43
43
  w as DID_CONNECT_URL_PARAMS_NAME,
44
44
  q as DID_SPACES_BASE_URL,
45
45
  s as DidConnect,
46
- Ve as EVENTS,
46
+ ve as EVENTS,
47
47
  j as EXT_DOWNLOAD_URL,
48
48
  K as FederatedConsumer,
49
49
  M as FederatedContext,
50
50
  y as FederatedProvider,
51
51
  z as LANG_COOKIE_NAME,
52
52
  p as Logo,
53
- Le as NotOpenError,
53
+ Ne as NotOpenError,
54
54
  C as OAuthConsumer,
55
55
  T as OAuthContext,
56
56
  i as OAuthProvider,
@@ -62,14 +62,14 @@ export {
62
62
  Q as RELAY_SOCKET_PREFIX,
63
63
  Z as SESSION_TOKEN_STORAGE_KEY,
64
64
  _ as Session,
65
- Je as SessionContext,
66
- Pe as SessionManager,
65
+ Qe as SessionContext,
66
+ Fe as SessionManager,
67
67
  $ as VERIFY_CODE_LENGTH,
68
68
  F as WrapDid,
69
- we as checkEnableAutoLogin,
70
- m as createAuthServiceSessionContext,
69
+ qe as checkEnableAutoLogin,
70
+ x as createAuthServiceSessionContext,
71
71
  oe as createAxios,
72
- x as createSessionContext,
72
+ m as createSessionContext,
73
73
  te as debug,
74
74
  re as debugTmp,
75
75
  se as debugTmpInterval,
@@ -77,34 +77,35 @@ export {
77
77
  ne as decodeKey,
78
78
  de as decodeUrlParams,
79
79
  Ee as decrypt,
80
- be as didSpacesIsRequired,
80
+ Be as didSpacesIsRequired,
81
81
  fe as encodeConnectUrl,
82
82
  pe as encodeKey,
83
83
  ue as encrypt,
84
84
  _e as formatCacheTtl,
85
- me as getApiErrorMessage,
86
- xe as getAppId,
85
+ xe as getApiErrorMessage,
86
+ me as getAppId,
87
87
  Ae as getBrowserLang,
88
88
  Ce as getConnectedInfo,
89
- qe as getMobileVisitorId,
90
- $e as getVisitorId,
91
- Te as getWebAuthnErrorMessage,
92
- ie as logger,
93
- je as login,
89
+ Te as getExtraHeaders,
90
+ je as getMobileVisitorId,
91
+ eo as getVisitorId,
92
+ ie as getWebAuthnErrorMessage,
93
+ Ie as logger,
94
+ ze as login,
94
95
  We as loginFederatedMaster,
95
96
  Ye as mountFederatedIframe,
96
- Ie as openPopup,
97
- le as parseNextWorkflow,
98
- Se as parseTokenFromConnectUrl,
99
- ce as runPopup,
100
- eo as setVisitorId,
101
- Oe as sleep,
102
- He as translations,
97
+ le as openPopup,
98
+ Se as parseNextWorkflow,
99
+ ce as parseTokenFromConnectUrl,
100
+ Oe as runPopup,
101
+ oo as setVisitorId,
102
+ ge as sleep,
103
+ Xe as translations,
103
104
  De as updateConnectedInfo,
104
- he as useConnect,
105
+ ke as useConnect,
105
106
  N as useDid,
106
107
  h as useFederatedContext,
107
108
  I as useOAuth,
108
- D as usePasskey,
109
- ge as version
109
+ g as usePasskey,
110
+ Re as version
110
111
  };
@@ -1,4 +1,4 @@
1
- const o = "3.3.9", s = {
1
+ const o = "3.3.10", s = {
2
2
  version: o
3
3
  };
4
4
  export {
package/lib/utils.js CHANGED
@@ -1,31 +1,31 @@
1
1
  import { Buffer as a } from "buffer";
2
2
  import h from "tweetnacl-sealedbox-js";
3
- import { getCookieOptions as A, getVisitorId as U } from "@arcblock/ux/lib/Util";
4
- import { getVisitorId as ke, setVisitorId as ye } from "@arcblock/ux/lib/Util";
3
+ import { isUrl as U, getCookieOptions as A, getVisitorId as E } from "@arcblock/ux/lib/Util";
4
+ import { getVisitorId as Ue, setVisitorId as Ae } from "@arcblock/ux/lib/Util";
5
5
  import c from "js-cookie";
6
- import d from "lodash/isNil";
6
+ import l from "lodash/isNil";
7
7
  import T from "lodash/isString";
8
8
  import m from "lodash/isUndefined";
9
- import { createAxios as E, BlockletSDK as P } from "@blocklet/js-sdk";
9
+ import { createAxios as P, BlockletSDK as x } from "@blocklet/js-sdk";
10
10
  import { withQuery as L } from "ufo";
11
11
  import { isValid as w, isFromPublicKey as S } from "@arcblock/did";
12
- import { getSafeUrl as x } from "@arcblock/ux/lib/Util/security";
13
- import { createDebug as u, createLogger as C } from "@arcblock/ux/lib/Util/logger";
14
- import I from "./package.json.js";
15
- import { DID_CONNECT_URL_PARAMS_NAME as f, DEFAULT_WINDOW_TIMEOUT as N } from "./constant.js";
16
- import { NotOpenError as D } from "./error.js";
12
+ import { getSafeUrl as C } from "@arcblock/ux/lib/Util/security";
13
+ import { createDebug as u, createLogger as I } from "@arcblock/ux/lib/Util/logger";
14
+ import N from "./package.json.js";
15
+ import { DID_CONNECT_URL_PARAMS_NAME as f, DEFAULT_WINDOW_TIMEOUT as D } from "./constant.js";
16
+ import { NotOpenError as W } from "./error.js";
17
17
  export * from "@arcblock/ux/lib/Util/wallet";
18
- const { version: O } = I, Z = u("did-connect"), ee = u("did-connect:tmp"), re = u("did-connect:tmp-interval"), oe = C("did-connect");
18
+ const { version: O } = N, re = u("did-connect"), oe = u("did-connect:tmp"), te = u("did-connect:tmp-interval"), ne = I("did-connect");
19
19
  function b(e) {
20
20
  return (e + "===".slice((e.length + 3) % 4)).replace(/-/g, "+").replace(/_/g, "/");
21
21
  }
22
22
  function _(e) {
23
23
  return e.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
24
24
  }
25
- const ne = (e) => _(window.btoa(e)), te = (e) => window.atob(b(e)), se = (e) => {
25
+ const se = (e) => _(window.btoa(e)), ie = (e) => window.atob(b(e)), ae = (e) => {
26
26
  const o = new URL(e), r = decodeURIComponent(o.searchParams.get("url"));
27
27
  return new URL(r).searchParams.get("_t_");
28
- }, W = (e, o) => {
28
+ }, R = (e, o) => {
29
29
  let r = o?.publisher;
30
30
  if (!r) {
31
31
  if (e?.agentDid)
@@ -37,40 +37,40 @@ const ne = (e) => _(window.btoa(e)), te = (e) => window.atob(b(e)), se = (e) =>
37
37
  const o = {
38
38
  connected_did: e.did || "",
39
39
  connected_pk: e.pk || "",
40
- connected_app: W(e.appInfo, e.memberAppInfo)
40
+ connected_app: R(e.appInfo, e.memberAppInfo)
41
41
  };
42
42
  return e?.connectedWallet?.os && (o.connected_wallet_os = e.connectedWallet.os), o;
43
- }, ie = (e, o = !1) => {
43
+ }, ce = (e, o = !1) => {
44
44
  if (!e) return;
45
45
  o || (e = z(e));
46
46
  const r = A({
47
47
  expireInDays: 7,
48
48
  returnDomain: !1
49
49
  });
50
- !d(e.connected_did) && !d(e.connected_pk) && w(e.connected_did) && S(e.connected_did, e.connected_pk) && (c.set("connected_did", e.connected_did, r), c.set("connected_pk", e.connected_pk, r)), !d(e.connected_app) && w(e.connected_app) && c.set("connected_app", e.connected_app, r), e.connected_wallet_os && c.set("connected_wallet_os", e.connected_wallet_os, r);
51
- }, ae = (e) => _(a.from(e).toString("base64")), l = (e) => Uint8Array.from(a.from(b(e), "base64")), ce = (e, o, r) => {
52
- const n = h.open(
50
+ !l(e.connected_did) && !l(e.connected_pk) && w(e.connected_did) && S(e.connected_did, e.connected_pk) && (c.set("connected_did", e.connected_did, r), c.set("connected_pk", e.connected_pk, r)), !l(e.connected_app) && w(e.connected_app) && c.set("connected_app", e.connected_app, r), e.connected_wallet_os && c.set("connected_wallet_os", e.connected_wallet_os, r);
51
+ }, pe = (e) => _(a.from(e).toString("base64")), d = (e) => Uint8Array.from(a.from(b(e), "base64")), le = (e, o, r) => {
52
+ const t = h.open(
53
53
  Uint8Array.from(a.from(e, "base64")),
54
- l(o),
55
- l(r)
54
+ d(o),
55
+ d(r)
56
56
  );
57
- return JSON.parse(a.from(n).toString("utf8"));
58
- }, pe = (e, o) => a.from(h.seal(Uint8Array.from(a.from(e)), l(o))).toString("base64"), de = (e, o = "_t_") => {
59
- const r = new URL(e), n = {
57
+ return JSON.parse(a.from(t).toString("utf8"));
58
+ }, de = (e, o) => a.from(h.seal(Uint8Array.from(a.from(e)), d(o))).toString("base64"), ue = (e, o = "_t_") => {
59
+ const r = new URL(e), t = {
60
60
  baseUrl: r.origin,
61
61
  prefix: r.pathname.replace(/\/auth$/, ""),
62
62
  token: r.searchParams.get(o) || ""
63
63
  };
64
- if (!n.baseUrl)
64
+ if (!t.baseUrl)
65
65
  throw new Error("Invalid next workflow: origin empty");
66
- if (!n.token)
66
+ if (!t.token)
67
67
  throw new Error("Invalid next workflow: token not found");
68
- return n;
68
+ return t;
69
69
  }, g = (e, o) => {
70
70
  let r = e.message;
71
- const { response: n } = e;
72
- return n && (r = n.data?.error || n.data?.message || n.data || o, r = typeof r == "object" ? o : r), r;
73
- }, le = (e, o, r) => {
71
+ const { response: t } = e;
72
+ return t && (r = t.data?.error || t.data?.message || t.data || o, r = typeof r == "object" ? o : r), r;
73
+ }, me = (e, o, r) => {
74
74
  if (!e)
75
75
  return e;
76
76
  if (!e.name)
@@ -87,27 +87,35 @@ const ne = (e) => _(window.btoa(e)), te = (e) => window.atob(b(e)), se = (e) =>
87
87
  default:
88
88
  return console.warn("Unhandled WebAuthn error type:", e.name), g(e, o);
89
89
  }
90
- }, ue = (e = {}, { lazy: o = !1, lazyTime: r = 300 } = {}) => {
91
- const n = {
90
+ }, M = (e) => {
91
+ const o = {}, r = U(e) ? e : window.location.href;
92
+ if (r) {
93
+ const { hostname: t, protocol: n, port: s } = new URL(r);
94
+ o["x-real-hostname"] = t, o["x-real-port"] = s, o["x-real-protocol"] = n.endsWith(":") ? n.substring(0, n.length - 1) : n;
95
+ }
96
+ return o;
97
+ }, we = (e = {}, { lazy: o = !1, lazyTime: r = 300 } = {}) => {
98
+ const t = {
99
+ ...M(e.baseURL),
92
100
  ...e.headers,
93
101
  "x-did-connect-version": O
94
- }, i = E(
102
+ }, n = P(
95
103
  {
96
104
  ...e,
97
- headers: n
105
+ headers: t
98
106
  },
99
107
  { lazy: o, lazyTime: r }
100
108
  );
101
- return i.interceptors.request.use((t) => {
102
- const s = U();
103
- return s && (t.headers["x-blocklet-visitor-id"] = s), t;
104
- }), i;
105
- }, me = (e = 0) => new Promise((o) => {
109
+ return n.interceptors.request.use((s) => {
110
+ const i = E();
111
+ return i && (s.headers["x-blocklet-visitor-id"] = i), s;
112
+ }), n;
113
+ }, fe = (e = 0) => new Promise((o) => {
106
114
  setTimeout(() => {
107
115
  o();
108
116
  }, e);
109
117
  });
110
- function we() {
118
+ function ge() {
111
119
  if (typeof window > "u")
112
120
  return null;
113
121
  const e = window.navigator.languages && window.navigator.languages[0] || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage || window.navigator.systemLanguage || null;
@@ -118,22 +126,22 @@ function we() {
118
126
  en: "en"
119
127
  }[e] || "en";
120
128
  }
121
- function fe(e, o) {
129
+ function he(e, o) {
122
130
  return [void 0, null, ""].includes(e) ? o : e / 86400;
123
131
  }
124
- async function ge() {
132
+ async function be() {
125
133
  const e = window.location.href;
126
134
  try {
127
135
  const o = new URL(e), r = o.searchParams.get(f);
128
136
  if (r) {
129
137
  o.searchParams.delete(f);
130
- const n = JSON.parse(a.from(r, "base64").toString("utf-8"));
131
- if (T(n.forceConnected) && m(n.sourceAppPid)) {
132
- const t = await new P().user.getUserPublicInfo({ did: n.forceConnected });
133
- m(t?.sourceAppPid) || (n.sourceAppPid = t.sourceAppPid);
138
+ const t = JSON.parse(a.from(r, "base64").toString("utf-8"));
139
+ if (T(t.forceConnected) && m(t.sourceAppPid)) {
140
+ const s = await new x().user.getUserPublicInfo({ did: t.forceConnected });
141
+ m(s?.sourceAppPid) || (t.sourceAppPid = s.sourceAppPid);
134
142
  }
135
143
  return {
136
- params: n,
144
+ params: t,
137
145
  url: o.href
138
146
  };
139
147
  }
@@ -145,8 +153,8 @@ async function ge() {
145
153
  url: e
146
154
  };
147
155
  }
148
- const he = (e, { width: o = 680, height: r = 720, name: n = "did-connect:popup", offsetX: i = 0, offsetY: t = 0 } = {}) => {
149
- const s = x(e, { allowDomains: null }), v = window.screenX + (window.innerWidth - o) / 2 + i, k = window.screenY + (window.innerHeight - r) / 2 + t, y = [
156
+ const _e = (e, { width: o = 680, height: r = 720, name: t = "did-connect:popup", offsetX: n = 0, offsetY: s = 0 } = {}) => {
157
+ const i = C(e, { allowDomains: null }), v = window.screenX + (window.innerWidth - o) / 2 + n, k = window.screenY + (window.innerHeight - r) / 2 + s, y = [
150
158
  `left=${v}`,
151
159
  `top=${k}`,
152
160
  `width=${o}`,
@@ -156,61 +164,62 @@ const he = (e, { width: o = 680, height: r = 720, name: n = "did-connect:popup",
156
164
  "scrollbars=yes",
157
165
  "status=yes",
158
166
  "popup=yes"
159
- ], p = window.open("", n, y.join(","));
167
+ ], p = window.open("", t, y.join(","));
160
168
  if (p === null)
161
- throw new D();
162
- return p.location.href = L(s, {
169
+ throw new W();
170
+ return p.location.href = L(i, {
163
171
  // NOTICE: 携带当前页面的 origin,用于在 popup 中通过该参数判断是否可以发送 postMessage,即使该参数被伪造,最终也只有该域名能接收到消息,所以没有关系
164
172
  opener: window.location.origin
165
173
  }), p;
166
- }, be = (e) => new Promise((o, r) => {
167
- let n, i;
168
- const t = ({ data: s }) => {
169
- if (s && s.type === "authorization_response") {
170
- if (s?.error) {
171
- console.error(s.error);
174
+ }, ve = (e) => new Promise((o, r) => {
175
+ let t, n;
176
+ const s = ({ data: i }) => {
177
+ if (i && i.type === "authorization_response") {
178
+ if (i?.error) {
179
+ console.error(i.error);
172
180
  return;
173
181
  }
174
182
  setTimeout(() => {
175
- e.popup?.close(), clearTimeout(i), clearInterval(n), window.removeEventListener("message", t, !1);
176
- }, e.closeTimeout || 0), o(s);
183
+ e.popup?.close(), clearTimeout(n), clearInterval(t), window.removeEventListener("message", s, !1);
184
+ }, e.closeTimeout || 0), o(i);
177
185
  }
178
186
  };
179
- i = setTimeout(
187
+ n = setTimeout(
180
188
  () => {
181
- clearInterval(n), r(new Error("Timeout")), window.removeEventListener("message", t, !1);
189
+ clearInterval(t), r(new Error("Timeout")), window.removeEventListener("message", s, !1);
182
190
  },
183
- (e.timeoutInSeconds || N) * 1e3
184
- ), n = setInterval(() => {
185
- e.popup?.closed && (clearInterval(n), clearTimeout(i), window.removeEventListener("message", t, !1), r(new Error("Popup closed")));
186
- }, 1e3), window.addEventListener("message", t);
191
+ (e.timeoutInSeconds || D) * 1e3
192
+ ), t = setInterval(() => {
193
+ e.popup?.closed && (clearInterval(t), clearTimeout(n), window.removeEventListener("message", s, !1), r(new Error("Popup closed")));
194
+ }, 1e3), window.addEventListener("message", s);
187
195
  });
188
196
  export {
189
- ue as createAxios,
190
- Z as debug,
191
- ee as debugTmp,
192
- re as debugTmpInterval,
193
- te as decodeConnectUrl,
194
- l as decodeKey,
195
- ge as decodeUrlParams,
196
- ce as decrypt,
197
- ne as encodeConnectUrl,
198
- ae as encodeKey,
199
- pe as encrypt,
200
- fe as formatCacheTtl,
197
+ we as createAxios,
198
+ re as debug,
199
+ oe as debugTmp,
200
+ te as debugTmpInterval,
201
+ ie as decodeConnectUrl,
202
+ d as decodeKey,
203
+ be as decodeUrlParams,
204
+ le as decrypt,
205
+ se as encodeConnectUrl,
206
+ pe as encodeKey,
207
+ de as encrypt,
208
+ he as formatCacheTtl,
201
209
  g as getApiErrorMessage,
202
- W as getAppId,
203
- we as getBrowserLang,
210
+ R as getAppId,
211
+ ge as getBrowserLang,
204
212
  z as getConnectedInfo,
205
- ke as getVisitorId,
206
- le as getWebAuthnErrorMessage,
207
- oe as logger,
208
- he as openPopup,
209
- de as parseNextWorkflow,
210
- se as parseTokenFromConnectUrl,
211
- be as runPopup,
212
- ye as setVisitorId,
213
- me as sleep,
214
- ie as updateConnectedInfo,
213
+ M as getExtraHeaders,
214
+ Ue as getVisitorId,
215
+ me as getWebAuthnErrorMessage,
216
+ ne as logger,
217
+ _e as openPopup,
218
+ ue as parseNextWorkflow,
219
+ ae as parseTokenFromConnectUrl,
220
+ ve as runPopup,
221
+ Ae as setVisitorId,
222
+ fe as sleep,
223
+ ce as updateConnectedInfo,
215
224
  O as version
216
225
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/did-connect-react",
3
- "version": "3.3.9",
3
+ "version": "3.3.10",
4
4
  "description": "Client side library to work with DID Connect by ArcBlock.",
5
5
  "keywords": [
6
6
  "react",
@@ -32,10 +32,10 @@
32
32
  "url": "https://github.com/ArcBlock/ux/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@arcblock/bridge": "3.3.9",
35
+ "@arcblock/bridge": "3.3.10",
36
36
  "@arcblock/did": "^1.27.16",
37
- "@arcblock/icons": "3.3.9",
38
- "@arcblock/react-hooks": "3.3.9",
37
+ "@arcblock/icons": "3.3.10",
38
+ "@arcblock/react-hooks": "3.3.10",
39
39
  "@arcblock/ws": "^1.27.16",
40
40
  "@blocklet/constant": "^1.17.7",
41
41
  "@fontsource/lexend": "^5.2.9",
@@ -81,5 +81,5 @@
81
81
  "eslint-plugin-react-hooks": "^4.6.2",
82
82
  "jest": "^29.7.0"
83
83
  },
84
- "gitHead": "c7a122040ee66c92cb979d185ad4a30cb80ef3fa"
84
+ "gitHead": "6c98492b766cb7f7486f3a81f3d4e4b0936af163"
85
85
  }
@@ -19,6 +19,7 @@ import {
19
19
  createAxios,
20
20
  sleep,
21
21
  getConnectedInfo,
22
+ getExtraHeaders,
22
23
  } from '../../utils';
23
24
 
24
25
  import translations from '../assets/locale';
@@ -31,19 +32,6 @@ function getSocketHost(baseUrl) {
31
32
  return new URL(targetUrl).host;
32
33
  }
33
34
 
34
- function getExtraHeaders(baseUrl) {
35
- const headers = {};
36
- const authUrl = baseUrl || window.location.href;
37
-
38
- if (authUrl) {
39
- const { hostname, protocol, port } = new URL(authUrl);
40
- headers['x-real-hostname'] = hostname;
41
- headers['x-real-port'] = port;
42
- headers['x-real-protocol'] = protocol.endsWith(':') ? protocol.substring(0, protocol.length - 1) : protocol;
43
- }
44
- return headers;
45
- }
46
-
47
35
  const fns = {};
48
36
  function createTokenFn({ action, prefix, checkFn, extraParams, baseUrl }) {
49
37
  const key = `${prefix}/token?${stringifyQuery(extraParams)}`;
package/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import SealedBox from 'tweetnacl-sealedbox-js';
3
- import { getCookieOptions, setVisitorId, getVisitorId } from '@arcblock/ux/lib/Util';
3
+ import { getCookieOptions, setVisitorId, getVisitorId, isUrl } from '@arcblock/ux/lib/Util';
4
4
  import Cookie from 'js-cookie';
5
5
  import isNil from 'lodash/isNil';
6
6
  import isString from 'lodash/isString';
@@ -202,8 +202,24 @@ export const getWebAuthnErrorMessage = (err, defaultMessage, t) => {
202
202
  }
203
203
  };
204
204
 
205
+ export const getExtraHeaders = (baseUrl) => {
206
+ const headers = {};
207
+ // 如果 baseUrl 是有效的 url,则使用 baseUrl,否则使用当前页面的 url
208
+ // 避免传入的 baseUrl 是相对路径,导致 new URL 失败
209
+ const authUrl = isUrl(baseUrl) ? baseUrl : window.location.href;
210
+
211
+ if (authUrl) {
212
+ const { hostname, protocol, port } = new URL(authUrl);
213
+ headers['x-real-hostname'] = hostname;
214
+ headers['x-real-port'] = port;
215
+ headers['x-real-protocol'] = protocol.endsWith(':') ? protocol.substring(0, protocol.length - 1) : protocol;
216
+ }
217
+ return headers;
218
+ };
219
+
205
220
  export const createAxios = (options = {}, { lazy = false, lazyTime = 300 } = {}) => {
206
221
  const mergedHeaders = {
222
+ ...getExtraHeaders(options.baseURL),
207
223
  ...options.headers,
208
224
  'x-did-connect-version': version,
209
225
  };