UNPKG

@airgap/coinlib-core

Version:

The @airgap/coinlib-core is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

471 lines 16.4 kB
"use strict"; // Copyright (C) 2016 Dmitry Chestnykh // MIT License. See LICENSE file for details. Object.defineProperty(exports, "__esModule", { value: true }); exports.writeFloat64LE = exports.writeFloat64BE = exports.writeFloat32LE = exports.writeFloat32BE = exports.readFloat64LE = exports.readFloat64BE = exports.readFloat32LE = exports.readFloat32BE = exports.writeUintLE = exports.writeUintBE = exports.readUintLE = exports.readUintBE = exports.writeInt64LE = exports.writeUint64LE = exports.writeInt64BE = exports.writeUint64BE = exports.readUint64LE = exports.readInt64LE = exports.readUint64BE = exports.readInt64BE = exports.writeInt32LE = exports.writeUint32LE = exports.writeInt32BE = exports.writeUint32BE = exports.readUint32LE = exports.readInt32LE = exports.readUint32BE = exports.readInt32BE = exports.writeInt16LE = exports.writeUint16LE = exports.writeInt16BE = exports.writeUint16BE = exports.readUint16LE = exports.readInt16LE = exports.readUint16BE = exports.readInt16BE = void 0; /** * Package binary provides functions for encoding and decoding numbers in byte arrays. */ var int_1 = require("../../../../@stablelib/int-1.0.1/packages/int/int"); // TODO(dchest): add asserts for correct value ranges and array offsets. /** * Reads 2 bytes from array starting at offset as big-endian * signed 16-bit integer and returns it. */ function readInt16BE(array, offset) { if (offset === void 0) { offset = 0; } return (((array[offset + 0] << 8) | array[offset + 1]) << 16) >> 16; } exports.readInt16BE = readInt16BE; /** * Reads 2 bytes from array starting at offset as big-endian * unsigned 16-bit integer and returns it. */ function readUint16BE(array, offset) { if (offset === void 0) { offset = 0; } return ((array[offset + 0] << 8) | array[offset + 1]) >>> 0; } exports.readUint16BE = readUint16BE; /** * Reads 2 bytes from array starting at offset as little-endian * signed 16-bit integer and returns it. */ function readInt16LE(array, offset) { if (offset === void 0) { offset = 0; } return (((array[offset + 1] << 8) | array[offset]) << 16) >> 16; } exports.readInt16LE = readInt16LE; /** * Reads 2 bytes from array starting at offset as little-endian * unsigned 16-bit integer and returns it. */ function readUint16LE(array, offset) { if (offset === void 0) { offset = 0; } return ((array[offset + 1] << 8) | array[offset]) >>> 0; } exports.readUint16LE = readUint16LE; /** * Writes 2-byte big-endian representation of 16-bit unsigned * value to byte array starting at offset. * * If byte array is not given, creates a new 2-byte one. * * Returns the output byte array. */ function writeUint16BE(value, out, offset) { if (out === void 0) { out = new Uint8Array(2); } if (offset === void 0) { offset = 0; } out[offset + 0] = value >>> 8; out[offset + 1] = value >>> 0; return out; } exports.writeUint16BE = writeUint16BE; exports.writeInt16BE = writeUint16BE; /** * Writes 2-byte little-endian representation of 16-bit unsigned * value to array starting at offset. * * If byte array is not given, creates a new 2-byte one. * * Returns the output byte array. */ function writeUint16LE(value, out, offset) { if (out === void 0) { out = new Uint8Array(2); } if (offset === void 0) { offset = 0; } out[offset + 0] = value >>> 0; out[offset + 1] = value >>> 8; return out; } exports.writeUint16LE = writeUint16LE; exports.writeInt16LE = writeUint16LE; /** * Reads 4 bytes from array starting at offset as big-endian * signed 32-bit integer and returns it. */ function readInt32BE(array, offset) { if (offset === void 0) { offset = 0; } return (array[offset] << 24) | (array[offset + 1] << 16) | (array[offset + 2] << 8) | array[offset + 3]; } exports.readInt32BE = readInt32BE; /** * Reads 4 bytes from array starting at offset as big-endian * unsigned 32-bit integer and returns it. */ function readUint32BE(array, offset) { if (offset === void 0) { offset = 0; } return ((array[offset] << 24) | (array[offset + 1] << 16) | (array[offset + 2] << 8) | array[offset + 3]) >>> 0; } exports.readUint32BE = readUint32BE; /** * Reads 4 bytes from array starting at offset as little-endian * signed 32-bit integer and returns it. */ function readInt32LE(array, offset) { if (offset === void 0) { offset = 0; } return (array[offset + 3] << 24) | (array[offset + 2] << 16) | (array[offset + 1] << 8) | array[offset]; } exports.readInt32LE = readInt32LE; /** * Reads 4 bytes from array starting at offset as little-endian * unsigned 32-bit integer and returns it. */ function readUint32LE(array, offset) { if (offset === void 0) { offset = 0; } return ((array[offset + 3] << 24) | (array[offset + 2] << 16) | (array[offset + 1] << 8) | array[offset]) >>> 0; } exports.readUint32LE = readUint32LE; /** * Writes 4-byte big-endian representation of 32-bit unsigned * value to byte array starting at offset. * * If byte array is not given, creates a new 4-byte one. * * Returns the output byte array. */ function writeUint32BE(value, out, offset) { if (out === void 0) { out = new Uint8Array(4); } if (offset === void 0) { offset = 0; } out[offset + 0] = value >>> 24; out[offset + 1] = value >>> 16; out[offset + 2] = value >>> 8; out[offset + 3] = value >>> 0; return out; } exports.writeUint32BE = writeUint32BE; exports.writeInt32BE = writeUint32BE; /** * Writes 4-byte little-endian representation of 32-bit unsigned * value to array starting at offset. * * If byte array is not given, creates a new 4-byte one. * * Returns the output byte array. */ function writeUint32LE(value, out, offset) { if (out === void 0) { out = new Uint8Array(4); } if (offset === void 0) { offset = 0; } out[offset + 0] = value >>> 0; out[offset + 1] = value >>> 8; out[offset + 2] = value >>> 16; out[offset + 3] = value >>> 24; return out; } exports.writeUint32LE = writeUint32LE; exports.writeInt32LE = writeUint32LE; /** * Reads 8 bytes from array starting at offset as big-endian * signed 64-bit integer and returns it. * * IMPORTANT: due to JavaScript limitation, supports exact * numbers in range -9007199254740991 to 9007199254740991. * If the number stored in the byte array is outside this range, * the result is not exact. */ function readInt64BE(array, offset) { if (offset === void 0) { offset = 0; } var hi = readInt32BE(array, offset); var lo = readInt32BE(array, offset + 4); return hi * 0x100000000 + lo - ((lo >> 31) * 0x100000000); } exports.readInt64BE = readInt64BE; /** * Reads 8 bytes from array starting at offset as big-endian * unsigned 64-bit integer and returns it. * * IMPORTANT: due to JavaScript limitation, supports values up to 2^53-1. */ function readUint64BE(array, offset) { if (offset === void 0) { offset = 0; } var hi = readUint32BE(array, offset); var lo = readUint32BE(array, offset + 4); return hi * 0x100000000 + lo; } exports.readUint64BE = readUint64BE; /** * Reads 8 bytes from array starting at offset as little-endian * signed 64-bit integer and returns it. * * IMPORTANT: due to JavaScript limitation, supports exact * numbers in range -9007199254740991 to 9007199254740991. * If the number stored in the byte array is outside this range, * the result is not exact. */ function readInt64LE(array, offset) { if (offset === void 0) { offset = 0; } var lo = readInt32LE(array, offset); var hi = readInt32LE(array, offset + 4); return hi * 0x100000000 + lo - ((lo >> 31) * 0x100000000); } exports.readInt64LE = readInt64LE; /** * Reads 8 bytes from array starting at offset as little-endian * unsigned 64-bit integer and returns it. * * IMPORTANT: due to JavaScript limitation, supports values up to 2^53-1. */ function readUint64LE(array, offset) { if (offset === void 0) { offset = 0; } var lo = readUint32LE(array, offset); var hi = readUint32LE(array, offset + 4); return hi * 0x100000000 + lo; } exports.readUint64LE = readUint64LE; /** * Writes 8-byte big-endian representation of 64-bit unsigned * value to byte array starting at offset. * * Due to JavaScript limitation, supports values up to 2^53-1. * * If byte array is not given, creates a new 8-byte one. * * Returns the output byte array. */ function writeUint64BE(value, out, offset) { if (out === void 0) { out = new Uint8Array(8); } if (offset === void 0) { offset = 0; } writeUint32BE(value / 0x100000000 >>> 0, out, offset); writeUint32BE(value >>> 0, out, offset + 4); return out; } exports.writeUint64BE = writeUint64BE; exports.writeInt64BE = writeUint64BE; /** * Writes 8-byte little-endian representation of 64-bit unsigned * value to byte array starting at offset. * * Due to JavaScript limitation, supports values up to 2^53-1. * * If byte array is not given, creates a new 8-byte one. * * Returns the output byte array. */ function writeUint64LE(value, out, offset) { if (out === void 0) { out = new Uint8Array(8); } if (offset === void 0) { offset = 0; } writeUint32LE(value >>> 0, out, offset); writeUint32LE(value / 0x100000000 >>> 0, out, offset + 4); return out; } exports.writeUint64LE = writeUint64LE; exports.writeInt64LE = writeUint64LE; /** * Reads bytes from array starting at offset as big-endian * unsigned bitLen-bit integer and returns it. * * Supports bit lengths divisible by 8, up to 48. */ function readUintBE(bitLength, array, offset) { if (offset === void 0) { offset = 0; } // TODO(dchest): implement support for bitLengths non-divisible by 8 if (bitLength % 8 !== 0) { throw new Error("readUintBE supports only bitLengths divisible by 8"); } if (bitLength / 8 > array.length - offset) { throw new Error("readUintBE: array is too short for the given bitLength"); } var result = 0; var mul = 1; for (var i = bitLength / 8 + offset - 1; i >= offset; i--) { result += array[i] * mul; mul *= 256; } return result; } exports.readUintBE = readUintBE; /** * Reads bytes from array starting at offset as little-endian * unsigned bitLen-bit integer and returns it. * * Supports bit lengths divisible by 8, up to 48. */ function readUintLE(bitLength, array, offset) { if (offset === void 0) { offset = 0; } // TODO(dchest): implement support for bitLengths non-divisible by 8 if (bitLength % 8 !== 0) { throw new Error("readUintLE supports only bitLengths divisible by 8"); } if (bitLength / 8 > array.length - offset) { throw new Error("readUintLE: array is too short for the given bitLength"); } var result = 0; var mul = 1; for (var i = offset; i < offset + bitLength / 8; i++) { result += array[i] * mul; mul *= 256; } return result; } exports.readUintLE = readUintLE; /** * Writes a big-endian representation of bitLen-bit unsigned * value to array starting at offset. * * Supports bit lengths divisible by 8, up to 48. * * If byte array is not given, creates a new one. * * Returns the output byte array. */ function writeUintBE(bitLength, value, out, offset) { if (out === void 0) { out = new Uint8Array(bitLength / 8); } if (offset === void 0) { offset = 0; } // TODO(dchest): implement support for bitLengths non-divisible by 8 if (bitLength % 8 !== 0) { throw new Error("writeUintBE supports only bitLengths divisible by 8"); } if (!(0, int_1.isSafeInteger)(value)) { throw new Error("writeUintBE value must be an integer"); } var div = 1; for (var i = bitLength / 8 + offset - 1; i >= offset; i--) { out[i] = (value / div) & 0xff; div *= 256; } return out; } exports.writeUintBE = writeUintBE; /** * Writes a little-endian representation of bitLen-bit unsigned * value to array starting at offset. * * Supports bit lengths divisible by 8, up to 48. * * If byte array is not given, creates a new one. * * Returns the output byte array. */ function writeUintLE(bitLength, value, out, offset) { if (out === void 0) { out = new Uint8Array(bitLength / 8); } if (offset === void 0) { offset = 0; } // TODO(dchest): implement support for bitLengths non-divisible by 8 if (bitLength % 8 !== 0) { throw new Error("writeUintLE supports only bitLengths divisible by 8"); } if (!(0, int_1.isSafeInteger)(value)) { throw new Error("writeUintLE value must be an integer"); } var div = 1; for (var i = offset; i < offset + bitLength / 8; i++) { out[i] = (value / div) & 0xff; div *= 256; } return out; } exports.writeUintLE = writeUintLE; /** * Reads 4 bytes from array starting at offset as big-endian * 32-bit floating-point number and returns it. */ function readFloat32BE(array, offset) { if (offset === void 0) { offset = 0; } var view = new DataView(array.buffer, array.byteOffset, array.byteLength); return view.getFloat32(offset); } exports.readFloat32BE = readFloat32BE; /** * Reads 4 bytes from array starting at offset as little-endian * 32-bit floating-point number and returns it. */ function readFloat32LE(array, offset) { if (offset === void 0) { offset = 0; } var view = new DataView(array.buffer, array.byteOffset, array.byteLength); return view.getFloat32(offset, true); } exports.readFloat32LE = readFloat32LE; /** * Reads 8 bytes from array starting at offset as big-endian * 64-bit floating-point number ("double") and returns it. */ function readFloat64BE(array, offset) { if (offset === void 0) { offset = 0; } var view = new DataView(array.buffer, array.byteOffset, array.byteLength); return view.getFloat64(offset); } exports.readFloat64BE = readFloat64BE; /** * Reads 8 bytes from array starting at offset as little-endian * 64-bit floating-point number ("double") and returns it. */ function readFloat64LE(array, offset) { if (offset === void 0) { offset = 0; } var view = new DataView(array.buffer, array.byteOffset, array.byteLength); return view.getFloat64(offset, true); } exports.readFloat64LE = readFloat64LE; /** * Writes 4-byte big-endian floating-point representation of value * to byte array starting at offset. * * If byte array is not given, creates a new 4-byte one. * * Returns the output byte array. */ function writeFloat32BE(value, out, offset) { if (out === void 0) { out = new Uint8Array(4); } if (offset === void 0) { offset = 0; } var view = new DataView(out.buffer, out.byteOffset, out.byteLength); view.setFloat32(offset, value); return out; } exports.writeFloat32BE = writeFloat32BE; /** * Writes 4-byte little-endian floating-point representation of value * to byte array starting at offset. * * If byte array is not given, creates a new 4-byte one. * * Returns the output byte array. */ function writeFloat32LE(value, out, offset) { if (out === void 0) { out = new Uint8Array(4); } if (offset === void 0) { offset = 0; } var view = new DataView(out.buffer, out.byteOffset, out.byteLength); view.setFloat32(offset, value, true); return out; } exports.writeFloat32LE = writeFloat32LE; /** * Writes 8-byte big-endian floating-point representation of value * to byte array starting at offset. * * If byte array is not given, creates a new 8-byte one. * * Returns the output byte array. */ function writeFloat64BE(value, out, offset) { if (out === void 0) { out = new Uint8Array(8); } if (offset === void 0) { offset = 0; } var view = new DataView(out.buffer, out.byteOffset, out.byteLength); view.setFloat64(offset, value); return out; } exports.writeFloat64BE = writeFloat64BE; /** * Writes 8-byte little-endian floating-point representation of value * to byte array starting at offset. * * If byte array is not given, creates a new 8-byte one. * * Returns the output byte array. */ function writeFloat64LE(value, out, offset) { if (out === void 0) { out = new Uint8Array(8); } if (offset === void 0) { offset = 0; } var view = new DataView(out.buffer, out.byteOffset, out.byteLength); view.setFloat64(offset, value, true); return out; } exports.writeFloat64LE = writeFloat64LE; //# sourceMappingURL=binary.js.map