Tron (TRX & TRC20)

Sign Transaction

The D'CENT Web SDK provides functions for signing transaction of coins.

getTronSignedTransaction()

Supported Coin Type

  • TRON

Parameters

  • transactionJson: this value conforms JSON format of Transaction Types in Tron Docs

Requirements:

  • D'CENT Bridge version 1.4.0 or higher is required.

  • D'CENT Biometric Wallet version 2.3.0 or higher is required.

Useage

const decode58Check = require('@tronscan/client/src/utils/crypto').decode58Check
const { Transaction } = require('@tronscan/client/src/protocol/core/Tron_pb')
const googleProtobufAnyPb = require('google-protobuf/google/protobuf/any_pb.js')
const _buf2hex = (buffer) => { // buffer is an ArrayBuffer
    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
const baseURL = 'https://api.trongrid.io'
// get node info
const command = '/wallet/getnodeinfo'
const reponse = await axios.get(baseURL + command)
let dataInfo = response.data
let arrBlcokInfo = dataInfo.block.split(',')
const nodeInfo = {
    number: arrBlcokInfo[0].split(':')[1],
    hash: arrBlcokInfo[1].split(':')[1],
}

let transferContract = new TronscanSdk.TransferContract()
transferContract.setToAddress(Uint8Array.from(decode58Check(toAddr)))
transferContract.setOwnerAddress(Uint8Array.from(decode58Check(fromAddr)))
transferContract.setAmount(amount)

let anyValue = new googleProtobufAnyPb.Any();
anyValue.pack(transferContract.serializeBinary(), 'protocol.' + typeName)
let contract = new Transaction.Contract();
contract.setType(contractType)
contract.setParameter(anyValue)

const refBlockHash = Buffer.from(nodeInfo.hash, 'hex').slice(8, 16).toString('base64');
const blockNumber = Number(nodeInfo.number);
const refBlockBytes = getBlockBytes(blockNumber).toString('base64');

let raw = new Transaction.raw()

raw.addContract(contract);
raw.setRefBlockNum(blockNumber)
raw.setRefBlockBytes(refBlockBytes)
raw.setRefBlockHash(refBlockHash)
raw.setTimestamp(Date.now());
raw.setExpiration(Date.now() + (100 * 60 * 60 * 1000)) // 10 hours
if (contractType === Transaction.Contract.ContractType.TRIGGERSMARTCONTRACT) {
    raw.setFeeLimit(50 * 1000000) // transfer fee limit
}
let transaction = new Transaction();
transaction.setRawData(raw);

const unsignedTx = _buf2hex(raw.serializeBinary())

const transactionJson = {
    unsignedTx: unsignedTx,
    fee: fee
    path: `m/44'/195'/0'/0/0`,
}

var result
try {
    result = await DcentWebConnector.getTronSignedTransaction(transactionJson);    
} catch (e) {
    console.log(e)
    result = e
}

Returned response object

  • The property of the response's parameter, the pubkey is deprecated.

{
    "header": {
        "version": "1.0",
        "response_from": "tron",
        "status": "success"
    },
    "body": {
        "command": "transaction",
        "parameter": {
            "signed_tx": "0x35544659743d463715380a2f66205ac9c38feb04033c29a5d415f8b009f566664a1972ac8be256308ec9b38a726f02eec103fc74963d7caf783cd55f1d7610d900",
            "pubkey": "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
        }
    }
}

getTrcTokenSignedTransaction()

Supported Coin Type

  • TRON_TRC_TOKEN

Parameters

  • Same as getTronSignedTransaction()

Returned response object

  • Same as getTronSignedTransaction()

Last updated