| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using NBitcoin; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Infrastructure.Bitcoin.Builders; |
| | | 6 | | |
| | | 7 | | using Domain.Bitcoin.Transactions.Constants; |
| | | 8 | | using Domain.Bitcoin.Transactions.Models; |
| | | 9 | | using Domain.Bitcoin.ValueObjects; |
| | | 10 | | using Domain.Node.Options; |
| | | 11 | | using Interfaces; |
| | | 12 | | using Outputs; |
| | | 13 | | |
| | | 14 | | public class FundingTransactionBuilder : IFundingTransactionBuilder |
| | | 15 | | { |
| | | 16 | | private readonly Network _network; |
| | | 17 | | private readonly IServiceProvider _serviceProvider; |
| | | 18 | | private readonly ILogger<FundingTransactionBuilder> _logger; |
| | | 19 | | |
| | 0 | 20 | | public FundingTransactionBuilder(IOptions<NodeOptions> nodeOptions, IServiceProvider serviceProvider, |
| | 0 | 21 | | ILogger<FundingTransactionBuilder> logger) |
| | | 22 | | { |
| | 0 | 23 | | _serviceProvider = serviceProvider; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | _network = Network.GetNetwork(nodeOptions.Value.BitcoinNetwork) ?? |
| | 0 | 26 | | throw new ArgumentException("Invalid Bitcoin network specified", nameof(nodeOptions)); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public SignedTransaction Build(FundingTransactionModel transaction) |
| | | 30 | | { |
| | 0 | 31 | | var coins = transaction.Utxos.ToArray(); |
| | 0 | 32 | | if (coins.Length == 0) |
| | 0 | 33 | | throw new ArgumentException("UTXO set cannot be empty"); |
| | | 34 | | |
| | 0 | 35 | | var totalInputAmount = coins.Sum(x => x.Amount); |
| | | 36 | | |
| | 0 | 37 | | if (_logger.IsEnabled(LogLevel.Trace)) |
| | 0 | 38 | | _logger.LogTrace("Building funding transaction with {UtxoCount} UTXOs for amount {FundingAmount}", |
| | 0 | 39 | | coins.Length, transaction.FundingOutput.Amount); |
| | | 40 | | |
| | | 41 | | // Create a new Bitcoin transaction |
| | 0 | 42 | | var tx = Transaction.Create(_network); |
| | | 43 | | |
| | | 44 | | // Set the transaction version as per BOLT spec |
| | 0 | 45 | | tx.Version = TransactionConstants.FundingTransactionVersion; |
| | | 46 | | |
| | | 47 | | // Add all inputs from the UTXO set |
| | 0 | 48 | | foreach (var coin in coins) |
| | 0 | 49 | | tx.Inputs.Add(new OutPoint(new uint256(coin.TxId), coin.Index)); |
| | | 50 | | |
| | | 51 | | // Convert and add the funding output |
| | 0 | 52 | | var fundingOutput = new FundingOutput(transaction.FundingOutput.Amount, |
| | 0 | 53 | | new PubKey(transaction.FundingOutput.LocalFundingPubKey), |
| | 0 | 54 | | new PubKey(transaction.FundingOutput.RemoteFundingPubKey)); |
| | 0 | 55 | | tx.Outputs.Add(fundingOutput.ToTxOut()); |
| | | 56 | | |
| | | 57 | | // Check if we are paying a change address |
| | 0 | 58 | | if (transaction.ChangeAddress is not null) |
| | | 59 | | { |
| | 0 | 60 | | var changeAmount = totalInputAmount - transaction.Fee - fundingOutput.Amount; |
| | 0 | 61 | | var bitcoinAddress = BitcoinAddress.Create(transaction.ChangeAddress.Address, _network); |
| | 0 | 62 | | tx.Outputs.Add(new TxOut(new Money(changeAmount.Satoshi), bitcoinAddress)); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Update the funding output info with transaction details |
| | 0 | 66 | | transaction.FundingOutput.TransactionId = tx.GetHash().ToBytes(); |
| | 0 | 67 | | transaction.FundingOutput.Index = 0; |
| | | 68 | | |
| | 0 | 69 | | if (_logger.IsEnabled(LogLevel.Information)) |
| | 0 | 70 | | _logger.LogInformation("Built funding transaction {TxId} with funding output at index 0", tx.GetHash()); |
| | | 71 | | |
| | | 72 | | // Return as SignedTransaction (note: needs to be signed by the signer afterwards) |
| | 0 | 73 | | return new SignedTransaction(tx.GetHash().ToBytes(), tx.ToBytes()); |
| | | 74 | | } |
| | | 75 | | } |