Example #1
0
 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));
 }
Example #2
0
 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));
 }
Example #3
0
 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));
 }
Example #4
0
 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));
 }
Example #5
0
        private static string GetExpirationTime(SiweMessage message)
        {
            if (!string.IsNullOrEmpty(message.ExpirationTime))
            {
                return(string.Format(EXPIRATION_TIME, message.ExpirationTime));
            }

            return(string.Empty);
        }
Example #6
0
        private static string GetStatement(SiweMessage message)
        {
            if (!string.IsNullOrEmpty(message.Statement))
            {
                return(string.Format(STATEMENT, message.Statement));
            }

            return(string.Empty);
        }
Example #7
0
        private static string GetRequestId(SiweMessage message)
        {
            if (!string.IsNullOrEmpty(message.RequestId))
            {
                return(string.Format(REQUEST_ID, message.RequestId));
            }

            return(string.Empty);
        }
Example #8
0
        private static string GetNotBefore(SiweMessage message)
        {
            if (!string.IsNullOrEmpty(message.NotBefore))
            {
                return(string.Format(NOT_BEFORE, message.NotBefore));
            }

            return(string.Empty);
        }
Example #9
0
        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");
        }
Example #10
0
 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));
 }
Example #11
0
 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));
 }
Example #12
0
        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);
        }
Example #13
0
        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));
        }
Example #14
0
 private static string GetUriLine(SiweMessage message)
 {
     return(string.Format(URI_LINE, message.Uri));
 }