All files / src buffer.ts

84.62% Statements 33/39
83.87% Branches 26/31
80% Functions 4/5
86.84% Lines 33/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                              8x 8x 8x   8x           10x         10x 10x 6x 4x     4x     2x     2x       4x 3x     3x     1x     1x         10x     8x 9x       8x 25x   8x 8x 72x 7x   65x 65x 6x     59x 2x         57x     8x  
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
 
export const BIG_ENDIAN = 'big';
export const LITTLE_ENDIAN = 'little';
const MAX_NUMBER_BYTE_LENGTH = 6;
 
export const intToBuffer = (
	value: number | string,
	byteLength: number,
	endianness: string = BIG_ENDIAN,
	signed: boolean = false,
) => {
	Iif (![BIG_ENDIAN, LITTLE_ENDIAN].includes(endianness)) {
		throw new Error(
			`Endianness must be either ${BIG_ENDIAN} or ${LITTLE_ENDIAN}`,
		);
	}
	const buffer = Buffer.alloc(byteLength);
	if (endianness === 'big') {
		if (byteLength <= MAX_NUMBER_BYTE_LENGTH) {
			Iif (signed) {
				buffer.writeIntBE(Number(value), 0, byteLength);
			} else {
				buffer.writeUIntBE(Number(value), 0, byteLength);
			}
		} else {
			Iif (signed) {
				buffer.writeBigInt64BE(BigInt(value));
			} else {
				buffer.writeBigUInt64BE(BigInt(value));
			}
		}
	} else {
		if (byteLength <= MAX_NUMBER_BYTE_LENGTH) {
			Iif (signed) {
				buffer.writeIntLE(Number(value), 0, byteLength);
			} else {
				buffer.writeUIntLE(Number(value), 0, byteLength);
			}
		} else {
			Iif (signed) {
				buffer.writeBigInt64LE(BigInt(value));
			} else {
				buffer.writeBigUInt64LE(BigInt(value));
			}
		}
	}
 
	return buffer;
};
 
export const bufferToIntAsString = (buffer: Buffer): string =>
	buffer.length <= MAX_NUMBER_BYTE_LENGTH
		? buffer.readIntBE(0, buffer.length).toString()
		: buffer.readBigUInt64BE().toString();
 
export const bufferToHex = (buffer: Buffer): string =>
	Buffer.from(buffer).toString('hex');
 
const hexRegex = /^[0-9a-f]+/i;
export const hexToBuffer = (hex: string, argumentName = 'Argument'): Buffer => {
	if (typeof hex !== 'string') {
		throw new TypeError(`${argumentName} must be a string.`);
	}
	const matchedHex = (hex.match(hexRegex) || [])[0];
	if (!matchedHex || matchedHex.length !== hex.length) {
		throw new TypeError(`${argumentName} must be a valid hex string.`);
	}
	// tslint:disable-next-line no-magic-numbers
	if (matchedHex.length % 2 !== 0) {
		throw new TypeError(
			`${argumentName} must have a valid length of hex string.`,
		);
	}
 
	return Buffer.from(matchedHex, 'hex');
};
 
export const stringToBuffer = (str: string): Buffer => Buffer.from(str, 'utf8');