@account-kit/privy-integration 4.75.3 → 4.75.5-alpha.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/dist/esm/Provider.d.ts +4 -71
- package/dist/esm/Provider.js +4 -109
- package/dist/esm/Provider.js.map +1 -1
- package/dist/esm/Provider.native.d.ts +6 -0
- package/dist/esm/Provider.native.js +13 -0
- package/dist/esm/Provider.native.js.map +1 -0
- package/dist/esm/adapters/react-native.d.ts +6 -0
- package/dist/esm/adapters/react-native.js +132 -0
- package/dist/esm/adapters/react-native.js.map +1 -0
- package/dist/esm/adapters/types.d.ts +56 -0
- package/dist/esm/adapters/types.js +2 -0
- package/dist/esm/adapters/types.js.map +1 -0
- package/dist/esm/adapters/web.d.ts +6 -0
- package/dist/esm/adapters/web.js +70 -0
- package/dist/esm/adapters/web.js.map +1 -0
- package/dist/esm/adapters/web.native.d.ts +5 -0
- package/dist/esm/adapters/web.native.js +6 -0
- package/dist/esm/adapters/web.native.js.map +1 -0
- package/dist/esm/context/AlchemyContext.d.ts +71 -0
- package/dist/esm/context/AlchemyContext.js +115 -0
- package/dist/esm/context/AlchemyContext.js.map +1 -0
- package/dist/esm/hooks/internal/useEmbeddedWallet.d.ts +3 -4
- package/dist/esm/hooks/internal/useEmbeddedWallet.js +6 -13
- package/dist/esm/hooks/internal/useEmbeddedWallet.js.map +1 -1
- package/dist/esm/hooks/useAlchemyClient.js +17 -24
- package/dist/esm/hooks/useAlchemyClient.js.map +1 -1
- package/dist/esm/hooks/useAlchemySendTransaction.js +1 -1
- package/dist/esm/hooks/useAlchemySendTransaction.js.map +1 -1
- package/dist/esm/hooks/useAlchemySolanaTransaction.js +1 -1
- package/dist/esm/hooks/useAlchemySolanaTransaction.js.map +1 -1
- package/dist/esm/providers/ReactNativeProvider.d.ts +37 -0
- package/dist/esm/providers/ReactNativeProvider.js +41 -0
- package/dist/esm/providers/ReactNativeProvider.js.map +1 -0
- package/dist/esm/providers/WebProvider.d.ts +37 -0
- package/dist/esm/providers/WebProvider.js +41 -0
- package/dist/esm/providers/WebProvider.js.map +1 -0
- package/dist/esm/providers/WebProvider.native.d.ts +5 -0
- package/dist/esm/providers/WebProvider.native.js +9 -0
- package/dist/esm/providers/WebProvider.native.js.map +1 -0
- package/dist/esm/react-native.d.ts +11 -0
- package/dist/esm/react-native.js +13 -0
- package/dist/esm/react-native.js.map +1 -0
- package/dist/esm/types.d.ts +6 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/types/Provider.d.ts +4 -71
- package/dist/types/Provider.d.ts.map +1 -1
- package/dist/types/Provider.native.d.ts +7 -0
- package/dist/types/Provider.native.d.ts.map +1 -0
- package/dist/types/adapters/react-native.d.ts +7 -0
- package/dist/types/adapters/react-native.d.ts.map +1 -0
- package/dist/types/adapters/types.d.ts +57 -0
- package/dist/types/adapters/types.d.ts.map +1 -0
- package/dist/types/adapters/web.d.ts +7 -0
- package/dist/types/adapters/web.d.ts.map +1 -0
- package/dist/types/adapters/web.native.d.ts +6 -0
- package/dist/types/adapters/web.native.d.ts.map +1 -0
- package/dist/types/context/AlchemyContext.d.ts +72 -0
- package/dist/types/context/AlchemyContext.d.ts.map +1 -0
- package/dist/types/hooks/internal/useEmbeddedWallet.d.ts +3 -4
- package/dist/types/hooks/internal/useEmbeddedWallet.d.ts.map +1 -1
- package/dist/types/hooks/useAlchemyClient.d.ts.map +1 -1
- package/dist/types/providers/ReactNativeProvider.d.ts +38 -0
- package/dist/types/providers/ReactNativeProvider.d.ts.map +1 -0
- package/dist/types/providers/WebProvider.d.ts +38 -0
- package/dist/types/providers/WebProvider.d.ts.map +1 -0
- package/dist/types/providers/WebProvider.native.d.ts +6 -0
- package/dist/types/providers/WebProvider.native.d.ts.map +1 -0
- package/dist/types/react-native.d.ts +12 -0
- package/dist/types/react-native.d.ts.map +1 -0
- package/dist/types/types.d.ts +6 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/version.d.ts.map +1 -1
- package/package.json +27 -4
- package/src/Provider.native.tsx +18 -0
- package/src/Provider.tsx +6 -0
- package/src/adapters/react-native.ts +202 -0
- package/src/adapters/types.ts +71 -0
- package/src/adapters/web.native.ts +6 -0
- package/src/adapters/web.ts +107 -0
- package/src/context/AlchemyContext.tsx +185 -0
- package/src/hooks/internal/useEmbeddedWallet.ts +6 -20
- package/src/hooks/useAlchemyClient.ts +24 -38
- package/src/hooks/useAlchemySendTransaction.ts +1 -1
- package/src/hooks/useAlchemySolanaTransaction.ts +1 -1
- package/src/providers/ReactNativeProvider.tsx +49 -0
- package/src/providers/WebProvider.native.tsx +11 -0
- package/src/providers/WebProvider.tsx +49 -0
- package/src/react-native.ts +29 -0
- package/src/types.ts +7 -0
- package/src/version.ts +1 -1
package/dist/esm/Provider.d.ts
CHANGED
|
@@ -1,73 +1,6 @@
|
|
|
1
|
-
import { type PropsWithChildren } from "react";
|
|
2
|
-
import type { SmartWalletClient } from "@account-kit/wallet-client";
|
|
3
|
-
import type { SmartContractAccount } from "@aa-sdk/core";
|
|
4
|
-
import type { AlchemyProviderConfig } from "./types.js";
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @internal
|
|
2
|
+
* Web-specific exports
|
|
3
|
+
* Re-exports the web provider and hooks for React applications
|
|
9
4
|
*/
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
* Client cache stored in React tree (similar to QueryClient in React Query)
|
|
13
|
-
*
|
|
14
|
-
* @internal
|
|
15
|
-
*/
|
|
16
|
-
interface ClientCache {
|
|
17
|
-
client: SmartWalletClient | null;
|
|
18
|
-
account: SmartContractAccount | null;
|
|
19
|
-
cacheKey: string | null;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Provider component that configures Alchemy infrastructure for transaction handling
|
|
23
|
-
* Must be nested INSIDE PrivyProvider to access authentication state
|
|
24
|
-
* Automatically manages client cache lifecycle and resets on logout
|
|
25
|
-
*
|
|
26
|
-
* @param {PropsWithChildren<AlchemyProviderConfig>} props - Component props
|
|
27
|
-
* @param {React.ReactNode} props.children - React children to wrap with Alchemy configuration
|
|
28
|
-
* @param {string} [props.apiKey] - Your Alchemy API key
|
|
29
|
-
* @param {string} [props.jwt] - JWT token for authentication
|
|
30
|
-
* @param {string} [props.rpcUrl] - Custom RPC URL for EVM chains
|
|
31
|
-
* @param {string} [props.solanaRpcUrl] - Custom RPC URL for Solana
|
|
32
|
-
* @param {string | string[]} [props.policyId] - Gas Manager policy ID(s) for EVM chains
|
|
33
|
-
* @param {string | string[]} [props.solanaPolicyId] - Gas Manager policy ID(s) for Solana
|
|
34
|
-
* @param {boolean} [props.disableSponsorship] - Set to true to disable sponsorship by default (default: false)
|
|
35
|
-
* @param {'eip7702' | 'owner'} [props.accountAuthMode] - Authorization mode for EVM smart accounts (default: 'eip7702')
|
|
36
|
-
* @returns {JSX.Element} Provider component
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```tsx
|
|
40
|
-
* <PrivyProvider appId="...">
|
|
41
|
-
* <AlchemyProvider
|
|
42
|
-
* apiKey="your-alchemy-api-key"
|
|
43
|
-
* policyId="your-gas-policy-id"
|
|
44
|
-
* >
|
|
45
|
-
* <YourApp />
|
|
46
|
-
* </AlchemyProvider>
|
|
47
|
-
* </PrivyProvider>
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function AlchemyProvider({ children, accountAuthMode, ...config }: PropsWithChildren<AlchemyProviderConfig>): import("react/jsx-runtime").JSX.Element;
|
|
51
|
-
/**
|
|
52
|
-
* Hook to access Alchemy provider configuration
|
|
53
|
-
* Must be used within an <AlchemyProvider> component
|
|
54
|
-
*
|
|
55
|
-
* @returns {NormalizedAlchemyConfig} The current Alchemy configuration with defaults applied
|
|
56
|
-
* @throws {Error} If used outside of AlchemyProvider
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```tsx
|
|
60
|
-
* const config = useAlchemyConfig();
|
|
61
|
-
* console.log('Policy ID:', config.policyId);
|
|
62
|
-
* console.log('Auth Mode:', config.accountAuthMode); // Always defined
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export declare function useAlchemyConfig(): NormalizedAlchemyConfig;
|
|
66
|
-
/**
|
|
67
|
-
* Hook to access the client cache (internal use only)
|
|
68
|
-
*
|
|
69
|
-
* @internal
|
|
70
|
-
* @returns {ClientCache} The client cache object
|
|
71
|
-
*/
|
|
72
|
-
export declare function useClientCache(): ClientCache;
|
|
73
|
-
export {};
|
|
5
|
+
export { AlchemyProvider } from "./providers/WebProvider.js";
|
|
6
|
+
export { useAlchemyConfig } from "./context/AlchemyContext.js";
|
package/dist/esm/Provider.js
CHANGED
|
@@ -1,112 +1,7 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useRef, useEffect, } from "react";
|
|
3
|
-
import { usePrivy } from "@privy-io/react-auth";
|
|
4
|
-
const AlchemyContext = createContext(null);
|
|
5
|
-
const ClientCacheContext = createContext(null);
|
|
6
1
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* Automatically manages client cache lifecycle and resets on logout
|
|
10
|
-
*
|
|
11
|
-
* @param {PropsWithChildren<AlchemyProviderConfig>} props - Component props
|
|
12
|
-
* @param {React.ReactNode} props.children - React children to wrap with Alchemy configuration
|
|
13
|
-
* @param {string} [props.apiKey] - Your Alchemy API key
|
|
14
|
-
* @param {string} [props.jwt] - JWT token for authentication
|
|
15
|
-
* @param {string} [props.rpcUrl] - Custom RPC URL for EVM chains
|
|
16
|
-
* @param {string} [props.solanaRpcUrl] - Custom RPC URL for Solana
|
|
17
|
-
* @param {string | string[]} [props.policyId] - Gas Manager policy ID(s) for EVM chains
|
|
18
|
-
* @param {string | string[]} [props.solanaPolicyId] - Gas Manager policy ID(s) for Solana
|
|
19
|
-
* @param {boolean} [props.disableSponsorship] - Set to true to disable sponsorship by default (default: false)
|
|
20
|
-
* @param {'eip7702' | 'owner'} [props.accountAuthMode] - Authorization mode for EVM smart accounts (default: 'eip7702')
|
|
21
|
-
* @returns {JSX.Element} Provider component
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```tsx
|
|
25
|
-
* <PrivyProvider appId="...">
|
|
26
|
-
* <AlchemyProvider
|
|
27
|
-
* apiKey="your-alchemy-api-key"
|
|
28
|
-
* policyId="your-gas-policy-id"
|
|
29
|
-
* >
|
|
30
|
-
* <YourApp />
|
|
31
|
-
* </AlchemyProvider>
|
|
32
|
-
* </PrivyProvider>
|
|
33
|
-
* ```
|
|
2
|
+
* Web-specific exports
|
|
3
|
+
* Re-exports the web provider and hooks for React applications
|
|
34
4
|
*/
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
// Normalize config with default values
|
|
38
|
-
const normalizedConfig = {
|
|
39
|
-
...config,
|
|
40
|
-
accountAuthMode,
|
|
41
|
-
};
|
|
42
|
-
// Store cache in a ref - persists across renders but scoped to this component instance
|
|
43
|
-
// This makes it SSR-safe (each request gets its own cache) and React StrictMode-safe
|
|
44
|
-
const cache = useRef({
|
|
45
|
-
client: null,
|
|
46
|
-
account: null,
|
|
47
|
-
cacheKey: null,
|
|
48
|
-
});
|
|
49
|
-
// Track previous state to detect logout and wallet changes
|
|
50
|
-
const prevAuthenticatedRef = useRef(authenticated);
|
|
51
|
-
const prevWalletAddressRef = useRef(user?.wallet?.address);
|
|
52
|
-
// Automatically reset cache when user logs out or switches wallets
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
const wasAuthenticated = prevAuthenticatedRef.current;
|
|
55
|
-
const prevWalletAddress = prevWalletAddressRef.current;
|
|
56
|
-
const currentWalletAddress = user?.wallet?.address;
|
|
57
|
-
// Reset cache on logout
|
|
58
|
-
if (wasAuthenticated && !authenticated) {
|
|
59
|
-
cache.current.client = null;
|
|
60
|
-
cache.current.account = null;
|
|
61
|
-
cache.current.cacheKey = null;
|
|
62
|
-
}
|
|
63
|
-
// Reset cache on wallet address change (account switching)
|
|
64
|
-
if (authenticated &&
|
|
65
|
-
prevWalletAddress &&
|
|
66
|
-
currentWalletAddress &&
|
|
67
|
-
prevWalletAddress !== currentWalletAddress) {
|
|
68
|
-
cache.current.client = null;
|
|
69
|
-
cache.current.account = null;
|
|
70
|
-
cache.current.cacheKey = null;
|
|
71
|
-
}
|
|
72
|
-
// Update refs for next render
|
|
73
|
-
prevAuthenticatedRef.current = authenticated;
|
|
74
|
-
prevWalletAddressRef.current = currentWalletAddress;
|
|
75
|
-
}, [authenticated, user?.wallet?.address]);
|
|
76
|
-
return (_jsx(AlchemyContext.Provider, { value: normalizedConfig, children: _jsx(ClientCacheContext.Provider, { value: cache.current, children: children }) }));
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Hook to access Alchemy provider configuration
|
|
80
|
-
* Must be used within an <AlchemyProvider> component
|
|
81
|
-
*
|
|
82
|
-
* @returns {NormalizedAlchemyConfig} The current Alchemy configuration with defaults applied
|
|
83
|
-
* @throws {Error} If used outside of AlchemyProvider
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```tsx
|
|
87
|
-
* const config = useAlchemyConfig();
|
|
88
|
-
* console.log('Policy ID:', config.policyId);
|
|
89
|
-
* console.log('Auth Mode:', config.accountAuthMode); // Always defined
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
export function useAlchemyConfig() {
|
|
93
|
-
const context = useContext(AlchemyContext);
|
|
94
|
-
if (!context) {
|
|
95
|
-
throw new Error("useAlchemyConfig must be used within <AlchemyProvider />");
|
|
96
|
-
}
|
|
97
|
-
return context;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Hook to access the client cache (internal use only)
|
|
101
|
-
*
|
|
102
|
-
* @internal
|
|
103
|
-
* @returns {ClientCache} The client cache object
|
|
104
|
-
*/
|
|
105
|
-
export function useClientCache() {
|
|
106
|
-
const context = useContext(ClientCacheContext);
|
|
107
|
-
if (!context) {
|
|
108
|
-
throw new Error("useClientCache must be used within <AlchemyProvider />. Make sure AlchemyProvider is nested inside PrivyProvider.");
|
|
109
|
-
}
|
|
110
|
-
return context;
|
|
111
|
-
}
|
|
5
|
+
export { AlchemyProvider } from "./providers/WebProvider.js";
|
|
6
|
+
export { useAlchemyConfig } from "./context/AlchemyContext.js";
|
|
112
7
|
//# sourceMappingURL=Provider.js.map
|
package/dist/esm/Provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../src/Provider.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../src/Provider.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC","sourcesContent":["/**\n * Web-specific exports\n * Re-exports the web provider and hooks for React applications\n */\nexport { AlchemyProvider } from \"./providers/WebProvider.js\";\nexport { useAlchemyConfig } from \"./context/AlchemyContext.js\";\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Native stub for web Provider
|
|
3
|
+
* This file prevents Metro from importing web-specific code
|
|
4
|
+
*/
|
|
5
|
+
export function AlchemyProvider() {
|
|
6
|
+
throw new Error("This module requires @privy-io/react-auth which is not available in React Native. " +
|
|
7
|
+
'Import from "@account-kit/privy-integration/react-native" instead.');
|
|
8
|
+
}
|
|
9
|
+
export function useAlchemyConfig() {
|
|
10
|
+
throw new Error("This module requires @privy-io/react-auth which is not available in React Native. " +
|
|
11
|
+
'Import from "@account-kit/privy-integration/react-native" instead.');
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=Provider.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Provider.native.js","sourceRoot":"","sources":["../../src/Provider.native.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,UAAU,eAAe;IAC7B,MAAM,IAAI,KAAK,CACb,oFAAoF;QAClF,oEAAoE,CACvE,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,MAAM,IAAI,KAAK,CACb,oFAAoF;QAClF,oEAAoE,CACvE,CAAC;AACJ,CAAC","sourcesContent":["/**\n * React Native stub for web Provider\n * This file prevents Metro from importing web-specific code\n */\n\nexport function AlchemyProvider() {\n throw new Error(\n \"This module requires @privy-io/react-auth which is not available in React Native. \" +\n 'Import from \"@account-kit/privy-integration/react-native\" instead.',\n );\n}\n\nexport function useAlchemyConfig() {\n throw new Error(\n \"This module requires @privy-io/react-auth which is not available in React Native. \" +\n 'Import from \"@account-kit/privy-integration/react-native\" instead.',\n );\n}\n"]}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { usePrivy, useEmbeddedEthereumWallet, } from "@privy-io/expo";
|
|
3
|
+
import { hashAuthorization } from "viem/utils";
|
|
4
|
+
import { parseSignature } from "viem";
|
|
5
|
+
/**
|
|
6
|
+
* React Native (Expo) adapter for @privy-io/expo
|
|
7
|
+
* Implements platform-specific hooks for React Native applications
|
|
8
|
+
*/
|
|
9
|
+
export const reactNativeAdapter = {
|
|
10
|
+
useEmbeddedWallet(preferredAddress) {
|
|
11
|
+
const { wallets } = useEmbeddedEthereumWallet();
|
|
12
|
+
const getEmbeddedWallet = useCallback(() => {
|
|
13
|
+
if (!wallets || wallets.length === 0) {
|
|
14
|
+
throw new Error("Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.");
|
|
15
|
+
}
|
|
16
|
+
// If a preferred address is specified, find that wallet
|
|
17
|
+
const wallet = preferredAddress
|
|
18
|
+
? wallets.find((w) => w.address.toLowerCase() === preferredAddress.toLowerCase())
|
|
19
|
+
: wallets[0];
|
|
20
|
+
if (!wallet) {
|
|
21
|
+
throw new Error(preferredAddress
|
|
22
|
+
? `Privy embedded wallet with address ${preferredAddress} not found.`
|
|
23
|
+
: "Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.");
|
|
24
|
+
}
|
|
25
|
+
return adaptExpoWallet(wallet);
|
|
26
|
+
}, [wallets, preferredAddress]);
|
|
27
|
+
return getEmbeddedWallet;
|
|
28
|
+
},
|
|
29
|
+
usePrivyAuth() {
|
|
30
|
+
const { user } = usePrivy();
|
|
31
|
+
return { authenticated: !!user, user };
|
|
32
|
+
},
|
|
33
|
+
useWalletAddress(preferredAddress) {
|
|
34
|
+
const { wallets } = useEmbeddedEthereumWallet();
|
|
35
|
+
if (!wallets || wallets.length === 0) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
// If a preferred address is specified, find that wallet
|
|
39
|
+
if (preferredAddress) {
|
|
40
|
+
const wallet = wallets.find((w) => w.address.toLowerCase() === preferredAddress.toLowerCase());
|
|
41
|
+
return wallet?.address;
|
|
42
|
+
}
|
|
43
|
+
// Otherwise return the first wallet
|
|
44
|
+
return wallets[0]?.address;
|
|
45
|
+
},
|
|
46
|
+
useAuthorizationSigner(preferredAddress) {
|
|
47
|
+
const { wallets } = useEmbeddedEthereumWallet();
|
|
48
|
+
const signAuthorization = useCallback(async (unsignedAuth) => {
|
|
49
|
+
if (!wallets || wallets.length === 0) {
|
|
50
|
+
throw new Error("Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.");
|
|
51
|
+
}
|
|
52
|
+
// If a preferred address is specified, find that wallet
|
|
53
|
+
const wallet = preferredAddress
|
|
54
|
+
? wallets.find((w) => w.address.toLowerCase() === preferredAddress.toLowerCase())
|
|
55
|
+
: wallets[0];
|
|
56
|
+
if (!wallet) {
|
|
57
|
+
throw new Error(preferredAddress
|
|
58
|
+
? `Privy embedded wallet with address ${preferredAddress} not found.`
|
|
59
|
+
: "Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.");
|
|
60
|
+
}
|
|
61
|
+
const provider = await wallet.getProvider?.();
|
|
62
|
+
if (!provider) {
|
|
63
|
+
throw new Error("Provider not available on this wallet. Ensure you're using the embedded Ethereum wallet.");
|
|
64
|
+
}
|
|
65
|
+
// Extract the implementation address (handle both 'address' and 'contractAddress' fields)
|
|
66
|
+
const implementationAddress = unsignedAuth.address ?? unsignedAuth.contractAddress;
|
|
67
|
+
if (!implementationAddress) {
|
|
68
|
+
throw new Error("Implementation address is required for EIP-7702 authorization");
|
|
69
|
+
}
|
|
70
|
+
// Create the authorization structure (matches Privy's implementation)
|
|
71
|
+
const authorization = {
|
|
72
|
+
chainId: unsignedAuth.chainId,
|
|
73
|
+
address: implementationAddress,
|
|
74
|
+
nonce: unsignedAuth.nonce,
|
|
75
|
+
};
|
|
76
|
+
// Hash the authorization using viem (same as Privy does)
|
|
77
|
+
const authorizationHash = hashAuthorization(authorization);
|
|
78
|
+
// Sign the hash directly with secp256k1_sign (same as Privy)
|
|
79
|
+
const signature = (await provider.request({
|
|
80
|
+
method: "secp256k1_sign",
|
|
81
|
+
params: [authorizationHash],
|
|
82
|
+
}));
|
|
83
|
+
// Parse the signature using viem (same as Privy)
|
|
84
|
+
const parsedSignature = parseSignature(signature);
|
|
85
|
+
return {
|
|
86
|
+
chainId: unsignedAuth.chainId,
|
|
87
|
+
address: implementationAddress,
|
|
88
|
+
nonce: unsignedAuth.nonce,
|
|
89
|
+
...parsedSignature,
|
|
90
|
+
};
|
|
91
|
+
}, [wallets, preferredAddress]);
|
|
92
|
+
return signAuthorization;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Adapts an Expo wallet to the common EmbeddedWallet interface
|
|
97
|
+
*
|
|
98
|
+
* @param {ExpoEmbeddedWallet} wallet - The Expo embedded wallet to adapt
|
|
99
|
+
* @returns {EmbeddedWallet} The adapted wallet following the common interface
|
|
100
|
+
*/
|
|
101
|
+
function adaptExpoWallet(wallet) {
|
|
102
|
+
// Use closure to maintain up-to-date chain ID across chain switches
|
|
103
|
+
let cachedChainId = wallet.chainId || "1";
|
|
104
|
+
return {
|
|
105
|
+
address: wallet.address,
|
|
106
|
+
get chainId() {
|
|
107
|
+
return cachedChainId;
|
|
108
|
+
},
|
|
109
|
+
getEthereumProvider: async () => {
|
|
110
|
+
if (!wallet.getProvider) {
|
|
111
|
+
throw new Error("getProvider is not available on this wallet. Ensure you're using the embedded Ethereum wallet.");
|
|
112
|
+
}
|
|
113
|
+
const provider = await wallet.getProvider();
|
|
114
|
+
// Always fetch current chain ID when provider is accessed
|
|
115
|
+
// This ensures we have the latest chain after wallet_switchEthereumChain calls
|
|
116
|
+
try {
|
|
117
|
+
const currentChainId = (await provider.request({
|
|
118
|
+
method: "eth_chainId",
|
|
119
|
+
params: [],
|
|
120
|
+
}));
|
|
121
|
+
// Convert hex to decimal string format (e.g., "0x1" -> "1")
|
|
122
|
+
cachedChainId = parseInt(currentChainId, 16).toString();
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Fall back to cached value if fetch fails
|
|
126
|
+
// Chain ID fetch errors are non-critical and can happen during initialization
|
|
127
|
+
}
|
|
128
|
+
return provider;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=react-native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-native.js","sourceRoot":"","sources":["../../../src/adapters/react-native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EACL,QAAQ,EACR,yBAAyB,GAE1B,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AActC;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAiB;IAC9C,iBAAiB,CAAC,gBAAy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;YAE5C,0DAA0D;YAC1D,+EAA+E;YAC/E,IAAI,CAAC;gBACH,MAAM,cAAc,GAAG,CAAC,MAAM,QAAQ,CAAC,OAAO,CAAC;oBAC7C,MAAM,EAAE,aAAa;oBACrB,MAAM,EAAE,EAAE;iBACX,CAAC,CAAW,CAAC;gBAEd,4DAA4D;gBAC5D,aAAa,GAAG,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;YAC1D,CAAC;YAAC,MAAM,CAAC;gBACP,2CAA2C;gBAC3C,8EAA8E;YAChF,CAAC;YAED,OAAO,QAAQ,CAAC;QAClB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback } from \"react\";\nimport {\n usePrivy,\n useEmbeddedEthereumWallet,\n type PrivyEmbeddedWalletProvider,\n} from \"@privy-io/expo\";\nimport type { Authorization } from \"viem\";\nimport { hashAuthorization } from \"viem/utils\";\nimport { parseSignature } from \"viem\";\nimport type { AuthorizationRequest } from \"@aa-sdk/core\";\nimport type { PrivyAdapter, EmbeddedWallet, PrivyAuthState } from \"./types.js\";\n\n/**\n * Wallet type from @privy-io/expo\n * Based on the example app structure\n */\ninterface ExpoEmbeddedWallet {\n address: string;\n chainId?: string;\n getProvider?: () => Promise<PrivyEmbeddedWalletProvider>;\n}\n\n/**\n * React Native (Expo) adapter for @privy-io/expo\n * Implements platform-specific hooks for React Native applications\n */\nexport const reactNativeAdapter: PrivyAdapter = {\n useEmbeddedWallet(preferredAddress?: string) {\n const { wallets } = useEmbeddedEthereumWallet();\n\n const getEmbeddedWallet = useCallback((): EmbeddedWallet => {\n if (!wallets || wallets.length === 0) {\n throw new Error(\n \"Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.\",\n );\n }\n\n // If a preferred address is specified, find that wallet\n const wallet = preferredAddress\n ? wallets.find(\n (w) => w.address.toLowerCase() === preferredAddress.toLowerCase(),\n )\n : wallets[0];\n\n if (!wallet) {\n throw new Error(\n preferredAddress\n ? `Privy embedded wallet with address ${preferredAddress} not found.`\n : \"Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.\",\n );\n }\n\n return adaptExpoWallet(wallet);\n }, [wallets, preferredAddress]);\n\n return getEmbeddedWallet;\n },\n\n usePrivyAuth(): PrivyAuthState {\n const { user } = usePrivy();\n return { authenticated: !!user, user };\n },\n\n useWalletAddress(preferredAddress?: string): string | undefined {\n const { wallets } = useEmbeddedEthereumWallet();\n\n if (!wallets || wallets.length === 0) {\n return undefined;\n }\n\n // If a preferred address is specified, find that wallet\n if (preferredAddress) {\n const wallet = wallets.find(\n (w) => w.address.toLowerCase() === preferredAddress.toLowerCase(),\n );\n return wallet?.address;\n }\n\n // Otherwise return the first wallet\n return wallets[0]?.address;\n },\n\n useAuthorizationSigner(preferredAddress?: string) {\n const { wallets } = useEmbeddedEthereumWallet();\n\n const signAuthorization = useCallback(\n async (\n unsignedAuth: AuthorizationRequest<number>,\n ): Promise<Authorization<number, true>> => {\n if (!wallets || wallets.length === 0) {\n throw new Error(\n \"Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.\",\n );\n }\n\n // If a preferred address is specified, find that wallet\n const wallet = preferredAddress\n ? wallets.find(\n (w) => w.address.toLowerCase() === preferredAddress.toLowerCase(),\n )\n : wallets[0];\n\n if (!wallet) {\n throw new Error(\n preferredAddress\n ? `Privy embedded wallet with address ${preferredAddress} not found.`\n : \"Privy embedded wallet not found. Please ensure the user is authenticated and has created a wallet.\",\n );\n }\n\n const provider = await wallet.getProvider?.();\n if (!provider) {\n throw new Error(\n \"Provider not available on this wallet. Ensure you're using the embedded Ethereum wallet.\",\n );\n }\n\n // Extract the implementation address (handle both 'address' and 'contractAddress' fields)\n const implementationAddress =\n unsignedAuth.address ?? unsignedAuth.contractAddress;\n\n if (!implementationAddress) {\n throw new Error(\n \"Implementation address is required for EIP-7702 authorization\",\n );\n }\n\n // Create the authorization structure (matches Privy's implementation)\n const authorization = {\n chainId: unsignedAuth.chainId,\n address: implementationAddress,\n nonce: unsignedAuth.nonce,\n };\n\n // Hash the authorization using viem (same as Privy does)\n const authorizationHash = hashAuthorization(authorization);\n\n // Sign the hash directly with secp256k1_sign (same as Privy)\n const signature = (await provider.request({\n method: \"secp256k1_sign\",\n params: [authorizationHash],\n })) as `0x${string}`;\n\n // Parse the signature using viem (same as Privy)\n const parsedSignature = parseSignature(signature);\n\n return {\n chainId: unsignedAuth.chainId,\n address: implementationAddress,\n nonce: unsignedAuth.nonce,\n ...parsedSignature,\n };\n },\n [wallets, preferredAddress],\n );\n\n return signAuthorization;\n },\n};\n\n/**\n * Adapts an Expo wallet to the common EmbeddedWallet interface\n *\n * @param {ExpoEmbeddedWallet} wallet - The Expo embedded wallet to adapt\n * @returns {EmbeddedWallet} The adapted wallet following the common interface\n */\nfunction adaptExpoWallet(wallet: ExpoEmbeddedWallet): EmbeddedWallet {\n // Use closure to maintain up-to-date chain ID across chain switches\n let cachedChainId = wallet.chainId || \"1\";\n\n return {\n address: wallet.address as `0x${string}`,\n get chainId() {\n return cachedChainId;\n },\n getEthereumProvider: async () => {\n if (!wallet.getProvider) {\n throw new Error(\n \"getProvider is not available on this wallet. Ensure you're using the embedded Ethereum wallet.\",\n );\n }\n const provider = await wallet.getProvider();\n\n // Always fetch current chain ID when provider is accessed\n // This ensures we have the latest chain after wallet_switchEthereumChain calls\n try {\n const currentChainId = (await provider.request({\n method: \"eth_chainId\",\n params: [],\n })) as string;\n\n // Convert hex to decimal string format (e.g., \"0x1\" -> \"1\")\n cachedChainId = parseInt(currentChainId, 16).toString();\n } catch {\n // Fall back to cached value if fetch fails\n // Chain ID fetch errors are non-critical and can happen during initialization\n }\n\n return provider;\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Address, Authorization } from "viem";
|
|
2
|
+
import type { AuthorizationRequest } from "@aa-sdk/core";
|
|
3
|
+
/**
|
|
4
|
+
* Platform-agnostic embedded wallet interface
|
|
5
|
+
* Abstracts differences between @privy-io/react-auth and @privy-io/expo
|
|
6
|
+
*/
|
|
7
|
+
export interface EmbeddedWallet {
|
|
8
|
+
/** Wallet address */
|
|
9
|
+
address: Address;
|
|
10
|
+
/** Chain ID as a string (may be CAIP-2 format like "eip155:1" or numeric string like "1") */
|
|
11
|
+
chainId: string;
|
|
12
|
+
/** Get EVM provider for the wallet */
|
|
13
|
+
getEthereumProvider(): Promise<any>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Platform-agnostic Privy auth state
|
|
17
|
+
*/
|
|
18
|
+
export interface PrivyAuthState {
|
|
19
|
+
/** Whether user is authenticated */
|
|
20
|
+
authenticated: boolean;
|
|
21
|
+
/** User object (platform-specific, used for cache invalidation) */
|
|
22
|
+
user: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Adapter interface that each platform must implement
|
|
26
|
+
* Provides platform-specific Privy functionality
|
|
27
|
+
*/
|
|
28
|
+
export interface PrivyAdapter {
|
|
29
|
+
/**
|
|
30
|
+
* Hook to get embedded wallet
|
|
31
|
+
* Must be called as a React hook (follows rules of hooks)
|
|
32
|
+
*
|
|
33
|
+
* @param preferredAddress - Optional address to find a specific wallet
|
|
34
|
+
*/
|
|
35
|
+
useEmbeddedWallet(preferredAddress?: string): () => EmbeddedWallet;
|
|
36
|
+
/**
|
|
37
|
+
* Hook to get Privy authentication state
|
|
38
|
+
* Must be called as a React hook (follows rules of hooks)
|
|
39
|
+
*/
|
|
40
|
+
usePrivyAuth(): PrivyAuthState;
|
|
41
|
+
/**
|
|
42
|
+
* Hook to get current wallet address (for cache invalidation)
|
|
43
|
+
* Returns undefined if no wallet is available
|
|
44
|
+
* Must be called as a React hook (follows rules of hooks)
|
|
45
|
+
*
|
|
46
|
+
* @param preferredAddress - Optional address to find a specific wallet
|
|
47
|
+
*/
|
|
48
|
+
useWalletAddress(preferredAddress?: string): string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Hook to get EIP-7702 authorization signer (optional, web only)
|
|
51
|
+
* Must be called as a React hook (follows rules of hooks)
|
|
52
|
+
*
|
|
53
|
+
* @param preferredAddress - Optional address to find a specific wallet
|
|
54
|
+
*/
|
|
55
|
+
useAuthorizationSigner?(preferredAddress?: string): ((auth: AuthorizationRequest<number>) => Promise<Authorization<number, true>>) | null;
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/adapters/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Address, Authorization } from \"viem\";\nimport type { AuthorizationRequest } from \"@aa-sdk/core\";\n\n/**\n * Platform-agnostic embedded wallet interface\n * Abstracts differences between @privy-io/react-auth and @privy-io/expo\n */\nexport interface EmbeddedWallet {\n /** Wallet address */\n address: Address;\n\n /** Chain ID as a string (may be CAIP-2 format like \"eip155:1\" or numeric string like \"1\") */\n chainId: string;\n\n /** Get EVM provider for the wallet */\n getEthereumProvider(): Promise<any>;\n}\n\n/**\n * Platform-agnostic Privy auth state\n */\nexport interface PrivyAuthState {\n /** Whether user is authenticated */\n authenticated: boolean;\n\n /** User object (platform-specific, used for cache invalidation) */\n user: any;\n}\n\n/**\n * Adapter interface that each platform must implement\n * Provides platform-specific Privy functionality\n */\nexport interface PrivyAdapter {\n /**\n * Hook to get embedded wallet\n * Must be called as a React hook (follows rules of hooks)\n *\n * @param preferredAddress - Optional address to find a specific wallet\n */\n useEmbeddedWallet(preferredAddress?: string): () => EmbeddedWallet;\n\n /**\n * Hook to get Privy authentication state\n * Must be called as a React hook (follows rules of hooks)\n */\n usePrivyAuth(): PrivyAuthState;\n\n /**\n * Hook to get current wallet address (for cache invalidation)\n * Returns undefined if no wallet is available\n * Must be called as a React hook (follows rules of hooks)\n *\n * @param preferredAddress - Optional address to find a specific wallet\n */\n useWalletAddress(preferredAddress?: string): string | undefined;\n\n /**\n * Hook to get EIP-7702 authorization signer (optional, web only)\n * Must be called as a React hook (follows rules of hooks)\n *\n * @param preferredAddress - Optional address to find a specific wallet\n */\n useAuthorizationSigner?(\n preferredAddress?: string,\n ):\n | ((\n auth: AuthorizationRequest<number>,\n ) => Promise<Authorization<number, true>>)\n | null;\n}\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useWallets, usePrivy, useSign7702Authorization, } from "@privy-io/react-auth";
|
|
3
|
+
/**
|
|
4
|
+
* Web adapter for @privy-io/react-auth
|
|
5
|
+
* Implements platform-specific hooks for React web applications
|
|
6
|
+
*/
|
|
7
|
+
export const webAdapter = {
|
|
8
|
+
useEmbeddedWallet(preferredAddress) {
|
|
9
|
+
const { wallets } = useWallets();
|
|
10
|
+
const getEmbeddedWallet = useCallback(() => {
|
|
11
|
+
const privyWallets = wallets.filter((w) => w.walletClientType === "privy");
|
|
12
|
+
if (privyWallets.length === 0) {
|
|
13
|
+
throw new Error("Privy embedded wallet not found. Please ensure the user is authenticated.");
|
|
14
|
+
}
|
|
15
|
+
// If a preferred address is specified, find that wallet
|
|
16
|
+
const embedded = preferredAddress
|
|
17
|
+
? privyWallets.find((w) => w.address.toLowerCase() === preferredAddress.toLowerCase())
|
|
18
|
+
: privyWallets[0];
|
|
19
|
+
if (!embedded) {
|
|
20
|
+
throw new Error(preferredAddress
|
|
21
|
+
? `Privy embedded wallet with address ${preferredAddress} not found.`
|
|
22
|
+
: "Privy embedded wallet not found. Please ensure the user is authenticated.");
|
|
23
|
+
}
|
|
24
|
+
return adaptWebWallet(embedded);
|
|
25
|
+
}, [wallets, preferredAddress]);
|
|
26
|
+
return getEmbeddedWallet;
|
|
27
|
+
},
|
|
28
|
+
usePrivyAuth() {
|
|
29
|
+
const { user } = usePrivy();
|
|
30
|
+
return { authenticated: !!user, user };
|
|
31
|
+
},
|
|
32
|
+
useWalletAddress(preferredAddress) {
|
|
33
|
+
const { wallets } = useWallets();
|
|
34
|
+
const privyWallets = wallets.filter((w) => w.walletClientType === "privy");
|
|
35
|
+
// If a preferred address is specified, find that wallet
|
|
36
|
+
if (preferredAddress) {
|
|
37
|
+
const wallet = privyWallets.find((w) => w.address.toLowerCase() === preferredAddress.toLowerCase());
|
|
38
|
+
return wallet?.address;
|
|
39
|
+
}
|
|
40
|
+
// Otherwise return the first embedded wallet
|
|
41
|
+
return privyWallets[0]?.address;
|
|
42
|
+
},
|
|
43
|
+
useAuthorizationSigner(_preferredAddress) {
|
|
44
|
+
const { signAuthorization } = useSign7702Authorization();
|
|
45
|
+
return useCallback(async (unsignedAuth) => {
|
|
46
|
+
const signature = await signAuthorization({
|
|
47
|
+
...unsignedAuth,
|
|
48
|
+
contractAddress: unsignedAuth.address ?? unsignedAuth.contractAddress,
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
...unsignedAuth,
|
|
52
|
+
...signature,
|
|
53
|
+
};
|
|
54
|
+
}, [signAuthorization]);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Adapts a Privy web wallet to the common EmbeddedWallet interface
|
|
59
|
+
*
|
|
60
|
+
* @param {PrivyWallet} wallet - The Privy web wallet to adapt
|
|
61
|
+
* @returns {EmbeddedWallet} The adapted wallet following the common interface
|
|
62
|
+
*/
|
|
63
|
+
function adaptWebWallet(wallet) {
|
|
64
|
+
return {
|
|
65
|
+
address: wallet.address,
|
|
66
|
+
chainId: wallet.chainId || "1",
|
|
67
|
+
getEthereumProvider: () => wallet.getEthereumProvider(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../../src/adapters/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EACL,UAAU,EACV,QAAQ,EACR,wBAAwB,GAEzB,MAAM,sBAAsB,CAAC;AAK9B;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAiB;IACtC,iBAAiB,C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AC,CAAC,gBAAgB,KAAK,OAAO,CAAC,CAAC;QAE3E,wDAAwD;QACxD,IAAI,gBAAgB,EAAE,CAAC;YACrB,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAC9B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,gBAAgB,CAAC,WAAW,EAAE,CAClE,CAAC;YACF,OAAO,MAAM,EAAE,OAAO,CAAC;QACzB,CAAC;QAED,6CAA6C;QAC7C,OAAO,YAAY,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;IAClC,CAAC;IAED,sBAAsB,CAAC,iBAA0B;QAC/C,MAAM,EAAE,iBAAiB,EAAE,GAAG,wBAAwB,EAAE,CAAC;QAEzD,OAAO,WAAW,CAChB,KAAK,EACH,YAA0C,EACJ,EAAE;YACxC,MAAM,SAAS,GAAG,MAAM,iBAAiB,CAAC;gBACxC,GAAG,YAAY;gBACf,eAAe,EAAE,YAAY,CAAC,OAAO,IAAI,YAAY,CAAC,eAAe;aACtE,CAAC,CAAC;YAEH,OAAO;gBACL,GAAG,YAAY;gBACf,GAAG,SAAS;aACb,CAAC;QACJ,CAAC,EACD,CAAC,iBAAiB,CAAC,CACpB,CAAC;IACJ,CAAC;CACF,CAAC;AAEF;;;;;GAKG;AACH,SAAS,cAAc,CAAC,MAAmB;IACzC,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,OAAwB;QACxC,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,GAAG;QAC9B,mBAAmB,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,mBAAmB,EAAE;KACxD,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback } from \"react\";\nimport {\n useWallets,\n usePrivy,\n useSign7702Authorization,\n type ConnectedWallet as PrivyWallet,\n} from \"@privy-io/react-auth\";\nimport type { Authorization } from \"viem\";\nimport type { AuthorizationRequest } from \"@aa-sdk/core\";\nimport type { PrivyAdapter, EmbeddedWallet, PrivyAuthState } from \"./types.js\";\n\n/**\n * Web adapter for @privy-io/react-auth\n * Implements platform-specific hooks for React web applications\n */\nexport const webAdapter: PrivyAdapter = {\n useEmbeddedWallet(preferredAddress?: string) {\n const { wallets } = useWallets();\n\n const getEmbeddedWallet = useCallback((): EmbeddedWallet => {\n const privyWallets = wallets.filter(\n (w) => w.walletClientType === \"privy\",\n );\n\n if (privyWallets.length === 0) {\n throw new Error(\n \"Privy embedded wallet not found. Please ensure the user is authenticated.\",\n );\n }\n\n // If a preferred address is specified, find that wallet\n const embedded = preferredAddress\n ? privyWallets.find(\n (w) => w.address.toLowerCase() === preferredAddress.toLowerCase(),\n )\n : privyWallets[0];\n\n if (!embedded) {\n throw new Error(\n preferredAddress\n ? `Privy embedded wallet with address ${preferredAddress} not found.`\n : \"Privy embedded wallet not found. Please ensure the user is authenticated.\",\n );\n }\n\n return adaptWebWallet(embedded);\n }, [wallets, preferredAddress]);\n\n return getEmbeddedWallet;\n },\n\n usePrivyAuth(): PrivyAuthState {\n const { user } = usePrivy();\n return { authenticated: !!user, user };\n },\n\n useWalletAddress(preferredAddress?: string): string | undefined {\n const { wallets } = useWallets();\n const privyWallets = wallets.filter((w) => w.walletClientType === \"privy\");\n\n // If a preferred address is specified, find that wallet\n if (preferredAddress) {\n const wallet = privyWallets.find(\n (w) => w.address.toLowerCase() === preferredAddress.toLowerCase(),\n );\n return wallet?.address;\n }\n\n // Otherwise return the first embedded wallet\n return privyWallets[0]?.address;\n },\n\n useAuthorizationSigner(_preferredAddress?: string) {\n const { signAuthorization } = useSign7702Authorization();\n\n return useCallback(\n async (\n unsignedAuth: AuthorizationRequest<number>,\n ): Promise<Authorization<number, true>> => {\n const signature = await signAuthorization({\n ...unsignedAuth,\n contractAddress: unsignedAuth.address ?? unsignedAuth.contractAddress,\n });\n\n return {\n ...unsignedAuth,\n ...signature,\n };\n },\n [signAuthorization],\n );\n },\n};\n\n/**\n * Adapts a Privy web wallet to the common EmbeddedWallet interface\n *\n * @param {PrivyWallet} wallet - The Privy web wallet to adapt\n * @returns {EmbeddedWallet} The adapted wallet following the common interface\n */\nfunction adaptWebWallet(wallet: PrivyWallet): EmbeddedWallet {\n return {\n address: wallet.address as `0x${string}`,\n chainId: wallet.chainId || \"1\",\n getEthereumProvider: () => wallet.getEthereumProvider(),\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.native.js","sourceRoot":"","sources":["../../../src/adapters/web.native.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC","sourcesContent":["/**\n * React Native stub for web adapter\n * This file prevents Metro from importing @privy-io/react-auth\n */\n\nexport const webAdapter = null;\n"]}
|