-1

I'm new on Stackoverflow as a member. So I'm sorry if I don't know yet how to put the code in the design you do :s

I tried all night to find a way to make the deposit Bitcoin Key out of the client.initWallet('***', '***', function(err, wallet) function.

The console.log(address) you see downstairs is working fine. But juste after the }, I cannont access the data. I really don't know what to do. If someone has any idea why I can't keep the value on "address" ...

Thanks for your time :D

const key = '*key*';
const secret = '*secretkey*';

blocktrail = require('blocktrail-sdk');

client = blocktrail.BlocktrailSDK({
    apiKey: key,
    apiSecret: secret,
    network: 'BTC',
    testnet: false
});


client.initWallet('*user*', '*password*',
    function(err, wallet) {
    wallet.getNewAddress(function (err, address) {
      **console.log(address);**
    });
});
saAction
  • 2,035
  • 1
  • 13
  • 18
Marcolry
  • 21
  • 2

1 Answers1

0

Bluebird can convert your callback functions to function which returns Promise,

const bluebird = require('bluebird');
const key = '*key*';
const secret = '*secretkey*';

async function getWallet(){
    blocktrail = require('blocktrail-sdk');
    client = blocktrail.BlocktrailSDK({
        apiKey: key,
        apiSecret: secret,
        network: 'BTC',
        testnet: false
    });

    var asyncInitWallet = bluebird.promisify(client.initWallet);
    var wallet = await asyncInitWallet('*user*', '*password*');

    var asyncGetNewAddress = bluebird.promisify(wallet.getNewAddress);
    var address = await asyncGetNewAddress();
    console.log('address: ', address);
}
getWallet();
Mehul Prajapati
  • 1,210
  • 2
  • 11
  • 29
  • Thanks for quick answering. I try your code and I find that on my console : (node:89482) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async functionwithout a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:89482) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. I'm not sure there is an easy answer :s.. – Marcolry Sep 01 '18 at 12:59
  • Yeah obviously, You need to keep await statements inside try/catch block, I just gave you an idea about how you can use promise to solve this. Inside catch block log the error to identify whats the error – Mehul Prajapati Sep 02 '18 at 06:17