import { Signature, Network } from "./types"; import { encodeByte, encodeElement, encodeList } from "./Encoder"; import { getAddressBytes } from "./AddressHelper"; import BigNumber from "bignumber.js"; import { Keccak } from "sha3"; var bigInt = require('big-integer') export type BuildTransactionArgs = { nonce: string, gasPrice: string, gasLimit: number, toAddress: string, amount: string, data?: Buffer, signature?: Signature, network: Network } export function buildTransactionHelper(args: BuildTransactionArgs): Buffer { let nonce = bigInt(args.nonce); let gasPriceBN = new BigNumber(args.gasPrice); gasPriceBN = gasPriceBN.times(new BigNumber(1000000000));//Gwei to wei let gasPrice = bigInt(gasPriceBN.toFixed()); let gasLimit = bigInt(args.gasLimit); let valueBN = new BigNumber(args.amount); valueBN = valueBN.times(new BigNumber(1000000000000000000)); //Ether to wei let value = bigInt(valueBN.toFixed()); let encodedData = []; //Add nonce let nonceB = getBytes(nonce); if (nonceB.length == 1) { encodedData.push(encodeByte(nonceB[0])); } else { encodedData.push(encodeElement(nonceB)); } //Add gasPrice encodedData.push(encodeElement(getBytes(gasPrice))); //Add gasLimit encodedData.push(encodeElement(getBytes(gasLimit))); //Add toAddress encodedData.push(encodeElement(getAddressBytes(args.toAddress))); //Add amount if(value == 0){ encodedData.push(encodeElement(Buffer.alloc(0))); }else{ encodedData.push(encodeElement(getBytes(value))); } //Add message if(args.data == null){ encodedData.push(encodeElement(Buffer.alloc(0))); }else{ let message = encodeElement(args.data); encodedData.push(message); } if (args.signature == null) { encodedData.push(encodeElement(Buffer.from([args.network]))); encodedData.push(encodeElement(Buffer.alloc(0))); encodedData.push(encodeElement(Buffer.alloc(0))); } else { encodedData.push(encodeElement(Buffer.from([args.signature.v]))); encodedData.push(encodeElement(args.signature.r)); encodedData.push(encodeElement(args.signature.s)); } let transaction = encodeList(encodedData); return transaction; } export function getHolaParametersHelper(args: BuildTransactionArgs): { blk: any[], u32: number[] } { //Get blk let blkData = []; blkData.push(buildTransactionHelper(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 gasPriceB = new BigNumber(args.gasPrice); gasPriceB = gasPriceB.times(new BigNumber(args.gasLimit)).dividedBy(new BigNumber(1000000000)); let feesD = gasPriceB.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 tx = buildTransactionHelper(args); let hashK256 = new Keccak(256); let resKeccak = hashK256.update(tx).digest(); let blkData = []; blkData.push(resKeccak); return {blk: blkData}; } export function getBytes(data: 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; }