UNPKG

@ocap/util

Version:

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

220 lines (219 loc) 6.14 kB
import type { LiteralUnion } from 'type-fest'; import rightPad from 'lodash/padEnd'; import leftPad from 'lodash/padStart'; import BN from 'bn.js'; export declare const isBase58btc: (data: any) => boolean; export type BytesType = string | Buffer | Uint8Array; export type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>; export type KeyPairType = { publicKey: BytesType; secretKey: BytesType; }; export { BN, leftPad, rightPad }; /** * 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 */ export 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 */ export declare const isHexPrefixed: (str: string) => boolean; /** * Removes '0x' from a given `String` if present * * @public * @static */ export declare const stripHexPrefix: (str: string | any) => any; /** * Returns true if object is BN, otherwise false * * @public * @static * @method isBN * @param {Object} object * @returns {Boolean} */ export declare const isBN: (object: $TSFixMe) => boolean; /** * Returns true if object is BigNumber, otherwise false * * @public * @static * @method isBigNumber * @param {Object} object * @returns {Boolean} */ export 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} */ export declare const isHexStrict: (hex: string) => boolean; /** * Check if string is HEX * * @public * @static * @method isHex * @param {String} hex to be checked * @returns {Boolean} */ export 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 */ export 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 */ export 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 */ export declare const hexToUtf8: (hex: string) => string; /** * Converts value to number representation * * @public * @static * @method hexToNumber * @param {String|Number|BN} value * @returns {Number} */ export declare const hexToNumber: (value: string | number | BN) => number; /** * Converts value to hex representation * * @public * @static * @method numberToHex * @param {String|Number|BN} value * @returns {String} */ export 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 */ export 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 */ export 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} */ export declare const toHex: (value: string | number | boolean | BN | Uint8Array | Buffer | (number | { test: string; })[] | { test: string; }, returnType?: boolean) => string; export declare const numberToString: (arg: $TSFixMe) => any; /** * Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token */ export declare const fromUnitToToken: (input: string | number | BN, decimal?: number, optionsInput?: $TSFixMe) => string; /** * Convert human readable token number to big number instance */ export declare const fromTokenToUnit: (input: string | number, decimal?: number) => BN; /** * Validates if a value is an Uint8Array. */ export declare function isUint8Array(value: $TSFixMe): boolean; /** * Generate a random UUID */ export declare function UUID(): string; /** * Check if a string is valid UUID */ export declare function isUUID(str: string): boolean; /** * Convert input to Uint8Array on best effort, base64 node supported */ export declare function toUint8Array(v: any): Uint8Array; /** * Convert input to Buffer on best effort, base64 not supported */ export declare function toBuffer(v: any): Buffer; /** * Convert input to base58btc format on best effort */ export declare function toBase58(v: any): string; /** * Decode base58 string */ export declare function fromBase58(v: string): Buffer; /** * Convert input to base64 format */ export declare function toBase64(v: any, escape?: boolean): string; /** * Decode base64(base64_url) string to buffer */ export declare function fromBase64(v: string): Buffer; /** * Convert did to address: remove `did:abt:` prefix */ export declare function toAddress(did: string): string; /** * Convert address to did: prepend `did:abt:` prefix */ export declare function toDid(address: string): string; export declare function isSameDid(a: string, b: string): boolean; export declare function formatTxType(type: string): any;