@chainsafe/eth2.0-utils
Version:
Utilities required across multiple lodestar packages
51 lines (42 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.intToBytes = intToBytes;
exports.bytesToInt = bytesToInt;
exports.bytesToBigInt = bytesToBigInt;
exports.toHex = toHex;
var _bigintBuffer = require("bigint-buffer");
/**
* Return a byte array from a number or BigInt
*/
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);
}
}
/**
* Convert byte array in LE to integer.
*/
function bytesToInt(value) {
const length = value.length;
let result = 0;
for (let i = 0; i < length; i++) {
result += value[i] * 2 ** (8 * i);
}
return result;
}
function bytesToBigInt(value) {
return (0, _bigintBuffer.toBigIntLE)(value);
}
function toHex(buffer) {
return "0x" + buffer.toString("hex");
}
//# sourceMappingURL=bytes.js.map