mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2026-02-03 23:22:49 +00:00
added checksum calculation, pls review
This commit is contained in:
parent
0ec801a581
commit
144ae811be
@ -38,7 +38,11 @@ public class Checksum {
|
||||
}
|
||||
|
||||
private static String calculateChecksum(String address) {
|
||||
// TODO
|
||||
throw new NotImplementedException(address);
|
||||
Curl curl = new Curl();
|
||||
curl.reset();
|
||||
curl.setState(Converter.copyTrits(address, curl.getState()));
|
||||
curl.transform();
|
||||
String checksum = Converter.trytes(curl.getState()).substring(0, 9);
|
||||
return checksum;
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,6 +91,16 @@ public class Converter {
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] copyTrits(final String input, final int[] destination) {
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
int index = Constants.TRYTE_ALPHABET.indexOf(input.charAt(i));
|
||||
destination[i * 3] = TRYTE_TO_TRITS_MAPPINGS [index][0];
|
||||
destination[i * 3 + 1] = TRYTE_TO_TRITS_MAPPINGS[index][1];
|
||||
destination[i * 3 + 2] = TRYTE_TO_TRITS_MAPPINGS[index][2];
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
public static String trytes(final int[] trits, final int offset, final int size) {
|
||||
|
||||
StringBuilder trytes = new StringBuilder();
|
||||
|
||||
@ -13,7 +13,7 @@ public class Curl {
|
||||
private static final int NUMBER_OF_ROUNDS = 27;
|
||||
private static final int[] TRUTH_TABLE = {1, 0, -1, 1, -1, 0, -1, 1, 0};
|
||||
|
||||
private final int[] state = new int[STATE_LENGTH];
|
||||
private int[] state = new int[STATE_LENGTH];
|
||||
|
||||
public void absorb(final int[] trits, int offset, int length) {
|
||||
|
||||
@ -35,7 +35,7 @@ public class Curl {
|
||||
return state;
|
||||
}
|
||||
|
||||
private void transform() {
|
||||
public void transform() {
|
||||
|
||||
final int[] scratchpad = new int[STATE_LENGTH];
|
||||
int scratchpadIndex = 0;
|
||||
@ -56,4 +56,5 @@ public class Curl {
|
||||
public int[] getState() {
|
||||
return state;
|
||||
}
|
||||
public void setState(int[] state) { this.state = state; }
|
||||
}
|
||||
|
||||
@ -1,11 +1,5 @@
|
||||
package jota.utils;
|
||||
|
||||
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
|
||||
import jota.model.Transfer;
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by pinpong on 02.12.16.
|
||||
*/
|
||||
@ -26,10 +20,7 @@ public class InputValidator {
|
||||
}
|
||||
|
||||
public static boolean isTrytes(String trytes, int length) {
|
||||
|
||||
// If no length specified, just validate the trytes
|
||||
|
||||
Regex regexTrytes = new Regex("^[9A-Z]{" + (length == 0 ? "0," : length) + "}$");
|
||||
return trytes.matches(regexTrytes.toString());
|
||||
return trytes.matches("^[9A-Z]{" + (length == 0 ? "0," : length) + "}$");
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,15 +11,16 @@ import static org.junit.Assert.assertEquals;
|
||||
*/
|
||||
public class ChecksumTest {
|
||||
|
||||
private static final String TEST_ADDRESS = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAZETAIRPTM";
|
||||
private static final String TEST_ADDRESS_WITHOUT_CHECKSUM = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAZETAIRPTM";
|
||||
private static final String TEST_ADDRESS_WITH_CHECKSUM = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAZETAIRPTMLSDUKRPBM";
|
||||
|
||||
@Test
|
||||
public void shouldAddChecksum() {
|
||||
assertEquals(Checksum.addChecksum(TEST_ADDRESS), true);
|
||||
assertEquals(Checksum.addChecksum(TEST_ADDRESS_WITHOUT_CHECKSUM), TEST_ADDRESS_WITH_CHECKSUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveChecksum() {
|
||||
assertEquals(Checksum.addChecksum(TEST_ADDRESS), Checksum.isValidChecksum(Checksum.addChecksum(TEST_ADDRESS)));
|
||||
assertEquals(Checksum.removeChecksum(TEST_ADDRESS_WITH_CHECKSUM), TEST_ADDRESS_WITHOUT_CHECKSUM);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user