Last active
June 18, 2018 19:43
-
-
Save StefH/8fa43ef4e87404cbbd59f030b4779d59 to your computer and use it in GitHub Desktop.
Nethereum AddressValidator code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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