public void ShouldVerifyTransactionUsingPublicKey() { EllipticCurveAlgorithm curve = EllipticCurveAlgorithm.Create(EllipticCurveAlgorithm.EcDsaSha2Nistp256); var privateKey = curve.GetPrivateKey(); var pubKey = curve.GetPublicKey(); //The first curve is created from the private key EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsa.FromPrivateKey(privateKey); //The second curve is created from the public key of the first curve and is used to verify the transaction //The public key can be easily shared which makes it easy for verification EllipticCurveDsa ellipticCurveDsaFake = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsaFake.FromPublicKey(pubKey); SHA256 sHA256 = SHA256.Create(); byte[] hashBytes = sHA256.ComputeHash(Encoding.Default.GetBytes("Message to be hashed")); byte[] signature = ellipticCurveDsa.SignHash(hashBytes); bool isCorrect = ellipticCurveDsa.VerifyHash(hashBytes, signature); bool isFake = ellipticCurveDsaFake.VerifyHash(hashBytes, signature); Assert.Equal(isCorrect, isFake); //Perform same operation using ChainUtility byte[] signature2 = ChainUtility.SignDataHash(hashBytes, Convert.ToBase64String(privateKey)); var newKeyPair = ChainUtility.GenerateNewKeyPair(); Assert.True(ChainUtility.VerifySignature(pubKey, signature2, hashBytes)); Assert.False(ChainUtility.VerifySignature(Convert.FromBase64String(newKeyPair.PublicKey), signature2, hashBytes)); }
/// <summary> /// Veify signature using TxInput data-Note the TxInput is build from the wallet object /// </summary> /// <returns></returns> public bool VerifyTransaction() { byte[] publicKey = Convert.FromBase64String(Input.Address); byte[] dataHashToVerify = ChainUtility.Hash(JsonConvert.SerializeObject(TxOutputs)); return(ChainUtility.VerifySignature(publicKey, Input.Signature, dataHashToVerify)); }