public void ShouldGenerateDifferentPrivateKeys() { KeyPair keyPair1 = ChainUtility.GenerateNewKeyPair(); KeyPair keyPair2 = ChainUtility.GenerateNewKeyPair(); Assert.NotEqual(keyPair1.PrivateKey, keyPair2.PrivateKey); }
public Wallet() { KeyPair = ChainUtility.GenerateNewKeyPair(); PublicKey = new UgoChain.Features.Wallet.PublicKey() { Key = KeyPair.PublicKey }; Balance = WALLET_INITIAL_BALANCE; }
/// <summary> /// Modify implementation later /// </summary> public static Wallet GetBlockchainWallet() { return(new Wallet() { KeyPair = ChainUtility.GenerateNewKeyPair(), PublicKey = new UgoChain.Features.Wallet.PublicKey() { Key = BLOCKCHAIN_ADDRESS_PEER_ONE } }); }
/// <summary> /// Modify implementation later /// </summary> public static Wallet GetBlockchainWallet() { return(new Wallet() { KeyPair = ChainUtility.GenerateNewKeyPair(), PublicKey = new PublicKey() { Key = BLOCKCHAIN_ADDRESS_MAIN_PEER } }); }
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)); }