-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputValidator.java
More file actions
88 lines (71 loc) · 2.33 KB
/
InputValidator.java
File metadata and controls
88 lines (71 loc) · 2.33 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
package jota.utils;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import jota.model.Transaction;
import jota.model.Transfer;
/**
* Created by pinpong on 02.12.16.
*/
public class InputValidator {
public static boolean isAddress(String address) {
return (address.length() == Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM ||
address.length() == Constants.ADDRESS_LENGTH_WITH_CHECKSUM) && isTrytes(address, address.length());
}
public static boolean checkAddress(String address) {
if (!isAddress(address)) {
throw new RuntimeException("Invalid address: " + address);
}
return true;
}
public static boolean isTrytes(final String trytes, final int length) {
return trytes.matches("^[A-Z9]{" + (length == 0 ? "0," : length) + "}$");
}
public static boolean isValue(final String value) {
return StringUtils.isNumeric(value);
}
public static boolean isArrayOfHashes(String[] hashes) {
if (hashes == null) return false;
for (String hash : hashes) {
// Check if address with checksum
if (hash.length() == 90) {
if (!isTrytes(hash, 90)) {
return false;
}
} else {
if (!isTrytes(hash, 81)) {
return false;
}
}
}
return true;
}
/**
* checks if input is correct hash collections
*
* @method isTransfersArray
* @param {array} hash
* @returns {boolean}
**/
public static boolean isTransfersCollectionCorrect(final List<Transfer> transfers) {
for (final Transfer transfer : transfers) {
if (!isTransfersArray(transfer)) {
return false;
}
}
return true;
}
public static boolean isTransfersArray(final Transfer transfer) {
if (!isAddress(transfer.getAddress())) {
return false;
}
// Check if message is correct trytes of any length
if (!isTrytes(transfer.getMessage(), 0)) {
return false;
}
// Check if tag is correct trytes of {0,27} trytes
if (!isTrytes(transfer.getTag(), 27)) {
return false;
}
return true;
}
}