Exemple #1
0
        private static Transaction CreateSignTransaction(string privateKey, string destinationAddress, List <TxIn> inputs, decimal amountToSend, decimal amountToReturn, bool isTest = false, decimal fee = 0.0005m, bool donation = false, decimal donationAmount = 0.001m, string donationAddress = "", string otherInfo = "")
        {
            try
            {
                var network = GetNetwork(isTest);
                var secret  = new BitcoinSecret(privateKey, network);
                // key = secret.PrivateKey;
                Transaction transaction = new ConsensusFactory().CreateTransaction();

                foreach (var txIn in inputs)
                {
                    txIn.ScriptSig = secret.GetAddress().ScriptPubKey;
                    transaction.AddInput(txIn);
                }
                TxOut txout       = new TxOut();
                var   destination = BitcoinAddress.Create(destinationAddress, network);
                txout.ScriptPubKey = destination.ScriptPubKey;

                //Money moneyFee = Money.Satoshis(fee);
                Money moneyFee    = Money.Coins(fee);
                Money moneyToSend = Money.Coins(amountToSend);
                txout.Value = moneyToSend - moneyFee;                 //the fee money is taken of from the buyer, so if you want to pay him more, add the fee to the money you send.

                transaction.AddOutput(txout);

                if (donation)
                {
                    TxOut txoutDonation       = new TxOut();
                    var   destinationDonation = BitcoinAddress.Create(donationAddress, network);
                    txoutDonation.ScriptPubKey = destinationDonation.ScriptPubKey;
                    txoutDonation.Value        = Money.Coins(donationAmount);
                    transaction.AddOutput(txoutDonation);
                }

                //incase there is a change we need to bring it back to the sender.
                if (amountToReturn > 0)
                {
                    TxOut txoutChange       = new TxOut();
                    var   returnDestination = BitcoinAddress.Create(secret.GetAddress().ToString(), network);
                    txoutChange.ScriptPubKey = returnDestination.ScriptPubKey;
                    txoutChange.Value        = Money.Coins(amountToReturn);
                    transaction.AddOutput(txoutChange);
                }

                transaction.Sign(secret, false);
                return(transaction);
                //return transaction.GetHash().ToString();
                //transaction.ToHex().ToString() //Getting the Hex and pasting it in broadcast network like testNet blockcypher
            }
            catch (Exception ex)
            {
                throw new Exception("Error in CreateSignTransaction");
            }
        }