Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+2420
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -0,0 +1 @@
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("ethUtil",[],r):"object"==typeof exports?exports.ethUtil=r():t.ethUtil=r()}(this,function(){return function(t){function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}var e={};return r.m=t,r.c=e,r.i=function(t){return t},r.d=function(t,r,e){Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:e})},r.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},r.p="",r(r.s=3)}([function(t,r,e){"use strict";(function(t,n){function i(){try{var t=new Uint8Array(1);return t.__proto__={__proto__:Uint8Array.prototype,foo:function(){return 42}},42===t.foo()&&"function"==typeof t.subarray&&0===t.subarray(1,1).byteLength}catch(r){return!1}}function o(){return t.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function f(r,e){if(o()<e)throw new RangeError("Invalid typed array length");return t.TYPED_ARRAY_SUPPORT?(r=new Uint8Array(e),r.__proto__=t.prototype):(null===r&&(r=new t(e)),r.length=e),r}function t(r,e,n){if(!(t.TYPED_ARRAY_SUPPORT||this instanceof t))return new t(r,e,n);if("number"==typeof r){if("string"==typeof e)throw Error("If encoding is specified then the first argument must be a string");return h(this,r)}return u(this,r,e,n)}function u(t,r,e,n){if("number"==typeof r)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&r instanceof ArrayBuffer?l(t,r,e,n):"string"==typeof r?c(t,r,e):g(t,r)}function s(t){if("number"!=typeof t)throw new TypeError('"size" argument must be a number');if(0>t)throw new RangeError('"size" argument must not be negative')}function a(t,r,e,n){return s(r),r>0&&void 0!==e?"string"==typeof n?f(t,r).fill(e,n):f(t,r).fill(e):f(t,r)}function h(r,e){if(s(e),r=f(r,0>e?0:0|y(e)),!t.TYPED_ARRAY_SUPPORT)for(var n=0;e>n;++n)r[n]=0;return r}function c(r,e,n){if("string"==typeof n&&""!==n||(n="utf8"),!t.isEncoding(n))throw new TypeError('"encoding" must be a valid string encoding');var i=0|d(e,n);r=f(r,i);var o=r.write(e,n);return o!==i&&(r=r.slice(0,o)),r}function p(t,r){var e=0>r.length?0:0|y(r.length);t=f(t,e);for(var n=0;e>n;n+=1)t[n]=255&r[n];return t}function l(r,e,n,i){if(0>n||n>e.byteLength)throw new RangeError("'offset' is out of bounds");if(n+(i||0)>e.byteLength)throw new RangeError("'length' is out of bounds");return e=void 0===n&&void 0===i?new Uint8Array(e):void 0===i?new Uint8Array(e,n):new Uint8Array(e,n,i),t.TYPED_ARRAY_SUPPORT?(r=e,r.__proto__=t.prototype):r=p(r,e),r}function g(r,e){if(t.isBuffer(e)){var n=0|y(e.length);return r=f(r,n),0===r.length?r:(e.copy(r,0,0,n),r)}if(e){if("undefined"!=typeof ArrayBuffer&&e.buffer instanceof ArrayBuffer||"length"in e)return"number"!=typeof e.length||Z(e.length)?f(r,0):p(r,e);if("Buffer"===e.type&&W(e.data))return p(r,e.data)}throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}function y(t){if(t>=o())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+o().toString(16)+" bytes");return 0|t}function w(r){return+r!=r&&(r=0),t.alloc(+r)}function d(r,e){if(t.isBuffer(r))return r.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(r)||r instanceof ArrayBuffer))return r.byteLength;"string"!=typeof r&&(r=""+r);var n=r.length;if(0===n)return 0;for(var i=!1;;)switch(e){case"ascii":case"latin1":case"binary":return n;case"utf8":case"utf-8":case void 0:return K(r).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*n;case"hex":return n>>>1;case"base64":return X(r).length;default:if(i)return K(r).length;e=(""+e).toLowerCase(),i=!0}}function v(t,r,e){var n=!1;if((void 0===r||0>r)&&(r=0),r>this.length)return"";if((void 0===e||e>this.length)&&(e=this.length),0>=e)return"";if(e>>>=0,r>>>=0,r>=e)return"";for(t||(t="utf8");;)switch(t){case"hex":return C(this,r,e);case"utf8":case"utf-8":return S(this,r,e);case"ascii":return Y(this,r,e);case"latin1":case"binary":return I(this,r,e);case"base64":return U(this,r,e);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return O(this,r,e);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}function A(t,r,e){var n=t[r];t[r]=t[e],t[e]=n}function E(r,e,n,i,o){if(0===r.length)return-1;if("string"==typeof n?(i=n,n=0):n>2147483647?n=2147483647:-2147483648>n&&(n=-2147483648),n=+n,isNaN(n)&&(n=o?0:r.length-1),0>n&&(n=r.length+n),r.length>n){if(0>n){if(!o)return-1;n=0}}else{if(o)return-1;n=r.length-1}if("string"==typeof e&&(e=t.from(e,i)),t.isBuffer(e))return 0===e.length?-1:b(r,e,n,i,o);if("number"==typeof e)return e=255&e,t.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(r,e,n):Uint8Array.prototype.lastIndexOf.call(r,e,n):b(r,[e],n,i,o);throw new TypeError("val must be string, number or Buffer")}function b(t,r,e,n,i){function o(t,r){retLine truncated
+58
View File
@@ -0,0 +1,58 @@
var webpack = require('webpack'); // eslint-disable-line
var env = process.env.NODE_ENV; // eslint-disable-line
var filename = 'ethjs-util'; // eslint-disable-line
var library = 'ethUtil'; // eslint-disable-line
var config = { // eslint-disable-line
module: {
loaders: [
{
test: /\.js$/,
loaders: ['babel-loader'],
exclude: /node_modules/,
},
{
test: /\.json$/,
loader: 'json',
},
],
},
devtool: 'cheap-module-source-map',
output: {
path: 'dist',
filename: filename + '.js', // eslint-disable-line
library: library, // eslint-disable-line
libraryTarget: 'umd',
umdNamedDefine: true,
},
plugins: [
new webpack.BannerPlugin({ banner: ' /* eslint-disable */ ', raw: true, entryOnly: true }),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(env),
}),
],
};
if (env === 'production') {
config.output.filename = filename + '.min.js'; // eslint-disable-line
config.plugins
.push(new webpack.optimize.UglifyJsPlugin({
compressor: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
screw_ie8: false,
},
mangle: {
screw_ie8: false,
},
output: {
screw_ie8: false,
},
}));
config.plugins.push(new webpack.optimize.DedupePlugin());
}
module.exports = config;
+220
View File
@@ -0,0 +1,220 @@
'use strict';
var isHexPrefixed = require('is-hex-prefixed');
var stripHexPrefix = require('strip-hex-prefix');
/**
* Pads a `String` to have an even length
* @param {String} value
* @return {String} output
*/
function padToEven(value) {
var a = value; // eslint-disable-line
if (typeof a !== 'string') {
throw new Error('[ethjs-util] while padding to even, value must be string, is currently ' + typeof a + ', while padToEven.');
}
if (a.length % 2) {
a = '0' + a;
}
return a;
}
/**
* Converts a `Number` into a hex `String`
* @param {Number} i
* @return {String}
*/
function intToHex(i) {
var hex = i.toString(16); // eslint-disable-line
return '0x' + hex;
}
/**
* Converts an `Number` to a `Buffer`
* @param {Number} i
* @return {Buffer}
*/
function intToBuffer(i) {
var hex = intToHex(i);
return new Buffer(padToEven(hex.slice(2)), 'hex');
}
/**
* Get the binary size of a string
* @param {String} str
* @return {Number}
*/
function getBinarySize(str) {
if (typeof str !== 'string') {
throw new Error('[ethjs-util] while getting binary size, method getBinarySize requires input \'str\' to be type String, got \'' + typeof str + '\'.');
}
return Buffer.byteLength(str, 'utf8');
}
/**
* Returns TRUE if the first specified array contains all elements
* from the second one. FALSE otherwise.
*
* @param {array} superset
* @param {array} subset
*
* @returns {boolean}
*/
function arrayContainsArray(superset, subset, some) {
if (Array.isArray(superset) !== true) {
throw new Error('[ethjs-util] method arrayContainsArray requires input \'superset\' to be an array got type \'' + typeof superset + '\'');
}
if (Array.isArray(subset) !== true) {
throw new Error('[ethjs-util] method arrayContainsArray requires input \'subset\' to be an array got type \'' + typeof subset + '\'');
}
return subset[Boolean(some) && 'some' || 'every'](function (value) {
return superset.indexOf(value) >= 0;
});
}
/**
* Should be called to get utf8 from it's hex representation
*
* @method toUtf8
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toUtf8(hex) {
var bufferValue = new Buffer(padToEven(stripHexPrefix(hex).replace(/^0+|0+$/g, '')), 'hex');
return bufferValue.toString('utf8');
}
/**
* Should be called to get ascii from it's hex representation
*
* @method toAscii
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toAscii(hex) {
var str = ''; // eslint-disable-line
var i = 0,
l = hex.length; // eslint-disable-line
if (hex.substring(0, 2) === '0x') {
i = 2;
}
for (; i < l; i += 2) {
var code = parseInt(hex.substr(i, 2), 16);
str += String.fromCharCode(code);
}
return str;
}
/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
*
* @method fromUtf8
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromUtf8(stringValue) {
var str = new Buffer(stringValue, 'utf8');
return '0x' + padToEven(str.toString('hex')).replace(/^0+|0+$/g, '');
}
/**
* Should be called to get hex representation (prefixed by 0x) of ascii string
*
* @method fromAscii
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromAscii(stringValue) {
var hex = ''; // eslint-disable-line
for (var i = 0; i < stringValue.length; i++) {
// eslint-disable-line
var code = stringValue.charCodeAt(i);
var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
}
return '0x' + hex;
}
/**
* getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3]
*
* @method getKeys get specific key from inner object array of objects
* @param {String} params
* @param {String} key
* @param {Boolean} allowEmpty
* @returns {Array} output just a simple array of output keys
*/
function getKeys(params, key, allowEmpty) {
if (!Array.isArray(params)) {
throw new Error('[ethjs-util] method getKeys expecting type Array as \'params\' input, got \'' + typeof params + '\'');
}
if (typeof key !== 'string') {
throw new Error('[ethjs-util] method getKeys expecting type String for input \'key\' got \'' + typeof key + '\'.');
}
var result = []; // eslint-disable-line
for (var i = 0; i < params.length; i++) {
// eslint-disable-line
var value = params[i][key]; // eslint-disable-line
if (allowEmpty && !value) {
value = '';
} else if (typeof value !== 'string') {
throw new Error('invalid abi');
}
result.push(value);
}
return result;
}
/**
* Is the string a hex string.
*
* @method check if string is hex string of specific length
* @param {String} value
* @param {Number} length
* @returns {Boolean} output the string is a hex string
*/
function isHexString(value, length) {
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) {
return false;
}
return true;
}
module.exports = {
arrayContainsArray: arrayContainsArray,
intToBuffer: intToBuffer,
getBinarySize: getBinarySize,
isHexPrefixed: isHexPrefixed,
stripHexPrefix: stripHexPrefix,
padToEven: padToEven,
intToHex: intToHex,
fromAscii: fromAscii,
fromUtf8: fromUtf8,
toAscii: toAscii,
toUtf8: toUtf8,
getKeys: getKeys,
isHexString: isHexString
};
+357
View File
@@ -0,0 +1,357 @@
'use strict';
var util = require('../index.js');
var assert = require('chai').assert;
var BN = require('bn.js');
describe('check all exports', function () {
it('should have all exports available', function () {
var expected = ['arrayContainsArray', 'toBuffer', 'intToBuffer', 'getBinarySize', 'stripHexPrefix', 'isHexPrefixed', 'padToEven', 'intToHex', 'fromAscii', 'fromUtf8', 'toAscii', 'getKeys', 'isHexString', 'toUtf8'];
Object.keys(util).forEach(function (utilKey) {
assert.equal(expected.includes(utilKey), true, utilKey);
});
});
it('should convert intToHex', function () {
assert.equal(util.intToHex(new BN(0)), '0x0');
});
it('should throw when invalid abi', function () {
assert.throws(function () {
return util.getKeys([], 3289);
}, Error);
});
it('should detect invalid length hex string', function () {
assert.equal(util.isHexString('0x0', 2), false);
});
it('should stripHexPrefix strip prefix of valid strings', function () {
assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj');
assert.equal(util.stripHexPrefix('0xksfdkj'), 'ksfdkj');
assert.equal(util.stripHexPrefix('0xkdsfdkj'), 'kdsfdkj');
assert.equal(util.stripHexPrefix('0x23442sfdkj'), '23442sfdkj');
assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj');
assert.equal(util.stripHexPrefix('0xaaaasfdkj'), 'aaaasfdkj');
assert.equal(util.stripHexPrefix('0xkdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj');
assert.equal(util.stripHexPrefix('0x111dssdddj'), '111dssdddj');
});
it('should stripHexPrefix strip prefix of mix hexed strings', function () {
assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj');
assert.equal(util.stripHexPrefix('ksfdkj'), 'ksfdkj');
assert.equal(util.stripHexPrefix('kdsfdkj'), 'kdsfdkj');
assert.equal(util.stripHexPrefix('23442sfdkj'), '23442sfdkj');
assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj');
assert.equal(util.stripHexPrefix('aaaasfdkj'), 'aaaasfdkj');
assert.equal(util.stripHexPrefix('kdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj');
assert.equal(util.stripHexPrefix('111dssdddj'), '111dssdddj');
});
it('should stripHexPrefix bypass if not string', function () {
assert.equal(util.stripHexPrefix(null), null);
assert.equal(util.stripHexPrefix(undefined), undefined);
assert.equal(util.stripHexPrefix(242423), 242423);
assert.deepEqual(util.stripHexPrefix({}), {});
assert.deepEqual(util.stripHexPrefix([]), []);
assert.equal(util.stripHexPrefix(true), true);
});
it('valid padToEven should pad to even', function () {
assert.equal(String(util.padToEven('0')).length % 2, 0);
assert.equal(String(util.padToEven('111')).length % 2, 0);
assert.equal(String(util.padToEven('22222')).length % 2, 0);
assert.equal(String(util.padToEven('ddd')).length % 2, 0);
assert.equal(String(util.padToEven('aa')).length % 2, 0);
assert.equal(String(util.padToEven('aaaaaa')).length % 2, 0);
assert.equal(String(util.padToEven('sdssd')).length % 2, 0);
assert.equal(String(util.padToEven('eee')).length % 2, 0);
assert.equal(String(util.padToEven('w')).length % 2, 0);
});
it('valid padToEven should pad to even check string prefix 0', function () {
assert.equal(String(util.padToEven('0')), '00');
assert.equal(String(util.padToEven('111')), '0111');
assert.equal(String(util.padToEven('22222')), '022222');
assert.equal(String(util.padToEven('ddd')), '0ddd');
assert.equal(String(util.padToEven('aa')), 'aa');
assert.equal(String(util.padToEven('aaaaaa')), 'aaaaaa');
assert.equal(String(util.padToEven('sdssd')), '0sdssd');
assert.equal(String(util.padToEven('eee')), '0eee');
assert.equal(String(util.padToEven('w')), '0w');
});
it('should padToEven throw as expected string got null', function () {
try {
util.padToEven(null);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got undefined', function () {
try {
util.padToEven(undefined);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got {}', function () {
try {
util.padToEven({});
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got new Buffer()', function () {
try {
util.padToEven(new Buffer());
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got number', function () {
try {
util.padToEven(24423232);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got number', function () {
try {
util.getKeys(2482822);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method invalid getKeys with allow empty and no defined value', function () {
try {
util.getKeys([{ type: undefined }], 'type', true);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method valid getKeys with allow empty and false', function () {
try {
util.getKeys([{ type: true }], 'type', true);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got number', function () {
try {
util.getKeys(2482822, 293849824);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got object', function () {
try {
util.getKeys({}, []);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got null', function () {
try {
util.getKeys(null);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got false', function () {
try {
util.getKeys(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('valid getKeys should get keys from object in array', function () {
assert.deepEqual(util.getKeys([{ type: 'sfd' }, { type: 'something' }], 'type'), ['sfd', 'something']);
assert.deepEqual(util.getKeys([{ cool: 'something' }, { cool: 'fdsdfsfd' }], 'cool'), ['something', 'fdsdfsfd']);
assert.deepEqual(util.getKeys([{ type: '234424' }, { type: '243234242432' }], 'type'), ['234424', '243234242432']);
assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }], 'type'), ['something', 'something']);
assert.deepEqual(util.getKeys([{ type: 'something' }], 'type'), ['something']);
assert.deepEqual(util.getKeys([], 'type'), []);
assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }, { type: 'something' }], 'type'), ['something', 'something', 'something']);
});
it('valid isHexString tests', function () {
assert.equal(util.isHexString('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x1e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x6e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0xecfaa1a0c4372a2ac5cca1e164510ec8df04f681fc960797f1419802ec00b225'), true);
assert.equal(util.isHexString('0x6e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x620e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x1e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x2e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), true);
assert.equal(util.isHexString('0x2bb303f0ae65c64ef80a3bb3ee8ceef5d50065bd'), true);
assert.equal(util.isHexString('0x6e026d45820d91256fc73d7ff2bdef353ebfe7e9'), true);
});
it('invalid isHexString tests', function () {
assert.equal(util.isHexString(' 0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), false);
assert.equal(util.isHexString('fdsjfsd'), false);
assert.equal(util.isHexString(' 0xfdsjfsd'), false);
assert.equal(util.isHexString('0xfds*jfsd'), false);
assert.equal(util.isHexString('0xfds$jfsd'), false);
assert.equal(util.isHexString('0xf@dsjfsd'), false);
assert.equal(util.isHexString('0xfdsjf!sd'), false);
assert.equal(util.isHexString('fds@@jfsd'), false);
assert.equal(util.isHexString(24223), false);
assert.equal(util.isHexString(null), false);
assert.equal(util.isHexString(undefined), false);
assert.equal(util.isHexString(false), false);
assert.equal(util.isHexString({}), false);
assert.equal(util.isHexString([]), false);
});
it('valid arrayContainsArray should array contain every array', function () {
assert.equal(util.arrayContainsArray([1, 2, 3], [1, 2]), true);
assert.equal(util.arrayContainsArray([3, 3], [3, 3]), true);
assert.equal(util.arrayContainsArray([1, 2, 'h'], [1, 2, 'h']), true);
assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [1, 2, 'fsffds']), true);
assert.equal(util.arrayContainsArray([1], [1]), true);
assert.equal(util.arrayContainsArray([], []), true);
assert.equal(util.arrayContainsArray([1, 3333], [1, 3333]), true);
});
it('valid getBinarySize should get binary size of string', function () {
assert.equal(util.getBinarySize('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), 42);
assert.equal(util.getBinarySize('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), 66);
});
it('invalid getBinarySize should throw invalid type Boolean', function () {
try {
util.getBinarySize(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('invalid getBinarySize should throw invalid type object', function () {
try {
util.getBinarySize({});
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('invalid getBinarySize should throw invalid type Array', function () {
try {
util.getBinarySize([]);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('valid arrayContainsArray should array some every array', function () {
assert.equal(util.arrayContainsArray([1, 2], [1], true), true);
assert.equal(util.arrayContainsArray([3, 3], [3, 2323], true), true);
assert.equal(util.arrayContainsArray([1, 2, 'h'], [2332, 2, 'h'], true), true);
assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [3232, 2, 'fsffds'], true), true);
assert.equal(util.arrayContainsArray([1], [1], true), true);
assert.equal(util.arrayContainsArray([1, 3333], [1, 323232], true), true);
});
it('method arrayContainsArray should throw as expected array for params got false', function () {
try {
util.arrayContainsArray(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method arrayContainsArray should throw as expected array for params got false', function () {
try {
util.arrayContainsArray([], false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method arrayContainsArray should throw as expected array for params got {}', function () {
try {
util.arrayContainsArray({}, false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
var fromAsciiTests = [{ value: 'myString', expected: '0x6d79537472696e67' }, { value: 'myString\x00', expected: '0x6d79537472696e6700' }, { value: '\x03\0\0\x005\xE8\xC6\xD5L]\x12|\x9D\xCE\xBE\x9E\x1A7\xAB\x9B\x052\x11(\xD0\x97Y\n<\x10\0\0\0\0\0\0e!\xDFd/\xF1\xF5\xEC\f:z\xA6\xCE\xA6\xB1\xE7\xB7\xF7\xCD\xA2\xCB\xDF\x076*\x85\b\x8E\x97\xF1\x9E\xF9C1\xC9U\xC0\xE92\x1A\xD3\x86B\x8C',
expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c' }];
describe('fromAscii', function () {
fromAsciiTests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected + ' ', function () {
assert.strictEqual(util.fromAscii(test.value), test.expected);
});
});
});
var fromUtf8Tests = [{ value: 'myString', expected: '0x6d79537472696e67' }, { value: 'myString\x00', expected: '0x6d79537472696e67' }, { value: 'expected value\0\0\0', expected: '0x65787065637465642076616c7565' }];
describe('fromUtf8', function () {
fromUtf8Tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected + ' ', function () {
assert.strictEqual(util.fromUtf8(test.value), test.expected);
});
});
});
var toUtf8Tests = [{ value: '0x6d79537472696e67', expected: 'myString' }, { value: '0x6d79537472696e6700', expected: 'myString' }, { value: '0x65787065637465642076616c7565000000000000000000000000000000000000', expected: 'expected value' }];
describe('toUtf8', function () {
toUtf8Tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected + ' ', function () {
assert.strictEqual(util.toUtf8(test.value), test.expected);
});
});
});
var toAsciiTests = [{ value: '0x6d79537472696e67', expected: 'myString' }, { value: '0x6d79537472696e6700', expected: 'myString\0' }, { value: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c',
expected: '\x03\0\0\x005\xE8\xC6\xD5L]\x12|\x9D\xCE\xBE\x9E\x1A7\xAB\x9B\x052\x11(\xD0\x97Y\n<\x10\0\0\0\0\0\0e!\xDFd/\xF1\xF5\xEC\f:z\xA6\xCE\xA6\xB1\xE7\xB7\xF7\xCD\xA2\xCB\xDF\x076*\x85\b\x8E\x97\xF1\x9E\xF9C1\xC9U\xC0\xE92\x1A\xD3\x86B\x8C' }];
describe('toAsciiTests', function () {
toAsciiTests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected + ' ', function () {
assert.strictEqual(util.toAscii(test.value), test.expected);
});
});
});
describe('intToHex', function () {
it('should convert a int to hex', function () {
var i = 6003400;
var hex = util.intToHex(i);
assert.equal(hex, '0x5b9ac8');
});
});
describe('intToBuffer', function () {
it('should convert a int to a buffer', function () {
var i = 6003400;
var buf = util.intToBuffer(i);
assert.equal(buf.toString('hex'), '5b9ac8');
});
it('should convert a int to a buffer for odd length hex values', function () {
var i = 1;
var buf = util.intToBuffer(i);
assert.equal(buf.toString('hex'), '01');
});
});
});
+229
View File
@@ -0,0 +1,229 @@
{
"authors": [
"Nick Dodson <nick.dodson@consensys.net>"
],
"babel": {
"plugins": [
[
"transform-es2015-template-literals",
{
"loose": true
}
],
"transform-es2015-literals",
"transform-es2015-function-name",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions",
[
"transform-es2015-classes",
{
"loose": true
}
],
"transform-es2015-object-super",
"transform-es2015-shorthand-properties",
[
"transform-es2015-computed-properties",
{
"loose": true
}
],
[
"transform-es2015-for-of",
{
"loose": true
}
],
"transform-es2015-sticky-regex",
"transform-es2015-unicode-regex",
"check-es2015-constants",
[
"transform-es2015-spread",
{
"loose": true
}
],
"transform-es2015-parameters",
[
"transform-es2015-destructuring",
{
"loose": true
}
],
"transform-es2015-block-scoping",
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
"env": {
"commonjs": {
"plugins": [
[
"transform-es2015-modules-commonjs",
{
"loose": true
}
]
]
}
}
},
"bugs": {
"url": "https://github.com/ethjs/ethjs-util/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Richard Moore",
"email": "me@ricmoo.com",
"url": "https://ethers.io"
}
],
"dependencies": {
"is-hex-prefixed": "1.0.0",
"strip-hex-prefix": "1.0.0"
},
"deprecated": false,
"description": "A simple set of Ethereum JS utilties.",
"devDependencies": {
"babel-cli": "6.18.0",
"babel-core": "6.18.2",
"babel-eslint": "7.1.0",
"babel-loader": "6.2.8",
"babel-plugin-check-es2015-constants": "6.8.0",
"babel-plugin-transform-es2015-arrow-functions": "6.8.0",
"babel-plugin-transform-es2015-block-scoped-functions": "6.8.0",
"babel-plugin-transform-es2015-block-scoping": "6.18.0",
"babel-plugin-transform-es2015-classes": "6.18.0",
"babel-plugin-transform-es2015-computed-properties": "6.8.0",
"babel-plugin-transform-es2015-destructuring": "6.19.0",
"babel-plugin-transform-es2015-for-of": "6.18.0",
"babel-plugin-transform-es2015-function-name": "6.9.0",
"babel-plugin-transform-es2015-literals": "6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.18.0",
"babel-plugin-transform-es2015-object-super": "6.8.0",
"babel-plugin-transform-es2015-parameters": "6.18.0",
"babel-plugin-transform-es2015-shorthand-properties": "6.18.0",
"babel-plugin-transform-es2015-spread": "6.8.0",
"babel-plugin-transform-es2015-sticky-regex": "6.8.0",
"babel-plugin-transform-es2015-template-literals": "6.8.0",
"babel-plugin-transform-es2015-unicode-regex": "6.11.0",
"babel-plugin-transform-es3-member-expression-literals": "6.5.0",
"babel-plugin-transform-es3-property-literals": "6.5.0",
"babel-plugin-transform-object-rest-spread": "6.19.0",
"babel-register": "6.18.0",
"bignumber.js": "3.0.1",
"bn.js": "4.11.6",
"chai": "3.5.0",
"check-es3-syntax-cli": "0.1.3",
"coveralls": "2.11.9",
"cross-env": "1.0.7",
"eslint": "2.10.1",
"eslint-config-airbnb": "9.0.1",
"eslint-import-resolver-webpack": "0.2.4",
"eslint-plugin-import": "1.8.0",
"eslint-plugin-jsx-a11y": "1.2.0",
"eslint-plugin-react": "5.1.1",
"eventsource-polyfill": "0.9.6",
"istanbul": "0.4.5",
"json-loader": "0.5.4",
"lint-staged": "1.0.1",
"mocha": "3.1.2",
"pre-commit": "1.1.3",
"rimraf": "2.3.4",
"webpack": "2.1.0-beta.15"
},
"engines": {
"node": ">=6.5.0",
"npm": ">=3"
},
"eslintConfig": {
"parser": "babel-eslint",
"extends": "airbnb",
"env": {
"node": true,
"mocha": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"import/no-unresolved": 2,
"comma-dangle": [
2,
"always-multiline"
],
"indent": [
2,
2,
{
"SwitchCase": 1
}
],
"no-console": 1,
"max-len": 0,
"prefer-template": 2,
"no-use-before-define": 0,
"newline-per-chained-call": 0,
"arrow-body-style": [
2,
"as-needed"
]
}
},
"files": [
"dist",
"internals",
"lib",
"src"
],
"homepage": "https://github.com/ethjs/ethjs-util#readme",
"keywords": [
"ethereum",
"rpc",
"formatter",
"format",
"ethereum",
"encoding",
"decoding"
],
"license": "MIT",
"lint-staged": {
"lint:eslint": "*.js"
},
"main": "lib/index.js",
"name": "ethjs-util",
"pre-commit": "build",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/ethjs/ethjs-util.git"
},
"scripts": {
"build": "npm run build:commonjs && npm run test:lib && npm run build:umd && npm run build:umd:min",
"build:clean": "npm run test:clean && rimraf ./dist",
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --copy-files",
"build:umd": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack --config ./internals/webpack/webpack.config.js ./lib/index.js --progress",
"build:umd:min": "cross-env BABEL_ENV=commonjs NODE_ENV=production webpack --config ./internals/webpack/webpack.config.js ./lib/index.js --progress",
"coveralls": "npm run test-travis && cat ./coverage/lcov.info | coveralls",
"lint": "npm run lint:js",
"lint:eslint": "eslint --ignore-path .gitignore --ignore-pattern **/**.min.js",
"lint:js": "npm run lint:eslint -- . ",
"lint:staged": "lint-staged",
"prebuild": "npm run build:clean && npm run test",
"prepublish": "npm run build",
"pretest": "npm run lint",
"release": "npmpub",
"start": "npm test",
"test": "mocha ./src/tests/**/*.js -R spec --timeout 2000000",
"test-travis": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- src/tests/**/*.js -R spec --timeout 2000000",
"test:clean": "rimraf ./coverage",
"test:lib": "mocha ./lib/tests/**/*.js -R spec --timeout 2000000"
},
"version": "0.1.6"
,"_resolved": "https://registry.npmjs.org/ethjs-util/-/ethjs-util-0.1.6.tgz"
,"_integrity": "sha512-CUnVOQq7gSpDHZVVrQW8ExxUETWrnrvXYvYz55wOU8Uj4VCgw56XC2B/fVqQN+f7gmrnRHSLVnFAwsCuNwji8w=="
,"_from": "ethjs-util@0.1.6"
}
+203
View File
@@ -0,0 +1,203 @@
const isHexPrefixed = require('is-hex-prefixed');
const stripHexPrefix = require('strip-hex-prefix');
/**
* Pads a `String` to have an even length
* @param {String} value
* @return {String} output
*/
function padToEven(value) {
var a = value; // eslint-disable-line
if (typeof a !== 'string') {
throw new Error(`[ethjs-util] while padding to even, value must be string, is currently ${typeof a}, while padToEven.`);
}
if (a.length % 2) {
a = `0${a}`;
}
return a;
}
/**
* Converts a `Number` into a hex `String`
* @param {Number} i
* @return {String}
*/
function intToHex(i) {
var hex = i.toString(16); // eslint-disable-line
return `0x${hex}`;
}
/**
* Converts an `Number` to a `Buffer`
* @param {Number} i
* @return {Buffer}
*/
function intToBuffer(i) {
const hex = intToHex(i);
return new Buffer(padToEven(hex.slice(2)), 'hex');
}
/**
* Get the binary size of a string
* @param {String} str
* @return {Number}
*/
function getBinarySize(str) {
if (typeof str !== 'string') {
throw new Error(`[ethjs-util] while getting binary size, method getBinarySize requires input 'str' to be type String, got '${typeof str}'.`);
}
return Buffer.byteLength(str, 'utf8');
}
/**
* Returns TRUE if the first specified array contains all elements
* from the second one. FALSE otherwise.
*
* @param {array} superset
* @param {array} subset
*
* @returns {boolean}
*/
function arrayContainsArray(superset, subset, some) {
if (Array.isArray(superset) !== true) { throw new Error(`[ethjs-util] method arrayContainsArray requires input 'superset' to be an array got type '${typeof superset}'`); }
if (Array.isArray(subset) !== true) { throw new Error(`[ethjs-util] method arrayContainsArray requires input 'subset' to be an array got type '${typeof subset}'`); }
return subset[Boolean(some) && 'some' || 'every']((value) => (superset.indexOf(value) >= 0));
}
/**
* Should be called to get utf8 from it's hex representation
*
* @method toUtf8
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toUtf8(hex) {
const bufferValue = new Buffer(padToEven(stripHexPrefix(hex).replace(/^0+|0+$/g, '')), 'hex');
return bufferValue.toString('utf8');
}
/**
* Should be called to get ascii from it's hex representation
*
* @method toAscii
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toAscii(hex) {
var str = ''; // eslint-disable-line
var i = 0, l = hex.length; // eslint-disable-line
if (hex.substring(0, 2) === '0x') {
i = 2;
}
for (; i < l; i += 2) {
const code = parseInt(hex.substr(i, 2), 16);
str += String.fromCharCode(code);
}
return str;
}
/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
*
* @method fromUtf8
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromUtf8(stringValue) {
const str = new Buffer(stringValue, 'utf8');
return `0x${padToEven(str.toString('hex')).replace(/^0+|0+$/g, '')}`;
}
/**
* Should be called to get hex representation (prefixed by 0x) of ascii string
*
* @method fromAscii
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromAscii(stringValue) {
var hex = ''; // eslint-disable-line
for(var i = 0; i < stringValue.length; i++) { // eslint-disable-line
const code = stringValue.charCodeAt(i);
const n = code.toString(16);
hex += n.length < 2 ? `0${n}` : n;
}
return `0x${hex}`;
}
/**
* getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3]
*
* @method getKeys get specific key from inner object array of objects
* @param {String} params
* @param {String} key
* @param {Boolean} allowEmpty
* @returns {Array} output just a simple array of output keys
*/
function getKeys(params, key, allowEmpty) {
if (!Array.isArray(params)) { throw new Error(`[ethjs-util] method getKeys expecting type Array as 'params' input, got '${typeof params}'`); }
if (typeof key !== 'string') { throw new Error(`[ethjs-util] method getKeys expecting type String for input 'key' got '${typeof key}'.`); }
var result = []; // eslint-disable-line
for (var i = 0; i < params.length; i++) { // eslint-disable-line
var value = params[i][key]; // eslint-disable-line
if (allowEmpty && !value) {
value = '';
} else if (typeof(value) !== 'string') {
throw new Error('invalid abi');
}
result.push(value);
}
return result;
}
/**
* Is the string a hex string.
*
* @method check if string is hex string of specific length
* @param {String} value
* @param {Number} length
* @returns {Boolean} output the string is a hex string
*/
function isHexString(value, length) {
if (typeof(value) !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) { return false; }
return true;
}
module.exports = {
arrayContainsArray,
intToBuffer,
getBinarySize,
isHexPrefixed,
stripHexPrefix,
padToEven,
intToHex,
fromAscii,
fromUtf8,
toAscii,
toUtf8,
getKeys,
isHexString,
};
+382
View File
@@ -0,0 +1,382 @@
const util = require('../index.js');
const assert = require('chai').assert;
const BN = require('bn.js');
describe('check all exports', () => {
it('should have all exports available', () => {
const expected = ['arrayContainsArray',
'toBuffer',
'intToBuffer',
'getBinarySize',
'stripHexPrefix',
'isHexPrefixed',
'padToEven',
'intToHex',
'fromAscii',
'fromUtf8',
'toAscii',
'getKeys',
'isHexString',
'toUtf8'];
Object.keys(util).forEach((utilKey) => {
assert.equal(expected.includes(utilKey), true, utilKey);
});
});
it('should convert intToHex', () => {
assert.equal(util.intToHex(new BN(0)), '0x0');
});
it('should throw when invalid abi', () => {
assert.throws(() => util.getKeys([], 3289), Error);
});
it('should detect invalid length hex string', () => {
assert.equal(util.isHexString('0x0', 2), false);
});
it('should stripHexPrefix strip prefix of valid strings', () => {
assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj');
assert.equal(util.stripHexPrefix('0xksfdkj'), 'ksfdkj');
assert.equal(util.stripHexPrefix('0xkdsfdkj'), 'kdsfdkj');
assert.equal(util.stripHexPrefix('0x23442sfdkj'), '23442sfdkj');
assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj');
assert.equal(util.stripHexPrefix('0xaaaasfdkj'), 'aaaasfdkj');
assert.equal(util.stripHexPrefix('0xkdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj');
assert.equal(util.stripHexPrefix('0x111dssdddj'), '111dssdddj');
});
it('should stripHexPrefix strip prefix of mix hexed strings', () => {
assert.equal(util.stripHexPrefix('0xkdsfksfdkj'), 'kdsfksfdkj');
assert.equal(util.stripHexPrefix('ksfdkj'), 'ksfdkj');
assert.equal(util.stripHexPrefix('kdsfdkj'), 'kdsfdkj');
assert.equal(util.stripHexPrefix('23442sfdkj'), '23442sfdkj');
assert.equal(util.stripHexPrefix('0xkdssdfssfdkj'), 'kdssdfssfdkj');
assert.equal(util.stripHexPrefix('aaaasfdkj'), 'aaaasfdkj');
assert.equal(util.stripHexPrefix('kdsdfsfsdfsdfsdfdkj'), 'kdsdfsfsdfsdfsdfdkj');
assert.equal(util.stripHexPrefix('111dssdddj'), '111dssdddj');
});
it('should stripHexPrefix bypass if not string', () => {
assert.equal(util.stripHexPrefix(null), null);
assert.equal(util.stripHexPrefix(undefined), undefined);
assert.equal(util.stripHexPrefix(242423), 242423);
assert.deepEqual(util.stripHexPrefix({}), {});
assert.deepEqual(util.stripHexPrefix([]), []);
assert.equal(util.stripHexPrefix(true), true);
});
it('valid padToEven should pad to even', () => {
assert.equal(String(util.padToEven('0')).length % 2, 0);
assert.equal(String(util.padToEven('111')).length % 2, 0);
assert.equal(String(util.padToEven('22222')).length % 2, 0);
assert.equal(String(util.padToEven('ddd')).length % 2, 0);
assert.equal(String(util.padToEven('aa')).length % 2, 0);
assert.equal(String(util.padToEven('aaaaaa')).length % 2, 0);
assert.equal(String(util.padToEven('sdssd')).length % 2, 0);
assert.equal(String(util.padToEven('eee')).length % 2, 0);
assert.equal(String(util.padToEven('w')).length % 2, 0);
});
it('valid padToEven should pad to even check string prefix 0', () => {
assert.equal(String(util.padToEven('0')), '00');
assert.equal(String(util.padToEven('111')), '0111');
assert.equal(String(util.padToEven('22222')), '022222');
assert.equal(String(util.padToEven('ddd')), '0ddd');
assert.equal(String(util.padToEven('aa')), 'aa');
assert.equal(String(util.padToEven('aaaaaa')), 'aaaaaa');
assert.equal(String(util.padToEven('sdssd')), '0sdssd');
assert.equal(String(util.padToEven('eee')), '0eee');
assert.equal(String(util.padToEven('w')), '0w');
});
it('should padToEven throw as expected string got null', () => {
try {
util.padToEven(null);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got undefined', () => {
try {
util.padToEven(undefined);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got {}', () => {
try {
util.padToEven({});
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got new Buffer()', () => {
try {
util.padToEven(new Buffer());
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('should padToEven throw as expected string got number', () => {
try {
util.padToEven(24423232);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got number', () => {
try {
util.getKeys(2482822);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method invalid getKeys with allow empty and no defined value', () => {
try {
util.getKeys([{ type: undefined }], 'type', true);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method valid getKeys with allow empty and false', () => {
try {
util.getKeys([{ type: true }], 'type', true);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got number', () => {
try {
util.getKeys(2482822, 293849824);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got object', () => {
try {
util.getKeys({}, []);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got null', () => {
try {
util.getKeys(null);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method getKeys should throw as expected array for params got false', () => {
try {
util.getKeys(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('valid getKeys should get keys from object in array', () => {
assert.deepEqual(util.getKeys([{ type: 'sfd' }, { type: 'something' }], 'type'), ['sfd', 'something']);
assert.deepEqual(util.getKeys([{ cool: 'something' }, { cool: 'fdsdfsfd' }], 'cool'), ['something', 'fdsdfsfd']);
assert.deepEqual(util.getKeys([{ type: '234424' }, { type: '243234242432' }], 'type'), ['234424', '243234242432']);
assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }], 'type'), ['something', 'something']);
assert.deepEqual(util.getKeys([{ type: 'something' }], 'type'), ['something']);
assert.deepEqual(util.getKeys([], 'type'), []);
assert.deepEqual(util.getKeys([{ type: 'something' }, { type: 'something' }, { type: 'something' }], 'type'), ['something', 'something', 'something']);
});
it('valid isHexString tests', () => {
assert.equal(util.isHexString('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x1e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x6e026d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0xecfaa1a0c4372a2ac5cca1e164510ec8df04f681fc960797f1419802ec00b225'), true);
assert.equal(util.isHexString('0x6e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x620e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x1e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x2e0e6d45820d91356fc73d7ff2bdef353ebfe7e9'), true);
assert.equal(util.isHexString('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), true);
assert.equal(util.isHexString('0x2bb303f0ae65c64ef80a3bb3ee8ceef5d50065bd'), true);
assert.equal(util.isHexString('0x6e026d45820d91256fc73d7ff2bdef353ebfe7e9'), true);
});
it('invalid isHexString tests', () => {
assert.equal(util.isHexString(' 0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), false);
assert.equal(util.isHexString('fdsjfsd'), false);
assert.equal(util.isHexString(' 0xfdsjfsd'), false);
assert.equal(util.isHexString('0xfds*jfsd'), false);
assert.equal(util.isHexString('0xfds$jfsd'), false);
assert.equal(util.isHexString('0xf@dsjfsd'), false);
assert.equal(util.isHexString('0xfdsjf!sd'), false);
assert.equal(util.isHexString('fds@@jfsd'), false);
assert.equal(util.isHexString(24223), false);
assert.equal(util.isHexString(null), false);
assert.equal(util.isHexString(undefined), false);
assert.equal(util.isHexString(false), false);
assert.equal(util.isHexString({}), false);
assert.equal(util.isHexString([]), false);
});
it('valid arrayContainsArray should array contain every array', () => {
assert.equal(util.arrayContainsArray([1, 2, 3], [1, 2]), true);
assert.equal(util.arrayContainsArray([3, 3], [3, 3]), true);
assert.equal(util.arrayContainsArray([1, 2, 'h'], [1, 2, 'h']), true);
assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [1, 2, 'fsffds']), true);
assert.equal(util.arrayContainsArray([1], [1]), true);
assert.equal(util.arrayContainsArray([], []), true);
assert.equal(util.arrayContainsArray([1, 3333], [1, 3333]), true);
});
it('valid getBinarySize should get binary size of string', () => {
assert.equal(util.getBinarySize('0x0e026d45820d91356fc73d7ff2bdef353ebfe7e9'), 42);
assert.equal(util.getBinarySize('0x220c96d48733a847570c2f0b40daa8793b3ae875b26a4ead1f0f9cead05c3863'), 66);
});
it('invalid getBinarySize should throw invalid type Boolean', () => {
try {
util.getBinarySize(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('invalid getBinarySize should throw invalid type object', () => {
try {
util.getBinarySize({});
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('invalid getBinarySize should throw invalid type Array', () => {
try {
util.getBinarySize([]);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('valid arrayContainsArray should array some every array', () => {
assert.equal(util.arrayContainsArray([1, 2], [1], true), true);
assert.equal(util.arrayContainsArray([3, 3], [3, 2323], true), true);
assert.equal(util.arrayContainsArray([1, 2, 'h'], [2332, 2, 'h'], true), true);
assert.equal(util.arrayContainsArray([1, 2, 'fsffds'], [3232, 2, 'fsffds'], true), true);
assert.equal(util.arrayContainsArray([1], [1], true), true);
assert.equal(util.arrayContainsArray([1, 3333], [1, 323232], true), true);
});
it('method arrayContainsArray should throw as expected array for params got false', () => {
try {
util.arrayContainsArray(false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method arrayContainsArray should throw as expected array for params got false', () => {
try {
util.arrayContainsArray([], false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
it('method arrayContainsArray should throw as expected array for params got {}', () => {
try {
util.arrayContainsArray({}, false);
} catch (error) {
assert.equal(typeof error, 'object');
}
});
const fromAsciiTests = [
{ value: 'myString', 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('fromAscii', () => {
fromAsciiTests.forEach((test) => {
it(`should turn ${test.value} to ${test.expected} `, () => {
assert.strictEqual(util.fromAscii(test.value), test.expected);
});
});
});
const fromUtf8Tests = [
{ value: 'myString', expected: '0x6d79537472696e67' },
{ value: 'myString\x00', expected: '0x6d79537472696e67' },
{ value: 'expected value\u0000\u0000\u0000', expected: '0x65787065637465642076616c7565' },
];
describe('fromUtf8', () => {
fromUtf8Tests.forEach((test) => {
it(`should turn ${test.value} to ${test.expected} `, () => {
assert.strictEqual(util.fromUtf8(test.value), test.expected);
});
});
});
const toUtf8Tests = [
{ value: '0x6d79537472696e67', expected: 'myString' },
{ value: '0x6d79537472696e6700', expected: 'myString' },
{ value: '0x65787065637465642076616c7565000000000000000000000000000000000000', expected: 'expected value' },
];
describe('toUtf8', () => {
toUtf8Tests.forEach((test) => {
it(`should turn ${test.value} to ${test.expected} `, () => {
assert.strictEqual(util.toUtf8(test.value), test.expected);
});
});
});
const toAsciiTests = [
{ value: '0x6d79537472696e67', 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('toAsciiTests', () => {
toAsciiTests.forEach((test) => {
it(`should turn ${test.value} to ${test.expected} `, () => {
assert.strictEqual(util.toAscii(test.value), test.expected);
});
});
});
describe('intToHex', () => {
it('should convert a int to hex', () => {
const i = 6003400;
const hex = util.intToHex(i);
assert.equal(hex, '0x5b9ac8');
});
});
describe('intToBuffer', () => {
it('should convert a int to a buffer', () => {
const i = 6003400;
const buf = util.intToBuffer(i);
assert.equal(buf.toString('hex'), '5b9ac8');
});
it('should convert a int to a buffer for odd length hex values', () => {
const i = 1;
const buf = util.intToBuffer(i);
assert.equal(buf.toString('hex'), '01');
});
});
});