@tomo-inc/wallet-connect-protocol 0.0.4

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,489 @@
1
+ # @tomo-inc/wallet-connect-protocol
2
+
3
+ > WalletConnect Protocol SDK for Tomo Wallet - Simple and Easy-to-use WalletConnect Integration
4
+
5
+ ## 📖 Introduction
6
+
7
+ `@tomo-inc/wallet-connect-protocol` is a lightweight WalletConnect SDK designed specifically for the Tomo Wallet ecosystem. It provides a clean API and out-of-the-box React components for quickly integrating WalletConnect protocol into your applications.
8
+
9
+ ### ✨ Features
10
+
11
+ - 🚀 **Simple & Easy** - Provider component and Hooks, ready to use
12
+ - 🌐 **Multi-Chain Support** - Supports Ethereum, Solana, Aptos, Cosmos, and more
13
+ - 📱 **QR Code Generation** - Built-in QR code generation with multiple format support
14
+ - 🔗 **URI Management** - Automatic generation and management of WalletConnect URIs
15
+ - 📦 **TypeScript Support** - Full type definitions
16
+ - ⚡ **Lightweight** - Core logic only, no UI dependencies
17
+ - 🎯 **React Friendly** - Native support for React Context and Hooks
18
+
19
+ ## 📦 Installation
20
+
21
+ ```bash
22
+ # Using pnpm
23
+ pnpm add @tomo-inc/wallet-connect-protocol
24
+
25
+ # Using npm
26
+ npm install @tomo-inc/wallet-connect-protocol
27
+
28
+ # Using yarn
29
+ yarn add @tomo-inc/wallet-connect-protocol
30
+ ```
31
+
32
+ ## 🎯 Framework Support
33
+
34
+ This SDK supports both **React** and **non-React** frameworks:
35
+
36
+ ### React Usage (Default)
37
+
38
+ ```typescript
39
+ import {
40
+ WalletConnectProvider,
41
+ useWalletConnect,
42
+ } from "@tomo-inc/wallet-connect-protocol";
43
+ ```
44
+
45
+ ### Pure JavaScript / Non-React Usage
46
+
47
+ For vanilla JavaScript, Vue, Angular, Svelte, or any other framework:
48
+
49
+ ```typescript
50
+ import {
51
+ WalletConnectClient,
52
+ getAllWallets,
53
+ } from "@tomo-inc/wallet-connect-protocol/vanilla";
54
+ ```
55
+
56
+ 📖 **See [VANILLA_JS.md](./VANILLA_JS.md) for complete pure JavaScript usage guide with examples for Vue, Angular, Svelte, etc.**
57
+
58
+ ## 🚀 Quick Start
59
+
60
+ ### 1. Get Project ID
61
+
62
+ First, obtain a free Project ID from [WalletConnect Cloud](https://cloud.walletconnect.com).
63
+
64
+ ### 2. Setup Provider
65
+
66
+ Wrap your root component with `WalletConnectProvider`:
67
+
68
+ ```tsx
69
+ import { WalletConnectProvider } from "@tomo-inc/wallet-connect-protocol";
70
+
71
+ function App() {
72
+ return (
73
+ <WalletConnectProvider
74
+ config={{
75
+ projectId: "YOUR_PROJECT_ID",
76
+ metadata: {
77
+ name: "My DApp",
78
+ description: "My awesome decentralized application",
79
+ url: "https://myapp.com",
80
+ icons: ["https://myapp.com/icon.png"],
81
+ },
82
+ }}
83
+ autoInit // Auto-initialize
84
+ >
85
+ <YourApp />
86
+ </WalletConnectProvider>
87
+ );
88
+ }
89
+ ```
90
+
91
+ ### 3. Use Hook
92
+
93
+ Use the `useWalletConnect` Hook in your components:
94
+
95
+ ```tsx
96
+ import { useWalletConnect } from "@tomo-inc/wallet-connect-protocol";
97
+ import { useState } from "react";
98
+
99
+ function ConnectButton() {
100
+ const { connect, uri, generateQRCode, sessions } = useWalletConnect();
101
+ const [qrCode, setQrCode] = useState<string | null>(null);
102
+
103
+ const handleConnect = async () => {
104
+ try {
105
+ // Create connection and get URI
106
+ const connectionUri = await connect();
107
+
108
+ // Generate QR code
109
+ const qr = await generateQRCode(connectionUri, {
110
+ width: 300,
111
+ margin: 4,
112
+ });
113
+
114
+ setQrCode(qr);
115
+ } catch (error) {
116
+ console.error("Connection failed:", error);
117
+ }
118
+ };
119
+
120
+ return (
121
+ <div>
122
+ <button onClick={handleConnect}>Connect Wallet</button>
123
+
124
+ {qrCode && (
125
+ <div>
126
+ <h3>Scan QR Code to Connect</h3>
127
+ <img src={qrCode} alt="WalletConnect QR Code" />
128
+ </div>
129
+ )}
130
+
131
+ {sessions.length > 0 && (
132
+ <div>
133
+ <h3>Active Sessions</h3>
134
+ <ul>
135
+ {sessions.map((session) => (
136
+ <li key={session.topic}>{session.peer.metadata.name}</li>
137
+ ))}
138
+ </ul>
139
+ </div>
140
+ )}
141
+ </div>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ## 📚 API Documentation
147
+
148
+ ### WalletConnectProvider
149
+
150
+ Provider component to wrap your application and provide WalletConnect context.
151
+
152
+ #### Props
153
+
154
+ | Property | Type | Required | Default | Description |
155
+ | ---------- | --------------------- | -------- | ------- | --------------------------- |
156
+ | `config` | `WalletConnectConfig` | ✅ | - | WalletConnect configuration |
157
+ | `autoInit` | `boolean` | ❌ | `true` | Auto-initialize on mount |
158
+ | `children` | `ReactNode` | ✅ | - | Child components |
159
+
160
+ #### WalletConnectConfig
161
+
162
+ ```typescript
163
+ interface WalletConnectConfig {
164
+ projectId: string; // WalletConnect Project ID
165
+ metadata: {
166
+ name: string; // App name
167
+ description: string; // App description
168
+ url: string; // App URL
169
+ icons: string[]; // App icons
170
+ };
171
+ relayUrl?: string; // Optional relay server URL
172
+ }
173
+ ```
174
+
175
+ ### useWalletConnect
176
+
177
+ Hook to access WalletConnect functionality.
178
+
179
+ #### Return Values
180
+
181
+ | Property | Type | Description |
182
+ | ----------------- | ----------------------------------------------------------- | ---------------------- |
183
+ | `client` | `WalletConnectClient \| null` | Client instance |
184
+ | `initialized` | `boolean` | Whether initialized |
185
+ | `connecting` | `boolean` | Whether connecting |
186
+ | `uri` | `string \| null` | Current connection URI |
187
+ | `sessions` | `SessionInfo[]` | Active sessions |
188
+ | `initialize` | `() => Promise<void>` | Initialize client |
189
+ | `connect` | `() => Promise<string>` | Create connection |
190
+ | `generateQRCode` | `(uri: string, options?: QRCodeOptions) => Promise<string>` | Generate QR code |
191
+ | `disconnect` | `(topic: string) => Promise<void>` | Disconnect session |
192
+ | `refreshSessions` | `() => void` | Refresh session list |
193
+
194
+ ### useConnect
195
+
196
+ Simplified connection Hook with automatic QR code generation.
197
+
198
+ ```typescript
199
+ const { connect, uri, qrCode, connecting, error, reset } = useConnect({
200
+ autoGenerateQRCode: true,
201
+ qrCodeOptions: { width: 300 },
202
+ onConnect: (uri) => console.log("Connected:", uri),
203
+ onError: (error) => console.error("Error:", error),
204
+ });
205
+ ```
206
+
207
+ ### useQRCode
208
+
209
+ QR code generation Hook.
210
+
211
+ ```typescript
212
+ const { qrCode, loading, error, generate } = useQRCode(uri, {
213
+ width: 300,
214
+ margin: 4,
215
+ });
216
+ ```
217
+
218
+ ### useSession
219
+
220
+ Session management Hook.
221
+
222
+ ```typescript
223
+ const {
224
+ session, // Current session
225
+ allSessions, // All sessions
226
+ disconnect, // Disconnect session
227
+ refresh, // Refresh sessions
228
+ isExpired, // Whether expired
229
+ getAccounts, // Get accounts
230
+ getChains, // Get chains
231
+ hasSession, // Has session
232
+ } = useSession(topic);
233
+ ```
234
+
235
+ ### WalletConnectClient
236
+
237
+ Low-level client class providing advanced features.
238
+
239
+ #### Methods
240
+
241
+ ```typescript
242
+ // Initialize
243
+ await client.initialize();
244
+
245
+ // Create connection
246
+ const uri = await client.connect();
247
+
248
+ // Generate QR code (Base64)
249
+ const qrCode = await client.generateQRCode(uri, {
250
+ width: 300,
251
+ height: 300,
252
+ margin: 4,
253
+ errorCorrectionLevel: "M",
254
+ color: {
255
+ dark: "#000000",
256
+ light: "#ffffff",
257
+ },
258
+ });
259
+
260
+ // Generate QR code to Canvas
261
+ await client.generateQRCodeToCanvas(canvasElement, uri);
262
+
263
+ // Get active sessions
264
+ const sessions = client.getActiveSessions();
265
+
266
+ // Disconnect session
267
+ await client.disconnectSession(topic);
268
+
269
+ // Listen to events
270
+ client.on("session_proposal", (proposal) => {
271
+ console.log("Session proposal received:", proposal);
272
+ });
273
+
274
+ client.on("display_uri", ({ uri }) => {
275
+ console.log("WalletConnect URI:", uri);
276
+ });
277
+
278
+ // Remove event listener
279
+ client.off("session_proposal", handler);
280
+
281
+ // Destroy client
282
+ await client.destroy();
283
+ ```
284
+
285
+ ## 🎯 Use Cases
286
+
287
+ ### Case 1: Basic Connection
288
+
289
+ ```tsx
290
+ function BasicConnect() {
291
+ const { connect, uri } = useWalletConnect();
292
+
293
+ return (
294
+ <div>
295
+ <button onClick={connect}>Connect</button>
296
+ {uri && <p>URI: {uri}</p>}
297
+ </div>
298
+ );
299
+ }
300
+ ```
301
+
302
+ ### Case 2: QR Code Display
303
+
304
+ ```tsx
305
+ function QRCodeConnect() {
306
+ const { connect, generateQRCode } = useWalletConnect();
307
+ const [qr, setQr] = useState<string | null>(null);
308
+
309
+ const handleConnect = async () => {
310
+ const uri = await connect();
311
+ const qrCode = await generateQRCode(uri);
312
+ setQr(qrCode);
313
+ };
314
+
315
+ return (
316
+ <div>
317
+ <button onClick={handleConnect}>Show QR Code</button>
318
+ {qr && <img src={qr} alt="QR Code" />}
319
+ </div>
320
+ );
321
+ }
322
+ ```
323
+
324
+ ### Case 3: Session Management
325
+
326
+ ```tsx
327
+ function SessionManager() {
328
+ const { sessions, disconnect } = useWalletConnect();
329
+
330
+ return (
331
+ <div>
332
+ <h2>Active Sessions ({sessions.length})</h2>
333
+ {sessions.map((session) => (
334
+ <div key={session.topic}>
335
+ <h3>{session.peer.metadata.name}</h3>
336
+ <p>{session.peer.metadata.description}</p>
337
+ <button onClick={() => disconnect(session.topic)}>Disconnect</button>
338
+ </div>
339
+ ))}
340
+ </div>
341
+ );
342
+ }
343
+ ```
344
+
345
+ ### Case 4: Using Native Client
346
+
347
+ For more control, use `WalletConnectClient` directly:
348
+
349
+ ```tsx
350
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol";
351
+
352
+ const client = new WalletConnectClient({
353
+ projectId: "YOUR_PROJECT_ID",
354
+ metadata: {
355
+ name: "My App",
356
+ description: "My awesome app",
357
+ url: "https://myapp.com",
358
+ icons: ["https://myapp.com/icon.png"],
359
+ },
360
+ });
361
+
362
+ // Initialize
363
+ await client.initialize();
364
+
365
+ // Listen to events
366
+ client.on("session_proposal", (proposal) => {
367
+ console.log("Session proposal:", proposal);
368
+ });
369
+
370
+ // Create connection
371
+ const uri = await client.connect();
372
+
373
+ // Generate QR code
374
+ const qrCode = await client.generateQRCode(uri);
375
+ ```
376
+
377
+ ## 🔧 Advanced Configuration
378
+
379
+ ### Custom QR Code Style
380
+
381
+ ```tsx
382
+ const qrCode = await generateQRCode(uri, {
383
+ width: 400,
384
+ height: 400,
385
+ margin: 8,
386
+ errorCorrectionLevel: "H", // 'L' | 'M' | 'Q' | 'H'
387
+ color: {
388
+ dark: "#1a1a1a",
389
+ light: "#ffffff",
390
+ },
391
+ });
392
+ ```
393
+
394
+ ### Manual Initialization
395
+
396
+ ```tsx
397
+ <WalletConnectProvider
398
+ config={config}
399
+ autoInit={false} // Disable auto-init
400
+ >
401
+ <App />
402
+ </WalletConnectProvider>;
403
+
404
+ // Manually initialize when needed
405
+ function SomeComponent() {
406
+ const { initialize, initialized } = useWalletConnect();
407
+
408
+ useEffect(() => {
409
+ if (!initialized) {
410
+ initialize();
411
+ }
412
+ }, []);
413
+ }
414
+ ```
415
+
416
+ ### Event Listening
417
+
418
+ ```tsx
419
+ function EventListener() {
420
+ const { client } = useWalletConnect();
421
+
422
+ useEffect(() => {
423
+ if (!client) return;
424
+
425
+ const handleProposal = (proposal: any) => {
426
+ console.log("Proposal received:", proposal);
427
+ };
428
+
429
+ const handleRequest = (request: any) => {
430
+ console.log("Request received:", request);
431
+ };
432
+
433
+ client.on("session_proposal", handleProposal);
434
+ client.on("session_request", handleRequest);
435
+
436
+ return () => {
437
+ client.off("session_proposal", handleProposal);
438
+ client.off("session_request", handleRequest);
439
+ };
440
+ }, [client]);
441
+
442
+ return <div>Listening...</div>;
443
+ }
444
+ ```
445
+
446
+ ## 🤝 Contributing
447
+
448
+ Contributions are welcome! Please check our [Contributing Guide](../../CONTRIBUTING.md) for more information.
449
+
450
+ ## 📄 License
451
+
452
+ MIT © [Tomo Inc.](https://tomo.inc)
453
+
454
+ ## 🔗 Related Links
455
+
456
+ - [WalletConnect Official Documentation](https://docs.walletconnect.com/)
457
+ - [WalletConnect Cloud](https://cloud.walletconnect.com)
458
+ - [Tomo Wallet](https://tomo.inc)
459
+
460
+ ## ❓ FAQ
461
+
462
+ ### How to get Project ID?
463
+
464
+ Visit [WalletConnect Cloud](https://cloud.walletconnect.com), sign up and create a new project to get a free Project ID.
465
+
466
+ ### QR Code not showing?
467
+
468
+ Ensure dependencies are correctly installed and the URI is a valid WalletConnect URI (starts with `wc:`).
469
+
470
+ ### How to handle connection errors?
471
+
472
+ Use try-catch to handle errors:
473
+
474
+ ```tsx
475
+ try {
476
+ await connect();
477
+ } catch (error) {
478
+ console.error("Connection failed:", error);
479
+ // Handle error, e.g., show error message
480
+ }
481
+ ```
482
+
483
+ ### Which browsers are supported?
484
+
485
+ All modern browsers (Chrome, Firefox, Safari, Edge) are supported. ES2022+ support required.
486
+
487
+ ## 📞 Support
488
+
489
+ For questions or suggestions, please [submit an issue](https://github.com/tomo-inc/tomo-wallet/issues).
package/VANILLA_JS.md ADDED
@@ -0,0 +1,491 @@
1
+ # Pure JavaScript / Non-React Usage Guide
2
+
3
+ This guide shows how to use `@tomo-inc/wallet-connect-protocol` in vanilla JavaScript, Vue, Angular, Svelte, or any other non-React framework.
4
+
5
+ ## 📦 Installation
6
+
7
+ ```bash
8
+ npm install @tomo-inc/wallet-connect-protocol
9
+ # or
10
+ pnpm add @tomo-inc/wallet-connect-protocol
11
+ # or
12
+ yarn add @tomo-inc/wallet-connect-protocol
13
+ ```
14
+
15
+ ## 🚀 Import Methods
16
+
17
+ ### Method 1: Using `/vanilla` Export (Recommended)
18
+
19
+ ```javascript
20
+ // Import from the vanilla export (excludes React dependencies)
21
+ import {
22
+ WalletConnectClient,
23
+ getAllWallets,
24
+ } from "@tomo-inc/wallet-connect-protocol/vanilla";
25
+ ```
26
+
27
+ ### Method 2: Direct Import
28
+
29
+ ```javascript
30
+ // Import specific modules directly
31
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol";
32
+ ```
33
+
34
+ ## 💡 Basic Usage
35
+
36
+ ### 1. Initialize WalletConnect Client
37
+
38
+ ```javascript
39
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol/vanilla";
40
+
41
+ // Create client instance
42
+ const client = new WalletConnectClient({
43
+ projectId: "YOUR_PROJECT_ID", // Get from https://cloud.walletconnect.com
44
+ metadata: {
45
+ name: "My App",
46
+ description: "My awesome application",
47
+ url: "https://myapp.com",
48
+ icons: ["https://myapp.com/icon.png"],
49
+ },
50
+ });
51
+
52
+ // Initialize the client
53
+ await client.initialize();
54
+ ```
55
+
56
+ ### 2. Connect Wallet
57
+
58
+ ```javascript
59
+ // Create connection and get URI
60
+ const uri = await client.connect({
61
+ requiredNamespaces: {
62
+ eip155: {
63
+ methods: ["eth_sendTransaction", "personal_sign"],
64
+ chains: ["eip155:1"], // Ethereum mainnet
65
+ events: ["chainChanged", "accountsChanged"],
66
+ },
67
+ },
68
+ });
69
+
70
+ console.log("WalletConnect URI:", uri);
71
+
72
+ // Generate QR code
73
+ const qrCode = await client.generateQRCode(uri, {
74
+ width: 400,
75
+ margin: 2,
76
+ });
77
+
78
+ // Display QR code
79
+ document.getElementById("qr-code").src = qrCode;
80
+ ```
81
+
82
+ ### 3. Listen to Events
83
+
84
+ ```javascript
85
+ // Listen for session events
86
+ client.on("session_proposal", (proposal) => {
87
+ console.log("New session proposal:", proposal);
88
+ });
89
+
90
+ client.on("session_delete", () => {
91
+ console.log("Session disconnected");
92
+ });
93
+
94
+ client.on("session_update", (update) => {
95
+ console.log("Session updated:", update);
96
+ });
97
+ ```
98
+
99
+ ### 4. Get Active Sessions
100
+
101
+ ```javascript
102
+ const sessions = client.getActiveSessions();
103
+ console.log("Active sessions:", sessions);
104
+ ```
105
+
106
+ ### 5. Send Requests
107
+
108
+ ```javascript
109
+ if (sessions.length > 0) {
110
+ const session = sessions[0];
111
+
112
+ // Send transaction
113
+ const result = await client.request({
114
+ topic: session.topic,
115
+ chainId: "eip155:1",
116
+ request: {
117
+ method: "eth_sendTransaction",
118
+ params: [
119
+ {
120
+ from: "0x...",
121
+ to: "0x...",
122
+ value: "0x0",
123
+ data: "0x",
124
+ },
125
+ ],
126
+ },
127
+ });
128
+
129
+ console.log("Transaction result:", result);
130
+ }
131
+ ```
132
+
133
+ ### 6. Disconnect
134
+
135
+ ```javascript
136
+ // Disconnect specific session
137
+ await client.disconnect(session.topic);
138
+
139
+ // Or disconnect all sessions
140
+ const sessions = client.getActiveSessions();
141
+ for (const session of sessions) {
142
+ await client.disconnect(session.topic);
143
+ }
144
+ ```
145
+
146
+ ## 🔧 Complete Example
147
+
148
+ ### Vanilla JavaScript
149
+
150
+ ```html
151
+ <!DOCTYPE html>
152
+ <html>
153
+ <head>
154
+ <title>WalletConnect Vanilla JS Demo</title>
155
+ </head>
156
+ <body>
157
+ <div id="app">
158
+ <button id="connect-btn">Connect Wallet</button>
159
+ <div id="qr-container" style="display: none;">
160
+ <img id="qr-code" />
161
+ </div>
162
+ <div id="status"></div>
163
+ </div>
164
+
165
+ <script type="module">
166
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol/vanilla";
167
+
168
+ const client = new WalletConnectClient({
169
+ projectId: "YOUR_PROJECT_ID",
170
+ metadata: {
171
+ name: "Vanilla JS Demo",
172
+ description: "WalletConnect Vanilla JS Demo",
173
+ url: "https://example.com",
174
+ icons: ["https://example.com/icon.png"],
175
+ },
176
+ });
177
+
178
+ // Initialize
179
+ await client.initialize();
180
+
181
+ // Event handlers
182
+ const connectBtn = document.getElementById("connect-btn");
183
+ const qrContainer = document.getElementById("qr-container");
184
+ const qrCode = document.getElementById("qr-code");
185
+ const status = document.getElementById("status");
186
+
187
+ connectBtn.addEventListener("click", async () => {
188
+ try {
189
+ status.textContent = "Connecting...";
190
+
191
+ const uri = await client.connect();
192
+ const qr = await client.generateQRCode(uri);
193
+
194
+ qrCode.src = qr;
195
+ qrContainer.style.display = "block";
196
+ status.textContent = "Scan QR code with your wallet";
197
+ } catch (error) {
198
+ status.textContent = "Error: " + error.message;
199
+ }
200
+ });
201
+
202
+ client.on("session_proposal", () => {
203
+ qrContainer.style.display = "none";
204
+ status.textContent = "Connected! ✓";
205
+ });
206
+ </script>
207
+ </body>
208
+ </html>
209
+ ```
210
+
211
+ ### Vue.js 3
212
+
213
+ ```vue
214
+ <template>
215
+ <div>
216
+ <button @click="connect">Connect Wallet</button>
217
+ <img v-if="qrCode" :src="qrCode" alt="QR Code" />
218
+ <div>{{ status }}</div>
219
+ </div>
220
+ </template>
221
+
222
+ <script setup>
223
+ import { ref, onMounted } from "vue";
224
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol/vanilla";
225
+
226
+ const qrCode = ref(null);
227
+ const status = ref("");
228
+ let client = null;
229
+
230
+ onMounted(async () => {
231
+ client = new WalletConnectClient({
232
+ projectId: "YOUR_PROJECT_ID",
233
+ metadata: {
234
+ name: "Vue App",
235
+ description: "WalletConnect Vue Demo",
236
+ url: "https://example.com",
237
+ icons: ["https://example.com/icon.png"],
238
+ },
239
+ });
240
+
241
+ await client.initialize();
242
+
243
+ client.on("session_proposal", () => {
244
+ qrCode.value = null;
245
+ status.value = "Connected!";
246
+ });
247
+ });
248
+
249
+ const connect = async () => {
250
+ try {
251
+ status.value = "Connecting...";
252
+ const uri = await client.connect();
253
+ qrCode.value = await client.generateQRCode(uri);
254
+ } catch (error) {
255
+ status.value = "Error: " + error.message;
256
+ }
257
+ };
258
+ </script>
259
+ ```
260
+
261
+ ### Angular
262
+
263
+ ```typescript
264
+ import { Component, OnInit } from "@angular/core";
265
+ import { WalletConnectClient } from "@tomo-inc/wallet-connect-protocol/vanilla";
266
+
267
+ @Component({
268
+ selector: "app-wallet-connect",
269
+ template: `
270
+ <button (click)="connect()">Connect Wallet</button>
271
+ <img *ngIf="qrCode" [src]="qrCode" alt="QR Code" />
272
+ <div>{{ status }}</div>
273
+ `,
274
+ })
275
+ export class WalletConnectComponent implements OnInit {
276
+ client: WalletConnectClient;
277
+ qrCode: string | null = null;
278
+ status: string = "";
279
+
280
+ async ngOnInit() {
281
+ this.client = new WalletConnectClient({
282
+ projectId: "YOUR_PROJECT_ID",
283
+ metadata: {
284
+ name: "Angular App",
285
+ description: "WalletConnect Angular Demo",
286
+ url: "https://example.com",
287
+ icons: ["https://example.com/icon.png"],
288
+ },
289
+ });
290
+
291
+ await this.client.initialize();
292
+
293
+ this.client.on("session_proposal", () => {
294
+ this.qrCode = null;
295
+ this.status = "Connected!";
296
+ });
297
+ }
298
+
299
+ async connect() {
300
+ try {
301
+ this.status = "Connecting...";
302
+ const uri = await this.client.connect();
303
+ this.qrCode = await this.client.generateQRCode(uri);
304
+ } catch (error) {
305
+ this.status = "Error: " + error.message;
306
+ }
307
+ }
308
+ }
309
+ ```
310
+
311
+ ### Svelte
312
+
313
+ ```svelte
314
+ <script>
315
+ import { onMount } from 'svelte';
316
+ import { WalletConnectClient } from '@tomo-inc/wallet-connect-protocol/vanilla';
317
+
318
+ let client;
319
+ let qrCode = null;
320
+ let status = '';
321
+
322
+ onMount(async () => {
323
+ client = new WalletConnectClient({
324
+ projectId: 'YOUR_PROJECT_ID',
325
+ metadata: {
326
+ name: 'Svelte App',
327
+ description: 'WalletConnect Svelte Demo',
328
+ url: 'https://example.com',
329
+ icons: ['https://example.com/icon.png']
330
+ }
331
+ });
332
+
333
+ await client.initialize();
334
+
335
+ client.on('session_proposal', () => {
336
+ qrCode = null;
337
+ status = 'Connected!';
338
+ });
339
+ });
340
+
341
+ async function connect() {
342
+ try {
343
+ status = 'Connecting...';
344
+ const uri = await client.connect();
345
+ qrCode = await client.generateQRCode(uri);
346
+ } catch (error) {
347
+ status = 'Error: ' + error.message;
348
+ }
349
+ }
350
+ </script>
351
+
352
+ <button on:click={connect}>Connect Wallet</button>
353
+ {#if qrCode}
354
+ <img src={qrCode} alt="QR Code" />
355
+ {/if}
356
+ <div>{status}</div>
357
+ ```
358
+
359
+ ## 🌐 Get Wallet List
360
+
361
+ ```javascript
362
+ import {
363
+ getAllWallets,
364
+ getWalletsByChain,
365
+ } from "@tomo-inc/wallet-connect-protocol/vanilla";
366
+
367
+ // Get all WalletConnect supported wallets
368
+ const wallets = await getAllWallets();
369
+ console.log(`Found ${wallets.length} wallets`);
370
+
371
+ // Get wallets that support Ethereum
372
+ const ethWallets = await getWalletsByChain("eip155:1");
373
+ console.log(`Found ${ethWallets.length} Ethereum wallets`);
374
+
375
+ // Display wallet options
376
+ wallets.forEach((wallet) => {
377
+ console.log(`${wallet.name}: ${wallet.homepage}`);
378
+ });
379
+ ```
380
+
381
+ ## 🔐 SIWE (Sign-In with Ethereum)
382
+
383
+ ```javascript
384
+ import {
385
+ SiweAuth,
386
+ createSiweMessage,
387
+ } from "@tomo-inc/wallet-connect-protocol/vanilla";
388
+
389
+ // Create SIWE authenticator
390
+ const siwe = new SiweAuth({
391
+ domain: window.location.host,
392
+ uri: window.location.origin,
393
+ statement: "Sign in to my app",
394
+ });
395
+
396
+ // After wallet is connected, sign in
397
+ const session = client.getActiveSessions()[0];
398
+ const address = session.namespaces.eip155.accounts[0].split(":")[2];
399
+
400
+ // Create SIWE message
401
+ const message = createSiweMessage({
402
+ domain: window.location.host,
403
+ address: address,
404
+ statement: "Sign in to my app",
405
+ uri: window.location.origin,
406
+ version: "1",
407
+ chainId: "1",
408
+ nonce: Math.random().toString(36).substring(7),
409
+ });
410
+
411
+ // Request signature
412
+ const signature = await client.request({
413
+ topic: session.topic,
414
+ chainId: "eip155:1",
415
+ request: {
416
+ method: "personal_sign",
417
+ params: [message, address],
418
+ },
419
+ });
420
+
421
+ console.log("SIWE signature:", signature);
422
+ ```
423
+
424
+ ## 📚 Available Exports
425
+
426
+ ### Core Client
427
+
428
+ - `WalletConnectClient` - Main WalletConnect client class
429
+
430
+ ### Utility Functions
431
+
432
+ - `formatAddress` - Format Ethereum address
433
+ - `extractAddressFromAccount` - Extract address from CAIP-10 account
434
+ - `extractChainIdFromAccount` - Extract chain ID from CAIP-10 account
435
+ - `generateDeepLink` - Generate wallet deep link
436
+ - `parseWalletConnectUri` - Parse WalletConnect URI
437
+ - `isValidWalletConnectUri` - Validate WalletConnect URI
438
+ - `getChainName` - Get chain name from chain ID
439
+ - `isMobile` - Detect mobile device
440
+
441
+ ### Wallet List API
442
+
443
+ - `getAllWallets` - Get all supported wallets
444
+ - `getWalletsByChain` - Get wallets by chain
445
+ - `getMobileWallets` - Get mobile wallets
446
+ - `getDesktopWallets` - Get desktop wallets
447
+ - `getBrowserWallets` - Get browser extension wallets
448
+ - `searchWallets` - Search wallets by name
449
+ - `getWalletById` - Get specific wallet by ID
450
+ - `POPULAR_WALLET_IDS` - Popular wallet IDs constant
451
+
452
+ ### Multi-Chain Support
453
+
454
+ - `createMultiChainNamespaces` - Create multi-chain namespaces
455
+ - `EVM_CHAINS` - EVM chain configurations
456
+ - `SOLANA_CHAINS` - Solana chain configurations
457
+ - `APTOS_CHAINS` - Aptos chain configurations
458
+
459
+ ### SIWE Support
460
+
461
+ - `SiweAuth` - SIWE authentication class
462
+ - `createSiweMessage` - Create SIWE message
463
+ - `verifySiweSignature` - Verify SIWE signature
464
+ - `parseSiweMessage` - Parse SIWE message
465
+
466
+ ## 🔍 Type Definitions
467
+
468
+ All exports include full TypeScript type definitions:
469
+
470
+ ```typescript
471
+ import type {
472
+ WalletConnectConfig,
473
+ SessionInfo,
474
+ ConnectParams,
475
+ Wallet,
476
+ WalletListOptions,
477
+ } from "@tomo-inc/wallet-connect-protocol/vanilla";
478
+ ```
479
+
480
+ ## ⚠️ Important Notes
481
+
482
+ 1. **No React Required**: The `/vanilla` export doesn't require React as a dependency
483
+ 2. **Browser Only**: This SDK is designed for browser environments
484
+ 3. **Project ID**: You need a project ID from [WalletConnect Cloud](https://cloud.walletconnect.com)
485
+ 4. **ES Modules**: The package uses ES modules, make sure your build tool supports it
486
+
487
+ ## 🔗 Resources
488
+
489
+ - [WalletConnect Documentation](https://docs.walletconnect.com)
490
+ - [Get Project ID](https://cloud.walletconnect.com)
491
+ - [WalletConnect Explorer](https://walletconnect.com/explorer)
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@tomo-inc/wallet-connect-protocol",
3
+ "version": "0.0.4",
4
+ "author": "tomo.inc",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "type": "module",
8
+ "description": "WalletConnect protocol SDK for Tomo Wallet",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./vanilla": {
19
+ "types": "./dist/vanilla.d.ts",
20
+ "import": "./dist/vanilla.js",
21
+ "require": "./dist/vanilla.cjs"
22
+ }
23
+ },
24
+ "dependencies": {
25
+ "@walletconnect/sign-client": "^2.16.1",
26
+ "@walletconnect/types": "^2.16.1",
27
+ "@walletconnect/utils": "^2.16.1",
28
+ "qrcode": "^1.5.4",
29
+ "siwe": "^2.3.2"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.0.0",
33
+ "@types/qrcode": "^1.5.5",
34
+ "@types/react": "^18.0.0",
35
+ "ethers": "^6.13.5",
36
+ "react": "^18.0.0",
37
+ "tsup": "^8.0.0",
38
+ "typescript": "^5.0.0"
39
+ },
40
+ "peerDependencies": {
41
+ "react": ">=16.8.0"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "react": {
45
+ "optional": false
46
+ }
47
+ },
48
+ "keywords": [
49
+ "walletconnect",
50
+ "wallet",
51
+ "tomo",
52
+ "sdk",
53
+ "web3",
54
+ "ethereum"
55
+ ],
56
+ "scripts": {
57
+ "build": "tsup",
58
+ "dev": "tsup --watch",
59
+ "typecheck": "tsc --noEmit"
60
+ }
61
+ }
package/project.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "wallet-connect-protocol",
3
+ "sourceRoot": "packages/wallet-connect-protocol/src",
4
+ "projectType": "library",
5
+ "targets": {
6
+ "build": {
7
+ "executor": "nx:run-commands",
8
+ "outputs": ["{projectRoot}/dist"],
9
+ "options": {
10
+ "command": "tsup",
11
+ "cwd": "packages/wallet-connect-protocol"
12
+ }
13
+ },
14
+ "dev": {
15
+ "executor": "nx:run-commands",
16
+ "options": {
17
+ "command": "tsup --watch",
18
+ "cwd": "packages/wallet-connect-protocol"
19
+ }
20
+ },
21
+ "typecheck": {
22
+ "executor": "nx:run-commands",
23
+ "options": {
24
+ "command": "tsc --noEmit",
25
+ "cwd": "packages/wallet-connect-protocol"
26
+ }
27
+ },
28
+ "lint": {
29
+ "executor": "nx:run-commands",
30
+ "options": {
31
+ "command": "eslint src/**/*.{ts,tsx}",
32
+ "cwd": "packages/wallet-connect-protocol"
33
+ }
34
+ },
35
+ "lint:fix": {
36
+ "executor": "nx:run-commands",
37
+ "options": {
38
+ "command": "eslint src/**/*.{ts,tsx} --fix",
39
+ "cwd": "packages/wallet-connect-protocol"
40
+ }
41
+ },
42
+ "format": {
43
+ "executor": "nx:run-commands",
44
+ "options": {
45
+ "command": "prettier --write \"src/**/*.{ts,tsx}\"",
46
+ "cwd": "packages/wallet-connect-protocol"
47
+ }
48
+ },
49
+ "clean": {
50
+ "executor": "nx:run-commands",
51
+ "options": {
52
+ "command": "rm -rf dist",
53
+ "cwd": "packages/wallet-connect-protocol"
54
+ }
55
+ }
56
+ },
57
+ "tags": ["scope:wallet-connect", "type:library"]
58
+ }