protected override Task <EditNBXplorerWalletExternalServiceDataViewModel> BuildViewModel( ExternalServiceData data) { var vm = new EditNBXplorerWalletExternalServiceDataViewModel( new NBXplorerWalletService(data, _nbXplorerPublicWalletProvider, _derivationSchemeParser, _nbXplorerClientProvider) .GetData()); vm.CryptoCodes = new SelectList(_nbXplorerOptions.Cryptos?.ToList() ?? new List <string>(), vm.CryptoCode); return(Task.FromResult(vm)); }
BuildModel(EditNBXplorerWalletExternalServiceDataViewModel viewModel, ExternalServiceData mainModel) { var failureViewModel = new EditNBXplorerWalletExternalServiceDataViewModel(viewModel); failureViewModel.CryptoCodes = new SelectList(_nbXplorerOptions.Cryptos?.ToList() ?? new List <string>(), viewModel.CryptoCode); if (viewModel.Action.Equals("add-private-key", StringComparison.InvariantCultureIgnoreCase)) { failureViewModel.PrivateKeys.Add(new PrivateKeyDetails()); return(null, failureViewModel); } if (viewModel.Action.StartsWith("remove-private-key", StringComparison.InvariantCultureIgnoreCase)) { var index = int.Parse(viewModel.Action.Substring(viewModel.Action.IndexOf(":") + 1)); failureViewModel.PrivateKeys.RemoveAt(index); return(null, failureViewModel); } if ((!string.IsNullOrEmpty(viewModel.DerivationStrategy) && !string.IsNullOrEmpty(viewModel.Address)) || string.IsNullOrEmpty(viewModel.DerivationStrategy) && string.IsNullOrEmpty(viewModel.Address)) { ModelState.AddModelError(string.Empty, "Please choose to track either an address OR a derivation scheme"); } var client = _nbXplorerClientProvider.GetClient(viewModel.CryptoCode); BitcoinAddress address = null; DerivationStrategyBase derivationStrategy = null; if (!string.IsNullOrEmpty(viewModel.Address) && !string.IsNullOrEmpty(viewModel.CryptoCode)) { try { var factory = client.Network.DerivationStrategyFactory; address = BitcoinAddress.Create(viewModel.Address, factory.Network); } catch (Exception) { ModelState.AddModelError(nameof(viewModel.Address), "Invalid Address"); } } if (!string.IsNullOrEmpty(viewModel.DerivationStrategy) && !string.IsNullOrEmpty(viewModel.CryptoCode)) { try { var factory = client.Network.DerivationStrategyFactory; derivationStrategy = _derivationSchemeParser.Parse(factory, viewModel.DerivationStrategy); } catch { ModelState.AddModelError(nameof(viewModel.DerivationStrategy), "Invalid Derivation Scheme"); } } //current External Service data var externalServiceData = mainModel; externalServiceData.Set(viewModel); if (!ModelState.IsValid) { return(null, failureViewModel); } if (derivationStrategy != null) { client.Track(TrackedSource.Create(derivationStrategy)); } if (address != null) { client.Track(TrackedSource.Create(address)); } return(externalServiceData, null); }