Adjust API CorePaymentAccountsService for creating XMR accts

This commit is contained in:
ghubstan 2021-12-02 19:00:22 -03:00
parent b85459d7c6
commit ce8b6df9c5
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -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<String> 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> cryptoCurrency = getCryptoCurrency(bsqCode);
Optional<CryptoCurrency> 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",