@varity-labs/client-js 2.0.0-alpha.1

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.
@@ -0,0 +1,355 @@
1
+ "use strict";
2
+ /**
3
+ * Wallet Manager - Handle wallet connections and operations
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
18
+ }) : function(o, v) {
19
+ o["default"] = v;
20
+ });
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.WalletManager = void 0;
40
+ const wallets_1 = require("thirdweb/wallets");
41
+ const types_1 = require("../types");
42
+ const formatting_1 = require("../utils/formatting");
43
+ /**
44
+ * WalletManager - Manage wallet connections and operations
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * // Connect MetaMask
49
+ * const account = await walletManager.connect({ walletType: 'metamask' });
50
+ *
51
+ * // Get balance
52
+ * const balance = await walletManager.getBalance();
53
+ *
54
+ * // Disconnect
55
+ * walletManager.disconnect();
56
+ * ```
57
+ */
58
+ class WalletManager {
59
+ constructor(client, chain) {
60
+ this.client = client;
61
+ this.chain = chain;
62
+ this.activeWallet = null;
63
+ this.activeAccount = null;
64
+ }
65
+ /**
66
+ * Connect wallet
67
+ * @param options Wallet connection options
68
+ * @returns Connected account
69
+ */
70
+ async connect(options) {
71
+ try {
72
+ let wallet;
73
+ switch (options.walletType) {
74
+ case 'metamask':
75
+ wallet = (0, wallets_1.createWallet)('io.metamask');
76
+ break;
77
+ case 'walletconnect':
78
+ // WalletConnect v5 uses walletConnect() function
79
+ wallet = (0, wallets_1.walletConnect)();
80
+ break;
81
+ case 'coinbase':
82
+ wallet = (0, wallets_1.createWallet)('com.coinbase.wallet');
83
+ break;
84
+ case 'injected':
85
+ // Use MetaMask as default injected wallet in v5
86
+ wallet = (0, wallets_1.createWallet)('io.metamask');
87
+ break;
88
+ case 'embedded':
89
+ // In v5, use inAppWallet() function
90
+ wallet = (0, wallets_1.inAppWallet)();
91
+ break;
92
+ default:
93
+ throw new types_1.WalletError(`Unsupported wallet type: ${options.walletType}`, { walletType: options.walletType });
94
+ }
95
+ // Connect wallet
96
+ const account = await wallet.connect({
97
+ client: this.client,
98
+ chain: this.chain,
99
+ });
100
+ this.activeWallet = wallet;
101
+ this.activeAccount = account;
102
+ return account;
103
+ }
104
+ catch (error) {
105
+ throw new types_1.WalletError(`Failed to connect wallet: ${error.message}`, { walletType: options.walletType, error });
106
+ }
107
+ }
108
+ /**
109
+ * Disconnect wallet
110
+ */
111
+ disconnect() {
112
+ if (this.activeWallet) {
113
+ this.activeWallet.disconnect();
114
+ this.activeWallet = null;
115
+ this.activeAccount = null;
116
+ }
117
+ }
118
+ /**
119
+ * Get connected account
120
+ * @returns Active account or null
121
+ */
122
+ getAccount() {
123
+ return this.activeAccount;
124
+ }
125
+ /**
126
+ * Check if wallet is connected
127
+ * @returns True if connected
128
+ */
129
+ isConnected() {
130
+ return this.activeAccount !== null;
131
+ }
132
+ /**
133
+ * Get wallet address
134
+ * @returns Wallet address or null
135
+ */
136
+ getAddress() {
137
+ return this.activeAccount?.address || null;
138
+ }
139
+ /**
140
+ * Get wallet balance (native currency)
141
+ * @returns Balance in wei/smallest unit
142
+ */
143
+ async getBalance() {
144
+ if (!this.activeAccount) {
145
+ throw new types_1.WalletError('No wallet connected');
146
+ }
147
+ try {
148
+ const balance = await (0, wallets_1.getWalletBalance)({
149
+ client: this.client,
150
+ chain: this.chain,
151
+ address: this.activeAccount.address,
152
+ });
153
+ return balance.value;
154
+ }
155
+ catch (error) {
156
+ throw new types_1.WalletError(`Failed to get balance: ${error.message}`, { error });
157
+ }
158
+ }
159
+ /**
160
+ * Get wallet info with balance
161
+ * @returns Wallet information
162
+ */
163
+ async getWalletInfo() {
164
+ if (!this.activeAccount) {
165
+ throw new types_1.WalletError('No wallet connected');
166
+ }
167
+ try {
168
+ const balance = await this.getBalance();
169
+ // Format based on native currency
170
+ const nativeCurrency = this.chain.nativeCurrency;
171
+ const isUSDC = nativeCurrency?.symbol === 'USDC' && nativeCurrency?.decimals === 6;
172
+ const balanceFormatted = isUSDC
173
+ ? (0, formatting_1.formatUSDC)(balance)
174
+ : this.formatBalance(balance, nativeCurrency?.decimals || 18);
175
+ return {
176
+ address: this.activeAccount.address,
177
+ balance: balance.toString(),
178
+ balanceFormatted,
179
+ chainId: this.chain.id,
180
+ };
181
+ }
182
+ catch (error) {
183
+ throw new types_1.WalletError(`Failed to get wallet info: ${error.message}`, { error });
184
+ }
185
+ }
186
+ /**
187
+ * Sign message
188
+ * @param message Message to sign
189
+ * @returns Signature
190
+ */
191
+ async signMessage(message) {
192
+ if (!this.activeAccount) {
193
+ throw new types_1.WalletError('No wallet connected');
194
+ }
195
+ try {
196
+ const signature = await this.activeAccount.signMessage({
197
+ message,
198
+ });
199
+ return signature;
200
+ }
201
+ catch (error) {
202
+ throw new types_1.WalletError(`Failed to sign message: ${error.message}`, { error });
203
+ }
204
+ }
205
+ /**
206
+ * Sign typed data (EIP-712)
207
+ * @param domain Domain data
208
+ * @param types Type definitions
209
+ * @param value Value to sign
210
+ * @returns Signature
211
+ */
212
+ async signTypedData(domain, types, value) {
213
+ if (!this.activeAccount) {
214
+ throw new types_1.WalletError('No wallet connected');
215
+ }
216
+ try {
217
+ const signature = await this.activeAccount.signTypedData({
218
+ domain,
219
+ types,
220
+ primaryType: Object.keys(types)[0],
221
+ message: value,
222
+ });
223
+ return signature;
224
+ }
225
+ catch (error) {
226
+ throw new types_1.WalletError(`Failed to sign typed data: ${error.message}`, { error });
227
+ }
228
+ }
229
+ /**
230
+ * Switch chain
231
+ * @param chainId Chain ID to switch to
232
+ */
233
+ async switchChain(chainId) {
234
+ if (!this.activeWallet) {
235
+ throw new types_1.WalletError('No wallet connected');
236
+ }
237
+ try {
238
+ // Import defineChain from thirdweb for runtime use
239
+ const { defineChain } = await Promise.resolve().then(() => __importStar(require('thirdweb')));
240
+ const targetChain = defineChain({ id: chainId });
241
+ await this.activeWallet.switchChain(targetChain);
242
+ }
243
+ catch (error) {
244
+ throw new types_1.WalletError(`Failed to switch chain: ${error.message}`, { chainId, error });
245
+ }
246
+ }
247
+ /**
248
+ * Get chain ID from wallet
249
+ * @returns Current chain ID
250
+ */
251
+ async getChainId() {
252
+ if (!this.activeAccount) {
253
+ throw new types_1.WalletError('No wallet connected');
254
+ }
255
+ return this.chain.id;
256
+ }
257
+ /**
258
+ * Send native currency transaction
259
+ * @param to Recipient address
260
+ * @param amount Amount to send (in wei/smallest unit)
261
+ * @returns Transaction hash
262
+ */
263
+ async sendTransaction(to, amount) {
264
+ if (!this.activeAccount) {
265
+ throw new types_1.WalletError('No wallet connected');
266
+ }
267
+ try {
268
+ // Create transaction
269
+ const transaction = await this.activeAccount.sendTransaction({
270
+ to,
271
+ value: amount,
272
+ chainId: this.chain.id,
273
+ });
274
+ return transaction.transactionHash;
275
+ }
276
+ catch (error) {
277
+ throw new types_1.WalletError(`Failed to send transaction: ${error.message}`, { to, amount: amount.toString(), error });
278
+ }
279
+ }
280
+ /**
281
+ * Get transaction count (nonce)
282
+ * @returns Transaction count
283
+ */
284
+ async getTransactionCount() {
285
+ if (!this.activeAccount) {
286
+ throw new types_1.WalletError('No wallet connected');
287
+ }
288
+ try {
289
+ // Get nonce
290
+ // This is a simplified version - in production, use proper nonce retrieval
291
+ return 0;
292
+ }
293
+ catch (error) {
294
+ throw new types_1.WalletError(`Failed to get transaction count: ${error.message}`, { error });
295
+ }
296
+ }
297
+ /**
298
+ * Format balance with decimals
299
+ * @param balance Balance in smallest unit
300
+ * @param decimals Number of decimals
301
+ * @returns Formatted balance string
302
+ */
303
+ formatBalance(balance, decimals) {
304
+ const divisor = BigInt(10 ** decimals);
305
+ const wholePart = balance / divisor;
306
+ const fractionalPart = balance % divisor;
307
+ const fractionalStr = fractionalPart.toString().padStart(decimals, '0');
308
+ return `${wholePart}.${fractionalStr}`;
309
+ }
310
+ /**
311
+ * Request wallet permissions
312
+ * @param permissions Permissions to request
313
+ */
314
+ async requestPermissions(permissions) {
315
+ if (!this.activeWallet) {
316
+ throw new types_1.WalletError('No wallet connected');
317
+ }
318
+ // Request permissions (implementation depends on wallet type)
319
+ console.log('Requesting permissions:', permissions);
320
+ }
321
+ /**
322
+ * Add custom token to wallet
323
+ * @param tokenAddress Token contract address
324
+ * @param symbol Token symbol
325
+ * @param decimals Token decimals
326
+ * @param image Token image URL
327
+ */
328
+ async addToken(tokenAddress, symbol, decimals, image) {
329
+ if (!this.activeWallet) {
330
+ throw new types_1.WalletError('No wallet connected');
331
+ }
332
+ try {
333
+ // Add token to wallet (MetaMask specific)
334
+ if (typeof window.ethereum !== 'undefined') {
335
+ await window.ethereum.request({
336
+ method: 'wallet_watchAsset',
337
+ params: {
338
+ type: 'ERC20',
339
+ options: {
340
+ address: tokenAddress,
341
+ symbol,
342
+ decimals,
343
+ image,
344
+ },
345
+ },
346
+ });
347
+ }
348
+ }
349
+ catch (error) {
350
+ throw new types_1.WalletError(`Failed to add token: ${error.message}`, { tokenAddress, error });
351
+ }
352
+ }
353
+ }
354
+ exports.WalletManager = WalletManager;
355
+ exports.default = WalletManager;
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@varity-labs/client-js",
3
+ "version": "2.0.0-alpha.1",
4
+ "description": "Comprehensive JavaScript/TypeScript client library for Varity L3 blockchain - Powered by Thirdweb SDK",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "build:watch": "tsc --watch",
10
+ "test": "jest --coverage",
11
+ "test:watch": "jest --watch",
12
+ "lint": "eslint src --ext .ts,.tsx",
13
+ "lint:fix": "eslint src --ext .ts,.tsx --fix",
14
+ "example": "ts-node examples/basic-usage.ts",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "keywords": [
18
+ "varity",
19
+ "blockchain",
20
+ "web3",
21
+ "thirdweb",
22
+ "arbitrum",
23
+ "l3",
24
+ "wallet",
25
+ "smart-contracts",
26
+ "ipfs",
27
+ "storage",
28
+ "siwe",
29
+ "usdc",
30
+ "depin",
31
+ "filecoin"
32
+ ],
33
+ "author": "Varity",
34
+ "license": "MIT",
35
+ "dependencies": {
36
+ "thirdweb": "^5.112.0",
37
+ "ethers": "^6.10.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^20.10.6",
41
+ "@types/jest": "^29.5.11",
42
+ "@types/react": "^18.2.48",
43
+ "typescript": "^5.3.3",
44
+ "ts-node": "^10.9.2",
45
+ "jest": "^29.7.0",
46
+ "ts-jest": "^29.1.1",
47
+ "eslint": "^8.56.0",
48
+ "@typescript-eslint/eslint-plugin": "^6.18.0",
49
+ "@typescript-eslint/parser": "^6.18.0",
50
+ "react": "^18.2.0"
51
+ },
52
+ "peerDependencies": {
53
+ "react": ">=18.0.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "react": {
57
+ "optional": true
58
+ }
59
+ },
60
+ "engines": {
61
+ "node": ">=16.0.0"
62
+ },
63
+ "files": [
64
+ "dist",
65
+ "README.md",
66
+ "LICENSE"
67
+ ],
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "https://github.com/varity-labs/varity-sdk"
71
+ },
72
+ "bugs": {
73
+ "url": "https://github.com/varity-labs/varity-sdk/issues"
74
+ },
75
+ "sideEffects": false
76
+ }