-
Notifications
You must be signed in to change notification settings - Fork 1
/
typeConversions.js
146 lines (135 loc) · 4.71 KB
/
typeConversions.js
1
2
3
4
5
6
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const typeConversionMap = {
uint: convertUnsignedInteger,
int: convertSignedInteger,
bool: convertBool,
address: convertAddress,
bytesFixed: convertBytesFixed,
string: convertString,
bytesDynamic: convertBytesDynamic,
array: convertToArray,
struct: convertToStruct,
enum: convertToEnum
}
/**
*
* @param {BigInt} unsignedInteger
* @returns {BigInt | }
*/
function convertUnsignedInteger(unsignedInteger) {
if (unsignedInteger < 0) {
throw Error("Value must be NonNegative.")
}
if (unsignedInteger < Number.MAX_SAFE_INTEGER) {
return Number(unsignedInteger);
}
if(unsignedInteger >= BigInt(2**256)){
throw Error("Value is greater than 2**256 - 1")
}
return unsignedInteger;
}
/**
* Converts a signed integer from Solidity to JavaScript.
* @param {BigInt} signedInteger The signed integer to convert.
* @returns {number | BigInt} The converted signed integer.
*/
function convertSignedInteger(signedInteger) {
if (signedInteger >= BigInt((-1)*2**255) && signedInteger < BigInt(2**255)) {
return (signedInteger >= BigInt(-Number.MAX_SAFE_INTEGER) && signedInteger <= Number.MAX_SAFE_INTEGER)
? Number(signedInteger)
: signedInteger;
}
throw new Error("Value is out of the int256 range");
}
/**
* Converts a Solidity boolean to a JavaScript boolean.
* @param {boolean} boolValue The boolean value to convert.
* @returns {boolean} The converted boolean value.
*/
function convertBool(boolValue) {
return Boolean(boolValue);
}
/**
* Converts a Solidity address to a JavaScript string.
* @param {string} address The address to convert.
* @returns {string} The converted address as a string.
*/
function convertAddress(address) {
// Assuming the address is already in a valid hexadecimal string format
return address;
}
/**
* Converts a fixed-length bytes array from Solidity to a JavaScript string or Uint8Array.
* @param {string} bytesFixed The fixed-length bytes array to convert.
* @returns {string | Uint8Array} The converted bytes array.
*/
function convertBytesFixed(bytesFixed) {
// Assuming the input is a hexadecimal string
return bytesFixed.startsWith('0x') ? bytesFixed : new Uint8Array(Buffer.from(bytesFixed, 'hex'));
}
/**
* Converts a Solidity string to a JavaScript string.
* @param {string} str The string to convert.
* @returns {string} The converted string.
*/
function convertString(str) {
return str;
}
/**
* Converts a dynamically-sized bytes array from Solidity to a JavaScript string or Uint8Array.
* @param {string} bytesDynamic The dynamically-sized bytes array to convert.
* @returns {string | Uint8Array} The converted bytes array.
*/
function convertBytesDynamic(bytesDynamic) {
// Assuming the input is a hexadecimal string
return bytesDynamic.startsWith('0x') ? bytesDynamic : new Uint8Array(Buffer.from(bytesDynamic, 'hex'));
}
/**
* Converts an array, recursively applying the converter function to each element.
* @param {Array} array The array to convert.
* @param {Function} converter The function to convert array elements.
* @returns {Array} The converted array.
*/
function convertToArray(array, converter) {
return array.map(element => {
if (Array.isArray(element)) {
// Recursive call for nested arrays
return convertToArray(element, converter);
} else if (typeof element === 'object' && element !== null) {
// Recursive call for nested objects (like structs)
return converter(element);
} else {
return converter(element);
}
});
}
/**
* Recursively converts a Solidity struct to a JavaScript object.
* @param {Object} struct The struct to convert.
* @param {Object} structConversionMap A mapping of struct fields to their conversion functions.
* @returns {Object} The converted object.
*/
function convertToStruct(struct, structConversionMap) {
let convertedStruct = {};
for (let key in struct) {
if (struct.hasOwnProperty(key)) {
// Apply appropriate conversion based on expected type
const converter = structConversionMap[key];
if (typeof converter === 'function') {
convertedStruct[key] = converter(struct[key]);
} else {
// If no specific converter, default to direct assignment
convertedStruct[key] = struct[key];
}
}
}
return convertedStruct;
}
/**
* Converts a Solidity enum to a JavaScript representation.
* @param {number} enumValue The enum value to convert.
* @returns {number} The converted enum value.
*/
function convertToEnum(enumValue) {
// Assuming enums are represented as integers
return enumValue;
}