import { BlockData } from "./types"; import * as constants from "./Constants" import BigNumber from "bignumber.js"; var bigint = require('big-integer') export type BuildTransactionArgs = { recipientID: string, amount: string, fees: string, blockData: BlockData, timestamp: number, senderPublicKey?: Buffer, signature?: Buffer, isIgnisTx: boolean } export function buildTransactionHelper(args: BuildTransactionArgs): Buffer{ if(args.senderPublicKey != null && args.senderPublicKey.length != 32) {throw Error("Invalid public key")}; //Get public key let pubK = args.senderPublicKey == null ? Buffer.alloc(32) : args.senderPublicKey; return Buffer.concat([ getFirstTxHalf(args), pubK, getSecondTxHalf(args) ]); } export function getHolaParametersHelper(args:BuildTransactionArgs): {blk: any[]}{ return {blk: [getFirstTxHalf(args), getSecondTxHalf(args)]}; } function getFirstTxHalf(args: BuildTransactionArgs): Buffer{ //Get timestamp let timestampBytes = getBytesInt(args.timestamp).reverse(); let chainId; if(args.isIgnisTx){ chainId = constants.IGNIS_CHAIN; }else{ chainId = constants.CHAIN; } let type; if(args.isIgnisTx){ type = constants.IGNIS_TYPE; }else{ type = constants.TYPE; } //Build array return Buffer.concat([ chainId, Buffer.from([type]), Buffer.from([constants.SUBTYPE]), Buffer.from([constants.TX_VERSION]), timestampBytes, constants.DEADLINE ]); } function getSecondTxHalf(args: BuildTransactionArgs): Buffer{ //Get recipient let recipient = getBytes(bigint(args.recipientID)).reverse(); //Get amountNQT let amountBN = new BigNumber(args.amount).times(new BigNumber(100000000)); let amountNQT = bigint(amountBN.toFixed()); let amountNQTBytes = getBytesLong(amountNQT).reverse(); //Get feeNQT let feeBN = new BigNumber(args.fees).times(new BigNumber(100000000)); let feeNQT = bigint(feeBN.toFixed()); let feeNQTBytes = getBytesLong(feeNQT).reverse(); //Get signature let signature = args.signature != null ? args.signature : Buffer.alloc(64); //Get ECBlockHeight let ecBlockHeightBytes = getBytesInt(bigint(args.blockData.blockheight)).reverse(); //Get ECBlockID let ecBlockIDBytes = getBytesLong(bigint(args.blockData.ecBlockID)).reverse(); let flags; if(args.isIgnisTx){ flags = constants.IGNIS_FLAGS; }else{ flags = constants.FLAGS; } //Return array return Buffer.concat([ recipient, amountNQTBytes, feeNQTBytes, signature, ecBlockHeightBytes, ecBlockIDBytes, flags ]); } function getBytes(data: number | bigint): Buffer{ let hexS = data.toString(16); if(hexS.length % 2 != 0) {hexS = '0' + hexS }; let bytes = Buffer.alloc(hexS.length /2); let cont = 0; for(var i = 0; i< hexS.length; i+=2){ var byte = parseInt(hexS.substring(i, i+2), 16); bytes[cont] = byte; cont++; } return bytes; } function getBytesInt(data: bigint | number) : Buffer{ let buff = getBytes(data); while(buff.length < 4){buff = Buffer.concat([Buffer.alloc(1), buff])} return buff; } function getBytesLong(data: bigint | number) : Buffer{ let buff = getBytes(data); while(buff.length < 8){buff = Buffer.concat([Buffer.alloc(1), buff])} return buff; }