How to send Bitcoin Cash from BTC segwit address?

How to send Bitcoin Cash from BTC segwit address?

I'm trying to build a nodejs command with commander, bitcoinjs-lib, bitcoincashjs, blocktrail-sdk. Currently I got the problem when trying to spend the Bitcoin Cash of segwit address.

Example:

  • A is BTC segwit address
  • B, C is Bitcoin Cash address
  • B send Bitcoin Cash to A

=> How to send Bitcoin Cash of A to C?

I've tried with this code but failed.

bch.Networks.defaultNetwork = bch.Networks.testnet;

var toAddress = "xxxx";

var blocktrail_client = blocktrail.BlocktrailSDK({
    apiKey: "xxxx",
    apiSecret: "xxxx",
    network: "BCC",
    testnet: true
});

var privKey = "xxxx";
var privateKey = new bch.PrivateKey(privKey);
var address = privateKey.toAddress().toString();

blocktrail_client
    .addressUnspentOutputs(address)
    .then(async (res, error) => {
        if (error) {
            return console.log(error);
        }
        if (res.data.length == 0) {
            return console.log("Error. Nothing to send.")
        }

    var tx = res.data[0];

    utxo = {
        txId: tx.hash,
        outputIndex: 0,
        address,
        script: tx.script_hex,
        satoshis: tx.value
    };

    const transaction = new bch.Transaction()
        .from(utxo)
        .to(toAddress, tx.value - 1000)
        .fee(1000)
        .sign(privateKey);

    blocktrail_client.sendRawTransaction(
        transaction.toString(),
        (error, hash) => {
            if (error) {
                return console.log(error);
            }
            return console.log(hash);
        }
    );
})
.catch(error => console.log(error));
https://ift.tt/2GVlSaR

Comments

Popular posts from this blog

Bitcoin Core errors with database block

Bitfinex & Market Synergy To Offer Institutional Calibre Cryptocurrency Connectivity