Exemplo n.º 1
0
        private static void Send(string walletPassword, string walletName, string walletAddress, string outPoint)
        {
            string walletFilePath = @"Wallets\";

            NBitcoin.BitcoinExtKey privateKey = null;
            try
            {
                Safe loadedSafe = Safe.Load(walletPassword, walletFilePath + walletName + ".json");
                for (int i = 0; i < 10; i++)
                {
                    if (loadedSafe.GetAddress(i).ToString() == walletAddress)
                    {
                        Write("Enter private key: ");
                        privateKey = new BitcoinExtKey(ReadLine());
                        if (!privateKey.Equals(loadedSafe.FindPrivateKey(loadedSafe.GetAddress(i))))
                        {
                            WriteLine("ERROR: Wrong private key!");
                            return;
                        }
                        break;
                    }
                }
            }
            catch (Exception)
            {
                WriteLine("ERROR: Wrong wallet or password!");
            }

            QBitNinjaClient client          = new QBitNinjaClient(Network.TestNet);
            var             balance         = client.GetBalance(BitcoinAddress.Create(walletAddress), false).Result;
            OutPoint        outPointToSpend = null;

            foreach (var entry in balance.Operations)
            {
                foreach (var coin in entry.ReceivedCoins)
                {
                    if (coin.Outpoint.ToString().Substring(0, coin.Outpoint.ToString().Length - 2) == outPoint)
                    {
                        outPointToSpend = coin.Outpoint;
                        break;
                    }
                }
            }

            var transaction = new Transaction();

            transaction.Inputs.Add(new TxIn()
            {
                PrevOut = outPointToSpend
            });

            Write("Enter address to send to: ");
            string addressToSendTo        = ReadLine();
            var    hallOfTheMakersAddress = BitcoinAddress.Create(addressToSendTo);

            Write("Enter amount to send: ");
            decimal amountToSend         = decimal.Parse(ReadLine());
            TxOut   hallOfTheMakersTxOut = new TxOut()
            {
                Value        = new Money(amountToSend, MoneyUnit.BTC),
                ScriptPubKey = hallOfTheMakersAddress.ScriptPubKey
            };

            Write("Enter amount to get back: ");
            decimal amountToGetBack = decimal.Parse(ReadLine());
            TxOut   changeBackTxOut = new TxOut()
            {
                Value        = new Money(amountToGetBack, MoneyUnit.BTC),
                ScriptPubKey = privateKey.ScriptPubKey
            };

            transaction.Outputs.Add(hallOfTheMakersTxOut);
            transaction.Outputs.Add(changeBackTxOut);

            Write("Enter message: ");
            var message = ReadLine();
            var bytes   = Encoding.UTF8.GetBytes(message);

            transaction.Outputs.Add(new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });

            transaction.Inputs[0].ScriptSig = privateKey.ScriptPubKey;
            transaction.Sign(privateKey, false);
            BroadcastResponse broadcast = client.Broadcast(transaction).Result;

            WriteLine(transaction);

            if (broadcast.Success)
            {
                WriteLine("TRANSACTION SUCCESSFULLY SENT!");
            }
            else
            {
                WriteLine("INTERNAL ERROR: Something went wrong!");
            }
        }
 public PSBT SignAll(BitcoinExtKey masterKey, SigHash sigHash = SigHash.All)
 {
     return(SignAll(masterKey?.ExtKey, sigHash));
 }
 public PSBT SignAll(BitcoinExtKey extkey, KeyPath keyPath, SigHash sigHash)
 {
     return(SignAll(extkey?.ExtKey, keyPath, sigHash));
 }
Exemplo n.º 4
0
        private static void Send(string password, string walletName, string wallet, string outPoint)
        {
            NBitcoin.BitcoinExtKey privateKey = null;
            try
            {
                // TODO: Load the Wallet and check the Private Key that the User provides.
                Safe loadSafe = Safe.Load(password, walletFilePath + walletName + ".json");
                for (int i = 0; i < 10; i++)
                {
                    if (loadSafe.GetAddress(i).ToString() == wallet)
                    {
                        Write("Enter private key: ");
                        privateKey = new BitcoinExtKey(ReadLine());

                        if (!privateKey.Equals(loadSafe.FindPrivateKey(loadSafe.GetAddress(i))))
                        {
                            WriteLine("Wrong private key!");
                            return;
                        }
                        break;
                    }
                }
            }
            catch
            {
                WriteLine("Wrong wallet or password!");
                return;
            }
            // TODO: Implement the Displaying of Balance and Sending the Transaction with the correct amount.

            QBitNinjaClient client          = new QBitNinjaClient(Network.TestNet);
            var             balance         = client.GetBalance(BitcoinAddress.Create(wallet, Network.TestNet), false).Result;
            OutPoint        outPointToSpend = null;

            foreach (var entry in balance.Operations)
            {
                foreach (var coin in entry.ReceivedCoins)
                {
                    if (coin.Outpoint.ToString().Substring(0, coin.Outpoint.ToString().Length - 2) == outPoint)
                    {
                        outPointToSpend = coin.Outpoint;
                        break;
                    }
                }
            }

            var transaction = Transaction.Create(Network.TestNet);

            transaction.Inputs.Add(new TxIn()
            {
                PrevOut = outPointToSpend
            });

            Write("Enter address to send to: ");
            string addressToSendTo       = ReadLine();
            var    hallOfTheMakerAddress = BitcoinAddress.Create(addressToSendTo, Network.TestNet);

            Write("Enter amount to send: ");
            decimal amountToSend      = decimal.Parse(ReadLine());
            TxOut   hallOfMakersTxOut = new TxOut()
            {
                Value        = new Money(amountToSend, MoneyUnit.BTC),
                ScriptPubKey = hallOfTheMakerAddress.ScriptPubKey
            };

            Write("Enter amount to get back: ");
            decimal amountToGetBack = decimal.Parse(ReadLine());
            TxOut   changeBackTxOut = new TxOut()
            {
                Value        = new Money(amountToGetBack, MoneyUnit.BTC),
                ScriptPubKey = privateKey.ScriptPubKey
            };

            transaction.Outputs.Add(hallOfMakersTxOut);
            transaction.Outputs.Add(changeBackTxOut);

            Write("Enter message: ");
            var message = ReadLine();
            var bytes   = Encoding.UTF8.GetBytes(message);

            transaction.Outputs.Add(new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });

            //transaction.Inputs[0].ScriptSig = privateKey.ScriptPubKey;

            // PowerPoint solution, not working
            // transaction.Sign(privateKey, false);

            // Found that but still not working

            // not sure about that => privateKey.PrivateKey
            Coin TestCoinVar       = new Coin(outPointToSpend, hallOfMakersTxOut);
            var  bitcoinPrivateKey = new BitcoinSecret(privateKey.PrivateKey, Network.TestNet);

            //transaction.Sign(bitcoinPrivateKey, transaction.Outputs.AsCoins());

            transaction.Inputs[0].ScriptSig = privateKey.ScriptPubKey;
            transaction.Sign(bitcoinPrivateKey, TestCoinVar);

            //Coin TestCoinVar = new Coin(outPointToSpend, hallOfMakersTxOut);
            //transaction.Sign(bitcoinPrivateKey, TestCoinVar);

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

            if (broadcastResponse.Success)
            {
                Console.WriteLine("Transaction send!");
            }
            else
            {
                Console.WriteLine("something went wrong!:-(");
            }
        }