import { Network, Signature } 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, 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 encodedData.push(encodeElement(getBytes(value))); //Add message encodedData.push(encodeElement(Buffer.alloc(0))); 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; } export function hexToBytes(hex: string) : Buffer{ for (var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return Buffer.from(bytes); }