UNPKG

@taquito/utils

Version:

Encoding, crypto, and utility helpers for Taquito.

188 lines (187 loc) 9.24 kB
/** * @packageDocumentation * @module @taquito/utils */ import { Buffer } from 'buffer'; import { PrefixV2 } from './constants'; import BigNumberJs from 'bignumber.js'; type BigNumber = InstanceType<typeof BigNumberJs>; declare const BigNumber: typeof BigNumberJs; /** * list of prefixes that can be used to decode an address */ export declare const addressPrefixes: PrefixV2[]; /** * list of prefixes that can be used to decode a public key */ export declare const publicKeyPrefixes: PrefixV2[]; /** * list of prefixes that can be used to decode a public key hash */ export declare const publicKeyHashPrefixes: PrefixV2[]; /** * list of prefixes that can be used to decode a signature */ export declare const signaturePrefixes: PrefixV2[]; /** * Decodes Base58 string, looks for known prefix and strips it * @param src Base58 string * @returns Payload and prefix * @example b58DecodeAndCheckPrefix('tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM') // returns [Uint8Array, PrefixV2.Ed25519PublicKeyHash] * @example b58DecodeAndCheckPrefix('tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', [PrefixV2.Ed25519PublicKeyHash]) // returns [Uint8Array, PrefixV2.Ed25519PublicKeyHash] * @example b58DecodeAndCheckPrefix('tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM', [PrefixV2.Ed25519PublicKeyHash], true) // returns Uint8Array */ export declare function b58DecodeAndCheckPrefix<T extends readonly PrefixV2[]>(src: string, allowed?: T): [Uint8Array, T[number]]; export declare function b58DecodeAndCheckPrefix<T extends readonly PrefixV2[]>(src: string, allowed: T, payloadOnly: false): [Uint8Array, T[number]]; export declare function b58DecodeAndCheckPrefix<T extends readonly PrefixV2[]>(src: string, allowed: T, payloadOnly: true): Uint8Array; /** * Decode a Base58 public key and return its binary representation * @param value Value to decode * @param fmt optional format of the decoded return value, 'hex' or 'array' * @returns string or Uint8Array of bytes * @example b58DecodePublicKey('edpkuNjKKT48xBoT5asPrWdmuM1Yw8D93MwgFgVvtca8jb5pstzaCh') // return '0060842d4ba23a9940ef5dcf4404fdaa430cfaaccb5029fad06cb5ea894e4562ae' */ export declare function b58DecodePublicKey(value: string, fmt?: 'hex'): string; export declare function b58DecodePublicKey(value: string, fmt: 'array'): Uint8Array; /** * Decode a Base58 public key hash and return its binary representation * @param value Value to decode * @param fmt optional format of the decoded return value, 'hex' or 'array' * @returns string or Uint8Array of bytes * @example b58DecodePublicKeyHash('tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe') // return '0001907d6a7e9f084df840d6e67ffa8db5464f87d4d1' */ export declare function b58DecodePublicKeyHash(value: string, fmt?: 'hex'): string; export declare function b58DecodePublicKeyHash(value: string, fmt: 'array'): Uint8Array; /** * Decode a Base58 string and assert tz4 type * @param value a bls address(tz4) to decode * @param fmt optional format of the decoded return value, 'hex' or 'array' * @returns string or Uint8Array of bytes * @example b58DecodeBlsAddress('tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8') // return 'af2dc3c40667abc0e89c0ef40171d22aed08d5eb' */ export declare function b58DecodeBlsAddress(value: string, fmt?: 'hex'): string; export declare function b58DecodeBlsAddress(value: string, fmt: 'array'): Uint8Array; /** * Decode a Base58 contract ID and return its binary representation * @param value Value to decode * @param fmt optional format of the decoded return value, 'hex' or 'array' * @returns string or Uint8Array of bytes * @example b58DecodeAddress('tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM') // return '0000e96b9f8b19af9c7ffa0c0480e1977b295850961f' */ export declare function b58DecodeAddress(value: string, fmt?: 'hex'): string; export declare function b58DecodeAddress(value: string, fmt: 'array'): Uint8Array; /** * Gets Tezos address (PKH) from Public Key * @param publicKey Base58 Public Key * @returns A string of the Tezos address (PKH) that was derived from the given Public Key * @example getPkhfromPk('edpkuNjKKT48xBoT5asPrWdmuM1Yw8D93MwgFgVvtca8jb5pstzaCh') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe' */ export declare function getPkhfromPk(publicKey: string): string; /** * Add the prefix to a hex string or Uint8Array and Base58 encode it * @param value Value to Base58 encode * @param pre prefix ID to append to the encoded string * @example b58Encode('e96b9f8b19af9c7ffa0c0480e1977b295850961f', PrefixV2.Ed25519PublicKeyHash) // returns 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM' */ export declare function b58Encode(value: string | Uint8Array, pre: PrefixV2): string; /** * Parse binary public key and return Base58 representation * @param value Binary key data * @returns return prefixed public key * @example encodeKey('02033aba7da4a2e7b5dd9f074555c118829aff16213ea1b65859686bd5fcfeaf3616') // return 'p2pk66xmhjiN7LpfrDGFwpxPtJxkLtPjQ6HUxJbKmRbxSR7RMpamDwi' */ export declare function encodeKey(value: string | Uint8Array): string; /** * Parse binary public key hash and return Base58 representation * @param value Key hash to parse * @returns return prefixed public key hash * @example encodeKeyHash('0001907d6a7e9f084df840d6e67ffa8db5464f87d4d1') // return 'tz2MVED1t9Jery77Bwm1m5YhUx8Wp5KWWRQe' */ export declare function encodeKeyHash(value: string | Uint8Array): string; /** * Parse binary Contract ID and return Base58 representation * @param value Address to parse (tz1, tz2, tz3, KT1, or sr1). * @example encodeAddress('0000e96b9f8b19af9c7ffa0c0480e1977b295850961f') // return 'tz1gvF4cD2dDtqitL3ZTraggSR1Mju2BKFEM' */ export declare function encodeAddress(value: string | Uint8Array): string; /** * Base58 encode an address without predefined prefix * @param value Address to base58 encode (tz4) hex dec * @returns return address * @example encodeBlsAddress('af2dc3c40667abc0e89c0ef40171d22aed08d5eb') // return 'tz4QyWfEiv56CVDATV3DT3CDVhPaMKif2Ce8' */ export declare function encodeBlsAddress(value: string): string; /** * convert a fragment of Michelson code in hex string to an 'expr' prefix + base58 encoded BLAKE2b hash string * @param value a fragment of Michelson code in hex string * @returns return 'expr' prefix + base58 encoded BLAKE2b hash * @example encodeExpr('050a000000160000b2e19a9e74440d86c59f13dab8a18ff873e889ea') // return 'exprv6UsC1sN3Fk2XfgcJCL8NCerP5rCGy1PRESZAqr7L2JdzX55EN' */ export declare function encodeExpr(value: string): string; /** * convert a signed operation in hex string to an 'op' prefix + base58 encoded BLAKE2b hash string * @param value signed operation in hex string * @returns return 'op' prefix + base58 encoded BLAKE2b hash * @example encodeOpHash('0f185d8a30061e8134c162dbb7a6c3ab8f5fdb153363ccd6149b49a33481156a6c00b2e19a9e74440d86c59f13dab8a18ff873e889eaa304ab05da13000001f1585a7384f36e45fb43dc37e8ce172bced3e05700ff0000000002002110c033f3a990c2e46a3d6054ecc2f74072aae7a34b5ac4d9ce9edc11c2410a97695682108951786f05b361da03b97245dc9897e1955e08b5b8d9e153b0bdeb0d') // return 'opapqvVXmebRTCFd2GQFydr4tJj3V5QocQuTmuhbatcHm4Seo2t' */ export declare function encodeOpHash(value: string): string; /** * Convert an hex string to a Uint8Array * @param hex Hex string to convert * @throws {@link ValueConversionError} */ export declare function hex2buf(hex: string): Uint8Array; /** * Merge 2 buffers together * @param b1 First buffer * @param b2 Second buffer */ export declare function mergebuf(b1: Uint8Array, b2: Uint8Array): Uint8Array; /** * Flatten a michelson json representation to an array * @param s michelson json */ export declare function mic2arr(s: any): any; /** * Convert a Uint8Array to an hex string * @param bytes Uint8Array to convert */ export declare function buf2hex(bytes: ArrayLike<number>): string; /** * Convert a string to a byte string representation * @param str String to convert */ export declare function stringToBytes(str: string): string; /** * Convert byte string representation to string * @param hex byte string to convert */ export declare function bytesToString(hex: string): string; /** * Convert hex string/UintArray/Buffer to bytes * @param hex String value to convert to bytes */ export declare function hex2Bytes(hex: string): Buffer; /** * Converts a number or Bignumber to hexadecimal string * @param val The value that will be converted to a hexadecimal string value */ export declare function toHexBuf(val: number | BigNumber, bitLength?: number): Buffer<ArrayBuffer>; export declare function numToHexBuffer(val: number | BigNumber, bitLength?: number): Buffer<ArrayBuffer>; /** * Converts a number or BigNumber to a padded hexadecimal string * @param val The value that will be converted into a padded hexadecimal string value * @param bitLength The length of bits * */ export declare function num2PaddedHex(val: number | BigNumber, bitLength?: number): string; /** * * Strips the first 2 characters of a hex string (0x) * * @param hex string to strip prefix from */ export declare function stripHexPrefix(hex: string): string; export declare function splitAddress(addr: string): [string, string | null]; export declare function compareArrays(a: ArrayLike<number>, b: ArrayLike<number>): number; export {};