Merge pull request #292 from caktux/develop

fix fromAscii, toAscii and toHex, fix wrong test assertions, add raw …
This commit is contained in:
caktux 2015-08-10 23:42:54 -04:00
commit 29d11b96de
4 changed files with 31 additions and 35 deletions

View File

@ -14,7 +14,7 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* @file utils.js * @file utils.js
* @author Marek Kotewicz <marek@ethdev.com> * @author Marek Kotewicz <marek@ethdev.com>
* @date 2015 * @date 2015
@ -22,13 +22,13 @@
/** /**
* Utils * Utils
* *
* @module utils * @module utils
*/ */
/** /**
* Utility functions * Utility functions
* *
* @class [utils] utils * @class [utils] utils
* @constructor * @constructor
*/ */
@ -90,7 +90,7 @@ var padRight = function (string, chars, sign) {
return string + (new Array(chars - string.length + 1).join(sign ? sign : "0")); return string + (new Array(chars - string.length + 1).join(sign ? sign : "0"));
}; };
/** /**
* Should be called to get utf8 from it's hex representation * Should be called to get utf8 from it's hex representation
* *
* @method toUtf8 * @method toUtf8
@ -111,8 +111,8 @@ var toUtf8 = function(hex) {
return utf8.decode(str); return utf8.decode(str);
}; };
/** /**
* Should be called to get ascii from it's hex representation * Should be called to get ascii from it's hex representation
* *
* @method toAscii * @method toAscii
@ -128,9 +128,6 @@ var toAscii = function(hex) {
} }
for (; i < l; i+=2) { for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16); var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code); str += String.fromCharCode(code);
} }
@ -138,7 +135,7 @@ var toAscii = function(hex) {
}; };
/** /**
* Shold be called to get hex representation (prefixed by 0x) of utf8 string * Shold be called to get hex representation (prefixed by 0x) of utf8 string
* *
* @method fromUtf8 * @method fromUtf8
* @param {String} string * @param {String} string
@ -157,7 +154,7 @@ var fromUtf8 = function(str) {
}; };
/** /**
* Shold be called to get hex representation (prefixed by 0x) of ascii string * Shold be called to get hex representation (prefixed by 0x) of ascii string
* *
* @method fromAscii * @method fromAscii
* @param {String} string * @param {String} string
@ -168,9 +165,6 @@ var fromAscii = function(str) {
var hex = ""; var hex = "";
for(var i = 0; i < str.length; i++) { for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i); var code = str.charCodeAt(i);
if (code === 0) {
break;
}
var n = code.toString(16); var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n; hex += n.length < 2 ? '0' + n : n;
} }
@ -196,13 +190,13 @@ var transformToFullName = function (json) {
/** /**
* Should be called to get display name of contract function * Should be called to get display name of contract function
* *
* @method extractDisplayName * @method extractDisplayName
* @param {String} name of function/event * @param {String} name of function/event
* @returns {String} display name for function/event eg. multiply(uint256) -> multiply * @returns {String} display name for function/event eg. multiply(uint256) -> multiply
*/ */
var extractDisplayName = function (name) { var extractDisplayName = function (name) {
var length = name.indexOf('('); var length = name.indexOf('(');
return length !== -1 ? name.substr(0, length) : name; return length !== -1 ? name.substr(0, length) : name;
}; };
@ -266,7 +260,7 @@ var toHex = function (val) {
else if(val.indexOf('0x') === 0) else if(val.indexOf('0x') === 0)
return val; return val;
else if (!isFinite(val)) else if (!isFinite(val))
return fromUtf8(val); return fromAscii(val);
} }
return fromDecimal(val); return fromDecimal(val);
@ -300,7 +294,7 @@ var getValueOfUnit = function (unit) {
* - -- microether szabo micro * - -- microether szabo micro
* - -- milliether finney milli * - -- milliether finney milli
* - ether -- -- * - ether -- --
* - kether einstein grand * - kether einstein grand
* - mether * - mether
* - gether * - gether
* - tether * - tether
@ -313,7 +307,7 @@ var getValueOfUnit = function (unit) {
var fromWei = function(number, unit) { var fromWei = function(number, unit) {
var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit)); var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit));
return isBigNumber(number) ? returnValue : returnValue.toString(10); return isBigNumber(number) ? returnValue : returnValue.toString(10);
}; };
/** /**
@ -322,12 +316,12 @@ var fromWei = function(number, unit) {
* Possible units are: * Possible units are:
* SI Short SI Full Effigy Other * SI Short SI Full Effigy Other
* - kwei femtoether ada * - kwei femtoether ada
* - mwei picoether babbage * - mwei picoether babbage
* - gwei nanoether shannon nano * - gwei nanoether shannon nano
* - -- microether szabo micro * - -- microether szabo micro
* - -- milliether finney milli * - -- milliether finney milli
* - ether -- -- * - ether -- --
* - kether einstein grand * - kether einstein grand
* - mether * - mether
* - gether * - gether
* - tether * - tether
@ -340,7 +334,7 @@ var fromWei = function(number, unit) {
var toWei = function(number, unit) { var toWei = function(number, unit) {
var returnValue = toBigNumber(number).times(getValueOfUnit(unit)); var returnValue = toBigNumber(number).times(getValueOfUnit(unit));
return isBigNumber(number) ? returnValue : returnValue.toString(10); return isBigNumber(number) ? returnValue : returnValue.toString(10);
}; };
/** /**
@ -359,7 +353,7 @@ var toBigNumber = function(number) {
if (isString(number) && (number.indexOf('0x') === 0 || number.indexOf('-0x') === 0)) { if (isString(number) && (number.indexOf('0x') === 0 || number.indexOf('-0x') === 0)) {
return new BigNumber(number.replace('0x',''), 16); return new BigNumber(number.replace('0x',''), 16);
} }
return new BigNumber(number.toString(10), 10); return new BigNumber(number.toString(10), 10);
}; };
@ -411,7 +405,7 @@ var toAddress = function (address) {
if (isStrictAddress(address)) { if (isStrictAddress(address)) {
return address; return address;
} }
if (/^[0-9a-f]{40}$/.test(address)) { if (/^[0-9a-f]{40}$/.test(address)) {
return '0x' + address; return '0x' + address;
} }
@ -425,7 +419,7 @@ var toAddress = function (address) {
* *
* @method isBigNumber * @method isBigNumber
* @param {Object} * @param {Object}
* @return {Boolean} * @return {Boolean}
*/ */
var isBigNumber = function (object) { var isBigNumber = function (object) {
return object instanceof BigNumber || return object instanceof BigNumber ||
@ -434,7 +428,7 @@ var isBigNumber = function (object) {
/** /**
* Returns true if object is string, otherwise false * Returns true if object is string, otherwise false
* *
* @method isString * @method isString
* @param {Object} * @param {Object}
* @return {Boolean} * @return {Boolean}
@ -485,12 +479,12 @@ var isBoolean = function (object) {
* @return {Boolean} * @return {Boolean}
*/ */
var isArray = function (object) { var isArray = function (object) {
return object instanceof Array; return object instanceof Array;
}; };
/** /**
* Returns true if given string is valid json object * Returns true if given string is valid json object
* *
* @method isJson * @method isJson
* @param {String} * @param {String}
* @return {Boolean} * @return {Boolean}
@ -531,4 +525,3 @@ module.exports = {
isArray: isArray, isArray: isArray,
isJson: isJson isJson: isJson
}; };

View File

@ -5,7 +5,9 @@ var assert = chai.assert;
var tests = [ var tests = [
{ value: 'myString', expected: '0x6d79537472696e67'}, { value: 'myString', expected: '0x6d79537472696e67'},
{ value: 'myString\x00', expected: '0x6d79537472696e67'}, { value: 'myString\x00', expected: '0x6d79537472696e6700'},
{ value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -17,4 +19,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });

View File

@ -5,7 +5,9 @@ var assert = chai.assert;
var tests = [ var tests = [
{ value: '0x6d79537472696e67', expected: 'myString'}, { value: '0x6d79537472696e67', expected: 'myString'},
{ value: '0x6d79537472696e6700', expected: 'myString'}, { value: '0x6d79537472696e6700', expected: 'myString\u0000'},
{ value: "0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c",
expected: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -17,4 +19,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });

View File

@ -30,7 +30,9 @@ var tests = [
{ value: 'myString', expected: '0x6d79537472696e67'}, { value: 'myString', expected: '0x6d79537472696e67'},
{ value: new BigNumber(15), expected: '0xf'}, { value: new BigNumber(15), expected: '0xf'},
{ value: true, expected: '0x1'}, { value: true, expected: '0x1'},
{ value: false, expected: '0x0'} { value: false, expected: '0x0'},
{ value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -42,4 +44,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });