@arcblock/forge-util
Advanced tools
Comparing version 0.29.3 to 0.30.0
// Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme) | ||
import { Buffer } from 'node/globals'; | ||
/** | ||
@@ -12,2 +13,6 @@ * Validates if a value is an Uint8Array. | ||
declare function isUint8Array(value: any): boolean; | ||
declare function toUint8Array(v: any, autoHex?: boolean): Uint8Array; | ||
declare function toBuffer(v: any, autoHex?: boolean): Buffer; | ||
declare function toBase58(v: any, autoHex?: boolean): any; | ||
declare function fromBase58(v: any): any; | ||
/** | ||
@@ -49,2 +54,6 @@ * Generate a random UUID | ||
toBN: (number: any) => any; | ||
toUint8Array: typeof toUint8Array; | ||
toBuffer: typeof toBuffer; | ||
toBase58: typeof toBase58; | ||
fromBase58: typeof fromBase58; | ||
UUID: typeof UUID; | ||
@@ -51,0 +60,0 @@ isUUID: typeof isUUID; |
@@ -16,2 +16,3 @@ /* eslint-disable no-bitwise */ | ||
const numberToBN = require('number-to-bn'); | ||
const multibase = require('multibase'); | ||
const utf8 = require('utf8'); | ||
@@ -100,3 +101,3 @@ const BN = require('bn.js'); | ||
*/ | ||
const isHex = hex => (isString(hex) || isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex); | ||
const isHex = hex => (isString(hex) || isNumber(hex)) && /^(-0x|0x|0X|-0X)?[0-9a-f]*$/i.test(hex); | ||
@@ -318,2 +319,10 @@ /** | ||
const toHex = (value, returnType) => { | ||
if (isUint8Array(value) || Buffer.isBuffer(value)) { | ||
return returnType ? 'bytes' : bytesToHex(value); | ||
} | ||
if (multibase.isEncoded(value) === 'base58btc') { | ||
return returnType ? 'bytes' : bytesToHex(multibase.decode(value)); | ||
} | ||
if (isBoolean(value)) { | ||
@@ -509,2 +518,86 @@ // eslint-disable-next-line no-nested-ternary | ||
/** | ||
* Convert input to Uint8Array on best effort | ||
* | ||
* @param {buffer|base58|hex|Uint8Array} v | ||
* @param {boolean} [autoHex=false] | ||
* @param {boolean} [enforceStrictHex=false] | ||
* @returns {Uint8Array} | ||
* @throws {Error} | ||
*/ | ||
function toUint8Array(v, autoHex = false, enforceStrictHex = false) { | ||
let vb = null; | ||
if (Buffer.isBuffer(v)) { | ||
vb = Uint8Array.from(v); | ||
} else if (isHex(v)) { | ||
if (!isHexStrict(v)) { | ||
if (enforceStrictHex) { | ||
throw new Error('toUint8Array expect strict hex encoded string'); | ||
} | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'It seems you provided an hex encoded string without `0x` prefix for toUint8Array' | ||
); | ||
} | ||
vb = Uint8Array.from(hexToBytes(v)); | ||
} else if (isUint8Array(v)) { | ||
vb = Uint8Array.from(v); | ||
} else if (multibase.isEncoded(v) === 'base58btc') { | ||
vb = Uint8Array.from(multibase.decode(v)); | ||
} else if (autoHex) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'It seems you provided an non-supported input to toUint8Array, its converted to hex implicitly' | ||
); | ||
vb = Uint8Array.from(hexToBytes(toHex(v))); | ||
} else { | ||
throw new Error( | ||
'Unsupported input type detected for toBuffer, only Uint8Array/Buffer/Hex/Base58 supported' | ||
); | ||
} | ||
return vb; | ||
} | ||
/** | ||
* Convert input to Buffer on best effort | ||
* | ||
* @param {buffer|base58|hex|Uint8Array} v | ||
* @param {boolean} [autoHex=false] | ||
* @param {boolean} [enforceStrictHex=false] | ||
* @returns {buffer} | ||
* @throws {Error} | ||
*/ | ||
function toBuffer(v, autoHex = false, enforceStrictHex = false) { | ||
return Buffer.from(toUint8Array(v, autoHex, enforceStrictHex)); | ||
} | ||
/** | ||
* Convert input to base58btc format on best effort | ||
* | ||
* @param {buffer|base58|hex|Uint8Array} v | ||
* @param {boolean} [autoHex=false] | ||
* @param {boolean} [enforceStrictHex=false] | ||
* @returns {string} | ||
* @throws {Error} | ||
*/ | ||
function toBase58(v, autoHex = false, enforceStrictHex = false) { | ||
return multibase.encode('base58btc', toBuffer(v, autoHex, enforceStrictHex)).toString(); | ||
} | ||
/** | ||
* Decode base58 string | ||
* | ||
* @param {string} v | ||
* @returns {buffer} | ||
*/ | ||
function fromBase58(v) { | ||
if (!multibase.isEncoded(v) === 'base58btc') { | ||
throw new Error('fromBase58 expect strict base58 encoded string format'); | ||
} | ||
return multibase.decode(v); | ||
} | ||
module.exports = { | ||
@@ -531,4 +624,8 @@ isBN, | ||
toBN, | ||
toUint8Array, | ||
toBuffer, | ||
toBase58, | ||
fromBase58, | ||
UUID, | ||
isUUID, | ||
}; |
{ | ||
"name": "@arcblock/forge-util", | ||
"version": "0.29.3", | ||
"version": "0.30.0", | ||
"description": "utils shared across mutlipe forge js libs, works in both node.js and browser", | ||
@@ -18,2 +18,3 @@ "keywords": [ | ||
"lodash": "^4.17.11", | ||
"multibase": "^0.6.0", | ||
"number-to-bn": "^1.7.0", | ||
@@ -55,3 +56,3 @@ "utf8": "^3.0.0" | ||
}, | ||
"gitHead": "7df2a2535997bb7606e9ada527d103f47fa698b8" | ||
"gitHead": "2d1a961d0e07e7014a170b1e7e9f491c0ec58f6e" | ||
} |
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
19284
613
5
+ Addedmultibase@^0.6.0
+ Addedbase-x@3.0.10(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedmultibase@0.6.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)