javascriptjavanode.jscryptographydigital-signature

how do I sign Tronix transaction locally in nodejs?


I've been using: https://github.com/cobowallet/tron-wallet

it has method .generateTransaction(to, amount, token, latestBlock) but, what it return is not exactly an Object, I can just pass to /wallet/broadcasttransaction (ref: https://github.com/tronprotocol/Documentation/blob/master/TRX/Tron-http.md)

I've been checking the example in: https://github.com/tronprotocol/wallet-cli/blob/master/src/main/java/org/tron/demo/TransactionSignDemo.java

but I can't seem to find, how to do: transaction.getRawData().toByteArray()

since I can't seem to resolve import org.tron.protos.Protocol.Transaction; any help would be appreciated.


Solution

  • cobowallet is easiest to sign and generate a tronix transaction. Here is an example:

        const parentNode = TronWallet.fromMnemonic('all all all all all all all all all all all all')
        const node = parentNode.derivePath("m/49'/195'/0'/0/0")
        const latestBlock = {
          hash: '000000000046b520515cd81609c969e5659568bf360859f7e5493ceedd6ec41d',
          number: 4633888,
          timestamp: 1543869858000
        }
    
        const tx = node.generateTransaction('TR2a3x6tX5trbhaTBU71Fpi1SWYj8gKEGy', 5000000, 'TRX', latestBlock)
        console.log(tx.hex)
        console.log(tx.txid)
    

    But make sure that latest block is up to date. You can use this URL (getnowblock) to fetch the latest block from the blockchain.