@eosrio/node-abieos 3.3.4-97af3fd → 4.0.0-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 +87 -1
- package/dist/_tsup-dts-rollup.d.ts +87 -1
- package/dist/abieos.cjs +172 -38
- package/dist/abieos.js +172 -38
- package/dist/abieos.node +0 -0
- package/dist/abieos.ts +205 -42
- package/package.json +19 -18
- 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 -3
- 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/README.md
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
Node.js native binding for [abieos](https://github.com/AntelopeIO/abieos), with some improvements:
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
9
|
+
- Internal loaded contract map
|
|
10
|
+
- deleteContract: to remove the loaded contract from memory (now in vanilla abieos too)
|
|
11
11
|
|
|
12
12
|
Made with ♥ by [EOS Rio](https://eosrio.io/)
|
|
13
13
|
|
|
@@ -57,3 +57,9 @@ npm install
|
|
|
57
57
|
npm run build:linux
|
|
58
58
|
npm run build
|
|
59
59
|
```
|
|
60
|
+
|
|
61
|
+
### Documentation
|
|
62
|
+
|
|
63
|
+
For detailed and user-friendly documentation, including installation, usage, API reference, error handling, debugging, and examples, please refer to the [documentation](docs/README.md).
|
|
64
|
+
|
|
65
|
+
For contribution guidelines and developer documentation, refer to the [contribution guidelines](docs/CONTRIBUTING.md).
|
|
@@ -1,16 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abieos class provides a singleton instance for interacting with the native abieos module.
|
|
3
|
+
* This pattern is used to ensure a single global context for the underlying C++ abieos library,
|
|
4
|
+
* which manages internal state and resources.
|
|
5
|
+
*/
|
|
1
6
|
export declare class Abieos {
|
|
7
|
+
static logTag: string;
|
|
2
8
|
private static instance;
|
|
3
9
|
static native: typeof abieos;
|
|
10
|
+
private static loadedContracts;
|
|
11
|
+
static debug: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Private constructor to enforce the Singleton pattern.
|
|
14
|
+
* Throws an error if an attempt is made to create a second instance.
|
|
15
|
+
*/
|
|
4
16
|
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* Returns the singleton instance of the Abieos class.
|
|
19
|
+
* If an instance does not already exist, it creates one.
|
|
20
|
+
* @returns {Abieos} The singleton instance of Abieos.
|
|
21
|
+
*/
|
|
5
22
|
static getInstance(): Abieos;
|
|
6
|
-
|
|
23
|
+
getLoadedAbis(): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Cleans up all loaded contracts by deleting them from the native context.
|
|
26
|
+
* This is useful for freeing up resources and ensuring a clean state.
|
|
27
|
+
*/
|
|
28
|
+
cleanup(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
31
|
+
* @param {string} nameString The string name to convert.
|
|
32
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
33
|
+
*/
|
|
34
|
+
stringToName(nameString: string): BigInt;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
37
|
+
* @param {string} contractName The name of the contract.
|
|
38
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
39
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
40
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
41
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
42
|
+
*/
|
|
7
43
|
jsonToHex(contractName: string, type: string, json: string | object): string;
|
|
44
|
+
/**
|
|
45
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
46
|
+
* @param {string} contractName The name of the contract.
|
|
47
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
48
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
49
|
+
* @returns {any} The parsed JSON object.
|
|
50
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
51
|
+
*/
|
|
8
52
|
hexToJson(contractName: string, type: string, hex: string): any;
|
|
53
|
+
/**
|
|
54
|
+
* Converts a binary buffer to its JSON representation.
|
|
55
|
+
* @param {string} contractName The name of the contract.
|
|
56
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
57
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
58
|
+
* @returns {any} The parsed JSON object.
|
|
59
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
60
|
+
*/
|
|
9
61
|
binToJson(contractName: string, type: string, buffer: Buffer): any;
|
|
62
|
+
/**
|
|
63
|
+
* Loads an ABI for a given contract.
|
|
64
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
65
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
66
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
67
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
68
|
+
*/
|
|
10
69
|
loadAbi(contractName: string, abi: string | object): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
72
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
73
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
74
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
75
|
+
* @throws {Error} If loading fails.
|
|
76
|
+
*/
|
|
11
77
|
loadAbiHex(contractName: string, abihex: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
80
|
+
* @param {string} contractName The name of the contract.
|
|
81
|
+
* @param {string} actionName The name of the action.
|
|
82
|
+
* @returns {string} The type name associated with the action.
|
|
83
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
84
|
+
*/
|
|
12
85
|
getTypeForAction(contractName: string, actionName: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
88
|
+
* @param {string} contractName The name of the contract.
|
|
89
|
+
* @param {string} table_name The name of the table.
|
|
90
|
+
* @returns {string} The type name associated with the table.
|
|
91
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
92
|
+
*/
|
|
13
93
|
getTypeForTable(contractName: string, table_name: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Deletes a contract's ABI from the abieos context.
|
|
96
|
+
* @param {string} contractName The name of the contract to delete.
|
|
97
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
98
|
+
* @throws {Error} If deletion fails.
|
|
99
|
+
*/
|
|
14
100
|
deleteContract(contractName: string): boolean;
|
|
15
101
|
}
|
|
16
102
|
|
|
@@ -1,16 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abieos class provides a singleton instance for interacting with the native abieos module.
|
|
3
|
+
* This pattern is used to ensure a single global context for the underlying C++ abieos library,
|
|
4
|
+
* which manages internal state and resources.
|
|
5
|
+
*/
|
|
1
6
|
export declare class Abieos {
|
|
7
|
+
static logTag: string;
|
|
2
8
|
private static instance;
|
|
3
9
|
static native: typeof abieos;
|
|
10
|
+
private static loadedContracts;
|
|
11
|
+
static debug: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Private constructor to enforce the Singleton pattern.
|
|
14
|
+
* Throws an error if an attempt is made to create a second instance.
|
|
15
|
+
*/
|
|
4
16
|
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* Returns the singleton instance of the Abieos class.
|
|
19
|
+
* If an instance does not already exist, it creates one.
|
|
20
|
+
* @returns {Abieos} The singleton instance of Abieos.
|
|
21
|
+
*/
|
|
5
22
|
static getInstance(): Abieos;
|
|
6
|
-
|
|
23
|
+
getLoadedAbis(): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Cleans up all loaded contracts by deleting them from the native context.
|
|
26
|
+
* This is useful for freeing up resources and ensuring a clean state.
|
|
27
|
+
*/
|
|
28
|
+
cleanup(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
31
|
+
* @param {string} nameString The string name to convert.
|
|
32
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
33
|
+
*/
|
|
34
|
+
stringToName(nameString: string): BigInt;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
37
|
+
* @param {string} contractName The name of the contract.
|
|
38
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
39
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
40
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
41
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
42
|
+
*/
|
|
7
43
|
jsonToHex(contractName: string, type: string, json: string | object): string;
|
|
44
|
+
/**
|
|
45
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
46
|
+
* @param {string} contractName The name of the contract.
|
|
47
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
48
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
49
|
+
* @returns {any} The parsed JSON object.
|
|
50
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
51
|
+
*/
|
|
8
52
|
hexToJson(contractName: string, type: string, hex: string): any;
|
|
53
|
+
/**
|
|
54
|
+
* Converts a binary buffer to its JSON representation.
|
|
55
|
+
* @param {string} contractName The name of the contract.
|
|
56
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
57
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
58
|
+
* @returns {any} The parsed JSON object.
|
|
59
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
60
|
+
*/
|
|
9
61
|
binToJson(contractName: string, type: string, buffer: Buffer): any;
|
|
62
|
+
/**
|
|
63
|
+
* Loads an ABI for a given contract.
|
|
64
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
65
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
66
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
67
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
68
|
+
*/
|
|
10
69
|
loadAbi(contractName: string, abi: string | object): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
72
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
73
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
74
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
75
|
+
* @throws {Error} If loading fails.
|
|
76
|
+
*/
|
|
11
77
|
loadAbiHex(contractName: string, abihex: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
80
|
+
* @param {string} contractName The name of the contract.
|
|
81
|
+
* @param {string} actionName The name of the action.
|
|
82
|
+
* @returns {string} The type name associated with the action.
|
|
83
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
84
|
+
*/
|
|
12
85
|
getTypeForAction(contractName: string, actionName: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
88
|
+
* @param {string} contractName The name of the contract.
|
|
89
|
+
* @param {string} table_name The name of the table.
|
|
90
|
+
* @returns {string} The type name associated with the table.
|
|
91
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
92
|
+
*/
|
|
13
93
|
getTypeForTable(contractName: string, table_name: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Deletes a contract's ABI from the abieos context.
|
|
96
|
+
* @param {string} contractName The name of the contract to delete.
|
|
97
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
98
|
+
* @throws {Error} If deletion fails.
|
|
99
|
+
*/
|
|
14
100
|
deleteContract(contractName: string): boolean;
|
|
15
101
|
}
|
|
16
102
|
|
package/dist/abieos.cjs
CHANGED
|
@@ -32,88 +32,222 @@ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
|
32
32
|
var import_module = require("module");
|
|
33
33
|
var abieos = (0, import_module.createRequire)(importMetaUrl)("./abieos.node");
|
|
34
34
|
var Abieos = class _Abieos {
|
|
35
|
+
static logTag = "[node-abieos]";
|
|
35
36
|
static instance;
|
|
36
37
|
static native;
|
|
38
|
+
static loadedContracts = /* @__PURE__ */ new Map();
|
|
39
|
+
static debug = false;
|
|
40
|
+
/**
|
|
41
|
+
* Private constructor to enforce the Singleton pattern.
|
|
42
|
+
* Throws an error if an attempt is made to create a second instance.
|
|
43
|
+
*/
|
|
37
44
|
constructor() {
|
|
38
45
|
if (_Abieos.instance) {
|
|
39
|
-
throw new Error(
|
|
46
|
+
throw new Error(`${_Abieos.logTag} Abieos is a Singleton class. Use Abieos.getInstance() to get the instance.`);
|
|
40
47
|
}
|
|
41
48
|
_Abieos.native = abieos;
|
|
42
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the singleton instance of the Abieos class.
|
|
52
|
+
* If an instance does not already exist, it creates one.
|
|
53
|
+
* @returns {Abieos} The singleton instance of Abieos.
|
|
54
|
+
*/
|
|
43
55
|
static getInstance() {
|
|
44
56
|
if (!_Abieos.instance) {
|
|
45
57
|
_Abieos.instance = new _Abieos();
|
|
46
58
|
}
|
|
47
59
|
return _Abieos.instance;
|
|
48
60
|
}
|
|
61
|
+
getLoadedAbis() {
|
|
62
|
+
return Array.from(_Abieos.loadedContracts.keys());
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Cleans up all loaded contracts by deleting them from the native context.
|
|
66
|
+
* This is useful for freeing up resources and ensuring a clean state.
|
|
67
|
+
*/
|
|
68
|
+
cleanup() {
|
|
69
|
+
_Abieos.loadedContracts.forEach((_, contractName) => {
|
|
70
|
+
try {
|
|
71
|
+
if (_Abieos.debug) {
|
|
72
|
+
console.log(`${_Abieos.logTag} Cleaning up contract '${contractName}'...`);
|
|
73
|
+
}
|
|
74
|
+
_Abieos.native.delete_contract(contractName);
|
|
75
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
76
|
+
} catch (e) {
|
|
77
|
+
console.error(`${_Abieos.logTag} Failed to delete contract '${contractName}' during cleanup: ${e.message}`);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
83
|
+
* @param {string} nameString The string name to convert.
|
|
84
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
85
|
+
*/
|
|
49
86
|
stringToName(nameString) {
|
|
50
|
-
|
|
87
|
+
try {
|
|
88
|
+
return _Abieos.native.string_to_name(nameString);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
throw new Error(`${_Abieos.logTag} Failed to convert string to name '${nameString}': ${e.message}`);
|
|
91
|
+
}
|
|
51
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
95
|
+
* @param {string} contractName The name of the contract.
|
|
96
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
97
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
98
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
99
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
100
|
+
*/
|
|
52
101
|
jsonToHex(contractName, type, json) {
|
|
53
102
|
const jsonData = typeof json === "object" ? JSON.stringify(json) : json;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return data;
|
|
103
|
+
try {
|
|
104
|
+
return _Abieos.native.json_to_hex(contractName, type, jsonData);
|
|
105
|
+
} catch (e) {
|
|
106
|
+
throw new Error(`${_Abieos.logTag} Failed to convert JSON to hex for contract '${contractName}', type '${type}': ${e.message}`);
|
|
59
107
|
}
|
|
60
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
111
|
+
* @param {string} contractName The name of the contract.
|
|
112
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
113
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
114
|
+
* @returns {any} The parsed JSON object.
|
|
115
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
116
|
+
*/
|
|
61
117
|
hexToJson(contractName, type, hex) {
|
|
62
|
-
|
|
63
|
-
|
|
118
|
+
try {
|
|
119
|
+
const data = _Abieos.native.hex_to_json(contractName, type, hex);
|
|
64
120
|
try {
|
|
65
121
|
return JSON.parse(data);
|
|
66
|
-
} catch (
|
|
67
|
-
throw new Error(
|
|
122
|
+
} catch (parseError) {
|
|
123
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from hex for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
68
124
|
}
|
|
69
|
-
}
|
|
70
|
-
throw new Error(
|
|
125
|
+
} catch (e) {
|
|
126
|
+
throw new Error(`${_Abieos.logTag} Native error when converting hex to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
71
127
|
}
|
|
72
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
|
+
*/
|
|
73
137
|
binToJson(contractName, type, buffer) {
|
|
74
|
-
|
|
75
|
-
|
|
138
|
+
try {
|
|
139
|
+
const data = _Abieos.native.bin_to_json(contractName, type, buffer);
|
|
76
140
|
try {
|
|
77
141
|
return JSON.parse(data);
|
|
78
|
-
} catch (
|
|
79
|
-
throw new Error(
|
|
142
|
+
} catch (parseError) {
|
|
143
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from binary for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
80
144
|
}
|
|
81
|
-
}
|
|
82
|
-
throw new Error(
|
|
145
|
+
} catch (e) {
|
|
146
|
+
throw new Error(`${_Abieos.logTag} Native error when converting binary to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
83
147
|
}
|
|
84
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Loads an ABI for a given contract.
|
|
151
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
152
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
153
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
154
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
155
|
+
*/
|
|
85
156
|
loadAbi(contractName, abi) {
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
157
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
158
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
159
|
+
}
|
|
160
|
+
const abiString = typeof abi === "object" ? JSON.stringify(abi) : abi;
|
|
161
|
+
if (typeof abiString !== "string") {
|
|
162
|
+
throw new Error(`${_Abieos.logTag} ABI must be a String or Object.`);
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const loaded = _Abieos.native.load_abi(contractName, abiString);
|
|
166
|
+
if (loaded) {
|
|
167
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
168
|
+
if (_Abieos.debug) {
|
|
169
|
+
console.log(`${_Abieos.logTag} Loaded ABI for contract '${contractName}'.`);
|
|
170
|
+
}
|
|
93
171
|
}
|
|
172
|
+
return loaded;
|
|
173
|
+
} catch (e) {
|
|
174
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI for contract '${contractName}': ${e.message}`);
|
|
94
175
|
}
|
|
95
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
179
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
180
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
181
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
182
|
+
* @throws {Error} If loading fails.
|
|
183
|
+
*/
|
|
96
184
|
loadAbiHex(contractName, abihex) {
|
|
97
|
-
|
|
185
|
+
if (typeof abihex !== "string") {
|
|
186
|
+
throw new Error(`${_Abieos.logTag} ABI hex must be a String.`);
|
|
187
|
+
}
|
|
188
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
189
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
const loaded = _Abieos.native.load_abi_hex(contractName, abihex);
|
|
193
|
+
if (loaded) {
|
|
194
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
195
|
+
if (_Abieos.debug) {
|
|
196
|
+
console.log(`${_Abieos.logTag} Loaded ABI hex for contract '${contractName}'.`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return loaded;
|
|
200
|
+
} catch (e) {
|
|
201
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI hex for contract '${contractName}': ${e.message}`);
|
|
202
|
+
}
|
|
98
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
206
|
+
* @param {string} contractName The name of the contract.
|
|
207
|
+
* @param {string} actionName The name of the action.
|
|
208
|
+
* @returns {string} The type name associated with the action.
|
|
209
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
210
|
+
*/
|
|
99
211
|
getTypeForAction(contractName, actionName) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return result;
|
|
212
|
+
try {
|
|
213
|
+
return _Abieos.native.get_type_for_action(contractName, actionName);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for action '${actionName}' in contract '${contractName}': ${e.message}`);
|
|
105
216
|
}
|
|
106
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
220
|
+
* @param {string} contractName The name of the contract.
|
|
221
|
+
* @param {string} table_name The name of the table.
|
|
222
|
+
* @returns {string} The type name associated with the table.
|
|
223
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
224
|
+
*/
|
|
107
225
|
getTypeForTable(contractName, table_name) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return result;
|
|
226
|
+
try {
|
|
227
|
+
return _Abieos.native.get_type_for_table(contractName, table_name);
|
|
228
|
+
} catch (e) {
|
|
229
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for table '${table_name}' in contract '${contractName}': ${e.message}`);
|
|
113
230
|
}
|
|
114
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Deletes a contract's ABI from the abieos context.
|
|
234
|
+
* @param {string} contractName The name of the contract to delete.
|
|
235
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
236
|
+
* @throws {Error} If deletion fails.
|
|
237
|
+
*/
|
|
115
238
|
deleteContract(contractName) {
|
|
116
|
-
|
|
239
|
+
try {
|
|
240
|
+
const deleted = _Abieos.native.delete_contract(contractName);
|
|
241
|
+
if (deleted) {
|
|
242
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
243
|
+
if (_Abieos.debug) {
|
|
244
|
+
console.log(`${_Abieos.logTag} Deleted contract '${contractName}' from abieos context.`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return deleted;
|
|
248
|
+
} catch (e) {
|
|
249
|
+
throw new Error(`${_Abieos.logTag} Failed to delete contract '${contractName}': ${e.message}`);
|
|
250
|
+
}
|
|
117
251
|
}
|
|
118
252
|
};
|
|
119
253
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/abieos.js
CHANGED
|
@@ -2,88 +2,222 @@
|
|
|
2
2
|
import { createRequire } from "module";
|
|
3
3
|
var abieos = createRequire(import.meta.url)("./abieos.node");
|
|
4
4
|
var Abieos = class _Abieos {
|
|
5
|
+
static logTag = "[node-abieos]";
|
|
5
6
|
static instance;
|
|
6
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
|
+
*/
|
|
7
14
|
constructor() {
|
|
8
15
|
if (_Abieos.instance) {
|
|
9
|
-
throw new Error(
|
|
16
|
+
throw new Error(`${_Abieos.logTag} Abieos is a Singleton class. Use Abieos.getInstance() to get the instance.`);
|
|
10
17
|
}
|
|
11
18
|
_Abieos.native = abieos;
|
|
12
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
|
+
*/
|
|
13
25
|
static getInstance() {
|
|
14
26
|
if (!_Abieos.instance) {
|
|
15
27
|
_Abieos.instance = new _Abieos();
|
|
16
28
|
}
|
|
17
29
|
return _Abieos.instance;
|
|
18
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
|
+
_Abieos.loadedContracts.forEach((_, contractName) => {
|
|
40
|
+
try {
|
|
41
|
+
if (_Abieos.debug) {
|
|
42
|
+
console.log(`${_Abieos.logTag} Cleaning up contract '${contractName}'...`);
|
|
43
|
+
}
|
|
44
|
+
_Abieos.native.delete_contract(contractName);
|
|
45
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.error(`${_Abieos.logTag} Failed to delete contract '${contractName}' during cleanup: ${e.message}`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
53
|
+
* @param {string} nameString The string name to convert.
|
|
54
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
55
|
+
*/
|
|
19
56
|
stringToName(nameString) {
|
|
20
|
-
|
|
57
|
+
try {
|
|
58
|
+
return _Abieos.native.string_to_name(nameString);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
throw new Error(`${_Abieos.logTag} Failed to convert string to name '${nameString}': ${e.message}`);
|
|
61
|
+
}
|
|
21
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
65
|
+
* @param {string} contractName The name of the contract.
|
|
66
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
67
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
68
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
69
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
70
|
+
*/
|
|
22
71
|
jsonToHex(contractName, type, json) {
|
|
23
72
|
const jsonData = typeof json === "object" ? JSON.stringify(json) : json;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return data;
|
|
73
|
+
try {
|
|
74
|
+
return _Abieos.native.json_to_hex(contractName, type, jsonData);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
throw new Error(`${_Abieos.logTag} Failed to convert JSON to hex for contract '${contractName}', type '${type}': ${e.message}`);
|
|
29
77
|
}
|
|
30
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
81
|
+
* @param {string} contractName The name of the contract.
|
|
82
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
83
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
84
|
+
* @returns {any} The parsed JSON object.
|
|
85
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
86
|
+
*/
|
|
31
87
|
hexToJson(contractName, type, hex) {
|
|
32
|
-
|
|
33
|
-
|
|
88
|
+
try {
|
|
89
|
+
const data = _Abieos.native.hex_to_json(contractName, type, hex);
|
|
34
90
|
try {
|
|
35
91
|
return JSON.parse(data);
|
|
36
|
-
} catch (
|
|
37
|
-
throw new Error(
|
|
92
|
+
} catch (parseError) {
|
|
93
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from hex for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
38
94
|
}
|
|
39
|
-
}
|
|
40
|
-
throw new Error(
|
|
95
|
+
} catch (e) {
|
|
96
|
+
throw new Error(`${_Abieos.logTag} Native error when converting hex to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
41
97
|
}
|
|
42
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Converts a binary buffer to its JSON representation.
|
|
101
|
+
* @param {string} contractName The name of the contract.
|
|
102
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
103
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
104
|
+
* @returns {any} The parsed JSON object.
|
|
105
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
106
|
+
*/
|
|
43
107
|
binToJson(contractName, type, buffer) {
|
|
44
|
-
|
|
45
|
-
|
|
108
|
+
try {
|
|
109
|
+
const data = _Abieos.native.bin_to_json(contractName, type, buffer);
|
|
46
110
|
try {
|
|
47
111
|
return JSON.parse(data);
|
|
48
|
-
} catch (
|
|
49
|
-
throw new Error(
|
|
112
|
+
} catch (parseError) {
|
|
113
|
+
throw new Error(`${_Abieos.logTag} Failed to parse JSON string from binary for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
50
114
|
}
|
|
51
|
-
}
|
|
52
|
-
throw new Error(
|
|
115
|
+
} catch (e) {
|
|
116
|
+
throw new Error(`${_Abieos.logTag} Native error when converting binary to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
53
117
|
}
|
|
54
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Loads an ABI for a given contract.
|
|
121
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
122
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
123
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
124
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
125
|
+
*/
|
|
55
126
|
loadAbi(contractName, abi) {
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
127
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
128
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
129
|
+
}
|
|
130
|
+
const abiString = typeof abi === "object" ? JSON.stringify(abi) : abi;
|
|
131
|
+
if (typeof abiString !== "string") {
|
|
132
|
+
throw new Error(`${_Abieos.logTag} ABI must be a String or Object.`);
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const loaded = _Abieos.native.load_abi(contractName, abiString);
|
|
136
|
+
if (loaded) {
|
|
137
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
138
|
+
if (_Abieos.debug) {
|
|
139
|
+
console.log(`${_Abieos.logTag} Loaded ABI for contract '${contractName}'.`);
|
|
140
|
+
}
|
|
63
141
|
}
|
|
142
|
+
return loaded;
|
|
143
|
+
} catch (e) {
|
|
144
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI for contract '${contractName}': ${e.message}`);
|
|
64
145
|
}
|
|
65
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
149
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
150
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
151
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
152
|
+
* @throws {Error} If loading fails.
|
|
153
|
+
*/
|
|
66
154
|
loadAbiHex(contractName, abihex) {
|
|
67
|
-
|
|
155
|
+
if (typeof abihex !== "string") {
|
|
156
|
+
throw new Error(`${_Abieos.logTag} ABI hex must be a String.`);
|
|
157
|
+
}
|
|
158
|
+
if (_Abieos.debug && _Abieos.loadedContracts.has(contractName)) {
|
|
159
|
+
console.info(`${_Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const loaded = _Abieos.native.load_abi_hex(contractName, abihex);
|
|
163
|
+
if (loaded) {
|
|
164
|
+
_Abieos.loadedContracts.set(contractName, Date.now());
|
|
165
|
+
if (_Abieos.debug) {
|
|
166
|
+
console.log(`${_Abieos.logTag} Loaded ABI hex for contract '${contractName}'.`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return loaded;
|
|
170
|
+
} catch (e) {
|
|
171
|
+
throw new Error(`${_Abieos.logTag} Failed to load ABI hex for contract '${contractName}': ${e.message}`);
|
|
172
|
+
}
|
|
68
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
176
|
+
* @param {string} contractName The name of the contract.
|
|
177
|
+
* @param {string} actionName The name of the action.
|
|
178
|
+
* @returns {string} The type name associated with the action.
|
|
179
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
180
|
+
*/
|
|
69
181
|
getTypeForAction(contractName, actionName) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return result;
|
|
182
|
+
try {
|
|
183
|
+
return _Abieos.native.get_type_for_action(contractName, actionName);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for action '${actionName}' in contract '${contractName}': ${e.message}`);
|
|
75
186
|
}
|
|
76
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
190
|
+
* @param {string} contractName The name of the contract.
|
|
191
|
+
* @param {string} table_name The name of the table.
|
|
192
|
+
* @returns {string} The type name associated with the table.
|
|
193
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
194
|
+
*/
|
|
77
195
|
getTypeForTable(contractName, table_name) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return result;
|
|
196
|
+
try {
|
|
197
|
+
return _Abieos.native.get_type_for_table(contractName, table_name);
|
|
198
|
+
} catch (e) {
|
|
199
|
+
throw new Error(`${_Abieos.logTag} Failed to get type for table '${table_name}' in contract '${contractName}': ${e.message}`);
|
|
83
200
|
}
|
|
84
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Deletes a contract's ABI from the abieos context.
|
|
204
|
+
* @param {string} contractName The name of the contract to delete.
|
|
205
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
206
|
+
* @throws {Error} If deletion fails.
|
|
207
|
+
*/
|
|
85
208
|
deleteContract(contractName) {
|
|
86
|
-
|
|
209
|
+
try {
|
|
210
|
+
const deleted = _Abieos.native.delete_contract(contractName);
|
|
211
|
+
if (deleted) {
|
|
212
|
+
_Abieos.loadedContracts.delete(contractName);
|
|
213
|
+
if (_Abieos.debug) {
|
|
214
|
+
console.log(`${_Abieos.logTag} Deleted contract '${contractName}' from abieos context.`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return deleted;
|
|
218
|
+
} catch (e) {
|
|
219
|
+
throw new Error(`${_Abieos.logTag} Failed to delete contract '${contractName}': ${e.message}`);
|
|
220
|
+
}
|
|
87
221
|
}
|
|
88
222
|
};
|
|
89
223
|
export {
|
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,227 @@ 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
|
+
Abieos.loadedContracts.forEach((_, contractName) => {
|
|
51
|
+
try {
|
|
52
|
+
if (Abieos.debug) {
|
|
53
|
+
console.log(`${Abieos.logTag} Cleaning up contract '${contractName}'...`);
|
|
54
|
+
}
|
|
55
|
+
Abieos.native.delete_contract(contractName);
|
|
56
|
+
Abieos.loadedContracts.delete(contractName);
|
|
57
|
+
} catch (e: any) {
|
|
58
|
+
console.error(`${Abieos.logTag} Failed to delete contract '${contractName}' during cleanup: ${e.message}`);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
/* node:coverage enable */
|
|
25
62
|
}
|
|
26
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Converts a string name to its corresponding 64-bit unsigned integer representation (BigInt).
|
|
66
|
+
* @param {string} nameString The string name to convert.
|
|
67
|
+
* @returns {BigInt} The BigInt representation of the name.
|
|
68
|
+
*/
|
|
69
|
+
public stringToName(nameString: string): BigInt {
|
|
70
|
+
// The native C++ function returns a JavaScript BigInt.
|
|
71
|
+
try {
|
|
72
|
+
return Abieos.native.string_to_name(nameString) as BigInt;
|
|
73
|
+
} catch (e: any) {
|
|
74
|
+
throw new Error(`${Abieos.logTag} Failed to convert string to name '${nameString}': ${e.message}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Converts a JSON string or object to its hexadecimal binary representation.
|
|
80
|
+
* @param {string} contractName The name of the contract.
|
|
81
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
82
|
+
* @param {string | object} json The JSON data as a string or object.
|
|
83
|
+
* @returns {string} The hexadecimal string representation of the binary data.
|
|
84
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
85
|
+
*/
|
|
27
86
|
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;
|
|
87
|
+
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json;
|
|
88
|
+
try {
|
|
89
|
+
return Abieos.native.json_to_hex(contractName, type, jsonData) as string;
|
|
90
|
+
} catch (e: any) {
|
|
91
|
+
throw new Error(`${Abieos.logTag} Failed to convert JSON to hex for contract '${contractName}', type '${type}': ${e.message}`);
|
|
34
92
|
}
|
|
35
93
|
}
|
|
36
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Converts a hexadecimal binary string to its JSON representation.
|
|
97
|
+
* @param {string} contractName The name of the contract.
|
|
98
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
99
|
+
* @param {string} hex The hexadecimal string to convert.
|
|
100
|
+
* @returns {any} The parsed JSON object.
|
|
101
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
102
|
+
*/
|
|
37
103
|
public hexToJson(contractName: string, type: string, hex: string): any {
|
|
38
|
-
|
|
39
|
-
|
|
104
|
+
try {
|
|
105
|
+
const data = Abieos.native.hex_to_json(contractName, type, hex) as string;
|
|
106
|
+
// Attempt to parse the string data as JSON.
|
|
107
|
+
// This is still necessary as the native module returns a string.
|
|
108
|
+
/* node:coverage disable */
|
|
40
109
|
try {
|
|
41
110
|
return JSON.parse(data);
|
|
42
|
-
} catch (
|
|
43
|
-
|
|
111
|
+
} catch (parseError: any) {
|
|
112
|
+
// If JSON.parse fails, it means the string returned by native module was not valid JSON.
|
|
113
|
+
// This could be an error message string from C++ if it didn't throw an N-API error,
|
|
114
|
+
// or just malformed JSON.
|
|
115
|
+
throw new Error(`${Abieos.logTag} Failed to parse JSON string from hex for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
44
116
|
}
|
|
45
|
-
|
|
46
|
-
|
|
117
|
+
/* node:coverage enable */
|
|
118
|
+
} catch (e: any) {
|
|
119
|
+
// This catches errors thrown by the N-API layer itself.
|
|
120
|
+
throw new Error(`${Abieos.logTag} Native error when converting hex to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
47
121
|
}
|
|
48
122
|
}
|
|
49
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Converts a binary buffer to its JSON representation.
|
|
126
|
+
* @param {string} contractName The name of the contract.
|
|
127
|
+
* @param {string} type The type within the ABI to use for conversion.
|
|
128
|
+
* @param {Buffer} buffer The binary data as a Buffer.
|
|
129
|
+
* @returns {any} The parsed JSON object.
|
|
130
|
+
* @throws {Error} If parsing fails or an error occurs in the native module.
|
|
131
|
+
*/
|
|
50
132
|
public binToJson(contractName: string, type: string, buffer: Buffer): any {
|
|
51
|
-
|
|
52
|
-
|
|
133
|
+
try {
|
|
134
|
+
const data = Abieos.native.bin_to_json(contractName, type, buffer) as string;
|
|
135
|
+
// Attempt to parse the string data as JSON.
|
|
136
|
+
/* node:coverage disable */
|
|
53
137
|
try {
|
|
54
138
|
return JSON.parse(data);
|
|
55
|
-
} catch (
|
|
56
|
-
throw new Error(
|
|
139
|
+
} catch (parseError: any) {
|
|
140
|
+
throw new Error(`${Abieos.logTag} Failed to parse JSON string from binary for contract '${contractName}', type '${type}'. Received: ${data}. Parse error: ${parseError.message}`);
|
|
57
141
|
}
|
|
58
|
-
|
|
59
|
-
|
|
142
|
+
/* node:coverage enable */
|
|
143
|
+
} catch (e: any) {
|
|
144
|
+
// This catches errors thrown by the N-API layer itself.
|
|
145
|
+
throw new Error(`${Abieos.logTag} Native error when converting binary to JSON for contract '${contractName}', type '${type}': ${e.message}`);
|
|
60
146
|
}
|
|
61
147
|
}
|
|
62
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Loads an ABI for a given contract.
|
|
151
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
152
|
+
* @param {string | object} abi The ABI as a JSON string or object.
|
|
153
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
154
|
+
* @throws {Error} If the ABI format is invalid or loading fails.
|
|
155
|
+
*/
|
|
63
156
|
public loadAbi(contractName: string, abi: string | object): boolean {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
157
|
+
|
|
158
|
+
if (Abieos.debug && Abieos.loadedContracts.has(contractName)) {
|
|
159
|
+
console.info(`${Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const abiString = typeof abi === 'object' ? JSON.stringify(abi) : abi;
|
|
163
|
+
if (typeof abiString !== 'string') { // Should be caught by TS but good runtime check
|
|
164
|
+
throw new Error(`${Abieos.logTag} ABI must be a String or Object.`);
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
const loaded = Abieos.native.load_abi(contractName, abiString) as boolean;
|
|
168
|
+
if (loaded) {
|
|
169
|
+
Abieos.loadedContracts.set(contractName, Date.now());
|
|
170
|
+
if (Abieos.debug) {
|
|
171
|
+
console.log(`${Abieos.logTag} Loaded ABI for contract '${contractName}'.`);
|
|
172
|
+
}
|
|
71
173
|
}
|
|
174
|
+
return loaded;
|
|
175
|
+
} catch (e: any) {
|
|
176
|
+
throw new Error(`${Abieos.logTag} Failed to load ABI for contract '${contractName}': ${e.message}`);
|
|
72
177
|
}
|
|
73
178
|
}
|
|
74
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Loads an ABI for a given contract from its hexadecimal representation.
|
|
182
|
+
* @param {string} contractName The name of the contract for which to load the ABI.
|
|
183
|
+
* @param {string} abihex The ABI as a hexadecimal string.
|
|
184
|
+
* @returns {boolean} True if the ABI was loaded successfully, false otherwise.
|
|
185
|
+
* @throws {Error} If loading fails.
|
|
186
|
+
*/
|
|
75
187
|
public loadAbiHex(contractName: string, abihex: string): boolean {
|
|
76
|
-
|
|
188
|
+
|
|
189
|
+
if (typeof abihex !== 'string') {
|
|
190
|
+
throw new Error(`${Abieos.logTag} ABI hex must be a String.`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* node:coverage disable */
|
|
194
|
+
if (Abieos.debug && Abieos.loadedContracts.has(contractName)) {
|
|
195
|
+
console.info(`${Abieos.logTag} Contract '${contractName}' is already loaded. Updating ABI...`);
|
|
196
|
+
}
|
|
197
|
+
/* node:coverage enable */
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const loaded = Abieos.native.load_abi_hex(contractName, abihex);
|
|
201
|
+
if (loaded) {
|
|
202
|
+
Abieos.loadedContracts.set(contractName, Date.now());
|
|
203
|
+
if (Abieos.debug) {
|
|
204
|
+
console.log(`${Abieos.logTag} Loaded ABI hex for contract '${contractName}'.`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return loaded;
|
|
208
|
+
} catch (e: any) {
|
|
209
|
+
throw new Error(`${Abieos.logTag} Failed to load ABI hex for contract '${contractName}': ${e.message}`);
|
|
210
|
+
}
|
|
77
211
|
}
|
|
78
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Retrieves the type name for a specific action within a contract's ABI.
|
|
215
|
+
* @param {string} contractName The name of the contract.
|
|
216
|
+
* @param {string} actionName The name of the action.
|
|
217
|
+
* @returns {string} The type name associated with the action.
|
|
218
|
+
* @throws {Error} If the contract or action is not found or another error occurs.
|
|
219
|
+
*/
|
|
79
220
|
public getTypeForAction(contractName: string, actionName: string): string {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return result;
|
|
221
|
+
try {
|
|
222
|
+
return Abieos.native.get_type_for_action(contractName, actionName) as string;
|
|
223
|
+
} catch (e: any) {
|
|
224
|
+
throw new Error(`${Abieos.logTag} Failed to get type for action '${actionName}' in contract '${contractName}': ${e.message}`);
|
|
85
225
|
}
|
|
86
226
|
}
|
|
87
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Retrieves the type name for a specific table within a contract's ABI.
|
|
230
|
+
* @param {string} contractName The name of the contract.
|
|
231
|
+
* @param {string} table_name The name of the table.
|
|
232
|
+
* @returns {string} The type name associated with the table.
|
|
233
|
+
* @throws {Error} If the contract or table is not found or another error occurs.
|
|
234
|
+
*/
|
|
88
235
|
public getTypeForTable(contractName: string, table_name: string): string {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return result;
|
|
236
|
+
try {
|
|
237
|
+
return Abieos.native.get_type_for_table(contractName, table_name) as string;
|
|
238
|
+
} catch (e: any) {
|
|
239
|
+
throw new Error(`${Abieos.logTag} Failed to get type for table '${table_name}' in contract '${contractName}': ${e.message}`);
|
|
94
240
|
}
|
|
95
241
|
}
|
|
96
242
|
|
|
243
|
+
/**
|
|
244
|
+
* Deletes a contract's ABI from the abieos context.
|
|
245
|
+
* @param {string} contractName The name of the contract to delete.
|
|
246
|
+
* @returns {boolean} True if the contract was successfully deleted, false otherwise.
|
|
247
|
+
* @throws {Error} If deletion fails.
|
|
248
|
+
*/
|
|
97
249
|
public deleteContract(contractName: string): boolean {
|
|
98
|
-
|
|
250
|
+
try {
|
|
251
|
+
const deleted = Abieos.native.delete_contract(contractName);
|
|
252
|
+
if (deleted) {
|
|
253
|
+
Abieos.loadedContracts.delete(contractName);
|
|
254
|
+
if (Abieos.debug) {
|
|
255
|
+
console.log(`${Abieos.logTag} Deleted contract '${contractName}' from abieos context.`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return deleted;
|
|
259
|
+
} catch (e: any) {
|
|
260
|
+
throw new Error(`${Abieos.logTag} Failed to delete contract '${contractName}': ${e.message}`);
|
|
261
|
+
}
|
|
99
262
|
}
|
|
100
|
-
}
|
|
263
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eosrio/node-abieos",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-2039717",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public",
|
|
6
|
+
"tag": "abieos"
|
|
7
|
+
},
|
|
4
8
|
"description": "Node Bindings for abieos: Binary <> JSON conversion using ABIs.",
|
|
5
9
|
"main": "./dist/abieos.js",
|
|
6
10
|
"types": "./dist/abieos.d.ts",
|
|
@@ -14,14 +18,16 @@
|
|
|
14
18
|
},
|
|
15
19
|
"scripts": {
|
|
16
20
|
"install": "node -e 'process.exit(0)'",
|
|
17
|
-
"
|
|
21
|
+
"build:linux": "cmake-js compile && node scripts/copy-module.mjs",
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"test": "node --test test/**/*.test.js",
|
|
24
|
+
"test:coverage": "node --test --experimental-test-coverage test/**/*.test.js",
|
|
25
|
+
"test:watch": "node --test --watch --experimental-test-coverage test/**/*.test.js",
|
|
18
26
|
"tsc:watch": "tsc --watch",
|
|
19
27
|
"clean": "cmake-js clean",
|
|
20
|
-
"build": "tsup",
|
|
21
|
-
"build:linux": "cmake-js compile --cc /usr/bin/clang-18 --cxx /usr/bin/clang++-18 && cp build/Release/node_abieos.node lib/abieos.node",
|
|
22
|
-
"build:linux:ci": "cmake-js compile && cp build/Release/node_abieos.node lib/abieos.node",
|
|
23
28
|
"build:win": "cmake-js",
|
|
24
|
-
"
|
|
29
|
+
"build:mac": "cmake-js",
|
|
30
|
+
"update_version": "node scripts/update-version.mjs"
|
|
25
31
|
},
|
|
26
32
|
"repository": {
|
|
27
33
|
"type": "git",
|
|
@@ -34,22 +40,17 @@
|
|
|
34
40
|
},
|
|
35
41
|
"homepage": "https://github.com/eosrio/node-abieos#readme",
|
|
36
42
|
"devDependencies": {
|
|
37
|
-
"@microsoft/api-extractor": "7.
|
|
43
|
+
"@microsoft/api-extractor": "7.52.8",
|
|
38
44
|
"@wharfkit/antelope": "^1.0.13",
|
|
39
|
-
"@types/node": "22.
|
|
40
|
-
"cmake-js": "7.3.
|
|
41
|
-
"node-addon-api": "8.3.
|
|
42
|
-
"tsup": "^8.
|
|
43
|
-
"typescript": "5.
|
|
45
|
+
"@types/node": "^22.15.21",
|
|
46
|
+
"cmake-js": "7.3.1",
|
|
47
|
+
"node-addon-api": "8.3.1",
|
|
48
|
+
"tsup": "^8.5.0",
|
|
49
|
+
"typescript": "5.8.3"
|
|
44
50
|
},
|
|
45
51
|
"binary": {
|
|
46
52
|
"napi_versions": [
|
|
47
|
-
|
|
53
|
+
10
|
|
48
54
|
]
|
|
49
|
-
},
|
|
50
|
-
"cmake-js": {
|
|
51
|
-
"runtime": "node",
|
|
52
|
-
"runtimeVersion": "22.0.0",
|
|
53
|
-
"arch": "x64"
|
|
54
55
|
}
|
|
55
56
|
}
|
package/.cache/jb/UpdateWork.dat
DELETED
|
Binary file
|
|
File without changes
|
package/.cache/jb/version.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(ssh://git@git.jetbrains.team/llvm/llvm-project.git f4157ca9dd49181f6d35eaf6d324ffa84a40f01b based on LLVM 31f1590e4fb324c43dc36199587c453e27b6f6fa revision)
|
package/.gitmodules
DELETED
package/lib/abieos.node
DELETED
|
Binary file
|
package/lib/abieos.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "module";
|
|
2
|
-
const abieos = createRequire(import.meta.url)('./abieos.node');
|
|
3
|
-
|
|
4
|
-
export class Abieos {
|
|
5
|
-
|
|
6
|
-
private static instance: Abieos;
|
|
7
|
-
public static native: typeof abieos;
|
|
8
|
-
|
|
9
|
-
private constructor() {
|
|
10
|
-
if (Abieos.instance) {
|
|
11
|
-
throw new Error("This class is a Singleton!");
|
|
12
|
-
}
|
|
13
|
-
Abieos.native = abieos;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public static getInstance(): Abieos {
|
|
17
|
-
if (!Abieos.instance) {
|
|
18
|
-
Abieos.instance = new Abieos();
|
|
19
|
-
}
|
|
20
|
-
return Abieos.instance;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public stringToName(nameString: string): BigInteger {
|
|
24
|
-
return Abieos.native.string_to_name(nameString) as BigInteger;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public jsonToHex(contractName: string, type: string, json: string | object): string {
|
|
28
|
-
const jsonData = typeof json === 'object' ? JSON.stringify(json) : json
|
|
29
|
-
const data: string = Abieos.native.json_to_hex(contractName, type, jsonData);
|
|
30
|
-
if (data === 'PARSING_ERROR') {
|
|
31
|
-
throw new Error('failed to parse data');
|
|
32
|
-
} else {
|
|
33
|
-
return data;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public hexToJson(contractName: string, type: string, hex: string): any {
|
|
38
|
-
const data = Abieos.native.hex_to_json(contractName, type, hex);
|
|
39
|
-
if (data) {
|
|
40
|
-
try {
|
|
41
|
-
return JSON.parse(data);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
throw new Error('failed to parse json string: ' + data);
|
|
44
|
-
}
|
|
45
|
-
} else {
|
|
46
|
-
throw new Error('failed to parse hex data');
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public binToJson(contractName: string, type: string, buffer: Buffer): any {
|
|
51
|
-
const data = Abieos.native.bin_to_json(contractName, type, buffer);
|
|
52
|
-
if (data[0] === '{' || data[0] === '[') {
|
|
53
|
-
try {
|
|
54
|
-
return JSON.parse(data);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
throw new Error('json parse error');
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
throw new Error(data);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public loadAbi(contractName: string, abi: string | object): boolean {
|
|
64
|
-
if (typeof abi === 'string') {
|
|
65
|
-
return Abieos.native.load_abi(contractName, abi) as boolean;
|
|
66
|
-
} else {
|
|
67
|
-
if (typeof abi === 'object') {
|
|
68
|
-
return Abieos.native.load_abi(contractName, JSON.stringify(abi)) as boolean;
|
|
69
|
-
} else {
|
|
70
|
-
throw new Error('ABI must be a String or Object');
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public loadAbiHex(contractName: string, abihex: string): boolean {
|
|
76
|
-
return Abieos.native.load_abi_hex(contractName, abihex) as boolean;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public getTypeForAction(contractName: string, actionName: string): string {
|
|
80
|
-
const result = Abieos.native.get_type_for_action(contractName, actionName);
|
|
81
|
-
if (result === 'NOT_FOUND') {
|
|
82
|
-
throw new Error(`[node-abieos] action ` + actionName + ' not found on contract ' + contractName);
|
|
83
|
-
} else {
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public getTypeForTable(contractName: string, table_name: string): string {
|
|
89
|
-
const result = Abieos.native.get_type_for_table(contractName, table_name);
|
|
90
|
-
if (result === 'NOT_FOUND') {
|
|
91
|
-
throw new Error(`[node-abieos] table ` + table_name + ' not found on contract ' + contractName);
|
|
92
|
-
} else {
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public deleteContract(contractName: string): boolean {
|
|
98
|
-
return Abieos.native.delete_contract(contractName) as boolean;
|
|
99
|
-
}
|
|
100
|
-
}
|
package/tsup.config.ts
DELETED
package/update-version.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import {promises} from "node:fs";
|
|
2
|
-
import {exec} from "node:child_process";
|
|
3
|
-
|
|
4
|
-
const oldPackageJson = await promises.readFile('package.json', 'utf8');
|
|
5
|
-
if (!oldPackageJson) {
|
|
6
|
-
console.error('Failed to read package.json');
|
|
7
|
-
process.exit(1);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const parsed = JSON.parse(oldPackageJson.toString());
|
|
11
|
-
const [version, hash] = parsed.version.split('-');
|
|
12
|
-
console.log(`Old version: ${version}`);
|
|
13
|
-
console.log(`Old hash: ${hash}`);
|
|
14
|
-
|
|
15
|
-
exec('git rev-parse --short HEAD', {
|
|
16
|
-
cwd: './abieos',
|
|
17
|
-
}, (error, stdout) => {
|
|
18
|
-
if (error) {
|
|
19
|
-
console.error('Failed to get git hash');
|
|
20
|
-
process.exit(1);
|
|
21
|
-
}
|
|
22
|
-
console.log(`New hash: ${stdout.trim()}`);
|
|
23
|
-
const newVersion = `${version}-${stdout.trim()}`;
|
|
24
|
-
console.log(`New version: ${newVersion}`);
|
|
25
|
-
parsed.version = newVersion;
|
|
26
|
-
const newPackageJson = JSON.stringify(parsed, null, 2);
|
|
27
|
-
promises.writeFile('package.json', newPackageJson).catch((err) => {
|
|
28
|
-
console.error('Failed to write package.json');
|
|
29
|
-
process.exit(1);
|
|
30
|
-
});
|
|
31
|
-
});
|