| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | 4 | |
|
| | 5 | | using Comparers; |
| | 6 | | using Domain.Bitcoin.Outputs; |
| | 7 | | using Domain.Bitcoin.ValueObjects; |
| | 8 | | using Domain.Money; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Represents a transaction output. |
| | 12 | | /// </summary> |
| | 13 | | public abstract class BaseOutput : IOutput |
| | 14 | | { |
| | 15 | | internal Script ScriptPubKey; |
| | 16 | | internal uint256 TxIdHash; |
| | 17 | | internal Script RedeemScript; |
| | 18 | | internal Money NBitcoinAmount; |
| | 19 | |
|
| | 20 | | /// <inheritdoc /> |
| | 21 | | public LightningMoney Amount |
| | 22 | | { |
| 1456 | 23 | | get => LightningMoney.Satoshis(NBitcoinAmount.Satoshi); |
| 0 | 24 | | set => Money.Satoshis(value.Satoshi); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | /// <inheritdoc /> |
| | 28 | | public BitcoinScript BitcoinScriptPubKey |
| | 29 | | { |
| 0 | 30 | | get => ScriptPubKey.ToBytes(); |
| 0 | 31 | | set => ScriptPubKey = new Script(value); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | /// <inheritdoc /> |
| | 35 | | public BitcoinScript RedeemBitcoinScript |
| | 36 | | { |
| 28 | 37 | | get => RedeemScript.ToBytes(); |
| 0 | 38 | | set => RedeemScript = new Script(value); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets or sets the transaction ID of the output. |
| | 43 | | /// </summary> |
| | 44 | | public TxId TransactionId |
| | 45 | | { |
| 4 | 46 | | get => TxIdHash.ToBytes(); |
| 136 | 47 | | set => TxIdHash = new uint256(value); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| 144 | 51 | | public uint Index { get; set; } |
| | 52 | |
|
| | 53 | | public abstract ScriptType ScriptType { get; } |
| | 54 | |
|
| 0 | 55 | | protected BaseOutput(LightningMoney amount, Script redeemScript, Script scriptPubKey) |
| | 56 | | { |
| 0 | 57 | | ArgumentNullException.ThrowIfNull(redeemScript); |
| 0 | 58 | | ArgumentNullException.ThrowIfNull(scriptPubKey); |
| 0 | 59 | | ArgumentNullException.ThrowIfNull(amount); |
| | 60 | |
|
| 0 | 61 | | NBitcoinAmount = Money.Satoshis(amount.Satoshi); |
| 0 | 62 | | RedeemScript = redeemScript; |
| 0 | 63 | | ScriptPubKey = scriptPubKey; |
| 0 | 64 | | TxIdHash = uint256.Zero; |
| 0 | 65 | | } |
| | 66 | |
|
| 500 | 67 | | protected BaseOutput(LightningMoney amount, Script redeemScript) |
| | 68 | | { |
| 500 | 69 | | ArgumentNullException.ThrowIfNull(redeemScript); |
| 500 | 70 | | ArgumentNullException.ThrowIfNull(amount); |
| | 71 | |
|
| 500 | 72 | | NBitcoinAmount = Money.Satoshis(amount.Satoshi); |
| 500 | 73 | | RedeemScript = redeemScript; |
| 500 | 74 | | ScriptPubKey = ScriptType switch |
| 500 | 75 | | { |
| 500 | 76 | | ScriptType.P2WPKH |
| 500 | 77 | | or ScriptType.P2WSH |
| 612 | 78 | | when redeemScript.ToString().StartsWith("0 ") => redeemScript, |
| 500 | 79 | | ScriptType.P2WPKH |
| 388 | 80 | | or ScriptType.P2WSH => redeemScript.WitHash.ScriptPubKey, |
| 0 | 81 | | ScriptType.P2SH => redeemScript.Hash.ScriptPubKey, |
| 0 | 82 | | _ => redeemScript.PaymentScript |
| 500 | 83 | | }; |
| 500 | 84 | | TxIdHash = uint256.Zero; |
| 500 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Converts the output to a NBitcoin.TxOut. |
| | 89 | | /// </summary> |
| | 90 | | /// <returns>TxOut object.</returns> |
| | 91 | | public TxOut ToTxOut() |
| | 92 | | { |
| 392 | 93 | | return new TxOut(Money.Satoshis(Amount.Satoshi), ScriptPubKey); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public ScriptCoin ToCoin() |
| | 97 | | { |
| 4 | 98 | | if (TxIdHash is null || TxIdHash == uint256.Zero || TxIdHash == uint256.One) |
| 0 | 99 | | throw new InvalidOperationException("Transaction ID is not set. Sign the transaction first."); |
| | 100 | |
|
| 4 | 101 | | if (Amount.IsZero) |
| 0 | 102 | | throw new InvalidOperationException("You can't spend a zero amount output."); |
| | 103 | |
|
| 4 | 104 | | return new ScriptCoin(TxIdHash, Index, Money.Satoshis(Amount.Satoshi), ScriptPubKey, RedeemScript); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | public int CompareTo(IOutput? other) => |
| 4 | 108 | | other is BaseOutput baseOutput |
| 4 | 109 | | ? TransactionOutputComparer.Instance.Compare(this, baseOutput) |
| 4 | 110 | | : 1; |
| | 111 | | } |