TRON Developer Guide — Transaction

TRON DAO
2 min readMay 5, 2019

--

Generating Signed Transaction

Install the npm library here or download the utils and the lib folders into your project to use natively.

Alternatively, you can use API Signature and Broadcast Flow to generate a signed transaction.

Creating a Transaction

Import transactionBuilder and crypto into your JavaScript file.

const CryptoUtils = require("@tronscan/client/src/utils/crypto");
const TransactionUtils = require("@tronscan/client/src/utils/transactionBuilder");

This function from the imported transactionBuilder file will create the Transaction Object.

function buildTransferTransaction(token, from, to, amount) {  if (token.toUpperCase() === 'TRX') {    let transferContract = new TransferContract();
transferContract.setToAddress(Uint8Array.from(decode58Check(to)));
transferContract.setOwnerAddress(Uint8Array.from(decode58Check(from)));
transferContract.setAmount(amount);
return buildTransferContract(
transferContract,
Transaction.Contract.ContractType.TRANSFERCONTRACT,
"TransferContract");
} else {
let transferContract = new TransferAssetContract();
transferContract.setToAddress(Uint8Array.from(decode58Check(to)));
transferContract.setOwnerAddress(Uint8Array.from(decode58Check(from)));
transferContract.setAmount(amount);
transferContract.setAssetName(encodeString(token));
return buildTransferContract(
transferContract,
Transaction.Contract.ContractType.TRANSFERASSETCONTRACT,
"TransferAssetContract");
}
}

The parameters for this function are:

Value

Data Type

Description

token

String

The asset you want to send in your transaction.
Example: “TRX”

from

String

The address that you want to send from in your transaction.
Example: “TDCMWoSbAfcegQqNUaRNjGhY4tAbdCmdwi”

to

String

The address that you want to send to in your transaction.
Example:
“TQ6pM81JDC2GhrUoNYtZGvPc7SvyqcemEu”

amount

Integer

The amount you want to spend of the specified token. If you want to send “TRX”, make sure you multiply this value by 1000000.
Example:
If you want to send 2 TRX, use 2000000

The buildTransferTransaction specified above will return the value of buildTransferContract, which is the function that creates the Transaction Object.

  • JavaScript
function buildTransferContract(message, contractType, typeName) {
let anyValue = new google_protobuf_any_pb.Any();
anyValue.pack(message.serializeBinary(), "protocol." + typeName);
let contract = new Transaction.Contract();
contract.setType(contractType);
contract.setParameter(anyValue);
let raw = new Transaction.raw();
raw.addContract(contract);
// raw.setTimestamp(new Date().getTime() * 1000000);
let transaction = new Transaction();
transaction.setRawData(raw);
return transaction;
}

Signing the Transaction

This function from the imported crypto file will sign the Transaction Object.

  • JavaScript
function signTransaction(priKeyBytes, transaction) {  if (typeof priKeyBytes === 'string') {
priKeyBytes = hexStr2byteArray(priKeyBytes);
}
let raw = transaction.getRawData();
let rawBytes = raw.serializeBinary();
let hashBytes = SHA256(rawBytes);
let signBytes = ECKeySign(hashBytes, priKeyBytes);
let uint8Array = new Uint8Array(signBytes);
let count = raw.getContractList().length;
for (let i = 0; i < count; i++) {
transaction.addSignature(uint8Array);
}
return {
transaction,
hex: byteArray2hexStr(transaction.serializeBinary()),
};
}

The parameters for this function are:

Value

Data Type

Description

priKeyBytes

Byte Array. You can also use a String due the function converting the value to byte array if needed.

The private key of the account.

Transaction

Transaction Object

The bundled transaction to be sent.

After successfully running these functions, you will have a signed transaction that can be broadcasted to the TRON blockchain.

Full Code Example

  • JavaScript
const CryptoUtils = require("@tronscan/client/src/utils/crypto");
const TransactionUtils = require("@tronscan/client/src/utils/transactionBuilder");
function transferContractTx() {
const privateKey = "b815adfd6ef133d5a878869cb3a2b31f32d4c1481132a71300c3e125be0ab1a1";
const token = "TRX";
const fromAddress = CryptoUtils.pkToAddress(privateKey);
const toAddress = "TQ6pM81JDC2GhrUoNYtZGvPc7SvyqcemEu";
const amount = 1;
let transaction = TransactionUtils.buildTransferTransaction(token, fromAddress, toAddress, amount);

let signedTransaction = CryptoUtils.signTransaction(privateKey, transaction);
}

--

--

TRON DAO
TRON DAO

Written by TRON DAO

The official Medium of TRON DAO.

No responses yet