Skip to content

Commit

Permalink
feat: Added EthereumAddress payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jan 29, 2024
1 parent 827079c commit 0c32ffe
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<Description>Modern cross-platform QR code generation, rendering and serialization.</Description>
<PackageTags>qr;qrcode;qrcoder;cross-platform;logo;modern;net8</PackageTags>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down
36 changes: 12 additions & 24 deletions src/libs/QrCodes/Payloads/BitcoinAddress.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
namespace QrCodes.Payloads;

/// <summary>
///
/// </summary>
public class BitcoinAddress : BitcoinLikeCryptoCurrencyAddress
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <param name="amount"></param>
/// <param name="label"></param>
/// <param name="message"></param>
public BitcoinAddress(
string address,
double? amount,
string? label = null,
string? message = null)
: base(
BitcoinLikeCryptoCurrencyType.Bitcoin,
address,
amount,
label,
message) { }
}
/// <inheritdoc />
public class BitcoinAddress(
string address,
double? amount = null,
string? label = null,
string? message = null)
: BitcoinLikeCryptoCurrencyAddress(
currencyType: BitcoinLikeCryptoCurrencyType.Bitcoin,
address: address,
amount: amount,
label: label,
message: message);
36 changes: 12 additions & 24 deletions src/libs/QrCodes/Payloads/BitcoinCashAddress.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
namespace QrCodes.Payloads;

/// <summary>
///
/// </summary>
public class BitcoinCashAddress : BitcoinLikeCryptoCurrencyAddress
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <param name="amount"></param>
/// <param name="label"></param>
/// <param name="message"></param>
public BitcoinCashAddress(
string address,
double? amount,
string? label = null,
string? message = null)
: base(
BitcoinLikeCryptoCurrencyType.BitcoinCash,
address,
amount,
label,
message) { }
}
/// <inheritdoc />
public class BitcoinCashAddress(
string address,
double? amount = null,
string? label = null,
string? message = null)
: BitcoinLikeCryptoCurrencyAddress(
currencyType: BitcoinLikeCryptoCurrencyType.BitcoinCash,
address: address,
amount: amount,
label: label,
message: message);
65 changes: 21 additions & 44 deletions src/libs/QrCodes/Payloads/BitcoinLikeCryptoCurrencyAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,32 @@
namespace QrCodes.Payloads;

/// <summary>
///
/// Generates a Bitcoin like cryptocurrency payment payload. <br/>
/// QR Codes with this payload can open a payment app. <br/>
/// According: https://en.bitcoin.it/wiki/BIP_0021 <br/>
/// Example: "bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=0.123" <br/>
/// </summary>
public class BitcoinLikeCryptoCurrencyAddress
/// <param name="currencyType">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="amount">Amount of coins to transfer</param>
/// <param name="label">Reference label</param>
/// <param name="message">Reference text aka message</param>
public class BitcoinLikeCryptoCurrencyAddress(
BitcoinLikeCryptoCurrencyAddress.BitcoinLikeCryptoCurrencyType currencyType,
string address,
double? amount = null,
string? label = null,
string? message = null)
{
private readonly BitcoinLikeCryptoCurrencyType _currencyType;
private readonly string _address, _label = string.Empty, _message = string.Empty;
private readonly double? _amount;

/// <summary>
/// Generates a Bitcoin like cryptocurrency payment payload. QR Codes with this payload can open a payment app.
/// </summary>
/// <param name="currencyType">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="amount">Amount of coins to transfer</param>
/// <param name="label">Reference label</param>
/// <param name="message">Reference text aka message</param>
// ReSharper disable once MemberCanBeProtected.Global
public BitcoinLikeCryptoCurrencyAddress(
BitcoinLikeCryptoCurrencyType currencyType,
string address,
double? amount,
string? label = null,
string? message = null)
{
_currencyType = currencyType;
_address = address;

if (!string.IsNullOrEmpty(label))
{
_label = Uri.EscapeDataString(label);
}

if (!string.IsNullOrEmpty(message))
{
_message = Uri.EscapeDataString(message);
}

_amount = amount;
}

/// <inheritdoc />
public override string ToString()
{
string? query = null;
var query = string.Empty;

var queryValues = new[]{
new KeyValuePair<string, string?>("label", _label),
new KeyValuePair<string, string?>("message", _message),
new KeyValuePair<string, string?>("amount", _amount?.ToString("#.########", CultureInfo.InvariantCulture))
new KeyValuePair<string, string?>("label", Uri.EscapeDataString(label ?? string.Empty)),
new KeyValuePair<string, string?>("message", Uri.EscapeDataString(message ?? string.Empty)),
new KeyValuePair<string, string?>("amount", amount?.ToString("#.########", CultureInfo.InvariantCulture))
};

if (queryValues.Any(keyPair => !string.IsNullOrEmpty(keyPair.Value)))
Expand All @@ -62,7 +39,7 @@ public override string ToString()
.ToArray());
}

return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), _currencyType)?.ToLower(CultureInfo.InvariantCulture)}:{_address}{query}";
return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType)?.ToLower(CultureInfo.InvariantCulture)}:{address}{query}";
}

/// <summary>
Expand All @@ -83,6 +60,6 @@ public enum BitcoinLikeCryptoCurrencyType
/// <summary>
///
/// </summary>
Litecoin
Litecoin,
}
}
37 changes: 37 additions & 0 deletions src/libs/QrCodes/Payloads/EthereumAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Globalization;

namespace QrCodes.Payloads;

/// <summary>
/// Generates a Ethereum like cryptocurrency payment payload. <br/>
/// A standard way of representing various transactions, especially payment requests in ether and ERC-20 tokens as URLs. <br/>
/// QR Codes with this payload can open a payment app. <br/>
/// According: https://github.com/ethereum/ercs/blob/master/ERCS/erc-681.md <br/>
/// Example: "ethereum:0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359?value=2.014e18" <br/>
/// </summary>
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
/// <param name="value">Amount of coins to transfer</param>
public class EthereumAddress(
string address,
double? value = null)
{
/// <inheritdoc />
public override string ToString()
{
var query = string.Empty;

var queryValues = new[]{
new KeyValuePair<string, string?>("value", value?.ToString("#.000e0", CultureInfo.InvariantCulture))
};

if (queryValues.Any(keyPair => !string.IsNullOrEmpty(keyPair.Value)))
{
query = "?" + string.Join("&", queryValues
.Where(keyPair => !string.IsNullOrEmpty(keyPair.Value))
.Select(keyPair => $"{keyPair.Key}={keyPair.Value}")
.ToArray());
}

return $"ethereum:{address}{query}";
}
}
36 changes: 12 additions & 24 deletions src/libs/QrCodes/Payloads/LitecoinAddress.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
namespace QrCodes.Payloads;

/// <summary>
///
/// </summary>
public class LitecoinAddress : BitcoinLikeCryptoCurrencyAddress
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <param name="amount"></param>
/// <param name="label"></param>
/// <param name="message"></param>
public LitecoinAddress(
string address,
double? amount,
string? label = null,
string? message = null)
: base(
currencyType: BitcoinLikeCryptoCurrencyType.Litecoin,
address: address,
amount: amount,
label: label,
message: message) { }
}
/// <inheritdoc />
public class LitecoinAddress(
string address,
double? amount = null,
string? label = null,
string? message = null)
: BitcoinLikeCryptoCurrencyAddress(
currencyType: BitcoinLikeCryptoCurrencyType.Litecoin,
address: address,
amount: amount,
label: label,
message: message);
2 changes: 1 addition & 1 deletion src/tests/QrCodes.Tests/Helpers/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void TestImageToFile(string? path, string testName, Image<Rgba32>
var fullPath = Path.Combine(path, $"qrtest_{testName}.png");
image.Save(fullPath);

//Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
}

public static void TestImageToFile(string path, string testName, byte[] data)
Expand Down
1 change: 0 additions & 1 deletion src/tests/QrCodes.Tests/ImageSharpRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using QrCodes.Renderers.Abstractions;
using Xunit;
using QrCodes.Tests.Helpers;
using SixLabors.ImageSharp;

namespace QrCodes.Tests;

Expand Down
19 changes: 19 additions & 0 deletions src/tests/QrCodes.Tests/PayloadTests.Ethereum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FluentAssertions;
using QrCodes.Payloads;
using Xunit;

namespace QrCodes.Tests;

public partial class PayloadTests
{
[Fact]
public void ethereum_address_generator_can_generate_address()
{
new EthereumAddress(
address: "0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359",
value: 2014000000000000000D)
.ToString()
.Should().Be(
"ethereum:0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359?value=2.014e18");
}
}
2 changes: 1 addition & 1 deletion src/tests/QrCodes.Tests/PayloadTests.Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void mail_should_build_type_mailto_receiver_only()
var receiver = "[email protected]";
var encoding = Mail.MailEncoding.MailTo;

var generator = new Mail(mailReceiver: receiver, encoding: encoding);
var generator = new Mail(email: receiver, encoding: encoding);

generator.ToString().Should().Be("mailto:[email protected]");
}
Expand Down

0 comments on commit 0c32ffe

Please sign in to comment.