public void AuthValidSignAndRecoverEip8()
        {
            // Generate all needed keypairs.
            EthereumEcdsa localPrivateKey     = EthereumEcdsa.Generate();
            EthereumEcdsa ephemeralPrivateKey = EthereumEcdsa.Generate();
            EthereumEcdsa receiverPrivateKey  = EthereumEcdsa.Generate();

            // Create an RLPx auth packet and sign it.
            RLPxAuthEIP8 authPacket = new RLPxAuthEIP8();

            authPacket.Sign(localPrivateKey, ephemeralPrivateKey, receiverPrivateKey, null);

            // Serialize and deserialize it.
            byte[]       serializedData     = authPacket.Serialize();
            RLPxAuthEIP8 deserializedPacket = new RLPxAuthEIP8(serializedData);

            // Verify all matches between our serialized/deserialized object data.
            Assert.Equal(authPacket.Nonce.ToHexString(), deserializedPacket.Nonce.ToHexString());
            Assert.Equal(authPacket.PublicKey.ToHexString(), deserializedPacket.PublicKey.ToHexString());
            Assert.Equal(authPacket.R.ToHexString(), deserializedPacket.R.ToHexString());
            Assert.Equal(authPacket.S.ToHexString(), deserializedPacket.S.ToHexString());
            Assert.Equal(authPacket.V, deserializedPacket.V);

            // Try to recover the public key
            EthereumEcdsa recoveredEphemeralPublicKey = deserializedPacket.RecoverDataFromSignature(receiverPrivateKey).remoteEphemeralPublicKey;

            // Verify our public key hashes match
            Assert.Equal(ephemeralPrivateKey.GetPublicKeyHash().ToHexString(), recoveredEphemeralPublicKey.GetPublicKeyHash().ToHexString());
        }
示例#2
0
        public override void Sign(EthereumEcdsa localPrivateKey, EthereumEcdsa ephemeralPrivateKey, EthereumEcdsa remotePublicKey, uint?chainId = null)
        {
            // Sign the data with the base method
            base.Sign(localPrivateKey, ephemeralPrivateKey, remotePublicKey, chainId);

            // Set our ephermal public key hash.
            EphermalPublicKeyHash = ephemeralPrivateKey.GetPublicKeyHash();
        }
示例#3
0
        public void GenerateSignRecoverVerify(bool useBouncyCastle)
        {
            // Generate ECDSA keypair, compute a hash, sign it, then recover the public key from the signature and verify it matches.
            EthereumEcdsa provider;

            if (useBouncyCastle)
            {
                provider = EthereumEcdsaBouncyCastle.Generate(new SystemRandomAccountDerivation());
            }
            else
            {
                provider = EthereumEcdsaNative.Generate(new SystemRandomAccountDerivation());
            }

            // Sign a hash.
            byte[] hash = KeccakHash.ComputeHashBytes(new byte[] { 11, 22, 33, 44 });
            (byte RecoveryID, BigInteger r, BigInteger s)signature = provider.SignData(hash);

            // Recover the public key for the signature we just made and verify it.
            EthereumEcdsa recovered = null;

            if (useBouncyCastle)
            {
                recovered = EthereumEcdsaBouncyCastle.Recover(hash, signature.RecoveryID, signature.r, signature.s);
            }
            else
            {
                recovered = EthereumEcdsaNative.Recover(hash, signature.RecoveryID, signature.r, signature.s);
            }

            Assert.True(provider.GetPublicKeyHash().ValuesEqual(recovered.GetPublicKeyHash()));

            // Verify the signature we just made.
            Assert.True(recovered.VerifyData(hash, signature.r, signature.s));

            // Generate a new ECDSA keypair (to verify other keypairs can't pass verification for signatures they didn't create).
            EthereumEcdsa provider2;

            if (useBouncyCastle)
            {
                provider2 = EthereumEcdsaBouncyCastle.Generate(new SystemRandomAccountDerivation());
            }
            else
            {
                provider2 = EthereumEcdsaNative.Generate(new SystemRandomAccountDerivation());
            }

            // Verify the prior signature cannot be verified with this new keypair.
            Assert.False(provider2.VerifyData(hash, signature.r, signature.s));
        }
示例#4
0
        public void SignAndVerify(bool useBouncyCastle)
        {
            // Generate ECDSA keypair, compute a hash, sign it, then recover the public key from the signature and verify it matches.
            EthereumEcdsa provider;

            if (useBouncyCastle)
            {
                provider = EthereumEcdsaBouncyCastle.Generate(new SystemRandomAccountDerivation());
            }
            else
            {
                provider = EthereumEcdsaNative.Generate(new SystemRandomAccountDerivation());
            }

            byte[] hash = KeccakHash.ComputeHashBytes(new byte[] { 11, 22, 33, 44 });
            (byte RecoveryID, BigInteger r, BigInteger s)signature = provider.SignData(hash);
            EthereumEcdsa recovered = EthereumEcdsa.Recover(hash, signature.RecoveryID, signature.r, signature.s);

            Assert.True(provider.GetPublicKeyHash().ValuesEqual(recovered.GetPublicKeyHash()));
        }