Skip to content

Commit 84b60fb

Browse files
author
AZ
committed
[WIP ]tryfix getInputs, start to finish the god damn java lib
1 parent 58874ca commit 84b60fb

9 files changed

Lines changed: 634 additions & 68 deletions

File tree

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

Lines changed: 347 additions & 67 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static GetNewAddressResponse create(List<String> addresses) {
1212
return res;
1313
}
1414

15-
public List<String> getAddress() {
15+
public List<String> getAddresses() {
1616
return addresses;
1717
}
1818
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jota.error;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.Collection;
6+
7+
/**
8+
* Created by Adrian on 09.12.2016.
9+
*/
10+
public class ArgumentException extends BaseException {
11+
public ArgumentException() {
12+
super("wrong arguments passed to function");
13+
}
14+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package jota.error;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.Collection;
6+
7+
/**
8+
* Created by Adrian on 09.12.2016.
9+
*/
10+
public class BaseException extends Exception {
11+
protected Collection<String> messages;
12+
13+
public BaseException(String msg) {
14+
super(msg);
15+
}
16+
17+
18+
public BaseException(String msg, Exception cause) {
19+
super(msg, cause);
20+
}
21+
22+
23+
public BaseException(Collection<String> messages) {
24+
super();
25+
this.messages = messages;
26+
}
27+
28+
29+
public BaseException(Collection<String> messages, Exception cause) {
30+
super(cause);
31+
this.messages = messages;
32+
}
33+
34+
@Override
35+
public String getMessage() {
36+
String msg;
37+
38+
if (this.messages != null && !this.messages.isEmpty()) {
39+
msg = "[";
40+
41+
for (String message : this.messages) {
42+
msg += message + ",";
43+
}
44+
45+
msg = StringUtils.removeEnd(msg, ",") + "]";
46+
47+
} else msg = super.getMessage();
48+
49+
return msg;
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jota.error;
2+
3+
/**
4+
* Created by Adrian on 09.12.2016.
5+
*/
6+
public class NotEnoughBalanceException extends BaseException {
7+
public NotEnoughBalanceException() {
8+
super("not enough balance dude");
9+
}
10+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package jota.model;
2+
3+
import org.apache.commons.lang3.builder.ToStringBuilder;
4+
import org.apache.commons.lang3.builder.ToStringStyle;
5+
6+
/**
7+
* Created by Adrian on 09.12.2016.
8+
*/
9+
public class Input {
10+
private String address;
11+
private long balance;
12+
private int keyIndex;
13+
14+
public Input(String address, long balance, int keyIndex) {
15+
this.address = address;
16+
this.balance = balance;
17+
this.keyIndex = keyIndex;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
23+
}
24+
25+
public String getAddress() {
26+
return address;
27+
}
28+
29+
public void setAddress(String address) {
30+
this.address = address;
31+
}
32+
33+
public long getBalance() {
34+
return balance;
35+
}
36+
37+
public void setBalance(long balance) {
38+
this.balance = balance;
39+
}
40+
41+
public int getKeyIndex() {
42+
return keyIndex;
43+
}
44+
45+
public void setKeyIndex(int keyIndex) {
46+
this.keyIndex = keyIndex;
47+
}
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package jota.model;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by Adrian on 09.12.2016.
9+
*/
10+
public class Inputs {
11+
private List<Input> inputsList;
12+
private long totalBalance;
13+
14+
public Inputs(List<Input> inputsList, long totalBalance) {
15+
this.inputsList = inputsList;
16+
this.totalBalance = totalBalance;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return new Gson().toJson(this);
22+
}
23+
24+
public List<Input> getInputsList() {
25+
return inputsList;
26+
}
27+
28+
public void setInputsList(List<Input> inputsList) {
29+
inputsList = inputsList;
30+
}
31+
32+
public long getTotalBalance() {
33+
return totalBalance;
34+
}
35+
36+
public void setTotalBalance(long totalBalance) {
37+
totalBalance = totalBalance;
38+
}
39+
}

‎src/main/java/jota/model/Transaction.java‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,59 @@ public String toString() {
4444
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
4545
}
4646

47+
public void setSignatureMessageChunk(String signatureMessageChunk) {
48+
49+
this.signatureMessageChunk = signatureMessageChunk;
50+
}
51+
52+
public void setIndex(String index) {
53+
this.index = index;
54+
}
55+
56+
public void setApprovalNonce(String approvalNonce) {
57+
this.approvalNonce = approvalNonce;
58+
}
59+
60+
public void setHash(String hash) {
61+
this.hash = hash;
62+
}
63+
64+
public void setDigest(String digest) {
65+
this.digest = digest;
66+
}
67+
68+
public void setType(String type) {
69+
this.type = type;
70+
}
71+
72+
public void setTimestamp(String timestamp) {
73+
this.timestamp = timestamp;
74+
}
75+
76+
public void setTrunkTransaction(String trunkTransaction) {
77+
this.trunkTransaction = trunkTransaction;
78+
}
79+
80+
public void setBranchTransaction(String branchTransaction) {
81+
this.branchTransaction = branchTransaction;
82+
}
83+
84+
public void setSignatureNonce(String signatureNonce) {
85+
this.signatureNonce = signatureNonce;
86+
}
87+
88+
public void setAddress(String address) {
89+
this.address = address;
90+
}
91+
92+
public void setValue(String value) {
93+
this.value = value;
94+
}
95+
96+
public void setBundle(String bundle) {
97+
this.bundle = bundle;
98+
}
99+
47100
public String getValue() {
48101
return value;
49102
}

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package jota.utils;
22

33
import jota.dto.response.GetBundleResponse;
4+
import jota.model.Bundle;
5+
import jota.model.Input;
46
import org.apache.commons.lang3.NotImplementedException;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79

10+
import java.util.List;
11+
812
/**
913
* Client Side computation service
1014
*
@@ -39,5 +43,72 @@ public static String newAddress(String seed, int index, boolean checksum) {
3943
public static GetBundleResponse getBundle(final String transaction) {
4044
throw new NotImplementedException("Not yet implemented");
4145
}
46+
47+
/*
48+
public static List<String> signInputsAndReturn(String seed, List<Input> inputs, Bundle bundle,
49+
List<String> signatureFragments) {
50+
bundle.finalize();
51+
bundle.addTrytes(signatureFragments);
52+
53+
// SIGNING OF INPUTS
54+
//
55+
// Here we do the actual signing of the inputs
56+
// Iterate over all bundle transactions, find the inputs
57+
// Get the corresponding private key and calculate the signatureFragment
58+
for (int i = 0; i < bundle.getTransactions().size(); i++) {
59+
if (Long.parseLong(bundle.getTransactions().get(i).getValue()) < 0) {
60+
String thisAddress = bundle.getTransactions().get(i).getAddress();
61+
62+
// Get the corresponding keyIndex of the address
63+
int keyIndex = 0;
64+
for (int k = 0; k < inputs.size(); k++) {
65+
if (inputs.get(k).getAddress().equals(thisAddress)) {
66+
keyIndex = inputs.get(k).getKeyIndex();
67+
break;
68+
}
69+
}
70+
71+
String bundleHash = bundle.getTransactions().get(i).getBundle();
72+
73+
// Get corresponding private key of address
74+
int[] key = Signing.key(Converter.trits(seed), keyIndex, 2);
75+
76+
// First 6561 trits for the firstFragment
77+
var firstFragment = key.Take(6561);
78+
79+
// Get the normalized bundle hash
80+
String normalizedBundleHash = bundle.normalizedBundle(bundleHash);
81+
/*
82+
// First bundle fragment uses 27 trytes
83+
var firstBundleFragment = normalizedBundleHash(27);
84+
85+
// Calculate the new signatureFragment with the first bundle fragment
86+
var firstSignedFragment = Signing.signatureFragment(firstBundleFragment, firstFragment);
87+
88+
// Convert signature to trytes and assign the new signatureFragment
89+
bundle.Transactions[i].signatureMessageFragment = Converter.trytes(firstSignedFragment);
90+
91+
// Because the signature is > 2187 trytes, we need to
92+
// find the second transaction to add the remainder of the signature
93+
for (var j = 0; j < bundle.Transactions.Count; j++)
94+
{
95+
// Same address as well as value = 0 (as we already spent the input)
96+
if (bundle.Transactions[j].Address == thisAddress && bundle.Transactions[j].Value == 0)
97+
{
98+
// Use the second 6562 trits
99+
var secondFragment = key.Skip(6561).Take(6561);
100+
101+
// The second 27 to 54 trytes of the bundle hash
102+
var secondBundleFragment = normalizedBundleHash.slice(27, 27*2);
103+
104+
// Calculate the new signature
105+
var secondSignedFragment = Signing.signatureFragment(secondBundleFragment, secondFragment);
106+
107+
// Convert signature to trytes and assign it again to this bundle entry
108+
bundle.Transactions[j].signatureMessageFragment = Converter.trytes(secondSignedFragment);
109+
}
110+
}*/
111+
// }
112+
// }
42113
}
43114

0 commit comments

Comments
 (0)