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 BuildTRC10TransactionArgs = { from: string, to: string, amount: string, assetDecimals: number fees: string, assetName: string timestamp: number, block: Block, signature?: Buffer } //Build Transactions export function buildTRC10TransactionHelper(args: BuildTRC10TransactionArgs): Buffer { let results = getFirstAndSecondTRC10TxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTRC10TxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildTRC10SignedTx(buffer, args) return signedTx; }else{ return buffer; } } //Build Hola Parameters export function getTRC10HolaParametersHelper(args: BuildTRC10TransactionArgs): {blk: any[]}{ const result = getFirstAndSecondTRC10TxSection(args); let blk = []; blk.push(result.firstSection); blk.push(result.secondSection); blk.push(getThirdTRC10TxSection(args.timestamp)); let feesBN = new BigNumber(args.fees).times(new BigNumber(1000000)); let fees = getBytesLong(bigInt(feesBN.toFixed())); blk.push(fees); return {blk: blk}; } export function getTRC10HolaParameters_LinkHelper(args: BuildTRC10TransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondTRC10TxSection(args); const secondP = getThirdTRC10TxSection(args.timestamp); let dataToSign = Buffer.concat([firstP.firstSection, firstP.secondSection, secondP]); dataToSign = Buffer.from(Sha256(dataToSign), 'hex'); let blk = []; blk.push(dataToSign); return {blk: blk}; } function buildTRC10SignedTx(buffer: Buffer, args: BuildTRC10TransactionArgs) { 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 getFirstAndSecondTRC10TxSection(args: BuildTRC10TransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract //First get value: let decimals = args.assetDecimals == 0 ? 1 : args.assetDecimals; let amountBN = new BigNumber(args.amount).times(new BigNumber(decimals)); let amount = bigInt(amountBN.toFixed()); let value = Buffer.concat([ Buffer.from([constants.ASSET_NAME_PREFIX]), //asset_name getVarInt(args.assetName.length), Buffer.from(args.assetName, "ascii"), constants.OWNER_ADDRESS_PREFIX_ASSET, //owner_address decodeAddress(args.from), constants.TO_ADDRESS_PREFIX_ASSET, //to_address decodeAddress(args.to), Buffer.from([constants.AMOUNT_PREFIX_ASSET]), //amount getVarInt(amount) ]) //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_TRANSFER_ASSET, hexToBytes(constants.type_url_transferasset) ]) 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_ASSET; 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 getThirdTRC10TxSection(timestamp: number): Buffer{ return Buffer.concat([ Buffer.from([constants.TIMESTAMP_PREFIX]), getVarInt(timestamp) ]) }