How to Use TransactionBuilder in NBitcoin

4

I have used this code. It returns a success, but does not send bitcoin.

    var txRepo = new NoSqlTransactionRepository(); 

    // Let’s me introduce you, Alice, Bob, Satoshi, and Nico with their private keys.
    BitcoinSecret alice = new BitcoinSecret("");
    BitcoinSecret satoshi = new BitcoinSecret("");

    var network = alice.Network;     

    Transaction aliceFunding = new Transaction()
    {
        Outputs =
            {
                new TxOut("0.0009", alice.PrivateKey.PubKey)
            }
    };
    Coin[] aliceCoins = aliceFunding
                            .Outputs
                            .Select((o, i) => new Coin(new OutPoint(aliceFunding.GetHash(), i), o))
                            .ToArray();

    //Now Alice wants to send 1.00 BTC to Satoshi, with 0.001 BTC fees for miners.
    var txBuilder = new TransactionBuilder();
    var tx = txBuilder
        .AddCoins(aliceCoins)
        .AddKeys(alice.PrivateKey)
        .Send(satoshi.GetAddress(), "0.0002")
        .SendFees("0.0001")
        .SetChange(alice.GetAddress())
        .BuildTransaction(true);

    txRepo.Put(tx.GetHash(), tx);
    //Assert(txBuilder.Verify(tx)); //check fully signed
    Literal1.Text = tx.GetHash().ToString();

    var client = new QBitNinjaClient(network);
    BroadcastResponse broadcastResponse = client.Broadcast(tx).Result;


    if (!broadcastResponse.Success)
    {
        Literal1.Text = (string.Format("ErrorCode: {0}", broadcastResponse.Error.ErrorCode)).ToString();
        //Literal2.Text = ("Error message: " + broadcastResponse.Error.Reason).ToString();
    }
    else
    {
        Literal1.Text = ("Success! You can check out the hash of the transaction in any block explorer:").ToString();
        //Literal2.Text = (transaction.GetHash()).ToString();
    }

Hitesh

Posted 2017-01-23T14:04:50.663

Reputation: 51

No answers