async Task <StakePoolUserConfig> ReadConfig(string configPath)
 {
     using (var sr = new StreamReader(configPath, Encoding.UTF8))
     {
         return(await StakePoolUserConfig.ReadConfig(_jsonSerializer, sr));
     }
 }
 async Task WriteConfig(StakePoolUserConfig config, string configPath)
 {
     using (var sw = new StreamWriter(configPath, false, Encoding.UTF8))
     {
         await config.WriteConfig(_jsonSerializer, sw);
     }
 }
        private async Task SaveCommandActionAsync()
        {
            try
            {
                var nextAddressResponse = await App.Current.Synchronizer.WalletRpcClient.NextExternalAddressAsync(SelectedVotingAccount.Account);

                var pubKeyAddress = nextAddressResponse.Item2;

                var bestApiVersion = PoolApiClient.BestSupportedApiVersion(SelectedStakePool.SupportedApiVersions);
                var client         = new PoolApiClient(bestApiVersion, SelectedStakePool.Uri, SelectedPoolApiKey, _httpClient);

                // Sumbit the pubkey.  In the current stakepool api version only a single pubkey can
                // be submitted per user, so if one was already submitted, simply ignore the error.
                try
                {
                    await client.CreateVotingAddressAsync(pubKeyAddress);
                }
                catch (PoolApiResponseException ex) when(ex.Code == StatusCode.AlreadyExists)
                {
                }

                var poolInfo = await client.GetPurchaseInfoAsync();

                var configEntry = new StakePoolUserConfig.Entry
                {
                    ApiKey             = SelectedPoolApiKey,
                    Host               = SelectedStakePool.Uri.Host,
                    MultisigVoteScript = poolInfo.RedeemScriptHex,
                    VotingAccount      = SelectedVotingAccount.Account.AccountNumber,
                };
                _configuredPools[configEntry.Host] = configEntry;

                var config = new StakePoolUserConfig {
                    Entries = _configuredPools.Values.ToArray()
                };
                await WriteConfig(config, _configPath);

                SelectedConfiguredPool = configEntry;

                _saveCommand.Executable = false;
                NeedsSaving             = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error communicating with stake pool");
            }
        }