@aurum-sdk/hooks 0.1.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,196 @@
1
+ # @aurum-sdk/hooks
2
+
3
+ React hooks for Aurum SDK
4
+
5
+ **Looking for the core SDK? Check out @aurum-sdk/core on [github](https://github.com/aurum-sdk/aurum/packages/sdk) or [npm](https://www.npmjs.com/package/@aurum-sdk/core)**
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pnpm add @aurum-sdk/hooks @aurum-sdk/core
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Setup Provider
16
+
17
+ Wrap your app with `AurumProvider`
18
+
19
+ ```tsx
20
+ import { Aurum } from '@aurum-sdk/core';
21
+ import { AurumProvider } from '@aurum-sdk/hooks';
22
+
23
+ const aurum = new Aurum({
24
+ brand: { appName: 'Your App Name' },
25
+ wallets: {
26
+ email: { projectId: 'cdp-project-id' },
27
+ walletConnect: { projectId: 'reown-project-id' },
28
+ },
29
+ });
30
+
31
+ function App() {
32
+ return (
33
+ <AurumProvider aurum={aurum}>
34
+ <YourApp />
35
+ </AurumProvider>
36
+ );
37
+ }
38
+ ```
39
+
40
+ ### 2. Use Hooks
41
+
42
+ ```tsx
43
+ import { useAccount, useConnect, useDisconnect } from '@aurum-sdk/hooks';
44
+
45
+ function WalletButton() {
46
+ const { publicAddress, isConnected, isInitializing } = useAccount();
47
+ const { connect, isPending } = useConnect();
48
+ const { disconnect } = useDisconnect();
49
+
50
+ if (isInitializing) return <div>Loading...</div>;
51
+
52
+ if (!isConnected) {
53
+ return (
54
+ <button onClick={() => connect()} disabled={isPending}>
55
+ {isPending ? 'Connecting...' : 'Connect Wallet'}
56
+ </button>
57
+ );
58
+ }
59
+
60
+ return (
61
+ <div>
62
+ <p>{publicAddress}</p>
63
+ <button onClick={disconnect}>Disconnect</button>
64
+ </div>
65
+ );
66
+ }
67
+ ```
68
+
69
+ ## Available Hooks
70
+
71
+ ### `useAurum`
72
+
73
+ Access the raw Aurum SDK instance.
74
+
75
+ ```tsx
76
+ const { aurum, isReady } = useAurum();
77
+ ```
78
+
79
+ | Return | Type | Description |
80
+ | --------- | --------- | ----------------------------------------- |
81
+ | `aurum` | `Aurum` | The Aurum SDK instance |
82
+ | `isReady` | `boolean` | Whether the SDK has finished initializing |
83
+
84
+ ### `useAccount`
85
+
86
+ Access connected user information.
87
+
88
+ ```tsx
89
+ const { publicAddress, walletName, walletId, email, isConnected, isInitializing } = useAccount();
90
+ ```
91
+
92
+ | Return | Type | Description |
93
+ | ---------------- | ------------------------- | -------------------------------------- |
94
+ | `publicAddress` | `string \| undefined` | The connected wallet address |
95
+ | `walletName` | `WalletName \| undefined` | Name of the connected wallet |
96
+ | `walletId` | `WalletId \| undefined` | ID of the connected wallet |
97
+ | `email` | `string \| undefined` | Email address when logged in via email |
98
+ | `isConnected` | `boolean` | Whether a wallet is connected |
99
+ | `isInitializing` | `boolean` | Whether the SDK is initializing |
100
+
101
+ ### `useConnect`
102
+
103
+ Connect to a wallet via modal, direct connection, or headless flows.
104
+
105
+ ```tsx
106
+ const { connect, emailAuthStart, emailAuthVerify, getWalletConnectSession, isPending, error } = useConnect();
107
+
108
+ // Open wallet selection modal
109
+ await connect();
110
+
111
+ // Or connect directly to a specific wallet (skips modal)
112
+ import { WalletId } from '@aurum-sdk/types';
113
+ await connect(WalletId.MetaMask);
114
+ ```
115
+
116
+ | Return | Type | Description |
117
+ | ------------------------- | -------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
118
+ | `connect` | `(walletId?: WalletId) => Promise<string>` | Opens wallet modal, or connects directly if walletId provided |
119
+ | `emailAuthStart` | `(email: string) => Promise<{ flowId: string }>` | Sends OTP to email for Coinbase Embedded Wallet |
120
+ | `emailAuthVerify` | `(flowId: string, otp: string) => Promise<{ address: string, email: string, isNewUser: boolean }>` | Verifies OTP and completes connection |
121
+ | `getWalletConnectSession` | `() => Promise<{ uri: string, waitForConnection: () => Promise<string> }>` | Gets WalletConnect URI for custom QR display |
122
+ | `isPending` | `boolean` | Whether connection is in progress |
123
+ | `error` | `Error \| null` | Error from last connection attempt |
124
+
125
+ **Note:** `WalletId.Email` and `WalletId.WalletConnect` cannot be used with `connect(walletId)` — use the headless methods below instead.
126
+
127
+ > **ConnectWidget Compatibility:** Do not use `useConnect()` with `<ConnectWidget>`. Use `useAccount()` to react to connection state changes instead.
128
+
129
+ #### Headless Email Authentication
130
+
131
+ Two-step flow for Coinbase Embedded Wallet:
132
+
133
+ ```tsx
134
+ const { emailAuthStart, emailAuthVerify, isPending, error } = useConnect();
135
+
136
+ // Step 1: Send OTP
137
+ const { flowId } = await emailAuthStart('user@example.com');
138
+
139
+ // Step 2: User enters OTP from their email
140
+ const otp = await promptUserForOTP();
141
+
142
+ // Step 3: Verify and connect
143
+ const { address, email } = await emailAuthVerify(flowId, otp);
144
+ ```
145
+
146
+ #### Headless WalletConnect
147
+
148
+ For custom QR code displays:
149
+
150
+ ```tsx
151
+ const { getWalletConnectSession, isPending, error } = useConnect();
152
+
153
+ const { uri, waitForConnection } = await getWalletConnectSession();
154
+
155
+ // Display your own QR code
156
+ renderQRCode(uri);
157
+
158
+ // Wait for user to scan and approve
159
+ const address = await waitForConnection();
160
+ ```
161
+
162
+ ### `useDisconnect`
163
+
164
+ Disconnect the current wallet.
165
+
166
+ ```tsx
167
+ const { disconnect } = useDisconnect();
168
+
169
+ await disconnect();
170
+ ```
171
+
172
+ | Return | Type | Description |
173
+ | ------------ | --------------------- | ------------------------------ |
174
+ | `disconnect` | `() => Promise<void>` | Disconnects the current wallet |
175
+
176
+ ### `useChain`
177
+
178
+ Access chain information and switch chains.
179
+
180
+ ```tsx
181
+ import { sepolia } from 'viem/chains';
182
+
183
+ const { chainId, switchChain, error } = useChain();
184
+
185
+ await switchChain(sepolia.id, sepolia);
186
+ ```
187
+
188
+ | Return | Type | Description |
189
+ | ------------- | ------------------------------------ | ------------------------------ |
190
+ | `chainId` | `number \| null` | Current chain ID |
191
+ | `switchChain` | `(chainId, chain?) => Promise<void>` | Switch to a different chain |
192
+ | `error` | `Error \| null` | Error from last switch attempt |
193
+
194
+ ## License
195
+
196
+ MIT
@@ -0,0 +1,135 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import * as _aurum_sdk_core from '@aurum-sdk/core';
4
+ import { Aurum } from '@aurum-sdk/core';
5
+ import * as _aurum_sdk_types from '@aurum-sdk/types';
6
+ import { WalletId, WalletConnectSessionResult, WalletName } from '@aurum-sdk/types';
7
+ import { Chain } from 'viem';
8
+
9
+ interface AurumProviderProps {
10
+ aurum: Aurum;
11
+ children: ReactNode;
12
+ }
13
+ declare function AurumProvider({ aurum, children }: AurumProviderProps): react_jsx_runtime.JSX.Element;
14
+
15
+ /**
16
+ * Access the raw Aurum SDK instance.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * const { aurum, isReady } = useAurum();
21
+ *
22
+ * if (isReady) {
23
+ * const chainId = await aurum.getChainId();
24
+ * }
25
+ * ```
26
+ */
27
+ declare function useAurum(): {
28
+ aurum: _aurum_sdk_core.Aurum;
29
+ isReady: boolean;
30
+ };
31
+
32
+ /**
33
+ * Access connected user information.
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * const { publicAddress, walletName, isConnected, isInitializing } = useAccount();
38
+ *
39
+ * if (isInitializing) return <div>Loading...</div>;
40
+ *
41
+ * if (isConnected) {
42
+ * return <div>Connected: {publicAddress}</div>;
43
+ * }
44
+ * ```
45
+ */
46
+ declare function useAccount(): {
47
+ publicAddress: string | undefined;
48
+ walletName: _aurum_sdk_types.WalletName | undefined;
49
+ walletId: _aurum_sdk_types.WalletId | undefined;
50
+ email: string | undefined;
51
+ isConnected: boolean;
52
+ isInitializing: boolean;
53
+ };
54
+
55
+ /**
56
+ * Connect to a wallet.
57
+ *
58
+ * @example
59
+ * ```tsx
60
+ * const { connect, emailAuthStart, emailAuthVerify, getWalletConnectSession, isPending, error } = useConnect();
61
+ *
62
+ * // Open wallet selection modal
63
+ * await connect();
64
+ *
65
+ * // Or connect directly to a specific wallet
66
+ * await connect(WalletId.MetaMask);
67
+ *
68
+ * // Or use headless email auth
69
+ * const { flowId } = await emailAuthStart('user@example.com');
70
+ * const { address, email } = await emailAuthVerify(flowId, '123456');
71
+ *
72
+ * // Or use headless WalletConnect
73
+ * const { uri, waitForConnection } = await getWalletConnectSession();
74
+ * // Display your own QR code with `uri`
75
+ * const address = await waitForConnection();
76
+ * ```
77
+ */
78
+ declare function useConnect(): {
79
+ connect: (walletId?: WalletId) => Promise<`0x${string}`>;
80
+ emailAuthStart: (email: string) => Promise<_aurum_sdk_types.EmailAuthStartResult>;
81
+ emailAuthVerify: (flowId: string, otp: string) => Promise<_aurum_sdk_types.EmailAuthVerifyResult>;
82
+ getWalletConnectSession: () => Promise<WalletConnectSessionResult>;
83
+ isPending: boolean;
84
+ error: Error | null;
85
+ };
86
+
87
+ /**
88
+ * Disconnect the current wallet.
89
+ *
90
+ * @example
91
+ * ```tsx
92
+ * const { disconnect } = useDisconnect();
93
+ *
94
+ * return <button onClick={disconnect}>Disconnect</button>;
95
+ * ```
96
+ */
97
+ declare function useDisconnect(): {
98
+ disconnect: () => Promise<void>;
99
+ };
100
+
101
+ /**
102
+ * Access chain information and switch chains.
103
+ *
104
+ * @example
105
+ * ```tsx
106
+ * import { sepolia } from 'viem/chains';
107
+ *
108
+ * const { chainId, switchChain, error } = useChain();
109
+ *
110
+ * return (
111
+ * <div>
112
+ * <p>Chain ID: {chainId}</p>
113
+ * <button onClick={() => switchChain(sepolia.id, sepolia)}>
114
+ * Switch to Sepolia
115
+ * </button>
116
+ * </div>
117
+ * );
118
+ * ```
119
+ */
120
+ declare function useChain(): {
121
+ chainId: number | null;
122
+ switchChain: (targetChainId: number | string | `0x${string}`, chain?: Chain) => Promise<void>;
123
+ error: Error | null;
124
+ };
125
+
126
+ interface AccountState {
127
+ publicAddress: string | undefined;
128
+ walletName: WalletName | undefined;
129
+ walletId: WalletId | undefined;
130
+ email: string | undefined;
131
+ isConnected: boolean;
132
+ isInitializing: boolean;
133
+ }
134
+
135
+ export { type AccountState, AurumProvider, useAccount, useAurum, useChain, useConnect, useDisconnect };
@@ -0,0 +1,135 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import * as _aurum_sdk_core from '@aurum-sdk/core';
4
+ import { Aurum } from '@aurum-sdk/core';
5
+ import * as _aurum_sdk_types from '@aurum-sdk/types';
6
+ import { WalletId, WalletConnectSessionResult, WalletName } from '@aurum-sdk/types';
7
+ import { Chain } from 'viem';
8
+
9
+ interface AurumProviderProps {
10
+ aurum: Aurum;
11
+ children: ReactNode;
12
+ }
13
+ declare function AurumProvider({ aurum, children }: AurumProviderProps): react_jsx_runtime.JSX.Element;
14
+
15
+ /**
16
+ * Access the raw Aurum SDK instance.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * const { aurum, isReady } = useAurum();
21
+ *
22
+ * if (isReady) {
23
+ * const chainId = await aurum.getChainId();
24
+ * }
25
+ * ```
26
+ */
27
+ declare function useAurum(): {
28
+ aurum: _aurum_sdk_core.Aurum;
29
+ isReady: boolean;
30
+ };
31
+
32
+ /**
33
+ * Access connected user information.
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * const { publicAddress, walletName, isConnected, isInitializing } = useAccount();
38
+ *
39
+ * if (isInitializing) return <div>Loading...</div>;
40
+ *
41
+ * if (isConnected) {
42
+ * return <div>Connected: {publicAddress}</div>;
43
+ * }
44
+ * ```
45
+ */
46
+ declare function useAccount(): {
47
+ publicAddress: string | undefined;
48
+ walletName: _aurum_sdk_types.WalletName | undefined;
49
+ walletId: _aurum_sdk_types.WalletId | undefined;
50
+ email: string | undefined;
51
+ isConnected: boolean;
52
+ isInitializing: boolean;
53
+ };
54
+
55
+ /**
56
+ * Connect to a wallet.
57
+ *
58
+ * @example
59
+ * ```tsx
60
+ * const { connect, emailAuthStart, emailAuthVerify, getWalletConnectSession, isPending, error } = useConnect();
61
+ *
62
+ * // Open wallet selection modal
63
+ * await connect();
64
+ *
65
+ * // Or connect directly to a specific wallet
66
+ * await connect(WalletId.MetaMask);
67
+ *
68
+ * // Or use headless email auth
69
+ * const { flowId } = await emailAuthStart('user@example.com');
70
+ * const { address, email } = await emailAuthVerify(flowId, '123456');
71
+ *
72
+ * // Or use headless WalletConnect
73
+ * const { uri, waitForConnection } = await getWalletConnectSession();
74
+ * // Display your own QR code with `uri`
75
+ * const address = await waitForConnection();
76
+ * ```
77
+ */
78
+ declare function useConnect(): {
79
+ connect: (walletId?: WalletId) => Promise<`0x${string}`>;
80
+ emailAuthStart: (email: string) => Promise<_aurum_sdk_types.EmailAuthStartResult>;
81
+ emailAuthVerify: (flowId: string, otp: string) => Promise<_aurum_sdk_types.EmailAuthVerifyResult>;
82
+ getWalletConnectSession: () => Promise<WalletConnectSessionResult>;
83
+ isPending: boolean;
84
+ error: Error | null;
85
+ };
86
+
87
+ /**
88
+ * Disconnect the current wallet.
89
+ *
90
+ * @example
91
+ * ```tsx
92
+ * const { disconnect } = useDisconnect();
93
+ *
94
+ * return <button onClick={disconnect}>Disconnect</button>;
95
+ * ```
96
+ */
97
+ declare function useDisconnect(): {
98
+ disconnect: () => Promise<void>;
99
+ };
100
+
101
+ /**
102
+ * Access chain information and switch chains.
103
+ *
104
+ * @example
105
+ * ```tsx
106
+ * import { sepolia } from 'viem/chains';
107
+ *
108
+ * const { chainId, switchChain, error } = useChain();
109
+ *
110
+ * return (
111
+ * <div>
112
+ * <p>Chain ID: {chainId}</p>
113
+ * <button onClick={() => switchChain(sepolia.id, sepolia)}>
114
+ * Switch to Sepolia
115
+ * </button>
116
+ * </div>
117
+ * );
118
+ * ```
119
+ */
120
+ declare function useChain(): {
121
+ chainId: number | null;
122
+ switchChain: (targetChainId: number | string | `0x${string}`, chain?: Chain) => Promise<void>;
123
+ error: Error | null;
124
+ };
125
+
126
+ interface AccountState {
127
+ publicAddress: string | undefined;
128
+ walletName: WalletName | undefined;
129
+ walletId: WalletId | undefined;
130
+ email: string | undefined;
131
+ isConnected: boolean;
132
+ isInitializing: boolean;
133
+ }
134
+
135
+ export { type AccountState, AurumProvider, useAccount, useAurum, useChain, useConnect, useDisconnect };
package/dist/index.js ADDED
@@ -0,0 +1,251 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+
6
+ // src/AurumProvider.tsx
7
+ var initialAccountState = {
8
+ publicAddress: void 0,
9
+ walletName: void 0,
10
+ walletId: void 0,
11
+ email: void 0,
12
+ isConnected: false,
13
+ isInitializing: true
14
+ };
15
+ var AurumContext = react.createContext(null);
16
+ function useAurumContext() {
17
+ const context = react.useContext(AurumContext);
18
+ if (!context) {
19
+ throw new Error("useAurumContext must be used within a AurumProvider");
20
+ }
21
+ return context;
22
+ }
23
+ function AurumProvider({ aurum, children }) {
24
+ const [isReady, setIsReady] = react.useState(false);
25
+ const accountStateRef = react.useRef(initialAccountState);
26
+ const listenersRef = react.useRef(/* @__PURE__ */ new Set());
27
+ const notifyListeners = react.useCallback(() => {
28
+ listenersRef.current.forEach((listener) => listener());
29
+ }, []);
30
+ const syncState = react.useCallback(async () => {
31
+ try {
32
+ const userInfo = await aurum.getUserInfo();
33
+ const isConnected = await aurum.isConnected();
34
+ const newState = {
35
+ publicAddress: userInfo?.publicAddress,
36
+ walletName: userInfo?.walletName,
37
+ walletId: userInfo?.walletId,
38
+ email: userInfo?.email,
39
+ isConnected,
40
+ isInitializing: false
41
+ };
42
+ accountStateRef.current = newState;
43
+ notifyListeners();
44
+ } catch {
45
+ accountStateRef.current = {
46
+ ...initialAccountState,
47
+ isInitializing: false
48
+ };
49
+ notifyListeners();
50
+ }
51
+ }, [aurum, notifyListeners]);
52
+ react.useEffect(() => {
53
+ let mounted = true;
54
+ const initialize = async () => {
55
+ await aurum.whenReady();
56
+ if (!mounted) return;
57
+ setIsReady(true);
58
+ await syncState();
59
+ };
60
+ initialize();
61
+ const handleAccountsChanged = () => {
62
+ if (mounted) syncState();
63
+ };
64
+ const handleChainChanged = () => {
65
+ if (mounted) syncState();
66
+ };
67
+ aurum.rpcProvider?.on?.("accountsChanged", handleAccountsChanged);
68
+ aurum.rpcProvider?.on?.("chainChanged", handleChainChanged);
69
+ return () => {
70
+ mounted = false;
71
+ aurum.rpcProvider?.removeListener?.("accountsChanged", handleAccountsChanged);
72
+ aurum.rpcProvider?.removeListener?.("chainChanged", handleChainChanged);
73
+ };
74
+ }, [aurum, syncState]);
75
+ const subscribe = react.useCallback((callback) => {
76
+ listenersRef.current.add(callback);
77
+ return () => {
78
+ listenersRef.current.delete(callback);
79
+ };
80
+ }, []);
81
+ const getSnapshot = react.useCallback(() => {
82
+ return accountStateRef.current;
83
+ }, []);
84
+ const getServerSnapshot = react.useCallback(() => {
85
+ return initialAccountState;
86
+ }, []);
87
+ const contextValue = react.useMemo(
88
+ () => ({
89
+ aurum,
90
+ isReady,
91
+ subscribe,
92
+ getSnapshot,
93
+ getServerSnapshot
94
+ }),
95
+ [aurum, isReady, subscribe, getSnapshot, getServerSnapshot]
96
+ );
97
+ return /* @__PURE__ */ jsxRuntime.jsx(AurumContext.Provider, { value: contextValue, children });
98
+ }
99
+
100
+ // src/hooks/useAurum.ts
101
+ function useAurum() {
102
+ const { aurum, isReady } = useAurumContext();
103
+ return { aurum, isReady };
104
+ }
105
+ function useAccount() {
106
+ const { subscribe, getSnapshot, getServerSnapshot } = useAurumContext();
107
+ const account = react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
108
+ return {
109
+ publicAddress: account.publicAddress,
110
+ walletName: account.walletName,
111
+ walletId: account.walletId,
112
+ email: account.email,
113
+ isConnected: account.isConnected,
114
+ isInitializing: account.isInitializing
115
+ };
116
+ }
117
+ function useConnect() {
118
+ const { aurum } = useAurumContext();
119
+ const [isPending, setIsPending] = react.useState(false);
120
+ const [error, setError] = react.useState(null);
121
+ const connect = react.useCallback(
122
+ async (walletId) => {
123
+ setIsPending(true);
124
+ setError(null);
125
+ try {
126
+ const address = await aurum.connect(walletId);
127
+ return address;
128
+ } catch (err) {
129
+ const error2 = err instanceof Error ? err : new Error("User rejected connection");
130
+ setError(error2);
131
+ throw error2;
132
+ } finally {
133
+ setIsPending(false);
134
+ }
135
+ },
136
+ [aurum]
137
+ );
138
+ const emailAuthStart = react.useCallback(
139
+ async (email) => {
140
+ setIsPending(true);
141
+ setError(null);
142
+ try {
143
+ const result = await aurum.emailAuthStart(email);
144
+ return result;
145
+ } catch (err) {
146
+ const error2 = err instanceof Error ? err : new Error("Failed to start email auth");
147
+ setError(error2);
148
+ throw error2;
149
+ } finally {
150
+ setIsPending(false);
151
+ }
152
+ },
153
+ [aurum]
154
+ );
155
+ const emailAuthVerify = react.useCallback(
156
+ async (flowId, otp) => {
157
+ setIsPending(true);
158
+ setError(null);
159
+ try {
160
+ const result = await aurum.emailAuthVerify(flowId, otp);
161
+ return result;
162
+ } catch (err) {
163
+ const error2 = err instanceof Error ? err : new Error("Failed to verify email");
164
+ setError(error2);
165
+ throw error2;
166
+ } finally {
167
+ setIsPending(false);
168
+ }
169
+ },
170
+ [aurum]
171
+ );
172
+ const getWalletConnectSession = react.useCallback(async () => {
173
+ setIsPending(true);
174
+ setError(null);
175
+ try {
176
+ const result = await aurum.getWalletConnectSession();
177
+ return result;
178
+ } catch (err) {
179
+ const error2 = err instanceof Error ? err : new Error("Failed to get WalletConnect session");
180
+ setError(error2);
181
+ throw error2;
182
+ } finally {
183
+ setIsPending(false);
184
+ }
185
+ }, [aurum]);
186
+ return {
187
+ connect,
188
+ emailAuthStart,
189
+ emailAuthVerify,
190
+ getWalletConnectSession,
191
+ isPending,
192
+ error
193
+ };
194
+ }
195
+ function useDisconnect() {
196
+ const { aurum } = useAurumContext();
197
+ const disconnect = react.useCallback(async () => {
198
+ await aurum.disconnect();
199
+ }, [aurum]);
200
+ return { disconnect };
201
+ }
202
+ function useChain() {
203
+ const { aurum, isReady } = useAurumContext();
204
+ const [chainId, setChainId] = react.useState(null);
205
+ const [error, setError] = react.useState(null);
206
+ react.useEffect(() => {
207
+ if (!isReady) return;
208
+ const fetchChainId = async () => {
209
+ try {
210
+ const id = await aurum.getChainId();
211
+ setChainId(id);
212
+ } catch {
213
+ setChainId(null);
214
+ }
215
+ };
216
+ fetchChainId();
217
+ const handleChainChanged = (newChainId) => {
218
+ setChainId(Number(newChainId));
219
+ };
220
+ aurum.rpcProvider?.on?.("chainChanged", handleChainChanged);
221
+ return () => {
222
+ aurum.rpcProvider?.removeListener?.("chainChanged", handleChainChanged);
223
+ };
224
+ }, [aurum, isReady]);
225
+ const switchChain = react.useCallback(
226
+ async (targetChainId, chain) => {
227
+ if (!targetChainId) {
228
+ throw new Error("chainId is required");
229
+ }
230
+ setError(null);
231
+ try {
232
+ await aurum.switchChain(targetChainId, chain);
233
+ } catch (err) {
234
+ const error2 = err instanceof Error ? err : new Error("Failed to switch chain");
235
+ setError(error2);
236
+ throw error2;
237
+ }
238
+ },
239
+ [aurum]
240
+ );
241
+ return { chainId, switchChain, error };
242
+ }
243
+
244
+ exports.AurumProvider = AurumProvider;
245
+ exports.useAccount = useAccount;
246
+ exports.useAurum = useAurum;
247
+ exports.useChain = useChain;
248
+ exports.useConnect = useConnect;
249
+ exports.useDisconnect = useDisconnect;
250
+ //# sourceMappingURL=index.js.map
251
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AurumContext.ts","../src/AurumProvider.tsx","../src/hooks/useAurum.ts","../src/hooks/useAccount.ts","../src/hooks/useConnect.ts","../src/hooks/useDisconnect.ts","../src/hooks/useChain.ts"],"names":["createContext","useContext","useState","useRef","useCallback","useEffect","useMemo","useSyncExternalStore","error"],"mappings":";;;;;;AAuBO,IAAM,mBAAA,GAAoC;AAAA,EAC/C,aAAA,EAAe,MAAA;AAAA,EACf,UAAA,EAAY,MAAA;AAAA,EACZ,QAAA,EAAU,MA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AAM;AACX,MAAA,KAAA,CAAM,WAAA,EAAa,cAAA,GAAiB,cAAA,EAAgB,kBAAkB,CAAA;AAAA,IACxE,CAAA;AAAA,EACF,CAAA,EAAG,CAAC,KAAA,EAAO,OAAO,CAAC,CAAA;AAEnB,EAAA,MAAM,WAAA,GAAcD,iBAAAA;AAAA,IAClB,OAAO,eAAgD,KAAA,KAAkB;AACvE,MAAA,IAAI,CAAC,aAAA,EAAe;AAClB,QAAA,MAAM,IAAI,MAAM,qBAAqB,CAAA;AAAA,MACvC;AACA,MAAA,QAAA,CAAS,IAAI,CAAA;AAEb,MAAA,IAAI;AACF,QAAA,MAAM,KAAA,CAAM,WAAA,CAAY,aAAA,EAAkD,KAAK,CAAA;AAAA,MAEjF,SAAS,GAAA,EAAK;AACZ,QAAA,MAAMI,SAAQ,GAAA,YAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,MAAM,wBAAwB,CAAA;AAC7E,QAAA,QAAA,CAASA,MAAK,CAAA;AACd,QAAA,MAAMA,MAAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,IACA,CAAC,KAAK;AAAA,GACR;AAEA,EAAA,OAAO,EAAE,OAAA,EAAS,WAAA,EAAa,KAAA,EAAM;AACvC","file":"index.js","sourcesContent":["'use client';\n\nimport { createContext, useContext } from 'react';\nimport type { Aurum } from '@aurum-sdk/core';\nimport type { WalletId, WalletName } from '@aurum-sdk/types';\n\nexport interface AccountState {\n publicAddress: string | undefined;\n walletName: WalletName | undefined;\n walletId: WalletId | undefined;\n email: string | undefined;\n isConnected: boolean;\n isInitializing: boolean;\n}\n\ninterface AurumContextValue {\n aurum: Aurum;\n isReady: boolean;\n subscribe: (callback: () => void) => () => void;\n getSnapshot: () => AccountState;\n getServerSnapshot: () => AccountState;\n}\n\nexport const initialAccountState: AccountState = {\n publicAddress: undefined,\n walletName: undefined,\n walletId: undefined,\n email: undefined,\n isConnected: false,\n isInitializing: true,\n};\n\nexport const AurumContext = createContext<AurumContextValue | null>(null);\n\nexport function useAurumContext(): AurumContextValue {\n const context = useContext(AurumContext);\n if (!context) {\n throw new Error('useAurumContext must be used within a AurumProvider');\n }\n return context;\n}\n","'use client';\n\nimport { useRef, useEffect, useCallback, useState, useMemo, type ReactNode } from 'react';\nimport { AurumContext, initialAccountState, type AccountState } from '@src/AurumContext';\nimport type { Aurum } from '@aurum-sdk/core';\n\ninterface AurumProviderProps {\n aurum: Aurum;\n children: ReactNode;\n}\n\nexport function AurumProvider({ aurum, children }: AurumProviderProps) {\n const [isReady, setIsReady] = useState(false);\n const accountStateRef = useRef<AccountState>(initialAccountState);\n const listenersRef = useRef<Set<() => void>>(new Set());\n\n // Notify all subscribers of state changes\n const notifyListeners = useCallback(() => {\n listenersRef.current.forEach((listener) => listener());\n }, []);\n\n // Sync state from SDK\n const syncState = useCallback(async () => {\n try {\n const userInfo = await aurum.getUserInfo();\n const isConnected = await aurum.isConnected();\n\n const newState: AccountState = {\n publicAddress: userInfo?.publicAddress,\n walletName: userInfo?.walletName,\n walletId: userInfo?.walletId,\n email: userInfo?.email,\n isConnected,\n isInitializing: false,\n };\n\n accountStateRef.current = newState;\n notifyListeners();\n } catch {\n accountStateRef.current = {\n ...initialAccountState,\n isInitializing: false,\n };\n notifyListeners();\n }\n }, [aurum, notifyListeners]);\n\n // Initialize SDK and set up event listeners\n useEffect(() => {\n let mounted = true;\n\n const initialize = async () => {\n await aurum.whenReady();\n if (!mounted) return;\n\n setIsReady(true);\n await syncState();\n };\n\n initialize();\n\n // Subscribe to provider events for account changes\n const handleAccountsChanged = () => {\n if (mounted) syncState();\n };\n\n const handleChainChanged = () => {\n if (mounted) syncState();\n };\n\n aurum.rpcProvider?.on?.('accountsChanged', handleAccountsChanged);\n aurum.rpcProvider?.on?.('chainChanged', handleChainChanged);\n\n return () => {\n mounted = false;\n aurum.rpcProvider?.removeListener?.('accountsChanged', handleAccountsChanged);\n aurum.rpcProvider?.removeListener?.('chainChanged', handleChainChanged);\n };\n }, [aurum, syncState]);\n\n // Subscribe function for useSyncExternalStore\n const subscribe = useCallback((callback: () => void) => {\n listenersRef.current.add(callback);\n return () => {\n listenersRef.current.delete(callback);\n };\n }, []);\n\n // Get current snapshot for useSyncExternalStore\n const getSnapshot = useCallback(() => {\n return accountStateRef.current;\n }, []);\n\n // Server snapshot - always returns initial state\n const getServerSnapshot = useCallback(() => {\n return initialAccountState;\n }, []);\n\n const contextValue = useMemo(\n () => ({\n aurum,\n isReady,\n subscribe,\n getSnapshot,\n getServerSnapshot,\n }),\n [aurum, isReady, subscribe, getSnapshot, getServerSnapshot],\n );\n\n return <AurumContext.Provider value={contextValue}>{children}</AurumContext.Provider>;\n}\n","'use client';\n\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access the raw Aurum SDK instance.\n *\n * @example\n * ```tsx\n * const { aurum, isReady } = useAurum();\n *\n * if (isReady) {\n * const chainId = await aurum.getChainId();\n * }\n * ```\n */\nexport function useAurum() {\n const { aurum, isReady } = useAurumContext();\n return { aurum, isReady };\n}\n","'use client';\n\nimport { useSyncExternalStore } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access connected user information.\n *\n * @example\n * ```tsx\n * const { publicAddress, walletName, isConnected, isInitializing } = useAccount();\n *\n * if (isInitializing) return <div>Loading...</div>;\n *\n * if (isConnected) {\n * return <div>Connected: {publicAddress}</div>;\n * }\n * ```\n */\nexport function useAccount() {\n const { subscribe, getSnapshot, getServerSnapshot } = useAurumContext();\n\n const account = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n\n return {\n publicAddress: account.publicAddress,\n walletName: account.walletName,\n walletId: account.walletId,\n email: account.email,\n isConnected: account.isConnected,\n isInitializing: account.isInitializing,\n };\n}\n","'use client';\n\nimport { useState, useCallback } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\nimport type { WalletId, WalletConnectSessionResult } from '@aurum-sdk/types';\n\n/**\n * Connect to a wallet.\n *\n * @example\n * ```tsx\n * const { connect, emailAuthStart, emailAuthVerify, getWalletConnectSession, isPending, error } = useConnect();\n *\n * // Open wallet selection modal\n * await connect();\n *\n * // Or connect directly to a specific wallet\n * await connect(WalletId.MetaMask);\n *\n * // Or use headless email auth\n * const { flowId } = await emailAuthStart('user@example.com');\n * const { address, email } = await emailAuthVerify(flowId, '123456');\n *\n * // Or use headless WalletConnect\n * const { uri, waitForConnection } = await getWalletConnectSession();\n * // Display your own QR code with `uri`\n * const address = await waitForConnection();\n * ```\n */\nexport function useConnect() {\n const { aurum } = useAurumContext();\n\n const [isPending, setIsPending] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const connect = useCallback(\n async (walletId?: WalletId) => {\n setIsPending(true);\n setError(null);\n\n try {\n const address = await aurum.connect(walletId);\n return address;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('User rejected connection');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const emailAuthStart = useCallback(\n async (email: string) => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.emailAuthStart(email);\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to start email auth');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const emailAuthVerify = useCallback(\n async (flowId: string, otp: string) => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.emailAuthVerify(flowId, otp);\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to verify email');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const getWalletConnectSession = useCallback(async (): Promise<WalletConnectSessionResult> => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.getWalletConnectSession();\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to get WalletConnect session');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n }, [aurum]);\n\n return {\n connect,\n emailAuthStart,\n emailAuthVerify,\n getWalletConnectSession,\n isPending,\n error,\n };\n}\n","'use client';\n\nimport { useCallback } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Disconnect the current wallet.\n *\n * @example\n * ```tsx\n * const { disconnect } = useDisconnect();\n *\n * return <button onClick={disconnect}>Disconnect</button>;\n * ```\n */\nexport function useDisconnect() {\n const { aurum } = useAurumContext();\n\n const disconnect = useCallback(async () => {\n await aurum.disconnect();\n }, [aurum]);\n\n return { disconnect };\n}\n","'use client';\n\nimport { useState, useCallback, useEffect } from 'react';\nimport type { Chain } from 'viem';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access chain information and switch chains.\n *\n * @example\n * ```tsx\n * import { sepolia } from 'viem/chains';\n *\n * const { chainId, switchChain, error } = useChain();\n *\n * return (\n * <div>\n * <p>Chain ID: {chainId}</p>\n * <button onClick={() => switchChain(sepolia.id, sepolia)}>\n * Switch to Sepolia\n * </button>\n * </div>\n * );\n * ```\n */\nexport function useChain() {\n const { aurum, isReady } = useAurumContext();\n const [chainId, setChainId] = useState<number | null>(null);\n const [error, setError] = useState<Error | null>(null);\n\n // Fetch initial chain ID when ready\n useEffect(() => {\n if (!isReady) return;\n\n const fetchChainId = async () => {\n try {\n const id = await aurum.getChainId();\n setChainId(id);\n } catch {\n // Not connected or error fetching chain ID\n setChainId(null);\n }\n };\n\n fetchChainId();\n\n // Listen for chain changes\n const handleChainChanged = (newChainId: number | string) => {\n setChainId(Number(newChainId));\n };\n\n aurum.rpcProvider?.on?.('chainChanged', handleChainChanged);\n\n return () => {\n aurum.rpcProvider?.removeListener?.('chainChanged', handleChainChanged);\n };\n }, [aurum, isReady]);\n\n const switchChain = useCallback(\n async (targetChainId: number | string | `0x${string}`, chain?: Chain) => {\n if (!targetChainId) {\n throw new Error('chainId is required');\n }\n setError(null);\n\n try {\n await aurum.switchChain(targetChainId as `0x${string}` | string | number, chain);\n // Chain ID will be updated via the chainChanged event listener\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to switch chain');\n setError(error);\n throw error;\n }\n },\n [aurum],\n );\n\n return { chainId, switchChain, error };\n}\n"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,244 @@
1
+ import { createContext, useState, useRef, useCallback, useEffect, useMemo, useSyncExternalStore, useContext } from 'react';
2
+ import { jsx } from 'react/jsx-runtime';
3
+
4
+ // src/AurumProvider.tsx
5
+ var initialAccountState = {
6
+ publicAddress: void 0,
7
+ walletName: void 0,
8
+ walletId: void 0,
9
+ email: void 0,
10
+ isConnected: false,
11
+ isInitializing: true
12
+ };
13
+ var AurumContext = createContext(null);
14
+ function useAurumContext() {
15
+ const context = useContext(AurumContext);
16
+ if (!context) {
17
+ throw new Error("useAurumContext must be used within a AurumProvider");
18
+ }
19
+ return context;
20
+ }
21
+ function AurumProvider({ aurum, children }) {
22
+ const [isReady, setIsReady] = useState(false);
23
+ const accountStateRef = useRef(initialAccountState);
24
+ const listenersRef = useRef(/* @__PURE__ */ new Set());
25
+ const notifyListeners = useCallback(() => {
26
+ listenersRef.current.forEach((listener) => listener());
27
+ }, []);
28
+ const syncState = useCallback(async () => {
29
+ try {
30
+ const userInfo = await aurum.getUserInfo();
31
+ const isConnected = await aurum.isConnected();
32
+ const newState = {
33
+ publicAddress: userInfo?.publicAddress,
34
+ walletName: userInfo?.walletName,
35
+ walletId: userInfo?.walletId,
36
+ email: userInfo?.email,
37
+ isConnected,
38
+ isInitializing: false
39
+ };
40
+ accountStateRef.current = newState;
41
+ notifyListeners();
42
+ } catch {
43
+ accountStateRef.current = {
44
+ ...initialAccountState,
45
+ isInitializing: false
46
+ };
47
+ notifyListeners();
48
+ }
49
+ }, [aurum, notifyListeners]);
50
+ useEffect(() => {
51
+ let mounted = true;
52
+ const initialize = async () => {
53
+ await aurum.whenReady();
54
+ if (!mounted) return;
55
+ setIsReady(true);
56
+ await syncState();
57
+ };
58
+ initialize();
59
+ const handleAccountsChanged = () => {
60
+ if (mounted) syncState();
61
+ };
62
+ const handleChainChanged = () => {
63
+ if (mounted) syncState();
64
+ };
65
+ aurum.rpcProvider?.on?.("accountsChanged", handleAccountsChanged);
66
+ aurum.rpcProvider?.on?.("chainChanged", handleChainChanged);
67
+ return () => {
68
+ mounted = false;
69
+ aurum.rpcProvider?.removeListener?.("accountsChanged", handleAccountsChanged);
70
+ aurum.rpcProvider?.removeListener?.("chainChanged", handleChainChanged);
71
+ };
72
+ }, [aurum, syncState]);
73
+ const subscribe = useCallback((callback) => {
74
+ listenersRef.current.add(callback);
75
+ return () => {
76
+ listenersRef.current.delete(callback);
77
+ };
78
+ }, []);
79
+ const getSnapshot = useCallback(() => {
80
+ return accountStateRef.current;
81
+ }, []);
82
+ const getServerSnapshot = useCallback(() => {
83
+ return initialAccountState;
84
+ }, []);
85
+ const contextValue = useMemo(
86
+ () => ({
87
+ aurum,
88
+ isReady,
89
+ subscribe,
90
+ getSnapshot,
91
+ getServerSnapshot
92
+ }),
93
+ [aurum, isReady, subscribe, getSnapshot, getServerSnapshot]
94
+ );
95
+ return /* @__PURE__ */ jsx(AurumContext.Provider, { value: contextValue, children });
96
+ }
97
+
98
+ // src/hooks/useAurum.ts
99
+ function useAurum() {
100
+ const { aurum, isReady } = useAurumContext();
101
+ return { aurum, isReady };
102
+ }
103
+ function useAccount() {
104
+ const { subscribe, getSnapshot, getServerSnapshot } = useAurumContext();
105
+ const account = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
106
+ return {
107
+ publicAddress: account.publicAddress,
108
+ walletName: account.walletName,
109
+ walletId: account.walletId,
110
+ email: account.email,
111
+ isConnected: account.isConnected,
112
+ isInitializing: account.isInitializing
113
+ };
114
+ }
115
+ function useConnect() {
116
+ const { aurum } = useAurumContext();
117
+ const [isPending, setIsPending] = useState(false);
118
+ const [error, setError] = useState(null);
119
+ const connect = useCallback(
120
+ async (walletId) => {
121
+ setIsPending(true);
122
+ setError(null);
123
+ try {
124
+ const address = await aurum.connect(walletId);
125
+ return address;
126
+ } catch (err) {
127
+ const error2 = err instanceof Error ? err : new Error("User rejected connection");
128
+ setError(error2);
129
+ throw error2;
130
+ } finally {
131
+ setIsPending(false);
132
+ }
133
+ },
134
+ [aurum]
135
+ );
136
+ const emailAuthStart = useCallback(
137
+ async (email) => {
138
+ setIsPending(true);
139
+ setError(null);
140
+ try {
141
+ const result = await aurum.emailAuthStart(email);
142
+ return result;
143
+ } catch (err) {
144
+ const error2 = err instanceof Error ? err : new Error("Failed to start email auth");
145
+ setError(error2);
146
+ throw error2;
147
+ } finally {
148
+ setIsPending(false);
149
+ }
150
+ },
151
+ [aurum]
152
+ );
153
+ const emailAuthVerify = useCallback(
154
+ async (flowId, otp) => {
155
+ setIsPending(true);
156
+ setError(null);
157
+ try {
158
+ const result = await aurum.emailAuthVerify(flowId, otp);
159
+ return result;
160
+ } catch (err) {
161
+ const error2 = err instanceof Error ? err : new Error("Failed to verify email");
162
+ setError(error2);
163
+ throw error2;
164
+ } finally {
165
+ setIsPending(false);
166
+ }
167
+ },
168
+ [aurum]
169
+ );
170
+ const getWalletConnectSession = useCallback(async () => {
171
+ setIsPending(true);
172
+ setError(null);
173
+ try {
174
+ const result = await aurum.getWalletConnectSession();
175
+ return result;
176
+ } catch (err) {
177
+ const error2 = err instanceof Error ? err : new Error("Failed to get WalletConnect session");
178
+ setError(error2);
179
+ throw error2;
180
+ } finally {
181
+ setIsPending(false);
182
+ }
183
+ }, [aurum]);
184
+ return {
185
+ connect,
186
+ emailAuthStart,
187
+ emailAuthVerify,
188
+ getWalletConnectSession,
189
+ isPending,
190
+ error
191
+ };
192
+ }
193
+ function useDisconnect() {
194
+ const { aurum } = useAurumContext();
195
+ const disconnect = useCallback(async () => {
196
+ await aurum.disconnect();
197
+ }, [aurum]);
198
+ return { disconnect };
199
+ }
200
+ function useChain() {
201
+ const { aurum, isReady } = useAurumContext();
202
+ const [chainId, setChainId] = useState(null);
203
+ const [error, setError] = useState(null);
204
+ useEffect(() => {
205
+ if (!isReady) return;
206
+ const fetchChainId = async () => {
207
+ try {
208
+ const id = await aurum.getChainId();
209
+ setChainId(id);
210
+ } catch {
211
+ setChainId(null);
212
+ }
213
+ };
214
+ fetchChainId();
215
+ const handleChainChanged = (newChainId) => {
216
+ setChainId(Number(newChainId));
217
+ };
218
+ aurum.rpcProvider?.on?.("chainChanged", handleChainChanged);
219
+ return () => {
220
+ aurum.rpcProvider?.removeListener?.("chainChanged", handleChainChanged);
221
+ };
222
+ }, [aurum, isReady]);
223
+ const switchChain = useCallback(
224
+ async (targetChainId, chain) => {
225
+ if (!targetChainId) {
226
+ throw new Error("chainId is required");
227
+ }
228
+ setError(null);
229
+ try {
230
+ await aurum.switchChain(targetChainId, chain);
231
+ } catch (err) {
232
+ const error2 = err instanceof Error ? err : new Error("Failed to switch chain");
233
+ setError(error2);
234
+ throw error2;
235
+ }
236
+ },
237
+ [aurum]
238
+ );
239
+ return { chainId, switchChain, error };
240
+ }
241
+
242
+ export { AurumProvider, useAccount, useAurum, useChain, useConnect, useDisconnect };
243
+ //# sourceMappingURL=index.mjs.map
244
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AurumContext.ts","../src/AurumProvider.tsx","../src/hooks/useAurum.ts","../src/hooks/useAccount.ts","../src/hooks/useConnect.ts","../src/hooks/useDisconnect.ts","../src/hooks/useChain.ts"],"names":["useState","useCallback","error","useEffect"],"mappings":";;;;AAuBO,IAAM,mBAAA,GAAoC;AAAA,EAC/C,aAAA,EAAe,MAAA;AAAA,EACf,UAAA,EAAY,MAAA;AAAA,EACZ,QAAA,EAAU,MAAA;AAAA,EACV,KAAA,EAAO,MAAA;AAAA,EACP,WAAA,EAAa,KAAA;AAAA,EACb,cAAA,EAAgB;AAClB,CAAA;AAEO,IAAM,YAAA,GAAe,cAAwC,IAAI,CAAA;AAEjE,SAAS,eAAA,GAAqC;AACnD,EAAA,MAAM,OAAA,GAAU,WAAW,YAAY,CAAA;AACvC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,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QAAA,CAAS,IAAI,CAAA;AAEb,MAAA,IAAI;AACF,QAAA,MAAM,KAAA,CAAM,WAAA,CAAY,aAAA,EAAkD,KAAK,CAAA;AAAA,MAEjF,SAAS,GAAA,EAAK;AACZ,QAAA,MAAMC,SAAQ,GAAA,YAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,MAAM,wBAAwB,CAAA;AAC7E,QAAA,QAAA,CAASA,MAAK,CAAA;AACd,QAAA,MAAMA,MAAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,IACA,CAAC,KAAK;AAAA,GACR;AAEA,EAAA,OAAO,EAAE,OAAA,EAAS,WAAA,EAAa,KAAA,EAAM;AACvC","file":"index.mjs","sourcesContent":["'use client';\n\nimport { createContext, useContext } from 'react';\nimport type { Aurum } from '@aurum-sdk/core';\nimport type { WalletId, WalletName } from '@aurum-sdk/types';\n\nexport interface AccountState {\n publicAddress: string | undefined;\n walletName: WalletName | undefined;\n walletId: WalletId | undefined;\n email: string | undefined;\n isConnected: boolean;\n isInitializing: boolean;\n}\n\ninterface AurumContextValue {\n aurum: Aurum;\n isReady: boolean;\n subscribe: (callback: () => void) => () => void;\n getSnapshot: () => AccountState;\n getServerSnapshot: () => AccountState;\n}\n\nexport const initialAccountState: AccountState = {\n publicAddress: undefined,\n walletName: undefined,\n walletId: undefined,\n email: undefined,\n isConnected: false,\n isInitializing: true,\n};\n\nexport const AurumContext = createContext<AurumContextValue | null>(null);\n\nexport function useAurumContext(): AurumContextValue {\n const context = useContext(AurumContext);\n if (!context) {\n throw new Error('useAurumContext must be used within a AurumProvider');\n }\n return context;\n}\n","'use client';\n\nimport { useRef, useEffect, useCallback, useState, useMemo, type ReactNode } from 'react';\nimport { AurumContext, initialAccountState, type AccountState } from '@src/AurumContext';\nimport type { Aurum } from '@aurum-sdk/core';\n\ninterface AurumProviderProps {\n aurum: Aurum;\n children: ReactNode;\n}\n\nexport function AurumProvider({ aurum, children }: AurumProviderProps) {\n const [isReady, setIsReady] = useState(false);\n const accountStateRef = useRef<AccountState>(initialAccountState);\n const listenersRef = useRef<Set<() => void>>(new Set());\n\n // Notify all subscribers of state changes\n const notifyListeners = useCallback(() => {\n listenersRef.current.forEach((listener) => listener());\n }, []);\n\n // Sync state from SDK\n const syncState = useCallback(async () => {\n try {\n const userInfo = await aurum.getUserInfo();\n const isConnected = await aurum.isConnected();\n\n const newState: AccountState = {\n publicAddress: userInfo?.publicAddress,\n walletName: userInfo?.walletName,\n walletId: userInfo?.walletId,\n email: userInfo?.email,\n isConnected,\n isInitializing: false,\n };\n\n accountStateRef.current = newState;\n notifyListeners();\n } catch {\n accountStateRef.current = {\n ...initialAccountState,\n isInitializing: false,\n };\n notifyListeners();\n }\n }, [aurum, notifyListeners]);\n\n // Initialize SDK and set up event listeners\n useEffect(() => {\n let mounted = true;\n\n const initialize = async () => {\n await aurum.whenReady();\n if (!mounted) return;\n\n setIsReady(true);\n await syncState();\n };\n\n initialize();\n\n // Subscribe to provider events for account changes\n const handleAccountsChanged = () => {\n if (mounted) syncState();\n };\n\n const handleChainChanged = () => {\n if (mounted) syncState();\n };\n\n aurum.rpcProvider?.on?.('accountsChanged', handleAccountsChanged);\n aurum.rpcProvider?.on?.('chainChanged', handleChainChanged);\n\n return () => {\n mounted = false;\n aurum.rpcProvider?.removeListener?.('accountsChanged', handleAccountsChanged);\n aurum.rpcProvider?.removeListener?.('chainChanged', handleChainChanged);\n };\n }, [aurum, syncState]);\n\n // Subscribe function for useSyncExternalStore\n const subscribe = useCallback((callback: () => void) => {\n listenersRef.current.add(callback);\n return () => {\n listenersRef.current.delete(callback);\n };\n }, []);\n\n // Get current snapshot for useSyncExternalStore\n const getSnapshot = useCallback(() => {\n return accountStateRef.current;\n }, []);\n\n // Server snapshot - always returns initial state\n const getServerSnapshot = useCallback(() => {\n return initialAccountState;\n }, []);\n\n const contextValue = useMemo(\n () => ({\n aurum,\n isReady,\n subscribe,\n getSnapshot,\n getServerSnapshot,\n }),\n [aurum, isReady, subscribe, getSnapshot, getServerSnapshot],\n );\n\n return <AurumContext.Provider value={contextValue}>{children}</AurumContext.Provider>;\n}\n","'use client';\n\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access the raw Aurum SDK instance.\n *\n * @example\n * ```tsx\n * const { aurum, isReady } = useAurum();\n *\n * if (isReady) {\n * const chainId = await aurum.getChainId();\n * }\n * ```\n */\nexport function useAurum() {\n const { aurum, isReady } = useAurumContext();\n return { aurum, isReady };\n}\n","'use client';\n\nimport { useSyncExternalStore } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access connected user information.\n *\n * @example\n * ```tsx\n * const { publicAddress, walletName, isConnected, isInitializing } = useAccount();\n *\n * if (isInitializing) return <div>Loading...</div>;\n *\n * if (isConnected) {\n * return <div>Connected: {publicAddress}</div>;\n * }\n * ```\n */\nexport function useAccount() {\n const { subscribe, getSnapshot, getServerSnapshot } = useAurumContext();\n\n const account = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n\n return {\n publicAddress: account.publicAddress,\n walletName: account.walletName,\n walletId: account.walletId,\n email: account.email,\n isConnected: account.isConnected,\n isInitializing: account.isInitializing,\n };\n}\n","'use client';\n\nimport { useState, useCallback } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\nimport type { WalletId, WalletConnectSessionResult } from '@aurum-sdk/types';\n\n/**\n * Connect to a wallet.\n *\n * @example\n * ```tsx\n * const { connect, emailAuthStart, emailAuthVerify, getWalletConnectSession, isPending, error } = useConnect();\n *\n * // Open wallet selection modal\n * await connect();\n *\n * // Or connect directly to a specific wallet\n * await connect(WalletId.MetaMask);\n *\n * // Or use headless email auth\n * const { flowId } = await emailAuthStart('user@example.com');\n * const { address, email } = await emailAuthVerify(flowId, '123456');\n *\n * // Or use headless WalletConnect\n * const { uri, waitForConnection } = await getWalletConnectSession();\n * // Display your own QR code with `uri`\n * const address = await waitForConnection();\n * ```\n */\nexport function useConnect() {\n const { aurum } = useAurumContext();\n\n const [isPending, setIsPending] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const connect = useCallback(\n async (walletId?: WalletId) => {\n setIsPending(true);\n setError(null);\n\n try {\n const address = await aurum.connect(walletId);\n return address;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('User rejected connection');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const emailAuthStart = useCallback(\n async (email: string) => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.emailAuthStart(email);\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to start email auth');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const emailAuthVerify = useCallback(\n async (flowId: string, otp: string) => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.emailAuthVerify(flowId, otp);\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to verify email');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n },\n [aurum],\n );\n\n const getWalletConnectSession = useCallback(async (): Promise<WalletConnectSessionResult> => {\n setIsPending(true);\n setError(null);\n\n try {\n const result = await aurum.getWalletConnectSession();\n return result;\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to get WalletConnect session');\n setError(error);\n throw error;\n } finally {\n setIsPending(false);\n }\n }, [aurum]);\n\n return {\n connect,\n emailAuthStart,\n emailAuthVerify,\n getWalletConnectSession,\n isPending,\n error,\n };\n}\n","'use client';\n\nimport { useCallback } from 'react';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Disconnect the current wallet.\n *\n * @example\n * ```tsx\n * const { disconnect } = useDisconnect();\n *\n * return <button onClick={disconnect}>Disconnect</button>;\n * ```\n */\nexport function useDisconnect() {\n const { aurum } = useAurumContext();\n\n const disconnect = useCallback(async () => {\n await aurum.disconnect();\n }, [aurum]);\n\n return { disconnect };\n}\n","'use client';\n\nimport { useState, useCallback, useEffect } from 'react';\nimport type { Chain } from 'viem';\nimport { useAurumContext } from '@src/AurumContext';\n\n/**\n * Access chain information and switch chains.\n *\n * @example\n * ```tsx\n * import { sepolia } from 'viem/chains';\n *\n * const { chainId, switchChain, error } = useChain();\n *\n * return (\n * <div>\n * <p>Chain ID: {chainId}</p>\n * <button onClick={() => switchChain(sepolia.id, sepolia)}>\n * Switch to Sepolia\n * </button>\n * </div>\n * );\n * ```\n */\nexport function useChain() {\n const { aurum, isReady } = useAurumContext();\n const [chainId, setChainId] = useState<number | null>(null);\n const [error, setError] = useState<Error | null>(null);\n\n // Fetch initial chain ID when ready\n useEffect(() => {\n if (!isReady) return;\n\n const fetchChainId = async () => {\n try {\n const id = await aurum.getChainId();\n setChainId(id);\n } catch {\n // Not connected or error fetching chain ID\n setChainId(null);\n }\n };\n\n fetchChainId();\n\n // Listen for chain changes\n const handleChainChanged = (newChainId: number | string) => {\n setChainId(Number(newChainId));\n };\n\n aurum.rpcProvider?.on?.('chainChanged', handleChainChanged);\n\n return () => {\n aurum.rpcProvider?.removeListener?.('chainChanged', handleChainChanged);\n };\n }, [aurum, isReady]);\n\n const switchChain = useCallback(\n async (targetChainId: number | string | `0x${string}`, chain?: Chain) => {\n if (!targetChainId) {\n throw new Error('chainId is required');\n }\n setError(null);\n\n try {\n await aurum.switchChain(targetChainId as `0x${string}` | string | number, chain);\n // Chain ID will be updated via the chainChanged event listener\n } catch (err) {\n const error = err instanceof Error ? err : new Error('Failed to switch chain');\n setError(error);\n throw error;\n }\n },\n [aurum],\n );\n\n return { chainId, switchChain, error };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@aurum-sdk/hooks",
3
+ "version": "0.1.0",
4
+ "description": "React hooks for Aurum SDK",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "engines": {
9
+ "node": ">=18.0.0",
10
+ "pnpm": ">=8.0.0"
11
+ },
12
+ "packageManager": "pnpm@8.15.0",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "clean": "rm -rf dist",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest",
29
+ "test:coverage": "vitest run --coverage",
30
+ "prepublishOnly": "pnpm build"
31
+ },
32
+ "peerDependencies": {
33
+ "react": ">=18.0.0",
34
+ "viem": ">=2.0.0"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "viem": {
38
+ "optional": true
39
+ }
40
+ },
41
+ "dependencies": {
42
+ "@aurum-sdk/core": "workspace:^",
43
+ "@aurum-sdk/types": "workspace:^"
44
+ },
45
+ "devDependencies": {
46
+ "@testing-library/react": "^16.0.0",
47
+ "@types/react": "^18.0.0",
48
+ "@vitest/coverage-v8": "^2.1.0",
49
+ "jsdom": "^25.0.0",
50
+ "react": "^19.2.2",
51
+ "tsup": "^8.0.0",
52
+ "typescript": "^5.0.0",
53
+ "viem": "^2.42.1",
54
+ "vitest": "^2.1.0"
55
+ },
56
+ "keywords": [
57
+ "react",
58
+ "hooks",
59
+ "wallet",
60
+ "ethereum",
61
+ "web3",
62
+ "aurum"
63
+ ],
64
+ "author": "Hunter Cote",
65
+ "license": "MIT",
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "https://github.com/aurum-sdk/aurum.git",
69
+ "directory": "packages/hooks"
70
+ },
71
+ "homepage": "https://github.com/aurum-sdk/aurum#readme"
72
+ }