UNPKG

low-level

Version:
106 lines 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Uint256 = exports.Uint128 = exports.Uint96 = exports.Uint64 = exports.AbstractBigUint = exports.Uint32 = exports.Uint16 = exports.Uint8 = exports.FixedUint = void 0; const uint_js_1 = require("./uint.js"); const utils_js_1 = require("./utils.js"); class FixedUint extends uint_js_1.Uint { static byteLength; constructor(buffer) { super(buffer); } static create(input) { return new this(utils_js_1.UintUtils.fixBufferByteLength((input instanceof uint_js_1.Uint ? input.getRaw() : input), this.byteLength)); } static alloc(fill) { return new this(Buffer.alloc(this.byteLength, fill)); } static empty() { return this.alloc(); } static from(input, arg2, arg3) { let uint; let buffer; if (typeof input === "number") { uint = this.alloc(); uint.iadd(input); return uint; } else if (typeof input === "string" && arg2 === undefined) { buffer = Buffer.from(input, "hex"); } else { buffer = Buffer.from(input, arg2, arg3); } return new this(utils_js_1.UintUtils.fixBufferByteLength(buffer, this.byteLength)); } } exports.FixedUint = FixedUint; class Uint8 extends FixedUint { static byteLength = 1; } exports.Uint8 = Uint8; class Uint16 extends FixedUint { static byteLength = 2; } exports.Uint16 = Uint16; class Uint32 extends FixedUint { static byteLength = 4; } exports.Uint32 = Uint32; class AbstractBigUint extends FixedUint { addNumber(value) { for (let i = this.buffer.byteLength - 4; i >= 0; i -= 4) { const sum = this.buffer.readUint32BE(i) + value; if (sum >= 0) { this.buffer.writeUint32BE(sum % 4294967296, i); } else { this.buffer.writeUint32BE((sum % 4294967296) + 4294967296, i); } value = Math.floor(sum / 4294967296); } } divNumber(value, returnRest) { let carry = 0; for (let i = 0; i < this.buffer.byteLength; i += 4) { const dividend = this.buffer.readUint32BE(i) + carry; this.buffer.writeUint32BE(Math.floor(dividend / value), i); carry = (dividend % value) * 4294967296; } if (returnRest) return (carry / 4294967296); } toShortUint() { for (let i = 0; i < this.buffer.byteLength; i++) { if (this.buffer[i] !== 0) { return this.slice(i); } } return uint_js_1.Uint.from(0); } } exports.AbstractBigUint = AbstractBigUint; // @ts-ignore class Uint64 extends AbstractBigUint { static byteLength = 8; toBigInt() { return this.buffer.readBigUint64BE(); } } exports.Uint64 = Uint64; // @ts-ignore class Uint96 extends AbstractBigUint { static byteLength = 12; } exports.Uint96 = Uint96; // @ts-ignore class Uint128 extends AbstractBigUint { static byteLength = 16; } exports.Uint128 = Uint128; // @ts-ignore class Uint256 extends AbstractBigUint { static byteLength = 32; } exports.Uint256 = Uint256; //# sourceMappingURL=fixed-uint.js.map