0

I am trying to use Nodejs tron library to create pair of wallet address and here is my code:

app.js

var app = require('express')()
var http = require('http').createServer(app)
var wallet_engine = require('./function')
app.get('/', function (req, res) {
    result = wallet_engine.create_wallet_trx()
    console.log(result)
    res.send(result)
})
//////server listen
http.listen(8443, function () {
    console.log('listening on *:8443')
})

and here is my function.js

module.exports = {
    create_wallet_trx: function () {
////////generate TRX Address
        var { HdTronPayments } = require('@faast/tron-payments')
        var keys = HdTronPayments.generateNewKeys()
        var tronPayments = new HdTronPayments({ hdKey: keys.xprv })
        var address = tronPayments.getPayport(356)
        var privateKey = tronPayments.getPrivateKey(356)
        var trx_wallet = { privateKey: privateKey, address: address }
        console.log(trx_wallet)
        return trx_wallet
    },
}

The problem is when i check console.log(trx_wallet) the result is there and i can see generated public and private key, also after returning data, console.log(result) is displaying data, but res.send(result) shows me empty json.

this is console.log() results

{
  privateKey: Promise {
    'B88BB56DAB80DB681765A0C07197DD23BB8E2FAD195BF9D0ECD09F5F8FC54297'
  },
  address: Promise { { address: 'TYCJSKERHReUXacw9wLorZYLDoijevvsVK' } }
}

and this is the result on my browser:

{"privateKey":{},"address":{}}

i know this is because of Nodejs Async system and i should wait to promise gets the value but i don't know how to wait for promise to get complete and then prints the result on my browser screen.

Sina Nouri
  • 111
  • 3
  • 13
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Nov 10 '19 at 19:20
  • They are promise objects which you are trying to `JSON.stringify`. Wait for their results, then send those. – Bergi Nov 10 '19 at 19:22
  • You cannot trust the output of `console.log`, at least not what you see when you *expand* it, because that part the console will often retrieve asynchronously... later. – trincot Nov 10 '19 at 19:22
  • @jonrsharpe i tried the aproach from your provided link before but it caused the browser to load forever. i did it like: `wallet_engine.create_wallet_trx(function(result) { res.send(result) }); ` – Sina Nouri Nov 10 '19 at 19:31
  • Are you sure the API you're working with accepts callbacks like that? That wouldn't be conventional for Node, and the function you show doesn't handle any arguments. – jonrsharpe Nov 10 '19 at 19:32

1 Answers1

2

You are doing good but here many calls are asynchronous that's for you facing problem. you should use async await or then as i did bellow it may help you..

// app js


var app = require('express')()
var http = require('http').createServer(app)
var wallet_engine = require('./function')
app.get('/', function (req, res) {
    result = wallet_engine.create_wallet_trx().then(data=>{
      res.json(data);
    }).catch(err=>{
      console.log(err);
    })
})
//////server listen
http.listen(8443, function () {
    console.log('listening on *:8443')
})



// function.js

var { HdTronPayments } = require('@faast/tron-payments')

module.exports = {
  create_wallet_trx: async function () {
      var keys = await HdTronPayments.generateNewKeys()
      var tronPayments = await new HdTronPayments({ hdKey: keys.xprv })
      var address = await tronPayments.getPayport(356)
      var privateKey = await tronPayments.getPrivateKey(356)
      var trx_wallet = { privateKey: privateKey, address: address }
      return trx_wallet
  }
}

result: {"privateKey":"92ACAECFE00E9F90E330A6B031F10365F29AFDD503922CC99CA8704F1BA53432","address":{"address":"TGfsHx4VU6B36AwUy8Bqt6edoNnUHpMtSQ"}}

Atul Kumar
  • 324
  • 4
  • 8
  • Thanks Atul it did the job. – Sina Nouri Nov 10 '19 at 19:57
  • Seems unlikely that all four awaits are necessary. Try omitting the first two leaving `var address = await tronPayments.getPayport(356)` and `var privateKey = await tronPayments.getPrivateKey(356)`. – Roamer-1888 Nov 10 '19 at 20:58