@use-glyph/sdk-react 1.0.4 → 1.0.6

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,56 +1,56 @@
1
- import { c as B, W as C, d as J, a as q, j as z, b as H } from "./index.js";
2
- import { useState as f, useEffect as p, useCallback as d } from "react";
3
- import { useAccount as Q, useDisconnect as V, useSignMessage as X, useSendTransaction as Z, useAccountEffect as L } from "wagmi";
4
- const T = "__glyph-widget-token__", k = "__glyph-widget-nonce__", r = B("EIP1193Strategy"), U = ({
1
+ import { c as J, W as C, d as q, a as z, j as H, b as Q } from "./index.js";
2
+ import { useState as g, useEffect as p, useCallback as u } from "react";
3
+ import { useAccount as V, useDisconnect as X, useSignMessage as Z, useSendTransaction as L, useAccountEffect as U } from "wagmi";
4
+ const T = "__glyph-widget-token__", k = "__glyph-widget-nonce__", r = J("EIP1193Strategy"), ee = ({
5
5
  children: O,
6
6
  glyphUrl: o,
7
7
  onLogin: E,
8
8
  onLogout: S,
9
9
  askForSignature: A = !0
10
10
  }) => {
11
- const [D, c] = f(!1), [F, h] = f(null), { address: t, isConnected: s, chainId: b } = Q(), { disconnectAsync: x } = V(), { signMessageAsync: m } = X(), { sendTransactionAsync: N } = Z();
12
- r.log({ address: t, isConnected: s });
13
- const [i, I] = f(() => {
11
+ const [$, c] = g(!1), [D, h] = g(null), { address: e, isConnected: s, chainId: b } = V(), { disconnectAsync: x } = X(), { signMessageAsync: m } = Z(), { sendTransactionAsync: N } = L();
12
+ r.log({ address: e, isConnected: s });
13
+ const [i, I] = g(() => {
14
14
  if (typeof window > "u")
15
15
  return null;
16
16
  try {
17
17
  return localStorage.getItem(T);
18
- } catch (e) {
19
- return r.warn("Error reading session token", e), null;
18
+ } catch (t) {
19
+ return r.warn("Error reading session token", t), null;
20
20
  }
21
- }), [g, K] = f(null), [n, _] = f(() => {
21
+ }), [d, F] = g(null), [n, _] = g(() => {
22
22
  if (typeof window > "u")
23
23
  return null;
24
24
  try {
25
25
  return localStorage.getItem(k);
26
- } catch (e) {
27
- return r.warn("Error reading session nonce", e), null;
26
+ } catch (t) {
27
+ return r.warn("Error reading session nonce", t), null;
28
28
  }
29
29
  });
30
30
  p(() => {
31
31
  try {
32
32
  !i || !n ? (localStorage.removeItem(T), localStorage.removeItem(k)) : (localStorage.setItem(T, i), localStorage.setItem(k, n));
33
- } catch (e) {
34
- r.error("Error saving session token", e);
33
+ } catch (t) {
34
+ r.error("Error saving session token", t);
35
35
  }
36
36
  }, [n, i]);
37
- const P = d(
38
- async (e) => {
37
+ const P = u(
38
+ async (t) => {
39
39
  try {
40
40
  return await fetch(
41
- `${o || C}/api/widget/auth/message/${e}`
42
- ).then((l) => l.json());
41
+ `${o || C}/api/widget/auth/message/${t}`
42
+ ).then((f) => f.json());
43
43
  } catch (a) {
44
44
  throw r.error("Can't fetch message to sign", a), new Error("Failed to fetch the message to sign.");
45
45
  }
46
46
  },
47
47
  [o]
48
- ), j = d(
49
- async (e, a, l) => {
48
+ ), j = u(
49
+ async (t, a, f) => {
50
50
  try {
51
- return await fetch(`${o || C}/api/widget/auth/verify/${e}`, {
51
+ return await fetch(`${o || C}/api/widget/auth/verify/${t}`, {
52
52
  method: "POST",
53
- body: JSON.stringify({ signature: a, nonce: l })
53
+ body: JSON.stringify({ signature: a, nonce: f })
54
54
  }).then((w) => w.json());
55
55
  } catch (y) {
56
56
  throw r.error("Can't verify signature", y), new Error("Signature verification failed.");
@@ -59,89 +59,91 @@ const T = "__glyph-widget-token__", k = "__glyph-widget-nonce__", r = B("EIP1193
59
59
  [o]
60
60
  );
61
61
  p(() => {
62
- !t || !s || n || (c(!0), P(t).then(({ message: e, nonce: a }) => {
63
- K(e), _(a), c(!1);
62
+ !e || !s || n || (c(!0), P(e).then(({ message: t, nonce: a }) => {
63
+ F(t), _(a), c(!1);
64
64
  }).finally(() => {
65
65
  c(!1);
66
66
  }));
67
- }, [t, s, P, n]), p(() => {
67
+ }, [e, s, P, n]), p(() => {
68
68
  if (!i || !n) return;
69
- const e = J(o || C, async () => i, {
70
- "x-evm-address": t,
69
+ const t = q(o || C, async () => i, {
70
+ "x-evm-address": e,
71
71
  "x-glyph-nonce": n
72
72
  });
73
- h(() => e);
74
- }, [i, t, o, n]);
75
- const v = d(async () => {
76
- var e, a, l, y, w;
77
- if (r.log("authenticate"), !t) {
73
+ h(() => t);
74
+ }, [i, e, o, n]);
75
+ const K = u(async () => {
76
+ await x();
77
+ }, [x]), v = u(async () => {
78
+ var t, a, f, y, w;
79
+ if (r.log("authenticate"), !e) {
78
80
  r.error("no wallet connected.");
79
81
  return;
80
82
  }
81
83
  try {
82
- if (!t || !s || !g || !n) {
84
+ if (!e || !s || !d || !n) {
83
85
  h(null), c(!1);
84
86
  return;
85
87
  }
86
88
  c(!0);
87
- const u = await m({ account: t, message: g }), Y = (e = await j(t, u, n)) == null ? void 0 : e.token;
88
- I(Y);
89
- } catch (u) {
90
- r.error("Authentication failed", u), (w = (l = (a = u == null ? void 0 : u.message) == null ? void 0 : a.toLowerCase) == null ? void 0 : (y = l.call(a)).includes) != null && w.call(y, "user rejected") || await alert(
89
+ const l = await m({ account: e, message: d });
90
+ console.log(n, d, l);
91
+ const B = (t = await j(e, l, n)) == null ? void 0 : t.token;
92
+ I(B);
93
+ } catch (l) {
94
+ r.error("Authentication failed", l), (w = (f = (a = l == null ? void 0 : l.message) == null ? void 0 : a.toLowerCase) == null ? void 0 : (y = f.call(a)).includes) != null && w.call(y, "user rejected") || await alert(
91
95
  "Authentication failed - your browser could be blocking the popups. Allow popups or try to manually click sign in."
92
96
  ), h(null), I(null), _(null);
93
97
  } finally {
94
98
  c(!1), E == null || E();
95
99
  }
96
- }, [t, s, g, n, m, j]), M = d(
97
- async (e = !1) => {
98
- r.log(`logout: disconnect=${e}`), _(null), I(null), c(!1), h(null), e && await x(), S == null || S();
99
- },
100
- [x]
101
- ), G = d(
102
- async ({ message: e }) => {
103
- if (!t || !s)
100
+ }, [e, s, d, n, m, j]), G = u(async () => {
101
+ _(null), I(null), c(!1), h(null), S == null || S();
102
+ }, []), R = u(
103
+ async ({ message: t }) => {
104
+ if (!e || !s)
104
105
  throw new Error("Wallet not connected");
105
- return m({ account: t, message: e });
106
+ return m({ account: e, message: t });
106
107
  },
107
- [t, s, m]
108
- ), R = d(
109
- async ({ transaction: e }) => {
110
- if (!t || !s) throw new Error("Wallet not connected");
111
- return N({ ...e, chainId: b || q.id });
108
+ [e, s, m]
109
+ ), Y = u(
110
+ async ({ transaction: t }) => {
111
+ if (!e || !s) throw new Error("Wallet not connected");
112
+ return N({ ...t, chainId: b || z.id });
112
113
  },
113
- [t, s, N, b]
114
- ), [W, $] = f(!1);
115
- return L({
114
+ [e, s, N, b]
115
+ ), [M, W] = g(!1);
116
+ return U({
116
117
  onConnect: () => {
117
- A && $(!0);
118
+ A && W(!0);
118
119
  },
119
120
  onDisconnect: () => {
120
- M();
121
+ G();
121
122
  }
122
123
  }), p(() => {
123
- A && s && t && W && n && g && (v(), $(!1));
124
- }, [s, t, v, W, n, g, A]), /* @__PURE__ */ z.jsx(
125
- H.Provider,
124
+ A && s && e && M && n && d && (v(), W(!1));
125
+ }, [s, e, v, M, n, d, A]), /* @__PURE__ */ H.jsx(
126
+ Q.Provider,
126
127
  {
127
128
  value: {
128
- ready: !D && !!n,
129
- authenticated: s && !!t && i !== null,
129
+ ready: !$ && !!n,
130
+ authenticated: s && !!e && i !== null,
130
131
  symbol: "APE",
131
132
  glyphUrl: o,
132
133
  // Pass as string to avoid type error (we are passing fallback value in useGlyph hook at a common place)
133
- hideWidget: !s || !t,
134
+ hideWidget: !s || !e,
134
135
  login: v,
135
- logout: M,
136
- signMessage: G,
137
- sendTransaction: R,
138
- apiFetch: F
136
+ logout: K,
137
+ // on disconnect, logoutSideEffect is called and clears states
138
+ signMessage: R,
139
+ sendTransaction: Y,
140
+ apiFetch: D
139
141
  },
140
142
  children: O
141
143
  }
142
144
  );
143
145
  };
144
- U.displayName = "EIP1193Strategy";
146
+ ee.displayName = "EIP1193Strategy";
145
147
  export {
146
- U as default
148
+ ee as default
147
149
  };