Как создать транзакцию в Nbitcoin C#
Ошибка транзакции Nbitcoin C# Я использую Push на https://live.blockcypher.com/btc/pushtx/, выдает ошибку Error validating transaction: Error running script for input 0 referencing b9ea9fa41f777485d5559f8cbfef696519c6b5c53a635fdd5733c7ceabd496f5 at 0: Script was NOT verified successfully.. .
using NBitcoin;
using System;
using System.Collections.Generic;
using System.Net;
class Program
{
static void Main(string[] args)
{
var senderPrivateKey = "cUL1cmQ6JxezE8mkatq8zc2YQY2eYKMpTxSxGzLERsGtKkicENjB";
var senderAddress = "tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5";
var receiverAddress = "tb1pr7h57eru4pqpksspw24vjn7kucyddnezhh23hd9dmu4vs9ksgfdqv7hejc";
var transactionHex = CreateAndSignTransaction(senderPrivateKey, senderAddress, receiverAddress);
Console.WriteLine("Transaction Hex: " + transactionHex);
}
static string CreateAndSignTransaction(string senderPrivateKey, string senderAddress, string receiverAddress)
{
var network = Network.TestNet;
var tx = Transaction.Create(network);
var receiverBitcoinAddress = BitcoinAddress.Create(receiverAddress, network);
var amountToSend = Money.Coins(0.00001m);
var changeBitcoinAddress = BitcoinAddress.Create(senderAddress, network);
var changeScriptPubKey = changeBitcoinAddress.ScriptPubKey;
var totalInputAmount = Money.Zero;
var inputs = GetUnspentOutputs(senderAddress);
foreach (var input in inputs)
{
var outPoint = input.Outpoint;
var txIn = new TxIn(outPoint);
// Создаем скрипт подписи
var secret = new BitcoinSecret(senderPrivateKey, network);
var publicKey = secret.GetAddress(ScriptPubKeyType.Segwit);
var witScriptPubKey = publicKey.ScriptPubKey;
txIn.ScriptSig = Script.Empty;
tx.Inputs.Add(txIn);
var coin = new Coin(outPoint, new TxOut(input.Amount, witScriptPubKey));
tx.Sign(secret, coin);
totalInputAmount += input.Amount;
}
// Создаем выходы
tx.Outputs.Add(amountToSend, receiverBitcoinAddress.ScriptPubKey);
var changeAmount = totalInputAmount - amountToSend;
if (changeAmount > Money.Zero)
{
tx.Outputs.Add(changeAmount, changeScriptPubKey);
}
return tx.ToHex();
}
static List<(OutPoint Outpoint, Money Amount, int Confirmation)> GetUnspentOutputs(string bitcoinAddress)
{
string url = $"https://blockstream.info/testnet/api/address/{bitcoinAddress}/utxo";
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var utxos = new List<(OutPoint Outpoint, Money Amount, int Confirmation)>();
foreach (var utxo in data)
{
OutPoint outpoint = new OutPoint(uint256.Parse((string)utxo["txid"]), (int)utxo["vout"]);
Money amount = Money.Coins((decimal)utxo["value"]);
int confirmation = utxo["confirmations"] != null ? (int)utxo["confirmations"] : 0;
utxos.Add((outpoint, amount, confirmation));
}
return utxos;
}
}
}
Hex code транзакции
01000000000103f596d4abcec73357dd5f633ac5b5c6196569efbf8c9f55d58574771fa49feab90000000000ffffffff47f67555d446fea2baac0d98472f9a35e7656a4d6471cb2135a08e58c33dc7a00100000000ffffffff74c3ef00508a1706dd2bbae245f2966cba17a7568cbe3d661862940ce86658180000000000ffffffff02e8030000000000002251201faf4f647ca8401b420172aac94fd6e608d6cf22bdd51bb4addf2ac816d0425a187dafe80a040000160014cc5b1bd11ac9a6e5f0f1837a987350ec82f0f4550247304402205be63509a16454894cac972ac924963f91e39eee22aff359c8bde339547c80e20220207ada38474d4c560416c8a87c5424707a5e97c6aa1b807c0d076cdabf4f043201210258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e024730440220421cbf183e88b03df45e7aa3f10a7d80ae7acfd34a6e307ec3fae304b774a2c7022031e38907fcf32517437a31edddfe0f1c53409d278985dcff166d32dc355bbff801210258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e024730440220628e0279ba7bfcd1ecce55ad85daf7eb71d4fceb5178ff9cd33a69742804c52a02202f3f3e23c8978565ea3dc50621fe6c5e1624ab6fc6301dff869365b7ce647bba01210258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e00000000
Decoded Transaction
{
"addresses": [
"tb1pr7h57eru4pqpksspw24vjn7kucyddnezhh23hd9dmu4vs9ksgfdqv7hejc",
"tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5"
],
"block_height": -1,
"block_index": -1,
"confirmations": 0,
"double_spend": false,
"fees": 0,
"hash": "a66f11acd8693d2e6e409bbb2acdeb594a2abaa9b02ba1b6152e4f033a187954",
"inputs": [
{
"addresses": [
"tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5"
],
"age": 2811605,
"output_index": 0,
"output_value": 17822,
"prev_hash": "b9ea9fa41f777485d5559f8cbfef696519c6b5c53a635fdd5733c7ceabd496f5",
"script_type": "pay-to-witness-pubkey-hash",
"sequence": 4294967295,
"witness": [
"304402205be63509a16454894cac972ac924963f91e39eee22aff359c8bde339547c80e20220207ada38474d4c560416c8a87c5424707a5e97c6aa1b807c0d076cdabf4f043201",
"0258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e"
]
},
{
"addresses": [
"tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5"
],
"age": 2811350,
"output_index": 1,
"output_value": 11765,
"prev_hash": "a0c73dc3588ea03521cb71644d6a65e7359a2f47980dacbaa2fe46d45575f647",
"script_type": "pay-to-witness-pubkey-hash",
"sequence": 4294967295,
"witness": [
"30440220421cbf183e88b03df45e7aa3f10a7d80ae7acfd34a6e307ec3fae304b774a2c7022031e38907fcf32517437a31edddfe0f1c53409d278985dcff166d32dc355bbff801",
"0258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e"
]
},
{
"addresses": [
"tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5"
],
"age": 2812672,
"output_index": 0,
"output_value": 14862,
"prev_hash": "185866e80c946218663dbe8c56a717ba6c96f245e2ba2bdd06178a5000efc374",
"script_type": "pay-to-witness-pubkey-hash",
"sequence": 4294967295,
"witness": [
"30440220628e0279ba7bfcd1ecce55ad85daf7eb71d4fceb5178ff9cd33a69742804c52a02202f3f3e23c8978565ea3dc50621fe6c5e1624ab6fc6301dff869365b7ce647bba01",
"0258556d400f25ebf63aae5a3b730f856d59da6a4a02ace582ba51e3b17ce7019e"
]
}
],
"outputs": [
{
"addresses": [
"tb1pr7h57eru4pqpksspw24vjn7kucyddnezhh23hd9dmu4vs9ksgfdqv7hejc"
],
"script": "51201faf4f647ca8401b420172aac94fd6e608d6cf22bdd51bb4addf2ac816d0425a",
"script_type": "pay-to-taproot",
"value": 1000
},
{
"addresses": [
"tb1qe3d3h5g6exnwtu83sdafsu6sajp0paz4ss7ne5"
],
"script": "0014cc5b1bd11ac9a6e5f0f1837a987350ec82f0f455",
"script_type": "pay-to-witness-pubkey-hash",
"value": 4444899999000
}
],
"preference": "low",
"received": "2024-05-07T14:27:18.849612491Z",
"relayed_by": "34.235.155.234",
"size": 530,
"total": 4444900000000,
"ver": 1,
"vin_sz": 3,
"vout_sz": 2,
"vsize": 288
}