@arcblock/forge-util
Advanced tools
Comparing version 1.5.7 to 1.5.8
@@ -0,1 +1,2 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable no-bitwise */ | ||
@@ -20,3 +21,2 @@ /* eslint-disable implicit-arrow-linebreak */ | ||
const isNull = require('lodash/isNull'); | ||
const numberToBN = require('number-to-bn'); | ||
const multibase = require('multibase'); | ||
@@ -32,2 +32,46 @@ const utf8 = require('utf8'); | ||
/** | ||
* Returns a BN object, converts a number value to a BN | ||
* @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object | ||
* @return {Object} `output` BN object of the number | ||
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number | ||
*/ | ||
const numberToBN = arg => { | ||
if (typeof arg === 'string' || typeof arg === 'number') { | ||
let multiplier = new BN(1); // eslint-disable-line | ||
const formattedString = String(arg) | ||
.toLowerCase() | ||
.trim(); | ||
const isHexPrefixed = formattedString.substr(0, 2) === '0x' || formattedString.substr(0, 3) === '-0x'; | ||
let stringArg = stripHexPrefix(formattedString); // eslint-disable-line | ||
if (stringArg.substr(0, 1) === '-') { | ||
stringArg = stripHexPrefix(stringArg.slice(1)); | ||
multiplier = new BN(-1, 10); | ||
} | ||
stringArg = stringArg === '' ? '0' : stringArg; | ||
if ( | ||
(!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/)) || | ||
stringArg.match(/^[a-fA-F]+$/) || | ||
(isHexPrefixed === true && stringArg.match(/^[0-9A-Fa-f]+$/)) | ||
) { | ||
return new BN(stringArg, 16).mul(multiplier); | ||
} | ||
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === '') && isHexPrefixed === false) { | ||
return new BN(stringArg, 10).mul(multiplier); | ||
} | ||
} else if (typeof arg === 'object' && arg.toString && !arg.pop && !arg.push) { | ||
if (arg.toString(10).match(/^-?[0-9]+$/) && (arg.mul || arg.dividedToIntegerBy)) { | ||
return new BN(arg.toString(10), 10); | ||
} | ||
} | ||
throw new Error( | ||
`[number-to-bn] while converting number ${JSON.stringify( | ||
arg | ||
)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.` | ||
); | ||
}; | ||
/** | ||
* Returns a `boolean` on whether or not the `string` starts with '0x' | ||
@@ -567,3 +611,4 @@ * | ||
function toBase58(v) { | ||
return multibase.encode('base58btc', toBuffer(v)).toString(); | ||
const buf = multibase.encode('base58btc', toUint8Array(v)); | ||
return Buffer.from(buf).toString('utf-8'); | ||
} | ||
@@ -584,3 +629,3 @@ | ||
return multibase.decode(v); | ||
return Buffer.from(multibase.decode(v)); | ||
} | ||
@@ -587,0 +632,0 @@ |
{ | ||
"name": "@arcblock/forge-util", | ||
"version": "1.5.7", | ||
"version": "1.5.8", | ||
"description": "utils shared across mutlipe forge js libs, works in both node.js and browser", | ||
@@ -16,9 +16,12 @@ "keywords": [ | ||
"dependencies": { | ||
"base64-url": "^2.3.2", | ||
"bn.js": "4.11.6", | ||
"base64-url": "^2.3.3", | ||
"bn.js": "5.1.3", | ||
"lodash": "^4.17.20", | ||
"multibase": "^0.6.0", | ||
"number-to-bn": "^1.7.0", | ||
"multibase": "^3.1.1", | ||
"utf8": "^3.0.0" | ||
}, | ||
"resolutions": { | ||
"bn.js": "5.1.3", | ||
"elliptic": "6.5.3" | ||
}, | ||
"devDependencies": { | ||
@@ -59,3 +62,3 @@ "jest": "^24.8.0", | ||
}, | ||
"gitHead": "c3b531936730cbee0d9a0f4e7ccd5ed917a7f4cf" | ||
"gitHead": "b494b9123023e94ff3faf9a05f1f9c2ae62c82cc" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24939
5
772
+ Added@multiformats/base-x@4.0.1(transitive)
+ Added@zxing/text-encoding@0.9.0(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedbn.js@5.1.3(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedmultibase@3.1.2(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedweb-encoding@1.1.5(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removednumber-to-bn@^1.7.0
- Removedbase-x@3.0.10(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbn.js@4.11.6(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedieee754@1.2.1(transitive)
- Removedis-hex-prefixed@1.0.0(transitive)
- Removedmultibase@0.6.1(transitive)
- Removednumber-to-bn@1.7.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstrip-hex-prefix@1.0.0(transitive)
Updatedbase64-url@^2.3.3
Updatedbn.js@5.1.3
Updatedmultibase@^3.1.1