Repository navigation
Expand file tree
/
Copy pathChecksum.java
More file actions
91 lines (80 loc) · 3.61 KB
/
Copy pathChecksum.java
File metadata and controls
91 lines (80 loc) · 3.61 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
89
90
91
package jota.utils;
import jota.error.InvalidAddressException;
import jota.pow.JCurl;
/**
* This class defines utility methods to add/remove the checksum to/from an address.
*
* @author pinpong
*/
public class Checksum {
/**
* Adds the checksum to the specified address.
*
* @param address The address without checksum.
* @return The address with the appended checksum.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
**/
public static String addChecksum(String address) throws InvalidAddressException {
InputValidator.checkAddress(address);
String addressWithChecksum = address;
addressWithChecksum += calculateChecksum(address);
return addressWithChecksum;
}
/**
* Remove the checksum to the specified address.
*
* @param address The address with checksum.
* @return The address without checksum.
* @throws InvalidAddressException is thrown when the specified address is not an address with checksum.
**/
public static String removeChecksum(String address) throws InvalidAddressException {
if (isAddressWithChecksum(address)) {
return removeChecksumFromAddress(address);
} else if (isAddressWithoutChecksum(address)) {
return address;
}
throw new InvalidAddressException();
}
private static String removeChecksumFromAddress(String addressWithChecksum) {
return addressWithChecksum.substring(0, Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM);
}
/**
* Determines whether the specified address with checksum has a valid checksum.
*
* @param addressWithChecksum The address with checksum.
* @return <code>true</code> if the specified address with checksum has a valid checksum [the specified address with checksum]; otherwise, <code>false</code>.
* @throws InvalidAddressException is thrown when the specified address is not an valid address.
**/
public static boolean isValidChecksum(String addressWithChecksum) throws InvalidAddressException {
String addressWithoutChecksum = removeChecksum(addressWithChecksum);
String addressWithRecalculateChecksum = addressWithChecksum += calculateChecksum(addressWithoutChecksum);
return addressWithRecalculateChecksum.equals(addressWithChecksum);
}
/**
* Check if specified address is a address with checksum.
*
* @param address The address to check.
* @return <code>true</code> if the specified address is with checksum ; otherwise, <code>false</code>.
* @throws InvalidAddressException is thrown when the specified address is not an valid address
**/
public static boolean isAddressWithChecksum(String address) throws InvalidAddressException {
return InputValidator.checkAddress(address) && address.length() == Constants.ADDRESS_LENGTH_WITH_CHECKSUM;
}
/**
* check if specified address is a address
*
* @param address address
* @return boolean
* @throws InvalidAddressException is thrown when the specified address is not an valid address
**/
public static boolean isAddressWithoutChecksum(String address) throws InvalidAddressException {
return InputValidator.checkAddress(address) && address.length() == Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM;
}
private static String calculateChecksum(String address) {
JCurl curl = new JCurl();
curl.reset();
curl.setState(Converter.copyTrits(address, curl.getState()));
curl.transform();
return Converter.trytes(curl.getState()).substring(0, 9);
}
}