web3-utils
Advanced tools
Comparing version
@@ -23,2 +23,3 @@ /* | ||
*/ | ||
var _ = require('underscore'); | ||
var ethjsUnit = require('ethjs-unit'); | ||
@@ -43,4 +44,4 @@ var utils = require('./utils.js'); | ||
// add data if given | ||
if (!!error && typeof error === 'object' && !(error instanceof Error) && error.data) { | ||
if (!!error.data && typeof error.data === 'object' || Array.isArray(error.data)) { | ||
if (_.isObject(error) && !(error instanceof Error) && error.data) { | ||
if (_.isObject(error.data) || _.isArray(error.data)) { | ||
error.data = JSON.stringify(error.data, null, 2); | ||
@@ -50,14 +51,14 @@ } | ||
} | ||
if (typeof error === 'string') { | ||
if (_.isString(error)) { | ||
error = new Error(error); | ||
} | ||
if (typeof callback === 'function') { | ||
if (_.isFunction(callback)) { | ||
callback(error, optionalData); | ||
} | ||
if (typeof reject === 'function') { | ||
if (_.isFunction(reject)) { | ||
// suppress uncatched error if an error listener is present | ||
// OR suppress uncatched error if an callback listener is present | ||
if (emitter && | ||
(typeof emitter.listeners === 'function' && | ||
emitter.listeners('error').length) || typeof callback === 'function') { | ||
(_.isFunction(emitter.listeners) && | ||
emitter.listeners('error').length) || _.isFunction(callback)) { | ||
emitter.catch(function () { }); | ||
@@ -70,3 +71,3 @@ } | ||
} | ||
if (emitter && typeof emitter.emit === 'function') { | ||
if (emitter && _.isFunction(emitter.emit)) { | ||
// emit later, to be able to return emitter | ||
@@ -88,3 +89,3 @@ setTimeout(function () { | ||
var _jsonInterfaceMethodToString = function (json) { | ||
if (!!json && typeof json === 'object' && json.name && json.name.indexOf('(') !== -1) { | ||
if (_.isObject(json) && json.name && json.name.indexOf('(') !== -1) { | ||
return json.name; | ||
@@ -117,3 +118,3 @@ } | ||
// console.log("result should have things: " + result) | ||
if (Array.isArray(result) && includeTuple) { | ||
if (_.isArray(result) && includeTuple) { | ||
// console.log("include tuple word, and its an array. joining...: " + result.types) | ||
@@ -163,3 +164,3 @@ types.push('tuple(' + result.join(',') + ')' + suffix); | ||
for (; i < l; i += 2) { | ||
var code = parseInt(hex.slice(i, i + 2), 16); | ||
var code = parseInt(hex.substr(i, 2), 16); | ||
str += String.fromCharCode(code); | ||
@@ -225,3 +226,3 @@ } | ||
unit = getUnitValue(unit); | ||
if (!utils.isBN(number) && !(typeof number === 'string')) { | ||
if (!utils.isBN(number) && !_.isString(number)) { | ||
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); | ||
@@ -255,3 +256,3 @@ } | ||
unit = getUnitValue(unit); | ||
if (!utils.isBN(number) && !(typeof number === 'string')) { | ||
if (!utils.isBN(number) && !_.isString(number)) { | ||
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); | ||
@@ -291,3 +292,3 @@ } | ||
* developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | ||
* Block tag `safe` and `block number` combination param is not supported | ||
* | ||
* @method compareBlockNumbers | ||
@@ -302,20 +303,18 @@ * | ||
var compareBlockNumbers = function (a, b) { | ||
// Increasing order: (genesis = earliest), safe, (finalized ~ latest), pending | ||
// safe vs block-num cant be compared as block number provided can be on left or right side of safe tag, until safe tag block number is extracted and compared | ||
if (a === b) { | ||
if (a == b) { | ||
return 0; | ||
} | ||
else if (("genesis" === a || "earliest" === a || 0 === a) && ("genesis" === b || "earliest" === b || 0 === b)) { | ||
else if (("genesis" == a || "earliest" == a || 0 == a) && ("genesis" == b || "earliest" == b || 0 == b)) { | ||
return 0; | ||
} | ||
else if ("genesis" === a || "earliest" === a || a === 0) { | ||
else if ("genesis" == a || "earliest" == a) { | ||
// b !== a, thus a < b | ||
return -1; | ||
} | ||
else if ("genesis" === b || "earliest" === b || b === 0) { | ||
else if ("genesis" == b || "earliest" == b) { | ||
// b !== a, thus a > b | ||
return 1; | ||
} | ||
else if (a === "latest" || a === "finalized") { | ||
if (b === "pending") { | ||
else if (a == "latest") { | ||
if (b == "pending") { | ||
return -1; | ||
@@ -328,4 +327,4 @@ } | ||
} | ||
else if (b === "latest" || b === "finalized") { | ||
if (a === "pending") { | ||
else if (b === "latest") { | ||
if (a == "pending") { | ||
return 1; | ||
@@ -338,13 +337,9 @@ } | ||
} | ||
else if (a === "pending") { | ||
else if (a == "pending") { | ||
// b (== OR <) "latest", thus a > b | ||
return 1; | ||
} | ||
else if (b === "pending") { | ||
else if (b == "pending") { | ||
return -1; | ||
} | ||
else if (a === "safe" || b === "safe") { | ||
// either a or b is "safe" and the other one did not fall into any of the conditions above, so the other one is a number | ||
return undefined; | ||
} | ||
else { | ||
@@ -371,2 +366,3 @@ let bnA = new BN(a); | ||
randomHex: randomHex, | ||
_: _, | ||
BN: utils.BN, | ||
@@ -420,4 +416,3 @@ isBN: utils.isBN, | ||
isInBloom: utils.isInBloom, | ||
compareBlockNumbers: compareBlockNumbers, | ||
toNumber: utils.toNumber | ||
compareBlockNumbers: compareBlockNumbers | ||
}; |
@@ -22,2 +22,3 @@ /* | ||
*/ | ||
var _ = require('underscore'); | ||
var BN = require('bn.js'); | ||
@@ -163,3 +164,3 @@ var utils = require('./utils.js'); | ||
/*jshint maxcomplexity:false */ | ||
if (Array.isArray(arg)) { | ||
if (_.isArray(arg)) { | ||
throw new Error('Autodetection of array types is not supported.'); | ||
@@ -170,3 +171,3 @@ } | ||
// if type is given | ||
if (!!arg && typeof arg === 'object' && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { | ||
if (_.isObject(arg) && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { | ||
type = arg.hasOwnProperty('t') ? arg.t : arg.type; | ||
@@ -187,3 +188,3 @@ value = arg.hasOwnProperty('v') ? arg.v : arg.value; | ||
// get the array size | ||
if (Array.isArray(value)) { | ||
if (_.isArray(value)) { | ||
arraySize = _parseTypeNArray(type); | ||
@@ -197,3 +198,3 @@ if (arraySize && value.length !== arraySize) { | ||
} | ||
if (Array.isArray(value)) { | ||
if (_.isArray(value)) { | ||
hexArg = value.map(function (val) { | ||
@@ -218,3 +219,3 @@ return _solidityPack(type, val, arraySize).toString('hex').replace('0x', ''); | ||
var args = Array.prototype.slice.call(arguments); | ||
var hexArgs = args.map(_processSolidityEncodePackedArgs); | ||
var hexArgs = _.map(args, _processSolidityEncodePackedArgs); | ||
// console.log(args, hexArgs); | ||
@@ -231,3 +232,3 @@ // console.log('0x'+ hexArgs.join('')); | ||
var soliditySha3Raw = function () { | ||
return utils.sha3Raw('0x' + Array.prototype.slice.call(arguments).map(_processSolidityEncodePackedArgs).join('')); | ||
return utils.sha3Raw('0x' + _.map(Array.prototype.slice.call(arguments), _processSolidityEncodePackedArgs).join('')); | ||
}; | ||
@@ -243,3 +244,3 @@ /** | ||
var args = Array.prototype.slice.call(arguments); | ||
var hexArgs = args.map(_processSolidityEncodePackedArgs); | ||
var hexArgs = _.map(args, _processSolidityEncodePackedArgs); | ||
return '0x' + hexArgs.join('').toLowerCase(); | ||
@@ -246,0 +247,0 @@ }; |
@@ -22,8 +22,8 @@ /* | ||
*/ | ||
var _ = require('underscore'); | ||
var BN = require('bn.js'); | ||
var numberToBN = require('number-to-bn'); | ||
var utf8 = require('utf8'); | ||
var ethereumjsUtil = require('@ethereumjs/util'); | ||
var Hash = require("eth-lib/lib/hash"); | ||
var ethereumBloomFilters = require('ethereum-bloom-filters'); | ||
var { keccak256 } = require('ethereum-cryptography/keccak.js'); | ||
/** | ||
@@ -188,3 +188,3 @@ * Returns true if object is BN, otherwise false | ||
for (var i = 0; i < l; i += 2) { | ||
code = parseInt(hex.slice(i, i + 2), 16); | ||
code = parseInt(hex.substr(i, 2), 16); | ||
// if (code !== 0) { | ||
@@ -197,22 +197,16 @@ str += String.fromCharCode(code); | ||
/** | ||
* Converts value to it's number representation. | ||
* However, if the value is larger than the maximum safe integer, returns the value as a string. | ||
* Converts value to it's number representation | ||
* | ||
* @method hexToNumber | ||
* @param {String|Number|BN} value | ||
* @param {Boolean} bigIntOnOverflow - if true, return the hex value in case of overflow | ||
* @return {Number|String} | ||
* @return {String} | ||
*/ | ||
var hexToNumber = function (value, bigIntOnOverflow = false) { | ||
var hexToNumber = function (value) { | ||
if (!value) { | ||
return value; | ||
} | ||
if (typeof value === 'string' && !isHexStrict(value)) { | ||
if (_.isString(value) && !isHexStrict(value)) { | ||
throw new Error('Given value "' + value + '" is not a valid hex string.'); | ||
} | ||
const n = toBN(value); | ||
if (bigIntOnOverflow && (n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER)) { | ||
return BigInt(n); | ||
} | ||
return n.toNumber(); | ||
return toBN(value).toNumber(); | ||
}; | ||
@@ -229,3 +223,3 @@ /** | ||
return value; | ||
if (typeof value === 'string' && !isHexStrict(value)) { | ||
if (_.isString(value) && !isHexStrict(value)) { | ||
throw new Error('Given value "' + value + '" is not a valid hex string.'); | ||
@@ -243,3 +237,3 @@ } | ||
var numberToHex = function (value) { | ||
if ((value === null || value === undefined)) { | ||
if (_.isNull(value) || _.isUndefined(value)) { | ||
return value; | ||
@@ -252,3 +246,3 @@ } | ||
var result = number.toString(16); | ||
return number.lt(new BN(0)) ? '-0x' + result.slice(1) : '0x' + result; | ||
return number.lt(new BN(0)) ? '-0x' + result.substr(1) : '0x' + result; | ||
}; | ||
@@ -289,3 +283,3 @@ /** | ||
for (var bytes = [], c = 0; c < hex.length; c += 2) | ||
bytes.push(parseInt(hex.slice(c, c + 2), 16)); | ||
bytes.push(parseInt(hex.substr(c, 2), 16)); | ||
return bytes; | ||
@@ -308,3 +302,3 @@ }; | ||
} | ||
if (typeof value === 'boolean') { | ||
if (_.isBoolean(value)) { | ||
return returnType ? 'bool' : value ? '0x01' : '0x00'; | ||
@@ -315,7 +309,7 @@ } | ||
} | ||
if (typeof value === 'object' && !!value && !isBigNumber(value) && !isBN(value)) { | ||
if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) { | ||
return returnType ? 'string' : utf8ToHex(JSON.stringify(value)); | ||
} | ||
// if its a negative number, pass it through numberToHex | ||
if (typeof value === 'string') { | ||
if (_.isString(value)) { | ||
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) { | ||
@@ -341,3 +335,3 @@ return returnType ? 'int256' : numberToHex(value); | ||
var isHexStrict = function (hex) { | ||
return ((typeof hex === 'string' || typeof hex === 'number') && /^(-)?0x[0-9a-f]*$/i.test(hex)); | ||
return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex)); | ||
}; | ||
@@ -352,3 +346,3 @@ /** | ||
var isHex = function (hex) { | ||
return ((typeof hex === 'string' || typeof hex === 'number') && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); | ||
return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); | ||
}; | ||
@@ -449,9 +443,5 @@ /** | ||
if (isHexStrict(value) && /^0x/i.test((value).toString())) { | ||
value = ethereumjsUtil.toBuffer(value); | ||
value = hexToBytes(value); | ||
} | ||
else if (typeof value === 'string') { | ||
// Assume value is an arbitrary string | ||
value = Buffer.from(value, 'utf-8'); | ||
} | ||
var returnValue = ethereumjsUtil.bufferToHex(keccak256(value)); | ||
var returnValue = Hash.keccak256(value); // jshint ignore:line | ||
if (returnValue === SHA3_NULL_S) { | ||
@@ -465,3 +455,3 @@ return null; | ||
// expose the under the hood keccak256 | ||
sha3._Hash = keccak256; | ||
sha3._Hash = Hash; | ||
/** | ||
@@ -481,29 +471,4 @@ * @method sha3Raw | ||
}; | ||
/** | ||
* Auto converts any given value into it's hex representation, | ||
* then converts hex to number. | ||
* | ||
* @method toNumber | ||
* @param {String|Number|BN} value | ||
* @param {Boolean} bigIntOnOverflow - if true, return the hex value in case of overflow | ||
* @return {Number|String} | ||
*/ | ||
var toNumber = function (value, bigIntOnOverflow = false) { | ||
return typeof value === 'number' ? value : hexToNumber(toHex(value), bigIntOnOverflow); | ||
}; | ||
// 1.x currently accepts 0x... strings, bn.js after update doesn't. it would be a breaking change | ||
var BNwrapped = function (value) { | ||
// check negative | ||
if (typeof value == "string" && value.includes("0x")) { | ||
const [negative, hexValue] = value.toLocaleLowerCase().startsWith('-') ? ["-", value.slice(3)] : ["", value.slice(2)]; | ||
return new BN(negative + hexValue, 16); | ||
} | ||
else { | ||
return new BN(value); | ||
} | ||
}; | ||
Object.setPrototypeOf(BNwrapped, BN); | ||
Object.setPrototypeOf(BNwrapped.prototype, BN.prototype); | ||
module.exports = { | ||
BN: BNwrapped, | ||
BN: BN, | ||
isBN: isBN, | ||
@@ -535,4 +500,3 @@ isBigNumber: isBigNumber, | ||
sha3: sha3, | ||
sha3Raw: sha3Raw, | ||
toNumber: toNumber | ||
sha3Raw: sha3Raw | ||
}; |
{ | ||
"name": "web3-utils", | ||
"version": "1.10.3", | ||
"version": "1.3.1", | ||
"description": "Collection of utility functions used in web3.js.", | ||
@@ -12,3 +12,3 @@ "repository": "https://github.com/ethereum/web3.js/tree/1.x/packages/web3-utils", | ||
"scripts": { | ||
"compile": "tsc -b tsconfig.json", | ||
"tsc": "tsc -b tsconfig.json", | ||
"dtslint": "dtslint --localTs ../../node_modules/typescript/lib types" | ||
@@ -18,9 +18,9 @@ }, | ||
"dependencies": { | ||
"@ethereumjs/util": "^8.1.0", | ||
"bn.js": "^5.2.1", | ||
"bn.js": "^4.11.9", | ||
"eth-lib": "0.2.8", | ||
"ethereum-bloom-filters": "^1.0.6", | ||
"ethereum-cryptography": "^2.1.2", | ||
"ethjs-unit": "0.1.6", | ||
"number-to-bn": "1.7.0", | ||
"randombytes": "^2.1.0", | ||
"underscore": "1.9.1", | ||
"utf8": "3.0.0" | ||
@@ -30,5 +30,5 @@ }, | ||
"dtslint": "^3.4.1", | ||
"typescript": "4.9.5" | ||
"typescript": "^3.9.5" | ||
}, | ||
"gitHead": "24d310caa06af88cb1fe236b7d3b9d135f053d71" | ||
"gitHead": "1a850aec1d8fa86bfacef1f3ffe71839b87e78e7" | ||
} |
# web3-utils | ||
[![NPM Package][npm-image]][npm-url] | ||
[![NPM Package][npm-image]][npm-url] [![Dependency Status][deps-image]][deps-url] [![Dev Dependency Status][deps-dev-image]][deps-dev-url] | ||
@@ -13,6 +13,4 @@ This is a sub-package of [web3.js][repo]. | ||
You can install the package either using [NPM](https://www.npmjs.com/package/web3-utils) or using [Yarn](https://yarnpkg.com/package/web3-utils) | ||
### Node.js | ||
### Using NPM | ||
```bash | ||
@@ -22,8 +20,2 @@ npm install web3-utils | ||
### Using Yarn | ||
```bash | ||
yarn add web3-utils | ||
``` | ||
## Usage | ||
@@ -50,1 +42,5 @@ | ||
[npm-url]: https://npmjs.org/package/web3-utils | ||
[deps-image]: https://david-dm.org/ethereum/web3.js/1.x/status.svg?path=packages/web3-utils | ||
[deps-url]: https://david-dm.org/ethereum/web3.js/1.x?path=packages/web3-utils | ||
[deps-dev-image]: https://david-dm.org/ethereum/web3.js/1.x/dev-status.svg?path=packages/web3-utils | ||
[deps-dev-url]: https://david-dm.org/ethereum/web3.js/1.x?type=dev&path=packages/web3-utils |
@@ -24,2 +24,4 @@ /* | ||
var _ = require('underscore'); | ||
var ethjsUnit = require('ethjs-unit'); | ||
@@ -47,4 +49,4 @@ var utils = require('./utils.js'); | ||
// add data if given | ||
if(!!error && typeof error === 'object' && !(error instanceof Error) && error.data) { | ||
if(!!error.data && typeof error.data === 'object' || Array.isArray(error.data)) { | ||
if(_.isObject(error) && !(error instanceof Error) && error.data) { | ||
if(_.isObject(error.data) || _.isArray(error.data)) { | ||
error.data = JSON.stringify(error.data, null, 2); | ||
@@ -56,10 +58,10 @@ } | ||
if(typeof error === 'string') { | ||
if(_.isString(error)) { | ||
error = new Error(error); | ||
} | ||
if (typeof callback === 'function') { | ||
if (_.isFunction(callback)) { | ||
callback(error, optionalData); | ||
} | ||
if (typeof reject === 'function') { | ||
if (_.isFunction(reject)) { | ||
// suppress uncatched error if an error listener is present | ||
@@ -69,4 +71,4 @@ // OR suppress uncatched error if an callback listener is present | ||
emitter && | ||
(typeof emitter.listeners === 'function' && | ||
emitter.listeners('error').length) || typeof callback === 'function' | ||
(_.isFunction(emitter.listeners) && | ||
emitter.listeners('error').length) || _.isFunction(callback) | ||
) { | ||
@@ -81,3 +83,3 @@ emitter.catch(function(){}); | ||
if(emitter && typeof emitter.emit === 'function') { | ||
if(emitter && _.isFunction(emitter.emit)) { | ||
// emit later, to be able to return emitter | ||
@@ -101,3 +103,3 @@ setTimeout(function () { | ||
var _jsonInterfaceMethodToString = function (json) { | ||
if (!!json && typeof json === 'object' && json.name && json.name.indexOf('(') !== -1) { | ||
if (_.isObject(json) && json.name && json.name.indexOf('(') !== -1) { | ||
return json.name; | ||
@@ -133,3 +135,3 @@ } | ||
// console.log("result should have things: " + result) | ||
if(Array.isArray(result) && includeTuple) { | ||
if(_.isArray(result) && includeTuple) { | ||
// console.log("include tuple word, and its an array. joining...: " + result.types) | ||
@@ -154,4 +156,4 @@ types.push('tuple(' + result.join(',') + ')' + suffix); | ||
}; | ||
/** | ||
@@ -184,3 +186,3 @@ * Returns a random hex string by the given bytes size | ||
for (; i < l; i+=2) { | ||
var code = parseInt(hex.slice(i, i + 2), 16); | ||
var code = parseInt(hex.substr(i, 2), 16); | ||
str += String.fromCharCode(code); | ||
@@ -254,3 +256,3 @@ } | ||
if(!utils.isBN(number) && !(typeof number === 'string')) { | ||
if(!utils.isBN(number) && !_.isString(number)) { | ||
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); | ||
@@ -287,3 +289,3 @@ } | ||
if(!utils.isBN(number) && !(typeof number === 'string')) { | ||
if(!utils.isBN(number) && !_.isString(number)) { | ||
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); | ||
@@ -332,3 +334,3 @@ } | ||
* developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | ||
* Block tag `safe` and `block number` combination param is not supported | ||
* | ||
* @method compareBlockNumbers | ||
@@ -343,16 +345,14 @@ * | ||
var compareBlockNumbers = function(a, b) { | ||
// Increasing order: (genesis = earliest), safe, (finalized ~ latest), pending | ||
// safe vs block-num cant be compared as block number provided can be on left or right side of safe tag, until safe tag block number is extracted and compared | ||
if (a === b) { | ||
if (a == b) { | ||
return 0; | ||
} else if (("genesis" === a || "earliest" === a || 0 === a) && ("genesis" === b || "earliest" === b || 0 === b)) { | ||
} else if (("genesis" == a || "earliest" == a || 0 == a) && ("genesis" == b || "earliest" == b || 0 == b)) { | ||
return 0; | ||
} else if ("genesis" === a || "earliest" === a || a === 0) { | ||
} else if ("genesis" == a || "earliest" == a) { | ||
// b !== a, thus a < b | ||
return -1; | ||
} else if ("genesis" === b || "earliest" === b || b === 0) { | ||
} else if ("genesis" == b || "earliest" == b) { | ||
// b !== a, thus a > b | ||
return 1; | ||
} else if (a === "latest" || a === "finalized") { | ||
if (b === "pending") { | ||
} else if (a == "latest") { | ||
if (b == "pending") { | ||
return -1; | ||
@@ -363,19 +363,15 @@ } else { | ||
} | ||
} else if (b === "latest" || b === "finalized") { | ||
if (a === "pending") { | ||
} else if (b === "latest") { | ||
if (a == "pending") { | ||
return 1; | ||
} else { | ||
// b !== ("pending" OR "latest"), thus a > b | ||
return -1 | ||
return -1 | ||
} | ||
} else if (a === "pending") { | ||
} else if (a == "pending") { | ||
// b (== OR <) "latest", thus a > b | ||
return 1; | ||
} else if (b === "pending") { | ||
} else if (b == "pending") { | ||
return -1; | ||
} else if(a === "safe" || b === "safe") { | ||
// either a or b is "safe" and the other one did not fall into any of the conditions above, so the other one is a number | ||
return undefined; | ||
} | ||
else { | ||
} else { | ||
let bnA = new BN(a); | ||
@@ -400,2 +396,3 @@ let bnB = new BN(b); | ||
randomHex: randomHex, | ||
_: _, | ||
BN: utils.BN, | ||
@@ -460,5 +457,3 @@ isBN: utils.isBN, | ||
compareBlockNumbers: compareBlockNumbers, | ||
toNumber: utils.toNumber | ||
compareBlockNumbers: compareBlockNumbers | ||
}; |
@@ -23,2 +23,3 @@ /* | ||
var _ = require('underscore'); | ||
var BN = require('bn.js'); | ||
@@ -175,3 +176,3 @@ var utils = require('./utils.js'); | ||
if(Array.isArray(arg)) { | ||
if(_.isArray(arg)) { | ||
throw new Error('Autodetection of array types is not supported.'); | ||
@@ -184,3 +185,3 @@ } | ||
// if type is given | ||
if (!!arg && typeof arg === 'object' && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { | ||
if (_.isObject(arg) && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { | ||
type = arg.hasOwnProperty('t') ? arg.t : arg.type; | ||
@@ -205,3 +206,3 @@ value = arg.hasOwnProperty('v') ? arg.v : arg.value; | ||
// get the array size | ||
if(Array.isArray(value)) { | ||
if(_.isArray(value)) { | ||
arraySize = _parseTypeNArray(type); | ||
@@ -216,3 +217,3 @@ if(arraySize && value.length !== arraySize) { | ||
if (Array.isArray(value)) { | ||
if (_.isArray(value)) { | ||
hexArg = value.map(function (val) { | ||
@@ -240,3 +241,3 @@ return _solidityPack(type, val, arraySize).toString('hex').replace('0x',''); | ||
var hexArgs = args.map(_processSolidityEncodePackedArgs); | ||
var hexArgs = _.map(args, _processSolidityEncodePackedArgs); | ||
@@ -256,3 +257,3 @@ // console.log(args, hexArgs); | ||
var soliditySha3Raw = function () { | ||
return utils.sha3Raw('0x'+ Array.prototype.slice.call(arguments).map(_processSolidityEncodePackedArgs).join('')); | ||
return utils.sha3Raw('0x'+ _.map(Array.prototype.slice.call(arguments), _processSolidityEncodePackedArgs).join('')); | ||
}; | ||
@@ -271,3 +272,3 @@ | ||
var hexArgs = args.map(_processSolidityEncodePackedArgs); | ||
var hexArgs = _.map(args, _processSolidityEncodePackedArgs); | ||
@@ -274,0 +275,0 @@ return '0x'+ hexArgs.join('').toLowerCase(); |
@@ -23,9 +23,11 @@ /* | ||
var _ = require('underscore'); | ||
var BN = require('bn.js'); | ||
var numberToBN = require('number-to-bn'); | ||
var utf8 = require('utf8'); | ||
var ethereumjsUtil = require('@ethereumjs/util'); | ||
var Hash = require("eth-lib/lib/hash"); | ||
var ethereumBloomFilters = require('ethereum-bloom-filters'); | ||
var {keccak256} = require('ethereum-cryptography/keccak.js'); | ||
/** | ||
@@ -212,3 +214,3 @@ * Returns true if object is BN, otherwise false | ||
for (var i=0; i < l; i+=2) { | ||
code = parseInt(hex.slice(i, i + 2), 16); | ||
code = parseInt(hex.substr(i, 2), 16); | ||
// if (code !== 0) { | ||
@@ -224,11 +226,9 @@ str += String.fromCharCode(code); | ||
/** | ||
* Converts value to it's number representation. | ||
* However, if the value is larger than the maximum safe integer, returns the value as a string. | ||
* Converts value to it's number representation | ||
* | ||
* @method hexToNumber | ||
* @param {String|Number|BN} value | ||
* @param {Boolean} bigIntOnOverflow - if true, return the hex value in case of overflow | ||
* @return {Number|String} | ||
* @return {String} | ||
*/ | ||
var hexToNumber = function (value, bigIntOnOverflow = false) { | ||
var hexToNumber = function (value) { | ||
if (!value) { | ||
@@ -238,11 +238,7 @@ return value; | ||
if (typeof value === 'string' && !isHexStrict(value)) { | ||
if (_.isString(value) && !isHexStrict(value)) { | ||
throw new Error('Given value "'+value+'" is not a valid hex string.'); | ||
} | ||
const n = toBN(value); | ||
if (bigIntOnOverflow && (n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER)) { | ||
return BigInt(n); | ||
} | ||
return n.toNumber(); | ||
return toBN(value).toNumber(); | ||
}; | ||
@@ -260,3 +256,3 @@ | ||
if (typeof value === 'string' && !isHexStrict(value)) { | ||
if (_.isString(value) && !isHexStrict(value)) { | ||
throw new Error('Given value "'+value+'" is not a valid hex string.'); | ||
@@ -277,3 +273,3 @@ } | ||
var numberToHex = function (value) { | ||
if ((value === null || value === undefined)) { | ||
if (_.isNull(value) || _.isUndefined(value)) { | ||
return value; | ||
@@ -289,3 +285,3 @@ } | ||
return number.lt(new BN(0)) ? '-0x' + result.slice(1) : '0x' + result; | ||
return number.lt(new BN(0)) ? '-0x' + result.substr(1) : '0x' + result; | ||
}; | ||
@@ -332,3 +328,3 @@ | ||
for (var bytes = [], c = 0; c < hex.length; c += 2) | ||
bytes.push(parseInt(hex.slice(c, c + 2), 16)); | ||
bytes.push(parseInt(hex.substr(c, 2), 16)); | ||
return bytes; | ||
@@ -354,3 +350,3 @@ }; | ||
if (typeof value === 'boolean' ) { | ||
if (_.isBoolean(value)) { | ||
return returnType ? 'bool' : value ? '0x01' : '0x00'; | ||
@@ -363,3 +359,3 @@ } | ||
if (typeof value === 'object' && !!value && !isBigNumber(value) && !isBN(value)) { | ||
if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) { | ||
return returnType ? 'string' : utf8ToHex(JSON.stringify(value)); | ||
@@ -369,3 +365,3 @@ } | ||
// if its a negative number, pass it through numberToHex | ||
if (typeof value === 'string') { | ||
if (_.isString(value)) { | ||
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) { | ||
@@ -392,3 +388,3 @@ return returnType ? 'int256' : numberToHex(value); | ||
var isHexStrict = function (hex) { | ||
return ((typeof hex === 'string' || typeof hex === 'number') && /^(-)?0x[0-9a-f]*$/i.test(hex)); | ||
return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex)); | ||
}; | ||
@@ -404,3 +400,3 @@ | ||
var isHex = function (hex) { | ||
return ((typeof hex === 'string' || typeof hex === 'number') && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); | ||
return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); | ||
}; | ||
@@ -511,9 +507,6 @@ | ||
if (isHexStrict(value) && /^0x/i.test((value).toString())) { | ||
value = ethereumjsUtil.toBuffer(value); | ||
} else if (typeof value === 'string') { | ||
// Assume value is an arbitrary string | ||
value = Buffer.from(value, 'utf-8'); | ||
value = hexToBytes(value); | ||
} | ||
var returnValue = ethereumjsUtil.bufferToHex(keccak256(value)); | ||
var returnValue = Hash.keccak256(value); // jshint ignore:line | ||
@@ -527,3 +520,3 @@ if(returnValue === SHA3_NULL_S) { | ||
// expose the under the hood keccak256 | ||
sha3._Hash = keccak256; | ||
sha3._Hash = Hash; | ||
@@ -547,31 +540,5 @@ /** | ||
/** | ||
* Auto converts any given value into it's hex representation, | ||
* then converts hex to number. | ||
* | ||
* @method toNumber | ||
* @param {String|Number|BN} value | ||
* @param {Boolean} bigIntOnOverflow - if true, return the hex value in case of overflow | ||
* @return {Number|String} | ||
*/ | ||
var toNumber = function (value, bigIntOnOverflow = false) { | ||
return typeof value === 'number' ? value : hexToNumber(toHex(value), bigIntOnOverflow); | ||
} | ||
// 1.x currently accepts 0x... strings, bn.js after update doesn't. it would be a breaking change | ||
var BNwrapped = function (value) { | ||
// check negative | ||
if (typeof value == "string" && value.includes("0x")) { | ||
const [negative, hexValue] = value.toLocaleLowerCase().startsWith('-') ? ["-", value.slice(3)] : ["", value.slice(2)]; | ||
return new BN(negative + hexValue, 16); | ||
} | ||
else { | ||
return new BN(value); | ||
} | ||
}; | ||
Object.setPrototypeOf(BNwrapped, BN); | ||
Object.setPrototypeOf(BNwrapped.prototype, BN.prototype); | ||
module.exports = { | ||
BN: BNwrapped, | ||
BN: BN, | ||
isBN: isBN, | ||
@@ -603,4 +570,3 @@ isBigNumber: isBigNumber, | ||
sha3: sha3, | ||
sha3Raw: sha3Raw, | ||
toNumber: toNumber | ||
sha3Raw: sha3Raw | ||
}; |
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"esModuleInterop": true | ||
"outDir": "./lib" | ||
}, | ||
@@ -10,2 +9,2 @@ "include": [ | ||
] | ||
} | ||
} |
@@ -83,3 +83,3 @@ /* | ||
export function numberToHex(value: number | string | BN): string; | ||
export function checkAddressChecksum(address: string): boolean; | ||
export function checkAddressChecksum(address: string, chainId?: number): boolean; | ||
export function fromAscii(string: string): string; | ||
@@ -90,3 +90,3 @@ export function fromDecimal(value: string | number): string; | ||
export function hexToBytes(hex: Hex): number[]; | ||
export function hexToNumber(hex: Hex,bigIntOnOverflow?: boolean): number | string; | ||
export function hexToNumber(hex: Hex): number; | ||
export function hexToNumberString(hex: Hex): string; | ||
@@ -100,8 +100,8 @@ export function hexToString(hex: Hex): string; | ||
export function padRight(string: string | number, characterAmount: number, sign?: string): string; | ||
export function sha3(value: string | BN | Buffer): string | null; | ||
export function sha3Raw(value: string | BN | Buffer): string; | ||
export function sha3(value: string | BN): string | null; | ||
export function sha3Raw(value: string | BN): string; | ||
export function randomHex(bytesSize: number): string; | ||
export function utf8ToHex(string: string): string; | ||
export function stringToHex(string: string): string; | ||
export function toChecksumAddress(address: string): string; | ||
export function toChecksumAddress(address: string, chainId?: number): string; | ||
export function toDecimal(hex: Hex): number; | ||
@@ -128,3 +128,2 @@ export function toHex(value: number | string | BN): string; | ||
export function stripHexPrefix(str: string): string; | ||
export function toNumber(value: number | string | BN, bigIntOnOverflow?: boolean): number | string; | ||
@@ -151,3 +150,3 @@ // interfaces | ||
hexToBytes(hex: Hex): number[]; | ||
hexToNumber(hex: Hex, bigIntOnOverflow?: boolean): number | string; | ||
hexToNumber(hex: Hex): number; | ||
hexToNumberString(hex: Hex): string; | ||
@@ -165,3 +164,3 @@ hexToString(hex: Hex): string; | ||
stringToHex(string: string): string; | ||
toChecksumAddress(address: string): string; | ||
toChecksumAddress(address: string, chainId?: number): string; | ||
toDecimal(hex: Hex): number; | ||
@@ -178,3 +177,3 @@ toHex(value: number | string | BN): string; | ||
isTopic(topic: string): boolean; | ||
_jsonInterfaceMethodToString(abiItem: AbiItem): string; | ||
jsonInterfaceMethodToString(abiItem: AbiItem): string; | ||
soliditySha3(...val: Mixed[]): string | null; | ||
@@ -189,3 +188,2 @@ soliditySha3Raw(...val: Mixed[]): string; | ||
stripHexPrefix(str: string): string; | ||
toNumber(value: number | string | BN, bigIntOnOverflow?: boolean): number | string; | ||
} | ||
@@ -223,3 +221,3 @@ | ||
export type AbiType = 'function' | 'constructor' | 'event' | 'fallback' | 'receive'; | ||
export type AbiType = 'function' | 'constructor' | 'event' | 'fallback'; | ||
export type StateMutabilityType = 'pure' | 'view' | 'nonpayable' | 'payable'; | ||
@@ -226,0 +224,0 @@ |
@@ -28,7 +28,7 @@ /* | ||
checkAddressChecksum('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51'); | ||
// $ExpectError | ||
// $ExpectType boolean | ||
checkAddressChecksum('0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359', 31); | ||
// $ExpectError | ||
// $ExpectType boolean | ||
checkAddressChecksum('0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359', undefined); | ||
// $ExpectError | ||
@@ -35,0 +35,0 @@ checkAddressChecksum([4]); |
@@ -26,10 +26,6 @@ /* | ||
// $ExpectType string | number | ||
// $ExpectType number | ||
hexToNumber('232'); | ||
// $ExpectType string | number | ||
// $ExpectType number | ||
hexToNumber(232); | ||
// $ExpectType string | number | ||
hexToNumber('0x1fffffffffffff'); // the max safe number 2 ^ 53 | ||
// $ExpectType string | number | ||
hexToNumber('0x20000000000000', true); // larger than the 2 ^ 53 (unsafe) | ||
@@ -36,0 +32,0 @@ // $ExpectError |
@@ -29,4 +29,2 @@ /* | ||
// $ExpectType string | ||
sha3Raw(Buffer.from('123')); | ||
// $ExpectType string | ||
sha3Raw(new BN(3)); | ||
@@ -33,0 +31,0 @@ |
@@ -29,4 +29,2 @@ /* | ||
// $ExpectType string | null | ||
sha3(Buffer.from('123')); | ||
// $ExpectType string | null | ||
sha3(new BN(3)); | ||
@@ -33,0 +31,0 @@ |
@@ -28,2 +28,6 @@ /* | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51'); | ||
// $ExpectType string | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', 31); | ||
// $ExpectType string | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', undefined); | ||
@@ -46,1 +50,13 @@ // $ExpectError | ||
toChecksumAddress(undefined); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', [4]); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', ['string']); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', new BN(3)); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', {}); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', true); | ||
// $ExpectError | ||
toChecksumAddress('0x8ee7f17bb3f88b01247c21ab6603880b64ae53e811f5e01138822e558cf1ab51', null); |
161702
-3.34%67
-1.47%4743
-2.43%44
-8.33%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated