Skip to content

Commit b7b45bb

Browse files
committed
first GetNewAddress implementation
1 parent 87bbf9c commit b7b45bb

7 files changed

Lines changed: 307 additions & 4 deletions

File tree

‎src/main/java/jota/IotaAPIProxy.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import jota.dto.request.*;
1313
import jota.dto.response.*;
14+
import jota.utils.IotaAPIUtils;
1415
import okhttp3.OkHttpClient;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;

‎src/main/java/jota/dto/response/GetNewAddressResponse.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ public class GetNewAddressResponse extends AbstractResponse {
77
public String getAddress() {
88
return address;
99
}
10+
11+
public static GetNewAddressResponse create(String address) {
12+
GetNewAddressResponse res = new GetNewAddressResponse();
13+
res.address = address;
14+
return res;
15+
}
1016
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package jota.utils;
2+
3+
import java.util.Arrays;
4+
5+
public class Converter {
6+
7+
public static final int RADIX = 3;
8+
public static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
9+
10+
public static final int NUMBER_OF_TRITS_IN_A_BYTE = 5;
11+
public static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3;
12+
13+
static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][];
14+
static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][];
15+
16+
public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
17+
18+
static {
19+
20+
final int[] trits = new int[NUMBER_OF_TRITS_IN_A_BYTE];
21+
22+
for (int i = 0; i < 243; i++) {
23+
BYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_BYTE);
24+
increment(trits, NUMBER_OF_TRITS_IN_A_BYTE);
25+
}
26+
27+
for (int i = 0; i < 27; i++) {
28+
TRYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
29+
increment(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
30+
}
31+
}
32+
33+
public static byte[] bytes(final int[] trits, final int offset, final int size) {
34+
35+
final byte[] bytes = new byte[(size + NUMBER_OF_TRITS_IN_A_BYTE - 1) / NUMBER_OF_TRITS_IN_A_BYTE];
36+
for (int i = 0; i < bytes.length; i++) {
37+
38+
int value = 0;
39+
for (int j = (size - i * NUMBER_OF_TRITS_IN_A_BYTE) < 5 ? (size - i * NUMBER_OF_TRITS_IN_A_BYTE) : NUMBER_OF_TRITS_IN_A_BYTE; j-- > 0; ) {
40+
value = value * RADIX + trits[offset + i * NUMBER_OF_TRITS_IN_A_BYTE + j];
41+
}
42+
bytes[i] = (byte)value;
43+
}
44+
45+
return bytes;
46+
}
47+
48+
public static byte[] bytes(final int[] trits) {
49+
return bytes(trits, 0, trits.length);
50+
}
51+
52+
public static void getTrits(final byte[] bytes, final int[] trits) {
53+
54+
int offset = 0;
55+
for (int i = 0; i < bytes.length && offset < trits.length; i++) {
56+
System.arraycopy(BYTE_TO_TRITS_MAPPINGS[bytes[i] < 0 ? (bytes[i] + BYTE_TO_TRITS_MAPPINGS.length) : bytes[i]], 0, trits, offset, trits.length - offset < NUMBER_OF_TRITS_IN_A_BYTE ? (trits.length - offset) : NUMBER_OF_TRITS_IN_A_BYTE);
57+
offset += NUMBER_OF_TRITS_IN_A_BYTE;
58+
}
59+
while (offset < trits.length) {
60+
trits[offset++] = 0;
61+
}
62+
}
63+
64+
public static int[] trits(final String trytes) {
65+
66+
final int[] trits = new int[trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE];
67+
for (int i = 0; i < trytes.length(); i++) {
68+
System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, trits, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
69+
}
70+
71+
return trits;
72+
}
73+
74+
public static void copyTrits(final long value, final int[] destination, final int offset, final int size) {
75+
76+
long absoluteValue = value < 0 ? -value : value;
77+
for (int i = 0; i < size; i++) {
78+
79+
int remainder = (int)(absoluteValue % RADIX);
80+
absoluteValue /= RADIX;
81+
if (remainder > MAX_TRIT_VALUE) {
82+
83+
remainder = MIN_TRIT_VALUE;
84+
absoluteValue++;
85+
}
86+
destination[offset + i] = remainder;
87+
}
88+
89+
if (value < 0) {
90+
91+
for (int i = 0; i < size; i++) {
92+
destination[offset + i] = -destination[offset + i];
93+
}
94+
}
95+
}
96+
97+
public static String trytes(final int[] trits, final int offset, final int size) {
98+
99+
StringBuilder trytes = new StringBuilder();
100+
for (int i = 0; i < (size + NUMBER_OF_TRITS_IN_A_TRYTE - 1) / NUMBER_OF_TRITS_IN_A_TRYTE; i++) {
101+
102+
int j = trits[offset + i * 3] + trits[offset + i * 3 + 1] * 3 + trits[offset + i * 3 + 2] * 9;
103+
if (j < 0) {
104+
105+
j += TRYTE_ALPHABET.length();
106+
}
107+
trytes.append(TRYTE_ALPHABET.charAt(j));
108+
}
109+
return trytes.toString();
110+
}
111+
112+
public static String trytes(final int[] trits) {
113+
return trytes(trits, 0, trits.length);
114+
}
115+
116+
public static int tryteValue(final int[] trits, final int offset) {
117+
return trits[offset] + trits[offset + 1] * 3 + trits[offset + 2] * 9;
118+
}
119+
120+
public static void increment(final int[] trits, final int size) {
121+
122+
for (int i = 0; i < size; i++) {
123+
if (++trits[i] > Converter.MAX_TRIT_VALUE) {
124+
trits[i] = Converter.MIN_TRIT_VALUE;
125+
} else {
126+
break;
127+
}
128+
}
129+
}
130+
}

‎src/main/java/jota/utils/Curl.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package jota.utils;
2+
3+
/**
4+
* (c) 2016 Come-from-Beyond
5+
*
6+
* Curl belongs to the sponge function family.
7+
*
8+
*/
9+
public class Curl {
10+
11+
public static final int HASH_LENGTH = 243;
12+
private static final int STATE_LENGTH = 3 * HASH_LENGTH;
13+
14+
private static final int NUMBER_OF_ROUNDS = 27;
15+
private static final int[] TRUTH_TABLE = {1, 0, -1, 1, -1, 0, -1, 1, 0};
16+
17+
private final int[] state = new int[STATE_LENGTH];
18+
19+
public void absorb(final int[] trits, int offset, int length) {
20+
21+
do {
22+
System.arraycopy(trits, offset, state, 0, length < HASH_LENGTH ? length : HASH_LENGTH);
23+
transform();
24+
offset += HASH_LENGTH;
25+
} while ((length -= HASH_LENGTH) > 0);
26+
}
27+
28+
public int [] squeeze(final int[] trits, int offset, int length) {
29+
30+
do {
31+
System.arraycopy(state, 0, trits, offset, length < HASH_LENGTH ? length : HASH_LENGTH);
32+
transform();
33+
offset += HASH_LENGTH;
34+
} while ((length -= HASH_LENGTH) > 0);
35+
36+
return state;
37+
}
38+
39+
private void transform() {
40+
41+
final int[] scratchpad = new int[STATE_LENGTH];
42+
int scratchpadIndex = 0;
43+
for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
44+
System.arraycopy(state, 0, scratchpad, 0, STATE_LENGTH);
45+
for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
46+
state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndex] + scratchpad[scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365)] * 3 + 4];
47+
}
48+
}
49+
}
50+
51+
public void reset() {
52+
for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
53+
state[stateIndex] = 0;
54+
}
55+
}
56+
57+
public int[] getState() {
58+
return state;
59+
}
60+
}

src/main/java/jota/IotaAPIUtils.java renamed to src/main/java/jota/utils/IotaAPIUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jota;
1+
package jota.utils;
22

33
import jota.dto.response.GetBundleResponse;
44
import jota.dto.response.GetNewAddressResponse;
@@ -15,8 +15,14 @@ public class IotaAPIUtils {
1515

1616
private static final Logger log = LoggerFactory.getLogger(IotaAPIUtils.class);
1717

18-
public static GetNewAddressResponse getNewAddress(final String seed, final Integer securityLevel) {
19-
throw new NotImplementedException("Not yet implemented");
18+
public static GetNewAddressResponse getNewAddress(final String seed, final int index) {
19+
20+
final int [] key = Signing.key(Converter.trits(seed), index, 2);
21+
final int [] digests = Signing.digests(key);
22+
final int [] addressTrits = Signing.address(digests);
23+
final String address = Converter.trytes(addressTrits);
24+
25+
return GetNewAddressResponse.create(address);
2026
}
2127

2228
public static GetBundleResponse getBundle(final String transaction) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package jota.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Signing {
8+
9+
static int[] key(int [] seed, int index, int length) {
10+
11+
final int[] subseed = seed;
12+
13+
for (int i = 0; i < index; i++) {
14+
for (int j = 0; j < 243; j++) {
15+
if (++subseed[j] > 1) {
16+
subseed[j] = -1;
17+
} else {
18+
break;
19+
}
20+
}
21+
}
22+
23+
Curl curl = new Curl();
24+
//curl.absorb(subseed, state);
25+
//curl.squeeze(subseed, state);
26+
curl.absorb(subseed, 0, subseed.length);
27+
28+
List<Integer> key = new ArrayList<>();
29+
int [] buffer = new int[subseed.length];
30+
int offset = 0;
31+
32+
while (length-- > 0) {
33+
34+
for (int i = 0; i < 27; i++) {
35+
36+
curl.squeeze(buffer, 0, buffer.length);
37+
for (int j = 0; j < 243; j++) {
38+
key.add(buffer[j]);
39+
}
40+
}
41+
}
42+
return to(key);
43+
}
44+
45+
private static int[] to(List<Integer> key) {
46+
int a [] = new int[key.size()]; int i = 0;
47+
for (Integer v : key) {
48+
a[i++] = v;
49+
}
50+
return a;
51+
}
52+
53+
public static int [] digests(int [] key) {
54+
final Curl curl = new Curl();
55+
56+
int[] digests = new int[key.length];
57+
int[] buffer = new int[key.length];
58+
59+
for (int i = 0; i < Math.floor(key.length / 6561); i++) {
60+
int [] keyFragment = Arrays.copyOfRange(key, i * 6561, (i + 1) * 6561);
61+
62+
for (int j = 0; j < 27; j++) {
63+
64+
buffer = Arrays.copyOfRange(keyFragment, j * 243, (j + 1) * 243);
65+
for (int k = 0; k < 26; k++) {
66+
67+
curl.absorb(buffer, 0, buffer.length);
68+
curl.squeeze(buffer, 0, buffer.length);
69+
}
70+
for (int k = 0; k < 243; k++) {
71+
72+
keyFragment[j * 243 + k] = buffer[k];
73+
}
74+
}
75+
76+
curl.absorb(keyFragment, 0, keyFragment.length);
77+
curl.squeeze(buffer, 0, buffer.length);
78+
79+
for (int j = 0; j < 243; j++) {
80+
digests[i * 243 + j] = buffer[j];
81+
}
82+
}
83+
return digests;
84+
}
85+
86+
public static int [] address(int [] digests) {
87+
final Curl curl = new Curl();
88+
int [] address = new int[digests.length];
89+
curl.absorb(digests, 0, digests.length);
90+
curl.squeeze(address, 0, address.length);
91+
return address;
92+
}
93+
}

‎src/test/java/jota/IotaAPIProxyTest.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
77
import jota.dto.response.*;
8+
import jota.utils.IotaAPIUtils;
89
import org.hamcrest.core.IsNull;
910
import org.junit.Before;
1011
import org.junit.Ignore;
@@ -122,5 +123,11 @@ public void shouldCreateIotaApiProxyInstanceWithDefaultValues() {
122123
IotaAPIProxy proxy = new IotaAPIProxy.Builder().build();
123124
assertThat(proxy, IsNull.notNullValue());
124125
}
125-
126+
127+
@Test
128+
public void shouldCreateANewAddress() {
129+
GetNewAddressResponse res = IotaAPIUtils.getNewAddress(TEST_SEED, 2);
130+
System.err.println(res);
131+
}
132+
126133
}

0 commit comments

Comments
 (0)