@use-glyph/sdk-react 1.0.5 → 1.0.8

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, b as q, d as z, j as H, a 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
  };
@@ -1,114 +1,115 @@
1
- import { c as J, i as K, S as D, G as R, a as O, j as Q, b as X, d as Z, W as L } from "./index.js";
1
+ import { c as K, i as O, S as R, G as F, j as Q, a as X, b as Z, W as L } from "./index.js";
2
2
  import { usePrivy as ss, useCrossAppAccounts as es, useWallets as ts } from "@privy-io/react-auth";
3
- import { memo as as, useState as p, useEffect as rs, useCallback as u } from "react";
4
- const t = J("PrivyStrategy"), F = ({ children: N, glyphUrl: y, onLogin: g, onLogout: h, useStagingTenant: f }) => {
5
- const [V, A] = p(!0), [Y, m] = p(null), [a, j] = p(null), [n, U] = p(!1), {
3
+ import { memo as as, useState as y, useEffect as rs, useCallback as g } from "react";
4
+ import { useChainId as ns } from "wagmi";
5
+ const t = K("PrivyStrategy"), N = ({ children: V, glyphUrl: f, onLogin: i, onLogout: l, useStagingTenant: h }) => {
6
+ const [Y, A] = y(!0), [j, m] = y(null), [a, U] = y(null), [n, H] = y(!1), w = ns(), {
6
7
  ready: c,
7
- authenticated: i,
8
+ authenticated: p,
8
9
  user: o,
9
- getAccessToken: w,
10
- login: b,
10
+ getAccessToken: b,
11
+ login: E,
11
12
  logout: v,
12
- signMessage: E,
13
- sendTransaction: G
14
- } = ss(), { signMessage: S, sendTransaction: T } = es();
15
- t.debug(c, i, !!o);
16
- const { wallets: H } = ts();
13
+ signMessage: G,
14
+ sendTransaction: S
15
+ } = ss(), { signMessage: I, sendTransaction: T } = es();
16
+ t.debug(c, p, !!o);
17
+ const { wallets: B } = ts();
17
18
  rs(() => {
18
- var C, l, M;
19
+ var M, u, D;
19
20
  if (!c) {
20
21
  m(null), A(!0);
21
22
  return;
22
23
  }
23
- if (!i || !o) {
24
+ if (!p || !o) {
24
25
  m(null), A(!1);
25
26
  return;
26
27
  }
27
28
  let e;
28
- const r = (C = o.linkedAccounts) == null ? void 0 : C.find(
29
+ const r = (M = o.linkedAccounts) == null ? void 0 : M.find(
29
30
  (s) => s.type === "wallet" && s.chainType === "ethereum" && s.walletClientType === "privy"
30
- ), I = r && K(r.address);
31
- I && t.debug("Privy (main) EVM embedded wallet", r.address), t.debug("finding cross-app accounts...");
32
- const W = ((M = (l = o.linkedAccounts) == null ? void 0 : l.filter) == null ? void 0 : M.call(l, (s) => s.type === "cross_app")) || [], k = W.filter(
31
+ ), W = r && O(r.address);
32
+ W && t.debug("Privy (main) EVM embedded wallet", r.address), t.debug("finding cross-app accounts...");
33
+ const k = ((D = (u = o.linkedAccounts) == null ? void 0 : u.filter) == null ? void 0 : D.call(u, (s) => s.type === "cross_app")) || [], x = k.filter(
33
34
  (s) => {
34
35
  var d;
35
- return ((d = s.providerApp) == null ? void 0 : d.id) !== (f ? D : R);
36
+ return ((d = s.providerApp) == null ? void 0 : d.id) !== (h ? R : F);
36
37
  }
37
38
  );
38
- t.debug("linked accounts - non-Glyph", { accounts: k });
39
- const P = k.map((s) => s.embeddedWallets[0].address);
39
+ t.debug("linked accounts - non-Glyph", { accounts: x });
40
+ const P = x.map((s) => s.embeddedWallets[0].address);
40
41
  P.length > 0 && (e = P[0], t.log("using cross_app (non-Glyph) account"), P.length > 1 && t.warn("too many cross_app accounts"));
41
- const x = W.filter(
42
+ const C = k.filter(
42
43
  (s) => {
43
44
  var d;
44
- return ((d = s.providerApp) == null ? void 0 : d.id) === (f ? D : R);
45
+ return ((d = s.providerApp) == null ? void 0 : d.id) === (h ? R : F);
45
46
  }
46
47
  );
47
- t.debug("linked accounts - Glyph", { accounts: x });
48
- const _ = x.map((s) => s.embeddedWallets[0].address);
49
- _.length > 0 && (e = _[0], t.log("using cross_app (Glyph) account"), _.length > 1 && t.warn("too many cross_app accounts")), e ? (U(!0), t.log("using cross_app account", e)) : I && (e = r.address, t.log("using Privy (main) EVM embedded wallet", e)), j(e ?? null), (async () => {
48
+ t.debug("linked accounts - Glyph", { accounts: C });
49
+ const _ = C.map((s) => s.embeddedWallets[0].address);
50
+ _.length > 0 && (e = _[0], t.log("using cross_app (Glyph) account"), _.length > 1 && t.warn("too many cross_app accounts")), e ? (H(!0), t.log("using cross_app account", e)) : W && (e = r.address, t.log("using Privy (main) EVM embedded wallet", e)), U(e ?? null), (async () => {
50
51
  if (!e) {
51
52
  t.error("user has no wallet");
52
53
  return;
53
54
  }
54
55
  const s = Z(
55
- y || L,
56
- async () => await w(),
56
+ f || L,
57
+ async () => await b(),
57
58
  { "x-evm-address": e }
58
59
  );
59
60
  m(() => s), A(!1);
60
61
  })();
61
62
  }, [
62
- y,
63
- i,
63
+ f,
64
+ p,
64
65
  c,
65
66
  o,
66
- f,
67
+ h,
67
68
  n,
68
- H,
69
+ B,
69
70
  a,
70
- w,
71
+ b,
71
72
  v
72
73
  ]);
73
- const B = u(async () => {
74
- await b(), g == null || g();
75
- }, [b]), $ = u(async () => {
76
- v(), h == null || h();
77
- }, [v]), q = u(
74
+ const $ = g(async () => {
75
+ await E(), i == null || i();
76
+ }, [i, E]), q = g(async () => {
77
+ v(), l == null || l();
78
+ }, [l, v]), z = g(
78
79
  async ({ message: e }) => {
79
80
  if (!a) throw new Error("No wallet address found");
80
- return n ? S(e, { address: a }) : E({ message: e });
81
+ return n ? I(e, { address: a }) : G({ message: e });
81
82
  },
82
- [a, n, S, E]
83
- ), z = u(
83
+ [a, n, I, G]
84
+ ), J = g(
84
85
  async ({ transaction: e }) => {
85
86
  if (!a) throw new Error("No wallet address found");
86
- const r = { ...e, chainId: O.id };
87
- return n ? T(r, { address: a }) : G(r, { address: a });
87
+ const r = { ...e, chainId: w };
88
+ return n ? T(r, { address: a }) : S(r, { address: a });
88
89
  },
89
- [a, n, T, G]
90
+ [w, n, a, T, S]
90
91
  );
91
92
  return /* @__PURE__ */ Q.jsx(
92
93
  X.Provider,
93
94
  {
94
95
  value: {
95
- ready: c && !V,
96
- authenticated: c && i,
96
+ ready: c && !Y,
97
+ authenticated: c && p,
97
98
  symbol: "APE",
98
- glyphUrl: y,
99
+ glyphUrl: f,
99
100
  // Pass as string to avoid type error (we are passing fallback value in useGlyph hook at a common place)
100
- login: B,
101
- logout: $,
102
- signMessage: q,
103
- sendTransaction: z,
104
- apiFetch: Y
101
+ login: $,
102
+ logout: q,
103
+ signMessage: z,
104
+ sendTransaction: J,
105
+ apiFetch: j
105
106
  },
106
- children: N
107
+ children: V
107
108
  }
108
109
  );
109
110
  };
110
- F.displayName = "PrivyStrategy";
111
- const is = as(F);
111
+ N.displayName = "PrivyStrategy";
112
+ const ps = as(N);
112
113
  export {
113
- is as default
114
+ ps as default
114
115
  };