UNPKG

@yubing744/rooch-sdk

Version:
124 lines (123 loc) 3.58 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, { HexString: () => HexString }); module.exports = __toCommonJS(hex_exports); var import_utils = require("@noble/hashes/utils"); class HexString { /** * Creates new hex string from Buffer * @param buffer A buffer to convert * @returns New HexString */ static fromBuffer(buffer) { return HexString.fromUint8Array(buffer); } /** * Creates new hex string from Uint8Array * @param arr Uint8Array to convert * @returns New HexString */ static fromUint8Array(arr) { return new HexString((0, import_utils.bytesToHex)(arr)); } /** * Ensures `hexString` is instance of `HexString` class * @param hexString String to check * @returns New HexString if `hexString` is regular string or `hexString` if it is HexString instance * @example * ``` * const regularString = "string"; * const hexString = new HexString("string"); // "0xstring" * HexString.ensure(regularString); // "0xstring" * HexString.ensure(hexString); // "0xstring" * ``` */ static ensure(hexString) { if (typeof hexString === "string") { return new HexString(hexString); } return hexString; } /** * Creates new HexString instance from regular string. If specified string already starts with "0x" prefix, * it will not add another one * @param hexString String to convert * @example * ``` * const string = "string"; * new HexString(string); // "0xstring" * ``` */ constructor(hexString) { if (hexString.startsWith("0x")) { this.hexString = hexString; } else { this.hexString = `0x${hexString}`; } } /** * Getter for inner hexString * @returns Inner hex string */ hex() { return this.hexString; } /** * Getter for inner hexString without prefix * @returns Inner hex string without prefix * @example * ``` * const hexString = new HexString("string"); // "0xstring" * hexString.noPrefix(); // "string" * ``` */ noPrefix() { return this.hexString.slice(2); } /** * Overrides default `toString` method * @returns Inner hex string */ toString() { return this.hex(); } /** * Trimmes extra zeroes in the begining of a string * @returns Inner hexString without leading zeroes * @example * ``` * new HexString("0x000000string").toShortString(); // result = "0xstring" * ``` */ toShortString() { const trimmed = this.hexString.replace(/^0x0*/, ""); return `0x${trimmed}`; } /** * Converts hex string to a Uint8Array * @returns Uint8Array from inner hexString without prefix */ toUint8Array() { return Uint8Array.from((0, import_utils.hexToBytes)(this.noPrefix())); } } //# sourceMappingURL=hex.js.map