ton3-core
Version:
TON low-level API tools
43 lines • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bitsToBigInt = exports.bitsToBigUint = exports.bitsToIntUint = void 0;
const bitsToBigUint = (bits) => {
if (!bits.length)
return { value: 0n, isSafe: true };
const value = bits
.slice()
.reverse()
.reduce((acc, bit, i) => (BigInt(bit) * (2n ** BigInt(i)) + acc), 0n);
const isSafe = value <= Number.MAX_SAFE_INTEGER;
return {
value,
isSafe
};
};
exports.bitsToBigUint = bitsToBigUint;
const bitsToBigInt = (bits) => {
if (!bits.length)
return { value: 0n, isSafe: true };
const { value: uint } = bitsToBigUint(bits);
const size = BigInt(bits.length);
const int = 1n << (size - 1n);
const value = uint >= int ? (uint - (int * 2n)) : uint;
const isSafe = value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER;
return {
value,
isSafe
};
};
exports.bitsToBigInt = bitsToBigInt;
const bitsToIntUint = (bits, options) => {
const { type = 'uint' } = options;
const { value, isSafe } = type === 'uint'
? bitsToBigUint(bits)
: bitsToBigInt(bits);
if (!isSafe) {
throw new Error('loaded value does not fit max/min safe integer value, use alternative BigInt methods');
}
return Number(value);
};
exports.bitsToIntUint = bitsToIntUint;
//# sourceMappingURL=numbers.js.map