wative-core 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/src/utils.ts ADDED
@@ -0,0 +1,94 @@
1
+ import * as fs from "fs"
2
+ import * as path from "path"
3
+ import Web3 from 'web3';
4
+
5
+ import * as CryptoJS from "crypto-js";
6
+
7
+
8
+ export interface Result {
9
+ status: boolean;
10
+ output: any;
11
+ }
12
+
13
+ export const excutePromiseFunction = async (fn: Function, params: any): Promise<Result> => {
14
+ let result: Result;
15
+ try {
16
+ let gas_price = await fn(...params);
17
+ result = {
18
+ status: true,
19
+ output: gas_price
20
+ };
21
+ } catch (err: any) {
22
+ result = {
23
+ status: false,
24
+ output: err.toString()
25
+ }
26
+ }
27
+ return result;
28
+ }
29
+
30
+
31
+ export class EvmGasUtil {
32
+ private web3: any;
33
+
34
+ constructor(rpc_url: string) {
35
+ this.web3 = new Web3(rpc_url);
36
+ }
37
+
38
+ public async getEvmGasPrice(): Promise<Result> {
39
+ let result: Result = await excutePromiseFunction(this.web3.eth.getGasPrice, []);
40
+ return result;
41
+ }
42
+
43
+ public async getEvmGasLimit(tx_params: any, rpc_url: string): Promise<Result> {
44
+ let result: Result = await excutePromiseFunction(this.web3.eth.estimateGas, [tx_params, rpc_url]);
45
+ return result;
46
+ }
47
+ }
48
+
49
+ export const checkFileExistence = (file_path: string): boolean => {
50
+ file_path = file_path.trim()
51
+ return fs.existsSync(file_path);
52
+ }
53
+
54
+ export const checkAccountLabelsExistence = (setting_path: string, account_labels: string[]) => {
55
+ const account_labels_size = account_labels.length;
56
+
57
+ if (account_labels_size === 0) {
58
+ return true;
59
+ }
60
+
61
+ for (let i = 0; i < account_labels_size; i++) {
62
+ const account_labels_path = path.join(setting_path, `accounts/${account_labels[i]}.json`);
63
+ if (!checkFileExistence(account_labels_path)) {
64
+ return false;
65
+ }
66
+ }
67
+ return true;
68
+ }
69
+
70
+ export const getJsonParams = (jsonFilePath: string) => {
71
+ const jsonString = fs.readFileSync(jsonFilePath).toString();
72
+ const data = JSON.parse(jsonString);
73
+ return data;
74
+ }
75
+
76
+ export const setJsonParams = (data: any, jsonFilePath: string) => {
77
+ const jsonString = JSON.stringify(data);
78
+ fs.writeFileSync(jsonFilePath, jsonString);
79
+ }
80
+
81
+ export const encryptMessage = (message: string, password: string) => {
82
+ const ciphertext = CryptoJS.AES.encrypt(message, password).toString();
83
+ return ciphertext;
84
+ }
85
+
86
+ export const decryptMessage = (ciphertext: string, password: string) => {
87
+ const bytes = CryptoJS.AES.decrypt(ciphertext, password);
88
+ const originalText = bytes.toString(CryptoJS.enc.Utf8);
89
+ return originalText;
90
+ }
91
+
92
+ export const hexToUint8Array = (hex_string: string) => {
93
+ return new Uint8Array(hex_string.match(/.{1,2}/g)!.map((val) => parseInt(val, 16)));
94
+ }