import { decodeAddress } from "./Encoding"; import { getVarInt} from "./Serializer"; import { Block, ResourceType, Vote } from "./types" import * as constants from "./Constants"; import {BigNumber} from "bignumber.js" import { sha256 } from "js-sha256"; var bigInt = require('big-integer') const Sha256 = (msg: Buffer) => sha256(msg) export type BuildTransactionArgs = { from: string, to: string, amount: string, fees: string, timestamp: number, block: Block, signature?: Buffer } export type BuildFreezeTransactionArgs = { from: string, amount: string, fees: string, timestamp: number, resourceType: ResourceType block: Block, signature?: Buffer } export type BuildUnfreezeTransactionArgs = { from: string, amount: string, fees: string, timestamp: number, resourceType: ResourceType block: Block, signature?: Buffer } export type BuildWithdrawTransactionArgs = { from: string, fees: string, timestamp: number, block: Block, signature?: Buffer } export type BuildVoteWitnessTransactionArgs = { from: string, votes: Vote[], fees: string, timestamp: number, block: Block, signature?: Buffer } export type BuildWithdrawExpiredUnfreezeTransactionArgs = { from: string, fees: string, timestamp: number, block: Block, signature?: Buffer } //Build Transactions export function buildTransactionHelper(args: BuildTransactionArgs): Buffer { let results = getFirstAndSecondTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } export function buildFreezeBalanceTransactionHelper(args: BuildFreezeTransactionArgs): Buffer{ let results = getFirstAndSecondFreezeTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } export function buildUnfreezeBalanceTransactionHelper(args: BuildUnfreezeTransactionArgs): Buffer{ let results = getFirstAndSecondUnfreezeTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } export function buildWithdrawBalanceTransactionHelper(args: BuildWithdrawTransactionArgs): Buffer{ let results = getFirstAndSecondWithdrawTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } export function buildVoteWitnessTransactionHelper(args: BuildVoteWitnessTransactionArgs): Buffer{ let results = getFirstAndSecondVoteTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } export function buildWithdrawExpiredUnfreezeTransactionHelper(args: BuildWithdrawExpiredUnfreezeTransactionArgs): Buffer{ let results = getFirstAndSecondWithdrawExpiredUnfreezeTxSection(args); let buffer = Buffer.concat([results.firstSection, results.secondSection, getThirdTxSection(args.timestamp)]); if(args.signature!= null){ //Build signed transaction let signedTx = buildSignedTx(buffer, args) return signedTx; }else{ return buffer; } } function buildSignedTx(buffer: Buffer, args: BuildWithdrawExpiredUnfreezeTransactionArgs) { return Buffer.concat([ Buffer.from([constants.DATA_PREFIX]), getVarInt(buffer.length), buffer, Buffer.from([constants.SIGNATURE_PREFIX]), getVarInt(args.signature.length), args.signature ]); } //Build Hola Parameters export function getHolaParametersHelper(args: BuildTransactionArgs): {blk: any[]}{ const result = getFirstAndSecondTxSection(args); let blk = []; blk.push(result.firstSection); blk.push(result.secondSection); blk.push(getThirdTxSection(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 getHolaParameters_LinkHelper(args: BuildTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondTxSection(args); const secondP = getThirdTxSection(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}; } export function getFreezeBalanceHolaParameters_LinkHelper(args: BuildFreezeTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondFreezeTxSection(args); const secondP = getThirdTxSection(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}; } export function getUnfreezeBalanceHolaParameters_LinkHelper(args: BuildUnfreezeTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondUnfreezeTxSection(args); const secondP = getThirdTxSection(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}; } export function getWithdrawBalanceHolaParameters_LinkHelper(args: BuildWithdrawTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondWithdrawTxSection(args); const secondP = getThirdTxSection(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}; } export function getVoteWitnessHolaParameters_LinkHelper(args: BuildVoteWitnessTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondVoteTxSection(args); const secondP = getThirdTxSection(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}; } export function getWithdrawExpiredUnfreezeHolaParameters_LinkHelper(args: BuildWithdrawExpiredUnfreezeTransactionArgs): {blk: any[]}{ const firstP = getFirstAndSecondWithdrawExpiredUnfreezeTxSection(args); const secondP = getThirdTxSection(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}; } //Tx building helpers function getFirstAndSecondTxSection(args: BuildTransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract //First get value: let amountBN = new BigNumber(args.amount).times(new BigNumber(1000000)); let amount = bigInt(amountBN.toFixed()); let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), constants.TO_ADDRESS_PREFIX, //to_address decodeAddress(args.to), Buffer.from([constants.AMOUNT_PREFIX]), //amount getVarInt(amount) ]) //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX, hexToBytes(constants.type_url) ]) 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; 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 getFirstAndSecondFreezeTxSection(args:BuildFreezeTransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract //First get value: let amountBN = new BigNumber(args.amount).times(new BigNumber(1000000)); let amount = bigInt(amountBN.toFixed()); let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), Buffer.from([constants.FREEZE_AMOUNT_PREFIX]), //freeze_amount getVarInt(amount) ]) if(args.resourceType == ResourceType.Energy){ value = Buffer.concat([ value, Buffer.from([constants.RESOURCE_TYPE_PREFIX]), Buffer.from([0x01]) ]) } //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_FREEZE, hexToBytes(constants.type_url_freeze) ]) 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_FREEZE; 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 getFirstAndSecondUnfreezeTxSection(args:BuildUnfreezeTransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract //First get value: let amountBN = new BigNumber(args.amount).times(new BigNumber(1000000)); let amount = bigInt(amountBN.toFixed()); let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), Buffer.from([constants.UNFREEZE_AMOUNT_PREFIX]), //freeze_amount getVarInt(amount) ]) if(args.resourceType == ResourceType.Energy){ value = Buffer.concat([ value, Buffer.from([constants.RESOURCE_TYPE_PREFIX]), Buffer.from([0x01]) ]) } //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_UNFREEZE, hexToBytes(constants.type_url_unfreeze) ]) 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_UNFREEZE; 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 getFirstAndSecondWithdrawTxSection(args:BuildWithdrawTransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), ]) //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_WITHDRAW, hexToBytes(constants.type_url_withdraw) ]) 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_WITHDRAW; 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 getFirstAndSecondVoteTxSection(args:BuildVoteWitnessTransactionArgs): {firstSection: Buffer, secondSection: Buffer}{ //Add contract let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), ]) args.votes.forEach(vote => { let voteD = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, decodeAddress(vote.vote_address), Buffer.from([constants.VOTES_AMOUNT_PREFIX]), getVarInt(vote.vote_number) ]) value = Buffer.concat([ value, Buffer.from([constants.VALUE_PREFIX]), getVarInt(voteD.length), voteD ]) }); //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_VOTE, hexToBytes(constants.type_url_vote) ]) 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_VOTE; 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 getFirstAndSecondWithdrawExpiredUnfreezeTxSection(args: BuildWithdrawExpiredUnfreezeTransactionArgs) : {firstSection: Buffer, secondSection: Buffer}{ //Add contract let value = Buffer.concat([ constants.OWNER_ADDRESS_PREFIX, //owner_address decodeAddress(args.from), ]) //Next, build type_url + value let type_url = Buffer.concat([ constants.TYPE_URL_PREFIX_WITHDRAW_EXPIRED, hexToBytes(constants.type_url_withdrawExpired) ]) 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_WITHDRAW_EXPIRED; 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 getThirdTxSection(timestamp: number): Buffer{ return Buffer.concat([ Buffer.from([constants.TIMESTAMP_PREFIX]), getVarInt(timestamp) ]) } export function getBlockData(block: Block): Buffer{ //Get ref_block_bytes let ref_block_bytes = getBytesLong(bigInt(block.ref_block_height)); ref_block_bytes = Buffer.from(ref_block_bytes.subarray(6, 8)); //Get ref_block_hash let hashBytes = hexToBytes(block.ref_block_hash); let ref_block_hash = Buffer.from(hashBytes.subarray(8,16)); //Get expiration let expiration = block.ref_block_timestamp + 3600000 //Add 1 hour to block_timestamp let expirationB = getVarInt(expiration); //Build array let buffer = Buffer.concat([ Buffer.from([constants.REF_BLOCK_BYTES_PREFIX]), //ref_block_bytes Buffer.from([ref_block_bytes.length]), ref_block_bytes, Buffer.from([constants.REF_BLOCK_HASH_PREFIX]), //ref_block_hash Buffer.from([ref_block_hash.length]), ref_block_hash, Buffer.from([constants.EXPIRATION_PREFIX]), //expiration expirationB, ]) return buffer; } 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 getBytesLong(data: bigint) : Buffer{ let buff = getBytes(data); while(buff.length < 8){buff = Buffer.concat([Buffer.alloc(1), buff])} return buff; } 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); }