@eosrio/node-abieos 3.3.4-a9d3362 → 4.0.1-2039717
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 +8 -2
- package/dist/_tsup-dts-rollup.d.cts +105 -19
- package/dist/_tsup-dts-rollup.d.ts +105 -19
- package/dist/abieos.cjs +261 -122
- package/dist/abieos.js +230 -91
- package/dist/abieos.node +0 -0
- package/dist/abieos.ts +210 -42
- package/package.json +55 -54
- package/.cache/jb/UpdateWork.dat +0 -0
- package/.cache/jb/compilation_graph.txt +0 -2
- package/.cache/jb/module.modulemap +0 -0
- package/.cache/jb/modules_graph.txt +0 -3
- package/.cache/jb/version.txt +0 -1
- package/.gitmodules +0 -4
- package/lib/abieos.node +0 -0
- package/lib/abieos.ts +0 -100
- package/tsup.config.ts +0 -12
- package/update-version.mjs +0 -31
package/dist/abieos.js
CHANGED
|
@@ -1,91 +1,230 @@
|
|
|
1
|
-
// lib/abieos.ts
|
|
2
|
-
import { createRequire } from "module";
|
|
3
|
-
var abieos = createRequire(import.meta.url)("./abieos.node");
|
|
4
|
-
var Abieos = class _Abieos {
|
|
5
|
-
static
|
|
6
|
-
static
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
// lib/abieos.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
var abieos = createRequire(import.meta.url)("./abieos.node");
|
|
4
|
+
var Abieos = class _Abieos {
|
|
5
|
+
static logTag = "[node-abieos]";
|
|
6
|
+
static instance;
|
|
7
|
+
static native;
|
|
8
|
+
static loadedContracts = /* @__PURE__ */ new Map();
|
|
9
|
+
static debug = false;
|
|
10
|
+
/**
|
|
11
|
+
* Private constructor to enforce the Singleton pattern.
|
|
12
|
+
* Throws an error if an attempt is made to create a second instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor() {
|
|
15
|
+
if (_Abieos.instance) {
|
|
16
|
+
throw new Error(`${_Abieos.logTag} Abieos is a Singleton class. Use Abieos.getInstance() to get the instance.`);
|
|
17
|
+
}
|
|
18
|
+
_Abieos.native = abieos;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the singleton instance of the Abieos class.
|
|
22
|
+
* If an instance does not already exist, it creates one.
|
|
23
|
+
* @returns {Abieos} The singleton instance of Abieos.
|
|
24
|
+
*/
|
|
25
|
+
static getInstance() {
|
|
26
|
+
if (!_Abieos.instance) {
|
|
27
|
+
_Abieos.instance = new _Abieos();
|
|
28
|
+
}
|
|
29
|
+
return _Abieos.instance;
|
|
30
|
+
}
|
|
31
|
+
getLoadedAbis() {
|
|
32
|
+
return Array.from(_Abieos.loadedContracts.keys());
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Cleans up all loaded contracts by deleting them from the native context.
|
|
36
|
+
* This is useful for freeing up resources and ensuring a clean state.
|
|
37
|
+
*/
|
|
38
|
+
cleanup() {
|
|
39
|
+
const errors = [];
|
|
40
|
+
_Abieos.loadedContracts.forEach((_, contractName) => {
|
|
41
|
+
try {
|
|
42
|
+
if (_Abieos.debug) {
|
|
43
|
+
console.log(`${_Abieos.logTag} Cleaning up contract '${contractName}'...`);
|
|
44
|
+
}
|
|
45
|
+
_Abieos.native.delete_contract(contractName);
|
|
46
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
errors.push({ contractName, error: e.message });
|
|
49
|
+
console.error(`${_Abieos.logTag} Failed to delete contract '${contractName}' during cleanup: ${e.message}`);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
if (errors.length > 0) {
|
|
53
|
+
throw new Error(`${_Abieos.logTag} Errors during cleanup: ${JSON.stringify(errors)}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
58
|
+
* @param {string} nameString The string name to convert.
|
|
59
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
60
|
+
*/
|
|
61
|
+
stringToName(nameString) {
|
|
62
|
+
try {
|
|
63
|
+
return _Abieos.native.string_to_name(nameString);
|
|
64
|
+
} catch (e) {
|
|
65
|
+
throw new Error(`${_Abieos.logTag} Failed to convert string to name '${nameString}': ${e.message}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
70
|
+
* @param {string} contractName The name of the contract.
|
|
71
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
72
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
73
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
74
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
75
|
+
*/
|
|
76
|
+
jsonToHex(contractName, type, json) {
|
|
77
|
+
const jsonData = typeof json === "object" ? JSON.stringify(json) : json;
|
|
78
|
+
try {
|
|
79
|
+
return _Abieos.native.json_to_hex(contractName, type, jsonData);
|
|
80
|
+
} catch (e) {
|
|
81
|
+
throw new Error(`${_Abieos.logTag} Failed to convert JSON to hex for contract '${contractName}', type '${type}': ${e.message}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
86
|
+
* @param {string} contractName The name of the contract.
|
|
87
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
88
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
89
|
+
* @returns {any} The parsed JSON object.
|
|
90
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
91
|
+
*/
|
|
92
|
+
hexToJson(contractName, type, hex) {
|
|
93
|
+
try {
|
|
94
|
+
const data = _Abieos.native.hex_to_json(contractName, type, hex);
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(data);
|
|
97
|
+
} catch (parseError) {
|
|
98
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from hex for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
99
|
+
}
|
|
100
|
+
} catch (e) {
|
|
101
|
+
throw new Error(`${_Abieos.logTag} Native error when converting hex to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Converts a binary buffer to its JSON representation.
|
|
106
|
+
* @param {string} contractName The name of the contract.
|
|
107
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
108
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
109
|
+
* @returns {any} The parsed JSON object.
|
|
110
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
111
|
+
*/
|
|
112
|
+
binToJson(contractName, type, buffer) {
|
|
113
|
+
try {
|
|
114
|
+
const data = _Abieos.native.bin_to_json(contractName, type, buffer);
|
|
115
|
+
try {
|
|
116
|
+
return JSON.parse(data);
|
|
117
|
+
} catch (parseError) {
|
|
118
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from binary for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
119
|
+
}
|
|
120
|
+
} catch (e) {
|
|
121
|
+
throw new Error(`${_Abieos.logTag} Native error when converting binary to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Loads an ABI for a given contract.
|
|
126
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
127
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
128
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
129
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
130
|
+
*/
|
|
131
|
+
loadAbi(contractName, abi) {
|
|
132
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
133
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
134
|
+
}
|
|
135
|
+
const abiString = typeof abi === "object" ? JSON.stringify(abi) : abi;
|
|
136
|
+
if (typeof abiString !== "string") {
|
|
137
|
+
throw new Error(`${_Abieos.logTag} ABI must be a String or Object.`);
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const loaded = _Abieos.native.load_abi(contractName, abiString);
|
|
141
|
+
if (loaded) {
|
|
142
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
143
|
+
if (_Abieos.debug) {
|
|
144
|
+
console.log(`${_Abieos.logTag} Loaded ABI for contract '${contractName}'.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return loaded;
|
|
148
|
+
} catch (e) {
|
|
149
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI for contract '${contractName}': ${e.message}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
154
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
155
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
156
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
157
|
+
* @throws {Error} If loading fails.
|
|
158
|
+
*/
|
|
159
|
+
loadAbiHex(contractName, abihex) {
|
|
160
|
+
if (typeof abihex !== "string") {
|
|
161
|
+
throw new Error(`${_Abieos.logTag} ABI hex must be a String.`);
|
|
162
|
+
}
|
|
163
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
164
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
const loaded = _Abieos.native.load_abi_hex(contractName, abihex);
|
|
168
|
+
if (loaded) {
|
|
169
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
170
|
+
if (_Abieos.debug) {
|
|
171
|
+
console.log(`${_Abieos.logTag} Loaded ABI hex for contract '${contractName}'.`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return loaded;
|
|
175
|
+
} catch (e) {
|
|
176
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI hex for contract '${contractName}': ${e.message}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
181
|
+
* @param {string} contractName The name of the contract.
|
|
182
|
+
* @param {string} actionName The name of the action.
|
|
183
|
+
* @returns {string} The type name associated with the action.
|
|
184
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
185
|
+
*/
|
|
186
|
+
getTypeForAction(contractName, actionName) {
|
|
187
|
+
try {
|
|
188
|
+
return _Abieos.native.get_type_for_action(contractName, actionName);
|
|
189
|
+
} catch (e) {
|
|
190
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for action '${actionName}' in contract '${contractName}': ${e.message}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
195
|
+
* @param {string} contractName The name of the contract.
|
|
196
|
+
* @param {string} table_name The name of the table.
|
|
197
|
+
* @returns {string} The type name associated with the table.
|
|
198
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
199
|
+
*/
|
|
200
|
+
getTypeForTable(contractName, table_name) {
|
|
201
|
+
try {
|
|
202
|
+
return _Abieos.native.get_type_for_table(contractName, table_name);
|
|
203
|
+
} catch (e) {
|
|
204
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for table '${table_name}' in contract '${contractName}': ${e.message}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Deletes a contract's ABI from the abieos context.
|
|
209
|
+
* @param {string} contractName The name of the contract to delete.
|
|
210
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
211
|
+
* @throws {Error} If deletion fails.
|
|
212
|
+
*/
|
|
213
|
+
deleteContract(contractName) {
|
|
214
|
+
try {
|
|
215
|
+
const deleted = _Abieos.native.delete_contract(contractName);
|
|
216
|
+
if (deleted) {
|
|
217
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
218
|
+
if (_Abieos.debug) {
|
|
219
|
+
console.log(`${_Abieos.logTag} Deleted contract '${contractName}' from abieos context.`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return deleted;
|
|
223
|
+
} catch (e) {
|
|
224
|
+
throw new Error(`${_Abieos.logTag} Failed to delete contract '${contractName}': ${e.message}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
export {
|
|
229
|
+
Abieos
|
|
230
|
+
};
|
package/dist/abieos.node
CHANGED
|
Binary file
|
package/dist/abieos.ts
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
2
|
const abieos = createRequire(import.meta.url)('./abieos.node');
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Abieos class provides a singleton instance for interacting with the native abieos module.
|
|
6
|
+
* This pattern is used to ensure a single global context for the underlying C++ abieos library,
|
|
7
|
+
* which manages internal state and resources.
|
|
8
|
+
*/
|
|
4
9
|
export class Abieos {
|
|
5
|
-
|
|
10
|
+
|
|
11
|
+
public static logTag: string = '[node-abieos]';
|
|
6
12
|
private static instance: Abieos;
|
|
7
13
|
public static native: typeof abieos;
|
|
14
|
+
private static loadedContracts: Map<string, number> = new Map();
|
|
15
|
+
public static debug: boolean = false;
|
|
8
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Private constructor to enforce the Singleton pattern.
|
|
19
|
+
* Throws an error if an attempt is made to create a second instance.
|
|
20
|
+
*/
|
|
9
21
|
private constructor() {
|
|
10
22
|
if (Abieos.instance) {
|
|
11
|
-
throw new Error(
|
|
23
|
+
throw new Error(`${Abieos.logTag} Abieos is a Singleton class. Use Abieos.getInstance() to get the instance.`);
|
|
12
24
|
}
|
|
13
25
|
Abieos.native = abieos;
|
|
14
26
|
}
|
|
15
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Returns the singleton instance of the Abieos class.
|
|
30
|
+
* If an instance does not already exist, it creates one.
|
|
31
|
+
* @returns {Abieos} The singleton instance of Abieos.
|
|
32
|
+
*/
|
|
16
33
|
public static getInstance(): Abieos {
|
|
17
34
|
if (!Abieos.instance) {
|
|
18
35
|
Abieos.instance = new Abieos();
|
|
@@ -20,81 +37,232 @@ export class Abieos {
|
|
|
20
37
|
return Abieos.instance;
|
|
21
38
|
}
|
|
22
39
|
|
|
23
|
-
public
|
|
24
|
-
return Abieos.
|
|
40
|
+
public getLoadedAbis(): string[] {
|
|
41
|
+
return Array.from(Abieos.loadedContracts.keys());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Cleans up all loaded contracts by deleting them from the native context.
|
|
46
|
+
* This is useful for freeing up resources and ensuring a clean state.
|
|
47
|
+
*/
|
|
48
|
+
public cleanup(): void {
|
|
49
|
+
/* node:coverage disable */
|
|
50
|
+
const errors: any[] = [];
|
|
51
|
+
Abieos.loadedContracts.forEach((_, contractName) => {
|
|
52
|
+
try {
|
|
53
|
+
if (Abieos.debug) {
|
|
54
|
+
console.log(`${Abieos.logTag} Cleaning up contract '${contractName}'...`);
|
|
55
|
+
}
|
|
56
|
+
Abieos.native.delete_contract(contractName);
|
|
57
|
+
Abieos.loadedContracts.delete(contractName);
|
|
58
|
+
} catch (e: any) {
|
|
59
|
+
errors.push({ contractName, error: e.message });
|
|
60
|
+
console.error(`${Abieos.logTag} Failed to delete contract '${contractName}' during cleanup: ${e.message}`);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
if (errors.length > 0) {
|
|
64
|
+
throw new Error(`${Abieos.logTag} Errors during cleanup: ${JSON.stringify(errors)}`);
|
|
65
|
+
}
|
|
66
|
+
/* node:coverage enable */
|
|
25
67
|
}
|
|
26
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
71
|
+
* @param {string} nameString The string name to convert.
|
|
72
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
73
|
+
*/
|
|
74
|
+
public stringToName(nameString: string): BigInt {
|
|
75
|
+
// The native C++ function returns a JavaScript BigInt.
|
|
76
|
+
try {
|
|
77
|
+
return Abieos.native.string_to_name(nameString) as BigInt;
|
|
78
|
+
} catch (e: any) {
|
|
79
|
+
throw new Error(`${Abieos.logTag} Failed to convert string to name '${nameString}': ${e.message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
85
|
+
* @param {string} contractName The name of the contract.
|
|
86
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
87
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
88
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
89
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
90
|
+
*/
|
|
27
91
|
public jsonToHex(contractName: string, type: string, json: string | object): string {
|
|
28
|
-
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return data;
|
|
92
|
+
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json;
|
|
93
|
+
try {
|
|
94
|
+
return Abieos.native.json_to_hex(contractName, type, jsonData) as string;
|
|
95
|
+
} catch (e: any) {
|
|
96
|
+
throw new Error(`${Abieos.logTag} Failed to convert JSON to hex for contract '${contractName}', type '${type}': ${e.message}`);
|
|
34
97
|
}
|
|
35
98
|
}
|
|
36
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
102
|
+
* @param {string} contractName The name of the contract.
|
|
103
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
104
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
105
|
+
* @returns {any} The parsed JSON object.
|
|
106
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
107
|
+
*/
|
|
37
108
|
public hexToJson(contractName: string, type: string, hex: string): any {
|
|
38
|
-
|
|
39
|
-
|
|
109
|
+
try {
|
|
110
|
+
const data = Abieos.native.hex_to_json(contractName, type, hex) as string;
|
|
111
|
+
// Attempt to parse the string data as JSON.
|
|
112
|
+
// This is still necessary as the native module returns a string.
|
|
113
|
+
/* node:coverage disable */
|
|
40
114
|
try {
|
|
41
115
|
return JSON.parse(data);
|
|
42
|
-
} catch (
|
|
43
|
-
|
|
116
|
+
} catch (parseError: any) {
|
|
117
|
+
// If JSON.parse fails, it means the string returned by native module was not valid JSON.
|
|
118
|
+
// This could be an error message string from C++ if it didn't throw an N-API error,
|
|
119
|
+
// or just malformed JSON.
|
|
120
|
+
throw new Error(`${Abieos.logTag} Failed to parse JSON string from hex for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
44
121
|
}
|
|
45
|
-
|
|
46
|
-
|
|
122
|
+
/* node:coverage enable */
|
|
123
|
+
} catch (e: any) {
|
|
124
|
+
// This catches errors thrown by the N-API layer itself.
|
|
125
|
+
throw new Error(`${Abieos.logTag} Native error when converting hex to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
47
126
|
}
|
|
48
127
|
}
|
|
49
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Converts a binary buffer to its JSON representation.
|
|
131
|
+
* @param {string} contractName The name of the contract.
|
|
132
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
133
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
134
|
+
* @returns {any} The parsed JSON object.
|
|
135
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
136
|
+
*/
|
|
50
137
|
public binToJson(contractName: string, type: string, buffer: Buffer): any {
|
|
51
|
-
|
|
52
|
-
|
|
138
|
+
try {
|
|
139
|
+
const data = Abieos.native.bin_to_json(contractName, type, buffer) as string;
|
|
140
|
+
// Attempt to parse the string data as JSON.
|
|
141
|
+
/* node:coverage disable */
|
|
53
142
|
try {
|
|
54
143
|
return JSON.parse(data);
|
|
55
|
-
} catch (
|
|
56
|
-
throw new Error(
|
|
144
|
+
} catch (parseError: any) {
|
|
145
|
+
throw new Error(`${Abieos.logTag} Failed to parse JSON string from binary for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
57
146
|
}
|
|
58
|
-
|
|
59
|
-
|
|
147
|
+
/* node:coverage enable */
|
|
148
|
+
} catch (e: any) {
|
|
149
|
+
// This catches errors thrown by the N-API layer itself.
|
|
150
|
+
throw new Error(`${Abieos.logTag} Native error when converting binary to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
60
151
|
}
|
|
61
152
|
}
|
|
62
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Loads an ABI for a given contract.
|
|
156
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
157
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
158
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
159
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
160
|
+
*/
|
|
63
161
|
public loadAbi(contractName: string, abi: string | object): boolean {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
162
|
+
|
|
163
|
+
if (Abieos.debug && Abieos.loadedContracts.has(contractName)) {
|
|
164
|
+
console.info(`${Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const abiString = typeof abi === 'object' ? JSON.stringify(abi) : abi;
|
|
168
|
+
if (typeof abiString !== 'string') { // Should be caught by TS but good runtime check
|
|
169
|
+
throw new Error(`${Abieos.logTag} ABI must be a String or Object.`);
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
const loaded = Abieos.native.load_abi(contractName, abiString) as boolean;
|
|
173
|
+
if (loaded) {
|
|
174
|
+
Abieos.loadedContracts.set(contractName, Date.now());
|
|
175
|
+
if (Abieos.debug) {
|
|
176
|
+
console.log(`${Abieos.logTag} Loaded ABI for contract '${contractName}'.`);
|
|
177
|
+
}
|
|
71
178
|
}
|
|
179
|
+
return loaded;
|
|
180
|
+
} catch (e: any) {
|
|
181
|
+
throw new Error(`${Abieos.logTag} Failed to load ABI for contract '${contractName}': ${e.message}`);
|
|
72
182
|
}
|
|
73
183
|
}
|
|
74
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
187
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
188
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
189
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
190
|
+
* @throws {Error} If loading fails.
|
|
191
|
+
*/
|
|
75
192
|
public loadAbiHex(contractName: string, abihex: string): boolean {
|
|
76
|
-
|
|
193
|
+
|
|
194
|
+
if (typeof abihex !== 'string') {
|
|
195
|
+
throw new Error(`${Abieos.logTag} ABI hex must be a String.`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* node:coverage disable */
|
|
199
|
+
if (Abieos.debug && Abieos.loadedContracts.has(contractName)) {
|
|
200
|
+
console.info(`${Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
201
|
+
}
|
|
202
|
+
/* node:coverage enable */
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const loaded = Abieos.native.load_abi_hex(contractName, abihex);
|
|
206
|
+
if (loaded) {
|
|
207
|
+
Abieos.loadedContracts.set(contractName, Date.now());
|
|
208
|
+
if (Abieos.debug) {
|
|
209
|
+
console.log(`${Abieos.logTag} Loaded ABI hex for contract '${contractName}'.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return loaded;
|
|
213
|
+
} catch (e: any) {
|
|
214
|
+
throw new Error(`${Abieos.logTag} Failed to load ABI hex for contract '${contractName}': ${e.message}`);
|
|
215
|
+
}
|
|
77
216
|
}
|
|
78
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
220
|
+
* @param {string} contractName The name of the contract.
|
|
221
|
+
* @param {string} actionName The name of the action.
|
|
222
|
+
* @returns {string} The type name associated with the action.
|
|
223
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
224
|
+
*/
|
|
79
225
|
public getTypeForAction(contractName: string, actionName: string): string {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return result;
|
|
226
|
+
try {
|
|
227
|
+
return Abieos.native.get_type_for_action(contractName, actionName) as string;
|
|
228
|
+
} catch (e: any) {
|
|
229
|
+
throw new Error(`${Abieos.logTag} Failed to get type for action '${actionName}' in contract '${contractName}': ${e.message}`);
|
|
85
230
|
}
|
|
86
231
|
}
|
|
87
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
235
|
+
* @param {string} contractName The name of the contract.
|
|
236
|
+
* @param {string} table_name The name of the table.
|
|
237
|
+
* @returns {string} The type name associated with the table.
|
|
238
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
239
|
+
*/
|
|
88
240
|
public getTypeForTable(contractName: string, table_name: string): string {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return result;
|
|
241
|
+
try {
|
|
242
|
+
return Abieos.native.get_type_for_table(contractName, table_name) as string;
|
|
243
|
+
} catch (e: any) {
|
|
244
|
+
throw new Error(`${Abieos.logTag} Failed to get type for table '${table_name}' in contract '${contractName}': ${e.message}`);
|
|
94
245
|
}
|
|
95
246
|
}
|
|
96
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Deletes a contract's ABI from the abieos context.
|
|
250
|
+
* @param {string} contractName The name of the contract to delete.
|
|
251
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
252
|
+
* @throws {Error} If deletion fails.
|
|
253
|
+
*/
|
|
97
254
|
public deleteContract(contractName: string): boolean {
|
|
98
|
-
|
|
255
|
+
try {
|
|
256
|
+
const deleted = Abieos.native.delete_contract(contractName);
|
|
257
|
+
if (deleted) {
|
|
258
|
+
Abieos.loadedContracts.delete(contractName);
|
|
259
|
+
if (Abieos.debug) {
|
|
260
|
+
console.log(`${Abieos.logTag} Deleted contract '${contractName}' from abieos context.`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return deleted;
|
|
264
|
+
} catch (e: any) {
|
|
265
|
+
throw new Error(`${Abieos.logTag} Failed to delete contract '${contractName}': ${e.message}`);
|
|
266
|
+
}
|
|
99
267
|
}
|
|
100
|
-
}
|
|
268
|
+
}
|