Exemple #1
0
        public void ShouldGenerateDifferentPrivateKeys()
        {
            KeyPair keyPair1 = ChainUtility.GenerateNewKeyPair();
            KeyPair keyPair2 = ChainUtility.GenerateNewKeyPair();

            Assert.NotEqual(keyPair1.PrivateKey, keyPair2.PrivateKey);
        }
Exemple #2
0
        public static string GetHash(string timeStamp, string lastHash, string data, long nonce, long difficulty)
        {
            string dataToHash = timeStamp + lastHash + data + nonce + difficulty;
            string hash       = Convert.ToBase64String(ChainUtility.Hash(dataToHash));

            return(hash);
        }
Exemple #3
0
 public Wallet()
 {
     KeyPair   = ChainUtility.GenerateNewKeyPair();
     PublicKey = new UgoChain.Features.Wallet.PublicKey()
     {
         Key = KeyPair.PublicKey
     };
     Balance = WALLET_INITIAL_BALANCE;
 }
Exemple #4
0
 /// <summary>
 /// Signs a transaction to be sent
 /// 1. Build a transaction input
 /// 2. Generate the hash of the TxOutputs for the transaction
 /// 3. Sign the hash generated with the wallet's private key
 /// </summary>
 /// <param name="transaction"></param>
 /// <param name="senderWallet"></param>
 /// <returns></returns>
 public void SignTransaction(Wallet senderWallet)
 {
     UgoChain.Features.Wallet.TxInput txInput = new UgoChain.Features.Wallet.TxInput();
     byte[] hash = ChainUtility.Hash(JsonConvert.SerializeObject(TxOutputs));
     txInput.TimeStamp = Helper.ConvertToUnixTimeStamp(DateTime.Now).ToString();
     txInput.Address   = senderWallet.PublicKey.Key;
     txInput.Amount    = senderWallet.Balance;
     txInput.Signature = senderWallet.SignHash(hash);
     Input             = txInput;
 }
Exemple #5
0
 /// <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
         }
     });
 }
Exemple #6
0
 /// <summary>
 /// Modify implementation later
 /// </summary>
 public static Wallet GetBlockchainWallet()
 {
     return(new Wallet()
     {
         KeyPair = ChainUtility.GenerateNewKeyPair(),
         PublicKey = new PublicKey()
         {
             Key = BLOCKCHAIN_ADDRESS_MAIN_PEER
         }
     });
 }
Exemple #7
0
        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));
        }
Exemple #8
0
 public byte[] SignHash(byte [] Hash)
 {
     byte[] signature = ChainUtility.SignDataHash(Hash, KeyPair.PrivateKey);
     return(signature);
 }
Exemple #9
0
 /// <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));
 }