コード例 #1
0
ファイル: CryptographyTest.cs プロジェクト: LAToken/lachain
        public void Test_SignRoundTrip()
        {
            var privateKey = "0xD95D6DB65F3E2223703C5D8E205D98E3E6B470F067B0F94F6C6BF73D4301CE48".HexToBytes();
            var publicKey  = Crypto.ComputePublicKey(privateKey, true);
            var address    = "0x6Bc32575ACb8754886dC283c2c8ac54B1Bd93195".HexToBytes();

            CollectionAssert.AreEqual(address, Crypto.ComputeAddress(publicKey));

            var       startTs = TimeUtils.CurrentTimeMillis();
            const int n       = 100;

            for (var it = 0; it < n; ++it)
            {
                // var message = "0xdeadbeef" + it.ToString("X4");
                var message =
                    "0xec808504a817c800825208948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a764000080018080";
                var digest = message.HexToBytes();
                // using old chain id
                var signature = Crypto.Sign(digest, privateKey, false);
                Assert.IsTrue(Crypto.VerifySignature(digest, signature, publicKey, false));
                var recoveredPubkey = Crypto.RecoverSignature(digest, signature, false);
                Assert.AreEqual(recoveredPubkey, publicKey);

                // using new chain id
                signature = Crypto.Sign(digest, privateKey, true);
                Assert.IsTrue(Crypto.VerifySignature(digest, signature, publicKey, true));
                recoveredPubkey = Crypto.RecoverSignature(digest, signature, true);
                Assert.AreEqual(recoveredPubkey, publicKey);
            }

            var endTs = TimeUtils.CurrentTimeMillis();

            Console.WriteLine($"Full sign + recover time: {endTs - startTs}ms");
            Console.WriteLine($"Per 1 iteration: {(double) (endTs - startTs) / n}ms");
        }
コード例 #2
0
ファイル: FrontEndService.cs プロジェクト: LAToken/lachain
        public bool VerifySign(string message, string sign, bool useNewChainId)
        {
            if (_privateWallet.IsLocked())
            {
                throw new Exception("wallet is locked");
            }
            var keyPair = _privateWallet.EcdsaKeyPair;

            Logger.LogInformation($"public key: {keyPair.PublicKey.ToHex()}, address: {keyPair.PublicKey.GetAddress().ToHex()}");
            try
            {
                var msg          = message.HexToBytes();
                var signBytes    = sign.HexToBytes();
                var pubkeyParsed = Crypto.RecoverSignature(msg, signBytes, useNewChainId);
                if (!keyPair.PublicKey.EncodeCompressed().SequenceEqual(pubkeyParsed))
                {
                    return(false);
                }
                return(Crypto.VerifySignature(msg, signBytes, keyPair.PublicKey.EncodeCompressed(), useNewChainId));
            }
            catch (Exception exception)
            {
                Logger.LogWarning($"Got exception trying to verify signed message: {exception}");
                throw;
            }
        }