reject creating crypto payment account with invalid currency code or address

This commit is contained in:
napoly 2022-11-15 12:25:16 +01:00 committed by woodser
parent 4cbc511bbd
commit d3949614c6

View file

@ -30,6 +30,10 @@ import bisq.core.payment.PaymentAccount;
import bisq.core.payment.PaymentAccountFactory; import bisq.core.payment.PaymentAccountFactory;
import bisq.core.payment.payload.PaymentMethod; import bisq.core.payment.payload.PaymentMethod;
import bisq.core.user.User; import bisq.core.user.User;
import bisq.asset.Asset;
import bisq.asset.AssetRegistry;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -43,6 +47,8 @@ import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import static bisq.common.config.Config.baseCurrencyNetwork;
import static bisq.core.locale.CurrencyUtil.findAsset;
import static bisq.core.locale.CurrencyUtil.getCryptoCurrency; import static bisq.core.locale.CurrencyUtil.getCryptoCurrency;
import static java.lang.String.format; import static java.lang.String.format;
@ -74,7 +80,7 @@ class CorePaymentAccountsService {
paymentAccount.getPaymentAccountPayload().getPaymentMethodId()); paymentAccount.getPaymentAccountPayload().getPaymentMethodId());
return paymentAccount; return paymentAccount;
} }
private static void setSelectedTradeCurrency(PaymentAccount paymentAccount) { private static void setSelectedTradeCurrency(PaymentAccount paymentAccount) {
TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency(); TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency();
List<TradeCurrency> tradeCurrencies = paymentAccount.getTradeCurrencies(); List<TradeCurrency> tradeCurrencies = paymentAccount.getTradeCurrencies();
@ -125,16 +131,15 @@ class CorePaymentAccountsService {
String address, String address,
boolean tradeInstant) { boolean tradeInstant) {
accountService.checkAccountOpen(); accountService.checkAccountOpen();
if (currencyCode == null) throw new RuntimeException("Cryptocurrency code is null"); verifyCryptoCurrencyAddress(currencyCode.toUpperCase(), address);
var cryptoCurrencyAccount = tradeInstant AssetAccount cryptoCurrencyAccount = tradeInstant
? (InstantCryptoCurrencyAccount) PaymentAccountFactory.getPaymentAccount(PaymentMethod.BLOCK_CHAINS_INSTANT) ? (InstantCryptoCurrencyAccount) PaymentAccountFactory.getPaymentAccount(PaymentMethod.BLOCK_CHAINS_INSTANT)
: (CryptoCurrencyAccount) PaymentAccountFactory.getPaymentAccount(PaymentMethod.BLOCK_CHAINS); : (CryptoCurrencyAccount) PaymentAccountFactory.getPaymentAccount(PaymentMethod.BLOCK_CHAINS);
cryptoCurrencyAccount.init(); cryptoCurrencyAccount.init();
cryptoCurrencyAccount.setAccountName(accountName); cryptoCurrencyAccount.setAccountName(accountName);
cryptoCurrencyAccount.setAddress(address); cryptoCurrencyAccount.setAddress(address);
Optional<CryptoCurrency> cryptoCurrency = getCryptoCurrency(currencyCode.toUpperCase()); Optional<CryptoCurrency> cryptoCurrency = getCryptoCurrency(currencyCode.toUpperCase());
if (!cryptoCurrency.isPresent()) throw new RuntimeException("Unsupported cryptocurrency code: " + currencyCode.toUpperCase()); cryptoCurrency.ifPresent(cryptoCurrencyAccount::setSingleTradeCurrency);
cryptoCurrencyAccount.setSingleTradeCurrency(cryptoCurrency.get());
user.addPaymentAccount(cryptoCurrencyAccount); user.addPaymentAccount(cryptoCurrencyAccount);
if (!(cryptoCurrencyAccount instanceof AssetAccount)) accountAgeWitnessService.publishMyAccountAgeWitness(cryptoCurrencyAccount.getPaymentAccountPayload()); // TODO (woodser): applies to Haveno? if (!(cryptoCurrencyAccount instanceof AssetAccount)) accountAgeWitnessService.publishMyAccountAgeWitness(cryptoCurrencyAccount.getPaymentAccountPayload()); // TODO (woodser): applies to Haveno?
log.info("Saved crypto payment account with id {} and payment method {}.", log.info("Saved crypto payment account with id {} and payment method {}.",
@ -164,6 +169,25 @@ class CorePaymentAccountsService {
paymentAccount.validateFormField(form, fieldId, value); paymentAccount.validateFormField(form, fieldId, value);
} }
private void verifyCryptoCurrencyAddress(String cryptoCurrencyCode, String address) {
Asset asset = getAsset(cryptoCurrencyCode);
if (!asset.validateAddress(address).isValid())
throw new IllegalArgumentException(
format("%s is not a valid %s address",
address,
cryptoCurrencyCode.toLowerCase()));
}
private Asset getAsset(String cryptoCurrencyCode) {
return findAsset(new AssetRegistry(),
cryptoCurrencyCode,
baseCurrencyNetwork())
.orElseThrow(() -> new IllegalStateException(
format("crypto currency with code '%s' not found",
cryptoCurrencyCode.toLowerCase())));
}
private void verifyPaymentAccountHasRequiredFields(PaymentAccount paymentAccount) { private void verifyPaymentAccountHasRequiredFields(PaymentAccount paymentAccount) {
if (!paymentAccount.hasMultipleCurrencies() && paymentAccount.getSingleTradeCurrency() == null) if (!paymentAccount.hasMultipleCurrencies() && paymentAccount.getSingleTradeCurrency() == null)
throw new IllegalArgumentException(format("no trade currency defined for %s payment account", throw new IllegalArgumentException(format("no trade currency defined for %s payment account",