@use-glyph/sdk-react 0.4.1 → 1.0.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/LICENSE CHANGED
@@ -8,9 +8,7 @@ This License does not include the right to (i) create derivative works or (ii) u
8
8
 
9
9
  All copies of the Glyph Code shall include the following notice statement: “This Glyph Code is licensed by Yuga Labs, Inc. and may only be used in accordance with the Glyph Software License v1.0 published at https://useglyph.io/license ” This License applies to any copies of the Glyph Code, regardless of where the copies were obtained.
10
10
 
11
- THE GLYPH CODE IS PROVIDED “AS IS,” “WHERE IS,” AND “AS AVAILABLE,” WITH ALL FAULTS AND WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, (I) CERTAIN FUNCTIONALITY OR ENTIRE RELEASES OF THE GLYPH CODE MAY BE RELEASED AS BETA OR TEST VERSIONS, MAY CONTAIN ERRORS, BUGS AND OTHER DEFICIENCIES, AND ARE USED BY YOU AT YOUR OWN RISK;
12
-
13
- AND (II) THE GLYPH CODE IS SUBJECT TO CHANGES AND REDESIGNS, WHETHER BY YUGA LABS OR THIRD PARTIES, IN FUTURE RELEASES WITHOUT NOTICE WHICH MAY RENDER CERTAIN FUNCTIONALITY OR ENTIRE VERSIONS OF EARLIER RELEASES OF THE GLYPH CODE UNSUPPORTED AND OBSOLETE.
11
+ THE GLYPH CODE IS PROVIDED “AS IS,” “WHERE IS,” AND “AS AVAILABLE,” WITH ALL FAULTS AND WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, (I) CERTAIN FUNCTIONALITY OR ENTIRE RELEASES OF THE GLYPH CODE MAY BE RELEASED AS BETA OR TEST VERSIONS, MAY CONTAIN ERRORS, BUGS AND OTHER DEFICIENCIES, AND ARE USED BY YOU AT YOUR OWN RISK; AND (II) THE GLYPH CODE IS SUBJECT TO CHANGES AND REDESIGNS, WHETHER BY YUGA LABS OR THIRD PARTIES, IN FUTURE RELEASES WITHOUT NOTICE WHICH MAY RENDER CERTAIN FUNCTIONALITY OR ENTIRE VERSIONS OF EARLIER RELEASES OF THE GLYPH CODE UNSUPPORTED AND OBSOLETE.
14
12
 
15
13
  IN NO EVENT SHALL YUGA LABS, ITS AFFILIATES, ITS LICENSORS OR LICENSEES, AND EACH OF THEIR RESPECTIVE OFFICERS, DIRECTORS, SHAREHOLDERS, MEMBERS, SUPERVISORS, MANAGERS, EMPLOYEES, ADVISORS, CONTRACTORS, DEVELOPMENT PARTNERS, AGENTS, AND SUCCESSORS AND ASSIGNS (“RELEASEES”) BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY, PUNITIVE, OR SPECIAL DAMAGES OF ANY NATURE ARISING FROM, OUT OF OR IN CONNECTION WITH, THE GLYPH CODE OR USE THEREOF, INCLUDING WITHOUT LIMITATION, THE PROVISION OF DEVELOPER PRODUCTS AND SERVICES. THIS LICENSE SHALL NOT BE CONSTRUED, INTERPRETED, CLASSIFIED OR TREATED AS ENABLING, OR OFFERING ANY OPPORTUNITY TO, THE LICENSEE TO PARTICIPATE IN OR RECEIVE ANY PROFITS, INCOME, OR OTHER PAYMENTS OR RETURNS ARISING FROM OR IN CONNECTION WITH THIS LICENSE, THE GLYPH CODE, OR THE DEVELOPER PRODUCTS AND SERVICES (OTHER THAN YOUR DEVELOPER PRODUCTS AND SERVICES). IN USING THE GLYPH CODE, YOU ARE SOLELY RESPONSIBLE FOR YOUR ACTIONS AND YOU WILL INDEMNIFY, DEFEND, AND HOLD EACH RELEASEE HARMLESS FOR ANY LIABILITY ARISING FROM YOUR USE OF THE GLYPH CODE AND ANY DEVELOPER PRODUCTS AND SERVICES.
16
14
 
@@ -1,116 +1,147 @@
1
- import { c as $, W as S, d as b, j as M, a as O, b as D } from "./index.js";
2
- import { useState as f, useCallback as g, useEffect as x } from "react";
3
- import { useAccount as K, useDisconnect as G, useSignMessage as R, useSendTransaction as Y, useAccountEffect as B } from "wagmi";
4
- const p = "__glyph-widget-token__", I = "__glyph-widget-nonce__", n = $("EIP1193Strategy"), J = ({ children: C, glyphUrl: a, onLogin: y, onLogout: h }) => {
5
- const [N, l] = f(!1), [P, u] = f(null), { address: t, isConnected: s, chainId: j } = K(), { disconnectAsync: _ } = G(), { signMessageAsync: w } = R(), { sendTransactionAsync: F } = Y();
6
- B({
7
- onDisconnect: () => {
8
- T();
9
- }
10
- }), n.log({ address: t, isConnected: s });
11
- const [o, m] = f(() => {
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 = ({
5
+ children: O,
6
+ glyphUrl: o,
7
+ onLogin: E,
8
+ onLogout: S,
9
+ askForSignature: A = !0
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(() => {
12
14
  if (typeof window > "u")
13
15
  return null;
14
16
  try {
15
- return localStorage.getItem(p);
17
+ return localStorage.getItem(T);
16
18
  } catch (e) {
17
- return n.warn("Error reading session token", e), null;
19
+ return r.warn("Error reading session token", e), null;
18
20
  }
19
- }), [i, A] = f(() => {
21
+ }), [g, K] = f(null), [n, _] = f(() => {
20
22
  if (typeof window > "u")
21
23
  return null;
22
24
  try {
23
- return localStorage.getItem(I);
25
+ return localStorage.getItem(k);
24
26
  } catch (e) {
25
- return n.warn("Error reading session nonce", e), null;
27
+ return r.warn("Error reading session nonce", e), null;
26
28
  }
27
29
  });
28
- x(() => {
30
+ p(() => {
29
31
  try {
30
- o ? (localStorage.setItem(p, o), localStorage.setItem(I, i)) : (localStorage.removeItem(p), localStorage.removeItem(I));
32
+ !i || !n ? (localStorage.removeItem(T), localStorage.removeItem(k)) : (localStorage.setItem(T, i), localStorage.setItem(k, n));
31
33
  } catch (e) {
32
- n.error("Error saving session token", e);
33
- }
34
- }, [i, o]);
35
- const v = g(async (e) => {
36
- try {
37
- return await fetch(`${a || S}/api/widget/auth/message/${e}`).then(
38
- (c) => c.json()
39
- );
40
- } catch (r) {
41
- throw n.error("Can't fetch message to sign", r), new Error("Failed to fetch the message to sign.");
42
- }
43
- }, [a]), k = g(async (e, r, c) => {
44
- try {
45
- return await fetch(`${a || S}/api/widget/auth/verify/${e}`, {
46
- method: "POST",
47
- body: JSON.stringify({ signature: r, nonce: c })
48
- }).then((E) => E.json());
49
- } catch (d) {
50
- throw n.error("Can't verify signature", d), new Error("Signature verification failed.");
34
+ r.error("Error saving session token", e);
51
35
  }
52
- }, [a]);
53
- x(() => {
54
- if (!o || !i) return;
55
- const e = b(a || S, async () => o, {
36
+ }, [n, i]);
37
+ const P = d(
38
+ async (e) => {
39
+ try {
40
+ return await fetch(
41
+ `${o || C}/api/widget/auth/message/${e}`
42
+ ).then((l) => l.json());
43
+ } catch (a) {
44
+ throw r.error("Can't fetch message to sign", a), new Error("Failed to fetch the message to sign.");
45
+ }
46
+ },
47
+ [o]
48
+ ), j = d(
49
+ async (e, a, l) => {
50
+ try {
51
+ return await fetch(`${o || C}/api/widget/auth/verify/${e}`, {
52
+ method: "POST",
53
+ body: JSON.stringify({ signature: a, nonce: l })
54
+ }).then((w) => w.json());
55
+ } catch (y) {
56
+ throw r.error("Can't verify signature", y), new Error("Signature verification failed.");
57
+ }
58
+ },
59
+ [o]
60
+ );
61
+ p(() => {
62
+ !t || !s || n || (c(!0), P(t).then(({ message: e, nonce: a }) => {
63
+ K(e), _(a), c(!1);
64
+ }).finally(() => {
65
+ c(!1);
66
+ }));
67
+ }, [t, s, P, n]), p(() => {
68
+ if (!i || !n) return;
69
+ const e = J(o || C, async () => i, {
56
70
  "x-evm-address": t,
57
- "x-glyph-nonce": i
71
+ "x-glyph-nonce": n
58
72
  });
59
- u(() => e);
60
- }, [o, t, a, i]);
61
- const W = g(async () => {
62
- var e;
63
- if (n.log("authenticate"), !t) {
64
- n.error("no wallet connected.");
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) {
78
+ r.error("no wallet connected.");
65
79
  return;
66
80
  }
67
81
  try {
68
- if (!t || !s) {
69
- u(null), l(!1);
82
+ if (!t || !s || !g || !n) {
83
+ h(null), c(!1);
70
84
  return;
71
85
  }
72
- l(!0);
73
- const { message: r, nonce: c } = await v(t), d = await w({ account: t, message: r }), E = (e = await k(t, d, c)) == null ? void 0 : e.token;
74
- m(E), A(c);
75
- } catch (r) {
76
- n.error("Authentication failed", r), u(null), m(null), A(null);
86
+ 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(
91
+ "Authentication failed - your browser could be blocking the popups. Allow popups or try to manually click sign in."
92
+ ), h(null), I(null), _(null);
77
93
  } finally {
78
- l(!1);
94
+ c(!1), E == null || E();
95
+ }
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)
104
+ throw new Error("Wallet not connected");
105
+ return m({ account: t, message: e });
106
+ },
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 });
112
+ },
113
+ [t, s, N, b]
114
+ ), [W, $] = f(!1);
115
+ return L({
116
+ onConnect: () => {
117
+ A && $(!0);
118
+ },
119
+ onDisconnect: () => {
120
+ M();
79
121
  }
80
- }, [t, s, v, w, k]), T = g(async (e = !1) => {
81
- n.log(`logout: disconnect=${e}`), m(null), l(!1), u(null), e && await _();
82
- }, [_]);
83
- return /* @__PURE__ */ M.jsx(
84
- O.Provider,
122
+ }), p(() => {
123
+ A && s && t && W && n && g && (v(), $(!1));
124
+ }, [s, t, v, W, n, g, A]), /* @__PURE__ */ z.jsx(
125
+ H.Provider,
85
126
  {
86
127
  value: {
87
- ready: !N,
88
- authenticated: s && !!t && o !== null,
128
+ ready: !D && !!n,
129
+ authenticated: s && !!t && i !== null,
89
130
  symbol: "APE",
90
- glyphUrl: a,
131
+ glyphUrl: o,
132
+ // Pass as string to avoid type error (we are passing fallback value in useGlyph hook at a common place)
91
133
  hideWidget: !s || !t,
92
- login: async () => {
93
- await W(), y == null || y();
94
- },
95
- logout: async () => {
96
- await T(!0), h == null || h();
97
- },
98
- signMessage: async ({ message: e }) => {
99
- if (!t || !s)
100
- throw new Error("Wallet not connected");
101
- return w({ account: t, message: e });
102
- },
103
- sendTransaction: async ({ transaction: e }) => {
104
- if (!t || !s) throw new Error("Wallet not connected");
105
- return F({ ...e, chainId: j || D.id });
106
- },
107
- apiFetch: P
134
+ login: v,
135
+ logout: M,
136
+ signMessage: G,
137
+ sendTransaction: R,
138
+ apiFetch: F
108
139
  },
109
- children: C
140
+ children: O
110
141
  }
111
142
  );
112
143
  };
113
- J.displayName = "EIP1193Strategy";
144
+ U.displayName = "EIP1193Strategy";
114
145
  export {
115
- J as default
146
+ U as default
116
147
  };
@@ -1,92 +1,114 @@
1
- import { c as B, i as $, S as M, G as T, j as q, a as z, b as J, d as K, W as O } from "./index.js";
2
- import { usePrivy as Q, useCrossAppAccounts as X, useWallets as Z } from "@privy-io/react-auth";
3
- import { memo as L, useState as l, useEffect as ss } from "react";
4
- const e = B("PrivyStrategy"), W = ({ children: k, glyphUrl: p, onLogin: u, onLogout: y, useStagingTenant: g }) => {
5
- const [C, f] = l(!0), [D, h] = l(null), [r, R] = l(null), [A, F] = l(!1), {
6
- ready: n,
7
- authenticated: d,
8
- user: a,
9
- getAccessToken: P,
10
- login: N,
11
- logout: w,
12
- signMessage: V,
13
- sendTransaction: Y
14
- } = Q(), { signMessage: j, sendTransaction: U } = X();
15
- e.debug(n, d, !!a);
16
- const { wallets: H } = Z();
17
- return ss(() => {
18
- var G, S, i, I, x;
19
- if (!n) {
20
- h(null), f(!0);
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";
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), {
6
+ ready: c,
7
+ authenticated: i,
8
+ user: o,
9
+ getAccessToken: w,
10
+ login: b,
11
+ 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();
17
+ rs(() => {
18
+ var C, l, M;
19
+ if (!c) {
20
+ m(null), A(!0);
21
21
  return;
22
22
  }
23
- if (!d || !a) {
24
- h(null), f(!1);
23
+ if (!i || !o) {
24
+ m(null), A(!1);
25
25
  return;
26
26
  }
27
- let s;
28
- const c = $((G = a.wallet) == null ? void 0 : G.address);
29
- c && e.debug("Privy (main) EVM embedded wallet", (S = a.wallet) == null ? void 0 : S.address), e.debug("finding cross-app accounts...");
30
- const _ = ((I = (i = a.linkedAccounts) == null ? void 0 : i.filter) == null ? void 0 : I.call(i, (t) => t.type === "cross_app")) || [], b = _.filter(
31
- (t) => {
32
- var o;
33
- return ((o = t.providerApp) == null ? void 0 : o.id) !== (g ? M : T);
27
+ let e;
28
+ const r = (C = o.linkedAccounts) == null ? void 0 : C.find(
29
+ (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(
33
+ (s) => {
34
+ var d;
35
+ return ((d = s.providerApp) == null ? void 0 : d.id) !== (f ? D : R);
34
36
  }
35
37
  );
36
- e.debug("linked accounts - non-Glyph", { accounts: b });
37
- const m = b.map((t) => t.embeddedWallets[0].address);
38
- m.length > 0 && (s = m[0], e.log("using cross_app (non-Glyph) account"), m.length > 1 && e.warn("too many cross_app accounts"));
39
- const E = _.filter(
40
- (t) => {
41
- var o;
42
- return ((o = t.providerApp) == null ? void 0 : o.id) === (g ? M : T);
38
+ t.debug("linked accounts - non-Glyph", { accounts: k });
39
+ const P = k.map((s) => s.embeddedWallets[0].address);
40
+ 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
+ (s) => {
43
+ var d;
44
+ return ((d = s.providerApp) == null ? void 0 : d.id) === (f ? D : R);
43
45
  }
44
46
  );
45
- e.debug("linked accounts - Glyph", { accounts: E });
46
- const v = E.map((t) => t.embeddedWallets[0].address);
47
- v.length > 0 && (s = v[0], e.log("using cross_app (Glyph) account"), v.length > 1 && e.warn("too many cross_app accounts")), s ? (F(!0), e.log("using cross_app account", s)) : c && (s = (x = a.wallet) == null ? void 0 : x.address, e.log("using Privy (main) EVM embedded wallet", s)), R(s ?? null), (async () => {
48
- if (!s) {
49
- e.error("user has no wallet");
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 () => {
50
+ if (!e) {
51
+ t.error("user has no wallet");
50
52
  return;
51
53
  }
52
- const t = K(
53
- p || O,
54
- async () => await P(),
55
- { "x-evm-address": s }
54
+ const s = Z(
55
+ y || L,
56
+ async () => await w(),
57
+ { "x-evm-address": e }
56
58
  );
57
- h(() => t), f(!1);
59
+ m(() => s), A(!1);
58
60
  })();
59
- }, [p, d, n, a, g, A, H, r, P, w]), /* @__PURE__ */ q.jsx(
60
- z.Provider,
61
+ }, [
62
+ y,
63
+ i,
64
+ c,
65
+ o,
66
+ f,
67
+ n,
68
+ H,
69
+ a,
70
+ w,
71
+ v
72
+ ]);
73
+ const B = u(async () => {
74
+ await b(), g == null || g();
75
+ }, [b]), $ = u(async () => {
76
+ v(), h == null || h();
77
+ }, [v]), q = u(
78
+ async ({ message: e }) => {
79
+ if (!a) throw new Error("No wallet address found");
80
+ return n ? S(e, { address: a }) : E({ message: e });
81
+ },
82
+ [a, n, S, E]
83
+ ), z = u(
84
+ async ({ transaction: e }) => {
85
+ 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 });
88
+ },
89
+ [a, n, T, G]
90
+ );
91
+ return /* @__PURE__ */ Q.jsx(
92
+ X.Provider,
61
93
  {
62
94
  value: {
63
- ready: n && !C,
64
- authenticated: n && d,
95
+ ready: c && !V,
96
+ authenticated: c && i,
65
97
  symbol: "APE",
66
- glyphUrl: p,
67
- login: () => {
68
- N(), u == null || u();
69
- },
70
- logout: () => {
71
- w(), y == null || y();
72
- },
73
- signMessage: async ({ message: s }) => {
74
- if (!r) throw new Error("No wallet address found");
75
- return A ? j(s, { address: r }) : V({ message: s });
76
- },
77
- sendTransaction: async ({ transaction: s }) => {
78
- if (!r) throw new Error("No wallet address found");
79
- const c = { ...s, chainId: J.id };
80
- return A ? U(c, { address: r }) : Y(c, { address: r });
81
- },
82
- apiFetch: D
98
+ glyphUrl: y,
99
+ // 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
83
105
  },
84
- children: k
106
+ children: N
85
107
  }
86
108
  );
87
109
  };
88
- W.displayName = "PrivyStrategy";
89
- const ns = L(W);
110
+ F.displayName = "PrivyStrategy";
111
+ const is = as(F);
90
112
  export {
91
- ns as default
113
+ is as default
92
114
  };