| | 1 | | namespace NLightning.Infrastructure.Protocol.Validators; |
| | 2 | |
|
| | 3 | | using Domain.Protocol.Constants; |
| | 4 | | using Domain.Protocol.Payloads; |
| | 5 | |
|
| | 6 | | public static class TxAddInputValidator |
| | 7 | | { |
| | 8 | | public static async void Validate(bool isInitiator, TxAddInputPayload input, int currentInputCount, Func<byte[], Tas |
| | 9 | | { |
| 0 | 10 | | if (isInitiator && (input.SerialId & 1) != 0) // Ensure even serial_id for initiator |
| | 11 | | { |
| 0 | 12 | | throw new InvalidOperationException("SerialId has the wrong parity."); |
| | 13 | | } |
| | 14 | |
|
| 0 | 15 | | if (!isSerialIdUnique(input.SerialId)) |
| | 16 | | { |
| 0 | 17 | | throw new InvalidOperationException("SerialId is already included in the transaction."); |
| | 18 | | } |
| | 19 | |
|
| 0 | 20 | | if (!await isValidPrevTx(input.PrevTx)) |
| | 21 | | { |
| 0 | 22 | | throw new InvalidOperationException("PrevTx is not a valid transaction."); |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | if (input.PrevTxVout >= GetOutputCount(input.PrevTx)) |
| | 26 | | { |
| 0 | 27 | | throw new InvalidOperationException("PrevTxVout is greater than or equal to the number of outputs on PrevTx. |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | if (!IsScriptPubKeyValid(input.PrevTx, input.PrevTxVout)) |
| | 31 | | { |
| 0 | 32 | | throw new InvalidOperationException("ScriptPubKey of the PrevTxVout output is not valid."); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | if (!isUniqueInput(input.PrevTx, input.PrevTxVout)) |
| | 36 | | { |
| 0 | 37 | | throw new InvalidOperationException("The PrevTx and PrevTxVout are identical to a previously added input."); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | if (currentInputCount >= InteractiveTransactionConstants.MAX_INPUTS_ALLOWED) |
| | 41 | | { |
| 0 | 42 | | throw new InvalidOperationException($"Cannot receive more than {InteractiveTransactionConstants.MAX_INPUTS_A |
| | 43 | | } |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private static uint GetOutputCount(byte[] prevTx) |
| | 47 | | { |
| | 48 | | _ = prevTx; |
| | 49 | | // TODO: Implement logic to parse prevTx and return the output count |
| 0 | 50 | | return 1; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | private static bool IsScriptPubKeyValid(byte[] prevTx, uint prevTxVout) |
| | 54 | | { |
| | 55 | | _ = prevTx; |
| | 56 | | _ = prevTxVout; |
| | 57 | | // TODO: Implement logic to parse prevTx and check the scriptPubKey of the given output |
| 0 | 58 | | return true; |
| | 59 | | } |
| | 60 | | } |