@zerohash-sdk/crypto-deposits-react 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -5,3 +5,5 @@ This library was generated with [Nx](https://nx.dev).
5
5
  ## Running unit tests
6
6
 
7
7
  Run `nx test @zerohash-sdk/crypto-deposits-react` to execute the unit tests via [Vitest](https://vitest.dev/).
8
+
9
+ > Published to npm as `@zerohash-sdk/crypto-deposits-react`.
@@ -0,0 +1,99 @@
1
+ import { default as default_2 } from 'react';
2
+
3
+ /**
4
+ * Generic app event structure
5
+ * @template TType - Event type string
6
+ * @template TData - Event data payload
7
+ */
8
+ declare type AppEvent<TType extends string = string, TData = Record<string, unknown>> = {
9
+ /** The type of event that occurred */
10
+ type: TType;
11
+ /** Data associated with the event */
12
+ data: TData;
13
+ };
14
+
15
+ /**
16
+ * A React wrapper component for the Connect Crypto Deposits product.
17
+ */
18
+ export declare const CryptoDeposits: default_2.FC<CryptoDepositsProps>;
19
+
20
+ declare type CryptoDepositsCompletedData = {
21
+ depositId: string;
22
+ assetSymbol: string;
23
+ network: string;
24
+ amount: string;
25
+ };
26
+
27
+ declare type CryptoDepositsEvent = AppEvent<CryptoDepositsEventType>;
28
+
29
+ declare type CryptoDepositsEventType = string;
30
+
31
+ declare interface CryptoDepositsProps {
32
+ /** JWT token used for authentication with the Connect Crypto Deposits service. */
33
+ jwt: string;
34
+ /** Target environment for the Connect Crypto Deposits service. @default "prod" */
35
+ env?: Environment;
36
+ /** Theme mode for the Connect Crypto Deposits interface. @default "auto" */
37
+ theme?: Theme;
38
+ /** Callback invoked when the crypto deposit flow is successfully completed. */
39
+ onCompleted?: (data: CryptoDepositsCompletedData) => void;
40
+ /** Callback invoked when an error occurs during the crypto deposit flow. */
41
+ onError?: (error: ErrorPayload) => void;
42
+ /** Callback invoked when the user closes the crypto deposits widget. */
43
+ onClose?: () => void;
44
+ /** Callback invoked when the crypto deposits widget has finished loading and is ready. */
45
+ onLoaded?: () => void;
46
+ /** Callback invoked for general application events during the crypto deposit flow. */
47
+ onEvent?: (event: CryptoDepositsEvent) => void;
48
+ }
49
+
50
+ /**
51
+ * Available environments for the Connect Crypto Deposits service.
52
+ *
53
+ * - `"local"` - Local development environment
54
+ * - `"dev"` - Development environment for early-stage testing
55
+ * - `"cert"` - Certificate/staging environment for integration testing
56
+ * - `"prod"` - Live production environment for real applications
57
+ */
58
+ declare type Environment = 'local' | 'dev' | 'cert' | 'prod';
59
+
60
+ /**
61
+ * Generic error codes for all Connect applications
62
+ */
63
+ declare enum ErrorCode {
64
+ /** Network connectivity error */
65
+ NETWORK_ERROR = 'network_error',
66
+ /** Authentication or session expired error */
67
+ AUTH_ERROR = 'auth_error',
68
+ /** Resource not found error */
69
+ NOT_FOUND_ERROR = 'not_found_error',
70
+ /** Validation error from user input */
71
+ VALIDATION_ERROR = 'validation_error',
72
+ /** Server error (5xx) */
73
+ SERVER_ERROR = 'server_error',
74
+ /** Client error (4xx) */
75
+ CLIENT_ERROR = 'client_error',
76
+ /** Unknown or unexpected error */
77
+ UNKNOWN_ERROR = 'unknown_error',
78
+ }
79
+
80
+ /**
81
+ * Generic error payload structure for error callbacks
82
+ */
83
+ declare type ErrorPayload = {
84
+ /** Error code indicating the type of error */
85
+ errorCode: ErrorCode;
86
+ /** Human-readable reason for the error */
87
+ reason: string;
88
+ };
89
+
90
+ /**
91
+ * Theme mode for the Connect Crypto Deposits interface.
92
+ *
93
+ * - `"auto"` - Automatically detect system preference (light/dark mode)
94
+ * - `"light"` - Force light theme
95
+ * - `"dark"` - Force dark theme
96
+ */
97
+ declare type Theme = 'auto' | 'light' | 'dark';
98
+
99
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,262 @@
1
+ import { jsxs as W, jsx as d } from "react/jsx-runtime";
2
+ import D, { useEffect as A, useState as L, useCallback as M, useRef as j } from "react";
3
+ const $ = {
4
+ dev: "https://grafana-faro-collector.dev.0hash.com/collect",
5
+ cert: "https://grafana-faro-collector.cert.zerohash.com/collect",
6
+ prod: "https://grafana-faro-collector.zerohash.com/collect",
7
+ "eu-cert": "https://grafana-faro-collector.cert.zerohash.eu/collect",
8
+ "eu-prod": "https://grafana-faro-collector.zerohash.eu/collect",
9
+ sandbox: "https://grafana-faro-collector.cert.zerohash.com/collect",
10
+ production: "https://grafana-faro-collector.zerohash.com/collect"
11
+ }, V = (e) => {
12
+ if (!e || typeof e != "string")
13
+ return null;
14
+ const t = e.split(".");
15
+ if (t.length < 2)
16
+ return null;
17
+ try {
18
+ const o = t[1].replace(/-/g, "+").replace(/_/g, "/"), r = o + "===".slice(0, (4 - o.length % 4) % 4), a = typeof atob < "u" ? atob(r) : Buffer.from(r, "base64").toString("utf-8"), s = JSON.parse(a)?.payload?.region;
19
+ if (typeof s != "string")
20
+ return null;
21
+ const n = s.toLowerCase();
22
+ return n === "us" || n === "eu" ? n : null;
23
+ } catch {
24
+ return null;
25
+ }
26
+ }, I = (e, t) => V(t) !== "eu" ? e : e === "cert" ? "eu-cert" : e === "prod" ? "eu-prod" : e, P = (e) => {
27
+ if (!e || typeof e != "string")
28
+ return {};
29
+ const t = e.split(".");
30
+ if (t.length < 2)
31
+ return {};
32
+ try {
33
+ const o = t[1].replace(/-/g, "+").replace(/_/g, "/"), r = o + "===".slice(0, (4 - o.length % 4) % 4), a = typeof atob < "u" ? atob(r) : Buffer.from(r, "base64").toString("utf-8"), i = JSON.parse(a), s = i?.payload ?? {}, n = (c) => typeof c == "string" && c.length > 0 ? c : void 0;
34
+ return {
35
+ participantCode: n(s.participant_code),
36
+ platformName: n(s.platform_name),
37
+ platformCode: n(i.platform_code),
38
+ region: n(s.region)
39
+ };
40
+ } catch {
41
+ return {};
42
+ }
43
+ }, z = (e, t, o) => {
44
+ const r = I(t, o);
45
+ return e[r] ?? e[t] ?? e.prod;
46
+ }, H = (e, t, o) => {
47
+ const r = I(t, o);
48
+ return e[r] ?? e[t];
49
+ }, O = () => {
50
+ }, J = () => {
51
+ const e = new Uint8Array(8);
52
+ return globalThis.crypto.getRandomValues(e), Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("");
53
+ }, G = (e, t) => {
54
+ const o = J(), r = globalThis.__ZH_WEB_SDK_VERSION__, a = {};
55
+ e.claims.participantCode && (a.participant_code = e.claims.participantCode), e.claims.platformName && (a.platform_name = e.claims.platformName), e.claims.platformCode && (a.platform_code = e.claims.platformCode), e.claims.region && (a.region = e.claims.region), r && (a.zh_web_sdk_version = r);
56
+ const i = {
57
+ meta: {
58
+ app: { name: e.appName, version: t ?? "unknown", environment: e.env },
59
+ session: { id: o, attributes: a },
60
+ browser: typeof navigator < "u" ? { userAgent: navigator.userAgent } : void 0,
61
+ page: typeof window < "u" ? { url: window.location.origin } : void 0
62
+ },
63
+ logs: [
64
+ {
65
+ message: `Failed to load the script for ${e.webComponentTag} from ${e.env} environment.`,
66
+ level: "error",
67
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
68
+ context: {
69
+ web_component: e.webComponentTag,
70
+ script_url: e.scriptUrl,
71
+ reason: e.reason,
72
+ tried_fallback: String(e.triedFallback),
73
+ elapsed_ms: String(e.elapsedMs)
74
+ }
75
+ }
76
+ ]
77
+ };
78
+ return { sessionId: o, payload: i };
79
+ }, K = (e, t, o) => {
80
+ if (!e || typeof fetch > "u")
81
+ return;
82
+ const { sessionId: r, payload: a } = G(t, o);
83
+ try {
84
+ fetch(e, {
85
+ method: "POST",
86
+ headers: { "Content-Type": "application/json", "x-faro-session-id": r },
87
+ body: JSON.stringify(a),
88
+ keepalive: !0
89
+ }).catch(() => {
90
+ });
91
+ } catch {
92
+ }
93
+ }, Z = (e) => {
94
+ const { webComponentTag: t, appName: o, appVersion: r, scriptUrls: a, fallbackScriptUrls: i, collectorUrls: s, env: n, jwt: c, timeoutMs: f = 15e3, onLoad: _, onError: g } = e;
95
+ if (typeof document > "u")
96
+ return O;
97
+ const h = `${t}-script-${n}`;
98
+ if (customElements.get(t) || document.getElementById(h))
99
+ return O;
100
+ const S = P(c), k = e.report ?? ((p) => K(s && H(s, n, c), p, r));
101
+ let m = !1, w = 0, v;
102
+ const R = () => {
103
+ v !== void 0 && clearTimeout(v);
104
+ }, C = (p, y, l) => {
105
+ if (m)
106
+ return;
107
+ const U = Math.round(performance.now() - w), E = {
108
+ webComponentTag: t,
109
+ appName: o,
110
+ env: n,
111
+ scriptUrl: y,
112
+ reason: p,
113
+ triedFallback: l,
114
+ elapsedMs: U,
115
+ claims: S
116
+ };
117
+ k(E);
118
+ const x = l ? void 0 : z(i ?? {}, n, c);
119
+ if (x && x !== y) {
120
+ N(x, !0);
121
+ return;
122
+ }
123
+ m = !0, R(), document.getElementById(h)?.remove(), g?.(E);
124
+ }, N = (p, y) => {
125
+ R(), w = performance.now();
126
+ const l = document.createElement("script");
127
+ l.id = h, l.src = p, l.type = "module", l.async = !0, l.onload = () => {
128
+ setTimeout(() => {
129
+ m || (customElements.get(t) ? (m = !0, R(), _?.()) : C("not-defined", p, y));
130
+ }, 0);
131
+ }, l.onerror = () => C("network", p, y), v = setTimeout(() => C("timeout", p, y), f), document.getElementById(h)?.remove(), document.head.appendChild(l);
132
+ }, T = z(a, n, c);
133
+ return T ? (N(T, !1), () => {
134
+ m = !0, R();
135
+ }) : O;
136
+ }, u = {
137
+ local: "http://localhost:5173",
138
+ dev: "https://sdk-cdn.dev.0hash.com",
139
+ cert: "https://sdk-cdn.cert.zerohash.com",
140
+ prod: "https://sdk-cdn.zerohash.com",
141
+ "eu-cert": "https://sdk-cdn.cert.zerohash.eu",
142
+ "eu-prod": "https://sdk-cdn.zerohash.eu"
143
+ }, q = "https://sdk-cdn.gating.0hash.com", Q = (e, t, o = {}) => {
144
+ const { eu: r = !1, legacyAliases: a = !1, local: i = u.local } = o, s = (c) => `${c}/${e}/${t}`, n = {
145
+ local: s(i),
146
+ dev: s(u.dev),
147
+ gating: s(q),
148
+ cert: s(u.cert),
149
+ prod: s(u.prod)
150
+ };
151
+ return a && (n.sandbox = s(u.cert), n.production = s(u.prod)), r && (n["eu-cert"] = s(u["eu-cert"]), n["eu-prod"] = s(u["eu-prod"])), n;
152
+ };
153
+ var F;
154
+ (function(e) {
155
+ e.NETWORK_ERROR = "network_error", e.AUTH_ERROR = "auth_error", e.NOT_FOUND_ERROR = "not_found_error", e.VALIDATION_ERROR = "validation_error", e.SERVER_ERROR = "server_error", e.CLIENT_ERROR = "client_error", e.UNKNOWN_ERROR = "unknown_error";
156
+ })(F || (F = {}));
157
+ const b = {
158
+ evergreen: "#00381D",
159
+ green: "#CCFFD0",
160
+ offBlack: "#1C1B1C",
161
+ grayLight: "#EBEBE9"
162
+ }, X = ({ onRetry: e, onClose: t }) => W("div", { role: "alert", style: {
163
+ position: "relative",
164
+ display: "flex",
165
+ flexDirection: "column",
166
+ alignItems: "center",
167
+ gap: "8px",
168
+ maxWidth: "360px",
169
+ margin: "0 auto",
170
+ padding: "32px 24px",
171
+ textAlign: "center",
172
+ fontFamily: "inherit",
173
+ color: b.offBlack,
174
+ background: "#FFFFFF",
175
+ border: `1px solid ${b.grayLight}`,
176
+ borderRadius: "12px"
177
+ }, children: [t && d("button", { type: "button", onClick: t, "aria-label": "Close", style: {
178
+ position: "absolute",
179
+ top: "12px",
180
+ right: "12px",
181
+ display: "flex",
182
+ padding: "4px",
183
+ color: b.offBlack,
184
+ background: "none",
185
+ border: "none",
186
+ cursor: "pointer",
187
+ lineHeight: 0
188
+ }, children: d("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: d("path", { d: "M18 6L6 18M6 6l12 12", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }) }), d("svg", { width: "32", height: "32", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: d("path", { d: "M12 9v4m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z", stroke: b.evergreen, strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }), d("p", { style: { margin: 0, fontSize: "16px", fontWeight: 600 }, children: "Something went wrong" }), d("p", { style: { margin: 0, fontSize: "14px", opacity: 0.75 }, children: "We couldn't load this content. Please check your internet connection and try again." }), d("button", { type: "button", onClick: e, style: {
189
+ marginTop: "8px",
190
+ padding: "8px 20px",
191
+ fontSize: "14px",
192
+ fontWeight: 600,
193
+ fontFamily: "inherit",
194
+ color: b.green,
195
+ background: b.evergreen,
196
+ border: "none",
197
+ borderRadius: "8px",
198
+ cursor: "pointer"
199
+ }, children: "Try again" })] }), Y = (e, t) => {
200
+ A(() => {
201
+ const o = e.current;
202
+ if (o)
203
+ for (const [r, a] of Object.entries(t))
204
+ a !== void 0 && (o[r] = a);
205
+ });
206
+ }, ee = (e) => {
207
+ const { webComponentTag: t, appName: o, appVersion: r, scriptUrls: a, fallbackScriptUrls: i, collectorUrls: s, env: n, jwt: c, timeoutMs: f } = e, [_, g] = L(!1), [h, S] = L(0);
208
+ A(() => (g(!1), Z({
209
+ webComponentTag: t,
210
+ appName: o ?? t,
211
+ appVersion: r,
212
+ scriptUrls: a,
213
+ fallbackScriptUrls: i,
214
+ collectorUrls: s ?? $,
215
+ env: n,
216
+ jwt: c,
217
+ timeoutMs: f,
218
+ onError: () => g(!0)
219
+ })), [
220
+ t,
221
+ o,
222
+ r,
223
+ a,
224
+ i,
225
+ s,
226
+ n,
227
+ c,
228
+ f,
229
+ h
230
+ ]);
231
+ const k = M(() => S((m) => m + 1), []);
232
+ return { loadFailed: _, retry: k };
233
+ }, te = Q("crypto-deposits-web", "index.js", { eu: !0 }), B = "zerohash-crypto-deposits", ne = ({
234
+ jwt: e,
235
+ env: t = "prod",
236
+ theme: o,
237
+ onCompleted: r,
238
+ onError: a,
239
+ onClose: i,
240
+ onLoaded: s,
241
+ onEvent: n,
242
+ ...c
243
+ }) => {
244
+ const f = j(null);
245
+ Y(f, { theme: o, onCompleted: r, onError: a, onClose: i, onLoaded: s, onEvent: n });
246
+ const { loadFailed: _, retry: g } = ee({
247
+ webComponentTag: B,
248
+ scriptUrls: te,
249
+ env: t,
250
+ jwt: e
251
+ });
252
+ return _ ? /* @__PURE__ */ d(X, { onRetry: g }) : D.createElement(B, {
253
+ ref: f,
254
+ jwt: e,
255
+ env: t,
256
+ theme: o,
257
+ ...c
258
+ });
259
+ };
260
+ export {
261
+ ne as CryptoDeposits
262
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerohash-sdk/crypto-deposits-react",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",