I am creating a raw transaction using the bitcoin testnet, but when I push the raw transaction onto the network it takes all my balance. Am I suppose to send the remaining 'change' back to myself? Here is the code im using to create the raw transaction:
var bitcoin = require('bitcoinjs-lib');
var keyPair = bitcoin.ECPair.fromWIF('cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT', bitcoin.networks.testnet);
var tx = new bitcoin.TransactionBuilder(bitcoin.networks.testnet);
tx.addInput('87502f792d477f0514a92486c875fa1fb631fd68c95ccf458c264155165a95c6', 1);
tx.addOutput('msWccFYm5PPCn6TNPbNEnprA4hydPGadBN', 10000);
tx.sign(0, keyPair);
console.log(tx.build().toHex());
Am I correct in thinking i also have to send myself back my remaining amount? so for example if my original balance was 0.00114 BTC I would do this:
tx.addInput('87502f792d477f0514a92486c875fa1fb631fd68c95ccf458c264155165a95c6', 1);
// senders address
tx.addOutput('ms9ySK54aEC2ykDviet9jo4GZE6GxEZMzf ', 104000);
// receivers address
tx.addOutput('msWccFYm5PPCn6TNPbNEnprA4hydPGadBN', 10000);
Is this the correct way?