Extend api.util (#4979)

* cleanupValue

* abiUnencode & abiSignature

* Export new functions
This commit is contained in:
Jaco Greeff
2017-03-22 10:36:41 +01:00
committed by Gav Wood
parent 7e87e9e8ad
commit e1f2ccd138
5 changed files with 120 additions and 5 deletions

View File

@@ -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 = [];