@imtbl/wallet 2.12.5-alpha.2 → 2.12.5-alpha.21

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.
Files changed (35) hide show
  1. package/README.md +331 -0
  2. package/dist/browser/index.js +26 -26
  3. package/dist/node/index.cjs +62 -56
  4. package/dist/node/index.js +27 -27
  5. package/dist/types/confirmation/confirmation.d.ts +1 -1
  6. package/dist/types/connectWallet.d.ts +15 -22
  7. package/dist/types/constants.d.ts +7 -0
  8. package/dist/types/guardian/index.d.ts +10 -5
  9. package/dist/types/index.d.ts +4 -2
  10. package/dist/types/magic/magicTEESigner.d.ts +3 -4
  11. package/dist/types/network/chainRegistry.d.ts +13 -0
  12. package/dist/types/{presets.d.ts → network/presets.d.ts} +37 -1
  13. package/dist/types/sequence/sequenceProvider.d.ts +14 -0
  14. package/dist/types/types.d.ts +57 -10
  15. package/dist/types/zkEvm/relayerClient.d.ts +8 -4
  16. package/dist/types/zkEvm/types.d.ts +3 -10
  17. package/dist/types/zkEvm/user/registerZkEvmUser.d.ts +7 -4
  18. package/dist/types/zkEvm/zkEvmProvider.d.ts +13 -5
  19. package/package.json +7 -7
  20. package/src/confirmation/confirmation.ts +14 -4
  21. package/src/connectWallet.test.ts +54 -3
  22. package/src/connectWallet.ts +141 -92
  23. package/src/constants.ts +10 -0
  24. package/src/guardian/index.ts +42 -25
  25. package/src/index.ts +14 -2
  26. package/src/magic/magicTEESigner.ts +8 -6
  27. package/src/network/chainRegistry.test.ts +64 -0
  28. package/src/network/chainRegistry.ts +74 -0
  29. package/src/{presets.ts → network/presets.ts} +62 -2
  30. package/src/sequence/sequenceProvider.ts +165 -0
  31. package/src/types.ts +65 -14
  32. package/src/zkEvm/relayerClient.ts +22 -6
  33. package/src/zkEvm/types.ts +4 -10
  34. package/src/zkEvm/user/registerZkEvmUser.ts +16 -5
  35. package/src/zkEvm/zkEvmProvider.ts +56 -29
package/README.md ADDED
@@ -0,0 +1,331 @@
1
+ # @imtbl/wallet
2
+
3
+ The Immutable Wallet SDK provides an EIP-1193 compliant Ethereum provider for interacting with Immutable's zkEVM blockchain. It handles authentication, transaction signing, and wallet management.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @imtbl/wallet
9
+ # or
10
+ yarn add @imtbl/wallet
11
+ # or
12
+ pnpm add @imtbl/wallet
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ The simplest way to get started is with the default authentication:
18
+
19
+ ```typescript
20
+ import { connectWallet } from '@imtbl/wallet';
21
+
22
+ // Uses default Immutable-hosted authentication
23
+ const provider = await connectWallet();
24
+
25
+ // Request accounts (triggers login if needed)
26
+ const accounts = await provider.request({ method: 'eth_requestAccounts' });
27
+ console.log('Connected:', accounts[0]);
28
+ ```
29
+
30
+ ## Integration Options
31
+
32
+ The wallet package supports three authentication modes:
33
+
34
+ | Option | Use Case | Package |
35
+ |--------|----------|---------|
36
+ | **Option 1: Default Auth** | Simplest setup, uses built-in Auth class | `@imtbl/wallet` only |
37
+ | **Option 2: NextAuth Integration** | Server-side session management with Next.js | `@imtbl/wallet` + `@imtbl/auth-next-client` |
38
+ | **Option 3: Using @imtbl/auth Directly** | Full control over authentication | `@imtbl/wallet` + `@imtbl/auth` |
39
+
40
+ ---
41
+
42
+ ## Option 1: Default Authentication
43
+
44
+ Uses the built-in `@imtbl/auth` instance. Best for simple applications or when you don't need server-side session management.
45
+
46
+ ```typescript
47
+ import { connectWallet } from '@imtbl/wallet';
48
+
49
+ // Basic setup - uses default Immutable authentication
50
+ const provider = await connectWallet({
51
+ clientId: 'your-client-id', // Optional, uses defaults if not provided
52
+ });
53
+
54
+ // Request accounts triggers the login flow
55
+ const accounts = await provider.request({ method: 'eth_requestAccounts' });
56
+ ```
57
+
58
+ ### With Custom Chain Configuration
59
+
60
+ ```typescript
61
+ import { connectWallet, IMMUTABLE_ZKEVM_TESTNET_CHAIN } from '@imtbl/wallet';
62
+
63
+ const provider = await connectWallet({
64
+ chains: [IMMUTABLE_ZKEVM_TESTNET_CHAIN],
65
+ initialChainId: 13473, // Testnet chain ID
66
+ });
67
+ ```
68
+
69
+ ---
70
+
71
+ ## Option 2: NextAuth Integration (Recommended for Next.js)
72
+
73
+ For Next.js applications using NextAuth for server-side session management, use `@imtbl/auth-next-client` which provides a `getUser` function compatible with the wallet.
74
+
75
+ ### Setup
76
+
77
+ 1. **Install dependencies:**
78
+
79
+ ```bash
80
+ npm install @imtbl/wallet @imtbl/auth-next-client @imtbl/auth-next-server next-auth@5
81
+ ```
82
+
83
+ 2. **Configure NextAuth server-side** (see `@imtbl/auth-next-server` documentation)
84
+
85
+ 3. **Use in your React components:**
86
+
87
+ ```tsx
88
+ 'use client';
89
+
90
+ import { connectWallet } from '@imtbl/wallet';
91
+ import { useImmutableSession } from '@imtbl/auth-next-client';
92
+ import { useState } from 'react';
93
+
94
+ export function WalletConnect() {
95
+ const { getUser, isAuthenticated } = useImmutableSession();
96
+ const [provider, setProvider] = useState(null);
97
+
98
+ const handleConnect = async () => {
99
+ // Pass getUser directly from useImmutableSession
100
+ const walletProvider = await connectWallet({
101
+ getUser, // <-- This is all you need!
102
+ });
103
+
104
+ // Request accounts to trigger login/registration
105
+ const accounts = await walletProvider.request({
106
+ method: 'eth_requestAccounts'
107
+ });
108
+
109
+ console.log('Connected:', accounts[0]);
110
+ setProvider(walletProvider);
111
+ };
112
+
113
+ return (
114
+ <button onClick={handleConnect} disabled={!isAuthenticated}>
115
+ Connect Wallet
116
+ </button>
117
+ );
118
+ }
119
+ ```
120
+
121
+ ### Key Benefits of NextAuth Integration
122
+
123
+ - **Server-side session management**: Tokens are stored securely on the server
124
+ - **Automatic token refresh**: Tokens are refreshed automatically via NextAuth callbacks
125
+ - **SSR compatibility**: Session data available during server-side rendering
126
+ - **Unified auth state**: Single source of truth for authentication across your app
127
+
128
+ ### How It Works
129
+
130
+ The `useImmutableSession` hook returns a `getUser` function that:
131
+ 1. Reads the current session from NextAuth
132
+ 2. Returns user data with fresh tokens
133
+ 3. Supports `forceRefresh` to trigger server-side token refresh when needed
134
+
135
+ ```typescript
136
+ const { getUser } = useImmutableSession();
137
+
138
+ // Normal call - returns cached session data instantly
139
+ const user = await getUser();
140
+
141
+ // Force refresh - triggers server-side token refresh
142
+ const freshUser = await getUser(true);
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Option 3: Using @imtbl/auth Directly
148
+
149
+ For applications that need full control over authentication but aren't using NextAuth:
150
+
151
+ ```typescript
152
+ import { connectWallet } from '@imtbl/wallet';
153
+ import { Auth } from '@imtbl/auth';
154
+
155
+ // Create your own Auth instance
156
+ const auth = new Auth({
157
+ clientId: 'your-client-id',
158
+ redirectUri: 'https://your-app.com/callback',
159
+ // ... other options
160
+ });
161
+
162
+ // Create a getUser function
163
+ const getUser = async (forceRefresh?: boolean) => {
164
+ if (forceRefresh) {
165
+ return auth.forceUserRefresh();
166
+ }
167
+ return auth.getUser();
168
+ };
169
+
170
+ // Pass to connectWallet
171
+ const provider = await connectWallet({ getUser });
172
+ ```
173
+
174
+ ---
175
+
176
+ ## API Reference
177
+
178
+ ### `connectWallet(options?)`
179
+
180
+ Creates an EIP-1193 compliant provider for Immutable zkEVM.
181
+
182
+ #### Options
183
+
184
+ | Option | Type | Description |
185
+ |--------|------|-------------|
186
+ | `getUser` | `(forceRefresh?: boolean) => Promise<User \| null>` | Function to get current user with tokens. If not provided, uses default auth. |
187
+ | `clientId` | `string` | Immutable client ID. Required when using default auth. |
188
+ | `chains` | `ChainConfig[]` | Chain configurations. Defaults to testnet + mainnet. |
189
+ | `initialChainId` | `number` | Initial chain to connect to. Defaults to first chain. |
190
+ | `popupOverlayOptions` | `PopupOverlayOptions` | Options for login popup overlays. |
191
+ | `announceProvider` | `boolean` | Whether to announce via EIP-6963. Defaults to `true`. |
192
+
193
+ #### Returns
194
+
195
+ `Promise<ZkEvmProvider>` - An EIP-1193 compliant provider.
196
+
197
+ ### Provider Methods
198
+
199
+ The returned provider supports standard Ethereum JSON-RPC methods:
200
+
201
+ ```typescript
202
+ // Get accounts (triggers login if needed)
203
+ const accounts = await provider.request({ method: 'eth_requestAccounts' });
204
+
205
+ // Get current accounts (returns empty if not connected)
206
+ const accounts = await provider.request({ method: 'eth_accounts' });
207
+
208
+ // Get chain ID
209
+ const chainId = await provider.request({ method: 'eth_chainId' });
210
+
211
+ // Send transaction
212
+ const txHash = await provider.request({
213
+ method: 'eth_sendTransaction',
214
+ params: [{
215
+ to: '0x...',
216
+ value: '0x...',
217
+ data: '0x...',
218
+ }],
219
+ });
220
+
221
+ // Sign message
222
+ const signature = await provider.request({
223
+ method: 'personal_sign',
224
+ params: ['0x...message', '0x...address'],
225
+ });
226
+ ```
227
+
228
+ ---
229
+
230
+ ## Chain Configuration
231
+
232
+ ### Preset Chains
233
+
234
+ ```typescript
235
+ import {
236
+ IMMUTABLE_ZKEVM_MAINNET_CHAIN,
237
+ IMMUTABLE_ZKEVM_TESTNET_CHAIN,
238
+ DEFAULT_CHAINS,
239
+ } from '@imtbl/wallet';
240
+
241
+ // Use testnet only
242
+ const provider = await connectWallet({
243
+ chains: [IMMUTABLE_ZKEVM_TESTNET_CHAIN],
244
+ });
245
+
246
+ // Use both (default)
247
+ const provider = await connectWallet({
248
+ chains: DEFAULT_CHAINS, // [testnet, mainnet]
249
+ });
250
+ ```
251
+
252
+ ### Custom Chain Configuration
253
+
254
+ ```typescript
255
+ const customChain: ChainConfig = {
256
+ chainId: 13473,
257
+ chainName: 'Immutable zkEVM Testnet',
258
+ rpcUrl: 'https://rpc.testnet.immutable.com',
259
+ relayerUrl: 'https://relayer.testnet.immutable.com',
260
+ apiUrl: 'https://api.sandbox.immutable.com',
261
+ // Optional Magic TEE config for dev environments
262
+ magicPublishableApiKey: 'pk_...',
263
+ magicProviderId: '...',
264
+ };
265
+ ```
266
+
267
+ ---
268
+
269
+ ## Events
270
+
271
+ The provider emits standard EIP-1193 events:
272
+
273
+ ```typescript
274
+ provider.on('accountsChanged', (accounts: string[]) => {
275
+ console.log('Accounts changed:', accounts);
276
+ });
277
+
278
+ provider.on('chainChanged', (chainId: string) => {
279
+ console.log('Chain changed:', chainId);
280
+ });
281
+
282
+ provider.on('disconnect', (error: Error) => {
283
+ console.log('Disconnected:', error);
284
+ });
285
+ ```
286
+
287
+ ---
288
+
289
+ ## Error Handling
290
+
291
+ ```typescript
292
+ import { connectWallet } from '@imtbl/wallet';
293
+
294
+ try {
295
+ const provider = await connectWallet({ getUser });
296
+ const accounts = await provider.request({ method: 'eth_requestAccounts' });
297
+ } catch (error) {
298
+ if (error.message.includes('Popup closed')) {
299
+ console.log('User cancelled login');
300
+ } else if (error.message.includes('Unauthorized')) {
301
+ console.log('User not authenticated - call eth_requestAccounts first');
302
+ } else {
303
+ console.error('Wallet error:', error);
304
+ }
305
+ }
306
+ ```
307
+
308
+ ---
309
+
310
+ ## TypeScript
311
+
312
+ Full TypeScript support is included:
313
+
314
+ ```typescript
315
+ import type {
316
+ ConnectWalletOptions,
317
+ ChainConfig,
318
+ GetUserFunction,
319
+ ZkEvmProvider,
320
+ } from '@imtbl/wallet';
321
+ ```
322
+
323
+ ---
324
+
325
+ ## Related Packages
326
+
327
+ | Package | Description |
328
+ |---------|-------------|
329
+ | [`@imtbl/auth`](../auth/README.md) | Core authentication with client-side session management |
330
+ | [`@imtbl/auth-next-server`](../auth-next-server/README.md) | NextAuth server-side configuration |
331
+ | [`@imtbl/auth-next-client`](../auth-next-client/README.md) | NextAuth client-side hooks and components |