@zerohash-sdk/fund-react 1.2.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/README.md +147 -0
- package/dist/index.d.ts +258 -0
- package/dist/index.js +42 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @zerohash-sdk/crypto-sell-react
|
|
2
|
+
|
|
3
|
+
A React SDK that enables frontend React applications to seamlessly integrate with the Connect Crypto Sell product.
|
|
4
|
+
|
|
5
|
+
Connect Crypto Sell provides a secure, customizable flow for selling crypto assets directly within your application.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- React 18.0.0 or higher
|
|
10
|
+
- React DOM 18.0.0 or higher
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @zerohash-sdk/crypto-sell-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Getting Started
|
|
19
|
+
|
|
20
|
+
Follow these simple steps to integrate Connect Crypto Sell into your React application:
|
|
21
|
+
|
|
22
|
+
### 1. Import the CryptoSell Component
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { CryptoSell } from '@zerohash-sdk/crypto-sell-react';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Add the CryptoSell Component to Your App
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
function App() {
|
|
32
|
+
const jwt = 'your-jwt-token'; // Obtain this from your backend
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<CryptoSell
|
|
36
|
+
jwt={jwt}
|
|
37
|
+
env="prod" // or "cert" for testing
|
|
38
|
+
theme="auto" // 'auto' (default), 'light', or 'dark'
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 3. Configure Event Callbacks (Optional)
|
|
45
|
+
|
|
46
|
+
Listen to events from the Crypto Sell SDK to handle user interactions:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
function App() {
|
|
50
|
+
const jwt = 'your-jwt-token';
|
|
51
|
+
|
|
52
|
+
const handleCompleted = ({ amountSold, assetSymbol }) => {
|
|
53
|
+
console.log('Sell completed:', amountSold, assetSymbol);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const handleError = ({ errorCode, reason }) => {
|
|
57
|
+
console.log('Crypto sell error:', errorCode, 'Reason:', reason);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const handleClose = () => {
|
|
61
|
+
console.log('Crypto sell widget closed');
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const handleEvent = ({ type, data }) => {
|
|
65
|
+
console.log('Crypto sell event:', type, 'Data:', data);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const handleLoaded = () => {
|
|
69
|
+
console.log('Crypto sell widget loaded and ready');
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<CryptoSell
|
|
74
|
+
jwt={jwt}
|
|
75
|
+
env="prod"
|
|
76
|
+
theme="auto"
|
|
77
|
+
onCompleted={handleCompleted}
|
|
78
|
+
onError={handleError}
|
|
79
|
+
onClose={handleClose}
|
|
80
|
+
onEvent={handleEvent}
|
|
81
|
+
onLoaded={handleLoaded}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Complete Example
|
|
88
|
+
|
|
89
|
+
Here's a full example of integrating Connect Crypto Sell into your React application:
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import React from 'react';
|
|
93
|
+
import { CryptoSell } from '@zerohash-sdk/crypto-sell-react';
|
|
94
|
+
|
|
95
|
+
function App() {
|
|
96
|
+
// JWT token should be obtained from your backend
|
|
97
|
+
const jwt = 'your-jwt-token';
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<div className="App">
|
|
101
|
+
<h1>My Crypto App</h1>
|
|
102
|
+
|
|
103
|
+
<CryptoSell
|
|
104
|
+
jwt={jwt}
|
|
105
|
+
env="prod" // 'prod' (default), 'cert', 'dev', or 'local'
|
|
106
|
+
theme="auto" // 'auto' (default), 'light', or 'dark'
|
|
107
|
+
onCompleted={({ amountSold, assetSymbol }) => {
|
|
108
|
+
console.log('Sold:', amountSold, assetSymbol);
|
|
109
|
+
}}
|
|
110
|
+
onError={({ errorCode, reason }) => {
|
|
111
|
+
console.log('Error:', errorCode, 'Reason:', reason);
|
|
112
|
+
}}
|
|
113
|
+
onClose={() => {
|
|
114
|
+
console.log('Crypto sell widget closed');
|
|
115
|
+
}}
|
|
116
|
+
onEvent={({ type, data }) => {
|
|
117
|
+
console.log('Event type:', type, 'Event data:', data);
|
|
118
|
+
}}
|
|
119
|
+
onLoaded={() => {
|
|
120
|
+
console.log('Crypto sell widget loaded and ready');
|
|
121
|
+
}}
|
|
122
|
+
/>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default App;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## API Reference
|
|
131
|
+
|
|
132
|
+
### CryptoSell Component Props
|
|
133
|
+
|
|
134
|
+
| Prop | Type | Required | Default | Description |
|
|
135
|
+
| ------------- | --------------------------------------- | -------- | -------- | -------------------------------------------------- |
|
|
136
|
+
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect |
|
|
137
|
+
| `env` | `"prod" \| "cert" \| "dev" \| "local"` | No | `"prod"` | Target environment |
|
|
138
|
+
| `theme` | `"auto" \| "light" \| "dark"` | No | `"auto"` | Theme mode for the interface |
|
|
139
|
+
| `onCompleted` | `({ amountSold, assetSymbol }) => void` | No | - | Callback when the sell flow completes successfully |
|
|
140
|
+
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
141
|
+
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
142
|
+
| `onEvent` | `({ type, data }) => void` | No | - | Callback for general events |
|
|
143
|
+
| `onLoaded` | `() => void` | No | - | Callback when the widget is loaded and ready |
|
|
144
|
+
|
|
145
|
+
## More Information & Support
|
|
146
|
+
|
|
147
|
+
For comprehensive documentation or support about Connect, visit our [Documentation Page](https://docs.zerohash.com/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
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
|
+
* Available environments for the Connect Fund service.
|
|
17
|
+
*
|
|
18
|
+
* - `"local"` - Local development environment (http://localhost:4204)
|
|
19
|
+
* - `"dev"` - Development environment for early-stage testing
|
|
20
|
+
* - `"cert"` - Certificate/staging environment for integration testing
|
|
21
|
+
* - `"prod"` - Live production environment for real applications
|
|
22
|
+
*/
|
|
23
|
+
declare type Environment = 'local' | 'dev' | 'cert' | 'prod';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Generic error codes for all Connect applications
|
|
27
|
+
*/
|
|
28
|
+
declare enum ErrorCode {
|
|
29
|
+
/** Network connectivity error */
|
|
30
|
+
NETWORK_ERROR = 'network_error',
|
|
31
|
+
/** Authentication or session expired error */
|
|
32
|
+
AUTH_ERROR = 'auth_error',
|
|
33
|
+
/** Resource not found error */
|
|
34
|
+
NOT_FOUND_ERROR = 'not_found_error',
|
|
35
|
+
/** Validation error from user input */
|
|
36
|
+
VALIDATION_ERROR = 'validation_error',
|
|
37
|
+
/** Server error (5xx) */
|
|
38
|
+
SERVER_ERROR = 'server_error',
|
|
39
|
+
/** Client error (4xx) */
|
|
40
|
+
CLIENT_ERROR = 'client_error',
|
|
41
|
+
/** Unknown or unexpected error */
|
|
42
|
+
UNKNOWN_ERROR = 'unknown_error',
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generic error payload structure for error callbacks
|
|
47
|
+
*/
|
|
48
|
+
declare type ErrorPayload = {
|
|
49
|
+
/** Error code indicating the type of error */
|
|
50
|
+
errorCode: ErrorCode;
|
|
51
|
+
/** Human-readable reason for the error */
|
|
52
|
+
reason: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A React wrapper component for the Connect Fund product.
|
|
57
|
+
*
|
|
58
|
+
* @param props - Component properties
|
|
59
|
+
* @param jwt - JWT token for authentication (required)
|
|
60
|
+
* @param env - Target environment ("local" | "dev" | "cert" | "prod", defaults to "prod")
|
|
61
|
+
* @param theme - Theme mode ("auto" | "light" | "dark", defaults to "auto")
|
|
62
|
+
* @param onCompleted - Callback invoked when the sell flow is successfully completed
|
|
63
|
+
* @param onError - Callback invoked when an error occurs
|
|
64
|
+
* @param onClose - Callback invoked when the user closes the widget
|
|
65
|
+
* @param onLoaded - Callback invoked when the widget has finished loading
|
|
66
|
+
* @param onEvent - Callback invoked for general application events
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* // Basic usage with production environment (default)
|
|
71
|
+
* <Fund jwt="your-jwt-token" />
|
|
72
|
+
*
|
|
73
|
+
* // Using cert environment for testing
|
|
74
|
+
* <Fund jwt="your-jwt-token" env="cert" />
|
|
75
|
+
*
|
|
76
|
+
* // Force dark theme
|
|
77
|
+
* <Fund jwt="your-jwt-token" theme="dark" />
|
|
78
|
+
*
|
|
79
|
+
* // With callback functions
|
|
80
|
+
* <Fund
|
|
81
|
+
* jwt="your-jwt-token"
|
|
82
|
+
* theme="auto"
|
|
83
|
+
* onLoaded={() => console.log('Crypto sell loaded and ready')}
|
|
84
|
+
* onCompleted={({ amountSold, assetSymbol }) => console.log('Sold', amountSold, assetSymbol)}
|
|
85
|
+
* onClose={() => console.log('Crypto sell closed')}
|
|
86
|
+
* onError={({ errorCode, reason }) => console.error('Error:', errorCode, reason)}
|
|
87
|
+
* onEvent={(event) => console.log('Event:', event)}
|
|
88
|
+
* />
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @returns React component that renders the Connect Fund interface
|
|
92
|
+
*/
|
|
93
|
+
export declare const Fund: default_2.FC<FundProps>;
|
|
94
|
+
|
|
95
|
+
declare type FundCompletedData = {
|
|
96
|
+
/** Deposit address for the asset */
|
|
97
|
+
depositAddress?: string;
|
|
98
|
+
/** Network used for the deposit */
|
|
99
|
+
network?: string;
|
|
100
|
+
/** Asset symbol (e.g. 'BTC.BITCOIN') */
|
|
101
|
+
assetSymbol?: string;
|
|
102
|
+
/** Amount to be deposited */
|
|
103
|
+
amount?: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Fund event structure
|
|
108
|
+
*/
|
|
109
|
+
declare type FundEvent = AppEvent<FundEventType>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Fund event type literals
|
|
113
|
+
*/
|
|
114
|
+
declare type FundEventType = string;
|
|
115
|
+
|
|
116
|
+
declare interface FundProps {
|
|
117
|
+
/**
|
|
118
|
+
* JWT token used for authentication with the Connect Fund service.
|
|
119
|
+
* This token should be obtained from your backend and contain the necessary
|
|
120
|
+
* claims for user identification and authorization.
|
|
121
|
+
*
|
|
122
|
+
* @example "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
123
|
+
*/
|
|
124
|
+
jwt: string;
|
|
125
|
+
/**
|
|
126
|
+
* Target environment for the Connect Fund service.
|
|
127
|
+
*
|
|
128
|
+
* @default "prod"
|
|
129
|
+
*
|
|
130
|
+
* Available environments:
|
|
131
|
+
* - `"local"` - Local development environment
|
|
132
|
+
* - `"dev"` - Development environment
|
|
133
|
+
* - `"cert"` - Certificate/staging environment for integration testing
|
|
134
|
+
* - `"prod"` - Live production environment for real applications
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```tsx
|
|
138
|
+
* // Use production (default)
|
|
139
|
+
* <Fund jwt={token} />
|
|
140
|
+
*
|
|
141
|
+
* // Use cert for testing
|
|
142
|
+
* <Fund jwt={token} env="cert" />
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
env?: Environment;
|
|
146
|
+
/**
|
|
147
|
+
* Theme mode for the Connect Fund interface.
|
|
148
|
+
*
|
|
149
|
+
* @default "auto"
|
|
150
|
+
*
|
|
151
|
+
* Available themes:
|
|
152
|
+
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
153
|
+
* - `"light"` - Force light theme
|
|
154
|
+
* - `"dark"` - Force dark theme
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```tsx
|
|
158
|
+
* // Auto-detect system preference (default)
|
|
159
|
+
* <Fund jwt={token} />
|
|
160
|
+
*
|
|
161
|
+
* // Force dark theme
|
|
162
|
+
* <Fund jwt={token} theme="dark" />
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
theme?: Theme;
|
|
166
|
+
/**
|
|
167
|
+
* Runs the SDK in Pay mode.
|
|
168
|
+
*
|
|
169
|
+
* - Uses the `pay-sdk` app id and `app:internal:pay` scope when talking to trade-api.
|
|
170
|
+
* - Signs the `account_funding_pay` terms agreement.
|
|
171
|
+
* - Polls deposit status by `transactionId` (single-deposit endpoint) instead of
|
|
172
|
+
* by `deposit_address` (list endpoint).
|
|
173
|
+
*
|
|
174
|
+
* @default false
|
|
175
|
+
*/
|
|
176
|
+
isPay?: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Callback invoked when the fund flow is successfully completed.
|
|
179
|
+
*
|
|
180
|
+
* @param data - Details about the completed deposit (asset symbol, network, amount, deposit address)
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```tsx
|
|
184
|
+
* <Fund
|
|
185
|
+
* jwt={token}
|
|
186
|
+
* onCompleted={({ assetSymbol, amount }) =>
|
|
187
|
+
* console.log(`Deposited ${amount} ${assetSymbol}`)
|
|
188
|
+
* }
|
|
189
|
+
* />
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
onCompleted?: (data: FundCompletedData) => void;
|
|
193
|
+
/**
|
|
194
|
+
* Callback invoked when an error occurs during the fund flow.
|
|
195
|
+
*
|
|
196
|
+
* @param error - Error details including error code and reason
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```tsx
|
|
200
|
+
* <Fund
|
|
201
|
+
* jwt={token}
|
|
202
|
+
* onError={({ errorCode, reason }) =>
|
|
203
|
+
* console.error('Fund error:', errorCode, reason)
|
|
204
|
+
* }
|
|
205
|
+
* />
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
onError?: (error: ErrorPayload) => void;
|
|
209
|
+
/**
|
|
210
|
+
* Callback invoked when the user closes the crypto sell widget.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```tsx
|
|
214
|
+
* <Fund
|
|
215
|
+
* jwt={token}
|
|
216
|
+
* onClose={() => setShowFund(false)}
|
|
217
|
+
* />
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
onClose?: () => void;
|
|
221
|
+
/**
|
|
222
|
+
* Callback invoked when the crypto sell widget has finished loading and is ready.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```tsx
|
|
226
|
+
* <Fund
|
|
227
|
+
* jwt={token}
|
|
228
|
+
* onLoaded={() => setIsLoading(false)}
|
|
229
|
+
* />
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
onLoaded?: () => void;
|
|
233
|
+
/**
|
|
234
|
+
* Callback invoked for general application events during the fund flow.
|
|
235
|
+
*
|
|
236
|
+
* @param event - Event object containing the event type and associated data
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```tsx
|
|
240
|
+
* <Fund
|
|
241
|
+
* jwt={token}
|
|
242
|
+
* onEvent={(event) => console.log('Crypto sell event:', event)}
|
|
243
|
+
* />
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
onEvent?: (event: FundEvent) => void;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Theme mode for the Connect Fund interface.
|
|
251
|
+
*
|
|
252
|
+
* - `"auto"` - Automatically detect system preference (light/dark mode)
|
|
253
|
+
* - `"light"` - Force light theme
|
|
254
|
+
* - `"dark"` - Force dark theme
|
|
255
|
+
*/
|
|
256
|
+
declare type Theme = 'auto' | 'light' | 'dark';
|
|
257
|
+
|
|
258
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import x, { useRef as b, useEffect as l } from "react";
|
|
2
|
+
const y = {
|
|
3
|
+
local: "http://localhost:5173/fund-web/index.js",
|
|
4
|
+
dev: "https://connect-sdk.dev.0hash.com/fund-web/index.js",
|
|
5
|
+
cert: "https://sdk.sandbox.connect.xyz/fund-web/index.js",
|
|
6
|
+
prod: "https://sdk.connect.xyz/fund-web/index.js"
|
|
7
|
+
}, I = "zerohash-fund-script", a = "zerohash-fund", R = (c = "prod") => y[c], w = ({
|
|
8
|
+
jwt: c,
|
|
9
|
+
env: r = "prod",
|
|
10
|
+
theme: h,
|
|
11
|
+
isPay: n = !1,
|
|
12
|
+
onCompleted: s,
|
|
13
|
+
onError: o,
|
|
14
|
+
onClose: d,
|
|
15
|
+
onLoaded: i,
|
|
16
|
+
onEvent: f,
|
|
17
|
+
...m
|
|
18
|
+
}) => {
|
|
19
|
+
const u = b(null);
|
|
20
|
+
return l(() => {
|
|
21
|
+
const t = u.current;
|
|
22
|
+
t && (s && (t.onCompleted = s), o && (t.onError = o), d && (t.onClose = d), i && (t.onLoaded = i), f && (t.onEvent = f), t.isPay = n);
|
|
23
|
+
}, [s, o, d, i, f, n]), l(() => {
|
|
24
|
+
const t = R(r), p = `${I}-${r}`;
|
|
25
|
+
if (document.getElementById(p))
|
|
26
|
+
return;
|
|
27
|
+
const e = document.createElement("script");
|
|
28
|
+
e.id = p, e.src = t, e.type = "module", e.async = !0, e.onerror = () => {
|
|
29
|
+
console.error(`Failed to load the script for ${a} from ${r} environment.`);
|
|
30
|
+
}, document.head.appendChild(e);
|
|
31
|
+
}, [r]), x.createElement(a, {
|
|
32
|
+
ref: u,
|
|
33
|
+
jwt: c,
|
|
34
|
+
env: r,
|
|
35
|
+
theme: h,
|
|
36
|
+
ispay: n ? "true" : void 0,
|
|
37
|
+
...m
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
w as Fund
|
|
42
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zerohash-sdk/fund-react",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"exports": {
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"!**/*.tsbuildinfo"
|
|
21
|
+
],
|
|
22
|
+
"nx": {
|
|
23
|
+
"name": "fund-react"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=18.0.0",
|
|
27
|
+
"react-dom": ">=18.0.0"
|
|
28
|
+
}
|
|
29
|
+
}
|