-
Notifications
You must be signed in to change notification settings - Fork 34
/
AssetValidationUtils.java
75 lines (62 loc) · 2.57 KB
/
AssetValidationUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package org.stellar.anchor.platform.utils;
import static java.util.stream.Collectors.toList;
import static org.stellar.anchor.util.MathHelper.decimal;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.stellar.anchor.api.exception.BadRequestException;
import org.stellar.anchor.api.rpc.method.AmountAssetRequest;
import org.stellar.anchor.api.sep.AssetInfo;
import org.stellar.anchor.asset.AssetService;
import org.stellar.anchor.util.SepHelper;
import org.stellar.anchor.util.StringHelper;
public class AssetValidationUtils {
private static final String STELLAR_ASSET_PREFIX = "stellar:";
public static boolean isStellarAsset(String asset) {
return asset.startsWith(STELLAR_ASSET_PREFIX);
}
/**
* validateAsset will validate if the provided amount has valid values and if its asset is
* supported.
*
* @param amount is the object containing the asset full name and the amount.
*/
public static void validateAsset(
String fieldName, AmountAssetRequest amount, AssetService assetService)
throws BadRequestException {
validateAsset(fieldName, amount, false, assetService);
}
public static void validateAsset(
String fieldName, AmountAssetRequest amount, boolean allowZero, AssetService assetService)
throws BadRequestException {
if (amount == null) {
return;
}
// asset amount needs to be non-empty and valid
SepHelper.validateAmount(fieldName + ".", amount.getAmount(), allowZero);
// asset name cannot be empty
if (StringHelper.isEmpty(amount.getAsset())) {
throw new BadRequestException(fieldName + ".asset cannot be empty");
}
List<AssetInfo> allAssets =
assetService.listAllAssets().stream()
.filter(assetInfo -> assetInfo.getSep38AssetName().equals(amount.getAsset()))
.collect(toList());
// asset name needs to be supported
if (CollectionUtils.isEmpty(allAssets)) {
throw new BadRequestException(
String.format("'%s' is not a supported asset.", amount.getAsset()));
}
if (allAssets.size() == 1) {
AssetInfo targetAsset = allAssets.get(0);
if (targetAsset.getSignificantDecimals() != null) {
// Check that significant decimal is correct
if (decimal(amount.getAmount(), targetAsset).compareTo(decimal(amount.getAmount())) != 0) {
throw new BadRequestException(
String.format(
"'%s' has invalid significant decimals. Expected: '%s'",
amount.getAmount(), targetAsset.getSignificantDecimals()));
}
}
}
}
}