From ce8b6df9c58fe54d543a82e8be820d8a9bbadedb Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Thu, 2 Dec 2021 19:00:22 -0300 Subject: [PATCH] Adjust API CorePaymentAccountsService for creating XMR accts --- .../core/api/CorePaymentAccountsService.java | 53 ++++++++++++++++--- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java b/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java index 0638ddbda4..de05384738 100644 --- a/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java +++ b/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java @@ -28,6 +28,9 @@ 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; @@ -37,10 +40,14 @@ import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.function.Predicate; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import static bisq.common.app.DevEnv.isDaoTradingActivated; +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; @@ -48,6 +55,9 @@ import static java.lang.String.format; @Slf4j class CorePaymentAccountsService { + private final Predicate apiDoesSupportCryptoCurrencyAccount = (c) -> + c.equals("BSQ") || c.equals("XMR"); + private final CoreWalletsService coreWalletsService; private final AccountAgeWitnessService accountAgeWitnessService; private final PaymentAccountForm paymentAccountForm; @@ -104,12 +114,9 @@ class CorePaymentAccountsService { String currencyCode, String address, boolean tradeInstant) { - String bsqCode = currencyCode.toUpperCase(); - if (!bsqCode.equals("BSQ")) - throw new IllegalArgumentException("api does not currently support " + currencyCode + " accounts"); - - // Validate the BSQ address string but ignore the return value. - coreWalletsService.getValidBsqAddress(address); + String cryptoCurrencyCode = currencyCode.toUpperCase(); + verifyApiDoesSupportCryptoCurrencyAccount(cryptoCurrencyCode); + verifyCryptoCurrencyAddress(cryptoCurrencyCode, address); AssetAccount cryptoCurrencyAccount = tradeInstant ? (InstantCryptoCurrencyAccount) PaymentAccountFactory.getPaymentAccount(PaymentMethod.BLOCK_CHAINS_INSTANT) @@ -117,7 +124,7 @@ class CorePaymentAccountsService { cryptoCurrencyAccount.init(); cryptoCurrencyAccount.setAccountName(accountName); cryptoCurrencyAccount.setAddress(address); - Optional cryptoCurrency = getCryptoCurrency(bsqCode); + Optional cryptoCurrency = getCryptoCurrency(cryptoCurrencyCode); cryptoCurrency.ifPresent(cryptoCurrencyAccount::setSingleTradeCurrency); user.addPaymentAccount(cryptoCurrencyAccount); log.info("Saved crypto payment account with id {} and payment method {}.", @@ -137,6 +144,38 @@ class CorePaymentAccountsService { .collect(Collectors.toList()); } + private void verifyCryptoCurrencyAddress(String cryptoCurrencyCode, String address) { + if (cryptoCurrencyCode.equals("BSQ")) { + // Validate the BSQ address, but ignore the return value. + coreWalletsService.getValidBsqAddress(address); + } else { + Asset asset = getAsset(cryptoCurrencyCode); + if (!asset.validateAddress(address).isValid()) + throw new IllegalArgumentException( + format("%s is not a valid %s address", + address, + cryptoCurrencyCode.toLowerCase())); + } + } + + private void verifyApiDoesSupportCryptoCurrencyAccount(String cryptoCurrencyCode) { + if (!apiDoesSupportCryptoCurrencyAccount.test(cryptoCurrencyCode)) + throw new IllegalArgumentException( + format("api does not currently support %s accounts", + cryptoCurrencyCode.toLowerCase())); + + } + + private Asset getAsset(String cryptoCurrencyCode) { + return findAsset(new AssetRegistry(), + cryptoCurrencyCode, + baseCurrencyNetwork(), + isDaoTradingActivated()) + .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",