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 } export function buildTransactionHelper(args: BuildTransactionArgs): Buffer { if(args.senderPublicKey != null && args.senderPublicKey.length != 32) {throw Error("Invalid public key")}; let pubKey = args.senderPublicKey == null ? Buffer.alloc(32) : args.senderPublicKey; return Buffer.concat([ getFirstTxHalf(args), pubKey, 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(); //Build array return Buffer.concat([ Buffer.from([constants.TYPE]), Buffer.from([constants.VERSION_SUBTYPE]), 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); //Add ECBlockHeight let ecBlockHeightBytes = getBytesInt(bigint(args.blockData.blockheight)).reverse(); //Add ECBlockID let ecBlockIDBytes = getBytesLong(bigint(args.blockData.ecBlockID)).reverse(); //Build array return Buffer.concat([ recipient, amountNQTBytes, feeNQTBytes, constants.REFERENCEDTX, signature, constants.FLAGS, ecBlockHeightBytes, ecBlockIDBytes ]); } 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; }