UNPKG

@yubing744/rooch-sdk

Version:
54 lines (53 loc) 2.09 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var hex_exports = {}; __export(hex_exports, { fromHexString: () => fromHexString, padLeft: () => padLeft, padRight: () => padRight, toHexString: () => toHexString }); module.exports = __toCommonJS(hex_exports); function toHexString(byteArray) { const hexArray = Array.from(byteArray).map((byte) => { const roundedByte = Math.floor(byte); return (roundedByte < 0 ? 256 + roundedByte : roundedByte).toString(16).padStart(2, "0"); }); return `0x${hexArray.join("")}`; } function fromHexString(hex, padding) { let hexWithoutPrefix = hex.startsWith("0x") ? hex.substring(2) : hex; if (padding && hexWithoutPrefix.length < padding) { hexWithoutPrefix = padLeft(hexWithoutPrefix, padding); } else if (!padding && hexWithoutPrefix.length % 2 !== 0) { hexWithoutPrefix = `0${hexWithoutPrefix}`; } const byteArray = new Uint8Array(hexWithoutPrefix.length / 2); for (let i = 0; i < hexWithoutPrefix.length; i += 2) { byteArray[i / 2] = parseInt(hexWithoutPrefix.substring(i, i + 2), 16); } return byteArray; } function padLeft(str, chars, sign = "0") { return new Array(chars - str.length + 1).join(sign) + str; } function padRight(str, chars, sign = "0") { return str + new Array(chars - str.length + 1).join(sign); } //# sourceMappingURL=hex.js.map