如何使用多个TxIn(事务输入)广播NBitcoin事务

时间:2017-11-22 07:59:27

标签: c# transactions blockchain nbitcoin

我在TestNet上运行。我试图添加多个事务输入TxIn(),以便从这些输入中花费。当我广播我的交易时,它返回成功,但我无法在块浏览器上看到它。我用一个TxIn()处理了一个事务,当我广播它时,它成功了,我可以在块浏览器上查看它。我已经在这2天以上了。非常感谢你的帮助人员

var bitcoinPrivateKey = new BitcoinSecret("cNZupUgfs54DmsShwxa1wpomQcszUtuJYFvx9zWPbXrT7KsWtiUd");
        var network = bitcoinPrivateKey.Network;
        var address = bitcoinPrivateKey.GetAddress();
        var client = new QBitNinjaClient(network);
        var balance = client.GetBalance(address).Result;

        var transactionId = uint256.Parse("06c0aec7543467951abad0c28998a2c1fc1cdc34e01113f8ec1fdb22be854771");
        var transactionResponse = client.GetTransaction(transactionId).Result;

        var tx = new Transaction();
        foreach (var operation in balance.Operations)
        {
            OutPoint spendOutPoint = null;

            var coinsReceived = operation.ReceivedCoins;
            foreach (var coin in coinsReceived)
            {
                if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
                {
                    spendOutPoint = coin.Outpoint;
                    tx.Inputs.Add(new TxIn()
                    {
                        PrevOut = spendOutPoint
                    });
                }
            }


        }
        var chimaTestDestinationAddress = new BitcoinPubKeyAddress("mxgN2AiqHjKfGvo6Y57fAe4Y754rPdKf4P");
        TxOut chimaTestDestinationAddressTxOut = new TxOut()
        {
            Value = new Money((decimal)0.50, MoneyUnit.BTC),
            ScriptPubKey = chimaTestDestinationAddress.ScriptPubKey
        };
        TxOut ugoChangeBackTxOut = new TxOut()
        {
            Value = new Money((decimal)2.98, MoneyUnit.BTC),
            ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
        };
        tx.Outputs.Add(chimaTestDestinationAddressTxOut);
        tx.Outputs.Add(ugoChangeBackTxOut);
        var msg = "ugo the jedi master";
        var msgBytes = Encoding.UTF8.GetBytes(msg);

        TxOut txDesc = new TxOut()
        {
            Value = Money.Zero,
            ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(msgBytes)
        };
        tx.Outputs.Add(txDesc);


        tx.Inputs[0].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
        tx.Inputs[1].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
        tx.Inputs[2].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
        tx.Inputs[3].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
        tx.Inputs[4].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
        tx.Sign(bitcoinPrivateKey, false);

        BroadcastResponse broadcast = client.Broadcast(tx).Result;
        if (!broadcast.Success)
        {
            Console.WriteLine("ErrorCode: " + broadcast.Error.ErrorCode);
            Console.WriteLine("Error message: " + broadcast.Error.Reason);
        }
        else
        {
            Console.WriteLine("Success, you can now checkout the transaction in any block explorer");
            Console.WriteLine("Hash: " + tx.GetHash());
        }

0 个答案:

没有答案