Using bitcore to create a transaction with two addresses and change address

3

2

I'm playing around with bitcore transactions and multi-sig transactions and was wondering how to send a transaction to more than one address, and keep the change.

Currently when I try to send the transaction it is sent, but the fee is calculated as:

unspent - satoshis

An example of this transaction I've made in the block chain can be seen here:

Example Transaction

Ad you can see it's calculated like this:

Original unspent amount: 0.09000000

Fee: 0.08990000

Traded: 0.00010000

Transaction sum: 0.00010000

Now I understand that one must spend all of the unspent outputs in a transaction, but how can I stop the fee from being as above, the remaining unspent balance?

Basically I want to split the transaction for two addresses, and send the rest to a change address.

Code:

var utxo = new bitcore.Transaction.UnspentOutput({
  "txid" : "f42af6ab8b4dc3c636c5bfc6ce819063a55060c5619d31a6fa45a36413cb7953",
  "vout" : 0,
  "address" : "2MzuRYCXSHcBDEsSS4rziShvEzWSfoDe2zL",
  "scriptPubKey" : "OP_HASH160 20 0x54025426880aa847ec86f4d2488bbe260bfe0fcb OP_EQUAL",
  "satoshis" : 10000
});

var pubs = [
    "02758b89e56bfa8da41f2c1701aa5927bc026cc3b51bbeff399c53a15f2ae52e28",
    "02987ab3466118bd3fb5cb0c4fddbdd7ff8e21188314ffed4ecad83fb7689d57d7",
    "037b7f8cea06cfd5a96023341441b52431e31867c6f3716e5f20be24b709d023eb"
];

var multiSigTx = new bitcore.Transaction()
    .from(utxo, pubs, 2)
    .to("n1fqBkX6GtUsaV3EPqjpHcTuWhAQQgKXov", 10000)
    .fee(5430)
    .change("2MzuRYCXSHcBDEsSS4rziShvEzWSfoDe2zL")
    .sign(["083b63f9dbaa100c1714e05a04c20ab32f83f7726ba54a682013a13df6a92949" ,"5bef634cdcc7f9c2fd2b429d1ddf6cae56923433b6e8b2c5515cf87d84751e0a"]);

var txSerialized = multiSigTx.serialize(true);

// Broadcast

insight.broadcast(txSerialized, function(err, returnedTxId) {
  if (err) {
    console.log(err);
  } else {
    console.log("Sent coins, tx id is: ");
    console.log(returnedTxId);
  }
});

Richard Macarthy

Posted 2015-05-27T12:15:52.170

Reputation: 213

Answers

2

I figured this out, the new transaction code should look like this with the address generated using fromString() method using bitcore.

var address = bitcore.Address.fromString('2NEvGYDNxcVPZ2ThtmPKYoKBCEa3aJNjPL3');
var multiSigTx = new bitcore.Transaction()
    .from(utxo, pubs, 2)
    .to("mntnnj64W4po96m2ck4GXQJTAiKZQChpWB", 10000)
    .to("msR1bBwUWjTTNYFU5UTVNCXDUnEx1y2MMQ", 10000)
    .fee(5430)
    .change(address)
    .sign(["7e925007e09447fa6160597391d7a24f8f47e40222d6c94b06bd9cecee5eddff" ,"6b8adfd22b8dd3186ea5333602b39a59fc1c40c543dc3848ea88c6cd6b738594"]);

var txSerialized = multiSigTx.serialize(true);

This will then show the correct change output for the transaction.

console.log(multiSigTx.getChangeOutput());

Richard Macarthy

Posted 2015-05-27T12:15:52.170

Reputation: 213

with this you send 10000 Satoshis to each of the two different addresses? How could we send an amount (10000 Satoshis) to an Array of addresses?Suisse 2017-10-18T21:11:41.900