import { asciiToTxHex } from '@helixnetwork/converter'; import { generateAddress } from '@helixnetwork/core'; const { extractJson } = require('@helixnetwork/extract-json'); /** * @method getNodeInfo * @param {ComposeAPI }helix - helix api wrapper * @return {Promise} * @fulfil {NodeInfo} Object with information about connected node. * @reject {Error} * - Fetch error */ export const getNodeInfo = async (helix:any): Promise => helix.getNodeInfo(); /** * @method createHelixTransaction * @param {ComposeAPI }helix - helix api wrapper * @param seed hex seed to sign transaction * @param address hex address * @param value hlx value to send to transaction. Defaults to zero * @param message message to be send with the transaction * @param tag transaction tag * @param remainderAddress remainder adrsss to credit balance * @param depth * @param mwm * @param security security level of seed */ export const createHelixTransaction = async (helix:any, seed: string, address: string, value: number = 0, message: string, inputs: any, tag: string, remainderAddress:string, depth: number = 2, mwm: number = 2, security: number = 2, ): Promise => { const transfer = [ { address, message: asciiToTxHex(message), tag: asciiToTxHex(tag), value, }, ]; return helix.prepareTransfers(seed, transfer, { inputs, security, remainderAddress }).then( (bytes: any) => helix.sendTransactionStrings( bytes, depth, mwm, ), ); }; /** * * @method findTransactions * @param {ComposeAPI }helix - helix api wrapper * @param {Hash[]} [addresses] - List of addresses * @param {Hash[]} [bundles] - List of bundle hashes * @param {Tag[]} [tags] - List of tags * @returns {Promise} * @fulfil {Hash[]} Array of transaction hashes * @reject {Error} * - `INVALID_SEARCH_KEY` * - `INVALID_HASH`: Invalid bundle hash * - `INVALID_TRANSACTION_HASH`: Invalid approvee transaction hash * - `INVALID_ADDRESS`: Invalid address * - `INVALID_TAG`: Invalid tag * - Fetch error */ export const findTransactions = ( helix:any, addresses: string[], bundles?: string[], tags?: string[], ): Promise => helix.findTransactions({ addresses, bundles, tags }); /** * @method getBalance * @param {ComposeAPI }helix - helix api wrapper * @param {Array} address -list of addresses * @returns {Promise} * * @fulfil {Balances} Object with list of `balances` and corresponding `milestone` * @reject {Error} * - `INVALID_HASH`: Invalid address * - `INVALID_THRESHOLD`: Invalid `threshold` * - Fetch error */ export const getBalance = (helix:any, address: string[]): Promise => helix.getBalances( address, 100, ); /** * * @method getInclusionStates * @param {ComposeAPI }helix - helix api wrapper * @param {Hash[]} transactions - List of transaction hashes * @param {Hash[]} tips - List of tips to check if transactions are referenced by * * @return {Promise} * @fulfil {boolean[]} Array of inclusion state * @reject {Error} * - `INVALID_TRANSACTION_HASH`: Invalid `hashes` or `tips` * - Fetch error */ export const getInclusionStates = ( helix:any, transaction: string[], tips: string[] = [], ): Promise => helix.getInclusionStates(transaction, tips); /** * Get Inclustion state till transaction is confirmed * @param {ComposeAPI }helix - helix api wrapper * @param transactions - List of transaction hashes * @param interval time interval for check to happen */ export const getInclutionStateTilConfirmation = async ( helix:any, transactions: string[], interval: number = 3000, ) => new Promise((resolve) => { const repeat = setInterval(() => { helix.getInclusionStates(transactions, []).then((res:any) => { if (res[0]) { clearInterval(repeat); resolve(true); } }); }, interval); }); /** * * @method getTransactionStrings * @param {ComposeAPI }helix - helix api wrapper * @param {Array} transaction -list of transactions * * @return {Promise} * @fulfil {HBytes[]} - Transaction hbytes * @reject Error{} * - `INVALID_TRANSACTION_HASH`: Invalid hash * - Fetch error */ export const getTransactionStrings = ( helix:any, transaction: string[], ): Promise => helix.getTransactionStrings( transaction, ); /** * * @method getTransactionObjects * @param {ComposeAPI }helix - helix api wrapper * @param {Array} transaction -list of transactions * * @return {Promise} * @fulfil Transaction Objects * @reject Error{} * - `INVALID_TRANSACTION_HASH`: Invalid hash * - Fetch error */ export const getTransactionObjectPayload = async ( helix:any, transaction: string[], ): Promise => { const res = await helix.getTransactionObjects( transaction, ); const msg = JSON.parse(extractJson(res[0])); return msg; }; /** * * @method getBundlePayload * @param {ComposeAPI }helix - helix api wrapper * @param {Hash} tailTransactionHash - Tail transaction hash * * @returns {Promise} * */ export const getBundlePayload = async ( helix:any, tailTransactionHash: string, ): Promise => { const result = await helix.getBundle( tailTransactionHash, ); const msg = JSON.parse(extractJson(result)); return msg; }; /** * Generates address from a particular index * @param seed valid helix seed * @param index index of the address to be generated */ export const generateHelixAddress = async ( seed : string, index:number, security: number = 2, ): Promise => generateAddress(seed, index, security);