Beispiel #1
0
        public async Task <IActionResult> AddDerivationScheme(string storeId, DerivationSchemeViewModel vm, string selectedScheme = null)
        {
            selectedScheme = selectedScheme ?? "BTC";
            var store = await _Repo.FindStore(storeId, GetUserId());

            if (store == null)
            {
                return(NotFound());
            }

            var network = vm.CryptoCurrency == null ? null : _ExplorerProvider.GetNetwork(vm.CryptoCurrency);

            vm.SetCryptoCurrencies(_ExplorerProvider, selectedScheme);
            if (network == null)
            {
                ModelState.AddModelError(nameof(vm.CryptoCurrency), "Invalid network");
                return(View(vm));
            }
            var wallet = _WalletProvider.GetWallet(network);

            if (wallet == null)
            {
                ModelState.AddModelError(nameof(vm.CryptoCurrency), "Invalid network");
                return(View(vm));
            }


            DerivationStrategyBase strategy = null;

            try
            {
                if (!string.IsNullOrEmpty(vm.DerivationScheme))
                {
                    strategy            = ParseDerivationStrategy(vm.DerivationScheme, vm.DerivationSchemeFormat, network);
                    vm.DerivationScheme = strategy.ToString();
                }
                store.SetDerivationStrategy(network, vm.DerivationScheme);
            }
            catch
            {
                ModelState.AddModelError(nameof(vm.DerivationScheme), "Invalid Derivation Scheme");
                vm.Confirmation = false;
                return(View(vm));
            }


            if (strategy == null || vm.Confirmation)
            {
                try
                {
                    if (strategy != null)
                    {
                        await wallet.TrackAsync(strategy);
                    }
                    store.SetDerivationStrategy(network, vm.DerivationScheme);
                }
                catch
                {
                    ModelState.AddModelError(nameof(vm.DerivationScheme), "Invalid Derivation Scheme");
                    return(View(vm));
                }

                await _Repo.UpdateStore(store);

                StatusMessage = $"Derivation scheme for {network.CryptoCode} has been modified.";
                return(RedirectToAction(nameof(UpdateStore), new { storeId = storeId }));
            }
            else
            {
                if (!string.IsNullOrEmpty(vm.DerivationScheme))
                {
                    var line = strategy.GetLineFor(DerivationFeature.Deposit);

                    for (int i = 0; i < 10; i++)
                    {
                        var address = line.Derive((uint)i);
                        vm.AddressSamples.Add((DerivationStrategyBase.GetKeyPath(DerivationFeature.Deposit).Derive((uint)i).ToString(), address.ScriptPubKey.GetDestinationAddress(network.NBitcoinNetwork).ToString()));
                    }
                }
                vm.Confirmation = true;
                return(View(vm));
            }
        }