refactored

This commit is contained in:
AZ
2017-01-29 20:23:23 +01:00
parent 34cfec47c0
commit 6176b0d556
8 changed files with 116 additions and 124 deletions
+88 -1
View File
@@ -1,12 +1,23 @@
package jota.model;
import jota.pow.ICurl;
import jota.pow.JCurl;
import jota.utils.Converter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
/**
* Created by pinpong on 02.12.16.
*/
public class Transaction {
private static final Logger log = LoggerFactory.getLogger(Transaction.class);
private ICurl customCurl;
private String hash;
private String signatureFragments;
private String address;
@@ -21,8 +32,21 @@ public class Transaction {
private String nonce;
private Boolean persistence;
public Transaction() {
public Transaction(ICurl curl) {
customCurl = curl;
}
public Transaction() {
customCurl = null;
}
public Transaction(String trytes) {
transactionObject(trytes);
}
public Transaction(String trytes, ICurl customCurl) {
transactionObject(trytes);
this.customCurl = customCurl;
}
public Transaction(String signatureFragments, String currentIndex, String lastIndex, String nonce, String hash, String tag, String timestamp, String trunkTransaction, String branchTransaction, String address, String value, String bundle) {
@@ -162,4 +186,67 @@ public class Transaction {
return obj != null && ((Transaction) obj).getHash().equals(this.getHash());
}
public String toTrytes() {
int[] valueTrits = Converter.trits(this.getValue(), 81);
int[] timestampTrits = Converter.trits(this.getTimestamp(), 27);
int[] currentIndexTrits = Converter.trits(this.getCurrentIndex(), 27);
int[] lastIndexTrits = Converter.trits(this.getLastIndex(), 27);
return this.getSignatureFragments()
+ this.getAddress()
+ Converter.trytes(valueTrits)
+ this.getTag()
+ Converter.trytes(timestampTrits)
+ Converter.trytes(currentIndexTrits)
+ Converter.trytes(lastIndexTrits)
+ this.getBundle()
+ this.getTrunkTransaction()
+ this.getBranchTransaction()
+ this.getNonce();
}
public void transactionObject(final String trytes) {
if (StringUtils.isEmpty(trytes)) {
log.warn("Warning: empty trytes in input for transactionObject");
return;
}
// validity check
for (int i = 2279; i < 2295; i++) {
if (trytes.charAt(i) != '9') {
log.warn("Trytes {} does not seem a valid tryte", trytes);
return;
}
}
int[] transactionTrits = Converter.trits(trytes);
int[] hash = new int[243];
final ICurl curl = customCurl == null ? new JCurl() : customCurl; // we need a fluent JCurl.
// generate the correct transaction hash
curl.reset();
curl.absorb(transactionTrits, 0, transactionTrits.length);
curl.squeeze(hash, 0, hash.length);
this.setHash(Converter.trytes(hash));
this.setSignatureFragments(trytes.substring(0, 2187));
this.setAddress(trytes.substring(2187, 2268));
this.setValue("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6804, 6837)));
this.setTag(trytes.substring(2295, 2322));
this.setTimestamp("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6966, 6993)));
this.setCurrentIndex("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 6993, 7020)));
this.setLastIndex("" + Converter.longValue(Arrays.copyOfRange(transactionTrits, 7020, 7047)));
this.setBundle(trytes.substring(2349, 2430));
this.setTrunkTransaction(trytes.substring(2430, 2511));
this.setBranchTransaction(trytes.substring(2511, 2592));
this.setNonce(trytes.substring(2592, 2673));
}
}