@chainsafe/lodestar-utils
Advanced tools
Comparing version 0.6.0 to 0.7.0
/// <reference types="node" /> | ||
declare type Endianness = "le" | "be"; | ||
/** | ||
* Return a byte array from a number or BigInt | ||
*/ | ||
export declare function intToBytes(value: bigint | number, length: number): Buffer; | ||
export declare function intToBytes(value: bigint | number, length: number, endianness?: Endianness): Buffer; | ||
/** | ||
* Convert byte array in LE to integer. | ||
*/ | ||
export declare function bytesToInt(value: Uint8Array): number; | ||
export declare function bytesToBigInt(value: Uint8Array): bigint; | ||
export declare function bigIntToBytes(value: bigint, length: number): Uint8Array; | ||
export declare function bytesToInt(value: Uint8Array, endianness?: Endianness): number; | ||
export declare function bigIntToBytes(value: bigint, length: number, endianness?: Endianness): Buffer; | ||
export declare function bytesToBigInt(value: Uint8Array, endianness?: Endianness): bigint; | ||
export declare function toHex(buffer: Uint8Array): string; | ||
export declare function fromHex(hex: string): Uint8Array; | ||
export {}; |
@@ -8,4 +8,4 @@ "use strict"; | ||
exports.bytesToInt = bytesToInt; | ||
exports.bigIntToBytes = bigIntToBytes; | ||
exports.bytesToBigInt = bytesToBigInt; | ||
exports.bigIntToBytes = bigIntToBytes; | ||
exports.toHex = toHex; | ||
@@ -19,13 +19,4 @@ exports.fromHex = fromHex; | ||
*/ | ||
function intToBytes(value, length) { | ||
if (typeof value === "number" && length <= 6) { | ||
// value is a number and length is at most 6 bytes | ||
const b = Buffer.alloc(length); | ||
b.writeUIntLE(value, 0, length); | ||
return b; | ||
} else { | ||
// value is number and is too large, or a BigInt | ||
value = BigInt(value); | ||
return (0, _bigintBuffer.toBufferLE)(value, length); | ||
} | ||
function intToBytes(value, length, endianness = "le") { | ||
return bigIntToBytes(BigInt(value), length, endianness); | ||
} | ||
@@ -37,20 +28,24 @@ /** | ||
function bytesToInt(value) { | ||
const length = value.length; | ||
let result = 0; | ||
function bytesToInt(value, endianness = "le") { | ||
return Number(bytesToBigInt(value, endianness)); | ||
} | ||
for (let i = 0; i < length; i++) { | ||
result += value[i] * 2 ** (8 * i); | ||
function bigIntToBytes(value, length, endianness = "le") { | ||
if (endianness === "le") { | ||
return (0, _bigintBuffer.toBufferLE)(value, length); | ||
} else if (endianness === "be") { | ||
return (0, _bigintBuffer.toBufferBE)(value, length); | ||
} | ||
return result; | ||
throw new Error("endianness must be either 'le' or 'be'"); | ||
} | ||
function bytesToBigInt(value) { | ||
return (0, _bigintBuffer.toBigIntLE)(value); | ||
} | ||
function bytesToBigInt(value, endianness = "le") { | ||
if (endianness === "le") { | ||
return (0, _bigintBuffer.toBigIntLE)(value); | ||
} else if (endianness === "be") { | ||
return (0, _bigintBuffer.toBigIntBE)(value); | ||
} | ||
function bigIntToBytes(value, length) { | ||
const b = (0, _bigintBuffer.toBufferLE)(value, length); | ||
return new Uint8Array(b.buffer, b.byteOffset, length); | ||
throw new Error("endianness must be either 'le' or 'be'"); | ||
} | ||
@@ -57,0 +52,0 @@ |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"main": "lib/index.js", | ||
@@ -38,3 +38,3 @@ "files": [ | ||
"dependencies": { | ||
"@chainsafe/ssz": "^0.6.2", | ||
"@chainsafe/ssz": "^0.6.4", | ||
"bigint-buffer": "^1.1.5", | ||
@@ -61,3 +61,3 @@ "camelcase": "^5.3.1", | ||
], | ||
"gitHead": "0e426d2e5816b88d2d1508f42a127a4f89082781" | ||
"gitHead": "6d82733185253e922300cbeca7cd56f778b30008" | ||
} |
Sorry, the diff of this file is not supported yet
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
69695
798
Updated@chainsafe/ssz@^0.6.4