Skip to content

Instantly share code, notes, and snippets.

@StefH
Last active June 18, 2018 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefH/8fa43ef4e87404cbbd59f030b4779d59 to your computer and use it in GitHub Desktop.
Save StefH/8fa43ef4e87404cbbd59f030b4779d59 to your computer and use it in GitHub Desktop.
Nethereum AddressValidator code
using System.Text.RegularExpressions;
using Nethereum.Util;
namespace SupplyChain.BlockChain.Validation
{
/// <inheritdoc cref="IAddressValidator"/>
public class AddressValidator : IAddressValidator
{
private static readonly Regex AddressRegex = new Regex("^0[xX]([A-Fa-f0-9]{40})$");
private static readonly AddressUtil AddressUtil = new AddressUtil();
/// <inheritdoc cref="IAddressValidator.IsValid(string)"/>
public bool IsValid(string address)
{
// Doesn't match length, prefix and hex
if (!AddressRegex.IsMatch(address))
{
return false;
}
// It's all lowercase, so no checksum needed
if (address == address.ToLower())
{
return true;
}
// Do checksum
return AddressUtil.IsChecksumAddress(address);
}
}
}
namespace SupplyChain.BlockChain.Validation
{
/// <summary>
/// Validate Ethereum Address
/// </summary>
public interface IAddressValidator
{
/// <summary>
/// Validate the Ethereum Address (https://github.com/Nethereum/Nethereum/issues/293)
/// </summary>
/// <param name="address">The Ethereum Address to validate.</param>
/// <returns>
/// <c>true</c> if the specified address is valid; otherwise, <c>false</c>.
/// </returns>
bool IsValid(string address);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment