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));
1you do not need to sign this transaction – amaclin – 2018-03-30T09:48:10.857
@amaclin thank you for your comment, would you please help me explain more details? – Tho Nguyen – 2018-03-30T10:06:45.127
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 – 2018-03-31T18:09:53.500
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 – 2018-11-27T17:19:59.187