0
votes

tran.js

var CoinStack = require('coinstack-sdk-js')

var coinstackclient = new CoinStack('YOUR_COINSTACK_ACCESS_KEY',
'YOUR_COINSTACK_SECRET_KEY'); // Actual keys not shown

var privateKeyWIF = CoinStack.ECKey.createKey(); //개인키 생성

var txBuilder = coinstackclient.createTransactionBuilder();
txBuilder.addOutput("1Q8xE8T3G9mxRoDUde6gDSxnK1uCac2kqh", 
CoinStack.Math.toSatoshi("0.01"))
txBuilder.setInput("1Q8xE8T3G9mxRoDUde6gDSxnK1uCac2kqh");
  txBuilder.buildTransaction(function(err, tx) {
  tx.sign(privateKeyWIF)
  var rawTx = tx.serialize()
  // send tx
  client.sendTransaction(rawTx, function(err) {
    if (null != err) {
        console.log("failed to send tx");
      }
   });
});

Error

(node:12012) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'sign' of undefined (node:12012) [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 don't know what to do

3

3 Answers

0
votes

the second argument of your callback, 'tx' is most likely undefined.

0
votes

Check if tx is not null or undefined and then call its function for ex, for example if (ex != null && ex.sign) { ... }

0
votes

Most likely txBuilder.buildTransaction() fails, and indeed your tx parameter in the callback function is not the expected object.

You should check for errors, before using tx, like so:

txBuilder.buildTransaction(function(err, tx) {
    if (err) {
        // output err or do something with it
        console.log('failed to build transaction');
        console.log(err);

        // stop here.
        return;
    }


    tx.sign(privateKeyWIF)
    var rawTx = tx.serialize();
        // send tx
        client.sendTransaction(rawTx, function(err) {
        if (err) {
            console.log("failed to send tx");
        }
    });
});