public async Task <IWriterResult> CreateAddressBook(string userId, AddressBookModel model) { try { var address = model.Address; if (model.AddressType == AddressType.Message || model.AddressType == AddressType.PayloadId || model.AddressType == AddressType.PaymentId) { address = $"{model.Address}:{model.PaymentId ?? string.Empty}"; } if (model.CurrencyId == Constant.NZDT_ID && !string.IsNullOrEmpty(model.BankAccount)) { address = $"{model.BankAccount}:{model.BankReference ?? string.Empty}"; if (!Constant.NZDT_BANK_REGEX.IsMatch(address)) { return(new WriterResult(false, "Invalid Bank Account Number")); } } if (model.AddressType == AddressType.PaymentId && !CurrencyExtensions.IsValidPaymentId(model.PaymentId)) { return(new WriterResult(false, "Invalid {0} PaymentId", model.Symbol)); } if (!await DepositService.ValidateAddress(model.CurrencyId, address).ConfigureAwait(false)) { return(new WriterResult(false, "Invalid {0} Address", model.Symbol)); } using (var context = ExchangeDataContextFactory.CreateContext()) { var currentUser = new Guid(userId); var addressBook = new Entity.AddressBook { Address = address, CurrencyId = model.CurrencyId, IsEnabled = true, Label = model.Label, UserId = currentUser, }; context.AddressBook.Add(addressBook); await context.SaveChangesAsync().ConfigureAwait(false); return(new WriterResult(true)); } } catch (Exception) { return(new WriterResult(false, "Failed to validate {0} Address", model.Symbol)); } }
public async Task <ActionResult> Create(WithdrawCurrencyModel model) { var currencyData = await CurrencyReader.GetCurrency(model.Symbol); var user = await UserManager.FindValidByIdAsync(User.Identity.GetUserId()); if (currencyData == null || user == null) { ViewBag.Message = $"Currency {model.Symbol} not found."; return(View("Error")); } if (currencyData.CurrencyId == Constant.NZDT_ID && !UserVerificationReader.IsVerified(user.VerificationLevel)) { return(View("NotVerified")); } // Verify Address var address = model.AddressData?.Trim(); if (string.IsNullOrEmpty(address)) { address = model.SelectedAddress; } else if (currencyData.AddressType != AddressType.Standard) { if (currencyData.AddressType == AddressType.PaymentId && !CurrencyExtensions.IsValidPaymentId(model.AddressData2?.Trim())) { ModelState.AddModelError("AddressData2", $"Please enter a valid Cryptonote PaymentId."); return(View(await CreateWithdrawModel(user, model))); } address = $"{model.AddressData?.Trim()}:{model.AddressData2?.Trim()}"; if (currencyData.Type == CurrencyType.Fiat) { address = address.TrimEnd(':'); } } if (string.IsNullOrEmpty(address)) { ModelState.AddModelError(user.IsUnsafeWithdrawEnabled ? "AddressData" : "AddressBookError", $"Please enter a valid {model.Symbol} address"); return(View(await CreateWithdrawModel(user, model))); } // Verify Amount var withdrawAmount = decimal.Round(model.Amount, currencyData.CurrencyDecimals); if (withdrawAmount < currencyData.WithdrawMin || withdrawAmount >= currencyData.WithdrawMax) { ModelState.AddModelError("Amount", $"Withdrawal amount must be between {currencyData.WithdrawMin} {currencyData.Symbol} and {currencyData.WithdrawMax} {currencyData.Symbol}"); return(View(await CreateWithdrawModel(user, model))); } // Verify Two Factor if (!await UserManager.VerifyUserTwoFactorCodeAsync(TwoFactorComponent.Withdraw, user.Id, model.Code1, model.Code2)) { ModelState.AddModelError("TwoFactorError", "The TFA code entered was incorrect or has expired, please try again."); return(View(await CreateWithdrawModel(user, model))); } // Create withdraw var twoFactortoken = await GenerateUserTwoFactorTokenAsync(TwoFactorTokenType.WithdrawConfirm, user.Id); var result = await WithdrawWriter.CreateWithdraw(User.Identity.GetUserId(), new CreateWithdrawModel { Address = address, Amount = withdrawAmount, CurrencyId = model.CurrencyId, TwoFactorToken = twoFactortoken, Type = WithdrawType.Normal, }); if (!result.Success) { ModelState.AddModelError("Error", result.Message); return(View(await CreateWithdrawModel(user, model))); } // Send Confirmation email if (!user.DisableWithdrawEmailConfirmation) { await SendConfirmationEmail(user, model.Symbol, withdrawAmount - model.Fee, address, result.Result, twoFactortoken, currencyData.AddressType); } return(RedirectToAction("Summary", new { withdrawId = result.Result, returnUrl = GetLocalReturnUrl(model.ReturnUrl) })); }