| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Protocol.Services; |
| | 4 | |
|
| | 5 | | using Domain.Bitcoin.Services; |
| | 6 | | using Domain.Protocol.Services; |
| | 7 | |
|
| | 8 | | public class DustService : IDustService |
| | 9 | | { |
| | 10 | | private readonly IFeeService _feeService; |
| | 11 | |
|
| 0 | 12 | | public DustService(IFeeService feeService) |
| | 13 | | { |
| 0 | 14 | | _feeService = feeService; |
| 0 | 15 | | } |
| | 16 | |
|
| | 17 | | public ulong CalculateP2PkhDustLimit() |
| | 18 | | { |
| | 19 | | const uint OUTPUT_SIZE = 34; |
| | 20 | | const uint INPUT_SIZE = 148; |
| 0 | 21 | | return CalculateDustLimit(OUTPUT_SIZE, INPUT_SIZE); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public ulong CalculateP2ShDustLimit() |
| | 25 | | { |
| | 26 | | const uint OUTPUT_SIZE = 32; |
| | 27 | | const uint INPUT_SIZE = 148; // Lower bound |
| 0 | 28 | | return CalculateDustLimit(OUTPUT_SIZE, INPUT_SIZE); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public ulong CalculateP2WpkhDustLimit() |
| | 32 | | { |
| | 33 | | const uint OUTPUT_SIZE = 31; |
| | 34 | | const uint INPUT_SIZE = 67; // Lower bound |
| 0 | 35 | | return CalculateDustLimit(OUTPUT_SIZE, INPUT_SIZE); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public ulong CalculateP2WshDustLimit() |
| | 39 | | { |
| | 40 | | const uint OUTPUT_SIZE = 43; |
| | 41 | | const uint INPUT_SIZE = 67; // Lower bound |
| 0 | 42 | | return CalculateDustLimit(OUTPUT_SIZE, INPUT_SIZE); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public ulong CalculateUnknownSegwitVersionDustLimit() |
| | 46 | | { |
| | 47 | | const uint OUTPUT_SIZE = 51; |
| | 48 | | const uint INPUT_SIZE = 67; // Lower bound |
| 0 | 49 | | return CalculateDustLimit(OUTPUT_SIZE, INPUT_SIZE); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private ulong CalculateDustLimit(uint outputSize, uint inputSize) |
| | 53 | | { |
| 0 | 54 | | var totalSize = outputSize + inputSize; |
| 0 | 55 | | return (totalSize * _feeService.GetCachedFeeRatePerKw() / 1000); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public bool IsDust(ulong amount, Script scriptPubKey) |
| | 59 | | { |
| 0 | 60 | | if (scriptPubKey.IsScriptType(ScriptType.P2PKH)) |
| | 61 | | { |
| 0 | 62 | | return amount < CalculateP2PkhDustLimit(); |
| | 63 | | } |
| 0 | 64 | | if (scriptPubKey.IsScriptType(ScriptType.P2SH)) |
| | 65 | | { |
| 0 | 66 | | return amount < CalculateP2ShDustLimit(); |
| | 67 | | } |
| 0 | 68 | | if (scriptPubKey.IsScriptType(ScriptType.P2WPKH)) |
| | 69 | | { |
| 0 | 70 | | return amount < CalculateP2WpkhDustLimit(); |
| | 71 | | } |
| 0 | 72 | | if (scriptPubKey.IsScriptType(ScriptType.P2WSH)) |
| | 73 | | { |
| 0 | 74 | | return amount < CalculateP2WshDustLimit(); |
| | 75 | | } |
| 0 | 76 | | if (scriptPubKey.ToBytes()[0] == (byte)OpcodeType.OP_RETURN) |
| | 77 | | { |
| 0 | 78 | | return false; // OP_RETURN outputs are never dust |
| | 79 | | } |
| 0 | 80 | | return amount < CalculateUnknownSegwitVersionDustLimit(); |
| | 81 | | } |
| | 82 | | } |