Transactions

NBitcoin 交易標誌(秘密,布爾)給出錯誤。而不是 bool 它說要傳遞 Coin Object

  • October 19, 2020

我正在使用 NBitcoin(我是新手)簽署交易。這裡 Transaction sign(secret, bool) 方法給出錯誤。(我在網際網路上搜尋過,但沒有得到任何幫助。)我使用的是最新穩定版本的 NBitcoin,而不是 bool 它說傳遞 Coin 對象。我該怎麼做?這是我的程式碼:

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)
       });

       Console.WriteLine(payment);

       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");
           node.VersionHandshake();
           Console.WriteLine("Sending message");
           node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash()));
           node.SendMessage(new TxPayload(payment));
           Thread.Sleep(500);
       }

Sign方法需要兩個參數:私鑰和要花費的硬幣

false根據以下連結中提到的文件,這行沒有任何意義,但我在另一個問題NBitcoin:signing a transaction in TestNet所以我必須在 VS 2019 中嘗試我的系統:

<https://programmingblockchain.gitbook.io/programmingblockchain/bitcoin_transfer/spend_your_coin#sign-your-transaction>


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

更新(我嘗試了以下方法,它對我有用):

創建私鑰並獲取地址:

測試程式碼-scr1

向地址發送一些金額:https ://blockstream.info/testnet/tx/6266c8262610ab0790f9f1841930bb27666588be8ab343a7e0470b7bb48a655b

使用下面的程式碼創建一個 Tx,簽名並廣播它:


           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);
           }
           else
           {
               Console.WriteLine("Success! Tx:");
               Console.WriteLine(transaction.GetHash());
           }

輸出

發送:https ://blockstream.info/testnet/tx/0ada6696fb134ebc4f96a55a3d5b60d90e5a723ec2e27634bb1777730e7df914

引用自:https://bitcoin.stackexchange.com/questions/99529