I am using NBitcoin (I'm new to it) to sign a transaction. Here Transaction sign(secret, bool) method is giving error. (I've searched the Internet, but got no help.) Instead of bool it says to pass Coin object, I'm using the latest stable version of NBitcoin. How should I do this? Here's my code:

var fee = Money.Coins(0.0001m);

        Transaction payment=Transaction.Create(bitcoinNetwork);
        payment.Inputs.Add(new TxIn()
            PrevOut = new OutPoint(fundingTransaction.GetHash(), 1)

        payment.Outputs.Add(new TxOut()
            Value = amount-fee,
            ScriptPubKey = toAddress.ScriptPubKey

        var output = fundingTransaction.Outputs[0];

        payment.Outputs.Add(new TxOut()
            Value = output.Value - amount - fee,
            ScriptPubKey = output.ScriptPubKey

        var message = "Thanks :)";
        var bytes = Encoding.UTF8.GetBytes(message);
        payment.Outputs.Add(new TxOut()
            Value = Money.Zero,
            ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)


        payment.Inputs[0].ScriptSig = fundingTransaction.Outputs[1].ScriptPubKey;

        payment.Sign(secret, false); // the problem arises here

        using (var node = Node.Connect(Network.Main))
            Console.WriteLine("Doing version handshake");
            Console.WriteLine("Sending message");
            node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash()));
            node.SendMessage(new TxPayload(payment));

Sign method expects two arguments: private key and coins to be spent

false makes no sense in that line according to the documentation mentioned in the below link but I found similar code in another question NBitcoin: signing a transaction in TestNet so I will have to try on my system in VS 2019:


transaction.Sign(bitcoinPrivateKey, receivedCoins.ToArray());

UPDATE(I tried the below things and it worked for me):

Create a Private key and get address:


Send some amount to the address: https://blockstream.info/testnet/tx/6266c8262610ab0790f9f1841930bb27666588be8ab343a7e0470b7bb48a655b

Use the below code to create a Tx, sign and broadcast it:

            var bitcoinPrivateKey = new BitcoinSecret("cPof7e5g6xfgB6AZrc6XVTVwA4efLJurh9kVxa6FRChbr8Jyqaon", Network.TestNet);
            var network = bitcoinPrivateKey.Network;
            var address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy);

            var client = new QBitNinjaClient(network);
            var transactionId = uint256.Parse("6266c8262610ab0790f9f1841930bb27666588be8ab343a7e0470b7bb48a655b");
            var transactionResponse = client.GetTransaction(transactionId).Result;

            var receivedCoins = transactionResponse.ReceivedCoins;
            OutPoint outPointToSpend = null;
            foreach (var coin in receivedCoins)
                if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
                    outPointToSpend = coin.Outpoint;

            var transaction = Transaction.Create(network);
            transaction.Inputs.Add(new TxIn()
                PrevOut = outPointToSpend

            var myAddress = BitcoinAddress.Create("tb1q5va20jy9dg30zdyy0nakg5y95trdr3gtw5jy9d", Network.TestNet);

            var txOutAmount = new Money(0.01m, MoneyUnit.BTC);

            // Tx fee
            var minerFee = new Money(0.0005m, MoneyUnit.BTC);

            // Change
            var txInAmount = (Money)receivedCoins[(int)outPointToSpend.N].Amount;
            var changeAmount = txInAmount - txOutAmount - minerFee;

            transaction.Outputs.Add(txOutAmount, myAddress.ScriptPubKey);
            transaction.Outputs.Add(changeAmount, bitcoinPrivateKey.ScriptPubKey);

            transaction.Inputs[0].ScriptSig = address.ScriptPubKey;

            //Sign Tx
            transaction.Sign(bitcoinPrivateKey, receivedCoins.ToArray());

            //Broadcast Tx
            BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;

            if (!broadcastResponse.Success)
                Console.Error.WriteLine("ErrorCode: " + broadcastResponse.Error.ErrorCode);
                Console.Error.WriteLine("Error message: " + broadcastResponse.Error.Reason);
                Console.WriteLine("Success! Tx:");


Tx: https://blockstream.info/testnet/tx/0ada6696fb134ebc4f96a55a3d5b60d90e5a723ec2e27634bb1777730e7df914

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.