import { Block } from "./types" import * as constants from "./Constants"; import {BigNumber} from "bignumber.js" import { decodeAddress } from "./Encoding"; import { getVarInt } from "./Serializer"; import { getBlockData, getBytesLong, hexToBytes } from "./TransactionHelper"; var bigInt = require('big-integer') import { sha256 } from "js-sha256"; const Sha256 = (msg: Buffer) => sha256(msg) export type BuildTRC20TransactionArgs = { from: string, to: string, contractAddress: string, amount: string, assetDecimals: number fee_limit: number, timestamp: number, block: Block, signature?: Buffer } //Build Transactions export function buildTRC20TransactionHelper(args: BuildTRC20TransactionArgs): Buffer { let results = getFirstAndSecondTRC20TxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTRC20TxSection(args.timestamp, args.fee_limit)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildTRC20SignedTx(buffer, args) return signedTx; }else{ return buffer; } } //Build Hola Parameters export function getTRC20HolaParametersHelper(args: BuildTRC20TransactionArgs): {blk: any[]}{ const result = getFirstAndSecondTRC20TxSection(args); let blk = []; blk.push(result.firstSection); blk.push(result.secondSection); blk.push(getThirdTRC20TxSection(args.timestamp, args.fee_limit)); let feesBN = new BigNumber(args.fee_limit).times(new BigNumber(1000000)); let fees = getBytesLong(bigInt(feesBN.toFixed())); blk.push(fees); return {blk: blk}; } export function getTRC20HolaParameters_LinkHelper(args: BuildTRC20TransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondTRC20TxSection(args); const secondP = getThirdTRC20TxSection(args.timestamp, args.fee_limit); let dataToSign = Buffer.concat([firstP.firstSection, firstP.secondSection, secondP]); dataToSign = Buffer.from(Sha256(dataToSign), 'hex'); let blk = []; blk.push(dataToSign); return {blk: blk}; } function buildTRC20SignedTx(buffer: Buffer, args: BuildTRC20TransactionArgs) { return Buffer.concat([ Buffer.from([constants.DATA_PREFIX]), getVarInt(buffer.length), buffer, Buffer.from([constants.SIGNATURE_PREFIX]), getVarInt(args.signature.length), args.signature ]); } //Tx Building Helpers function getFirstAndSecondTRC20TxSection(args: BuildTRC20TransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract //First get value: let decimals = args.assetDecimals == 0 ? 1 : args.assetDecimals; let digits = new BigNumber(10).pow(new BigNumber(decimals)); let amountBN = new BigNumber(args.amount).times(digits); let val = amountBN.toString(16) if(val.length %2 !=0) val = "0" + val; let amount = Buffer.from(val, "hex"); //Get data field let data = Buffer.concat([ constants.TRANSFER_ID, fillBytes(decodeAddress(args.to).slice(1)), fillBytes(amount) ]); let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX_TRIGGERSMARTCONTRACT, //owner_address decodeAddress(args.from), constants.CONTRACT_ADDRESS_PREFIX_TRIGGERSMARTCONTRACT, //to_address decodeAddress(args.contractAddress), constants.DATA_PREFIX_TRIGGERSMARTCONTRACT, data ]) //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_TRIGGERSMARTCONTRACT, hexToBytes(constants.type_url_triggerSmartContract) ]) let type_value = Buffer.concat([ type_url, Buffer.from([constants.VALUE_PREFIX]), getVarInt(value.length), value ]) //Next, build type + parameter let type = constants.TYPE_PARAMETER_PREFIX_TRIGGERSMARTCONTRACT; let type_parameter = Buffer.concat([ type, //type Buffer.from([constants.PARAMETER_PREFIX]), //parameter getVarInt(type_value.length), type_value ]) //Finally, build contract (without value) let contract = Buffer.concat([ Buffer.from([constants.CONTRACT_PREFIX]), getVarInt(type_parameter.length), type, Buffer.from([constants.PARAMETER_PREFIX]), getVarInt(type_value.length), type_url, Buffer.from([constants.PARAMETER_PREFIX]), getVarInt(value.length) ]); //Build final array: ref_block_bytes + ref_block_hash + expiration + contract let buffer = Buffer.concat([ getBlockData(args.block), contract ]) return {firstSection: buffer, secondSection: value} } function getThirdTRC20TxSection(timestamp: number, fee_limit: number): Buffer{ let feeLimitBN = new BigNumber(fee_limit).times(new BigNumber(1000000)); let feeLimit = bigInt(feeLimitBN.toFixed()); return Buffer.concat([ Buffer.from([constants.TIMESTAMP_PREFIX]), getVarInt(timestamp), constants.FEE_LIMIT_PREFIX, getVarInt(feeLimit) ]) } function fillBytes(data: Buffer): Buffer { let fullBytes = Buffer.alloc(32); if (data.length > fullBytes.length) { throw Error("Data is too long"); } for (let i = 0; i <= data.length; i++) { fullBytes[32 - i] = data[data.length - i]; } return fullBytes; }