Skip to content

Commit 3728143

Browse files
authored
Adds nonce to Payout model (#439)
* Add nonce for payout authorisation. * Renamed crypto helper to nonce helper.
1 parent d0eac3c commit 3728143

File tree

5 files changed

+81
-22
lines changed

5 files changed

+81
-22
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: CurrencyExtensions.cs
3+
//
4+
// Description: Contains extension methods for the Currency enum.
5+
//
6+
// Author(s):
7+
// Aaron Clauson ([email protected])
8+
//
9+
// History:
10+
// 23 Oct 2024 Aaron Clauson Created, Carne, Wexford, Ireland.
11+
//
12+
// License:
13+
// MIT
14+
//-----------------------------------------------------------------------------
15+
16+
namespace NoFrixion.MoneyMoov;
17+
18+
public static class CurrencyExtensions
19+
{
20+
public static string GetCurrencySymbol(this CurrencyTypeEnum currency) =>
21+
currency switch
22+
{
23+
CurrencyTypeEnum.BTC => "₿",
24+
CurrencyTypeEnum.GBP => "£",
25+
CurrencyTypeEnum.EUR => "€",
26+
_ => "€"
27+
};
28+
29+
public static bool IsFiat(this CurrencyTypeEnum currency) =>
30+
currency switch
31+
{
32+
CurrencyTypeEnum.BTC => false,
33+
_ => true
34+
};
35+
36+
public static int GetDecimalPlaces(this CurrencyTypeEnum currency) =>
37+
currency switch
38+
{
39+
CurrencyTypeEnum.BTC => PaymentsConstants.BITCOIN_ROUNDING_DECIMAL_PLACES,
40+
_ => PaymentsConstants.FIAT_ROUNDING_DECIMAL_PLACES
41+
};
42+
}

src/NoFrixion.MoneyMoov/Extensions/PaymentAmount.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,12 @@ public static class PaymentAmount
2121
/// Combines the display currency symbol and amount.
2222
/// </summary>
2323
public static string DisplayCurrencyAndAmount(CurrencyTypeEnum currency, decimal amount) =>
24-
GetCurrencySymbol(currency) + " " + GetDisplayAmount(currency, amount);
24+
currency.GetCurrencySymbol() + " " + GetDisplayAmount(currency, amount);
2525

2626
public static string GetDisplayAmount(CurrencyTypeEnum currency, decimal amount) =>
27-
IsFiat(currency) ? amount.ToString("N2") : amount.ToString("N8");
27+
currency.IsFiat() ? amount.ToString("N2") : amount.ToString("N8");
2828

2929
public static decimal GetRoundedAmount(CurrencyTypeEnum currency, decimal amount) =>
30-
Math.Round(amount, IsFiat(currency) ? PaymentsConstants.FIAT_ROUNDING_DECIMAL_PLACES : PaymentsConstants.BITCOIN_ROUNDING_DECIMAL_PLACES);
31-
32-
public static string GetCurrencySymbol(CurrencyTypeEnum currency) =>
33-
currency switch
34-
{
35-
CurrencyTypeEnum.BTC => "₿",
36-
CurrencyTypeEnum.GBP => "£",
37-
CurrencyTypeEnum.EUR => "€",
38-
_ => "€"
39-
};
40-
41-
private static bool IsFiat(CurrencyTypeEnum currency) =>
42-
currency switch
43-
{
44-
CurrencyTypeEnum.BTC => false,
45-
_ => true
46-
};
30+
Math.Round(amount, currency.IsFiat() ? PaymentsConstants.FIAT_ROUNDING_DECIMAL_PLACES : PaymentsConstants.BITCOIN_ROUNDING_DECIMAL_PLACES);
4731
}
4832

src/NoFrixion.MoneyMoov/Models/PaymentRequests/PaymentRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public PaymentMethodTypeEnum PaymentMethodTypes
301301
/// <summary>
302302
/// Gets the symbol for the payment request currency.
303303
/// </summary>
304-
public string DisplayCurrencySymbol() => PaymentAmount.GetCurrencySymbol(Currency);
304+
public string DisplayCurrencySymbol() => Currency.GetCurrencySymbol();
305305

306306
/// <summary>
307307
/// Gets the amount to display with the correct number of decimal places based on the currency type.

src/NoFrixion.MoneyMoov/Models/Payouts/Payout.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ public Counterparty? DestinationAccount
389389
/// </summary>
390390
public PaymentRailEnum PaymentRail { get; set; }
391391

392+
public string? Nonce { get; set; }
393+
392394
public NoFrixionProblem Validate()
393395
{
394396
var context = new ValidationContext(this, serviceProvider: null, items: null);
@@ -436,10 +438,11 @@ public string GetApprovalHash()
436438
ID.ToString() +
437439
AccountID.ToString() +
438440
Currency +
439-
Math.Round(Amount, 2).ToString() +
441+
Math.Round(Amount, Currency.GetDecimalPlaces()).ToString() +
440442
Destination.GetApprovalHash() +
441443
Scheduled.GetValueOrDefault().ToString() +
442-
ScheduleDate?.ToString("o");
444+
ScheduleDate?.ToString("o") +
445+
(string.IsNullOrEmpty(Nonce) ? string.Empty : Nonce);
443446

444447
return HashHelper.CreateHash(input);
445448
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: NonceHelper.cs
3+
//
4+
// Description: Helper functions for dealing with nonces.
5+
//
6+
// Author(s):
7+
// Aaron Clauson ([email protected])
8+
//
9+
// History:
10+
// 23 Oct 2024 Aaron Clauson Created, Carne, Wexford, Ireland.
11+
//
12+
// License:
13+
// MIT.
14+
//-----------------------------------------------------------------------------
15+
16+
using System.Security.Cryptography;
17+
18+
namespace NoFrixion.MoneyMoov;
19+
20+
public static class NonceHelper
21+
{
22+
public static string GenerateRandomNonce()
23+
{
24+
var nonce = new byte[64];
25+
var random = RandomNumberGenerator.Create();
26+
random.GetBytes(nonce);
27+
28+
return Convert.ToBase64String(nonce);
29+
}
30+
}

0 commit comments

Comments
 (0)