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