Пример #1
0
        private static void EthereumTestRpcDemo(EthereumAccount ethereumAccount)
        {
            var senderPrivateKey  = "0x4faec59e004fd62384813d760e55d6df65537b4ccf62f268253ad7d4243a7193";
            var reciverPrivateKey = "0x03fd5782c37523be6598ca0e5d091756635d144e42d518bb5f8db11cf931b447";

            Console.WriteLine($"Please run the docker image with the following command:{Environment.NewLine}" +
                              "docker run -d -p 8545:8545 trufflesuite/ganache-cli:latest " +
                              $"--account=\"{senderPrivateKey}, 300000000000000000000\"" +
                              $" --account=\"{reciverPrivateKey}, 0\"");

            // Check if Account already stored in KeyStore
            try
            {
                var senderAccount  = ethereumAccount.GetPublicAddressAsync(c_senderId).Result;
                var reciverAccount = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;
            }
            catch (Exception)
            {
                // TODO: Add Check for key not found exception
                ethereumAccount.CreateAccountAsync(c_senderId, senderPrivateKey).Wait();
                ethereumAccount.CreateAccountAsync(c_ReciverId, reciverPrivateKey).Wait();
            }
            finally
            {
                SendCoins(ethereumAccount);
            }
        }
Пример #2
0
        private static void EthereumTestnetDemo(EthereumAccount ethereumAccount)
        {
            try
            {
                var senderAccount  = ethereumAccount.GetPublicAddressAsync(c_senderId).Result;
                var reciverAccount = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;
            }
            catch (Exception)
            {
                // TODO: Add Check for key not found exception
                ethereumAccount.CreateAccountAsync(c_senderId, "0x6d1ae68da64400f4b29baf4bde6fc9796e71480e8f1a502ad6fc2dae92dce268").Wait();
                ethereumAccount.CreateAccountAsync(c_ReciverId).Wait();

                var senderPublicAddress = ethereumAccount.GetPublicAddressAsync(c_senderId);
                Console.WriteLine("Accounts were created. " +
                                  $"To continue the demo please send ether to address {senderPublicAddress}{Environment.NewLine}" +
                                  "You can send ether for: https://www.rinkeby.io/#faucet");
            }

            SendCoins(ethereumAccount);
        }
Пример #3
0
        private static void SendCoins(EthereumAccount ethereumAccount)
        {
            Console.WriteLine("Sender - Happy to transfer my crypto coins!");

            // Init
            var senderAddress  = ethereumAccount.GetPublicAddressAsync(c_senderId).Result;
            var reciverAddress = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;
            var balance        = ethereumAccount.GetCurrentBalance(senderAddress).Result;

            PrintCurrentBalance(senderAddress, balance);

            var encryptionKeyName = ConfigurationManager.AppSettings["EncryptionKeyName"];
            var decryptionKeyName = ConfigurationManager.AppSettings["DecryptionKeyName"];
            var signKeyName       = ConfigurationManager.AppSettings["SignKeyName"];
            var verifyKeyName     = ConfigurationManager.AppSettings["VerifyKeyName"];

            var encryptionCertPassword = ConfigurationManager.AppSettings["EncryptionCertPassword"];
            var decryptionCertPassword = ConfigurationManager.AppSettings["DecryptionCertPassword"];
            var signCertPassword       = ConfigurationManager.AppSettings["SignCertPassword"];
            var verifyCertPassword     = ConfigurationManager.AppSettings["VerifyCertPassword"];

            var kv = new KeyVault(ConfigurationManager.AppSettings["AzureKeyVaultUri"],
                                  ConfigurationManager.AppSettings["applicationId"],
                                  ConfigurationManager.AppSettings["applicationSecret"]);
            var secretsMgmnt =
                new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    kv,
                    kv);

            secretsMgmnt.InitializeAsync().Wait();
            //var securedComm = new RabbitMQBusImpl(ConfigurationManager.AppSettings["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");

            var queueClient =
                new CloudQueueClientWrapper(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
            var securedComm = new AzureQueue("transactions", queueClient, secretsMgmnt, true);

            securedComm.InitializeAsync().Wait();

            // While there are sufficient funds, transfer some...
            while (balance >= 0)
            {
                var amountToSend = 0.001;
                // Message structure: {amountToSend};{senderName};{reciverAddress}
                var message = $"{amountToSend};{c_senderId};{reciverAddress}";
                securedComm.EnqueueAsync(Communication.Utils.ToByteArray(message)).Wait();

                // Sleep 1 minute
                Thread.Sleep(60000);

                var newBalance = ethereumAccount.GetCurrentBalance(senderAddress).Result;
                PrintCurrentBalance(senderAddress, newBalance);

                // Wait for mining..
                while (newBalance.Equals(balance))
                {
                    newBalance = ethereumAccount.GetCurrentBalance(senderAddress).Result;
                }

                balance = newBalance;
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            // Init
            var kv = new KeyVault(ConfigurationManager.AppSettings["AzureKeyVaultUri"],
                                  ConfigurationManager.AppSettings["applicationId"],
                                  ConfigurationManager.AppSettings["applicationSecret"]);
            var sqlDb = new SqlConnector(ConfigurationManager.AppSettings["SqlUserID"],
                                         ConfigurationManager.AppSettings["SqlPassword"],
                                         ConfigurationManager.AppSettings["SqlInitialCatalog"],
                                         ConfigurationManager.AppSettings["SqlDataSource"],
                                         ConfigurationManager.AppSettings["applicationId"],
                                         ConfigurationManager.AppSettings["applicationSecret"]);

            sqlDb.Initialize().Wait();

            var ethereumAccount = new EthereumAccount(sqlDb, ConfigurationManager.AppSettings["EthereumNodeUrl"]);

            Console.WriteLine("Receiver - I just love getting new crypto coins");

            var reciverAddress = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;

            PrintCurrentBalance(reciverAddress, ethereumAccount.GetCurrentBalance(reciverAddress).Result);

            var encryptionKeyName = ConfigurationManager.AppSettings["EncryptionKeyName"];
            var decryptionKeyName = ConfigurationManager.AppSettings["DecryptionKeyName"];
            var signKeyName       = ConfigurationManager.AppSettings["SignKeyName"];
            var verifyKeyName     = ConfigurationManager.AppSettings["VerifyKeyName"];

            var encryptionCertPassword = ConfigurationManager.AppSettings["EncryptionCertPassword"];
            var decryptionCertPassword = ConfigurationManager.AppSettings["DecryptionCertPassword"];
            var signCertPassword       = ConfigurationManager.AppSettings["SignCertPassword"];
            var verifyCertPassword     = ConfigurationManager.AppSettings["VerifyCertPassword"];

            var secretsMgmnt =
                new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    kv,
                    kv);

            secretsMgmnt.InitializeAsync().Wait();
            //var securedComm = new RabbitMQBusImpl(ConfigurationManager.AppSettings["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");
            var queueClient =
                new CloudQueueClientWrapper(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
            var securedComm = new AzureQueue("notifications", queueClient, secretsMgmnt, true);

            securedComm.InitializeAsync().Wait();

            // Listen on the notifications queue, check balance when a notification arrives
            var consumerTag =
                securedComm.DequeueAsync(
                    msg =>
            {
                var data = Communication.Utils.FromByteArray <string>(msg);
                if (data.Equals(reciverAddress, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Great, Balance change!");
                    PrintCurrentBalance(reciverAddress,
                                        ethereumAccount.GetCurrentBalance(reciverAddress).Result);
                }
                else
                {
                    Console.WriteLine("Not my balance!");
                    Console.WriteLine(msg);
                }
            }, (message) => { Console.WriteLine("Verification failure, doing nothing"); },
                    TimeSpan.FromSeconds(3));

            // wait 30 minutes
            Thread.Sleep(30 * 1000 * 60);

            // switch based on the chosen queue
            //securedComm.CancelListeningOnQueue(consumerTag.Result);
            securedComm.CancelListeningOnQueue();
        }