UNPKG

@aeternity/aepp-sdk

Version:
115 lines (100 loc) 3.91 kB
import _fillInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/fill"; import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; import _Number$isInteger from "@babel/runtime-corejs3/core-js-stable/number/is-integer"; /* * ISC License (ISC) * Copyright 2018 aeternity developers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ import BigNumber from 'bignumber.js'; import { isBase64, isHex } from './string'; /** * Bytes module * @module @aeternity/aepp-sdk/es/utils/bytes * @example import { Crypto } from '@aeternity/aepp-sdk' */ /** * Left pad the input data with 0 bytes * @param length to pad to * @param inputBuffer data to pad * @return the padded data */ export function leftPad(length, inputBuffer) { var fill = length - inputBuffer.length; if (fill > 0) { var fillArray = new Uint8Array(fill); _fillInstanceProperty(fillArray).call(fillArray, 0, fill); return _concatInstanceProperty(Buffer).call(Buffer, [fillArray, inputBuffer]); } else { return inputBuffer; } } /** * Right pad the input data with 0 bytes * @param length to pad to * @param inputBuffer data to pad * @return the padded data */ export function rightPad(length, inputBuffer) { var fill = length - inputBuffer.length; if (fill > 0) { var fillArray = new Uint8Array(fill); _fillInstanceProperty(fillArray).call(fillArray, 0, fill); return _concatInstanceProperty(Buffer).call(Buffer, [inputBuffer, fillArray]); } else { return inputBuffer; } } /** * Convert bignumber to byte array * @param x bignumber instance * @return Buffer */ export function bigNumberToByteArray(x) { if (!x.isInteger()) throw new Error("Unexpected not integer value: ".concat(x.toFixed())); var hexString = x.toString(16); if (hexString.length % 2 > 0) hexString = '0' + hexString; return Buffer.from(hexString, 'hex'); } export function toBytes(val) { var big = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // """ // Encode a value to bytes. // If the value is an int it will be encoded as bytes big endian // Raises ValueError if the input is not an int or string if (val === undefined || val === null) return Buffer.from([]); if (_Number$isInteger(val) || BigNumber.isBigNumber(val) || big) { if (!BigNumber.isBigNumber(val)) val = BigNumber(val); return bigNumberToByteArray(val); } if (typeof val === 'string') { return val.toString('utf-8'); } throw new Error('Byte serialization not supported'); } /** * Convert a string to a Buffer. If encoding is not specified, hex-encoding * will be used if the input is valid hex. If the input is valid base64 but * not valid hex, base64 will be used. Otherwise, utf8 will be used. * @param {string} str String to be converted. * @param {string=} enc Encoding of the input string (optional). * @return {buffer} Buffer (bytearray) containing the input data. */ export function str2buf(str, enc) { if (!str || str.constructor !== String) return str; if (!enc && isHex(str)) enc = 'hex'; if (!enc && isBase64(str)) enc = 'base64'; return Buffer.from(str, enc); } //# sourceMappingURL=bytes.js.map