#c# #blockchain #bitcoin #nbitcoin
#c# #блокчейн #биткойн #nbitcoin
Вопрос:
Я использую NBitcoin для подписания транзакции. Здесь метод Transaction sign (secret, bool) выдает ошибку. (Я искал в Интернете, но никакой помощи.) Вместо bool он говорит передать объект монеты, как мне это сделать? Вот мой код:
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);
}
Ответ №1:
Я изменил свой код следующим образом (на случай, если кому-то понадобится в будущем):
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).Resu<
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).Resu<
return broadcastResponse.Success;
}