UNPKG

@ocap/util

Version:

utils shared across multiple forge js libs, works in both node.js and browser

231 lines (230 loc) 6.85 kB
import { BN } from "./bn.cjs"; import { scopeMatch, scopeMatchAny } from "./scope-match.cjs"; import { LiteralUnion } from "type-fest"; //#region src/index.d.ts declare const leftPad: (str: any, len: number, char?: string) => string; declare const rightPad: (str: any, len: number, char?: string) => string; declare const isBase58btc: (data: any) => boolean; type BytesType = string | Buffer | Uint8Array; type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>; type KeyPairType = { publicKey: BytesType; secretKey: BytesType; }; /** * Returns a BN object, converts a number value to a BN * @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object * @return {BN} `output` BN object of the number * @throws if the argument is not an array, object that isn't a bignumber, not a string number or number */ declare const numberToBN: (arg: string | number | BN) => BN; /** * Returns a `boolean` on whether or not the `string` starts with '0x' * * @public * @static * @param {String} str the string input value * @return {Boolean} a boolean if it is or is not hex prefixed * @throws if the str input is not a string */ declare const isHexPrefixed: (str: string) => boolean; /** * Removes '0x' from a given `String` if present * * @public * @static */ declare const stripHexPrefix: (str: string | any) => any; /** * Returns true if object is BN, otherwise false * * @public * @static * @method isBN * @param {Object} object * @returns {Boolean} */ declare const isBN: (object: $TSFixMe) => boolean; /** * Returns true if object is BigNumber, otherwise false * * @public * @static * @method isBigNumber * @param {Object} object * @returns {Boolean} */ declare const isBigNumber: (object: $TSFixMe) => boolean; /** * Check if string is HEX, requires a 0x in front * * @public * @static * @method isHexStrict * @param {String} hex to be checked * @returns {Boolean} */ declare const isHexStrict: (hex: string) => boolean; /** * Check if string is HEX * * @public * @static * @method isHex * @param {String} hex to be checked * @returns {Boolean} */ declare const isHex: (hex: string) => boolean; /** * Takes an input and transforms it into an BN * * @public * @static * @method toBN * @param {Number|String|BN} num, string, HEX string or BN * @returns {BN} BN */ declare const toBN: (num: number | string | BN, base?: number | "hex") => BN; /** * Should be called to get hex representation (prefixed by 0x) of utf8 string * * @public * @static * @method utf8ToHex * @param {String} str * @returns {String} hex representation of input string */ declare const utf8ToHex: (str: string) => string; /** * Should be called to get utf8 from it's hex representation * * @public * @static * @method hexToUtf8 * @param {String} hex * @returns {String} ascii string representation of hex value */ declare const hexToUtf8: (hex: string) => string; /** * Converts value to number representation * * @public * @static * @method hexToNumber * @param {String|Number|BN} value * @returns {Number} */ declare const hexToNumber: (value: string | number | BN) => number; /** * Converts value to hex representation * * @public * @static * @method numberToHex * @param {String|Number|BN} value * @returns {String} */ declare const numberToHex: (value: $TSFixMe) => string; /** * Convert a byte array to a hex string * Note: Implementation from crypto-js * * @public * @static * @method bytesToHex * @param {Array} bytes * @returns {String} the hex string */ declare const bytesToHex: (bytes: $TSFixMe) => string; /** * Convert a hex string to a byte array * Note: Implementation from crypto-js * * @public * @static * @method hexToBytes * @param {String} hex * @returns {Array} the byte array */ declare const hexToBytes: (hex: $TSFixMe) => Array<any>; /** * Auto converts any given value into it's hex representation. * And even stringify objects before. * * @public * @static * @method toHex * @param {String|Number|BN|Object|TypedArray|Buffer} value * @param {Boolean} returnType * @returns {String} */ declare const toHex: (value: string | number | boolean | BN | Uint8Array | Buffer | (number | { test: string; })[] | { test: string; }, returnType?: boolean) => string; declare const numberToString: (arg: $TSFixMe) => any; /** * Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token */ declare const fromUnitToToken: (input: string | number | BN, decimal?: number, optionsInput?: $TSFixMe) => string; /** * Convert human readable token number to big number instance */ declare const fromTokenToUnit: (input: string | number, decimal?: number) => BN; /** * Validates if a value is an Uint8Array. */ declare function isUint8Array(value: $TSFixMe): boolean; /** * Generate a random UUID */ declare function UUID(): string; /** * Check if a string is valid UUID */ declare function isUUID(str: string): boolean; /** * Convert input to Uint8Array on best effort, base64 node supported */ declare function toUint8Array(v: any): Uint8Array; /** * Convert input to Buffer on best effort, base64 not supported */ declare function toBuffer(v: any): Buffer; /** * Convert input to base58btc format on best effort */ declare function toBase58(v: any): string; /** * Decode base58 string */ declare function fromBase58(v: string): Buffer; /** * Convert input to base64 format */ declare function toBase64(v: any, escape?: boolean): string; /** * Decode base64(base64_url) string to buffer */ declare function fromBase64(v: string): Buffer; /** * Convert did to address: remove `did:{method}:` prefix. * Strips only the first layer prefix (anchored regex). */ declare function toAddress(did: string): string; /** * Convert address to did: prepend `did:{method}:` prefix. * @param address - bare address or full DID * @param method - DID method (default: 'abt') */ declare function toDid(address: string, method?: string): string; declare function isSameDid(a: string, b: string): boolean; /** * Extract the method from a DID string. * @returns The method string, or null if no prefix found */ declare function extractMethod(did: unknown): string | null; declare function formatTxType(type: string): string; //#endregion export { BN, BytesType, EncodingType, KeyPairType, UUID, bytesToHex, extractMethod, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };