Esempio n. 1
0
        /////////////////private methods///////////////////
        private void InitKeys()
        {
            // load from storage if exists
            if (resourceDataAccess.FileExists())
            {
                this.ecKey = KeyUtils.LoadNBEcKey(this.resourceDataAccess);
            }
            else
            {
                // create a new private key and store

                this.ecKey = KeyUtils.CreateNBEcKey();
                byte[] priv = this.ecKey.ToBytes();
                KeyUtils.SaveEcKey(this.ecKey, this.resourceDataAccess);
            }
        }
Esempio n. 2
0
        private static void GenerateKeyPair(EcKey ecKey)
        {
            Console.Clear();
            DrawTitle();

            if (env == Env.Test)
            {
                ecKeyFilePath = appConfig.BitPayConfiguration.EnvConfig.Test.PrivateKeyPath;
            }
            if (env == Env.Prod)
            {
                ecKeyFilePath = appConfig.BitPayConfiguration.EnvConfig.Prod.PrivateKeyPath;
            }

            if (!string.IsNullOrEmpty(ecKeyFilePath))
            {
                Console.WriteLine(" The current private key file defined is: " + ecKeyFilePath);
                Console.WriteLine(" Would you like to change it? [yes|no] (default: no)");
                Console.WriteLine();
                Console.Write(" > ");
                var answer = Console.ReadLine();
                while (answer.ToLower() != "yes" && answer.ToLower() != "no" && answer.ToLower() != "")
                {
                    answer = Console.ReadLine();
                }

                if (answer.ToLower() == "no" || answer.ToLower() == "")
                {
                    if (File.Exists(ecKeyFilePath))
                    {
                        SetNotification(" Selected private key file: " + ecKeyFilePath);

                        return;
                    }

                    SetNotification(" The private key file does not longer exists in: \n \"" + ecKeyFilePath +
                                    "\"\n Please, proceed with the following instructions.");
                    Console.Clear();
                    DrawTitle();
                }
            }

            Console.WriteLine(" Enter the full path for the private key files where this will loaded from or generated:");
            Console.WriteLine(" If click Enter, a file named \"bitpay_private_" + env.ToLower() +
                              " will be generated in the root of this application and");
            Console.WriteLine(" any file with the same name in this directory will be overwritten.");
            Console.WriteLine();
            Console.Write(" > ");
            var newEcKeyPath = Console.ReadLine().Trim();

            if (string.IsNullOrEmpty(newEcKeyPath))
            {
                ecKeyFilePath = @"bitpay_private_" + env.ToLower() + ".key";
                if (KeyUtils.PrivateKeyExists(ecKeyFilePath))
                {
                    SetNotification(" The file name entered already exists: \n \"" + ecKeyFilePath +
                                    "\"\n Make sure you want to modify it and then delete it before trying again");

                    return;
                }

                ecKey = KeyUtils.CreateEcKey();
                KeyUtils.SaveEcKey(ecKey);
                SetNotification(" New private key generated successfully with public key:\n " + ecKey.PublicKeyHexBytes +
                                "\n in: \"" + ecKeyFilePath + "\"");
            }
            else
            {
                if (!File.Exists(newEcKeyPath))
                {
                    SetNotification(" The file entered not found in: \n \"" + newEcKeyPath + "\"");
                    Console.Clear();
                    DrawTitle();
                    Console.WriteLine(" Would you like to provide a different file path? [yes|no] (default: no)");
                    Console.WriteLine(
                        " If 'no', a new file will be generated in the entered location with the given name.");
                    Console.WriteLine();
                    Console.Write(" > ");
                    var answer = Console.ReadLine();
                    while (answer.ToLower() != "yes" && answer.ToLower() != "no" && answer.ToLower() != "")
                    {
                        answer = Console.ReadLine();
                    }

                    if (answer.ToLower() == "yes")
                    {
                        GenerateKeyPair(ecKey);
                    }

                    if (KeyUtils.PrivateKeyExists(newEcKeyPath))
                    {
                        SetNotification(" The file name entered already exists: \n \"" + newEcKeyPath +
                                        "\"\n Be sure you want to modify it and then delete it manually before trying again");

                        return;
                    }
                    ecKeyFilePath = newEcKeyPath;
                }

                try
                {
                    ecKey = KeyUtils.CreateEcKey();
                    KeyUtils.SaveEcKey(ecKey);
                }
                catch (Exception e)
                {
                    SetNotification(
                        " An error occurred, please, check if the you have the right\n permissions to write in the specified directory.\n Error Details: " +
                        e.Message);

                    return;
                }

                SetNotification(" New key pair generated successfully with public key: " + ecKey.PublicKeyHexBytes +
                                " in: \n \"" + newEcKeyPath + "\"");
            }

            if (env == Env.Test)
            {
                appConfig.BitPayConfiguration.EnvConfig.Test.PrivateKeyPath = ecKeyFilePath;
            }
            if (env == Env.Prod)
            {
                appConfig.BitPayConfiguration.EnvConfig.Prod.PrivateKeyPath = ecKeyFilePath;
            }

            GenerateConfFile(confFilePath);
        }