UNPKG

@kraken-crypto/ccxt

Version:

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

135 lines (130 loc) 3.82 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var errors = require('./errors.js'); /** * Some data helpers. * * * @_subsection api/utils:Data Helpers [about-data] */ function _getBytes(value, name, copy) { if (value instanceof Uint8Array) { if (copy) { return new Uint8Array(value); } return value; } if (typeof (value) === "string" && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) { const result = new Uint8Array((value.length - 2) / 2); let offset = 2; for (let i = 0; i < result.length; i++) { result[i] = parseInt(value.substring(offset, offset + 2), 16); offset += 2; } return result; } errors.assertArgument(false, "invalid BytesLike value", name || "value", value); } /** * Get a typed Uint8Array for %%value%%. If already a Uint8Array * the original %%value%% is returned; if a copy is required use * [[getBytesCopy]]. * * @see: getBytesCopy */ function getBytes(value, name) { return _getBytes(value, name, false); } /** * Get a typed Uint8Array for %%value%%, creating a copy if necessary * to prevent any modifications of the returned value from being * reflected elsewhere. * * @see: getBytes */ function getBytesCopy(value, name) { return _getBytes(value, name, true); } /** * Returns true if %%value%% is a valid [[HexString]]. * * If %%length%% is ``true`` or a //number//, it also checks that * %%value%% is a valid [[DataHexString]] of %%length%% (if a //number//) * bytes of data (e.g. ``0x1234`` is 2 bytes). */ function isHexString(value, length) { if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) { return false; } if (typeof (length) === "number" && value.length !== 2 + 2 * length) { return false; } if (length === true && (value.length % 2) !== 0) { return false; } return true; } /** * Returns true if %%value%% is a valid representation of arbitrary * data (i.e. a valid [[DataHexString]] or a Uint8Array). */ function isBytesLike(value) { return (isHexString(value, true) || (value instanceof Uint8Array)); } const HexCharacters = "0123456789abcdef"; /** * Returns a [[DataHexString]] representation of %%data%%. */ function hexlify(data) { const bytes = getBytes(data); let result = "0x"; for (let i = 0; i < bytes.length; i++) { const v = bytes[i]; result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f]; } return result; } /** * Returns a [[DataHexString]] by concatenating all values * within %%data%%. */ function concat(datas) { return "0x" + datas.map((d) => hexlify(d).substring(2)).join(""); } function zeroPad(data, length, left) { const bytes = getBytes(data); errors.assert(length >= bytes.length, "padding exceeds data length", "BUFFER_OVERRUN", { buffer: new Uint8Array(bytes), length: length, offset: length + 1 }); const result = new Uint8Array(length); result.fill(0); if (left) { result.set(bytes, length - bytes.length); } else { result.set(bytes, 0); } return hexlify(result); } /** * Return the [[DataHexString]] of %%data%% padded on the **left** * to %%length%% bytes. * * If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is * thrown. * * This pads data the same as **values** are in Solidity * (e.g. ``uint128``). */ function zeroPadValue(data, length) { return zeroPad(data, length, true); } exports.concat = concat; exports.getBytes = getBytes; exports.getBytesCopy = getBytesCopy; exports.hexlify = hexlify; exports.isBytesLike = isBytesLike; exports.isHexString = isHexString; exports.zeroPadValue = zeroPadValue;