| | | 1 | | namespace NLightning.Infrastructure.Bitcoin.Services; |
| | | 2 | | |
| | | 3 | | using Domain.Money; |
| | | 4 | | using Domain.Protocol.Payloads; |
| | | 5 | | using Protocol.Interfaces; |
| | | 6 | | using Protocol.Validators; |
| | | 7 | | |
| | | 8 | | public class InteractiveTransactionService : IInteractiveTransactionService |
| | | 9 | | { |
| | | 10 | | private readonly LightningMoney _dustLimitAmount; |
| | | 11 | | private readonly bool _isInitiator; |
| | 0 | 12 | | private readonly Dictionary<ulong, TxAddInputPayload> _inputs = []; |
| | 0 | 13 | | private readonly Dictionary<ulong, TxAddOutputPayload> _outputs = []; |
| | | 14 | | |
| | 0 | 15 | | public InteractiveTransactionService(LightningMoney dustLimitAmount, bool isInitiator) |
| | | 16 | | { |
| | 0 | 17 | | _dustLimitAmount = dustLimitAmount; |
| | 0 | 18 | | _isInitiator = isInitiator; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public void AddInput(TxAddInputPayload input) |
| | | 22 | | { |
| | 0 | 23 | | TxAddInputValidator.Validate(_isInitiator, input, _inputs.Count, IsValidPrevTx, IsUniqueInput, IsSerialIdUnique) |
| | 0 | 24 | | _inputs.Add(input.SerialId, input); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public void AddOutput(TxAddOutputPayload output) |
| | | 28 | | { |
| | 0 | 29 | | TxAddOutputValidator.Validate(_isInitiator, output, _outputs.Count, IsSerialIdUnique, IsStandardScript, |
| | 0 | 30 | | _dustLimitAmount); |
| | 0 | 31 | | _outputs.Add(output.SerialId, output); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void RemoveInput(TxRemoveInputPayload input) |
| | | 35 | | { |
| | 0 | 36 | | TxRemoveInputValidator.Validate(_isInitiator, input, IsSerialIdPresent); |
| | 0 | 37 | | _inputs.Remove(input.SerialId); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public void RemoveOutput(TxRemoveOutputPayload output) |
| | | 41 | | { |
| | 0 | 42 | | TxRemoveOutputValidator.Validate(_isInitiator, output, IsSerialIdPresent); |
| | 0 | 43 | | _outputs.Remove(output.SerialId); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private Task<bool> IsValidPrevTx(byte[] prevTx) |
| | | 47 | | { |
| | | 48 | | // TODO: Implement logic to validate the prevTx by talking to bitcoind |
| | 0 | 49 | | return Task.FromResult(true); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private bool IsUniqueInput(byte[] prevTx, uint prevTxVout) |
| | | 53 | | { |
| | 0 | 54 | | return !_inputs.Values.Any(i => i.PrevTx.SequenceEqual(prevTx) && i.PrevTxVout == prevTxVout); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private bool IsSerialIdUnique(ulong serialId) |
| | | 58 | | { |
| | 0 | 59 | | return !_inputs.ContainsKey(serialId); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private bool IsSerialIdPresent(ulong serialId) |
| | | 63 | | { |
| | 0 | 64 | | return _inputs.ContainsKey(serialId); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private bool IsStandardScript(byte[] script) |
| | | 68 | | { |
| | | 69 | | // TODO: Check using NBitcoin |
| | 0 | 70 | | return script.Length > 0; |
| | | 71 | | } |
| | | 72 | | } |