Extend api.util (#4979)
* cleanupValue * abiUnencode & abiSignature * Export new functions
This commit is contained in:
@@ -20,6 +20,38 @@ export function bytesToHex (bytes) {
|
||||
return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join('');
|
||||
}
|
||||
|
||||
export function cleanupValue (value, type) {
|
||||
// TODO: make work with arbitrary depth arrays
|
||||
if (value instanceof Array && type.match(/bytes[0-9]+/)) {
|
||||
// figure out if it's an ASCII string hiding in there:
|
||||
let ascii = '';
|
||||
|
||||
for (let index = 0, ended = false; index < value.length && ascii !== null; ++index) {
|
||||
const val = value[index];
|
||||
|
||||
if (val === 0) {
|
||||
ended = true;
|
||||
} else {
|
||||
ascii += String.fromCharCode(val);
|
||||
}
|
||||
|
||||
if ((ended && val !== 0) || (!ended && (val < 32 || val >= 128))) {
|
||||
ascii = null;
|
||||
}
|
||||
}
|
||||
|
||||
value = ascii === null
|
||||
? bytesToHex(value)
|
||||
: ascii;
|
||||
}
|
||||
|
||||
if (type.substr(0, 4) === 'uint' && +type.substr(4) <= 48) {
|
||||
value = +value;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function hexToBytes (hex) {
|
||||
const raw = toHex(hex).slice(2);
|
||||
const bytes = [];
|
||||
|
||||
Reference in New Issue
Block a user