Bitcoin cash bitbox sdk falis when trying to send tx

0

Trying to run the bitbox scripts to send transaction presented here: https://developer.bitcoin.com/bitbox/docs/transactionBuilder. When submitting the transaction, I always get the following error:

{
  error: 'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 16)'
}

Code snippet:

const bitbox = require('bitbox-sdk');
const log = require('ololog').configure({ time: false });
const ansi = require('ansicolor').nice;


const Address = new bitbox.Address()

let xpriv = 'tprv8jBszV65QgT8TAxvj8Go5r8C3BXwq3mYUvaEfEnsfjkx6PRuQYG4W8Bpc4HM2zbiT9S384shi2Zrr6qxXD6nUySxuvztP9o25hLuMcDvMYD'; // testnet

/* Transaction builder */

// instance of transaction builder

let transactionBuilder = new bitbox.TransactionBuilder('testnet');

// tx input

let txid = 'b89785f05a50f893f6c5cf395df8fcc8353cf70db9cafed45b8aff318694c173'; // utxo when funding 0'/00
let vout_index = 1;
transactionBuilder.addInput(txid, vout_index);

// tx output

let originalAmount = 10000;
const BitcoinCash = new bitbox.BitcoinCash();
let byteCount = BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });
// amount to send to receiver. It's the original amount - 1 sat/byte for tx size
let sendAmount = originalAmount - byteCount;
// add output w/ address and amount to send
transactionBuilder.addOutput(Address.toLegacyAddress(address3), sendAmount);

// lock time

// transactionBuilder.setLockTime(50000)

// sign

// node of address which is going to spend utxo
const HDNode = new bitbox.HDNode();
let hdnode = HDNode.fromXPriv(xpriv);

// keypair
let keyPair = HDNode.toKeyPair(hdnode.derivePath("0'/00")); // funded address
let redeemScript;
transactionBuilder.sign(0, keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount, transactionBuilder.signatureAlgorithms.SCHNORR);

// build tx

let tx = transactionBuilder.build();

// return hex string to send to BCH network

// output rawhex
let hex = tx.toHex();
console.log("Raw tx hex: ", hex);

// sendRawTransaction to running BCH node
const RawTransactions = new bitbox.RawTransactions(restURL='https://trest.bitcoin.com/v2/');
RawTransactions.sendRawTransaction(hex)
.then((result) => { 
    console.log(result); 
})
.catch((err) => { 
    console.log(err); 
});

Federico Caccia

Posted 2019-11-28T16:46:38.700

Reputation: 101

Question was closed 2019-11-28T21:02:50.120

No answers