<p>First, you need to serialize the transaction:</p>

<pre><code>let txSerialized = transaction.serialize(true);
</code></pre>

<p>Then you can broadcast it:</p>

<pre><code>client.sendRawTransaction(txSerialized, (error, response) =&gt; { if (error) console.log(error); console.log(response); });
</code></pre>

CC BY-SA 4.0
2019-02-06T18:36:55.570Z
It is still not working, the serialize output looks like this:
01000000640051f5400e0359ef92789402a38f8791d3f6b713a0a74caa8b9bdfcdfb8a45e10000000000ffffffff037f0884a3fced8fdefac7015348235fc5194c363d8421814d50c6d581c5f0d60000000000ffffffff058ff97b67d41b971ffbf45c325a1b2ab2628f464a83596b200c6e096cf708960000000000ffffffff07ad884fb66af20de225a7dcbe885725172fb1c058dee217c435d19180641ece0000000000ffffffff0821b50e54f3ae6b6b6ba5504c....
The output goes on for about 6000 more digits.
Don't think it is the right way of doing it.
Deb
CC BY-SA 4.0
2019-02-06T18:39:52.523Z
When I tried to broadcast the result, it says:
message: 'absurdly-high-fee, 244999985000 > 10000000 (code 256)',
  code: -26,
  name: 'RpcError'
Deb
CC BY-SA 4.0
2019-02-06T19:07:23.460Z
Did you forget the change output? You need to send change back to the sender based on the tx fee you want to pay.
JBaczuk
CC BY-SA 4.0
2019-02-05T12:25:56.237Z
2019-02-05T12:25:56.237Z
JBaczuk
<p>I am trying to use the rpc command <code>listunspent</code> to get all the UTXOs relevant to an address, and then I am trying to pass it to a transaction, using bitcore-lib.
Below is my code:</p>

<pre><code>const Client = require('bitcoin-core');
const bitcore = require('bitcore-lib');
const client = new Client({ 
  network: 'regtest', 
  username: 'bitcoinuser', 
  password: 'bitcoinpass', 
  port: 18443 
});

var tx = new bitcore.Transaction();

var sourceAddress = ["mwkT2k1o6k4dF7ZZQ2VBbw7VedBkQA2CBK"];
var minconf = 6;
var maxconf = 999999;
var targetAddress = "2Mw3iwZdVXjeb7BN5hWqegFs8VdfZo5Rmdo";
var privateKey = "cQqVxdtPz95monJe8QJUE4h5JVtSSAjTQ1SU6z8PL9pmFj5V7ieA";

var txns = [];
client.listUnspent(minconf, maxconf, sourceAddress)
    .then(function(help){
    txns = help.slice();
    return txns;
  }).then(function(txns){
    return tx.from(txns).to(targetAddress, 15000).sign(privateKey);
  }).then(function(result){
    console.log(result);
  });
</code></pre>

<p>The result is similar to the transaction object.</p>

<p>What I want is to receive the transaction hash, which I can then broadcast.</p>

<p>Note: I am trying to run my core in regtest mode, I do not want to use livenet or testnet. I have also disclosed the public and private keys, as they do not hold any real value.</p>

<p>The first few lines of the output looks like this:</p>

<pre><code>Transaction {
  inputs:
   [ PublicKeyInput {
       witnesses: [],
       output:
        Output {
          _satoshisBN:
           BN { negative: 0, words: [ 16972032, 37 ], length: 2, red: null 
},
          _satoshis: 2500000000,
          _scriptBuffer:
           &lt;Buffer 21 02 0b 0f c0 79 9d 39 8d 83 be 39 08 2c 72 49 0a 57 00 0e c2 f5 e0 01 ab a1 ee 9b 07 19 cc bd fb e3 ac&gt;,
          _script:
           Script {
             chunks:
              [ { buf:
                   &lt;Buffer 02 0b 0f c0 79 9d 39 8d 83 be 39 08 2c 72 49 0a 57 00 0e c2 f5 e0 01 ab a1 ee 9b 07 19 cc bd fb e3&gt;,
                  len: 33,
                  opcodenum: 33 },
                { opcodenum: 172 } ],
             _isOutput: true } },
       prevTxId:
        &lt;Buffer e1 45 8a fb cd df 9b 8b aa 4c a7 a0 13 b7 f6 d3 91 87 8f a3 02 94 78 92 ef 59 03 0e 40 f5 51 00&gt;,
       outputIndex: 0,
       sequenceNumber: 4294967295,
       _script: Script { chunks: [], _isInput: true },
       _scriptBuffer: &lt;Buffer &gt; },
     PublicKeyInput {
       witnesses: [],
       output:
        Output {
          _satoshisBN:
</code></pre>

CC BY-SA 4.0
2019-02-04T14:54:47.493Z
What have you tried, and what is the result? You can't broadcast the transaction hash alone, you use `sendrawtransaction` rpc to broadcast a tx
JBaczuk
CC BY-SA 4.0
2019-02-04T16:10:10.457Z
What I am looking for is the transaction hash, which I will then broadcast using `sendrawtransaction`. But I get the above output which looks like an object. How will get the transaction hash? Check the JS code above, and please recommend changes.
Deb
CC BY-SA 4.0
2019-02-04T16:27:50.347Z
can you post the full output?
JBaczuk
CC BY-SA 4.0
2019-02-05T05:45:03.837Z
The full output is actually a repetition of the the first segment, the above segment is for one UTXO, there are some 500 UTXOs I have, so there is a similar segment for each UTXO. I want the transaction hash, what I am getting is this object. Can you help me figure it out?
Deb
CC BY-SA 4.0
2019-02-05T05:46:02.760Z
Each PublicKeyInput object is for one individual UTXO.
Deb
CC BY-SA 4.0
2019-02-05T05:57:27.670Z
I believe the transaction hash you need for each UTXO is the `prevTxId` property. To add it to a transaction, you'll also need the `outputIndex` property.
JBaczuk
CC BY-SA 4.0
2019-02-05T06:06:14.677Z
I want to get the hash of the current transaction, which will be then broadcasted.
Deb
<json-rpc><raw-transaction><bitcore><node.js><listunspent>
How to properly use the rpc command `listunspent` with `bitcore-lib` to create a transaction and get the transaction hash?
Deb
# How to properly use the rpc command `listunspent` with `bitcore-lib` to create a transaction and get the transaction hash?

(Asked by: username_0 on 2019-02-04)

I am trying to use the rpc command listunspent to get all the UTXOs relevant to an address, and then I am trying to pass it to a transaction, using bitcore-lib.
Below is my code:
const Client = require('bitcoin-core');
const bitcore = require('bitcore-lib');
const client = new Client({ 
  network: 'regtest', 
  username: 'bitcoinuser', 
  password: 'bitcoinpass', 
  port: 18443 
});

var tx = new bitcore.Transaction();

var sourceAddress = ["mwkT2k1o6k4dF7ZZQ2VBbw7VedBkQA2CBK"];
var minconf = 6;
var maxconf = 999999;
var targetAddress = "2Mw3iwZdVXjeb7BN5hWqegFs8VdfZo5Rmdo";
var privateKey = "cQqVxdtPz95monJe8QJUE4h5JVtSSAjTQ1SU6z8PL9pmFj5V7ieA";

var txns = [];
client.listUnspent(minconf, maxconf, sourceAddress)
    .then(function(help){
    txns = help.slice();
    return txns;
  }).then(function(txns){
    return tx.from(txns).to(targetAddress, 15000).sign(privateKey);
  }).then(function(result){
    console.log(result);
  });

The result is similar to the transaction object.
What I want is to receive the transaction hash, which I can then broadcast.
Note: I am trying to run my core in regtest mode, I do not want to use livenet or testnet. I have also disclosed the public and private keys, as they do not hold any real value.
The first few lines of the output looks like this:
Transaction {
  inputs:
   [ PublicKeyInput {
       witnesses: [],
       output:
        Output {
          _satoshisBN:
           BN { negative: 0, words: [ 16972032, 37 ], length: 2, red: null 
},
          _satoshis: 2500000000,
          _scriptBuffer:
           ,
          _script:
           Script {
             chunks:
              [ { buf:
                   ,
                  len: 33,
                  opcodenum: 33 },
                { opcodenum: 172 } ],
             _isOutput: true } },
       prevTxId:
        ,
       outputIndex: 0,
       sequenceNumber: 4294967295,
       _script: Script { chunks: [], _isInput: true },
       _scriptBuffer:  },
     PublicKeyInput {
       witnesses: [],
       output:
        Output {
          _satoshisBN:

**Tags:** json-rpc, raw-transaction, bitcore, node.js, listunspent

> **Comments:**
> **username_1**: What have you tried, and what is the result? You can't broadcast the transaction hash alone, you use `sendrawtransaction` rpc to broadcast a tx  
> **username_0**: What I am looking for is the transaction hash, which I will then broadcast using `sendrawtransaction`. But I get the above output which looks like an object. How will get the transaction hash? Check the JS code above, and please recommend changes.  
> **username_1**: can you post the full output?  
> **username_0**: The full output is actually a repetition of the the first segment, the above segment is for one UTXO, there are some 500 UTXOs I have, so there is a similar segment for each UTXO. I want the transaction hash, what I am getting is this object. Can you help me figure it out?  
> **username_0**: Each PublicKeyInput object is for one individual UTXO.  
> **username_1**: I believe the transaction hash you need for each UTXO is the `prevTxId` property. To add it to a transaction, you'll also need the `outputIndex` property.  
> **username_0**: I want to get the hash of the current transaction, which will be then broadcasted.  

---

## 1 answers

### Answer by username_1 on 2019-02-05. Score: 0

First, you need to serialize the transaction:
let txSerialized = transaction.serialize(true);

Then you can broadcast it:
client.sendRawTransaction(txSerialized, (error, response) => { if (error) console.log(error); console.log(response); });

> **Comments:**
> **username_0**: It is still not working, the serialize output looks like this: 01000000640051f5400e0359ef92789402a38f8791d3f6b713a0a74caa8b9bdfcdfb8a45e10000000000ffffffff037f0884a3fced8fdefac7015348235fc5194c363d8421814d50c6d581c5f0d60000000000ffffffff058ff97b67d41b971ffbf45c325a1b2ab2628f464a83596b200c6e096cf708960000000000ffffffff07ad884fb66af20de225a7dcbe885725172fb1c058dee217c435d19180641ece0000000000ffffffff0821b50e54f3ae6b6b6ba5504c.... The output goes on for about 6000 more digits. Don't think it is the right way of doing it.  
> **username_0**: When I tried to broadcast the result, it says: message: 'absurdly-high-fee, 244999985000 > 10000000 (code 256)',   code: -26,   name: 'RpcError'  
> **username_1**: Did you forget the change output? You need to send change back to the sender based on the tx fee you want to pay.  

---

