import BigNumber from "bignumber.js"; import { DecodeAddress } from "./AddressHelper" import { OutputType } from "./types"; import {blake2b} from'ethereum-cryptography/blake2b' var bigInt = require('big-integer') var CBOR = require('cbor-sync'); export type BuildTransactionArgs={ To: string; From: string; Nonce: string; Value: string; GasLimit: number; GasFeeCap: string; GasPremium: string; Signature?: string, outputType?: OutputType } export function BuildTransactionHelper(args: BuildTransactionArgs): any{ if(args.outputType == null){ args.outputType = OutputType.CBOR } if(args.outputType == OutputType.CBOR){ return GetDataToSignHelper(args); }else{ let transaction = { "from": args.From, "to": args.To, "amount":args.Value.toString(), "gasLimit": args.GasLimit, "gasFeeCap": args.GasFeeCap, "gasPremium": args.GasPremium, "nonce": args.Nonce.toString(), "signature": "" } if(args.Signature != null){ transaction.signature = args.Signature } return transaction; } } export function GetDataToSignHelper(args: BuildTransactionArgs): Buffer{ let fromBytes = DecodeAddress(args.From); let toBytes = DecodeAddress(args.To); let nonce = Number(args.Nonce); let valueBN = new BigNumber(args.Value); valueBN = valueBN.times(new BigNumber(1000000000000000000)); //FIL to attaFIL let value = bigInt(valueBN.toFixed()); let valueB = getBytes(value); let gasFeeCapBN = new BigNumber(args.GasFeeCap); let gasFeeCap = bigInt(gasFeeCapBN.toFixed()); let gasFeeCapB = getBytes(gasFeeCap); let gasPremiumBN = new BigNumber(args.GasPremium); let gasPremium = bigInt(gasPremiumBN.toFixed()); let gasPremiumB = getBytes(gasPremium); let txToEncode = [ 0, toBytes, fromBytes, nonce, valueB, args.GasLimit, gasFeeCapB, gasPremiumB, 0, Buffer.from("","base64") ] //return cbor.encode(txToEncode); return CBOR.encode(txToEncode); } export function GetHolaParametersHelper(args: BuildTransactionArgs): {blk: Buffer[], u32: any[]}{ //Get blk let blkData = []; blkData.push(GetDataToSignHelper(args)); //Get u32 let u32Data = []; let valueD = new BigNumber(args.Value).toFixed();; let amountPrecision = 1; if(valueD.includes(".")){ amountPrecision = valueD.split(".")[1].length; } u32Data.push(amountPrecision); let gasFeeCapB = new BigNumber(args.GasFeeCap); let totalGasB = gasFeeCapB.plus(new BigNumber(args.GasPremium)) let gasPriceB = totalGasB.times(new BigNumber(args.GasLimit)).dividedBy(new BigNumber(1000000000)); //Se muestra en nanoFIL 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 GetHolaParamerters_LinkHelper(args: BuildTransactionArgs): {blk: Buffer[]}{ //Get blk let blkData = []; let txToSign = GetDataToSignHelper(args); txToSign = blake2b(txToSign, 32); txToSign = Buffer.concat([Buffer.from("0171A0E40220","hex"), txToSign]); txToSign = blake2b(txToSign, 32); blkData.push(txToSign); return {blk: blkData}; } export function getBytes(data: bigint): Buffer{ let hexS = data.toString(16); if(hexS.length % 2 != 0) {hexS = '0' + hexS }; if(hexS == "00") return Buffer.alloc(0); 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 Buffer.concat([Buffer.from("00", "hex"), bytes]); }