@avalabs/avacloud-waas-react 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/README.md ADDED
@@ -0,0 +1,287 @@
1
+ # @avalabs/avacloud-waas-react
2
+
3
+ Official React SDK for AvaCloud Authentication and wallet integration. This library provides a seamless way to integrate AvaCloud authentication and wallet functionality into your React applications.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@avalabs/avacloud-waas-react.svg)](https://www.npmjs.com/package/@avalabs/avacloud-waas-react)
6
+ [![License](https://img.shields.io/npm/l/@avalabs/avacloud-waas-react.svg)](https://github.com/ava-labs/avacloud-waas-react/blob/main/LICENSE)
7
+
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ # npm
13
+ npm install @avalabs/avacloud-waas-react
14
+
15
+ # yarn
16
+ yarn add @avalabs/avacloud-waas-react
17
+
18
+ # pnpm
19
+ pnpm add @avalabs/avacloud-waas-react
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ Wrap your application with the `AvaCloudWalletProvider`:
25
+
26
+ ```tsx
27
+ import { AvaCloudWalletProvider } from '@avalabs/avacloud-waas-react';
28
+
29
+ function App() {
30
+ return (
31
+ <AvaCloudWalletProvider
32
+ orgId="your-avacloud-org-id" // Required
33
+ chainId={43113} // Avalanche Fuji Testnet
34
+ >
35
+ <YourApp />
36
+ </AvaCloudWalletProvider>
37
+ );
38
+ }
39
+ ```
40
+
41
+ Use the authentication hooks and components in your application:
42
+
43
+ ```tsx
44
+ import { useAvaCloudWallet, LoginButton, WalletDisplay } from '@avalabs/avacloud-waas-react';
45
+
46
+ function YourComponent() {
47
+ const { isAuthenticated, isLoading, user, wallet } = useAvaCloudWallet();
48
+
49
+ if (isLoading) {
50
+ return <div>Loading...</div>;
51
+ }
52
+
53
+ return (
54
+ <div>
55
+ {isAuthenticated ? (
56
+ <>
57
+ <p>Welcome, {user?.email || 'User'}!</p>
58
+ <WalletDisplay />
59
+ </>
60
+ ) : (
61
+ <LoginButton />
62
+ )}
63
+ </div>
64
+ );
65
+ }
66
+ ```
67
+
68
+ ## Core Components
69
+
70
+ ### AvaCloudWalletProvider
71
+
72
+ The main provider component that wraps your application and provides authentication context.
73
+
74
+ ```tsx
75
+ <AvaCloudWalletProvider
76
+ authServiceUrl="https://auth.avacloud.io"
77
+ chainId={43113}
78
+ darkMode={false}
79
+ onAuthSuccess={(user) => console.log('Auth success', user)}
80
+ onAuthError={(error) => console.error('Auth error', error)}
81
+ onWalletUpdate={(wallet) => console.log('Wallet updated', wallet)}
82
+ >
83
+ {children}
84
+ </AvaCloudWalletProvider>
85
+ ```
86
+
87
+ #### Props
88
+
89
+ | Prop | Type | Description |
90
+ |------|------|-------------|
91
+ | `authServiceUrl` | `string` | URL of the AvaCloud authentication service |
92
+ | `chainId` | `number` | (Optional) EVM chain ID to use (defaults to Avalanche Fuji Testnet - 43113) |
93
+ | `darkMode` | `boolean` | (Optional) Whether to use dark mode for UI components |
94
+ | `onAuthSuccess` | `(user: Auth0User) => void` | (Optional) Callback called when authentication is successful |
95
+ | `onAuthError` | `(error: Error) => void` | (Optional) Callback called when authentication fails |
96
+ | `onWalletUpdate` | `(wallet: WalletInfo) => void` | (Optional) Callback called when wallet information is updated |
97
+
98
+ ### UI Components
99
+
100
+ - `LoginButton`: Button component for initiating the login flow
101
+ - `WalletButton`: Button component for displaying wallet information and actions
102
+ - `WalletDisplay`: Component for displaying wallet address and balance
103
+ - `UserProfile`: Component for displaying user profile information
104
+ - `WalletCard`: Card component for displaying wallet information
105
+ - `TokensView`: Component for displaying token balances
106
+ - `SendView`: Component for sending tokens
107
+ - `ReceiveView`: Component for receiving tokens (displays QR code)
108
+ - `ExportView`: Component for exporting wallet information
109
+
110
+ ## Hooks
111
+
112
+ ### useAvaCloudWallet
113
+
114
+ The main hook for accessing authentication state and methods.
115
+
116
+ ```tsx
117
+ const {
118
+ isAuthenticated,
119
+ isLoading,
120
+ user,
121
+ wallet,
122
+ login,
123
+ logout,
124
+ addAccount,
125
+ } = useAvaCloudWallet();
126
+ ```
127
+
128
+ ### useAuth
129
+
130
+ Simplified hook for basic authentication state.
131
+
132
+ ```tsx
133
+ const { isAuthenticated, login, logout } = useAuth();
134
+ ```
135
+
136
+ ### useSignMessage
137
+
138
+ Hook for signing messages with the connected wallet.
139
+
140
+ ```tsx
141
+ const { signMessage, isLoading, error } = useSignMessage();
142
+
143
+ const handleSign = async () => {
144
+ try {
145
+ const signature = await signMessage('Hello, AvaCloud!');
146
+ console.log('Signature:', signature);
147
+ } catch (error) {
148
+ console.error('Error signing message:', error);
149
+ }
150
+ };
151
+ ```
152
+
153
+ ### useSignTransaction
154
+
155
+ Hook for signing transactions with the connected wallet.
156
+
157
+ ```tsx
158
+ const { signTransaction, isLoading, error } = useSignTransaction();
159
+
160
+ const handleSignTx = async () => {
161
+ try {
162
+ const signedTx = await signTransaction({
163
+ to: '0x...',
164
+ value: '0.1',
165
+ data: '0x...',
166
+ });
167
+ console.log('Signed transaction:', signedTx);
168
+ } catch (error) {
169
+ console.error('Error signing transaction:', error);
170
+ }
171
+ };
172
+ ```
173
+
174
+ ### useTransferTokens
175
+
176
+ Hook for transferring tokens.
177
+
178
+ ```tsx
179
+ const { transfer, isLoading, error } = useTransferTokens();
180
+
181
+ const handleTransfer = async () => {
182
+ try {
183
+ const txHash = await transfer({
184
+ to: '0x...',
185
+ amount: '0.1',
186
+ tokenAddress: '0x...', // Optional, use null for native token
187
+ });
188
+ console.log('Transaction hash:', txHash);
189
+ } catch (error) {
190
+ console.error('Error transferring tokens:', error);
191
+ }
192
+ };
193
+ ```
194
+
195
+ ## Advanced Usage
196
+
197
+ ### Theme Customization
198
+
199
+ Use the `ThemeProvider` to customize the appearance of UI components:
200
+
201
+ ```tsx
202
+ import { ThemeProvider } from '@avalabs/avacloud-waas-react';
203
+
204
+ function App() {
205
+ return (
206
+ <ThemeProvider darkMode={true}>
207
+ <YourApp />
208
+ </ThemeProvider>
209
+ );
210
+ }
211
+ ```
212
+
213
+ ### Custom Authentication Flow
214
+
215
+ You can implement a custom authentication flow using the lower-level hooks:
216
+
217
+ ```tsx
218
+ import { usePostMessage } from '@avalabs/avacloud-waas-react';
219
+
220
+ function CustomAuth() {
221
+ const { sendMessage, lastMessage } = usePostMessage();
222
+
223
+ const handleCustomLogin = () => {
224
+ sendMessage({
225
+ type: 'login',
226
+ payload: {
227
+ // Custom payload
228
+ }
229
+ });
230
+ };
231
+
232
+ // Handle response in useEffect
233
+ useEffect(() => {
234
+ if (lastMessage?.type === 'login_success') {
235
+ // Handle successful login
236
+ }
237
+ }, [lastMessage]);
238
+
239
+ return (
240
+ <button onClick={handleCustomLogin}>
241
+ Custom Login
242
+ </button>
243
+ );
244
+ }
245
+ ```
246
+
247
+ ## Browser Support
248
+
249
+ - Chrome (latest)
250
+ - Firefox (latest)
251
+ - Safari (latest)
252
+ - Edge (latest)
253
+
254
+ ## Contributing
255
+
256
+ We welcome contributions! Please see [CONTRIBUTING.md](https://github.com/ava-labs/avacloud-waas-react/blob/main/CONTRIBUTING.md) for details.
257
+
258
+ ## License
259
+
260
+ MIT © [Ava Labs, Inc.](https://github.com/ava-labs)
261
+
262
+ ## Using the AvaCloud Organization ID
263
+
264
+ The `AvaCloudWalletProvider` requires an AvaCloud organization ID (`orgId`). This will be used to fetch the organization configuration from the AvaCloud API, which includes the mapping to the appropriate Cubist organization ID used for wallet operations.
265
+
266
+ ```jsx
267
+ import { AvaCloudWalletProvider } from '@avalabs/avacloud-waas-react';
268
+
269
+ function App() {
270
+ return (
271
+ <AvaCloudWalletProvider
272
+ authServiceUrl="https://ac-auth-service.vercel.app/"
273
+ orgId="your-avacloud-org-id" // Required AvaCloud organization ID
274
+ >
275
+ <YourApp />
276
+ </AvaCloudWalletProvider>
277
+ );
278
+ }
279
+ ```
280
+
281
+ When provided, the `orgId` will be used to:
282
+
283
+ 1. Fetch the organization configuration from the AvaCloud API
284
+ 2. Map to a Cubist organization ID as specified in the configuration
285
+ 3. Use the mapped ID for Cubist authentication and wallet operations
286
+
287
+ This enables organizations to have their AvaCloud identity seamlessly map to their Cubist wallets.