@gelatonetwork/smartwallet-react-privy 0.0.2-alpha.1 → 0.0.2-alpha.3
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/CHANGELOG.md +23 -0
- package/README.md +66 -2
- package/_dist/provider.d.ts.map +1 -1
- package/_dist/provider.js +17 -13
- package/_dist/provider.js.map +1 -1
- package/_dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -3
- package/src/provider.tsx +30 -17
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@gelatonetwork/smartwallet-react-privy",
|
3
|
-
"version": "0.0.2-alpha.
|
3
|
+
"version": "0.0.2-alpha.3",
|
4
4
|
"author": "Gelato",
|
5
5
|
"type": "module",
|
6
6
|
"description": "Context provider supporting Privy WaaS",
|
@@ -10,7 +10,9 @@
|
|
10
10
|
"peerDependencies": {
|
11
11
|
"typescript": "^5.3",
|
12
12
|
"viem": "^2.28",
|
13
|
-
"wagmi": "^2.15"
|
13
|
+
"wagmi": "^2.15",
|
14
|
+
"@gelatonetwork/smartwallet-react-types": "0.0.2-alpha.3",
|
15
|
+
"@gelatonetwork/smartwallet": "0.0.2-alpha.3"
|
14
16
|
},
|
15
17
|
"peerDependenciesMeta": {
|
16
18
|
"typescript": {
|
@@ -30,7 +32,8 @@
|
|
30
32
|
"viem": "^2.28",
|
31
33
|
"wagmi": "^2.15",
|
32
34
|
"zod": "^3.24.3",
|
33
|
-
"@gelatonetwork/smartwallet-react-types": "0.0.2-alpha.
|
35
|
+
"@gelatonetwork/smartwallet-react-types": "0.0.2-alpha.3",
|
36
|
+
"@gelatonetwork/smartwallet": "0.0.2-alpha.3"
|
34
37
|
},
|
35
38
|
"devDependencies": {
|
36
39
|
"@types/react": "^18",
|
package/src/provider.tsx
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
+
import {
|
2
|
+
type GelatoSmartWalletClient,
|
3
|
+
createGelatoSmartWalletClient
|
4
|
+
} from "@gelatonetwork/smartwallet";
|
5
|
+
import type { wallet } from "@gelatonetwork/smartwallet-react-types";
|
1
6
|
import { PrivyProvider, usePrivy, useSignAuthorization, useWallets } from "@privy-io/react-auth";
|
2
7
|
import { WagmiProvider, createConfig } from "@privy-io/wagmi";
|
3
8
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
4
9
|
import { ChainId } from "caip";
|
5
10
|
import { createContext, useContext, useEffect, useState } from "react";
|
11
|
+
import type { FC, ReactNode } from "react";
|
6
12
|
import {
|
7
13
|
type Account,
|
8
14
|
type Chain,
|
9
15
|
type Hex,
|
10
16
|
type Transport,
|
11
|
-
type WalletClient,
|
12
17
|
createWalletClient,
|
13
18
|
custom
|
14
19
|
} from "viem";
|
15
20
|
import * as chains from "viem/chains";
|
16
21
|
import { extractChain } from "viem/utils";
|
17
|
-
|
18
|
-
import type { wallet } from "@gelatonetwork/smartwallet-react-types";
|
19
|
-
import type { FC, ReactNode } from "react";
|
20
22
|
import type { Config as WagmiConfig } from "wagmi";
|
21
23
|
|
22
24
|
type GelatoSmartWalletPrivyContextType = wallet.ProviderContext;
|
@@ -40,31 +42,36 @@ type GelatoSmartWalletPrivyContextProps = wallet.ProviderProps;
|
|
40
42
|
const GelatoSmartWalletPrivyInternal: FC<{
|
41
43
|
children: ReactNode;
|
42
44
|
wagmi: { config: WagmiConfig | undefined };
|
43
|
-
|
45
|
+
apiKey?: string | undefined;
|
46
|
+
}> = ({ children, wagmi, apiKey }) => {
|
44
47
|
const { ready, authenticated, logout } = usePrivy();
|
45
48
|
const { wallets, ready: walletsReady } = useWallets();
|
46
49
|
const { signAuthorization } = useSignAuthorization();
|
47
50
|
|
48
|
-
const [
|
51
|
+
const [smartWalletClient, setSmartWalletClient] = useState<GelatoSmartWalletClient<
|
52
|
+
Transport,
|
53
|
+
Chain,
|
54
|
+
Account
|
55
|
+
> | null>(null);
|
49
56
|
|
50
57
|
const logoutWrapper = async () => {
|
51
|
-
if (!
|
58
|
+
if (!smartWalletClient) {
|
52
59
|
return;
|
53
60
|
}
|
54
61
|
|
55
|
-
|
62
|
+
setSmartWalletClient(null);
|
56
63
|
await logout();
|
57
64
|
};
|
58
65
|
|
59
66
|
const switchNetwork = async (chain: Chain) => {
|
60
|
-
if (!
|
67
|
+
if (!smartWalletClient) {
|
61
68
|
return;
|
62
69
|
}
|
63
70
|
|
64
71
|
const primaryWallet = wallets[0];
|
65
72
|
|
66
73
|
await primaryWallet.switchChain(chain.id);
|
67
|
-
|
74
|
+
smartWalletClient.switchChain({ id: chain.id });
|
68
75
|
};
|
69
76
|
|
70
77
|
useEffect(() => {
|
@@ -73,7 +80,7 @@ const GelatoSmartWalletPrivyInternal: FC<{
|
|
73
80
|
}
|
74
81
|
|
75
82
|
if (!authenticated || !wallets || wallets.length === 0) {
|
76
|
-
|
83
|
+
setSmartWalletClient(null);
|
77
84
|
return;
|
78
85
|
}
|
79
86
|
|
@@ -91,13 +98,13 @@ const GelatoSmartWalletPrivyInternal: FC<{
|
|
91
98
|
}
|
92
99
|
|
93
100
|
const provider = await primaryWallet.getEthereumProvider();
|
94
|
-
const
|
101
|
+
const client = createWalletClient({
|
95
102
|
account: primaryWallet.address as Hex,
|
96
103
|
chain,
|
97
104
|
transport: custom(provider)
|
98
105
|
});
|
99
106
|
|
100
|
-
|
107
|
+
client.signAuthorization = async (parameters) => {
|
101
108
|
const { chainId, nonce } = parameters;
|
102
109
|
const contractAddress = parameters.contractAddress ?? parameters.address;
|
103
110
|
|
@@ -114,21 +121,27 @@ const GelatoSmartWalletPrivyInternal: FC<{
|
|
114
121
|
return signedAuthorization;
|
115
122
|
};
|
116
123
|
|
117
|
-
|
124
|
+
const walletClientGelato = createGelatoSmartWalletClient<Transport, Chain, Account>(
|
125
|
+
client,
|
126
|
+
apiKey
|
127
|
+
);
|
128
|
+
setSmartWalletClient(walletClientGelato);
|
118
129
|
} catch (error) {
|
119
130
|
console.error("Failed to get wallet client:", error);
|
120
131
|
}
|
121
132
|
};
|
122
133
|
|
123
134
|
fetchWalletClient();
|
124
|
-
}, [ready, wallets, walletsReady, authenticated, signAuthorization]);
|
135
|
+
}, [ready, wallets, walletsReady, authenticated, signAuthorization, apiKey]);
|
125
136
|
|
126
137
|
return (
|
127
138
|
<GelatoSmartWalletPrivyProviderContext.Provider
|
128
139
|
value={{
|
140
|
+
gelato: {
|
141
|
+
client: smartWalletClient as GelatoSmartWalletClient<Transport, Chain, Account>
|
142
|
+
},
|
129
143
|
wagmi: {
|
130
|
-
config: wagmi.config
|
131
|
-
client: walletClient as WalletClient<Transport, Chain, Account>
|
144
|
+
config: wagmi.config
|
132
145
|
},
|
133
146
|
logout: logoutWrapper,
|
134
147
|
switchNetwork
|