import BigNumber from "bignumber.js"; import { getAddressBytes } from "./AddressHelper"; import { encodeByte, encodeElement, encodeList } from "./Encoder"; import { Network, Signature } from "./types"; var bigint = require('big-integer') import { Keccak } from "sha3"; export type BuildTransactionArgs = { nonce: string, gasPrice: string, gasLimit: number, fromAddress: string, 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(1000000000000000000));//Klay to peb gasPriceBN = gasPriceBN.times(new BigNumber(1000000000));//gWei to peb let gasPrice = bigint(gasPriceBN.toFixed()); let gasLimit = bigint(args.gasLimit); let valueBN = new BigNumber(args.amount); valueBN = valueBN.times(new BigNumber(1000000000000000000)); //Klay to peb 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 from encodedData.push(encodeElement(getAddressBytes(args.fromAddress))); //Get rlp encoding of singnature let signature = [] if(args.signature == null){ signature.push(encodeElement(getBytes(bigint(args.network)))); signature.push(encodeElement(Buffer.alloc(0))); signature.push(encodeElement(Buffer.alloc(0))); }else{ signature.push(encodeElement(getBytes(bigint(args.signature.v)))); signature.push(encodeElement(args.signature.r)); signature.push(encodeElement(args.signature.s)); } let signatureList = encodeList(signature); let signatures = [] signatures.push(signatureList) //Add signature to transaction encodedData.push(encodeList(signatures)); let transaction = encodeList(encodedData); let typeAndTx = Buffer.concat([getBytes(bigint(8)), transaction]); return typeAndTx; } export function getDataToSignHelper(args: BuildTransactionArgs) : Buffer{ let nonce = bigint(args.nonce); let gasPriceBN = new BigNumber(args.gasPrice); //asPriceBN = gasPriceBN.times(new BigNumber(1000000000000000000));//Klay to peb gasPriceBN = gasPriceBN.times(new BigNumber(1000000000));//Klay to peb let gasPrice = bigint(gasPriceBN.toFixed()); let gasLimit = bigint(args.gasLimit); let valueBN = new BigNumber(args.amount); valueBN = valueBN.times(new BigNumber(1000000000000000000)); //Klay to peb let value = bigint(valueBN.toFixed()); let encodedData = []; //Add type let typeB = getBytes(bigint(0x08)); encodedData.push(encodeByte(typeB[0])); //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 from encodedData.push(encodeElement(getAddressBytes(args.fromAddress))); //Get rlp encoding of data let dataRLP = encodeList(encodedData); //Add temporal signatures let finalEncodedData = [] finalEncodedData.push(encodeElement(dataRLP)); finalEncodedData.push(encodeElement(getBytes(bigint(args.network)))); finalEncodedData.push(encodeElement(Buffer.alloc(0))); finalEncodedData.push(encodeElement(Buffer.alloc(0))); let dataToSign = encodeList(finalEncodedData); return dataToSign; } export function getHolaParametersHelper(args: BuildTransactionArgs) : {blk: any[], u32: number[]}{ //Get blk let blkData = []; blkData.push(getDataToSignHelper(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 = getDataToSignHelper(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; }