solidity string support

This commit is contained in:
Marek Kotewicz
2015-01-06 18:29:38 +01:00
parent b6232cfdd1
commit 1d139f7a0b
4 changed files with 31 additions and 13 deletions

View File

@ -21,6 +21,11 @@
* @date 2014
*/
// TODO: is these line is supposed to be here?
if (process.env.NODE_ENV !== 'build') {
var web3 = require('./web3'); // jshint ignore:line
}
// TODO: make these be actually accurate instead of falling back onto JS's doubles.
var hexToDec = function (hex) {
return parseInt(hex, 16).toString();
@ -50,16 +55,20 @@ var padLeft = function (string, chars) {
};
var setupInputTypes = function () {
var prefixedType = function (prefix) {
// @param prefix is the string prefix of the type
// @param bitsInNumber is number of bits per number in type
var prefixedType = function (prefix, bitsInNumber) {
return function (type, value) {
var expected = prefix;
if (type.indexOf(expected) !== 0) {
return false;
}
var padding = parseInt(type.slice(expected.length)) / 8;
var padding = parseInt(type.slice(expected.length)) / 8 * bitsInNumber;
if (typeof value === "number")
value = value.toString(16);
else if (typeof value === "string")
value = web3.toHex(value);
else if (value.indexOf('0x') === 0)
value = value.substr(2);
else
@ -83,9 +92,10 @@ var setupInputTypes = function () {
};
return [
prefixedType('uint'),
prefixedType('int'),
prefixedType('hash'),
prefixedType('uint', 1),
prefixedType('int', 1),
prefixedType('hash', 1),
prefixedType('string', 8),
namedType('address', 20),
namedType('bool', 1, formatBool),
];
@ -118,14 +128,17 @@ var toAbiInput = function (json, methodName, params) {
};
var setupOutputTypes = function () {
var prefixedType = function (prefix) {
// @param prefix is the string prefix of the type
// @param bitsInNumber is number of bits per number in type
var prefixedType = function (prefix, bitsInNumber) {
return function (type) {
var expected = prefix;
if (type.indexOf(expected) !== 0) {
return -1;
}
var padding = parseInt(type.slice(expected.length)) / 8;
var padding = parseInt(type.slice(expected.length)) / 8 * bitsInNumber;
return padding * 2;
};
};
@ -148,10 +161,15 @@ var setupOutputTypes = function () {
return value === '1' ? true : false;
};
var formatString = function (value) {
return web3.toAscii(value);
};
return [
{ padding: prefixedType('uint'), format: formatInt },
{ padding: prefixedType('int'), format: formatInt },
{ padding: prefixedType('hash'), format: formatHash },
{ padding: prefixedType('uint', 1), format: formatInt },
{ padding: prefixedType('int', 1), format: formatInt },
{ padding: prefixedType('hash', 1), format: formatHash },
{ padding: prefixedType('string', 8), format: formatString },
{ padding: namedType('address', 20) },
{ padding: namedType('bool', 1), format: formatBool }
];