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 +603 -0
- package/dist/angular/index.d.ts +1 -0
- package/dist/angular/index.js +5 -0
- package/dist/angular/tresori-sdk.service.d.ts +44 -0
- package/dist/angular/tresori-sdk.service.js +121 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +20 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +5 -0
- package/dist/react/useTresoriSDK.d.ts +17 -0
- package/dist/react/useTresoriSDK.js +47 -0
- package/dist/react-native/index.d.ts +2 -0
- package/dist/react-native/index.js +20 -0
- package/dist/tresori-sdk.d.ts +46 -0
- package/dist/tresori-sdk.js +250 -0
- package/dist/types.d.ts +181 -0
- package/dist/types.js +2 -0
- package/package.json +93 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.TresoriSDKService = void 0;
|
|
42
|
+
const core_1 = require("@angular/core");
|
|
43
|
+
const rxjs_1 = require("rxjs");
|
|
44
|
+
const tresori_sdk_1 = require("../tresori-sdk");
|
|
45
|
+
let TresoriSDKService = (() => {
|
|
46
|
+
let _classDecorators = [(0, core_1.Injectable)({
|
|
47
|
+
providedIn: "root",
|
|
48
|
+
})];
|
|
49
|
+
let _classDescriptor;
|
|
50
|
+
let _classExtraInitializers = [];
|
|
51
|
+
let _classThis;
|
|
52
|
+
var TresoriSDKService = _classThis = class {
|
|
53
|
+
constructor(config) {
|
|
54
|
+
this.sdk = new tresori_sdk_1.TresoriSDK(config);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create a new wallet
|
|
58
|
+
*/
|
|
59
|
+
createWallet(request) {
|
|
60
|
+
return (0, rxjs_1.from)(this.sdk.createWallet(request));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get wallet balance (native token or ERC20 token)
|
|
64
|
+
*/
|
|
65
|
+
getBalance(request) {
|
|
66
|
+
return (0, rxjs_1.from)(this.sdk.getBalance(request));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Send a transaction (native token or ERC20 token)
|
|
70
|
+
*/
|
|
71
|
+
sendTransaction(request) {
|
|
72
|
+
return (0, rxjs_1.from)(this.sdk.sendTransaction(request));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Check transaction status
|
|
76
|
+
*/
|
|
77
|
+
checkTransactionStatus(request) {
|
|
78
|
+
return (0, rxjs_1.from)(this.sdk.checkTransactionStatus(request));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Compile Solidity code
|
|
82
|
+
*/
|
|
83
|
+
compileSolCode(solFilePath) {
|
|
84
|
+
return (0, rxjs_1.from)(this.sdk.compileSolCode(solFilePath));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Deploy a smart contract
|
|
88
|
+
*/
|
|
89
|
+
deployContract(request) {
|
|
90
|
+
return (0, rxjs_1.from)(this.sdk.deployContract(request));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Execute a write transaction on a smart contract (gasless)
|
|
94
|
+
*/
|
|
95
|
+
writeTransaction(request) {
|
|
96
|
+
return (0, rxjs_1.from)(this.sdk.writeTransaction(request));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Send a transaction via relayer (gasless)
|
|
100
|
+
*/
|
|
101
|
+
relayerSendTransaction(request) {
|
|
102
|
+
return (0, rxjs_1.from)(this.sdk.relayerSendTransaction(request));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get the underlying SDK instance for advanced usage
|
|
106
|
+
*/
|
|
107
|
+
getSDK() {
|
|
108
|
+
return this.sdk;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
__setFunctionName(_classThis, "TresoriSDKService");
|
|
112
|
+
(() => {
|
|
113
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
114
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
115
|
+
TresoriSDKService = _classThis = _classDescriptor.value;
|
|
116
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
117
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
118
|
+
})();
|
|
119
|
+
return TresoriSDKService = _classThis;
|
|
120
|
+
})();
|
|
121
|
+
exports.TresoriSDKService = TresoriSDKService;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.TresoriSDK = void 0;
|
|
18
|
+
var tresori_sdk_1 = require("./tresori-sdk");
|
|
19
|
+
Object.defineProperty(exports, "TresoriSDK", { enumerable: true, get: function () { return tresori_sdk_1.TresoriSDK; } });
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useTresoriSDK, UseTresoriSDKReturn } from "./useTresoriSDK";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTresoriSDK = void 0;
|
|
4
|
+
var useTresoriSDK_1 = require("./useTresoriSDK");
|
|
5
|
+
Object.defineProperty(exports, "useTresoriSDK", { enumerable: true, get: function () { return useTresoriSDK_1.useTresoriSDK; } });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TresoriSDK } from "../tresori-sdk";
|
|
2
|
+
import { TresoriSDKConfig } from "../types";
|
|
3
|
+
import { CreateWalletRequest, CreateWalletResponse, GetBalanceRequest, GetBalanceResponse, SendTransactionRequest, SendTransactionResponse, CheckTransactionStatusRequest, CheckTransactionStatusResponse, CompileSolCodeResponse, DeployContractRequest, DeployContractResponse, WriteTransactionRequest, WriteTransactionResponse, RelayerSendTransactionRequest, RelayerSendTransactionResponse } from "../types";
|
|
4
|
+
export interface UseTresoriSDKReturn {
|
|
5
|
+
sdk: TresoriSDK;
|
|
6
|
+
createWallet: (request: CreateWalletRequest) => Promise<CreateWalletResponse>;
|
|
7
|
+
getBalance: (request: GetBalanceRequest) => Promise<GetBalanceResponse>;
|
|
8
|
+
sendTransaction: (request: SendTransactionRequest) => Promise<SendTransactionResponse>;
|
|
9
|
+
checkTransactionStatus: (request: CheckTransactionStatusRequest) => Promise<CheckTransactionStatusResponse>;
|
|
10
|
+
compileSolCode: (fileInput: string | File | Blob | Buffer, fileName?: string) => Promise<CompileSolCodeResponse>;
|
|
11
|
+
deployContract: (request: DeployContractRequest) => Promise<DeployContractResponse>;
|
|
12
|
+
writeTransaction: (request: WriteTransactionRequest) => Promise<WriteTransactionResponse>;
|
|
13
|
+
relayerSendTransaction: (request: RelayerSendTransactionRequest) => Promise<RelayerSendTransactionResponse>;
|
|
14
|
+
loading: boolean;
|
|
15
|
+
error: Error | null;
|
|
16
|
+
}
|
|
17
|
+
export declare function useTresoriSDK(config: TresoriSDKConfig): UseTresoriSDKReturn;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTresoriSDK = useTresoriSDK;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const tresori_sdk_1 = require("../tresori-sdk");
|
|
6
|
+
function useTresoriSDK(config) {
|
|
7
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
8
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
9
|
+
const sdk = (0, react_1.useMemo)(() => new tresori_sdk_1.TresoriSDK(config), [config.apiKey, config.baseUrl]);
|
|
10
|
+
const handleRequest = (0, react_1.useCallback)(async (requestFn) => {
|
|
11
|
+
setLoading(true);
|
|
12
|
+
setError(null);
|
|
13
|
+
try {
|
|
14
|
+
const result = await requestFn();
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
19
|
+
setError(error);
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
setLoading(false);
|
|
24
|
+
}
|
|
25
|
+
}, []);
|
|
26
|
+
const createWallet = (0, react_1.useCallback)((request) => handleRequest(() => sdk.createWallet(request)), [sdk, handleRequest]);
|
|
27
|
+
const getBalance = (0, react_1.useCallback)((request) => handleRequest(() => sdk.getBalance(request)), [sdk, handleRequest]);
|
|
28
|
+
const sendTransaction = (0, react_1.useCallback)((request) => handleRequest(() => sdk.sendTransaction(request)), [sdk, handleRequest]);
|
|
29
|
+
const checkTransactionStatus = (0, react_1.useCallback)((request) => handleRequest(() => sdk.checkTransactionStatus(request)), [sdk, handleRequest]);
|
|
30
|
+
const compileSolCode = (0, react_1.useCallback)((fileInput, fileName) => handleRequest(() => sdk.compileSolCode(fileInput, fileName)), [sdk, handleRequest]);
|
|
31
|
+
const deployContract = (0, react_1.useCallback)((request) => handleRequest(() => sdk.deployContract(request)), [sdk, handleRequest]);
|
|
32
|
+
const writeTransaction = (0, react_1.useCallback)((request) => handleRequest(() => sdk.writeTransaction(request)), [sdk, handleRequest]);
|
|
33
|
+
const relayerSendTransaction = (0, react_1.useCallback)((request) => handleRequest(() => sdk.relayerSendTransaction(request)), [sdk, handleRequest]);
|
|
34
|
+
return {
|
|
35
|
+
sdk,
|
|
36
|
+
createWallet,
|
|
37
|
+
getBalance,
|
|
38
|
+
sendTransaction,
|
|
39
|
+
checkTransactionStatus,
|
|
40
|
+
compileSolCode,
|
|
41
|
+
deployContract,
|
|
42
|
+
writeTransaction,
|
|
43
|
+
relayerSendTransaction,
|
|
44
|
+
loading,
|
|
45
|
+
error,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.TresoriSDK = void 0;
|
|
18
|
+
var tresori_sdk_1 = require("../tresori-sdk");
|
|
19
|
+
Object.defineProperty(exports, "TresoriSDK", { enumerable: true, get: function () { return tresori_sdk_1.TresoriSDK; } });
|
|
20
|
+
__exportStar(require("../types"), exports);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TresoriSDKConfig, CreateWalletRequest, CreateWalletResponse, GetBalanceRequest, GetBalanceResponse, SendTransactionRequest, SendTransactionResponse, CheckTransactionStatusRequest, CheckTransactionStatusResponse, CompileSolCodeResponse, DeployContractRequest, DeployContractResponse, WriteTransactionRequest, WriteTransactionResponse, RelayerSendTransactionRequest, RelayerSendTransactionResponse } from "./types";
|
|
2
|
+
export declare class TresoriSDK {
|
|
3
|
+
private apiKey;
|
|
4
|
+
private baseUrl;
|
|
5
|
+
private client;
|
|
6
|
+
constructor(config: TresoriSDKConfig);
|
|
7
|
+
/**
|
|
8
|
+
* Create a new wallet
|
|
9
|
+
*/
|
|
10
|
+
createWallet(request: CreateWalletRequest): Promise<CreateWalletResponse>;
|
|
11
|
+
/**
|
|
12
|
+
* Get wallet balance (native token or ERC20 token)
|
|
13
|
+
*/
|
|
14
|
+
getBalance(request: GetBalanceRequest): Promise<GetBalanceResponse>;
|
|
15
|
+
/**
|
|
16
|
+
* Send a transaction (native token or ERC20 token)
|
|
17
|
+
*/
|
|
18
|
+
sendTransaction(request: SendTransactionRequest): Promise<SendTransactionResponse>;
|
|
19
|
+
/**
|
|
20
|
+
* Check transaction status
|
|
21
|
+
*/
|
|
22
|
+
checkTransactionStatus(request: CheckTransactionStatusRequest): Promise<CheckTransactionStatusResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* Compile Solidity code
|
|
25
|
+
* Works in both browser and Node.js environments
|
|
26
|
+
* @param fileInput - File path (Node.js), File object (browser), Blob, or Buffer
|
|
27
|
+
* @param fileName - Optional file name (required for Blob/Buffer in browser)
|
|
28
|
+
*/
|
|
29
|
+
compileSolCode(fileInput: string | File | Blob | Buffer, fileName?: string): Promise<CompileSolCodeResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Deploy a smart contract
|
|
32
|
+
*/
|
|
33
|
+
deployContract(request: DeployContractRequest): Promise<DeployContractResponse>;
|
|
34
|
+
/**
|
|
35
|
+
* Execute a write transaction on a smart contract (gasless)
|
|
36
|
+
*/
|
|
37
|
+
writeTransaction(request: WriteTransactionRequest): Promise<WriteTransactionResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Send a transaction via relayer (gasless)
|
|
40
|
+
*/
|
|
41
|
+
relayerSendTransaction(request: RelayerSendTransactionRequest): Promise<RelayerSendTransactionResponse>;
|
|
42
|
+
/**
|
|
43
|
+
* Handle API errors
|
|
44
|
+
*/
|
|
45
|
+
private handleError;
|
|
46
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TresoriSDK = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
// Environment detection
|
|
9
|
+
const isNode = typeof process !== "undefined" &&
|
|
10
|
+
process.versions != null &&
|
|
11
|
+
process.versions.node != null;
|
|
12
|
+
const isReactNative = typeof globalThis !== "undefined" &&
|
|
13
|
+
typeof globalThis.navigator !== "undefined" &&
|
|
14
|
+
globalThis.navigator.product === "ReactNative";
|
|
15
|
+
// Conditional imports for Node.js only
|
|
16
|
+
let FormDataNode;
|
|
17
|
+
let fs;
|
|
18
|
+
if (isNode) {
|
|
19
|
+
try {
|
|
20
|
+
FormDataNode = require("form-data");
|
|
21
|
+
fs = require("fs");
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
// form-data not available
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class TresoriSDK {
|
|
28
|
+
constructor(config) {
|
|
29
|
+
if (!config.apiKey) {
|
|
30
|
+
throw new Error("API key is required. Please provide apiKey in the configuration.");
|
|
31
|
+
}
|
|
32
|
+
this.apiKey = config.apiKey;
|
|
33
|
+
// baseUrl is required but defaults to localhost for backward compatibility
|
|
34
|
+
// It's recommended to always provide baseUrl explicitly
|
|
35
|
+
this.baseUrl = config.baseUrl || "http://localhost:3000";
|
|
36
|
+
this.client = axios_1.default.create({
|
|
37
|
+
baseURL: this.baseUrl,
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a new wallet
|
|
45
|
+
*/
|
|
46
|
+
async createWallet(request) {
|
|
47
|
+
try {
|
|
48
|
+
const response = await this.client.post("/v2/wallet/create-wallet", request, {
|
|
49
|
+
headers: {
|
|
50
|
+
apikey: this.apiKey,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
return response.data;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw this.handleError(error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get wallet balance (native token or ERC20 token)
|
|
61
|
+
*/
|
|
62
|
+
async getBalance(request) {
|
|
63
|
+
try {
|
|
64
|
+
const response = await this.client.post("/v2/wallet/balance", request, {
|
|
65
|
+
headers: {
|
|
66
|
+
apiKey: this.apiKey,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
return response.data;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
throw this.handleError(error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Send a transaction (native token or ERC20 token)
|
|
77
|
+
*/
|
|
78
|
+
async sendTransaction(request) {
|
|
79
|
+
try {
|
|
80
|
+
const response = await this.client.post("/v2/wallet/send-transaction", request, {
|
|
81
|
+
headers: {
|
|
82
|
+
apiKey: this.apiKey,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return response.data;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw this.handleError(error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check transaction status
|
|
93
|
+
*/
|
|
94
|
+
async checkTransactionStatus(request) {
|
|
95
|
+
try {
|
|
96
|
+
const response = await this.client.post("/v2/wallet/check-trx-status", request, {
|
|
97
|
+
headers: {
|
|
98
|
+
apiKey: this.apiKey,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
return response.data;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw this.handleError(error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Compile Solidity code
|
|
109
|
+
* Works in both browser and Node.js environments
|
|
110
|
+
* @param fileInput - File path (Node.js), File object (browser), Blob, or Buffer
|
|
111
|
+
* @param fileName - Optional file name (required for Blob/Buffer in browser)
|
|
112
|
+
*/
|
|
113
|
+
async compileSolCode(fileInput, fileName) {
|
|
114
|
+
try {
|
|
115
|
+
let formData;
|
|
116
|
+
let headers = {
|
|
117
|
+
apiKey: this.apiKey,
|
|
118
|
+
};
|
|
119
|
+
// Browser/React Native environment - use native FormData
|
|
120
|
+
if (!isNode ||
|
|
121
|
+
isReactNative ||
|
|
122
|
+
(typeof File !== "undefined" && fileInput instanceof File)) {
|
|
123
|
+
// React Native and browsers have FormData built-in
|
|
124
|
+
formData = new globalThis.FormData();
|
|
125
|
+
if (fileInput instanceof File || fileInput instanceof Blob) {
|
|
126
|
+
formData.append("solFile", fileInput, fileName || "contract.sol");
|
|
127
|
+
}
|
|
128
|
+
else if (fileInput instanceof Buffer) {
|
|
129
|
+
// Browser/React Native with Buffer
|
|
130
|
+
const blob = new Blob([fileInput]);
|
|
131
|
+
formData.append("solFile", blob, fileName || "contract.sol");
|
|
132
|
+
}
|
|
133
|
+
else if (typeof fileInput === "string" && isReactNative) {
|
|
134
|
+
// React Native file URI (file://)
|
|
135
|
+
formData.append("solFile", {
|
|
136
|
+
uri: fileInput,
|
|
137
|
+
type: "text/plain",
|
|
138
|
+
name: fileName || "contract.sol",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
else if (typeof fileInput === "string" && !isNode) {
|
|
142
|
+
// Browser string (treat as file path, but React Native handles URIs)
|
|
143
|
+
throw new Error("For React Native, use file URI (file://) or install react-native-fs");
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
throw new Error("Invalid file input for browser/React Native environment");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Node.js environment - use form-data package
|
|
150
|
+
else {
|
|
151
|
+
if (!FormDataNode || !fs) {
|
|
152
|
+
throw new Error("form-data package is required for Node.js. Install it: npm install form-data");
|
|
153
|
+
}
|
|
154
|
+
formData = new FormDataNode();
|
|
155
|
+
if (typeof fileInput === "string") {
|
|
156
|
+
// File path
|
|
157
|
+
formData.append("solFile", fs.createReadStream(fileInput));
|
|
158
|
+
}
|
|
159
|
+
else if (Buffer.isBuffer(fileInput)) {
|
|
160
|
+
// Buffer
|
|
161
|
+
formData.append("solFile", fileInput, {
|
|
162
|
+
filename: fileName || "contract.sol",
|
|
163
|
+
contentType: "text/plain",
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
throw new Error("Invalid file input for Node.js environment");
|
|
168
|
+
}
|
|
169
|
+
// Add form-data headers for Node.js
|
|
170
|
+
headers = {
|
|
171
|
+
...headers,
|
|
172
|
+
...formData.getHeaders(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const response = await this.client.post("/v2/wallet/compile-sol-code", formData, { headers });
|
|
176
|
+
return response.data;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw this.handleError(error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Deploy a smart contract
|
|
184
|
+
*/
|
|
185
|
+
async deployContract(request) {
|
|
186
|
+
try {
|
|
187
|
+
const response = await this.client.post("/v2/wallet/deploy-contract", request, {
|
|
188
|
+
headers: {
|
|
189
|
+
apiKey: this.apiKey,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
return response.data;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
throw this.handleError(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Execute a write transaction on a smart contract (gasless)
|
|
200
|
+
*/
|
|
201
|
+
async writeTransaction(request) {
|
|
202
|
+
try {
|
|
203
|
+
const response = await this.client.post("/relayer/write-transaction", request, {
|
|
204
|
+
headers: {
|
|
205
|
+
apiKey: this.apiKey,
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
return response.data;
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
throw this.handleError(error);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Send a transaction via relayer (gasless)
|
|
216
|
+
*/
|
|
217
|
+
async relayerSendTransaction(request) {
|
|
218
|
+
try {
|
|
219
|
+
const response = await this.client.post("/relayer/send-transaction", request, {
|
|
220
|
+
headers: {
|
|
221
|
+
apikey: this.apiKey,
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
return response.data;
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
throw this.handleError(error);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Handle API errors
|
|
232
|
+
*/
|
|
233
|
+
handleError(error) {
|
|
234
|
+
if (error.response) {
|
|
235
|
+
// Server responded with error status
|
|
236
|
+
const message = error.response.data?.message || error.response.statusText;
|
|
237
|
+
const status = error.response.status;
|
|
238
|
+
return new Error(`API Error (${status}): ${message}`);
|
|
239
|
+
}
|
|
240
|
+
else if (error.request) {
|
|
241
|
+
// Request was made but no response received
|
|
242
|
+
return new Error("Network Error: No response from server");
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
// Something else happened
|
|
246
|
+
return new Error(`Error: ${error.message}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
exports.TresoriSDK = TresoriSDK;
|