| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | 4 | |
|
| | 5 | | using Comparers; |
| | 6 | | using Domain.Money; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Represents a transaction output. |
| | 10 | | /// </summary> |
| | 11 | | public abstract class BaseOutput |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Gets the amount of the output in satoshis. |
| | 15 | | /// </summary> |
| 4460 | 16 | | public LightningMoney Amount { get; set; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Gets the scriptPubKey of the output. |
| | 20 | | /// </summary> |
| 1052 | 21 | | public Script ScriptPubKey { get; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Gets or sets the transaction ID of the output. |
| | 25 | | /// </summary> |
| 2880 | 26 | | public uint256? TxId { get; set; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets or sets the index of the output in the transaction. |
| | 30 | | /// </summary> |
| | 31 | | /// <remarks> |
| | 32 | | /// Output is nonexistent if this is -1. |
| | 33 | | /// </remarks> |
| 2520 | 34 | | public int Index { get; set; } |
| | 35 | |
|
| 268 | 36 | | public Script RedeemScript { get; } |
| | 37 | |
|
| | 38 | | public abstract ScriptType ScriptType { get; } |
| | 39 | |
|
| 44 | 40 | | protected BaseOutput(Script redeemScript, Script scriptPubKey, LightningMoney amount) |
| | 41 | | { |
| 44 | 42 | | ArgumentNullException.ThrowIfNull(redeemScript); |
| 44 | 43 | | ArgumentNullException.ThrowIfNull(scriptPubKey); |
| 44 | 44 | | ArgumentNullException.ThrowIfNull(amount); |
| | 45 | |
|
| 44 | 46 | | Amount = amount; |
| 44 | 47 | | RedeemScript = redeemScript; |
| 44 | 48 | | ScriptPubKey = scriptPubKey; |
| 44 | 49 | | TxId = uint256.Zero; |
| 44 | 50 | | Index = -1; |
| 44 | 51 | | } |
| 1316 | 52 | | protected BaseOutput(Script redeemScript, LightningMoney amount) |
| | 53 | | { |
| 1316 | 54 | | ArgumentNullException.ThrowIfNull(redeemScript); |
| 1316 | 55 | | ArgumentNullException.ThrowIfNull(amount); |
| | 56 | |
|
| 1316 | 57 | | Amount = amount; |
| 1316 | 58 | | RedeemScript = redeemScript; |
| 1316 | 59 | | ScriptPubKey = ScriptType switch |
| 1316 | 60 | | { |
| 1316 | 61 | | ScriptType.P2WPKH |
| 1316 | 62 | | or ScriptType.P2WSH |
| 1492 | 63 | | when redeemScript.ToString().StartsWith("0 ") => redeemScript, |
| 1316 | 64 | | ScriptType.P2WPKH |
| 1140 | 65 | | or ScriptType.P2WSH => redeemScript.WitHash.ScriptPubKey, |
| 0 | 66 | | ScriptType.P2SH => redeemScript.Hash.ScriptPubKey, |
| 0 | 67 | | _ => redeemScript.PaymentScript |
| 1316 | 68 | | }; |
| 1316 | 69 | | TxId = uint256.Zero; |
| 1316 | 70 | | Index = -1; |
| 1316 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Converts the output to a NBitcoin.TxOut. |
| | 75 | | /// </summary> |
| | 76 | | /// <returns>TxOut object.</returns> |
| | 77 | | public TxOut ToTxOut() |
| | 78 | | { |
| 512 | 79 | | return new TxOut((Money)Amount, ScriptPubKey); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public ScriptCoin ToCoin() |
| | 83 | | { |
| 180 | 84 | | if (Index == -1) |
| 16 | 85 | | throw new InvalidOperationException("Output is nonexistent. Sign the transaction first."); |
| | 86 | |
|
| 164 | 87 | | if (TxId is null || TxId == uint256.Zero || TxId == uint256.One) |
| 0 | 88 | | throw new InvalidOperationException("Transaction ID is not set. Sign the transaction first."); |
| | 89 | |
|
| 164 | 90 | | if (Amount.IsZero) |
| 4 | 91 | | throw new InvalidOperationException("You can't spend a zero amount output."); |
| | 92 | |
|
| 160 | 93 | | return new ScriptCoin(TxId, checked((uint)Index), Amount, ScriptPubKey, RedeemScript); |
| | 94 | | } |
| | 95 | |
|
| 24 | 96 | | public int CompareTo(BaseOutput? other) => other is null ? 1 : TransactionOutputComparer.Instance.Compare(this, othe |
| | 97 | | } |