1

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("<my private key>", 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<ICoin>();
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("<To address>", 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"
    }
  ]
}
s k
  • 175
  • 6
  • I have just noticed that my ErrorCode = Invalid, and Reason: Unknown, but my transaction did went through. I am confused now. – s k Apr 10 '21 at 08:54
  • I am getting a different error when I run this code: Invalid base58 data –  Apr 11 '21 at 23:18
  • @Prayank You need to put in your own private and public keys into the code. – s k Apr 12 '21 at 00:46
  • Yes I did that. Will share the code when I switch on my desktop again. On mobile right now. –  Apr 12 '21 at 01:08
  • Its working for me. No errors. Had to replace BitcoinPubKeyAddress with BitcoinWitPubKeyAddress. Code: https://pastebin.com/sQbdzmzL Tx: https://tbtc.bitaps.com/1312cde5f927bb0dcfeed457c53cf2b85678a2dc02468dc951366f872b7d3a4a –  Apr 12 '21 at 13:16
  • @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 Apr 13 '21 at 03:39
  • https://coinfaucet.eu/en/btc-testnet/ –  Apr 13 '21 at 03:47
  • Not working. In fact I am using the private key that shown in your code. – s k Apr 13 '21 at 07:48
  • Create a new wallet in Bitcoin Core or Electrum (Testnet). And share one bitcoin address. I will send some amount to test. –  Apr 13 '21 at 09:51
  • @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 Apr 14 '21 at 02:12
  • That's weird or maybe normal and something we can ignore. TBH I also have few issues using NBitcoin. Example(last post here): https://gitter.im/MetacoSA/NBitcoin I will be using library in a project but mostly use core RPCs. Nicolas Dorier or other people good in C# and used NBitcoin more can help you better. –  Apr 14 '21 at 03:04
  • 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 Apr 14 '21 at 14:12
  • scantxoutset can be used for it: https://bitcoin.stackexchange.com/a/99288/ –  Apr 14 '21 at 14:24

0 Answers0