UNPKG

@2waychain/2wayjs

Version:

API wrapper to access the AIBlock 2 Way Chain

126 lines (125 loc) 3.85 kB
import { IClientResponse, IPending2WTxDetails, IResult } from '../interfaces'; type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; /** * Cast `status` received from 2 Way network to lowercase string variant * * TODO: There's probably already a built-in function for this? * * @export * @param {('Success' | 'Error' | 'InProgress' | 'Unknown')} status * @return {*} {('success' | 'error' | 'pending' | 'unknown')} */ export declare function castAPIStatus(status: 'Success' | 'Error' | 'InProgress' | 'Unknown'): 'success' | 'error' | 'pending' | 'unknown'; /** * Converts a string into a byte array for handling by nacl * * @param msg {string} * @returns */ export declare function getStringBytes(msg: string): Uint8Array; /** * Converts a HEX string into a byte array * * @returns * @param hexString - HEX string to obtain byte array from */ export declare function getHexStringBytes(hexString: string): Uint8Array; /** * Converts a sequence of bytes into a HEX string * * @param bytes {Uint8Array} * @returns */ export declare function getBytesHexString(bytes: Uint8Array): string; /** * Formats a token balance for display * * @param balance {number} * @param fraction - Optional fraction to divide the balance by * @returns */ export declare function formatBalance(balance: number, fraction?: number): string; /** * Converts a byte array into a utf-8 string * * @param bytes {Uint8Array} * @returns */ export declare function getBytesString(bytes: Uint8Array): string; /** * Truncates string to a specified number of bytes * * @param chars {string} * @param n {number} * @returns */ export declare function truncateByBytesUTF8(chars: string, n: number): string; export declare function typedArrayToBuffer(array: Uint8Array): ArrayBuffer; /** * Concatenates two typed arrays, as long as they are of the same type * * @param a {Uint8Array} * @param b {Uint8Array} * @returns */ export declare function concatTypedArrays<T extends TypedArray>(a: T, b: T): T; /** * Filter out `value` from `IResult` containing possible errors, * if an error occurs, throw an exception * * @export * @template T * @return {*} * @param result - Result wrapper */ export declare function throwIfErr<T>(result: IResult<T>): T; /** * Throws an error if there's a client API-level error. * * * @export * @param {IClientResponse} result * @return {*} {IClientResponse} */ export declare function throwIfIClientError(result: IClientResponse): IClientResponse; /** * Calculate the nonce value required to provide valid PoW * for a specified ID (challenge) and target (difficulty) * * @export * @param {number} target * @param {string} id * @return {*} {number} */ export declare function calculateNonceForId(target: number, id: string): number; /** * Create a unique ID as well as the required nonce for PoW * * @export * @param {number} [difficulty] * @return {*} {{ * headers: { 'x-cache-id': string; 'x-nonce': number }; * }} */ export declare function createIdAndNonceHeaders(difficulty?: number): { headers: { 'x-cache-id': string; 'x-nonce': number; }; }; /** * Generate a unique ID * * @export * @return {*} {string} */ export declare const getUniqueID: () => string; /** * Adds sensible defaults to asset structures for receiving and sending in 2 way transactions. Ensures * error handling for missing fields. * * @param {IPending2WTxDetails} txStructure * @return {*} {IResult<IPending2WTxDetails>} */ export declare const formatAssetStructures: (txStructure: IPending2WTxDetails) => IResult<IPending2WTxDetails>; export {};