public void ShouldValidateSignature() { var domain = "login.xyz"; var address = "0xb8a316ea8a9e48ebd25b73c71bc0f22f5c337d1f"; var statement = "Sign-In With Ethereum Example Statement"; var uri = "https://login.xyz"; var version = "1"; var chainId = "1"; var nonce = "uolthxpe"; var issuedAt = "2021-11-25T02:36:37.013Z"; var signature = "0x6eabbdf0861ca83b6cf98381dcbc3db16dffce9a0449dc8b359718d13b0093c3285b6dea7e84ad1aa4871b63899319a988ddf39df3080bcdc60f68dd0942e8221c"; var message = "login.xyz wants you to sign in with your Ethereum account:\n0xb8a316ea8a9e48ebd25b73c71bc0f22f5c337d1f\n\nSign-In With Ethereum Example Statement\n\nURI: https://login.xyz\nVersion: 1\nChain ID: 1\nNonce: uolthxpe\nIssued At: 2021-11-25T02:36:37.013Z"; var siweMessage = new SiweMessage(); siweMessage.Domain = domain; siweMessage.Address = address; siweMessage.Statement = statement; siweMessage.Uri = uri; siweMessage.Version = version; siweMessage.ChainId = chainId; siweMessage.Nonce = nonce; siweMessage.IssuedAt = issuedAt; siweMessage.Signature = signature; var builtMessage = SiweMessageStringBuilder.BuildMessage(siweMessage); Assert.Equal(message, builtMessage); var messageSigner = new EthereumMessageSigner(); var accountRecovered = messageSigner.EncodeUTF8AndEcRecover(builtMessage, signature); Assert.True(accountRecovered.IsTheSameAddress(address)); }
public void ShouldParseAndBuildWithAllOptionalFields() { var message = "[email protected]:8080 wants you to sign in with your Ethereum account:\n0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2\n\nI accept the ServiceOrg Terms of Service: https://service.org/tos\n\nURI: https://service.org/login\nVersion: 1\nChain ID: 1\nNonce: 32891757\nIssued At: 2021-09-30T16:25:24.000Z\nExpiration Time: 2021-09-30T16:25:24.000Z\nNot Before: 2021-09-30T16:25:24.000Z\nRequest ID: 200\nResources:\n- ipfs://Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu\n- https://example.com/my-web2-claim.json"; var domain = "[email protected]:8080"; var address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; var statement = "I accept the ServiceOrg Terms of Service: https://service.org/tos"; var uri = "https://service.org/login"; var version = "1"; var chainId = "1"; var nonce = "32891757"; var issuedAt = "2021-09-30T16:25:24.000Z"; var expirationTime = "2021-09-30T16:25:24.000Z"; var notBefore = "2021-09-30T16:25:24.000Z"; var requestId = "200"; var resource1 = "ipfs://Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu"; var resource2 = "https://example.com/my-web2-claim.json"; var decodedMessage = SiweMessageParser.Parse(message); Assert.Equal(domain, decodedMessage.Domain); Assert.Equal(address, decodedMessage.Address); Assert.Equal(statement, decodedMessage.Statement); Assert.Equal(uri, decodedMessage.Uri); Assert.Equal(version, decodedMessage.Version); Assert.Equal(chainId, decodedMessage.ChainId); Assert.Equal(nonce, decodedMessage.Nonce); Assert.Equal(issuedAt, decodedMessage.IssuedAt); Assert.Equal(expirationTime, decodedMessage.ExpirationTime); Assert.Equal(notBefore, decodedMessage.NotBefore); Assert.Equal(requestId, decodedMessage.RequestId); Assert.Equal(resource1, decodedMessage.Resources[0]); Assert.Equal(resource2, decodedMessage.Resources[1]); var decodedMessage2 = SiweMessageParser.ParseUsingAbnf(message); Assert.Equal(domain, decodedMessage2.Domain); Assert.Equal(address, decodedMessage2.Address); Assert.Equal(statement, decodedMessage2.Statement); Assert.Equal(uri, decodedMessage2.Uri); Assert.Equal(version, decodedMessage2.Version); Assert.Equal(chainId, decodedMessage2.ChainId); Assert.Equal(nonce, decodedMessage2.Nonce); Assert.Equal(issuedAt, decodedMessage2.IssuedAt); Assert.Equal(expirationTime, decodedMessage2.ExpirationTime); Assert.Equal(notBefore, decodedMessage2.NotBefore); Assert.Equal(requestId, decodedMessage2.RequestId); Assert.Equal(resource1, decodedMessage2.Resources[0]); Assert.Equal(resource2, decodedMessage2.Resources[1]); var builtMessage = SiweMessageStringBuilder.BuildMessage(decodedMessage2); Assert.Equal(message, builtMessage); }
public bool IsMessageSignatureValid(SiweMessage siweMessage) { var builtMessage = SiweMessageStringBuilder.BuildMessage(siweMessage); var messageSigner = new EthereumMessageSigner(); var accountRecovered = messageSigner.EncodeUTF8AndEcRecover(builtMessage, siweMessage.Signature); if (accountRecovered.IsTheSameAddress(siweMessage.Address)) { return(true); } return(false); }
public virtual bool ValidateSiweMessageHasCorrectNonce(SiweMessage siweMessage) { if (_messages.ContainsKey(siweMessage.Nonce)) { var currentMessage = SiweMessageStringBuilder.BuildMessage(siweMessage); var existingMessage = SiweMessageStringBuilder.BuildMessage(_messages[siweMessage.Nonce]); if (currentMessage == existingMessage) { return(true); } } return(false); }
public string BuildMessageToSign(SiweMessage siweMessage) { if (string.IsNullOrEmpty(siweMessage.IssuedAt)) { siweMessage.SetIssuedAtNow(); } if (string.IsNullOrEmpty(siweMessage.Version)) { siweMessage.Version = "1"; } _siweSessionNonceManagement.AssignNewNonce(siweMessage); return(SiweMessageStringBuilder.BuildMessage(siweMessage)); }
public void ShouldParseAndBuildTimestampWithoutMicroseconds() { var message = "service.org wants you to sign in with your Ethereum account:\n0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2\n\nI accept the ServiceOrg Terms of Service: https://service.org/tos\n\nURI: https://service.org/login\nVersion: 1\nChain ID: 1\nNonce: 32891757\nIssued At: 2021-09-30T16:25:24Z"; var domain = "service.org"; var address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; var statement = "I accept the ServiceOrg Terms of Service: https://service.org/tos"; var uri = "https://service.org/login"; var version = "1"; var chainId = "1"; var nonce = "32891757"; var issuedAt = "2021-09-30T16:25:24Z"; var decodedMessage = SiweMessageParser.Parse(message); Assert.Equal(domain, decodedMessage.Domain); Assert.Equal(address, decodedMessage.Address); Assert.Equal(statement, decodedMessage.Statement); Assert.Equal(uri, decodedMessage.Uri); Assert.Equal(version, decodedMessage.Version); Assert.Equal(chainId, decodedMessage.ChainId); Assert.Equal(nonce, decodedMessage.Nonce); Assert.Equal(issuedAt, decodedMessage.IssuedAt); var decodedMessage2 = SiweMessageParser.ParseUsingAbnf(message); Assert.Equal(domain, decodedMessage2.Domain); Assert.Equal(address, decodedMessage2.Address); Assert.Equal(statement, decodedMessage2.Statement); Assert.Equal(uri, decodedMessage2.Uri); Assert.Equal(version, decodedMessage2.Version); Assert.Equal(chainId, decodedMessage2.ChainId); Assert.Equal(nonce, decodedMessage2.Nonce); Assert.Equal(issuedAt, decodedMessage2.IssuedAt); var builtMessage = SiweMessageStringBuilder.BuildMessage(decodedMessage2); Assert.Equal(message, builtMessage); }