import { Network, Signature } from "./types" import { encodeByte, encodeElement, encodeList } from "./Encoder"; import { getAddressBytes } from "./AddressHelper"; import { BigNumber } from "bignumber.js"; import { Keccak } from "sha3"; import { log } from "console"; 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) { let network = formatV(args.network); encodedData.push(encodeElement(network)); encodedData.push(encodeElement(Buffer.alloc(0))); encodedData.push(encodeElement(Buffer.alloc(0))); } else { let v = formatV(args.signature.v) encodedData.push(encodeElement(v)); encodedData.push(encodeElement(args.signature.r)); encodedData.push(encodeElement(args.signature.s)); } let transaction = encodeList(encodedData); return transaction; } 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); } export function formatV(vRaw){ var v = Buffer.alloc(4); v.writeInt32BE(vRaw, 0); v = trimStart(v) return v; } export function trimStart(d) { var i = 0 while (!d[i] && (i < d.length)) i++ return d.slice(i) }