1

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));

closed as off-topic by Andrew Chow Jul 17 at 17:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about cryptocurrencies or projects that are not Bitcoin, and are not applicable to Bitcoin, are off-topic. For more information, see this meta-discussion." – Andrew Chow
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    you do not need to sign this transaction – amaclin Mar 30 '18 at 9:48
  • @amaclin thank you for your comment, would you please help me explain more details? – Tho Nguyen Mar 30 '18 at 10:06
  • This makes no sense. You can't send BCH to a BTC address; it's a different network, different blockchain, and different currency. You can't send USD to a BTC address either. You'll need to find an exchange or someone willing to trade. Also, segwit does not exist on the BCH chain. – Pieter Wuille Mar 31 '18 at 18:09
  • 1
    @PieterWuille: Actually, Bitcoin addresses are valid on the Bitcoin Cash network, so you can send BCH to an address that is part of a Bitcoin wallet. This has been a major headache to wallet provider services. Most cases are fairly easy to fix, but recovering funds sent to a segwit address is more complicated. – Murch Nov 27 '18 at 17:19