import { getScriptFromAddress } from "./AddressHelper"; import * as constants from "./Constants" import BigNumber from "bignumber.js"; var bigint = require('big-integer') export type BuildTransactionArgs = { from: string, to: string, sequence: number, amount: string, fees: string, destinationTag?: number, signingPublicKey?: Buffer, signature?: Buffer } export function buildTransactionHelper(args: BuildTransactionArgs): Buffer{ let buffer = getFirstTxHalf(args); //Add signingPubKey if(args.signingPublicKey != null){ buffer = Buffer.concat([ buffer, constants.SIGNING_PUBKEY_ID, Buffer.from([args.signingPublicKey.length]), args.signingPublicKey ]); } //Add signature if(args.signature != null){ buffer = Buffer.concat([ buffer, constants.TX_SIGNATURE_ID, Buffer.from([args.signature.length]), args.signature ]); } return Buffer.concat([buffer, getSecondTxHalf(args)]); } export function getHolaParametersHelper(args: BuildTransactionArgs): {blk: any[], u32: number[]}{ //Get blk let blkData = []; if(args.destinationTag != null){ blkData.push(Buffer.from([0x01])); }else{ blkData.push(Buffer.from([0x00])); } let buffer = Buffer.concat([constants.SIGN_PREFIX, getFirstTxHalf(args)]); blkData.push(buffer); blkData.push(getSecondTxHalf(args)); //Get u32 let u32Data = []; let valueD = new BigNumber(args.amount).toFixed();; let amountPrecision = 1; if(valueD.includes(".")){ amountPrecision = valueD.split(".")[1].length; } u32Data.push(amountPrecision); let feesD = new BigNumber(args.fees).toFixed(); let feesPrecision = 1; if(feesD.includes(".")){ feesPrecision = feesD.split(".")[1].length; } u32Data.push(feesPrecision); return {blk: blkData, u32: u32Data}; } export function getHolaParameters_LINKHelper(args: BuildTransactionArgs): {blk: any[]}{ //Get blk let blkData = []; let buffer = Buffer.concat([constants.SIGN_PREFIX, getFirstTxHalf(args)]); blkData.push(buffer); blkData.push(getSecondTxHalf(args)); return {blk: blkData}; } export function getDataToSignHelper(args: BuildTransactionArgs): Buffer{ if(args.signingPublicKey == null){ throw Error("Missing public key") } return Buffer.concat([ constants.SIGN_PREFIX, getFirstTxHalf(args), constants.SIGNING_PUBKEY_ID, Buffer.from([args.signingPublicKey.length]), args.signingPublicKey, getSecondTxHalf(args) ]); } function getFirstTxHalf(args: BuildTransactionArgs): Buffer{ //Get sequence let sequenceB = getBytesInt(args.sequence); //Add transaction type, flags and sequence let buffer = Buffer.concat([ constants.TRANSACTION_TYPE_ID, constants.TRANSACTION_TYPE, constants.FLAGS_ID, constants.FLAGS, constants.SEQUENCE_ID, sequenceB ]) //Add destinationTag if(args.destinationTag != null){ let destinationTagB = getBytes(args.destinationTag); if(destinationTagB.length > 4) throw Error("Invalid destinationTag"); while(destinationTagB.length < 4){destinationTagB = Buffer.concat([Buffer.alloc(1), destinationTagB])} buffer = Buffer.concat([ buffer, constants.TAG_ID, destinationTagB ]) } //Get amount let amountBN = new BigNumber(args.amount).times(new BigNumber(1000000)); let amount = bigint(amountBN.toFixed()); let amountB = getBytesLong(amount); amountB[0] = amountB[0] |0x40; //Get fee let feeBN = new BigNumber(args.fees).times(new BigNumber(1000000)); let fee = bigint(feeBN.toFixed()); let feeB = getBytesLong(fee); feeB[0] = feeB[0] | 0x40; //Add amount and fee buffer = Buffer.concat([ buffer, constants.AMOUNT_ID, amountB, constants.FEE_ID, feeB ]) return buffer; } function getSecondTxHalf(args: BuildTransactionArgs): Buffer{ //Get account let fromScript = getScriptFromAddress(args.from); //Get destination let toScript = getScriptFromAddress(args.to); return Buffer.concat([ constants.ACCOUNT_ID, Buffer.from([fromScript.length]), fromScript, constants.DESTINATION_ID, Buffer.from([toScript.length]), toScript ]) } function getBytes(data: bigint | number): 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; }