| | 1 | | using Microsoft.Extensions.Options; |
| | 2 | | using NBitcoin; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Bitcoin.Factories; |
| | 5 | |
|
| | 6 | | using Domain.Bitcoin.Services; |
| | 7 | | using Domain.Money; |
| | 8 | | using Domain.Node.Options; |
| | 9 | | using Domain.Protocol.Factories; |
| | 10 | | using Outputs; |
| | 11 | | using Protocol.Models; |
| | 12 | | using Transactions; |
| | 13 | |
|
| | 14 | | public class CommitmentTransactionFactory : ICommitmentTransactionFactory |
| | 15 | | { |
| | 16 | | private readonly IFeeService _feeService; |
| | 17 | | private readonly NodeOptions _nodeOptions; |
| | 18 | |
|
| 100 | 19 | | public CommitmentTransactionFactory(IFeeService feeService, IOptions<NodeOptions> nodeOptions) |
| | 20 | | { |
| 100 | 21 | | _feeService = feeService; |
| 100 | 22 | | _nodeOptions = nodeOptions.Value; |
| 100 | 23 | | } |
| | 24 | |
|
| | 25 | | public CommitmentTransaction CreateCommitmentTransaction(FundingOutput fundingOutput, PubKey localPaymentBasepoint, |
| | 26 | | PubKey remotePaymentBasepoint, PubKey localDelayedPubKey, |
| | 27 | | PubKey revocationPubKey, LightningMoney toLocalAmount, |
| | 28 | | LightningMoney toRemoteAmount, uint toSelfDelay, |
| | 29 | | CommitmentNumber commitmentNumber, bool isChannelFunder, |
| | 30 | | params BitcoinSecret[] secrets) |
| | 31 | | { |
| 36 | 32 | | var commitmentTransaction = new CommitmentTransaction(_nodeOptions.AnchorAmount, _nodeOptions.DustLimitAmount, |
| 36 | 33 | | _nodeOptions.MustTrimHtlcOutputs, _nodeOptions.Network, |
| 36 | 34 | | fundingOutput, localPaymentBasepoint, |
| 36 | 35 | | remotePaymentBasepoint, localDelayedPubKey, |
| 36 | 36 | | revocationPubKey, toLocalAmount, toRemoteAmount, |
| 36 | 37 | | toSelfDelay, commitmentNumber, isChannelFunder); |
| | 38 | |
|
| 36 | 39 | | commitmentTransaction.ConstructTransaction(_feeService.GetCachedFeeRatePerKw()); |
| | 40 | |
|
| 36 | 41 | | commitmentTransaction.SignTransaction(secrets); |
| | 42 | |
|
| 36 | 43 | | return commitmentTransaction; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public CommitmentTransaction CreateCommitmentTransaction(FundingOutput fundingOutput, PubKey localPaymentBasepoint, |
| | 47 | | PubKey remotePaymentBasepoint, PubKey localDelayedPubKey, |
| | 48 | | PubKey revocationPubKey, LightningMoney toLocalAmount, |
| | 49 | | LightningMoney toRemoteAmount, uint toSelfDelay, |
| | 50 | | CommitmentNumber commitmentNumber, bool isChannelFunder, |
| | 51 | | IEnumerable<OfferedHtlcOutput> offeredHtlcs, |
| | 52 | | IEnumerable<ReceivedHtlcOutput> receivedHtlcs, |
| | 53 | | params BitcoinSecret[] secrets) |
| | 54 | | { |
| 64 | 55 | | var commitmentTransaction = new CommitmentTransaction(_nodeOptions.AnchorAmount, _nodeOptions.DustLimitAmount, |
| 64 | 56 | | _nodeOptions.MustTrimHtlcOutputs, _nodeOptions.Network, |
| 64 | 57 | | fundingOutput, localPaymentBasepoint, |
| 64 | 58 | | remotePaymentBasepoint, localDelayedPubKey, |
| 64 | 59 | | revocationPubKey, toLocalAmount, toRemoteAmount, |
| 64 | 60 | | toSelfDelay, commitmentNumber, isChannelFunder); |
| | 61 | |
|
| 312 | 62 | | foreach (var offeredHtlc in offeredHtlcs) |
| | 63 | | { |
| 92 | 64 | | commitmentTransaction.AddOfferedHtlcOutput(offeredHtlc); |
| | 65 | | } |
| | 66 | |
|
| 328 | 67 | | foreach (var receivedHtlc in receivedHtlcs) |
| | 68 | | { |
| 100 | 69 | | commitmentTransaction.AddReceivedHtlcOutput(receivedHtlc); |
| | 70 | | } |
| | 71 | |
|
| 64 | 72 | | commitmentTransaction.ConstructTransaction(_feeService.GetCachedFeeRatePerKw()); |
| | 73 | |
|
| 64 | 74 | | commitmentTransaction.SignTransaction(secrets); |
| | 75 | |
|
| 64 | 76 | | return commitmentTransaction; |
| | 77 | | } |
| | 78 | | } |