Skip to content

Commit

Permalink
chore: added wrong casing exception and check for address validation (#…
Browse files Browse the repository at this point in the history
…240)

* chore: added wrong casing exception and check for address validation

* chore: added wrong casing exception and check for address validation
  • Loading branch information
Kammerlo authored Aug 21, 2024
1 parent c3592f8 commit e224a74
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public class AccountServiceImpl implements AccountService {

@Override
public AccountBalanceResponse getAccountBalance(AccountBalanceRequest accountBalanceRequest) {


Long index = null;
String hash = null;
String accountAddress = accountBalanceRequest.getAccountIdentifier().getAddress();
if (Objects.isNull(CardanoAddressUtils.getEraAddressType(accountAddress))) {
log.error("[findBalanceDataByAddressAndBlock] Provided address is invalid {}", accountAddress);
throw ExceptionFactory.invalidAddressError(accountAddress);
}
CardanoAddressUtils.verifyAddress(accountAddress);

PartialBlockIdentifier blockIdentifier = accountBalanceRequest.getBlockIdentifier();
log.info("[accountBalance] Looking for block: {} || latest}", blockIdentifier);

Expand All @@ -69,15 +69,15 @@ public AccountBalanceResponse getAccountBalance(AccountBalanceRequest accountBal

@Override
public AccountCoinsResponse getAccountCoins(AccountCoinsRequest accountCoinsRequest) {
CardanoAddressUtils.verifyAddress(accountCoinsRequest.getAccountIdentifier().getAddress());

String accountAddress = accountCoinsRequest.getAccountIdentifier().getAddress();
CardanoAddressUtils.verifyAddress(accountAddress);

List<Currency> currencies = accountCoinsRequest.getCurrencies();
// accountCoinsRequest.getIncludeMempool(); // TODO

log.debug("[accountCoins] Request received {}", accountCoinsRequest);
if (Objects.isNull(CardanoAddressUtils.getEraAddressType(accountAddress))) {
log.debug("[findBalanceDataByAddressAndBlock] Provided address is invalid {}", accountAddress);
throw ExceptionFactory.invalidAddressError(accountAddress);
}

if (Objects.nonNull(currencies)) {
validateCurrencies(currencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static ApiException invalidAddressError(String address) {
Details.builder().message(address).build()));
}

public static ApiException invalidAddressCasingError(String address) {
return new ApiException(RosettaErrorType.INVALID_ADDRESS_CASING.toRosettaError(true,
Details.builder().message(address).build()));
}

public static ApiException missingStakingKeyError() {
return new ApiException(RosettaErrorType.STAKING_KEY_MISSING.toRosettaError(false));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cardanofoundation.rosetta.common.util;

import java.util.Objects;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -255,4 +256,21 @@ public static byte[] hexStringToBuffer(String input) {
boolean checkEmptyHexString = CardanoAddressUtils.isEmptyHexString(input);
return checkEmptyHexString ? HexUtil.decodeHexString(StringUtils.EMPTY) : HexUtil.decodeHexString(input);
}

public static void verifyAddress(String address) {
try {
if (address == null || !AddressUtil.isValidAddress(address)) {
throw ExceptionFactory.invalidAddressError(address);
}
} catch (RuntimeException e) {
log.debug("[verifyAddress] Provided address is invalid {}", address);
throw ExceptionFactory.invalidAddressError(Optional.ofNullable(address).orElse(Constants.EMPTY_SYMBOL));
}
// Shelley era checking for lower case, upper case characters can lead to problems with other tools
// if Shelley Era and contains upper case characters
if (Objects.equals(getEraAddressType(address), EraAddressType.SHELLEY) && address.chars().anyMatch(Character::isUpperCase)) {
log.debug("[verifyAddress] Provided address is invalid {}", address);
throw ExceptionFactory.invalidAddressCasingError(address);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public enum RosettaErrorType {
OUTSIDE_VALIDITY_INTERVAL_UTXO(
"Error when sending the transaction - OutsideValidityIntervalUTxO", 4037),
CONFIG_NOT_FOUND("Environment configurations needed to run server were not found", 4038),
INVALID_ADDRESS_CASING("Provided address has wrong casing, this might lead to problems with other tools", 4039),
UNSPECIFIED_ERROR("An error occurred", 5000),
NOT_IMPLEMENTED("Not implemented", 5001),
ADDRESS_GENERATION_ERROR("Address generation error", 5002),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ void getAccountCoinsInvalidAddressThrowTest() {
assertEquals(RosettaErrorType.INVALID_ADDRESS.getMessage(),
actualException.getError().getMessage());
verify(accountCoinsRequest).getAccountIdentifier();
verify(accountCoinsRequest).getCurrencies();
verifyNoMoreInteractions(accountCoinsRequest);
verifyNoMoreInteractions(accountIdentifier);
}
Expand Down Expand Up @@ -372,7 +371,6 @@ private void verifyPositiveAccountCoinsCase(AccountCoinsResponse actual, Utxo ut
verify(ledgerBlockService).findLatestBlockIdentifier();
verify(ledgerAccountService).findUtxoByAddressAndCurrency(accountAddress,
Collections.emptyList());
verify(accountCoinsRequest).getAccountIdentifier();
verify(accountCoinsRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountCoinsRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,19 @@ void publicKeyToHdPublicKeyInvalidPublicKeyTest() {
assertEquals(RosettaErrorType.INVALID_STAKING_KEY_FORMAT.getMessage(),
actualException.getError().getMessage());
}

@Test
void isAddressValidTest() {
String validAddress = "addr1qyj9der2u29gcuwcxum7ylyfqrcg86d0l76khlazmn9cnxgqk2v9xpzd89c93yaszz2zgczqshqvf424cmpmswlr2lnqt6j3lu";
// not expecting an exception
CardanoAddressUtils.verifyAddress(validAddress);
// checking null
assertThrows(ApiException.class, () -> CardanoAddressUtils.verifyAddress(null));
String invalidAddress = "addr1qyj9der2u29gcuwcxum7ylyfqrcg86d0l76khlazmn9cnxgqk2v9xpzd89c93yaszz2zgczqshqvf424cmpmswlr2lnqt6j3lb";
assertThrows(RuntimeException.class, () -> CardanoAddressUtils.verifyAddress(invalidAddress));
// wrong Casing
String wrongCasingAddress = "addr1qyj9der2u29gcuwcxum7ylyfqrcg86d0l76khlazmn9cnxgqk2v9xpzd89c93yaszz2zgczqshqvf424cmpmswlr2lnqt6j3lU";
assertThrows(ApiException.class, () -> CardanoAddressUtils.verifyAddress(wrongCasingAddress));

}
}

0 comments on commit e224a74

Please sign in to comment.