Fix crash when invalid value is stored in UiConfig.json for FeeDisplayFormat
MODIFY
WalletWasabi.Fluent/ViewModels/TransactionBroadcasting/BroadcastTransactionViewModel.cs
WalletWasabi.Fluent/ViewModels/TransactionBroadcasting/BroadcastTransactionViewModel.cs
@@ -89,7 +89,7 @@ public partial class BroadcastTransactionViewModel : RoutableViewModel
? null
: totalInputValue - totalOutputValue;
- var feeDisplayFormat = (FeeDisplayFormat?)Services.UiConfig.FeeDisplayFormat ?? FeeDisplayFormat.BTC;
+ var feeDisplayFormat = Services.UiConfig.FeeDisplayFormat.GetEnumValueOrDefault(FeeDisplayFormat.BTC);
var feeMoneyUnit = feeDisplayFormat.ToMoneyUnit();
FeeString =

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionSummaryViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionSummaryViewModel.cs
@@ -62,7 +62,7 @@ public partial class TransactionSummaryViewModel : ViewModelBase
destinationAmount.GenerateFiatText(_wallet.Synchronizer.UsdExchangeRate, "USD");
AmountText = $"{btcAmountText}{fiatAmountText}";
- var feeDisplayFormat = (FeeDisplayFormat?)Services.UiConfig.FeeDisplayFormat ?? FeeDisplayFormat.BTC;
+ var feeDisplayFormat = Services.UiConfig.FeeDisplayFormat.GetEnumValueOrDefault(FeeDisplayFormat.BTC);
var btcFeeMoneyUnit = feeDisplayFormat.ToMoneyUnit();
var fee = _transaction.Fee;

MODIFY
WalletWasabi/Extensions/EnumExtension.cs
WalletWasabi/Extensions/EnumExtension.cs
@@ -7,9 +7,14 @@ public static class EnumExtensions
{
public static T? GetFirstAttribute<T>(this Enum value) where T : Attribute
{
+ var stringValue = value.ToString();
var type = value.GetType();
- var memberInfo = type.GetMember(value.ToString());
- var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
+ var memberInfo =
+ type.GetMember(stringValue)
+ .FirstOrDefault()
+ ?? throw new InvalidOperationException($"Enum of type '{typeof(T).FullName}' does not contain value '{stringValue}'");
+
+ var attributes = memberInfo.GetCustomAttributes(typeof(T), false);
return attributes.Any() ? (T)attributes[0] : null;
}
@@ -20,4 +25,14 @@ public static class EnumExtensions
return attribute is { } ? attribute.FriendlyName : value.ToString();
}
+
+ public static T? GetEnumValueOrDefault<T>(this int value, T defaultValue) where T: Enum
+ {
+ if (Enum.IsDefined(typeof(T), value))
+ {
+ return (T?)Enum.ToObject(typeof(T), value);
+ }
+
+ return defaultValue;
+ }
}

Fix crash when invalid value is stored in UiConfig.json for FeeDisplayFormat

(cherry picked from commit 550980aafdfb7eb02860cc5577186295157f1c71)