private static string GetIssuedAt(SiweMessage message) { if (string.IsNullOrEmpty(message.IssuedAt)) { throw new ArgumentException("IssuedAt cannot be null or empty"); } return(string.Format(ISSUED_AT, message.IssuedAt)); }
private static string GetChainId(SiweMessage message) { if (string.IsNullOrEmpty(message.ChainId)) { throw new ArgumentException("ChainId cannot be null or empty"); } return(string.Format(CHAIN_ID, message.ChainId)); }
private static string GetDomain(SiweMessage message) { if (string.IsNullOrEmpty(message.Domain)) { throw new ArgumentException("Domain cannot be null or empty"); } return(string.Format(DOMAIN, message.Domain)); }
private static string GetVersion(SiweMessage message) { if (string.IsNullOrEmpty(message.Version)) { throw new ArgumentException("Version cannot be null or empty"); } return(string.Format(VERSION, message.Version)); }
private static string GetExpirationTime(SiweMessage message) { if (!string.IsNullOrEmpty(message.ExpirationTime)) { return(string.Format(EXPIRATION_TIME, message.ExpirationTime)); } return(string.Empty); }
private static string GetStatement(SiweMessage message) { if (!string.IsNullOrEmpty(message.Statement)) { return(string.Format(STATEMENT, message.Statement)); } return(string.Empty); }
private static string GetRequestId(SiweMessage message) { if (!string.IsNullOrEmpty(message.RequestId)) { return(string.Format(REQUEST_ID, message.RequestId)); } return(string.Empty); }
private static string GetNotBefore(SiweMessage message) { if (!string.IsNullOrEmpty(message.NotBefore)) { return(string.Format(NOT_BEFORE, message.NotBefore)); } return(string.Empty); }
private static string GetAddress(SiweMessage message) { //should this be a checksum address? //if (message.Address.IsEthereumChecksumAddress()) if (message.Address.IsValidEthereumAddressHexFormat()) { return(string.Format(ADDRESS, message.Address)); } throw new FormatException("Invalid address format"); }
private static string GetNonce(SiweMessage message) { if (string.IsNullOrEmpty(message.Nonce)) { throw new ArgumentException("Nonce cannot be null or empty"); } if (message.Nonce.Length < 8) { throw new ArgumentException("Nonce has to be bigger or equal to 8 characters"); } return(string.Format(NONCE, message.Nonce)); }
public static string BuildMessage(SiweMessage message) { return(GetDomain(message) + GetAddress(message) + GetStatement(message) + GetUriLine(message) + GetVersion(message) + GetChainId(message) + GetNonce(message) + GetIssuedAt(message) + GetExpirationTime(message) + GetNotBefore(message) + GetRequestId(message) + GetResources(message)); }
private static string GetResources(SiweMessage message) { if (message.Resources != null && message.Resources.Count > 0) { var returnString = RESOURCES; foreach (var resource in message.Resources) { returnString += string.Format(RESOURCE, resource); } return(returnString); } return(string.Empty); }
public static SiweMessage Parse(string siweMessage) { if (string.IsNullOrEmpty(siweMessage)) { throw new ArgumentException("Siwe Message cannot be null or empty", nameof(siweMessage)); } var matches = _regex.Matches(siweMessage); if (matches.Count > 0) { var siweMessageDecoded = new SiweMessage(); var fullMatch = matches[0]; var domain = fullMatch.Groups["domain"].Captures[0].Value; var address = fullMatch.Groups["address"].Captures[0].Value; if (fullMatch.Groups["statement"].Captures.Count > 0) { siweMessageDecoded.Statement = fullMatch.Groups["statement"].Captures[0].Value; } var uri = fullMatch.Groups["uri"].Captures[0].Value; var version = fullMatch.Groups["version"].Captures[0].Value; var chainId = fullMatch.Groups["chainId"].Captures[0].Value; var nonce = fullMatch.Groups["nonce"].Captures[0].Value; var issuedAt = fullMatch.Groups["issuedAt"].Captures[0].Value; siweMessageDecoded.Domain = domain; siweMessageDecoded.Address = address; siweMessageDecoded.Uri = uri; siweMessageDecoded.Version = version; siweMessageDecoded.ChainId = chainId; siweMessageDecoded.Nonce = nonce; siweMessageDecoded.IssuedAt = issuedAt; if (fullMatch.Groups["expirationTime"].Captures.Count > 0) { siweMessageDecoded.ExpirationTime = fullMatch.Groups["expirationTime"].Captures[0].Value; } if (fullMatch.Groups["notBefore"].Captures.Count > 0) { siweMessageDecoded.NotBefore = fullMatch.Groups["notBefore"].Captures[0].Value; } if (fullMatch.Groups["notBefore"].Captures.Count > 0) { siweMessageDecoded.NotBefore = fullMatch.Groups["notBefore"].Captures[0].Value; } if (fullMatch.Groups["requestId"].Captures.Count > 0) { siweMessageDecoded.RequestId = fullMatch.Groups["requestId"].Captures[0].Value; } if (fullMatch.Groups["resources"].Captures.Count > 0) { var resources = new List <string>(); var matchedResources = fullMatch.Groups["resources"].Captures[0].Value; resources.AddRange(matchedResources.Split(new string[] { "\n- " }, StringSplitOptions.RemoveEmptyEntries)); siweMessageDecoded.Resources = resources; } return(siweMessageDecoded); } throw new ArgumentException("Invalid Siwe Message", nameof(siweMessage)); }
private static string GetUriLine(SiweMessage message) { return(string.Format(URI_LINE, message.Uri)); }