<p>The verification passed. But nothing happen after that. The balance in both accounts remain the same.</p>
<p>Anything wrong in the way that I build my transaction?</p>
<pre><code>var fpk = new BitcoinSecret(&quot;&lt;my private key&gt;&quot;, Network.TestNet);
var fspk = fpk.GetAddress(ScriptPubKeyType.Legacy);
var api = new QBitNinjaClient(&quot;http://tapi.qbit.ninja/&quot;, fpk.Network);
var fbal = await api.GetBalance(fspk, true);
var fcoin = new List&lt;ICoin&gt;();
foreach (var o in fbal.Operations)
{
    if (o.Confirmations &lt; 2) continue; // 2 confirmations
    fcoin.AddRange(o.ReceivedCoins);
}

var builder = fpk.Network.CreateTransactionBuilder();
var tx = builder
    .AddCoins(fcoin)
    .AddKeys(new ISecret[] { fpk })
    .Send(new BitcoinPubKeyAddress(&quot;&lt;To address&gt;&quot;, fpk.Network), Money.Coins(0.001))
    .SubtractFees()
    .SendFees(Money.Coins(0.00001))
    .SetChange(fpk)
    .BuildTransaction(true);

Debug.Assert(builder.Verify(tx));
var r = await api.Broadcast(tx);
</code></pre>
<p>The response was success but with error code:</p>
<p><strong>ErrorCode: INVALID</strong></p>
<p><strong>Reason: &quot;Unknown&quot;</strong></p>
<pre><code>{
  &quot;hash&quot;: &quot;a7d08d4f1993ea1b4143738c9ebf7cb4a04cd26fae7e97e3fa0dd0994718cf70&quot;,
  &quot;ver&quot;: 1,
  &quot;vin_sz&quot;: 1,
  &quot;vout_sz&quot;: 2,
  &quot;lock_time&quot;: 0,
  &quot;size&quot;: 225,
  &quot;in&quot;: [
    {
      &quot;prev_out&quot;: {
        &quot;hash&quot;: &quot;a98e4e3ad94d69f415c3849aedfb3d371afb24f49096b4b6de250c52ea9e7608&quot;,
        &quot;n&quot;: 0
      },
      &quot;scriptSig&quot;: &quot;304402201064fbd041900c2fe594a46cd4e0536427c94c82934bd125b3d5efd2863838d802206256840d67972126470443c71cfe1b72d2198a1d771a5eead54123573bafe87a01 030e9c36c9e37c2047f8c42cc5d56ff5ab1fc1b51b716fbbb2fc20201618401c0c&quot;
    }
  ],
  &quot;out&quot;: [
    {
      &quot;value&quot;: &quot;0.00998000&quot;,
      &quot;scriptPubKey&quot;: &quot;OP_DUP OP_HASH160 35343be6a983983c1a69871ddf3d604126a8e06f OP_EQUALVERIFY OP_CHECKSIG&quot;
    },
    {
      &quot;value&quot;: &quot;0.00009000&quot;,
      &quot;scriptPubKey&quot;: &quot;OP_DUP OP_HASH160 6652f3e81f9f8a832fecf707fc7b5757bf7faff5 OP_EQUALVERIFY OP_CHECKSIG&quot;
    }
  ]
}
</code></pre>

CC BY-SA 4.0
2021-04-10T08:54:39.003Z
I have just noticed that my ErrorCode = Invalid, and Reason: Unknown, but my transaction did went through. I am confused now.
s k
CC BY-SA 4.0
2021-04-12T00:46:24.560Z
@Prayank You need to put in your own *private* and *public* keys into the code.
s k
CC BY-SA 4.0
2021-04-13T03:39:12.013Z
@Prayank I tried to run your code, but there is no balance in the account. May I know which faucet is able to drop coins for segwit address?
s k
CC BY-SA 4.0
2021-04-13T07:48:08.313Z
Not working. In fact I am using the private key that shown in your code.
s k
CC BY-SA 4.0
2021-04-14T02:12:04.133Z
@Prayank I have confirmed that sending to a SegWit key will returns with Error = null, but Legacy keys will result with Error = Unknown, even though the transaction is successful.
s k
CC BY-SA 4.0
2021-04-14T14:12:42.803Z
I don't like the way that *bitcoin-cli* works, it forces me to create a wallet which I think should have let us handle ourselves. I am using ``QBitNinja.GetBalance(<pubKey>, true)`` to get all the unspent amount for particular address.
s k
<nbitcoin>
NBitcoin how to send a transaction?
s k
# NBitcoin how to send a transaction?

(Asked by: username_0 on 2021-04-09)

The verification passed. But nothing happen after that. The balance in both accounts remain the same.
Anything wrong in the way that I build my transaction?
var fpk = new BitcoinSecret("", Network.TestNet);
var fspk = fpk.GetAddress(ScriptPubKeyType.Legacy);
var api = new QBitNinjaClient("http://tapi.qbit.ninja/", fpk.Network);
var fbal = await api.GetBalance(fspk, true);
var fcoin = new List();
foreach (var o in fbal.Operations)
{
    if (o.Confirmations < 2) continue; // 2 confirmations
    fcoin.AddRange(o.ReceivedCoins);
}

var builder = fpk.Network.CreateTransactionBuilder();
var tx = builder
    .AddCoins(fcoin)
    .AddKeys(new ISecret[] { fpk })
    .Send(new BitcoinPubKeyAddress("", fpk.Network), Money.Coins(0.001))
    .SubtractFees()
    .SendFees(Money.Coins(0.00001))
    .SetChange(fpk)
    .BuildTransaction(true);

Debug.Assert(builder.Verify(tx));
var r = await api.Broadcast(tx);

The response was success but with error code:
ErrorCode: INVALID
Reason: "Unknown"
{
  "hash": "a7d08d4f1993ea1b4143738c9ebf7cb4a04cd26fae7e97e3fa0dd0994718cf70",
  "ver": 1,
  "vin_sz": 1,
  "vout_sz": 2,
  "lock_time": 0,
  "size": 225,
  "in": [
    {
      "prev_out": {
        "hash": "a98e4e3ad94d69f415c3849aedfb3d371afb24f49096b4b6de250c52ea9e7608",
        "n": 0
      },
      "scriptSig": "304402201064fbd041900c2fe594a46cd4e0536427c94c82934bd125b3d5efd2863838d802206256840d67972126470443c71cfe1b72d2198a1d771a5eead54123573bafe87a01 030e9c36c9e37c2047f8c42cc5d56ff5ab1fc1b51b716fbbb2fc20201618401c0c"
    }
  ],
  "out": [
    {
      "value": "0.00998000",
      "scriptPubKey": "OP_DUP OP_HASH160 35343be6a983983c1a69871ddf3d604126a8e06f OP_EQUALVERIFY OP_CHECKSIG"
    },
    {
      "value": "0.00009000",
      "scriptPubKey": "OP_DUP OP_HASH160 6652f3e81f9f8a832fecf707fc7b5757bf7faff5 OP_EQUALVERIFY OP_CHECKSIG"
    }
  ]
}

**Tags:** nbitcoin

> **Comments:**
> **username_0**: I have just noticed that my ErrorCode = Invalid, and Reason: Unknown, but my transaction did went through. I am confused now.  
> **username_0**: @Prayank You need to put in your own *private* and *public* keys into the code.  
> **username_0**: @Prayank I tried to run your code, but there is no balance in the account. May I know which faucet is able to drop coins for segwit address?  
> **username_0**: Not working. In fact I am using the private key that shown in your code.  
> **username_0**: @Prayank I have confirmed that sending to a SegWit key will returns with Error = null, but Legacy keys will result with Error = Unknown, even though the transaction is successful.  
> **username_0**: I don't like the way that *bitcoin-cli* works, it forces me to create a wallet which I think should have let us handle ourselves. I am using ``QBitNinja.GetBalance(<pubKey>, true)`` to get all the unspent amount for particular address.  

---

## 0 answers

