'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
let converter;
/**
* Convert a little-endian buffer into a BigInt.
* @param buf The little-endian buffer to convert
* @returns A BigInt with the little-endian representation of buf.
*/
function toBigIntLE(buf) {
{
const reversed = Buffer.from(buf);
reversed.reverse();
const hex = reversed.toString('hex');
if (hex.length === 0) {
return BigInt(0);
}
return BigInt(`0x${hex}`);
}
return converter.toBigInt(buf, false);
}
exports.toBigIntLE = toBigIntLE;
/**
* Convert a big-endian buffer into a BigInt
* @param buf The big-endian buffer to convert.
* @returns A BigInt with the big-endian representation of buf.
*/
function toBigIntBE(buf) {
{
const hex = buf.toString('hex');
if (hex.length === 0) {
return BigInt(0);
}
return BigInt(`0x${hex}`);
}
return converter.toBigInt(buf, true);
}
exports.toBigIntBE = toBigIntBE;
/**
* Convert a BigInt to a little-endian buffer.
* @param num The BigInt to convert.
* @param width The number of bytes that the resulting buffer should be.
* @returns A little-endian buffer representation of num.
*/
function toBufferLE(num, width) {
{
const hex = num.toString(16);
const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
buffer.reverse();
return buffer;
}
// Allocation is done here, since it is slower using napi in C
return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
}
exports.toBufferLE = toBufferLE;
/**
* Convert a BigInt to a big-endian buffer.
* @param num The BigInt to convert.
* @param width The number of bytes that the resulting buffer should be.
* @returns A big-endian buffer representation of num.
*/
function toBufferBE(num, width) {
{
const hex = num.toString(16);
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
}
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
}
exports.toBufferBE = toBufferBE;