如何解决NBitcoin交易标志秘密,布尔给出错误而不是布尔它说通过硬币
我正在使用NBitcoin签署交易。在这里,交易符号(秘密,布尔)方法给出了错误。 (我已经在Internet上搜索了,但没有帮助。)与其说布尔值传递了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);
}
解决方法
我更改了代码,如下所示(以防将来有人需要):
public static bool SendBTC(string secret,string toAddress,decimal amount,string fundingTransactionHash)
{
Network bitcoinNetwork = Network.TestNet;
var bitcoinPrivateKey = new BitcoinSecret(secret,bitcoinNetwork);
var address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy);
var client = new QBitNinjaClient(bitcoinNetwork);
var transactionId = uint256.Parse(fundingTransactionHash);
var transactionResponse = client.GetTransaction(transactionId).Result;
var receivedCoins = transactionResponse.ReceivedCoins;
OutPoint outPointToSpend = null;
foreach (var coin in receivedCoins)
{
if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy).ScriptPubKey)
{
outPointToSpend = coin.Outpoint;
}
}
var transaction = Transaction.Create(bitcoinNetwork);
transaction.Inputs.Add(new TxIn()
{
PrevOut = outPointToSpend
});
var receiverAddress = BitcoinAddress.Create(toAddress,bitcoinNetwork);
var txOutAmount = new Money(amount,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,receiverAddress.ScriptPubKey);
transaction.Outputs.Add(changeAmount,bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy).ScriptPubKey);
transaction.Inputs[0].ScriptSig = address.ScriptPubKey;
//Sign Tx
transaction.Sign(bitcoinPrivateKey,receivedCoins.ToArray());
//Broadcast Tx
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
return broadcastResponse.Success;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。