tresori-sdk 1.0.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/README.md ADDED
@@ -0,0 +1,603 @@
1
+ # Tresori SDK
2
+
3
+ A comprehensive TypeScript/JavaScript SDK for interacting with the Tresori wallet API. Supports wallet creation, balance queries, transactions, smart contract compilation, deployment, and gasless transactions via relayer.
4
+
5
+ ## ✨ Features
6
+
7
+ - ✅ **Universal Compatibility** - Works in **browser**, **Node.js**, and **React Native** (Android & iOS)
8
+ - ✅ **TypeScript & JavaScript** - Full TypeScript support with type definitions
9
+ - ✅ **Framework Support** - React hooks, React Native, and Angular service included
10
+ - ✅ **All API Endpoints** - Complete coverage of all Tresori API endpoints
11
+ - ✅ **Error Handling** - Comprehensive error handling
12
+ - ✅ **Type Safe** - Full TypeScript type definitions
13
+ - ✅ **Mobile Ready** - Native support for Android and iOS
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install tresori-sdk
19
+ ```
20
+
21
+ ### For Node.js (Server-side)
22
+
23
+ If you're using the SDK in Node.js and need to compile Solidity files, you'll also need:
24
+
25
+ ```bash
26
+ npm install form-data
27
+ ```
28
+
29
+ Note: `form-data` is an optional dependency. It's only needed for the `compileSolCode` method in Node.js environments. In browsers, the native `FormData` API is used automatically.
30
+
31
+ ## Environment Support
32
+
33
+ ### ✅ Browser (Client-side)
34
+
35
+ - Works in all modern browsers
36
+ - Uses native `FormData` API for file uploads
37
+ - Perfect for React, Angular, Vue, or vanilla JS projects
38
+ - No additional dependencies needed
39
+
40
+ ### ✅ React Native (Mobile - Android & iOS)
41
+
42
+ - Works on both Android and iOS
43
+ - Uses native React Native `FormData` API
44
+ - Perfect for mobile applications
45
+ - File operations supported with file URIs
46
+ - See [React Native Setup Guide](examples/react-native-setup.md) for details
47
+
48
+ ### ✅ Node.js (Server-side)
49
+
50
+ - Works in Node.js 14+ environments
51
+ - Uses `form-data` package for file uploads (optional dependency)
52
+ - Perfect for backend services, scripts, and serverless functions
53
+
54
+ ### ✅ Both JavaScript and TypeScript
55
+
56
+ - Full TypeScript support with complete type definitions
57
+ - Works with plain JavaScript (ES6+)
58
+ - Type-safe API calls with IntelliSense support
59
+
60
+ ## Quick Start
61
+
62
+ ### JavaScript/TypeScript
63
+
64
+ ```typescript
65
+ import { TresoriSDK } from "tresori-sdk";
66
+
67
+ // Initialize the SDK with API key and base URL
68
+ // baseUrl can be different for sandbox or production environments
69
+ const sdk = new TresoriSDK({
70
+ apiKey: "your-api-key-here", // Required: Your API key
71
+ baseUrl: "https://api.tresori.com", // Required: API base URL (sandbox or production)
72
+ });
73
+
74
+ // Create a wallet
75
+ const wallet = await sdk.createWallet({
76
+ walletType: "CUSTODIAL",
77
+ chainId: 11155111,
78
+ userId: "304b32a9-2b76-4b30-bf01-1c6e25a2dda3",
79
+ });
80
+
81
+ console.log("Wallet Address:", wallet.result.address);
82
+ ```
83
+
84
+ ### Environment Configuration
85
+
86
+ The SDK requires both `apiKey` and `baseUrl` to be configured during initialization. This allows you to easily switch between sandbox and production environments:
87
+
88
+ ```typescript
89
+ // Production Environment
90
+ const productionSDK = new TresoriSDK({
91
+ apiKey: "your-production-api-key",
92
+ baseUrl: "https://api.tresori.com", // Production URL
93
+ });
94
+
95
+ // Sandbox Environment
96
+ const sandboxSDK = new TresoriSDK({
97
+ apiKey: "your-sandbox-api-key",
98
+ baseUrl: "https://sandbox-api.tresori.com", // Sandbox URL
99
+ });
100
+
101
+ // Local Development
102
+ const localSDK = new TresoriSDK({
103
+ apiKey: "your-local-api-key",
104
+ baseUrl: "http://localhost:3000", // Local development URL
105
+ });
106
+ ```
107
+
108
+ **Note:** Both `apiKey` and `baseUrl` are required. The `baseUrl` defaults to `http://localhost:3000` if not provided, but it's recommended to always specify it explicitly for clarity.
109
+
110
+ ## API Reference
111
+
112
+ ### Create Wallet
113
+
114
+ Create a new custodial or non-custodial wallet.
115
+
116
+ ```typescript
117
+ const wallet = await sdk.createWallet({
118
+ walletType: "CUSTODIAL", // or 'NON_CUSTODIAL'
119
+ chainId: 11155111,
120
+ userId: "your-user-id",
121
+ });
122
+
123
+ // Response:
124
+ // {
125
+ // status: 200,
126
+ // message: "success",
127
+ // result: {
128
+ // address: "0x...",
129
+ // balance: "0.0",
130
+ // publicKey: "0x..."
131
+ // }
132
+ // }
133
+ ```
134
+
135
+ ### Get Balance
136
+
137
+ Get native token balance or ERC20 token balance.
138
+
139
+ #### Native Token Balance
140
+
141
+ ```typescript
142
+ const balance = await sdk.getBalance({
143
+ chainId: 11155111,
144
+ address: "0x0aA6eB8B6f8f2922c8193e594812B511FC230717",
145
+ });
146
+
147
+ // Response: "0.0" (string)
148
+ ```
149
+
150
+ #### ERC20 Token Balance
151
+
152
+ ```typescript
153
+ const balance = await sdk.getBalance({
154
+ chainId: 11155111,
155
+ address: "0x0aA6eB8B6f8f2922c8193e594812B511FC230717",
156
+ smartContractAddress: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
157
+ });
158
+
159
+ // Response:
160
+ // {
161
+ // native: "0.01",
162
+ // token: {
163
+ // balance: "0.0",
164
+ // symbol: "UNKNOWN",
165
+ // name: null,
166
+ // contractAddress: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
167
+ // decimals: 18
168
+ // }
169
+ // }
170
+ ```
171
+
172
+ ### Send Transaction
173
+
174
+ Send native token or ERC20 token transaction.
175
+
176
+ #### Send Native Token
177
+
178
+ ```typescript
179
+ const tx = await sdk.sendTransaction({
180
+ chainId: 11155111,
181
+ to: "0xe3240ff264bef46e9e87e056f4c8098a4aec5927",
182
+ amount: 0.01,
183
+ address: "0x113bddc9D755246851a0E1791B7fb3865b0B502C",
184
+ });
185
+
186
+ console.log("Transaction Hash:", tx.result.hash);
187
+ ```
188
+
189
+ #### Send ERC20 Token
190
+
191
+ ```typescript
192
+ const tx = await sdk.sendTransaction({
193
+ chainId: 11155111,
194
+ to: "0xe3240ff264bef46e9e87e056f4c8098a4aec5927",
195
+ amount: 0.01,
196
+ address: "0x113bddc9D755246851a0E1791B7fb3865b0B502C",
197
+ smartContractAddress: "0x113bddc9D755246851a0E1791B7fb3865b0B502C",
198
+ });
199
+ ```
200
+
201
+ ### Check Transaction Status
202
+
203
+ Check the status of a transaction.
204
+
205
+ ```typescript
206
+ const status = await sdk.checkTransactionStatus({
207
+ transactionHash:
208
+ "0x573a115584fdf63397a9e91a8d06f28dfb69477fb345c8d2c78c6735dee4a191",
209
+ chainId: 11155111,
210
+ });
211
+
212
+ console.log("Status:", status.result.status); // "SUCCESS"
213
+ console.log("Block Number:", status.result.blockNumber);
214
+ ```
215
+
216
+ ### Compile Solidity Code
217
+
218
+ Compile a Solidity file. Works in both browser and Node.js environments.
219
+
220
+ #### Node.js (Server-side)
221
+
222
+ ```typescript
223
+ // Using file path
224
+ const compiled = await sdk.compileSolCode("/path/to/contract.sol");
225
+
226
+ // Or using Buffer
227
+ const fs = require("fs");
228
+ const fileBuffer = fs.readFileSync("/path/to/contract.sol");
229
+ const compiled = await sdk.compileSolCode(fileBuffer, "contract.sol");
230
+
231
+ console.log("ABI:", compiled.result.abi);
232
+ console.log("Bytecode:", compiled.result.bytecode);
233
+ console.log("Contract Name:", compiled.result.contractName);
234
+ ```
235
+
236
+ #### Browser (Client-side)
237
+
238
+ ```typescript
239
+ // Using File input from HTML
240
+ const fileInput = document.querySelector(
241
+ 'input[type="file"]'
242
+ ) as HTMLInputElement;
243
+ const file = fileInput.files?.[0];
244
+ if (file) {
245
+ const compiled = await sdk.compileSolCode(file);
246
+ console.log("ABI:", compiled.result.abi);
247
+ }
248
+
249
+ // Or using File/Blob object
250
+ const blob = new Blob([solidityCode], { type: "text/plain" });
251
+ const compiled = await sdk.compileSolCode(blob, "contract.sol");
252
+ ```
253
+
254
+ ### Deploy Contract
255
+
256
+ Deploy a smart contract.
257
+
258
+ ```typescript
259
+ const deployment = await sdk.deployContract({
260
+ address: '0x113bddc9D755246851a0E1791B7fb3865b0B502C',
261
+ chainId: 11155111,
262
+ abi: [...], // Contract ABI array
263
+ bytecode: '0x608060405234801561000f575f5ffd5b...',
264
+ contractName: 'MintableToken',
265
+ constructorArgs: ['x', 'SYM', 18, '1000000000000000000000', '10000000000000000000000'], // Optional
266
+ });
267
+
268
+ console.log('Contract Address:', deployment.result.contractAddress);
269
+ console.log('Transaction Hash:', deployment.result.transactionHash);
270
+ ```
271
+
272
+ ### Write Transaction (Gasless)
273
+
274
+ Execute a write transaction on a smart contract (gasless).
275
+
276
+ ```typescript
277
+ const result = await sdk.writeTransaction({
278
+ contractAddress: '0xd80E071f5d58Cb5Cc20a1502AD707F1C3DEA935C',
279
+ functionName: 'increment',
280
+ params: [],
281
+ abi: [...], // Contract ABI array
282
+ fromAddress: '0x113bddc9D755246851a0E1791B7fb3865b0B502C',
283
+ chainId: 11155111,
284
+ });
285
+
286
+ console.log('Transaction Hash:', result.result.txHash);
287
+ console.log('Is Gasless:', result.result.isGasless); // true
288
+ ```
289
+
290
+ ### Relayer Send Transaction (Gasless)
291
+
292
+ Send a transaction via relayer (gasless).
293
+
294
+ ```typescript
295
+ // Send native token (gasless)
296
+ const result = await sdk.relayerSendTransaction({
297
+ fromAddress: "0x113bddc9D755246851a0E1791B7fb3865b0B502C",
298
+ to: "0xc1cca3d4225cff920a22ca937230cec06f28f9bf",
299
+ amount: 1,
300
+ chainId: 11155111,
301
+ });
302
+
303
+ // Send ERC20 token (gasless)
304
+ const result = await sdk.relayerSendTransaction({
305
+ fromAddress: "0x113bddc9D755246851a0E1791B7fb3865b0B502C",
306
+ to: "0xc1cca3d4225cff920a22ca937230cec06f28f9bf",
307
+ amount: 1,
308
+ chainId: 11155111,
309
+ tokenAddress: "0xcb4139F276e80Ec5Dd9D1E19c316829A4b243bD2",
310
+ });
311
+
312
+ console.log("Transaction Hash:", result.result.txHash);
313
+ console.log("Explorer URL:", result.result.explorerUrl);
314
+ ```
315
+
316
+ ## Usage Examples
317
+
318
+ ### Browser (Client-side) Example
319
+
320
+ ```typescript
321
+ // In a browser environment (React, Angular, Vue, or vanilla JS)
322
+ import { TresoriSDK } from "tresori-sdk";
323
+
324
+ // Initialize with API key and base URL
325
+ const sdk = new TresoriSDK({
326
+ apiKey: "your-api-key", // Your API key
327
+ baseUrl: "https://api.tresori.com", // Production or sandbox URL
328
+ });
329
+
330
+ // All methods work in browser
331
+ const wallet = await sdk.createWallet({
332
+ walletType: "CUSTODIAL",
333
+ chainId: 11155111,
334
+ userId: "user-123",
335
+ });
336
+ ```
337
+
338
+ ### Node.js (Server-side) Example
339
+
340
+ ```typescript
341
+ // In a Node.js environment
342
+ import { TresoriSDK } from "tresori-sdk";
343
+
344
+ // Initialize with API key and base URL
345
+ const sdk = new TresoriSDK({
346
+ apiKey: "your-api-key", // Your API key
347
+ baseUrl: "https://api.tresori.com", // Production or sandbox URL
348
+ });
349
+
350
+ // All methods work in Node.js
351
+ const wallet = await sdk.createWallet({
352
+ walletType: "CUSTODIAL",
353
+ chainId: 11155111,
354
+ userId: "user-123",
355
+ });
356
+ ```
357
+
358
+ ### Environment-based Configuration
359
+
360
+ You can easily switch between environments using environment variables:
361
+
362
+ ```typescript
363
+ import { TresoriSDK } from "tresori-sdk";
364
+
365
+ const sdk = new TresoriSDK({
366
+ apiKey: process.env.TRESORI_API_KEY || "your-api-key",
367
+ baseUrl: process.env.TRESORI_API_URL || "https://api.tresori.com",
368
+ });
369
+ ```
370
+
371
+ Or create environment-specific instances:
372
+
373
+ ```typescript
374
+ // config.ts
375
+ export const getSDK = (environment: "sandbox" | "production") => {
376
+ const configs = {
377
+ sandbox: {
378
+ apiKey: "sandbox-api-key",
379
+ baseUrl: "https://sandbox-api.tresori.com",
380
+ },
381
+ production: {
382
+ apiKey: "production-api-key",
383
+ baseUrl: "https://api.tresori.com",
384
+ },
385
+ };
386
+
387
+ return new TresoriSDK(configs[environment]);
388
+ };
389
+
390
+ // Usage
391
+ const sdk = getSDK(
392
+ process.env.NODE_ENV === "production" ? "production" : "sandbox"
393
+ );
394
+ ```
395
+
396
+ ## React Integration
397
+
398
+ For React projects, use the provided hook:
399
+
400
+ ```typescript
401
+ import { useTresoriSDK } from "tresori-sdk/react";
402
+ import { useState } from "react";
403
+
404
+ function WalletComponent() {
405
+ const { createWallet, getBalance, loading, error } = useTresoriSDK({
406
+ apiKey: "your-api-key", // Your API key
407
+ baseUrl: "https://api.tresori.com", // Production or sandbox URL
408
+ });
409
+
410
+ const handleCreateWallet = async () => {
411
+ try {
412
+ const wallet = await createWallet({
413
+ walletType: "CUSTODIAL",
414
+ chainId: 11155111,
415
+ userId: "user-123",
416
+ });
417
+ console.log("Wallet created:", wallet.result.address);
418
+ } catch (err) {
419
+ console.error("Error:", err);
420
+ }
421
+ };
422
+
423
+ return (
424
+ <div>
425
+ <button onClick={handleCreateWallet} disabled={loading}>
426
+ {loading ? "Creating..." : "Create Wallet"}
427
+ </button>
428
+ {error && <p>Error: {error.message}</p>}
429
+ </div>
430
+ );
431
+ }
432
+ ```
433
+
434
+ ## React Native Integration
435
+
436
+ For React Native projects (Android & iOS), use the SDK directly:
437
+
438
+ ```typescript
439
+ import { TresoriSDK } from "tresori-sdk";
440
+ import { useState } from "react";
441
+ import { View, Button, Text } from "react-native";
442
+
443
+ function WalletScreen() {
444
+ const [sdk] = useState(
445
+ () =>
446
+ new TresoriSDK({
447
+ apiKey: "your-api-key", // Your API key
448
+ baseUrl: "https://api.tresori.com", // Production or sandbox URL
449
+ })
450
+ );
451
+ const [walletAddress, setWalletAddress] = useState<string | null>(null);
452
+
453
+ const handleCreateWallet = async () => {
454
+ try {
455
+ const wallet = await sdk.createWallet({
456
+ walletType: "CUSTODIAL",
457
+ chainId: 11155111,
458
+ userId: "user-123",
459
+ });
460
+ setWalletAddress(wallet.result.address);
461
+ } catch (err) {
462
+ console.error("Error:", err);
463
+ }
464
+ };
465
+
466
+ return (
467
+ <View>
468
+ <Button title="Create Wallet" onPress={handleCreateWallet} />
469
+ {walletAddress && <Text>Address: {walletAddress}</Text>}
470
+ </View>
471
+ );
472
+ }
473
+ ```
474
+
475
+ ### File Upload in React Native
476
+
477
+ For compiling Solidity files in React Native, use file URIs:
478
+
479
+ ```typescript
480
+ import DocumentPicker from "react-native-document-picker";
481
+ import { TresoriSDK } from "tresori-sdk";
482
+
483
+ const sdk = new TresoriSDK({
484
+ apiKey: "your-api-key",
485
+ baseUrl: "https://api.tresori.com",
486
+ });
487
+
488
+ // Pick and compile a file
489
+ const compileContract = async () => {
490
+ const res = await DocumentPicker.pick({
491
+ type: [DocumentPicker.types.allFiles],
492
+ });
493
+
494
+ const compiled = await sdk.compileSolCode(
495
+ res[0].uri, // file:// URI
496
+ res[0].name
497
+ );
498
+
499
+ console.log("ABI:", compiled.result.abi);
500
+ };
501
+ ```
502
+
503
+ **See [React Native Setup Guide](examples/react-native-setup.md) for complete setup instructions.**
504
+
505
+ ## Angular Integration
506
+
507
+ For Angular projects, use the provided service:
508
+
509
+ ```typescript
510
+ import { Component, OnInit } from "@angular/core";
511
+ import { TresoriSDKService } from "tresori-sdk/angular";
512
+
513
+ @Component({
514
+ selector: "app-wallet",
515
+ templateUrl: "./wallet.component.html",
516
+ })
517
+ export class WalletComponent implements OnInit {
518
+ private tresoriService: TresoriSDKService;
519
+
520
+ constructor() {
521
+ this.tresoriService = new TresoriSDKService({
522
+ apiKey: "your-api-key", // Your API key
523
+ baseUrl: "https://api.tresori.com", // Production or sandbox URL
524
+ });
525
+ }
526
+
527
+ ngOnInit() {
528
+ this.createWallet();
529
+ }
530
+
531
+ createWallet() {
532
+ this.tresoriService
533
+ .createWallet({
534
+ walletType: "CUSTODIAL",
535
+ chainId: 11155111,
536
+ userId: "user-123",
537
+ })
538
+ .subscribe({
539
+ next: (wallet) => {
540
+ console.log("Wallet created:", wallet.result.address);
541
+ },
542
+ error: (err) => {
543
+ console.error("Error:", err);
544
+ },
545
+ });
546
+ }
547
+ }
548
+ ```
549
+
550
+ ## Error Handling
551
+
552
+ All methods throw errors that can be caught:
553
+
554
+ ```typescript
555
+ try {
556
+ const wallet = await sdk.createWallet({
557
+ walletType: "CUSTODIAL",
558
+ chainId: 11155111,
559
+ userId: "user-123",
560
+ });
561
+ } catch (error) {
562
+ if (error instanceof Error) {
563
+ console.error("API Error:", error.message);
564
+ }
565
+ }
566
+ ```
567
+
568
+ ## TypeScript Support
569
+
570
+ The SDK is fully typed. Import types as needed:
571
+
572
+ ```typescript
573
+ import {
574
+ CreateWalletRequest,
575
+ CreateWalletResponse,
576
+ GetBalanceRequest,
577
+ GetBalanceResponse,
578
+ SendTransactionRequest,
579
+ SendTransactionResponse,
580
+ CheckTransactionStatusRequest,
581
+ CheckTransactionStatusResponse,
582
+ CompileSolCodeResponse,
583
+ DeployContractRequest,
584
+ DeployContractResponse,
585
+ WriteTransactionRequest,
586
+ WriteTransactionResponse,
587
+ RelayerSendTransactionRequest,
588
+ RelayerSendTransactionResponse,
589
+ } from "tresori-sdk";
590
+ ```
591
+
592
+ ## License
593
+
594
+ MIT
595
+
596
+ ## Support
597
+
598
+ For issues and questions, please open an issue on the [GitHub repository](https://github.com/hrajput2507/nimbly-npm/issues).
599
+
600
+ ## Repository
601
+
602
+ - **GitHub**: [https://github.com/hrajput2507/nimbly-npm](https://github.com/hrajput2507/nimbly-npm)
603
+ - **Issues**: [https://github.com/hrajput2507/nimbly-npm/issues](https://github.com/hrajput2507/nimbly-npm/issues)
@@ -0,0 +1 @@
1
+ export { TresoriSDKService } from "./tresori-sdk.service";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TresoriSDKService = void 0;
4
+ var tresori_sdk_service_1 = require("./tresori-sdk.service");
5
+ Object.defineProperty(exports, "TresoriSDKService", { enumerable: true, get: function () { return tresori_sdk_service_1.TresoriSDKService; } });
@@ -0,0 +1,44 @@
1
+ import { Observable } from "rxjs";
2
+ import { TresoriSDK } from "../tresori-sdk";
3
+ import { TresoriSDKConfig } from "../types";
4
+ import { CreateWalletRequest, CreateWalletResponse, GetBalanceRequest, GetBalanceResponse, SendTransactionRequest, SendTransactionResponse, CheckTransactionStatusRequest, CheckTransactionStatusResponse, CompileSolCodeResponse, DeployContractRequest, DeployContractResponse, WriteTransactionRequest, WriteTransactionResponse, RelayerSendTransactionRequest, RelayerSendTransactionResponse } from "../types";
5
+ export declare class TresoriSDKService {
6
+ private sdk;
7
+ constructor(config: TresoriSDKConfig);
8
+ /**
9
+ * Create a new wallet
10
+ */
11
+ createWallet(request: CreateWalletRequest): Observable<CreateWalletResponse>;
12
+ /**
13
+ * Get wallet balance (native token or ERC20 token)
14
+ */
15
+ getBalance(request: GetBalanceRequest): Observable<GetBalanceResponse>;
16
+ /**
17
+ * Send a transaction (native token or ERC20 token)
18
+ */
19
+ sendTransaction(request: SendTransactionRequest): Observable<SendTransactionResponse>;
20
+ /**
21
+ * Check transaction status
22
+ */
23
+ checkTransactionStatus(request: CheckTransactionStatusRequest): Observable<CheckTransactionStatusResponse>;
24
+ /**
25
+ * Compile Solidity code
26
+ */
27
+ compileSolCode(solFilePath: string): Observable<CompileSolCodeResponse>;
28
+ /**
29
+ * Deploy a smart contract
30
+ */
31
+ deployContract(request: DeployContractRequest): Observable<DeployContractResponse>;
32
+ /**
33
+ * Execute a write transaction on a smart contract (gasless)
34
+ */
35
+ writeTransaction(request: WriteTransactionRequest): Observable<WriteTransactionResponse>;
36
+ /**
37
+ * Send a transaction via relayer (gasless)
38
+ */
39
+ relayerSendTransaction(request: RelayerSendTransactionRequest): Observable<RelayerSendTransactionResponse>;
40
+ /**
41
+ * Get the underlying SDK instance for advanced usage
42
+ */
43
+ getSDK(): TresoriSDK;
44
+ }