コード例 #1
0
        public bool VerifyMessage(string aMessage, string aSignature, out string aAddress)
        {
            bool lResult = false;

            if (!FAddresses.Any())
            {
                GetAddress(0);
                GetAddress(1);
            }
            string lOutputAddress;

            try
            {
                var lSigner = new EthereumMessageSigner();
                lOutputAddress = lSigner.EncodeUTF8AndEcRecover(aMessage, aSignature);
            }
            catch
            {
                lOutputAddress = null;
            }
            if (lOutputAddress != null)
            {
                lResult = FAddresses.Keys.Any(lAddress => string.Equals(lAddress.ToLowerInvariant(), lOutputAddress.ToLowerInvariant(), StringComparison.OrdinalIgnoreCase));
            }
            aAddress = lOutputAddress;
            return(lResult);
        }
        public bool Validate(string plainMessage, string signature, string publicAddress)
        {
            if (string.IsNullOrEmpty(plainMessage))
            {
                throw new ArgumentNullException(nameof(plainMessage));
            }

            if (string.IsNullOrEmpty(signature))
            {
                throw new ArgumentNullException(nameof(signature));
            }

            if (string.IsNullOrEmpty(publicAddress))
            {
                throw new ArgumentNullException(nameof(publicAddress));
            }

            string recoveredAddress;

            try
            {
                recoveredAddress = _signer.EncodeUTF8AndEcRecover(plainMessage, signature);
            }
            catch (FormatException e)
            {
                _log.Warning(e.Message, e, new { plainMessage, signature, publicAddress });
                return(false);
            }

            return(string.Equals(recoveredAddress, publicAddress, StringComparison.OrdinalIgnoreCase));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        public bool VerifySign(string payload, string sign, string pubKey)
        {
            var expectedSigner = new EthECKey(pubKey.HexToByteArray(), false);
            var signerAddress  = _signer.EncodeUTF8AndEcRecover(payload, sign);

            return(expectedSigner.GetPublicAddress().Equals(signerAddress, StringComparison.CurrentCultureIgnoreCase));
        }
コード例 #5
0
ファイル: EthSignService.cs プロジェクト: SP8DE/protocol
        public bool VerifySignature(string message, string signature, string pubKey)
        {
            var signer  = new EthereumMessageSigner();
            var account = signer.EncodeUTF8AndEcRecover(message, signature);

            return(string.Equals(account, pubKey, System.StringComparison.InvariantCultureIgnoreCase));
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: harilachu/LearnBlockChain
        static async Task SigningMessage()
        {
            try
            {
                // Setup
                var url        = "http://127.0.0.1:8502";
                var privateKey = "0x82E41304BB1E81A848BFC705CB06FEDB0554A9906744EB225820A3CF3CF40A79";
                var account    = new Account(privateKey);
                var web3       = new Web3(account, url);
                // An already-deployed SimpleStorage.sol contract on Rinkeby:
                var contractAddress = "0xcf5636b4c062262b52e00455b04b1c89d3c04d11";
                var service         = new StorageService(web3, contractAddress);

                //Signing
                var msg1    = "wee test message 18/09/2017 02:55PM";
                var signer1 = new EthereumMessageSigner();
                var ethKey  = new EthECKey(privateKey);

                var signature1 = signer1.EncodeUTF8AndSign(msg1, ethKey);
                Console.WriteLine($"Signature: {signature1}");
                Console.WriteLine($"Public address from Key: {ethKey.GetPublicAddress()}");
                //get accounts addresses
                var accounts = await web3.Eth.Accounts.SendRequestAsync();

                Console.WriteLine($"Public address from eth Accounts: {accounts[0]}");
                //Recover signature
                var addressRec1 = signer1.EncodeUTF8AndEcRecover(msg1, signature1);
                Console.WriteLine($"Recovered address from signature: {addressRec1}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
コード例 #7
0
        // Recover public address from signature and message
        public string RecoverPersonalSignature(string message, string signature)
        {
            EthereumMessageSigner signer = new EthereumMessageSigner();

            string publicAddress = signer.EncodeUTF8AndEcRecover(message, signature);

            return(publicAddress);
        }
コード例 #8
0
        public void ShouldSignAndVerifyEncodingMessageAsUTF8()
        {
            var address    = "0x12890d2cce102216644c59dae5baed380d84830c";
            var msg        = "wee test message 18/09/2017 02:55PM";
            var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var signer     = new EthereumMessageSigner();
            var signature  = signer.EncodeUTF8AndSign(msg, new EthECKey(privateKey));
            var addressRec = signer.EncodeUTF8AndEcRecover(msg, signature);

            Assert.Equal(address.ToLower(), addressRec.ToLower());
        }
コード例 #9
0
        private bool IsSignedMessageValid(Address address, string signedText, string signature)
        {
            if (string.IsNullOrEmpty(signedText) || string.IsNullOrEmpty(signature))
            {
                return(false);
            }

            var publicKey = (Address)_ethereumMessageSigner.EncodeUTF8AndEcRecover(signedText, signature);

            return(publicKey == address);
        }
コード例 #10
0
        public void ShouldVerifyMEWSignatureEncodingMessageAsUTF8()
        {
            var address = "0xe651c5051ce42241765bbb24655a791ff0ec8d13";
            var msg     = "wee test message 18/09/2017 02:55PM";
            var sig     = "0xf5ac62a395216a84bd595069f1bb79f1ee08a15f07bb9d9349b3b185e69b20c60061dbe5cdbe7b4ed8d8fea707972f03c21dda80d99efde3d96b42c91b2703211b";

            var signer     = new EthereumMessageSigner();
            var addressRec = signer.EncodeUTF8AndEcRecover(msg, sig);

            Assert.Equal(address.ToLower(), addressRec.ToLower());
        }
コード例 #11
0
        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);
        }
コード例 #12
0
ファイル: EthSignServiceTest.cs プロジェクト: SP8DE/protocol
        public void test5()
        {
            var signature =
                "0x7c87377d7a148ee69ec5ed9be2c87033639b6970acf9a8e165e81cf3dce4516d2bdcc6957eb435447ef9eaa34f541a81f999f0b967bfbfcca1850f58f4cdf5fe1c";
            var text    = "0x492d0fd814940d1375225a7e10905585b72b0a8c;-7440852294285764000;636706243532255384";
            var hasher  = new Sha3Keccack();
            var hash    = hasher.CalculateHash(text);
            var signer  = new EthereumMessageSigner();
            var account = signer.EncodeUTF8AndEcRecover(text, signature);

            Assert.Equal("0x492d0fd814940d1375225a7e10905585b72b0a8c".ToLower(), account.EnsureHexPrefix().ToLower());
        }
コード例 #13
0
        //                                      FREE RODRIGO
        public void SignMsg()
        {
            //var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";

            //who send the message
            string privateKey = FCryptoCurrencyAdvocacy.GetPrivateKey(1);

            if (this.Fmsg == null)
            {
                Fmsg = "Testing signature message";
            }

            var signer1    = new EthereumMessageSigner();
            var signature1 = signer1.EncodeUTF8AndSign(this.Fmsg, new EthECKey(privateKey));

            //verify msg
            var addressRec1 = signer1.EncodeUTF8AndEcRecover(this.Fmsg, signature1);
        }
コード例 #14
0
        public void ShouldVerifySignatureEncodingMessageAsUTF8()
        {
            var address    = "0x12890d2cce102216644c59dae5baed380d84830c";
            var msg        = "Hello from Nethereum";
            var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";

            var signer = new EthereumMessageSigner();

            var signature = signer.EncodeUTF8AndSign(msg, new EthECKey(privateKey));

            var signatureExpected =
                "0xe20e42c13fbf52a5d65229f4dd1dcd3255691166ce2852456631baf4836afd4630480609a76794ee3018c5514ee3a0592031cf2490e7356dffe4ed202606f5181c";

            Assert.Equal(signatureExpected, signature);

            var addressRec = signer.EncodeUTF8AndEcRecover(msg, signatureExpected);

            Assert.Equal(address.ToLower(), addressRec.ToLower());
        }
コード例 #15
0
ファイル: EthSignServiceTest.cs プロジェクト: SP8DE/protocol
        public void test4()
        {
            var signature =
                "0xdb46c6be4b1ee0ef670625630d38899213ea5d63d749954d82a4651b81f7d44f6d16e2e4903038c022b71d11baa26b08142c6b9f5db106d7b96b57d1836ce10e1b";
            var text    = "test";
            var hasher  = new Sha3Keccack();
            var hash    = hasher.CalculateHash(text);
            var signer  = new EthereumMessageSigner();
            var account = signer.EncodeUTF8AndEcRecover(text, signature);

            Assert.Equal("0xe77992311815D6961cBB231CFd2009990203385F".ToLower(), account.EnsureHexPrefix().ToLower());

            signature = signer.Sign(hash.HexToByteArray(),
                                    "0xd3a7d42d881a9b59ccefcac0f5bcc69f85e68fdf0bfb6fcbbe42373320de420f");

            account = signer.EcRecover(hash.HexToByteArray(), signature);

            Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower());
        }
コード例 #16
0
        public string GetAddressFromSignedMessage(string message, string signature)
        {
            var signer = new EthereumMessageSigner();

            return(signer.EncodeUTF8AndEcRecover(message, signature));
        }
コード例 #17
0
        public bool IsValid(string address, string signature, string message)
        {
            var r = _signer.EncodeUTF8AndEcRecover(message, signature);

            return(address.ToLower() == r.ToLower());
        }