<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NBitcoin;
using Stratis.FederatedPeg.IntegrationTests.Utils;

using Xunit;
using Xunit.Abstractions;

namespace Stratis.FederatedPeg.Tests.Utils
{
    public class PowerShellScriptGeneratorAsTests : TestBase
    {
        private readonly ITestOutputHelper output;

        private Dictionary<int, string> consoleColors;

        private Action<string> newLine;

        private Dictionary<Mnemonic, PubKey> PubKeysByMnemonic { get { return originalPubKeysByMnemonic ?? this.pubKeysByMnemonic; }}

        private Dictionary<Mnemonic, PubKey> originalPubKeysByMnemonic;

        private IList<Mnemonic> originalMnemonics;

        private IList<Mnemonic> Mnemonics { get { return originalMnemonics ?? this.mnemonics; } }

        public PowerShellScriptGeneratorAsTests(ITestOutputHelper output)
        {
            this.output = output;
        }

        [Fact]
        public void Generate_PS1_Fragment_With_New_Mnemonics()
        {
            this.originalMnemonics = Enumerable.Range(0, this.mnemonics.Count)
                .Select(_ => new Mnemonic(Wordlist.English, WordCount.Twelve)).ToArray();
            this.originalPubKeysByMnemonic = this.Mnemonics.ToDictionary(m => m, m => m.DeriveExtKey().PrivateKey.PubKey);
            Generate_PS1_Fragment();
        }

        [Fact]
        public void Generate_PS1_Fragment()
        {
            var stringBuilder = new StringBuilder();
            this.newLine = s => stringBuilder.AppendLine(s);

            SetFolderVariables();
            CreateHelpers();
            CopyStratisChainFiles();
            CreatePoaKeyFiles();
            AddCommentedFederationDetails();
            SetFederationVariables();
            SetTimeoutVariables();
            SetConsoleColors();
            StartGatewayDs();
            StartChainsD();
            ShowRunningNodes();
            EnableWallets();

            this.output.WriteLine(stringBuilder.ToString());
        }

        private void CreatePoaKeyFiles()
        {
            //for now just use the same private keys for multisig wallet and block signing
            this.federationMemberIndexes.ForEach(i =>
            {
                var privateKey = this.Mnemonics[i].DeriveExtKey().PrivateKey;
                var targetFile = $"$root_datadir\\gateway{i + 1}\\fedpeg\\FederatedPegRegTest\\federationKey.dat";

                var keyAsString = System.BitConverter.ToString(privateKey.ToBytes());
                this.newLine($"$mining_key_hex_{i + 1} = \"{keyAsString}\"");
                this.newLine($"$bytes_{i + 1} = foreach($hexByte in $mining_key_hex_{i + 1}.Split(\"-\")) {{[System.Convert]::ToByte($hexByte, 16)}}");
                this.newLine($"New-Item -path \"{targetFile}\" -type file");
                this.newLine($"$bytes_{i + 1} | set-content {targetFile} -Encoding Byte");
            });
            this.newLine(Environment.NewLine);
        }

        private void StartChainsD()
        {<commit_msg>Refactored federation setup tool and added menue option to generate powershell script (#286)<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NBitcoin;
using Stratis.FederatedPeg.IntegrationTests.Utils;

using Xunit;
using Xunit.Abstractions;

namespace Stratis.FederatedPeg.Tests.Utils
{
    public class PowerShellScriptGeneratorAsTests : TestBase
    {
        private readonly ITestOutputHelper output;

        private Dictionary<int, string> consoleColors;

        private Action<string> newLine;

        private Dictionary<Mnemonic, PubKey> PubKeysByMnemonic { get { return originalPubKeysByMnemonic ?? this.pubKeysByMnemonic; }}

        private Dictionary<Mnemonic, PubKey> originalPubKeysByMnemonic;

        private IList<Mnemonic> originalMnemonics;

        private IList<Mnemonic> Mnemonics { get { return originalMnemonics ?? this.mnemonics; } }

        public PowerShellScriptGeneratorAsTests(ITestOutputHelper output)
        {
            this.output = output;
        }

        [Fact]
        public void Generate_PS1_Fragment_With_New_Mnemonics()
        {
            this.originalMnemonics = Enumerable.Range(0, this.mnemonics.Count)
                .Select(_ => new Mnemonic(Wordlist.English, WordCount.Twelve)).ToArray();
            this.originalPubKeysByMnemonic = this.Mnemonics.ToDictionary(m => m, m => m.DeriveExtKey().PrivateKey.PubKey);
            Generate_PS1_Fragment();
        }

        [Fact]
        public void Generate_PS1_Fragment()
        {
            this.output.WriteLine(CreatePowershellScript());
        }

        public string CreatePowershellScript()
        {
            var stringBuilder = new StringBuilder();
            this.newLine = s => stringBuilder.AppendLine(s);

            SetFolderVariables();
            CreateHelpers();
            CopyStratisChainFiles();
            CreatePoaKeyFiles();
            AddCommentedFederationDetails();
            SetFederationVariables();
            SetTimeoutVariables();
            SetConsoleColors();
            StartGatewayDs();
            StartChainsD();
            ShowRunningNodes();
            EnableWallets();

            return stringBuilder.ToString();
        }

        private void CreatePoaKeyFiles()
        {
            //for now just use the same private keys for multisig wallet and block signing
            this.federationMemberIndexes.ForEach(i =>
            {
                var privateKey = this.Mnemonics[i].DeriveExtKey().PrivateKey;
                var targetFile = $"$root_datadir\\gateway{i + 1}\\fedpeg\\FederatedPegRegTest\\federationKey.dat";

                var keyAsString = System.BitConverter.ToString(privateKey.ToBytes());
                this.newLine($"$mining_key_hex_{i + 1} = \"{keyAsString}\"");
                this.newLine($"$bytes_{i + 1} = foreach($hexByte in $mining_key_hex_{i + 1}.Split(\"-\")) {{[System.Convert]::ToByte($hexByte, 16)}}");
                this.newLine($"New-Item -path \"{targetFile}\" -type file");
                this.newLine($"$bytes_{i + 1} | set-content {targetFile} -Encoding Byte");
            });
            this.newLine(Environment.NewLine);
        }

        private void StartChainsD()
        {
{'fraction_non_alphanumeric': 0.10541220893643802, 'fraction_numerical': 0.0039332913782252985, 'mean_word_length': 2.434359805510535, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7647038', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1723, 'n_words': 331}