diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..964491c
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,13 @@
+language: java
+jdk:
+- oraclejdk8
+script: mvn package
+deploy:
+ provider: releases
+ file_glob: true
+ file: "/home/travis/build/iotaledger/iota.lib.java/target/jota*.jar"
+ skip_cleanup: true
+ on:
+ tags: true
+ repo: iotaledger/iota.lib.java
+ all_branches: true
diff --git a/README.md b/README.md
index c0f5a0e..915be80 100644
--- a/README.md
+++ b/README.md
@@ -4,11 +4,11 @@ The JOTA library is a simple Java wrapper around [[IOTA]](http://www.iotatoken.c
It allows to connect easily using java directly to a local or a remote [[IOTA node]](https://iota.readme.io/docs/syncing-to-the-network).
-* **Latest release:** 1.0.0 Release
-* **Compatibility:** in development to be fully compatible with IOTA IRI v1.1.0
+* **Latest release:** 0.9.0 RC1
+* **Compatibility:** fully compatible with IOTA IRI v1.2.4
* **API coverage:** 14 of 14 commands fully implemented
* **License:** Apache License 2.0
-* **Readme updated:** 2016-11-12 21:05:02 (UTC)
+* **Readme updated:** 2016-01-19 21:05:02 (UTC)
A list of all *IOTA* JSON-REST API commands currently supported by jota wrapper can be found in the `Commands` enum (see [here](https://github.com/davassi/JOTA/blob/master/src/main/java/jota/IotaAPICommands.java) for more details).
@@ -32,27 +32,33 @@ Other dependencies:
Connect to your local node with the default settings is quite straightforward: it requires only 2 lines of code. For example, in order to fetch the Node Info:
- IotaApiProxy api = new IotaApiProxy.Builder.build();
+ IotaApi api = new IotaApi.Builder.build();
GetNodeInfoResponse response = api.getNodeInfo();
-of if you need to connect to a remote node on https:
+of if you need to connect to a remote node:
- IotaApiProxy api = new IotaApiProxy.Builder
- .protocol("https")
+ IotaApi api = new IotaApi.Builder
+ .protocol("http")
.nodeAddress("somewhere_over_the_rainbow")
- .port(54321)
+ .port(14265)
.build();
GetNodeInfoResponse response = api.getNodeInfo();
-Jota is still *not* in the central maven repository. It will be available when it will cover 100% iota's rest interface.
+In order to communicate with *IOTA node*, JOTA needs to be aware of your node's exact configuration. If you dont want to use the builder the easiest way of providing this information is via a `node_config.properties` file, for example:
-In order to communicate with *IOTA node*, JOTA needs to be aware of your node's exact configuration. The easiest way of providing this information is via a `node_config.properties` file, for example:
-
- iota.node.protocol=http
+ iota.node.protocol=http``****************``
iota.node.host=127.0.0.1
iota.node.port=14265
+Jota is still *not* in the central maven repository. It will be available when it will cover 100% iota's rest interface.
+
+##Warning
+ - This is pre-release software!
+ - There may be performance and stability issues.
+ - You may loose all your money :)
+ - Please report any issues using the Issue Tracker"
+
That's it!
##Examples
@@ -63,7 +69,5 @@ There's an extensive list of test coverages on the src/test/java package of the
If JOTA has been useful to you and you feel like contributing, consider posting a bug report or a pull request. Alternatively, donations are very welcome too!
-* Bitcoin: `3FGCHqhG1SUpgn2eS1Agq2KnxJemWnQFbB`
-
-
-
+* Bitcoin (Gianni Davassi): `3FGCHqhG1SUpgn2eS1Agq2KnxJemWnQFbB`
+* Bitcoin (Adrian Ziser): `3FGCHqhG1SUpgn2eS1Agq2KnxJemWnQFbB`
\ No newline at end of file
diff --git a/node_config.properties b/node_config.properties
index f3c3793..fbd32d9 100644
--- a/node_config.properties
+++ b/node_config.properties
@@ -1,5 +1,7 @@
iota.node.protocol=http
-#iota.node.host=138.68.126.141
-iota.node.host=127.0.0.1
+iota.node.host=node.iotawallet.info
+#iota.node.host=138.68.90.186
+#iota.node.host=192.168.11.2
+#iota.node.host=138.68.90.186
iota.node.port=14265
diff --git a/pom.xml b/pom.xml
index 70b381e..31625ee 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,12 +2,20 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
- com.iota
+ org.iotajota
- 1.0.0-RELEASE
+ 0.9.0-RC1JOTAJOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface.
+
+
+ Apache License, Version 2.0
+ http://www.apache.org/licenses/LICENSE-2.0.txt
+ repo
+
+
+
1.7UTF-8
@@ -63,6 +71,7 @@
4.12test
+
@@ -91,6 +100,13 @@
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+ true
+
+
diff --git a/src/main/java/jota/IotaAPI.java b/src/main/java/jota/IotaAPI.java
new file mode 100644
index 0000000..8bd00fd
--- /dev/null
+++ b/src/main/java/jota/IotaAPI.java
@@ -0,0 +1,837 @@
+package jota;
+
+import jota.dto.response.*;
+import jota.error.*;
+import jota.model.*;
+import jota.pow.ICurl;
+import jota.pow.JCurl;
+import jota.utils.*;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * IotaAPI Builder. Usage:
+ *
+ * GetNodeInfoResponse response = api.getNodeInfo();
+ *
+ * @author davassi
+ */
+public class IotaAPI extends IotaAPICore {
+
+ private static final Logger log = LoggerFactory.getLogger(IotaAPI.class);
+ private ICurl customCurl;
+ private StopWatch stopWatch;
+
+ protected IotaAPI(Builder builder) {
+ super(builder);
+ customCurl = builder.customCurl;
+ }
+
+ /**
+ * Generates a new address from a seed and returns the remainderAddress.
+ * This is either done deterministically, or by providing the index of the new remainderAddress
+ *
+ * @param seed Tryte-encoded seed. It should be noted that this seed is not transferred
+ * @param index Optional (default null). Key index to start search from. If the index is provided, the generation of the address is not deterministic.
+ * @param checksum Optional (default false). Adds 9-tryte address checksum
+ * @param total Optional (default 1)Total number of addresses to generate
+ * @param returnAll If true, it returns all addresses which were deterministically generated (until findTransactions returns null)
+ * @return an array of strings with the specifed number of addresses
+ */
+ public GetNewAddressResponse getNewAddress(final String seed, final int index, final boolean checksum, final int total, final boolean returnAll) {
+ StopWatch stopWatch = new StopWatch();
+
+ List allAddresses = new ArrayList<>();
+
+ // If total number of addresses to generate is supplied, simply generate
+ // and return the list of all addresses
+ if (total != 0) {
+ for (int i = index; i < index + total; i++) {
+ allAddresses.add(IotaAPIUtils.newAddress(seed, i, checksum, customCurl));
+ }
+ return GetNewAddressResponse.create(allAddresses, stopWatch.getElapsedTimeMili());
+ }
+ // No total provided: Continue calling findTransactions to see if address was
+ // already created if null, return list of addresses
+ for (int i = index; ; i++) {
+
+ final String newAddress = IotaAPIUtils.newAddress(seed, i, checksum, customCurl);
+ final FindTransactionResponse response = findTransactionsByAddresses(newAddress);
+
+ allAddresses.add(newAddress);
+ if (response.getHashes().length == 0) {
+ break;
+ }
+ }
+
+ // If !returnAll return only the last address that was generated
+ if (!returnAll) {
+
+ //allAddresses = allAddresses.subList(allAddresses.size() - 2, allAddresses.size() - 1);
+ allAddresses = allAddresses.subList(allAddresses.size() - 1, allAddresses.size());
+ }
+ return GetNewAddressResponse.create(allAddresses, stopWatch.getElapsedTimeMili());
+ }
+
+ /**
+ * @param {string} seed
+ * @param {object} options
+ * @param {function} callback
+ * @method getTransfers
+ * @property {int} start Starting key index
+ * @property {int} end Ending key index
+ * @property {bool} inclusionStates returns confirmation status of all transactions
+ * @returns {object} success
+ **/
+ public GetTransferResponse getTransfers(String seed, Integer start, Integer end, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesExcpection {
+ StopWatch stopWatch = new StopWatch();
+ // validate & if needed pad seed
+ if ((seed = InputValidator.validateSeed(seed)) == null) {
+ throw new IllegalStateException("Invalid Seed");
+ }
+
+ start = start != null ? 0 : start;
+
+ if (start > end || end > (start + 500)) {
+ throw new ArgumentException();
+ }
+ StopWatch sw = new StopWatch();
+
+ System.out.println("GetTransfer started");
+ GetNewAddressResponse gnr = getNewAddress(seed, start, false, end == null ? end - start : end, true);
+ if (gnr != null && gnr.getAddresses() != null) {
+ System.out.println("GetTransfers after getNewAddresses " + sw.getElapsedTimeMili() + " ms");
+ Bundle[] bundles = bundlesFromAddresses(gnr.getAddresses().toArray(new String[gnr.getAddresses().size()]), inclusionStates);
+ System.out.println("GetTransfers after bundlesFromAddresses " + sw.getElapsedTimeMili() + " ms");
+ return GetTransferResponse.create(bundles, stopWatch.getElapsedTimeMili());
+ }
+ return GetTransferResponse.create(new Bundle[]{}, stopWatch.getElapsedTimeMili());
+ }
+
+ public Bundle[] bundlesFromAddresses(String[] addresses, final Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException, NoNodeInfoException, NoInclusionStatesExcpection {
+
+ List trxs = findTransactionObjects(addresses);
+ // set of tail transactions
+ List tailTransactions = new ArrayList<>();
+ List nonTailBundleHashes = new ArrayList<>();
+
+ for (Transaction trx : trxs) {
+ // Sort tail and nonTails
+ if (Long.parseLong(trx.getCurrentIndex()) == 0) {
+ tailTransactions.add(trx.getHash());
+ } else {
+ if (nonTailBundleHashes.indexOf(trx.getBundle()) == -1) {
+ nonTailBundleHashes.add(trx.getBundle());
+ }
+ }
+ }
+
+ List bundleObjects = findTransactionObjectsByBundle(nonTailBundleHashes.toArray(new String[nonTailBundleHashes.size()]));
+ for (Transaction trx : bundleObjects) {
+ // Sort tail and nonTails
+ if (Long.parseLong(trx.getCurrentIndex()) == 0) {
+ if (tailTransactions.indexOf(trx.getHash()) == -1) {
+ tailTransactions.add(trx.getHash());
+ }
+ }
+ }
+
+ final List finalBundles = new ArrayList<>();
+ final String[] tailTxArray = tailTransactions.toArray(new String[tailTransactions.size()]);
+
+ // If inclusionStates, get the confirmation status
+ // of the tail transactions, and thus the bundles
+ GetInclusionStateResponse gisr = null;
+ if (tailTxArray != null && tailTxArray.length != 0 && inclusionStates) {
+ try {
+ gisr = getLatestInclusion(tailTxArray);
+ } catch (IllegalAccessError ignored) {
+ throw new NoInclusionStatesExcpection();
+ }
+ if (gisr == null || gisr.getStates() == null || gisr.getStates().length == 0) {
+ throw new NoInclusionStatesExcpection();
+ }
+ }
+ final GetInclusionStateResponse finalInclusionStates = gisr;
+ Parallel.For(Arrays.asList(tailTxArray),
+ new Parallel.Operation() {
+ public void perform(String param) {
+
+ try {
+ GetBundleResponse bundleResponse = getBundle(param);
+ Bundle gbr = new Bundle(bundleResponse.getTransactions(), bundleResponse.getTransactions().size());
+ if (gbr != null && gbr.getTransactions() != null) {
+ if (inclusionStates) {
+ boolean thisInclusion = finalInclusionStates.getStates()[Arrays.asList(tailTxArray).indexOf(param)];
+ for (Transaction t : gbr.getTransactions()) {
+ t.setPersistence(thisInclusion);
+ }
+ }
+ finalBundles.add(gbr);
+ }
+ // If error returned from getBundle, simply ignore it because the bundle was most likely incorrect
+ } catch (InvalidBundleException | ArgumentException | InvalidSignatureException e) {
+ log.warn("GetBundleError: ", e);
+ }
+ }
+ });
+
+ Collections.sort(finalBundles);
+ Bundle[] returnValue = new Bundle[finalBundles.size()];
+ for (int i = 0; i < finalBundles.size(); i++) {
+ returnValue[i] = new Bundle(finalBundles.get(i).getTransactions(), finalBundles.get(i).getTransactions().size());
+ }
+ return returnValue;
+ }
+
+ /**
+ * @param trytes
+ * @return a StoreTransactionsResponse
+ */
+ public StoreTransactionsResponse broadcastAndStore(final String... trytes) throws BroadcastAndStoreException {
+
+ try {
+ broadcastTransactions(trytes);
+ } catch (Exception e) {
+ log.error("Impossible to broadcastAndStore, aborting.", e);
+ throw new BroadcastAndStoreException();
+ }
+ return storeTransactions(trytes);
+ }
+
+ /**
+ * Facade method: Gets transactions to approve, attaches to Tangle, broadcasts and stores
+ *
+ * @param {array} trytes
+ * @param {int} depth
+ * @param {int} minWeightMagnitude
+ * @return
+ */
+ public List sendTrytes(final String[] trytes, final int depth, final int minWeightMagnitude) {
+ final GetTransactionsToApproveResponse txs = getTransactionsToApprove(depth);
+
+ // attach to tangle - do pow
+ final GetAttachToTangleResponse res = attachToTangle(txs.getTrunkTransaction(), txs.getBranchTransaction(), minWeightMagnitude, trytes);
+
+ try {
+ broadcastAndStore(res.getTrytes());
+ } catch (BroadcastAndStoreException e) {
+ return new ArrayList<>();
+ }
+
+ final List trx = new ArrayList<>();
+
+ for (final String tryte : Arrays.asList(res.getTrytes())) {
+ trx.add(new Transaction(tryte, customCurl));
+ }
+ return trx;
+ }
+
+ /**
+ * Wrapper function for getTrytes and transactionObjects
+ * gets the trytes and transaction object from a list of transaction hashes
+ *
+ * @param {array} hashes
+ * @return
+ * @method getTransactionsObjects
+ * @returns {function} callback
+ * @returns {object} success
+ **/
+ public List getTransactionsObjects(String[] hashes) {
+
+ if (!InputValidator.isArrayOfHashes(hashes)) {
+ throw new IllegalStateException("Not an Array of Hashes: " + Arrays.toString(hashes));
+ }
+
+ final GetTrytesResponse trytesResponse = getTrytes(hashes);
+
+ final List trxs = new ArrayList<>();
+
+ for (final String tryte : trytesResponse.getTrytes()) {
+ trxs.add(new Transaction(tryte, customCurl));
+ }
+ return trxs;
+ }
+
+ /**
+ * Wrapper function for findTransactions, getTrytes and transactionObjects
+ * Returns the transactionObject of a transaction hash. The input can be a valid
+ * findTransactions input
+ *
+ * @param {object} input
+ * @method getTransactionsObjects
+ * @returns {function} callback
+ * @returns {object} success
+ **/
+ public List findTransactionObjects(String[] input) {
+ FindTransactionResponse ftr = findTransactions(input, null, null, null);
+ if (ftr == null || ftr.getHashes() == null)
+ return new ArrayList<>();
+ // get the transaction objects of the transactions
+ return getTransactionsObjects(ftr.getHashes());
+ }
+
+ /**
+ * Wrapper function for findTransactions, getTrytes and transactionObjects
+ * Returns the transactionObject of a transaction hash. The input can be a valid
+ * findTransactions input
+ *
+ * @param {object} input
+ * @method getTransactionsObjects
+ * @returns {function} callback
+ * @returns {object} success
+ **/
+ public List findTransactionObjectsByBundle(String[] input) {
+ FindTransactionResponse ftr = findTransactions(null, null, null, input);
+ if (ftr == null || ftr.getHashes() == null)
+ return new ArrayList<>();
+
+ // get the transaction objects of the transactions
+ return getTransactionsObjects(ftr.getHashes());
+ }
+
+ /**
+ * Prepares transfer by generating bundle, finding and signing inputs
+ *
+ * @param {string} seed
+ * @param {object} transfers
+ * @param {object} options
+ * @param {function} callback
+ * @return
+ * @method prepareTransfers
+ * @property {array} inputs Inputs used for signing. Needs to have correct keyIndex and address value
+ * @property {string} address Remainder address
+ * @returns {array} trytes Returns bundle trytes
+ **/
+ public List prepareTransfers(String seed, final List transfers, String remainder, List inputs) throws NotEnoughBalanceException {
+
+ // Input validation of transfers object
+ if (!InputValidator.isTransfersCollectionCorrect(transfers)) {
+ throw new IllegalStateException("Invalid Transfer");
+ }
+
+ // validate & if needed pad seed
+ if ((seed = InputValidator.validateSeed(seed)) == null) {
+ throw new IllegalStateException("Invalid Seed");
+ }
+
+
+ // Create a new bundle
+ final Bundle bundle = new Bundle();
+ final List signatureFragments = new ArrayList<>();
+
+ long totalValue = 0;
+ String tag = "";
+
+ // Iterate over all transfers, get totalValue
+ // and prepare the signatureFragments, message and tag
+ for (final Transfer transfer : transfers) {
+
+ int signatureMessageLength = 1;
+
+ // If message longer than 2187 trytes, increase signatureMessageLength (add 2nd transaction)
+ if (transfer.getMessage().length() > 2187) {
+
+ // Get total length, message / maxLength (2187 trytes)
+ signatureMessageLength += Math.floor(transfer.getMessage().length() / 2187);
+
+ String msgCopy = transfer.getMessage();
+
+ // While there is still a message, copy it
+ while (!msgCopy.isEmpty()) {
+
+ String fragment = StringUtils.substring(msgCopy, 0, 2187);
+ msgCopy = StringUtils.substring(msgCopy, 2187, msgCopy.length());
+
+ // Pad remainder of fragment
+
+ fragment = StringUtils.rightPad(fragment, 2187, '9');
+
+ signatureFragments.add(fragment);
+ }
+ } else {
+ // Else, get single fragment with 2187 of 9's trytes
+ String fragment = StringUtils.substring(transfer.getMessage(), 0, 2187);
+
+ fragment = StringUtils.rightPad(fragment, 2187, '9');
+
+ signatureFragments.add(fragment);
+ }
+
+ // get current timestamp in seconds
+ long timestamp = (long) Math.floor(Calendar.getInstance().getTimeInMillis() / 1000);
+
+ // If no tag defined, get 27 tryte tag.
+ tag = transfer.getTag().isEmpty() ? "999999999999999999999999999" : transfer.getTag();
+
+ // Pad for required 27 tryte length
+ tag = StringUtils.rightPad(tag, 27, '9');
+
+ // Add first entry to the bundle
+ bundle.addEntry(signatureMessageLength, transfer.getAddress(), transfer.getValue(), tag, timestamp);
+ // Sum up total value
+ totalValue += transfer.getValue();
+ }
+
+ // Get inputs if we are sending tokens
+ if (totalValue != 0) {
+
+ // Case 1: user provided inputs
+ // Validate the inputs by calling getBalances
+ if (inputs != null && !inputs.isEmpty()) {
+
+ // Get list if addresses of the provided inputs
+ List inputsAddresses = new ArrayList<>();
+ for (final Input i : inputs) {
+ inputsAddresses.add(i.getAddress());
+ }
+
+ GetBalancesResponse balancesResponse = getBalances(100, inputsAddresses);
+ String[] balances = balancesResponse.getBalances();
+
+ List confirmedInputs = new ArrayList<>();
+ int totalBalance = 0;
+ int i = 0;
+ for (String balance : balances) {
+ long thisBalance = Integer.parseInt(balance);
+ totalBalance += thisBalance;
+
+ // If input has balance, add it to confirmedInputs
+ if (thisBalance > 0) {
+ Input inputEl = inputs.get(i++);
+ inputEl.setBalance(thisBalance);
+ confirmedInputs.add(inputEl);
+ }
+ }
+
+ // Return not enough balance error
+ if (totalValue > totalBalance) {
+ throw new IllegalStateException("Not enough balance");
+ }
+
+ return addRemainder(seed, confirmedInputs, bundle, tag, totalValue, null, signatureFragments);
+ }
+
+ // Case 2: Get inputs deterministically
+ //
+ // If no inputs provided, derive the addresses from the seed and
+ // confirm that the inputs exceed the threshold
+ else {
+
+ @SuppressWarnings("unchecked") GetBalancesAndFormatResponse newinputs = getInputs(seed, 0, 0, totalValue);
+ // If inputs with enough balance
+ return addRemainder(seed, newinputs.getInput(), bundle, tag, totalValue, null, signatureFragments);
+ }
+ } else {
+
+ // If no input required, don't sign and simply finalize the bundle
+ bundle.finalize(customCurl);
+ bundle.addTrytes(signatureFragments);
+
+ List trxb = bundle.getTransactions();
+ List bundleTrytes = new ArrayList<>();
+
+ for (Transaction trx : trxb) {
+ bundleTrytes.add(trx.toTrytes());
+ }
+ Collections.reverse(bundleTrytes);
+ return bundleTrytes;
+ }
+ }
+
+ /**
+ * Gets the inputs of a seed
+ *
+ * @param {string} seed
+ * @param {object} options
+ * @param {function} callback
+ * @method getInputs
+ * @property {int} start Starting key index
+ * @property {int} end Ending key index
+ * @property {int} threshold Min balance required
+ **/
+ public GetBalancesAndFormatResponse getInputs(String seed, int start, int end, long threshold) {
+ StopWatch stopWatch = new StopWatch();
+ // validate the seed
+ if (!InputValidator.isTrytes(seed, 0)) {
+ throw new IllegalStateException("Invalid Seed");
+ }
+
+ // validate & if needed pad seed
+ if ((seed = InputValidator.validateSeed(seed)) == null) {
+ throw new IllegalStateException("Invalid Seed");
+ }
+
+ // If start value bigger than end, return error
+ // or if difference between end and start is bigger than 500 keys
+ if (start > end || end > (start + 500)) {
+ throw new IllegalStateException("Invalid inputs provided");
+ }
+
+ // Case 1: start and end
+ //
+ // If start and end is defined by the user, simply iterate through the keys
+ // and call getBalances
+ if (end != 0) {
+
+ List allAddresses = new ArrayList<>();
+
+ for (int i = start; i < end; i++) {
+
+ String address = IotaAPIUtils.newAddress(seed, i, false, customCurl);
+ allAddresses.add(address);
+ }
+
+ return getBalanceAndFormat(allAddresses, threshold, start, end, stopWatch);
+ }
+ // Case 2: iterate till threshold || end
+ //
+ // Either start from index: 0 or start (if defined) until threshold is reached.
+ // Calls getNewAddress and deterministically generates and returns all addresses
+ // We then do getBalance, format the output and return it
+ else {
+ final GetNewAddressResponse res = getNewAddress(seed, start, false, 0, true);
+ return getBalanceAndFormat(res.getAddresses(), threshold, start, end, stopWatch);
+ }
+ }
+
+ // Calls getBalances and formats the output
+ // returns the final inputsObject then
+ public GetBalancesAndFormatResponse getBalanceAndFormat(final List addresses, long threshold, int start, int end, StopWatch stopWatch) {
+
+ GetBalancesResponse getBalancesResponse = getBalances(100, addresses);
+ List balances = Arrays.asList(getBalancesResponse.getBalances());
+
+ // If threshold defined, keep track of whether reached or not
+ // else set default to true
+ boolean thresholdReached = threshold == 0;
+ int i = -1;
+
+ List inputs = new ArrayList<>();
+ long totalBalance = 0;
+
+ for (String address : addresses) {
+
+ long balance = Long.parseLong(balances.get(++i));
+
+ if (balance > 0) {
+ final Input newEntry = new Input(address, balance, start + i);
+
+ inputs.add(newEntry);
+ // Increase totalBalance of all aggregated inputs
+ totalBalance += balance;
+
+ if (!thresholdReached && totalBalance >= threshold) {
+ thresholdReached = true;
+ break;
+ }
+ }
+ }
+
+ if (thresholdReached) {
+ return GetBalancesAndFormatResponse.create(inputs, totalBalance, stopWatch.getElapsedTimeMili());
+ }
+ throw new IllegalStateException("Not enough balance");
+ }
+
+ /**
+ * Gets the associated bundle transactions of a single transaction
+ * Does validation of signatures, total sum as well as bundle order
+ *
+ * @param {string} transaction Hash of a tail transaction
+ * @method getBundle
+ * @returns {list} bundle Transaction objects
+ **/
+ public GetBundleResponse getBundle(String transaction) throws ArgumentException, InvalidBundleException, InvalidSignatureException {
+ StopWatch stopWatch = new StopWatch();
+
+ Bundle bundle = traverseBundle(transaction, null, new Bundle());
+ if (bundle == null) {
+ throw new ArgumentException("Unknown Bundle");
+ }
+
+ long totalSum = 0;
+ int lastIndex = 0;
+ String bundleHash = bundle.getTransactions().get(0).getBundle();
+
+ ICurl curl = new JCurl();
+ curl.reset();
+
+ List signaturesToValidate = new ArrayList<>();
+
+ for (int i = 0; i < bundle.getTransactions().size(); i++) {
+ Transaction trx = bundle.getTransactions().get(i);
+ Long bundleValue = Long.parseLong(trx.getValue());
+ totalSum += bundleValue;
+
+ if (i != Integer.parseInt(bundle.getTransactions().get(i).getCurrentIndex())) {
+ throw new ArgumentException("Invalid Bundle");
+ }
+
+ String trxTrytes = trx.toTrytes().substring(2187, 2187 + 162);
+ //System.out.println("Bundlesize "+bundle.getTransactions().size()+" "+trxTrytes);
+ // Absorb bundle hash + value + timestamp + lastIndex + currentIndex trytes.
+ curl.absorb(Converter.trits(trxTrytes));
+ // Check if input transaction
+ if (bundleValue < 0) {
+ String address = trx.getAddress();
+ Signature sig = new Signature();
+ sig.setAddress(address);
+ sig.getSignatureFragments().add(trx.getSignatureFragments());
+
+ // Find the subsequent txs with the remaining signature fragment
+ for (int y = i; y < bundle.getTransactions().size() - 1; y++) {
+ Transaction newBundleTx = bundle.getTransactions().get(i + 1);
+
+ // Check if new tx is part of the signature fragment
+ if (newBundleTx.getAddress().equals(address) && Long.parseLong(newBundleTx.getValue()) == 0) {
+ if (sig.getSignatureFragments().indexOf(newBundleTx.getSignatureFragments()) == -1)
+ sig.getSignatureFragments().add(newBundleTx.getSignatureFragments());
+ }
+ }
+ signaturesToValidate.add(sig);
+ }
+ }
+
+ // Check for total sum, if not equal 0 return error
+ if (totalSum != 0) throw new InvalidBundleException("Invalid Bundle Sum");
+ int[] bundleFromTrxs = new int[243];
+ curl.squeeze(bundleFromTrxs);
+ String bundleFromTxString = Converter.trytes(bundleFromTrxs);
+
+ // Check if bundle hash is the same as returned by tx object
+ if (!bundleFromTxString.equals(bundleHash)) throw new InvalidBundleException("Invalid Bundle Hash");
+ // Last tx in the bundle should have currentIndex === lastIndex
+ bundle.setLength(bundle.getTransactions().size());
+ if (!bundle.getTransactions().get(bundle.getLength() - 1).getCurrentIndex().equals(bundle.getTransactions().get(bundle.getLength() - 1).getLastIndex()))
+ throw new InvalidBundleException("Invalid Bundle");
+
+ // Validate the signatures
+ for (Signature aSignaturesToValidate : signaturesToValidate) {
+ String[] signatureFragments = aSignaturesToValidate.getSignatureFragments().toArray(new String[aSignaturesToValidate.getSignatureFragments().size()]);
+ String address = aSignaturesToValidate.getAddress();
+ boolean isValidSignature = new Signing().validateSignatures(address, signatureFragments, bundleHash);
+
+ if (!isValidSignature) throw new InvalidSignatureException();
+ }
+
+ return GetBundleResponse.create(bundle.getTransactions(), stopWatch.getElapsedTimeMili());
+ }
+
+ /**
+ * Replays a transfer by doing Proof of Work again
+ *
+ * @param {string} tail
+ * @param {int} depth
+ * @param {int} minWeightMagnitude
+ * @param {function} callback
+ * @method replayBundle
+ * @returns {object} analyzed Transaction objects
+ **/
+ public ReplayBundleResponse replayBundle(String transaction, int depth, int minWeightMagnitude) throws InvalidBundleException, ArgumentException, InvalidSignatureException {
+ StopWatch stopWatch = new StopWatch();
+
+ List bundleTrytes = new ArrayList<>();
+
+ GetBundleResponse bundleResponse = getBundle(transaction);
+ Bundle bundle = new Bundle(bundleResponse.getTransactions(), bundleResponse.getTransactions().size());
+ for (Transaction trx : bundle.getTransactions()) {
+
+ bundleTrytes.add(trx.toTrytes());
+ }
+
+ List trxs = sendTrytes(bundleTrytes.toArray(new String[bundleTrytes.size()]), depth, minWeightMagnitude);
+
+ Boolean[] successful = new Boolean[trxs.size()];
+
+ for (int i = 0; i < trxs.size(); i++) {
+
+ final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
+
+
+ successful[i] = response.getHashes().length != 0;
+ }
+
+ return ReplayBundleResponse.create(successful, stopWatch.getElapsedTimeMili());
+ }
+
+ /**
+ * Wrapper function for getNodeInfo and getInclusionStates
+ *
+ * @param {array} hashes
+ * @method getLatestInclusion
+ * @returns {function} callback
+ * @returns {array} state
+ **/
+ public GetInclusionStateResponse getLatestInclusion(String[] hashes) throws NoNodeInfoException {
+ GetNodeInfoResponse getNodeInfoResponse = getNodeInfo();
+ if (getNodeInfoResponse == null) throw new NoNodeInfoException();
+
+ String[] latestMilestone = {getNodeInfoResponse.getLatestSolidSubtangleMilestone()};
+
+ return getInclusionStates(hashes, latestMilestone);
+ }
+
+ public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMagnitude, final List transfers, Input[] inputs, String address) throws NotEnoughBalanceException {
+ StopWatch stopWatch = new StopWatch();
+
+ List trytes = prepareTransfers(seed, transfers, address, inputs == null ? null : Arrays.asList(inputs));
+ List trxs = sendTrytes(trytes.toArray(new String[trytes.size()]), depth, minWeightMagnitude);
+
+ Boolean[] successful = new Boolean[trxs.size()];
+
+ for (int i = 0; i < trxs.size(); i++) {
+
+ final FindTransactionResponse response = findTransactionsByBundles(trxs.get(i).getBundle());
+
+ successful[i] = response.getHashes().length != 0;
+ }
+
+ return SendTransferResponse.create(successful, stopWatch.getElapsedTimeMili());
+ }
+
+ /**
+ * Basically traverse the Bundle by going down the trunkTransactions until
+ * the bundle hash of the transaction is no longer the same. In case the input
+ * transaction hash is not a tail, we return an error.
+ *
+ * @param {string} trunkTx Hash of a trunk or a tail transaction of a bundle
+ * @param {string} bundleHash
+ * @param {array} bundle List of bundles to be populated
+ * @method traverseBundle
+ * @returns {array} bundle Transaction objects
+ **/
+ public Bundle traverseBundle(String trunkTx, String bundleHash, Bundle bundle) throws ArgumentException {
+ GetTrytesResponse gtr = getTrytes(trunkTx);
+
+ if (gtr != null) {
+
+ if (gtr.getTrytes().length == 0) {
+ throw new ArgumentException("Bundle transactions not visible");
+ }
+
+ Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl);
+ if (trx == null || trx.getBundle() == null) {
+ throw new ArgumentException("Invalid trytes, could not create object");
+ }
+ // If first transaction to search is not a tail, return error
+ if (bundleHash == null && Integer.parseInt(trx.getCurrentIndex()) != 0) {
+ throw new ArgumentException("Invalid tail transaction supplied.");
+ }
+ // If no bundle hash, define it
+ if (bundleHash == null) {
+ bundleHash = trx.getBundle();
+ }
+ // If different bundle hash, return with bundle
+ if (!bundleHash.equals(trx.getBundle())) {
+ return bundle;
+ }
+ // If only one bundle element, return
+ if (Integer.parseInt(trx.getLastIndex()) == 0 && Integer.parseInt(trx.getCurrentIndex()) == 0) {
+ return new Bundle(Collections.singletonList(trx), 1);
+ }
+ // Define new trunkTransaction for search
+ trunkTx = trx.getTrunkTransaction();
+ // Add transaction object to bundle
+ bundle.getTransactions().add(trx);
+
+ // Continue traversing with new trunkTx
+ return traverseBundle(trunkTx, bundleHash, bundle);
+ } else {
+ throw new ArgumentException("Get Trytes Response was null");
+ }
+ }
+
+ public String findTailTransactionHash(String hash) throws ArgumentException {
+ GetTrytesResponse gtr = getTrytes(hash);
+
+ if (gtr == null) throw new ArgumentException("Invalid hash");
+
+ if (gtr.getTrytes().length == 0) {
+ throw new ArgumentException("Bundle transactions not visible");
+ }
+
+ Transaction trx = new Transaction(gtr.getTrytes()[0], customCurl);
+ if (trx == null || trx.getBundle() == null) {
+ throw new ArgumentException("Invalid trytes, could not create object");
+ }
+ if (Integer.parseInt(trx.getCurrentIndex()) == 0) return trx.getHash();
+ else return findTailTransactionHash(trx.getBundle());
+ }
+
+ public List addRemainder(final String seed,
+ final List inputs,
+ final Bundle bundle,
+ final String tag,
+ final long totalValue,
+ final String remainderAddress,
+ final List signatureFragments) throws NotEnoughBalanceException {
+
+ long totalTransferValue = totalValue;
+ for (int i = 0; i < inputs.size(); i++) {
+ long thisBalance = inputs.get(i).getBalance();
+ long toSubtract = 0 - thisBalance;
+ long timestamp = (long) Math.floor(Calendar.getInstance().getTimeInMillis() / 1000);
+
+ // Add input as bundle entry
+ bundle.addEntry(2, inputs.get(i).getAddress(), toSubtract, tag, timestamp);
+ // If there is a remainder value
+ // Add extra output to send remaining funds to
+
+ if (thisBalance >= totalTransferValue) {
+ long remainder = thisBalance - totalTransferValue;
+
+ // If user has provided remainder address
+ // Use it to send remaining funds to
+ if (remainder > 0 && remainderAddress != null) {
+ // Remainder bundle entry
+ bundle.addEntry(1, remainderAddress, remainder, tag, timestamp);
+ // Final function for signing inputs
+ return IotaAPIUtils.signInputsAndReturn(seed, inputs, bundle, signatureFragments, customCurl);
+ } else if (remainder > 0) {
+ // Generate a new Address by calling getNewAddress
+
+ GetNewAddressResponse res = getNewAddress(seed, 0, false, 0, false);
+ // Remainder bundle entry
+ bundle.addEntry(1, res.getAddresses().get(0), remainder, tag, timestamp);
+
+ // Final function for signing inputs
+ return IotaAPIUtils.signInputsAndReturn(seed, inputs, bundle, signatureFragments, customCurl);
+ } else {
+ // If there is no remainder, do not add transaction to bundle
+ // simply sign and return
+ return IotaAPIUtils.signInputsAndReturn(seed, inputs, bundle, signatureFragments, customCurl);
+ }
+
+ // If multiple inputs provided, subtract the totalTransferValue by
+ // the inputs balance
+ } else {
+ totalTransferValue -= thisBalance;
+ }
+ }
+ throw new NotEnoughBalanceException();
+ }
+
+ public static class Builder extends IotaAPICore.Builder {
+ private ICurl customCurl;
+
+ public Builder withCustomCurl(ICurl curl) {
+ customCurl = curl;
+ return this;
+ }
+
+ public IotaAPI build() {
+ super.build();
+ return new IotaAPI(this);
+ }
+ }
+}
diff --git a/src/main/java/jota/IotaAPIProxy.java b/src/main/java/jota/IotaAPICore.java
similarity index 84%
rename from src/main/java/jota/IotaAPIProxy.java
rename to src/main/java/jota/IotaAPICore.java
index b742299..1004eb6 100644
--- a/src/main/java/jota/IotaAPIProxy.java
+++ b/src/main/java/jota/IotaAPICore.java
@@ -2,7 +2,6 @@ package jota;
import jota.dto.request.*;
import jota.dto.response.*;
-import jota.utils.IotaAPIUtils;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,31 +13,21 @@ import retrofit2.converter.gson.GsonConverterFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
-import java.util.Collection;
+import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
/**
- * IotaAPIProxy Builder. Usage:
- *
- * IotaApiProxy api = IotaApiProxy.Builder
- * .protocol("http")
- * .nodeAddress("localhost")
- * .port(12345)
- * .build();
- *
- * GetNodeInfoResponse response = api.getNodeInfo();
- *
- * @author davassi
+ * Created by Adrian on 15.01.2017.
*/
-public class IotaAPIProxy {
+public class IotaAPICore {
- private static final Logger log = LoggerFactory.getLogger(IotaAPIProxy.class);
+ private static final Logger log = LoggerFactory.getLogger(IotaAPICore.class);
private IotaAPIService service;
private String protocol, host, port;
- private IotaAPIProxy(final Builder builder) {
+ protected IotaAPICore(final Builder builder) {
protocol = builder.protocol;
host = builder.host;
port = builder.port;
@@ -49,7 +38,11 @@ public class IotaAPIProxy {
try {
final Response res = call.execute();
if (res.code() == 400) {
- throw new IllegalAccessError(res.errorBody().toString());
+ throw new IllegalAccessError("400 " + res.errorBody().string());
+ } else if (res.code() == 401) {
+ throw new IllegalAccessError("401 " + res.errorBody().string());
+ } else if (res.code() == 500) {
+ throw new IllegalAccessError("500 " + res.errorBody().string());
}
return res;
} catch (IOException e) {
@@ -58,7 +51,7 @@ public class IotaAPIProxy {
}
}
- private static final String env(String env, String def) {
+ private static String env(String env, String def) {
final String value = System.getenv(env);
if (value == null) {
log.warn("Environment variable '{}' is not defined, and actual value has not been specified. "
@@ -73,8 +66,8 @@ public class IotaAPIProxy {
final String nodeUrl = protocol + "://" + host + ":" + port;
final OkHttpClient client = new OkHttpClient.Builder()
- .readTimeout(120, TimeUnit.SECONDS)
- .connectTimeout(120, TimeUnit.SECONDS)
+ .readTimeout(5000, TimeUnit.SECONDS)
+ .connectTimeout(5000, TimeUnit.SECONDS)
.build();
final Retrofit retrofit = new Retrofit.Builder()
@@ -148,12 +141,6 @@ public class IotaAPIProxy {
return wrapCheckedException(res).body();
}
- public GetInclusionStateResponse getInclusionStates(Collection transactions, Collection tips) {
- final Call res = service.getInclusionStates(IotaGetInclusionStateRequest
- .createGetInclusionStateRequest(transactions, tips));
- return wrapCheckedException(res).body();
- }
-
public GetTrytesResponse getTrytes(String... hashes) {
final Call res = service.getTrytes(IotaGetTrytesRequest.createGetTrytesRequest(hashes));
return wrapCheckedException(res).body();
@@ -169,6 +156,10 @@ public class IotaAPIProxy {
return wrapCheckedException(res).body();
}
+ public GetBalancesResponse getBalances(Integer threshold, List addresses) {
+ return getBalances(threshold, addresses.toArray(new String[]{}));
+ }
+
public InterruptAttachingToTangleResponse interruptAttachingToTangle() {
final Call res = service.interruptAttachingToTangle(IotaCommandRequest.createInterruptAttachToTangleRequest());
return wrapCheckedException(res).body();
@@ -184,26 +175,17 @@ public class IotaAPIProxy {
return wrapCheckedException(res).body();
}
- // end of proxied calls.
-
public BroadcastTransactionsResponse broadcastTransactions(String... trytes) {
final Call res = service.broadcastTransactions(IotaBroadcastTransactionRequest.createBroadcastTransactionsRequest(trytes));
return wrapCheckedException(res).body();
}
- public GetBundleResponse getBundle(String transaction) {
- return IotaAPIUtils.getBundle(transaction);
- }
-
- public GetNewAddressResponse getNewAddress(String seed, Integer securityLevel) {
- return IotaAPIUtils.getNewAddress(seed, securityLevel);
- }
-
- public static class Builder {
+ @SuppressWarnings("unchecked")
+ public static class Builder > {
String protocol, host, port;
- public IotaAPIProxy build() {
+ public IotaAPICore build() {
if (protocol == null || host == null || port == null) {
@@ -216,7 +198,7 @@ public class IotaAPIProxy {
}
}
- return new IotaAPIProxy(this);
+ return new IotaAPICore(this);
}
private boolean checkPropertiesFiles() {
@@ -253,19 +235,19 @@ public class IotaAPIProxy {
port = env("IOTA_NODE_PORT", "14265");
}
- public Builder host(String host) {
+ public T host(String host) {
this.host = host;
- return this;
+ return (T) this;
}
- public Builder port(String port) {
+ public T port(String port) {
this.port = port;
- return this;
+ return (T) this;
}
- public Builder protocol(String protocol) {
+ public T protocol(String protocol) {
this.protocol = protocol;
- return this;
+ return (T) this;
}
}
diff --git a/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java b/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java
index bfef85b..8d50a54 100644
--- a/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java
+++ b/src/main/java/jota/dto/request/IotaAttachToTangleRequest.java
@@ -20,4 +20,36 @@ public class IotaAttachToTangleRequest extends IotaCommandRequest {
public static IotaAttachToTangleRequest createAttachToTangleRequest(final String trunkTransaction, final String branchTransaction, final Integer minWeightMagnitude, final String... trytes) {
return new IotaAttachToTangleRequest(trunkTransaction, branchTransaction, minWeightMagnitude, trytes);
}
+
+ public String getTrunkTransaction() {
+ return trunkTransaction;
+ }
+
+ public void setTrunkTransaction(String trunkTransaction) {
+ this.trunkTransaction = trunkTransaction;
+ }
+
+ public String getBranchTransaction() {
+ return branchTransaction;
+ }
+
+ public void setBranchTransaction(String branchTransaction) {
+ this.branchTransaction = branchTransaction;
+ }
+
+ public Integer getMinWeightMagnitude() {
+ return minWeightMagnitude;
+ }
+
+ public void setMinWeightMagnitude(Integer minWeightMagnitude) {
+ this.minWeightMagnitude = minWeightMagnitude;
+ }
+
+ public String[] getTrytes() {
+ return trytes;
+ }
+
+ public void setTrytes(String[] trytes) {
+ this.trytes = trytes;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java b/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java
index 42847d9..5a0a5d5 100644
--- a/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java
+++ b/src/main/java/jota/dto/request/IotaBroadcastTransactionRequest.java
@@ -14,4 +14,12 @@ public class IotaBroadcastTransactionRequest extends IotaCommandRequest {
public static IotaBroadcastTransactionRequest createBroadcastTransactionsRequest(final String... trytes) {
return new IotaBroadcastTransactionRequest(trytes);
}
+
+ public String[] getTrytes() {
+ return trytes;
+ }
+
+ public void setTrytes(String[] trytes) {
+ this.trytes = trytes;
+ }
}
\ No newline at end of file
diff --git a/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java b/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java
index b3a72df..71e70fe 100644
--- a/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java
+++ b/src/main/java/jota/dto/request/IotaFindTransactionsRequest.java
@@ -4,6 +4,38 @@ import jota.IotaAPICommands;
public class IotaFindTransactionsRequest extends IotaCommandRequest {
+ public String[] getBundles() {
+ return bundles;
+ }
+
+ public void setBundles(String[] bundles) {
+ this.bundles = bundles;
+ }
+
+ public String[] getAddresses() {
+ return addresses;
+ }
+
+ public void setAddresses(String[] addresses) {
+ this.addresses = addresses;
+ }
+
+ public String[] getTags() {
+ return tags;
+ }
+
+ public void setTags(String[] tags) {
+ this.tags = tags;
+ }
+
+ public String[] getApprovees() {
+ return approvees;
+ }
+
+ public void setApprovees(String[] approvees) {
+ this.approvees = approvees;
+ }
+
private String[] bundles; // List of bundle hashes. The hashes need to be extended to 81chars by padding the hash with 9's.
private String[] addresses;
private String[] tags;
diff --git a/src/main/java/jota/dto/request/IotaGetBalancesRequest.java b/src/main/java/jota/dto/request/IotaGetBalancesRequest.java
index 807865d..33d760d 100644
--- a/src/main/java/jota/dto/request/IotaGetBalancesRequest.java
+++ b/src/main/java/jota/dto/request/IotaGetBalancesRequest.java
@@ -17,5 +17,20 @@ public class IotaGetBalancesRequest extends IotaCommandRequest {
return new IotaGetBalancesRequest(threshold, addresses);
}
+ public String[] getAddresses() {
+ return addresses;
+ }
+
+ public void setAddresses(String[] addresses) {
+ this.addresses = addresses;
+ }
+
+ public Integer getThreshold() {
+ return threshold;
+ }
+
+ public void setThreshold(Integer threshold) {
+ this.threshold = threshold;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaGetInclusionStateRequest.java b/src/main/java/jota/dto/request/IotaGetInclusionStateRequest.java
index d0d323c..704806e 100644
--- a/src/main/java/jota/dto/request/IotaGetInclusionStateRequest.java
+++ b/src/main/java/jota/dto/request/IotaGetInclusionStateRequest.java
@@ -25,4 +25,19 @@ public class IotaGetInclusionStateRequest extends IotaCommandRequest {
tips.toArray(new String[]{}));
}
+ public String[] getTransactions() {
+ return transactions;
+ }
+
+ public void setTransactions(String[] transactions) {
+ this.transactions = transactions;
+ }
+
+ public String[] getTips() {
+ return tips;
+ }
+
+ public void setTips(String[] tips) {
+ this.tips = tips;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaGetTransactionsToApproveRequest.java b/src/main/java/jota/dto/request/IotaGetTransactionsToApproveRequest.java
index f597448..fb1b367 100644
--- a/src/main/java/jota/dto/request/IotaGetTransactionsToApproveRequest.java
+++ b/src/main/java/jota/dto/request/IotaGetTransactionsToApproveRequest.java
@@ -14,4 +14,12 @@ public class IotaGetTransactionsToApproveRequest extends IotaCommandRequest {
public static IotaGetTransactionsToApproveRequest createIotaGetTransactionsToApproveRequest(Integer depth) {
return new IotaGetTransactionsToApproveRequest(depth);
}
+
+ public Integer getDepth() {
+ return depth;
+ }
+
+ public void setDepth(Integer depth) {
+ this.depth = depth;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaGetTrytesRequest.java b/src/main/java/jota/dto/request/IotaGetTrytesRequest.java
index b493451..eaa5888 100644
--- a/src/main/java/jota/dto/request/IotaGetTrytesRequest.java
+++ b/src/main/java/jota/dto/request/IotaGetTrytesRequest.java
@@ -14,4 +14,12 @@ public class IotaGetTrytesRequest extends IotaCommandRequest {
public static IotaGetTrytesRequest createGetTrytesRequest(String... hashes) {
return new IotaGetTrytesRequest(hashes);
}
+
+ public String[] getHashes() {
+ return hashes;
+ }
+
+ public void setHashes(String[] hashes) {
+ this.hashes = hashes;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaNeighborsRequest.java b/src/main/java/jota/dto/request/IotaNeighborsRequest.java
index 82c96b1..c81c491 100644
--- a/src/main/java/jota/dto/request/IotaNeighborsRequest.java
+++ b/src/main/java/jota/dto/request/IotaNeighborsRequest.java
@@ -18,5 +18,13 @@ public class IotaNeighborsRequest extends IotaCommandRequest {
public static IotaNeighborsRequest createRemoveNeighborsRequest(String... uris) {
return new IotaNeighborsRequest(IotaAPICommands.REMOVE_NEIGHBORS, uris);
}
+
+ public String[] getUris() {
+ return uris;
+ }
+
+ public void setUris(String[] uris) {
+ this.uris = uris;
+ }
}
diff --git a/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java b/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java
index 5247894..1b9c55b 100644
--- a/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java
+++ b/src/main/java/jota/dto/request/IotaStoreTransactionsRequest.java
@@ -14,4 +14,12 @@ public class IotaStoreTransactionsRequest extends IotaCommandRequest {
public static IotaStoreTransactionsRequest createStoreTransactionsRequest(final String... trytes) {
return new IotaStoreTransactionsRequest(trytes);
}
+
+ public String[] getTrytes() {
+ return trytes;
+ }
+
+ public void setTrytes(String[] trytes) {
+ this.trytes = trytes;
+ }
}
diff --git a/src/main/java/jota/dto/response/AbstractResponse.java b/src/main/java/jota/dto/response/AbstractResponse.java
index cab9488..547bf65 100644
--- a/src/main/java/jota/dto/response/AbstractResponse.java
+++ b/src/main/java/jota/dto/response/AbstractResponse.java
@@ -7,9 +7,9 @@ import org.apache.commons.lang3.builder.ToStringStyle;
public abstract class AbstractResponse {
- private Integer duration;
+ private Long duration;
- public Integer getDuration() {
+ public Long getDuration() {
return duration;
}
@@ -27,4 +27,8 @@ public abstract class AbstractResponse {
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj, false);
}
+
+ public void setDuration(Long duration) {
+ this.duration = duration;
+ }
}
diff --git a/src/main/java/jota/dto/response/AnalyzeTransactionResponse.java b/src/main/java/jota/dto/response/AnalyzeTransactionResponse.java
index fdb7059..08c56cf 100644
--- a/src/main/java/jota/dto/response/AnalyzeTransactionResponse.java
+++ b/src/main/java/jota/dto/response/AnalyzeTransactionResponse.java
@@ -1,86 +1,15 @@
package jota.dto.response;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
+import jota.model.Transaction;
+
+import java.util.ArrayList;
+import java.util.List;
public class AnalyzeTransactionResponse extends AbstractResponse {
- private Transactions[] transactions;
+ private List transactions = new ArrayList<>();
- public Transactions[] getTransactions() {
+ public List getTransactions() {
return transactions;
}
-
- static class Transactions {
- private String signatureMessageChunk;
- private String index;
- private String approvalNonce;
- private String hash;
- private String digest;
- private String type;
- private String timestamp;
- private String trunkTransaction;
- private String branchTransaction;
- private String signatureNonce;
- private String address;
- private String value;
- private String bundle;
-
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
- }
-
- public String getValue() {
- return value;
- }
-
- public String getDigest() {
- return digest;
- }
-
- public String getTrunkTransaction() {
- return trunkTransaction;
- }
-
- public String getTimestamp() {
- return timestamp;
- }
-
- public String getSignatureNonce() {
- return signatureNonce;
- }
-
- public String getType() {
- return type;
- }
-
- public String getAddress() {
- return address;
- }
-
- public String getApprovalNonce() {
- return approvalNonce;
- }
-
- public String getBranchTransaction() {
- return branchTransaction;
- }
-
- public String getBundle() {
- return bundle;
- }
-
- public String getHash() {
- return hash;
- }
-
- public String getIndex() {
- return index;
- }
-
- public String getSignatureMessageChunk() {
- return signatureMessageChunk;
- }
- }
}
diff --git a/src/main/java/jota/dto/response/GetAttachToTangleResponse.java b/src/main/java/jota/dto/response/GetAttachToTangleResponse.java
index 53eb98b..8b70851 100644
--- a/src/main/java/jota/dto/response/GetAttachToTangleResponse.java
+++ b/src/main/java/jota/dto/response/GetAttachToTangleResponse.java
@@ -7,4 +7,8 @@ public class GetAttachToTangleResponse extends AbstractResponse {
public String[] getTrytes() {
return trytes;
}
+
+ public GetAttachToTangleResponse(long duration) {
+ setDuration(duration);
+ }
}
diff --git a/src/main/java/jota/dto/response/GetBalancesAndFormatResponse.java b/src/main/java/jota/dto/response/GetBalancesAndFormatResponse.java
new file mode 100644
index 0000000..189e72e
--- /dev/null
+++ b/src/main/java/jota/dto/response/GetBalancesAndFormatResponse.java
@@ -0,0 +1,35 @@
+package jota.dto.response;
+
+import java.util.List;
+
+import jota.model.Input;
+
+public class GetBalancesAndFormatResponse extends AbstractResponse {
+
+ private List input;
+ private long totalBalance;
+
+ public List getInput() {
+ return input;
+ }
+
+ public void setInput(List input) {
+ this.input = input;
+ }
+
+ public long getTotalBalance() {
+ return totalBalance;
+ }
+
+ public void setTotalBalance(long totalBalance) {
+ this.totalBalance = totalBalance;
+ }
+
+ public static GetBalancesAndFormatResponse create(List inputs, long totalBalance2, long duration) {
+ GetBalancesAndFormatResponse res = new GetBalancesAndFormatResponse();
+ res.setInput(inputs);
+ res.setTotalBalance(totalBalance2);
+ res.setDuration(duration);
+ return res;
+ }
+}
diff --git a/src/main/java/jota/dto/response/GetBundleResponse.java b/src/main/java/jota/dto/response/GetBundleResponse.java
index c508f78..c3dfa1b 100644
--- a/src/main/java/jota/dto/response/GetBundleResponse.java
+++ b/src/main/java/jota/dto/response/GetBundleResponse.java
@@ -1,93 +1,23 @@
package jota.dto.response;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
+import jota.model.Transaction;
+
+import java.util.ArrayList;
+import java.util.List;
public class GetBundleResponse extends AbstractResponse {
- public Transactions[] transactions;
+ private List transactions = new ArrayList<>();
- private String warning;
-
- public String getWarning() {
- return warning;
+ public static GetBundleResponse create(List transactions, long duration) {
+ GetBundleResponse res = new GetBundleResponse();
+ res.transactions = transactions;
+ res.setDuration(duration);
+ return res;
}
- public Transactions[] getTransactions() {
+
+ public List getTransactions() {
return transactions;
}
-
- public static class Transactions {
- private String signatureMessageChunk;
- private String index;
- private String approvalNonce;
- private String hash;
- private String digest;
- private String type;
- private String timestamp;
- private String trunkTransaction;
- private String branchTransaction;
- private String signatureNonce;
- private String address;
- private String value;
- private String bundle;
-
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
- }
-
- public String getAddress() {
- return address;
- }
-
- public String getApprovalNonce() {
- return approvalNonce;
- }
-
- public String getBranchTransaction() {
- return branchTransaction;
- }
-
- public String getBundle() {
- return bundle;
- }
-
- public String getDigest() {
- return digest;
- }
-
- public String getHash() {
- return hash;
- }
-
- public String getIndex() {
- return index;
- }
-
- public String getSignatureMessageChunk() {
- return signatureMessageChunk;
- }
-
- public String getSignatureNonce() {
- return signatureNonce;
- }
-
- public String getTimestamp() {
- return timestamp;
- }
-
- public String getType() {
- return type;
- }
-
- public String getTrunkTransaction() {
- return trunkTransaction;
- }
-
- public String getValue() {
- return value;
- }
-
- }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/jota/dto/response/GetNeighborsResponse.java b/src/main/java/jota/dto/response/GetNeighborsResponse.java
index 550a546..3740307 100644
--- a/src/main/java/jota/dto/response/GetNeighborsResponse.java
+++ b/src/main/java/jota/dto/response/GetNeighborsResponse.java
@@ -1,34 +1,15 @@
package jota.dto.response;
+import jota.model.Neighbor;
+
+import java.util.ArrayList;
+import java.util.List;
+
public class GetNeighborsResponse extends AbstractResponse {
- public Neighbors[] neighbors;
+ private List neighbors = new ArrayList<>();
- public Neighbors[] getNeighbors() {
+ public List getNeighbors() {
return neighbors;
}
-
- public static class Neighbors {
-
- private String address;
- private Integer numberOfAllTransactions;
- private Integer numberOfInvalidTransactions;
- private Integer numberOfNewTransactions;
-
- public String getAddress() {
- return address;
- }
-
- public Integer getNumberOfAllTransactions() {
- return numberOfAllTransactions;
- }
-
- public Integer getNumberOfInvalidTransactions() {
- return numberOfInvalidTransactions;
- }
-
- public Integer getNumberOfNewTransactions() {
- return numberOfNewTransactions;
- }
- }
}
diff --git a/src/main/java/jota/dto/response/GetNewAddressResponse.java b/src/main/java/jota/dto/response/GetNewAddressResponse.java
index dab7d86..42d2288 100644
--- a/src/main/java/jota/dto/response/GetNewAddressResponse.java
+++ b/src/main/java/jota/dto/response/GetNewAddressResponse.java
@@ -1,16 +1,19 @@
package jota.dto.response;
+import java.util.List;
+
public class GetNewAddressResponse extends AbstractResponse {
- private String address;
+ private List addresses;
- public String getAddress() {
- return address;
- }
-
- public static GetNewAddressResponse create(String address) {
+ public static GetNewAddressResponse create(List addresses, long duration) {
GetNewAddressResponse res = new GetNewAddressResponse();
- res.address = address;
+ res.addresses = addresses;
+ res.setDuration(duration);
return res;
}
+
+ public List getAddresses() {
+ return addresses;
+ }
}
diff --git a/src/main/java/jota/dto/response/GetNodeInfoResponse.java b/src/main/java/jota/dto/response/GetNodeInfoResponse.java
index f7477e4..233c0fb 100644
--- a/src/main/java/jota/dto/response/GetNodeInfoResponse.java
+++ b/src/main/java/jota/dto/response/GetNodeInfoResponse.java
@@ -4,6 +4,7 @@ public class GetNodeInfoResponse extends AbstractResponse {
private String appName;
private String appVersion;
+ private String jreVersion;
private int jreAvailableProcessors;
private long jreFreeMemory;
private long jreMaxMemory;
@@ -26,6 +27,10 @@ public class GetNodeInfoResponse extends AbstractResponse {
return appVersion;
}
+ public String getJreVersion() {
+ return jreVersion;
+ }
+
public Integer getJreAvailableProcessors() {
return jreAvailableProcessors;
}
diff --git a/src/main/java/jota/dto/response/GetTransactionsToApproveResponse.java b/src/main/java/jota/dto/response/GetTransactionsToApproveResponse.java
index 5f76cf3..ba7d478 100644
--- a/src/main/java/jota/dto/response/GetTransactionsToApproveResponse.java
+++ b/src/main/java/jota/dto/response/GetTransactionsToApproveResponse.java
@@ -3,13 +3,13 @@ package jota.dto.response;
public class GetTransactionsToApproveResponse extends AbstractResponse {
private String trunkTransaction;
- private String branchTransactionToApprove;
-
- public String getBranchTransactionToApprove() {
- return branchTransactionToApprove;
- }
+ private String branchTransaction;
public String getTrunkTransaction() {
return trunkTransaction;
}
-}
+
+ public String getBranchTransaction() {
+ return branchTransaction;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/dto/response/GetTransferResponse.java b/src/main/java/jota/dto/response/GetTransferResponse.java
new file mode 100644
index 0000000..e98251e
--- /dev/null
+++ b/src/main/java/jota/dto/response/GetTransferResponse.java
@@ -0,0 +1,21 @@
+package jota.dto.response;
+
+import jota.model.Bundle;
+
+/**
+ * Created by pinpong on 28.12.16.
+ */
+public class GetTransferResponse extends AbstractResponse {
+
+ private Bundle[] transferBundle;
+
+ public static GetTransferResponse create(Bundle[] transferBundle, long duration) {
+ GetTransferResponse res = new GetTransferResponse();
+ res.transferBundle = transferBundle;
+ return res;
+ }
+
+ public Bundle[] getTransfers() {
+ return transferBundle;
+ }
+}
diff --git a/src/main/java/jota/dto/response/GetTransfersResponse.java b/src/main/java/jota/dto/response/GetTransfersResponse.java
deleted file mode 100644
index 4046ad4..0000000
--- a/src/main/java/jota/dto/response/GetTransfersResponse.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package jota.dto.response;
-
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-
-public class GetTransfersResponse extends AbstractResponse {
-
- private Transfers[] transfers;
-
- public Transfers[] getTransfers() {
- return transfers;
- }
-
- public static class Transfers {
- private String timestamp;
- private String address;
- private String hash;
- private Integer persistence;
- private long value;
-
- public String getAddress() {
- return address;
- }
-
- public String getHash() {
- return hash;
- }
-
- public Integer getPersistence() {
- return persistence;
- }
-
- public String getTimestamp() {
- return timestamp;
- }
-
- public long getValue() {
- return value;
- }
-
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
- }
- }
-}
diff --git a/src/main/java/jota/dto/response/ReplayBundleResponse.java b/src/main/java/jota/dto/response/ReplayBundleResponse.java
new file mode 100644
index 0000000..2c60c6c
--- /dev/null
+++ b/src/main/java/jota/dto/response/ReplayBundleResponse.java
@@ -0,0 +1,25 @@
+package jota.dto.response;
+
+/**
+ * Created by pinpong on 01.01.17.
+ */
+public class ReplayBundleResponse extends AbstractResponse {
+
+ private Boolean[] successfully;
+
+ public static ReplayBundleResponse create(Boolean[] successfully, long duration) {
+ ReplayBundleResponse res = new ReplayBundleResponse();
+ res.successfully = successfully;
+ res.setDuration(duration);
+ return res;
+ }
+
+ public Boolean[] getSuccessfully() {
+ return successfully;
+ }
+
+ public void setSuccessfully(Boolean[] successfully) {
+ this.successfully = successfully;
+ }
+
+}
diff --git a/src/main/java/jota/dto/response/SendTransferResponse.java b/src/main/java/jota/dto/response/SendTransferResponse.java
new file mode 100644
index 0000000..d31b295
--- /dev/null
+++ b/src/main/java/jota/dto/response/SendTransferResponse.java
@@ -0,0 +1,25 @@
+package jota.dto.response;
+
+/**
+ * Created by pinpong on 28.12.16.
+ */
+public class SendTransferResponse extends AbstractResponse {
+
+ private Boolean[] successfully;
+
+ public static SendTransferResponse create(Boolean[] successfully, long duration) {
+ SendTransferResponse res = new SendTransferResponse();
+ res.successfully = successfully;
+ res.setDuration(duration);
+ return res;
+ }
+
+ public Boolean[] getSuccessfully() {
+ return successfully;
+ }
+
+ public void setSuccessfully(Boolean[] successfully) {
+ this.successfully = successfully;
+ }
+
+}
diff --git a/src/main/java/jota/dto/response/StoreTransactionsResponse.java b/src/main/java/jota/dto/response/StoreTransactionsResponse.java
index 04128d2..af6c773 100644
--- a/src/main/java/jota/dto/response/StoreTransactionsResponse.java
+++ b/src/main/java/jota/dto/response/StoreTransactionsResponse.java
@@ -1,6 +1,8 @@
package jota.dto.response;
public class StoreTransactionsResponse extends AbstractResponse {
- // empty response
+ public StoreTransactionsResponse(long duration) {
+ setDuration(duration);
+ }
}
diff --git a/src/main/java/jota/error/ArgumentException.java b/src/main/java/jota/error/ArgumentException.java
new file mode 100644
index 0000000..075d90e
--- /dev/null
+++ b/src/main/java/jota/error/ArgumentException.java
@@ -0,0 +1,17 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 09.12.2016.
+ */
+public class ArgumentException extends BaseException {
+
+ private static final long serialVersionUID = -7850044681919575720L;
+
+ public ArgumentException() {
+ super("Wrong arguments passed to function");
+ }
+
+ public ArgumentException(String msg) {
+ super(msg);
+ }
+}
diff --git a/src/main/java/jota/error/BaseException.java b/src/main/java/jota/error/BaseException.java
new file mode 100644
index 0000000..972a0ac
--- /dev/null
+++ b/src/main/java/jota/error/BaseException.java
@@ -0,0 +1,41 @@
+package jota.error;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Created by Adrian on 09.12.2016.
+ */
+public class BaseException extends Exception {
+
+ private static final long serialVersionUID = 5617085097507773343L;
+
+ protected Collection messages;
+
+ public BaseException(String msg) {
+ super(msg);
+ if (messages == null) {
+ messages = new ArrayList<>();
+ }
+ messages.add(msg);
+ }
+
+ public BaseException(String msg, Exception cause) {
+ super(msg, cause);
+ }
+
+ public BaseException(Collection messages) {
+ this.messages = messages;
+ }
+
+ public BaseException(Collection messages, Exception cause) {
+ super(cause);
+ this.messages = messages;
+ }
+
+ @Override
+ public String getMessage() {
+ return Arrays.toString(messages.toArray());
+ }
+}
diff --git a/src/main/java/jota/error/BroadcastAndStoreException.java b/src/main/java/jota/error/BroadcastAndStoreException.java
new file mode 100644
index 0000000..a5b58e6
--- /dev/null
+++ b/src/main/java/jota/error/BroadcastAndStoreException.java
@@ -0,0 +1,11 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 22.01.2017.
+ */
+public class BroadcastAndStoreException extends BaseException {
+
+ public BroadcastAndStoreException() {
+ super("Impossible to broadcastAndStore, aborting.");
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/error/InvalidBundleException.java b/src/main/java/jota/error/InvalidBundleException.java
new file mode 100644
index 0000000..9f7877d
--- /dev/null
+++ b/src/main/java/jota/error/InvalidBundleException.java
@@ -0,0 +1,15 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 27.12.2016.
+ */
+public class InvalidBundleException extends BaseException {
+
+ public InvalidBundleException(){
+ super("Invalid Bundle");
+ }
+
+ public InvalidBundleException(String msg){
+ super(msg);
+ }
+}
diff --git a/src/main/java/jota/error/InvalidSignatureException.java b/src/main/java/jota/error/InvalidSignatureException.java
new file mode 100644
index 0000000..d8d5565
--- /dev/null
+++ b/src/main/java/jota/error/InvalidSignatureException.java
@@ -0,0 +1,10 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 27.12.2016.
+ */
+public class InvalidSignatureException extends BaseException {
+ public InvalidSignatureException() {
+ super("Invalid Signatures!");
+ }
+}
diff --git a/src/main/java/jota/error/NoInclusionStatesExcpection.java b/src/main/java/jota/error/NoInclusionStatesExcpection.java
new file mode 100644
index 0000000..c79ee4b
--- /dev/null
+++ b/src/main/java/jota/error/NoInclusionStatesExcpection.java
@@ -0,0 +1,11 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 22.01.2017.
+ */
+public class NoInclusionStatesExcpection extends BaseException {
+
+ public NoInclusionStatesExcpection() {
+ super("No inclusion states for the provided seed");
+ }
+}
diff --git a/src/main/java/jota/error/NoNodeInfoException.java b/src/main/java/jota/error/NoNodeInfoException.java
new file mode 100644
index 0000000..920c6f3
--- /dev/null
+++ b/src/main/java/jota/error/NoNodeInfoException.java
@@ -0,0 +1,11 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 22.01.2017.
+ */
+public class NoNodeInfoException extends BaseException {
+
+ public NoNodeInfoException() {
+ super("Node info could not be retrieved");
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/error/NotEnoughBalanceException.java b/src/main/java/jota/error/NotEnoughBalanceException.java
new file mode 100644
index 0000000..64c516c
--- /dev/null
+++ b/src/main/java/jota/error/NotEnoughBalanceException.java
@@ -0,0 +1,13 @@
+package jota.error;
+
+/**
+ * Created by Adrian on 09.12.2016.
+ */
+public class NotEnoughBalanceException extends BaseException {
+
+ private static final long serialVersionUID = -3807270816402226476L;
+
+ public NotEnoughBalanceException() {
+ super("Not enough balance");
+ }
+}
diff --git a/src/main/java/jota/model/Bundle.java b/src/main/java/jota/model/Bundle.java
new file mode 100644
index 0000000..93acfae
--- /dev/null
+++ b/src/main/java/jota/model/Bundle.java
@@ -0,0 +1,147 @@
+package jota.model;
+
+import jota.pow.ICurl;
+import jota.pow.JCurl;
+import jota.utils.Converter;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by pinpong on 09.12.16.
+ */
+public class Bundle implements Comparable {
+
+ private List transactions;
+ private int length;
+
+ public static String EMPTY_HASH = "999999999999999999999999999999999999999999999999999999999999999999999999999999999";
+
+
+ public Bundle() {
+ this(new ArrayList(), 0);
+ }
+
+ public Bundle(List transactions, int length) {
+ this.transactions = transactions;
+ this.length = length;
+ }
+
+ public List getTransactions() {
+ return transactions;
+ }
+
+ public int getLength() {
+ return length;
+ }
+
+ public void setLength(int length) {
+ this.length = length;
+ }
+
+ public void addEntry(int signatureMessageLength, String address, long value, String tag, long timestamp) {
+ if (getTransactions() == null) {
+ this.transactions = new ArrayList<>(getTransactions());
+ }
+
+ for (int i = 0; i < signatureMessageLength; i++) {
+ Transaction trx = new Transaction(address, String.valueOf(i == 0 ? value : 0), tag, String.valueOf(timestamp));
+ getTransactions().add(trx);
+ }
+ }
+
+ public void finalize(ICurl customCurl) {
+
+ ICurl curl = customCurl == null ? new JCurl() : customCurl;
+ curl.reset();
+
+ for (int i = 0; i < this.getTransactions().size(); i++) {
+
+ int[] valueTrits = Converter.trits(this.getTransactions().get(i).getValue(), 81);
+
+ int[] timestampTrits = Converter.trits(this.getTransactions().get(i).getTimestamp(), 27);
+
+ int[] currentIndexTrits = Converter.trits(this.getTransactions().get(i).setCurrentIndex("" + i), 27);
+
+ int[] lastIndexTrits = Converter.trits(this.getTransactions().get(i).setLastIndex("" + (this.getTransactions().size() - 1)), 27);
+
+
+ int[] t = Converter.trits(this.getTransactions().get(i).getAddress() + Converter.trytes(valueTrits) + this.getTransactions().get(i).getTag() + Converter.trytes(timestampTrits) + Converter.trytes(currentIndexTrits) + Converter.trytes(lastIndexTrits));
+ curl.absorb(t, 0, t.length);
+ }
+
+ int[] hash = new int[243];
+ curl.squeeze(hash, 0, hash.length);
+ String hashInTrytes = Converter.trytes(hash);
+
+ for (int i = 0; i < this.getTransactions().size(); i++) {
+ this.getTransactions().get(i).setBundle(hashInTrytes);
+ }
+ }
+
+
+ public void addTrytes(List signatureFragments) {
+ String emptySignatureFragment = "";
+ String emptyHash = EMPTY_HASH;
+
+ emptySignatureFragment = StringUtils.rightPad(emptySignatureFragment, 2187, '9');
+
+ for (int i = 0; i < this.getTransactions().size(); i++) {
+
+ // Fill empty signatureMessageFragment
+ this.getTransactions().get(i).setSignatureFragments((signatureFragments.size() <= i || signatureFragments.get(i).isEmpty()) ? emptySignatureFragment : signatureFragments.get(i));
+ // Fill empty trunkTransaction
+ this.getTransactions().get(i).setTrunkTransaction(emptyHash);
+
+ // Fill empty branchTransaction
+ this.getTransactions().get(i).setBranchTransaction(emptyHash);
+
+ // Fill empty nonce
+ this.getTransactions().get(i).setNonce(emptyHash);
+ }
+ }
+
+ public int[] normalizedBundle(String bundleHash) {
+ int[] normalizedBundle = new int[81];
+
+ for (int i = 0; i < 3; i++) {
+
+ long sum = 0;
+ for (int j = 0; j < 27; j++) {
+
+ sum += (normalizedBundle[i * 27 + j] = Converter.value(Converter.tritsString("" + bundleHash.charAt(i * 27 + j))));
+ }
+
+ if (sum >= 0) {
+ while (sum-- > 0) {
+ for (int j = 0; j < 27; j++) {
+ if (normalizedBundle[i * 27 + j] > -13) {
+ normalizedBundle[i * 27 + j]--;
+ break;
+ }
+ }
+ }
+ } else {
+
+ while (sum++ < 0) {
+
+ for (int j = 0; j < 27; j++) {
+
+ if (normalizedBundle[i * 27 + j] < 13) {
+ normalizedBundle[i * 27 + j]++;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ return normalizedBundle;
+ }
+
+ @Override
+ public int compareTo(Bundle o) {
+ return Long.compare(Long.parseLong(this.getTransactions().get(0).getTimestamp()), Long.parseLong(o.getTransactions().get(0).getTimestamp()));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/model/Input.java b/src/main/java/jota/model/Input.java
new file mode 100644
index 0000000..16ce445
--- /dev/null
+++ b/src/main/java/jota/model/Input.java
@@ -0,0 +1,48 @@
+package jota.model;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+/**
+ * Created by Adrian on 09.12.2016.
+ */
+public class Input {
+ private String address;
+ private long balance;
+ private int keyIndex;
+
+ public Input(String address, long balance, int keyIndex) {
+ this.address = address;
+ this.balance = balance;
+ this.keyIndex = keyIndex;
+ }
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public long getBalance() {
+ return balance;
+ }
+
+ public void setBalance(long balance) {
+ this.balance = balance;
+ }
+
+ public int getKeyIndex() {
+ return keyIndex;
+ }
+
+ public void setKeyIndex(int keyIndex) {
+ this.keyIndex = keyIndex;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/model/Inputs.java b/src/main/java/jota/model/Inputs.java
new file mode 100644
index 0000000..5d4b993
--- /dev/null
+++ b/src/main/java/jota/model/Inputs.java
@@ -0,0 +1,39 @@
+package jota.model;
+
+import com.google.gson.Gson;
+
+import java.util.List;
+
+/**
+ * Created by Adrian on 09.12.2016.
+ */
+public class Inputs {
+ private List inputsList;
+ private long totalBalance;
+
+ public Inputs(List inputsList, long totalBalance) {
+ this.inputsList = inputsList;
+ this.totalBalance = totalBalance;
+ }
+
+ @Override
+ public String toString() {
+ return new Gson().toJson(this);
+ }
+
+ public List getInputsList() {
+ return inputsList;
+ }
+
+ public void setInputsList(List inputsList) {
+ inputsList = inputsList;
+ }
+
+ public long getTotalBalance() {
+ return totalBalance;
+ }
+
+ public void setTotalBalance(long totalBalance) {
+ this.totalBalance = totalBalance;
+ }
+}
diff --git a/src/main/java/jota/model/Neighbor.java b/src/main/java/jota/model/Neighbor.java
new file mode 100644
index 0000000..2eee719
--- /dev/null
+++ b/src/main/java/jota/model/Neighbor.java
@@ -0,0 +1,36 @@
+package jota.model;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class Neighbor {
+
+ private String address;
+ private Integer numberOfAllTransactions;
+ private Integer numberOfInvalidTransactions;
+ private Integer numberOfNewTransactions;
+
+ public Neighbor(String address, Integer numberOfAllTransactions, Integer numberOfInvalidTransactions, Integer numberOfNewTransactions) {
+ this.address = address;
+ this.numberOfAllTransactions = numberOfAllTransactions;
+ this.numberOfInvalidTransactions = numberOfInvalidTransactions;
+ this.numberOfNewTransactions = numberOfNewTransactions;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public Integer getNumberOfAllTransactions() {
+ return numberOfAllTransactions;
+ }
+
+ public Integer getNumberOfInvalidTransactions() {
+ return numberOfInvalidTransactions;
+ }
+
+ public Integer getNumberOfNewTransactions() {
+ return numberOfNewTransactions;
+ }
+
+}
diff --git a/src/main/java/jota/model/Signature.java b/src/main/java/jota/model/Signature.java
new file mode 100644
index 0000000..3e0a9ba
--- /dev/null
+++ b/src/main/java/jota/model/Signature.java
@@ -0,0 +1,33 @@
+package jota.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by Adrian on 27.12.2016.
+ */
+public class Signature {
+
+ String address;
+ List signatureFragments;
+
+ public Signature() {
+ this.signatureFragments = new ArrayList<>();
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public List getSignatureFragments() {
+ return signatureFragments;
+ }
+
+ public void setSignatureFragments(List signatureFragments) {
+ this.signatureFragments = signatureFragments;
+ }
+}
diff --git a/src/main/java/jota/model/Transaction.java b/src/main/java/jota/model/Transaction.java
new file mode 100644
index 0000000..dbe0506
--- /dev/null
+++ b/src/main/java/jota/model/Transaction.java
@@ -0,0 +1,252 @@
+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;
+ private String value;
+ private String tag;
+ private String timestamp;
+ private String currentIndex;
+ private String lastIndex;
+ private String bundle;
+ private String trunkTransaction;
+ private String branchTransaction;
+ private String nonce;
+ private Boolean persistence;
+
+ 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) {
+
+ this.hash = hash;
+ this.tag = tag;
+ this.signatureFragments = signatureFragments;
+ this.address = address;
+ this.value = value;
+ this.timestamp = timestamp;
+ this.currentIndex = currentIndex;
+ this.lastIndex = lastIndex;
+ this.bundle = bundle;
+ this.trunkTransaction = trunkTransaction;
+ this.branchTransaction = branchTransaction;
+ this.nonce = nonce;
+ }
+
+
+ public Transaction(String address, String value, String tag, String timestamp) {
+ this.address = address;
+ this.value = value;
+ this.tag = tag;
+ this.timestamp = timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
+ }
+
+ public String getHash() {
+ return hash;
+ }
+
+ public void setHash(String hash) {
+ this.hash = hash;
+ }
+
+ public String getSignatureFragments() {
+ return signatureFragments;
+ }
+
+ public String setSignatureFragments(String signatureFragments) {
+ return this.signatureFragments = signatureFragments;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public String getTag() {
+ return tag;
+ }
+
+ public void setTag(String tag) {
+ this.tag = tag;
+ }
+
+ public String getTimestamp() {
+ return timestamp;
+ }
+
+ public void setTimestamp(String timestamp) {
+ this.timestamp = timestamp;
+ }
+
+ public String getCurrentIndex() {
+ return currentIndex;
+ }
+
+ public String setCurrentIndex(String currentIndex) {
+ return this.currentIndex = currentIndex;
+ }
+
+ public String getLastIndex() {
+ return lastIndex;
+ }
+
+ public String setLastIndex(String lastIndex) {
+ return this.lastIndex = lastIndex;
+ }
+
+ public String getBundle() {
+ return bundle;
+ }
+
+ public void setBundle(String bundle) {
+ this.bundle = bundle;
+ }
+
+ public String getTrunkTransaction() {
+ return trunkTransaction;
+ }
+
+ public void setTrunkTransaction(String trunkTransaction) {
+ this.trunkTransaction = trunkTransaction;
+ }
+
+ public String getBranchTransaction() {
+ return branchTransaction;
+ }
+
+ public void setBranchTransaction(String branchTransaction) {
+ this.branchTransaction = branchTransaction;
+ }
+
+ public String getNonce() {
+ return nonce;
+ }
+
+ public void setNonce(String nonce) {
+ this.nonce = nonce;
+ }
+
+ public Boolean getPersistence() {
+ return persistence;
+ }
+
+ public void setPersistence(Boolean persistence) {
+ this.persistence = persistence;
+ }
+
+ public boolean equals(Object obj) {
+ 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));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/model/Transfer.java b/src/main/java/jota/model/Transfer.java
new file mode 100644
index 0000000..a1b675e
--- /dev/null
+++ b/src/main/java/jota/model/Transfer.java
@@ -0,0 +1,97 @@
+package jota.model;
+
+import com.google.gson.Gson;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class Transfer {
+
+ private String timestamp;
+ private String address;
+ private String hash;
+ private Boolean persistence;
+ private long value;
+ private String message;
+ private String tag;
+
+ public Transfer(String timestamp, String address, String hash, Boolean persistence, long value, String message,
+ String tag) {
+ this.timestamp = timestamp;
+ this.address = address;
+ this.hash = hash;
+ this.persistence = persistence;
+ this.value = value;
+ this.message = message;
+ this.tag = tag;
+
+ }
+
+ public Transfer(String address, long value, String message, String tag) {
+ this.address = address;
+ this.value = value;
+ this.message = message;
+ this.tag = tag;
+ }
+
+ @Override
+ public String toString() {
+ return new Gson().toJson(this);
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public String getHash() {
+ return hash;
+ }
+
+ public Boolean getPersistence() {
+ return persistence;
+ }
+
+ public String getTimestamp() {
+ return timestamp;
+ }
+
+ public long getValue() {
+ return value;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getTag() {
+ return tag;
+ }
+
+ public void setTimestamp(String timestamp) {
+ this.timestamp = timestamp;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public void setHash(String hash) {
+ this.hash = hash;
+ }
+
+ public void setPersistence(Boolean persistence) {
+ this.persistence = persistence;
+ }
+
+ public void setValue(long value) {
+ this.value = value;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public void setTag(String tag) {
+ this.tag = tag;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/pow/ICurl.java b/src/main/java/jota/pow/ICurl.java
new file mode 100644
index 0000000..9534837
--- /dev/null
+++ b/src/main/java/jota/pow/ICurl.java
@@ -0,0 +1,22 @@
+package jota.pow;
+
+/**
+ * Created by Adrian on 07.01.2017.
+ */
+public interface ICurl {
+ JCurl absorb(final int[] trits, int offset, int length);
+
+ JCurl absorb(final int[] trits);
+
+ int[] squeeze(final int[] trits, int offset, int length);
+
+ int[] squeeze(final int[] trits);
+
+ JCurl transform();
+
+ JCurl reset();
+
+ int[] getState();
+
+ void setState(int[] state);
+}
diff --git a/src/main/java/jota/utils/Curl.java b/src/main/java/jota/pow/JCurl.java
similarity index 70%
rename from src/main/java/jota/utils/Curl.java
rename to src/main/java/jota/pow/JCurl.java
index b07236f..c57f4b8 100644
--- a/src/main/java/jota/utils/Curl.java
+++ b/src/main/java/jota/pow/JCurl.java
@@ -1,11 +1,11 @@
-package jota.utils;
+package jota.pow;
/**
* (c) 2016 Come-from-Beyond
*
- * Curl belongs to the sponge function family.
+ * JCurl belongs to the sponge function family.
*/
-public class Curl {
+public class JCurl implements ICurl {
public static final int HASH_LENGTH = 243;
private static final int STATE_LENGTH = 3 * HASH_LENGTH;
@@ -13,17 +13,45 @@ 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) {
+ public JCurl absorb(final int[] trits, int offset, int length) {
do {
System.arraycopy(trits, offset, state, 0, length < HASH_LENGTH ? length : HASH_LENGTH);
transform();
offset += HASH_LENGTH;
} while ((length -= HASH_LENGTH) > 0);
+
+ return this;
}
+
+
+ public JCurl absorb(final int[] trits) {
+ return absorb(trits, 0, trits.length);
+ }
+
+ public JCurl transform() {
+
+ final int[] scratchpad = new int[STATE_LENGTH];
+ int scratchpadIndex = 0;
+ for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
+ System.arraycopy(state, 0, scratchpad, 0, STATE_LENGTH);
+ for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
+ state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndex] + scratchpad[scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365)] * 3 + 4];
+ }
+ }
+ return this;
+ }
+
+ public JCurl reset() {
+ for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
+ state[stateIndex] = 0;
+ }
+ return this;
+ }
+
public int[] squeeze(final int[] trits, int offset, int length) {
do {
@@ -34,26 +62,15 @@ public class Curl {
return state;
}
-
- private void transform() {
-
- final int[] scratchpad = new int[STATE_LENGTH];
- int scratchpadIndex = 0;
- for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
- System.arraycopy(state, 0, scratchpad, 0, STATE_LENGTH);
- for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
- state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndex] + scratchpad[scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365)] * 3 + 4];
- }
- }
- }
-
- public void reset() {
- for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
- state[stateIndex] = 0;
- }
+
+ public int[] squeeze(final int[] trits) {
+ return squeeze(trits, 0, trits.length);
}
public int[] getState() {
return state;
}
+ public void setState(int[] state) {
+ this.state = state;
+ }
}
diff --git a/src/main/java/jota/utils/Checksum.java b/src/main/java/jota/utils/Checksum.java
new file mode 100644
index 0000000..79605e5
--- /dev/null
+++ b/src/main/java/jota/utils/Checksum.java
@@ -0,0 +1,50 @@
+package jota.utils;
+
+import jota.pow.JCurl;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class Checksum {
+
+ public static String addChecksum(String address) {
+ InputValidator.checkAddress(address);
+ String addressWithChecksum = address;
+ addressWithChecksum += calculateChecksum(address);
+ return addressWithChecksum;
+ }
+
+ public static String removeChecksum(String addressWithChecksum) {
+ if (isAddressWithChecksum(addressWithChecksum)) {
+ return getAddress(addressWithChecksum);
+ }
+ return StringUtils.EMPTY;
+ }
+
+ private static String getAddress(String addressWithChecksum) {
+ return addressWithChecksum.substring(0, Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM);
+ }
+
+ public static boolean isValidChecksum(String addressWithChecksum) {
+ String addressWithoutChecksum = removeChecksum(addressWithChecksum);
+ String addressWithRecalculateChecksum = addressWithChecksum += calculateChecksum(addressWithoutChecksum);
+ return addressWithRecalculateChecksum.equals(addressWithChecksum);
+ }
+
+ public static boolean isAddressWithChecksum(String address) {
+ return InputValidator.checkAddress(address) && address.length() == Constants.ADDRESS_LENGTH_WITH_CHECKSUM;
+ }
+
+ public static boolean isAddressWithoutChecksum(String address) {
+ return InputValidator.checkAddress(address) && address.length() == Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM;
+ }
+
+ public 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);
+ }
+}
diff --git a/src/main/java/jota/utils/Constants.java b/src/main/java/jota/utils/Constants.java
new file mode 100644
index 0000000..5f8f9a2
--- /dev/null
+++ b/src/main/java/jota/utils/Constants.java
@@ -0,0 +1,14 @@
+package jota.utils;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class Constants {
+
+ public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ public static final int SEED_LENGTH_MAX = 81;
+
+ public static int ADDRESS_LENGTH_WITHOUT_CHECKSUM = 81;
+ public static int ADDRESS_LENGTH_WITH_CHECKSUM = 90;
+}
diff --git a/src/main/java/jota/utils/Converter.java b/src/main/java/jota/utils/Converter.java
index be5313f..a5f5802 100644
--- a/src/main/java/jota/utils/Converter.java
+++ b/src/main/java/jota/utils/Converter.java
@@ -1,17 +1,24 @@
package jota.utils;
+import jota.model.Transaction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
public class Converter {
- public static final int RADIX = 3;
- public static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
+ private static final Logger log = LoggerFactory.getLogger(Converter.class);
- public static final int NUMBER_OF_TRITS_IN_A_BYTE = 5;
- public static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3;
- public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][];
- static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][];
+ private static final int RADIX = 3;
+ private static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
+
+ private static final int NUMBER_OF_TRITS_IN_A_BYTE = 5;
+ private static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3;
+ private static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][];
+ private static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][];
static {
@@ -59,14 +66,72 @@ public class Converter {
}
}
- public static int[] trits(final String trytes) {
-
- final int[] trits = new int[trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE];
- for (int i = 0; i < trytes.length(); i++) {
- 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);
+ public static int[] convertToIntArray(List integers)
+ {
+ int[] ret = new int[integers.size()];
+ for (int i=0; i < ret.length; i++)
+ {
+ ret[i] = integers.get(i);
}
+ return ret;
+ }
- return trits;
+ public static int[] trits(final String trytes, int length) {
+ int[] trits = trits(trytes);
+
+ List tritsList = new LinkedList<>();
+
+ for(int i : trits)
+ tritsList.add(i);
+
+ while(tritsList.size() < length)
+ tritsList.add(0);
+
+ return convertToIntArray(tritsList);
+ }
+ public static int[] tritsString(final String trytes){
+ int[] d = new int[3 * trytes.length()];
+ for (int i = 0; i < trytes.length(); i++) {
+ System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, d, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
+ }
+ return d;
+ }
+
+ public static int[] trits(final String trytes) {
+ final List trits = new LinkedList<>();
+ if (InputValidator.isValue(trytes)) {
+
+ long value = Long.parseLong(trytes);
+
+ long absoluteValue = value < 0 ? -value : value;
+
+ int position = 0;
+
+ while (absoluteValue > 0) {
+
+ int remainder = (int) (absoluteValue % RADIX);
+ absoluteValue /= RADIX;
+
+ if (remainder > MAX_TRIT_VALUE) {
+ remainder = MIN_TRIT_VALUE;
+ absoluteValue++;
+ }
+
+ trits.add(position++,remainder);
+ }
+ if (value < 0) {
+ for (int i = 0; i < trits.size(); i++) {
+ trits.set(i,-trits.get(i));
+ }
+ }
+ } else {
+ int[] d = new int[3 * trytes.length()];
+ for (int i = 0; i < trytes.length(); i++) {
+ System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, d, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
+ }
+ return d;
+ }
+ return convertToIntArray(trits);
}
public static void copyTrits(final long value, final int[] destination, final int offset, final int size) {
@@ -81,7 +146,7 @@ public class Converter {
remainder = MIN_TRIT_VALUE;
absoluteValue++;
}
- destination[offset + i] = remainder;
+ destination[offset +i ] = remainder;
}
if (value < 0) {
@@ -92,6 +157,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();
@@ -100,9 +175,9 @@ public class Converter {
int j = trits[offset + i * 3] + trits[offset + i * 3 + 1] * 3 + trits[offset + i * 3 + 2] * 9;
if (j < 0) {
- j += TRYTE_ALPHABET.length();
+ j += Constants.TRYTE_ALPHABET.length();
}
- trytes.append(TRYTE_ALPHABET.charAt(j));
+ trytes.append(Constants.TRYTE_ALPHABET.charAt(j));
}
return trytes.toString();
}
@@ -115,6 +190,24 @@ public class Converter {
return trits[offset] + trits[offset + 1] * 3 + trits[offset + 2] * 9;
}
+ public static int value(final int[] trits) {
+ int value = 0;
+
+ for (int i = trits.length; i-- > 0; ) {
+ value = value * 3 + trits[i];
+ }
+ return value;
+ }
+
+ public static long longValue(final int[] trits) {
+ long value = 0;
+
+ for (int i = trits.length; i-- > 0; ) {
+ value = value * 3 + trits[i];
+ }
+ return value;
+ }
+
public static void increment(final int[] trits, final int size) {
for (int i = 0; i < size; i++) {
@@ -125,4 +218,5 @@ public class Converter {
}
}
}
-}
\ No newline at end of file
+
+}
diff --git a/src/main/java/jota/utils/InputValidator.java b/src/main/java/jota/utils/InputValidator.java
new file mode 100644
index 0000000..4e01cd4
--- /dev/null
+++ b/src/main/java/jota/utils/InputValidator.java
@@ -0,0 +1,93 @@
+package jota.utils;
+
+import jota.model.Transfer;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+
+import java.util.List;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class InputValidator {
+
+ public static boolean isAddress(String address) {
+ return (address.length() == Constants.ADDRESS_LENGTH_WITHOUT_CHECKSUM ||
+ address.length() == Constants.ADDRESS_LENGTH_WITH_CHECKSUM) && isTrytes(address, address.length());
+ }
+
+ public static boolean checkAddress(String address) {
+ if (!isAddress(address)) {
+ throw new RuntimeException("Invalid address: " + address);
+ }
+ return true;
+ }
+
+ public static boolean isTrytes(final String trytes, final int length) {
+ return trytes.matches("^[A-Z9]{" + (length == 0 ? "0," : length) + "}$");
+ }
+
+ public static boolean isValue(final String value) {
+ return NumberUtils.isNumber(value);
+ }
+
+ public static boolean isArrayOfHashes(String[] hashes) {
+ if (hashes == null)
+ return false;
+
+ for (String hash : hashes) {
+ // Check if address with checksum
+ if (hash.length() == 90) {
+ if (!isTrytes(hash, 90)) {
+ return false;
+ }
+ } else {
+ if (!isTrytes(hash, 81)) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * checks if input is correct hash collections
+ *
+ * @param {array} hash
+ * @method isTransfersArray
+ * @returns {boolean}
+ **/
+ public static boolean isTransfersCollectionCorrect(final List transfers) {
+
+ for (final Transfer transfer : transfers) {
+ if (!isTransfersArray(transfer)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static boolean isTransfersArray(final Transfer transfer) {
+
+ if (!isAddress(transfer.getAddress())) {
+ return false;
+ }
+
+ // Check if message is correct trytes of any length
+ if (!isTrytes(transfer.getMessage(), 0)) {
+ return false;
+ }
+
+ // Check if tag is correct trytes of {0,27} trytes
+ return isTrytes(transfer.getTag(), 27);
+ }
+
+ public static String validateSeed(String seed) {
+ if (seed.length() > 81)
+ return null;
+
+ seed = StringUtils.rightPad(seed, 81, '9');
+
+ return seed;
+ }
+}
diff --git a/src/main/java/jota/utils/IotaAPIUtils.java b/src/main/java/jota/utils/IotaAPIUtils.java
index dd47181..dfc869b 100644
--- a/src/main/java/jota/utils/IotaAPIUtils.java
+++ b/src/main/java/jota/utils/IotaAPIUtils.java
@@ -1,11 +1,15 @@
package jota.utils;
-import jota.dto.response.GetBundleResponse;
-import jota.dto.response.GetNewAddressResponse;
-import org.apache.commons.lang3.NotImplementedException;
+import java.util.*;
+
+import jota.pow.ICurl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import jota.model.Bundle;
+import jota.model.Input;
+import jota.model.Transaction;
+
/**
* Client Side computation service
*
@@ -15,17 +19,102 @@ public class IotaAPIUtils {
private static final Logger log = LoggerFactory.getLogger(IotaAPIUtils.class);
- public static GetNewAddressResponse getNewAddress(final String seed, final int index) {
+ /**
+ * Generates a new address
+ *
+ * @param seed
+ * @param index
+ * @param checksum
+ * @return an String with address
+ */
+ public static String newAddress(String seed, int index, boolean checksum, ICurl curl) {
+ Signing signing = new Signing(curl);
+ final int[] key = signing.key(Converter.trits(seed), index, 2);
+ final int[] digests = signing.digests(key);
+ final int[] addressTrits = signing.address(digests);
- final int[] key = Signing.key(Converter.trits(seed), index, 2);
- final int[] digests = Signing.digests(key);
- final int[] addressTrits = Signing.address(digests);
- final String address = Converter.trytes(addressTrits);
+ String address = Converter.trytes(addressTrits);
- return GetNewAddressResponse.create(address);
+ if (checksum) {
+ address = Checksum.addChecksum(address);
+ }
+ return address;
}
- public static GetBundleResponse getBundle(final String transaction) {
- throw new NotImplementedException("Not yet implemented");
+ public static List signInputsAndReturn(final String seed,
+ final List inputs,
+ final Bundle bundle,
+ final List signatureFragments, ICurl curl) {
+ bundle.finalize(curl);
+ bundle.addTrytes(signatureFragments);
+
+ // SIGNING OF INPUTS
+ //
+ // Here we do the actual signing of the inputs
+ // Iterate over all bundle transactions, find the inputs
+ // Get the corresponding private key and calculate the signatureFragment
+ for (int i = 0; i < bundle.getTransactions().size(); i++) {
+ if (Long.parseLong(bundle.getTransactions().get(i).getValue()) < 0) {
+ String thisAddress = bundle.getTransactions().get(i).getAddress();
+
+ // Get the corresponding keyIndex of the address
+ int keyIndex = 0;
+ for (Input input : inputs) {
+ if (input.getAddress().equals(thisAddress)) {
+ keyIndex = input.getKeyIndex();
+ break;
+ }
+ }
+
+ String bundleHash = bundle.getTransactions().get(i).getBundle();
+
+ // Get corresponding private key of address
+ int[] key = new Signing(curl).key(Converter.trits(seed), keyIndex, 2);
+
+ // First 6561 trits for the firstFragment
+ int[] firstFragment = Arrays.copyOfRange(key, 0, 6561);
+
+ // Get the normalized bundle hash
+ int[] normalizedBundleHash = bundle.normalizedBundle(bundleHash);
+
+ // First bundle fragment uses 27 trytes
+ int[] firstBundleFragment = Arrays.copyOfRange(normalizedBundleHash, 0, 27);
+
+ // Calculate the new signatureFragment with the first bundle fragment
+ int[] firstSignedFragment = new Signing(curl).signatureFragment(firstBundleFragment, firstFragment);
+
+ // Convert signature to trytes and assign the new signatureFragment
+ bundle.getTransactions().get(i).setSignatureFragments(Converter.trytes(firstSignedFragment));
+
+ // Because the signature is > 2187 trytes, we need to
+ // find the second transaction to add the remainder of the signature
+ for (int j = 0; j < bundle.getTransactions().size(); j++) {
+ // Same address as well as value = 0 (as we already spent the input)
+ if (bundle.getTransactions().get(j).getAddress().equals(thisAddress) && Long.parseLong(bundle.getTransactions().get(j).getValue()) == 0) {
+ // Use the second 6562 trits
+ int[] secondFragment = Arrays.copyOfRange(key, 6561, 6561 * 2);
+
+ // The second 27 to 54 trytes of the bundle hash
+ int[] secondBundleFragment = Arrays.copyOfRange(normalizedBundleHash, 27, 27 * 2);
+
+ // Calculate the new signature
+ int[] secondSignedFragment = new Signing(curl).signatureFragment(secondBundleFragment, secondFragment);
+
+ // Convert signature to trytes and assign it again to this bundle entry
+ bundle.getTransactions().get(j).setSignatureFragments(Converter.trytes(secondSignedFragment));
+ }
+ }
+ }
+ }
+
+ List bundleTrytes = new ArrayList<>();
+
+ // Convert all bundle entries into trytes
+ for (Transaction tx : bundle.getTransactions()) {
+ bundleTrytes.add(tx.toTrytes());
+ }
+ Collections.reverse(bundleTrytes);
+ return bundleTrytes;
}
}
+
diff --git a/src/main/java/jota/utils/IotaUnitConverter.java b/src/main/java/jota/utils/IotaUnitConverter.java
index 57271e8..eac11b4 100644
--- a/src/main/java/jota/utils/IotaUnitConverter.java
+++ b/src/main/java/jota/utils/IotaUnitConverter.java
@@ -1,7 +1,9 @@
package jota.utils;
+import java.text.DecimalFormat;
+
/**
- * Created by pinpong on 30.11.16.
+ * Created by Sascha on 30.11.16.
*/
public class IotaUnitConverter {
@@ -13,4 +15,58 @@ public class IotaUnitConverter {
private static long convertUnits(long amount, IotaUnits toUnit) {
return (long) (amount / Math.pow(10, toUnit.getValue()));
}
+
+ public static String convertRawIotaAmountToDisplayText(long amount, boolean extended) {
+ IotaUnits unit = findOptimalIotaUnitToDisplay(amount);
+ double amountInDisplayUnit = convertAmountTo(amount, unit);
+ return createAmountWithUnitDisplayText(amountInDisplayUnit, unit, extended);
+ }
+
+ public static double convertAmountTo(long amount, IotaUnits target) {
+ return amount / Math.pow(10, target.getValue());
+ }
+
+ private static String createAmountWithUnitDisplayText(double amountInUnit, IotaUnits unit, boolean extended) {
+ String result = createAmountDisplayText(amountInUnit, unit, extended);
+ result += " " + unit.getUnit();
+ return result;
+ }
+
+ public static String createAmountDisplayText(double amountInUnit, IotaUnits unit, boolean extended) {
+ DecimalFormat df;
+ if (extended) df = new DecimalFormat("##0.##################");
+ else
+ df = new DecimalFormat("##0.##");
+
+ String result = "";
+ // display unit as integer if value is between 1-999 or in decimal format
+ result += unit == IotaUnits.IOTA ? (long) amountInUnit : df.format(amountInUnit);
+ return result;
+ }
+
+ public static IotaUnits findOptimalIotaUnitToDisplay(long amount) {
+ int length = String.valueOf(amount).length();
+
+ if (amount < 0) {// do not count "-" sign
+ length -= 1;
+ }
+
+ IotaUnits units = IotaUnits.IOTA;
+
+ if (length >= 1 && length <= 3) {
+ units = IotaUnits.IOTA;
+ } else if (length > 3 && length <= 6) {
+ units = IotaUnits.KILO_IOTA;
+ } else if (length > 6 && length <= 9) {
+ units = IotaUnits.MEGA_IOTA;
+ } else if (length > 9 && length <= 12) {
+ units = IotaUnits.GIGA_IOTA;
+ } else if (length > 12 && length <= 15) {
+ units = IotaUnits.TERA_IOTA;
+ } else if (length > 15 && length <= 18) {
+ units = IotaUnits.PETA_IOTA;
+ }
+ return units;
+ }
+
}
diff --git a/src/main/java/jota/utils/IotaUnits.java b/src/main/java/jota/utils/IotaUnits.java
index f3fbfe0..d94b235 100644
--- a/src/main/java/jota/utils/IotaUnits.java
+++ b/src/main/java/jota/utils/IotaUnits.java
@@ -1,9 +1,10 @@
package jota.utils;
/**
- * Created by pinpong on 30.11.16.
- */
+ * Table of IOTA units based off of the standard system of Units
+ **/
public enum IotaUnits {
+
IOTA("i", 0),
KILO_IOTA("Ki", 3),
MEGA_IOTA("Mi", 6),
diff --git a/src/main/java/jota/utils/NamedThreadFactory.java b/src/main/java/jota/utils/NamedThreadFactory.java
new file mode 100644
index 0000000..a6e3dab
--- /dev/null
+++ b/src/main/java/jota/utils/NamedThreadFactory.java
@@ -0,0 +1,26 @@
+package jota.utils;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Created by Adrian on 15.01.2017.
+ */
+public class NamedThreadFactory implements ThreadFactory {
+ private final String baseName;
+ private final AtomicInteger threadNum = new AtomicInteger(0);
+
+ public NamedThreadFactory(String baseName) {
+ this.baseName = baseName;
+ }
+
+ @Override
+ public synchronized Thread newThread(Runnable r) {
+ Thread t = Executors.defaultThreadFactory().newThread(r);
+
+ t.setName(baseName + "-" + threadNum.getAndIncrement());
+
+ return t;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/utils/Parallel.java b/src/main/java/jota/utils/Parallel.java
new file mode 100644
index 0000000..f5cdeb7
--- /dev/null
+++ b/src/main/java/jota/utils/Parallel.java
@@ -0,0 +1,45 @@
+package jota.utils;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * Created by Adrian on 15.01.2017.
+ */
+public class Parallel {
+ private static final int NUM_CORES = Runtime.getRuntime().availableProcessors();
+
+ private static final ExecutorService forPool = Executors.newFixedThreadPool(NUM_CORES * 2, new NamedThreadFactory("Parallel.For"));
+
+ public static void For(final Iterable elements, final Operation operation) {
+ try {
+ // invokeAll blocks for us until all submitted tasks in the call complete
+ forPool.invokeAll(createCallables(elements, operation));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static Collection> createCallables(final Iterable elements, final Operation operation) {
+ List> callables = new LinkedList<>();
+ for (final T elem : elements) {
+ callables.add(new Callable() {
+ @Override
+ public Void call() {
+ operation.perform(elem);
+ return null;
+ }
+ });
+ }
+
+ return callables;
+ }
+
+ public interface Operation {
+ void perform(T pParameter);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/jota/utils/SeedRandomGenerator.java b/src/main/java/jota/utils/SeedRandomGenerator.java
new file mode 100644
index 0000000..fa8b58c
--- /dev/null
+++ b/src/main/java/jota/utils/SeedRandomGenerator.java
@@ -0,0 +1,20 @@
+package jota.utils;
+
+import java.security.SecureRandom;
+
+/**
+ * Created by pinpong on 13.12.16.
+ */
+public class SeedRandomGenerator {
+
+ public static String generateNewSeed() {
+ char[] chars = Constants.TRYTE_ALPHABET.toCharArray();
+ StringBuilder builder = new StringBuilder();
+ SecureRandom random = new SecureRandom();
+ for (int i = 0; i < Constants.SEED_LENGTH_MAX; i++) {
+ char c = chars[random.nextInt(chars.length)];
+ builder.append(c);
+ }
+ return builder.toString();
+ }
+}
diff --git a/src/main/java/jota/utils/Signing.java b/src/main/java/jota/utils/Signing.java
index 639a006..1cf70a5 100644
--- a/src/main/java/jota/utils/Signing.java
+++ b/src/main/java/jota/utils/Signing.java
@@ -4,36 +4,47 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import jota.model.Bundle;
+import jota.pow.ICurl;
+import jota.pow.JCurl;
+
public class Signing {
+ private ICurl curl;
- static int[] key(int[] seed, int index, int length) {
+ public Signing() {
+ this(null);
+ }
- final int[] subseed = seed;
+ public Signing(ICurl curl) {
+ this.curl = curl == null ? new JCurl() : curl;
+ }
+
+ public int[] key(int[] seed, int index, int length) {
for (int i = 0; i < index; i++) {
for (int j = 0; j < 243; j++) {
- if (++subseed[j] > 1) {
- subseed[j] = -1;
+ if (++seed[j] > 1) {
+ seed[j] = -1;
} else {
break;
}
}
}
- Curl curl = new Curl();
- //curl.absorb(subseed, state);
- //curl.squeeze(subseed, state);
- curl.absorb(subseed, 0, subseed.length);
+ curl.reset();
+ curl.absorb(seed, 0, seed.length);
+ curl.squeeze(seed, 0, seed.length);
+ curl.reset();
+ curl.absorb(seed, 0, seed.length);
- List key = new ArrayList<>();
- int[] buffer = new int[subseed.length];
+ final List key = new ArrayList<>();
+ int[] buffer = new int[seed.length];
int offset = 0;
while (length-- > 0) {
for (int i = 0; i < 27; i++) {
-
- curl.squeeze(buffer, 0, buffer.length);
+ curl.squeeze(buffer, offset, buffer.length);
for (int j = 0; j < 243; j++) {
key.add(buffer[j]);
}
@@ -42,7 +53,7 @@ public class Signing {
return to(key);
}
- private static int[] to(List key) {
+ private int[] to(List key) {
int a[] = new int[key.size()];
int i = 0;
for (Integer v : key) {
@@ -51,11 +62,40 @@ public class Signing {
return a;
}
- public static int[] digests(int[] key) {
- final Curl curl = new Curl();
+ public int[] signatureFragment(int[] normalizedBundleFragment, int[] keyFragment) {
- int[] digests = new int[key.length];
- int[] buffer = new int[key.length];
+ int[] hash;
+
+ for (int i = 0; i < 27; i++) {
+
+ hash = Arrays.copyOfRange(keyFragment, i * 243, (i + 1) * 243);
+
+ for (int j = 0; j < 13 - normalizedBundleFragment[i]; j++) {
+ curl.reset()
+ .absorb(hash, 0, hash.length)
+ .squeeze(hash, 0, hash.length);
+ }
+
+ for (int j = 0; j < 243; j++) {
+ System.arraycopy(hash, j, keyFragment, i * 243 + j, 1);
+ }
+ }
+
+ return keyFragment;
+ }
+
+ public int[] address(int[] digests) {
+ int[] address = new int[243];
+ curl.reset()
+ .absorb(digests)
+ .squeeze(address);
+ return address;
+ }
+
+ public int[] digests(int[] key) {
+
+ int[] digests = new int[(int) Math.floor(key.length / 6561) * 243];
+ int[] buffer = new int[243];
for (int i = 0; i < Math.floor(key.length / 6561); i++) {
int[] keyFragment = Arrays.copyOfRange(key, i * 6561, (i + 1) * 6561);
@@ -64,31 +104,69 @@ public class Signing {
buffer = Arrays.copyOfRange(keyFragment, j * 243, (j + 1) * 243);
for (int k = 0; k < 26; k++) {
-
- curl.absorb(buffer, 0, buffer.length);
- curl.squeeze(buffer, 0, buffer.length);
- }
- for (int k = 0; k < 243; k++) {
-
- keyFragment[j * 243 + k] = buffer[k];
+ curl.reset()
+ .absorb(buffer)
+ .squeeze(buffer);
}
+ System.arraycopy(buffer, 0, keyFragment, j * 243, 243);
}
+ curl.reset();
curl.absorb(keyFragment, 0, keyFragment.length);
curl.squeeze(buffer, 0, buffer.length);
- for (int j = 0; j < 243; j++) {
- digests[i * 243 + j] = buffer[j];
- }
+ System.arraycopy(buffer, 0, digests, i * 243, 243);
}
return digests;
}
- public static int[] address(int[] digests) {
- final Curl curl = new Curl();
- int[] address = new int[digests.length];
- curl.absorb(digests, 0, digests.length);
- curl.squeeze(address, 0, address.length);
- return address;
+ public int[] digest(int[] normalizedBundleFragment, int[] signatureFragment) {
+ curl.reset();
+ int[] buffer = new int[243];
+
+ for (int i = 0; i < 27; i++) {
+ buffer = Arrays.copyOfRange(signatureFragment, i * 243, (i + 1) * 243);
+
+ for (int j = normalizedBundleFragment[i] + 13; j-- > 0; ) {
+
+ ICurl jCurl = new JCurl();
+ jCurl.reset();
+ jCurl.absorb(buffer);
+ jCurl.squeeze(buffer);
+ }
+ curl.absorb(buffer);
+ }
+ curl.squeeze(buffer);
+
+ return buffer;
+ }
+
+ public Boolean validateSignatures(String expectedAddress, String[] signatureFragments, String bundleHash) {
+
+ Bundle bundle = new Bundle();
+
+ int[][] normalizedBundleFragments = new int[3][27];
+ int[] normalizedBundleHash = bundle.normalizedBundle(bundleHash);
+
+ // Split hash into 3 fragments
+ for (int i = 0; i < 3; i++) {
+ normalizedBundleFragments[i] = Arrays.copyOfRange(normalizedBundleHash, i * 27, (i + 1) * 27);
+ }
+
+ // Get digests
+ int[] digests = new int[signatureFragments.length * 243];
+
+ for (int i = 0; i < signatureFragments.length; i++) {
+
+ int[] digestBuffer = digest(normalizedBundleFragments[i % 3], Converter.trits(signatureFragments[i]));
+
+ for (int j = 0; j < 243; j++) {
+ System.arraycopy(digestBuffer, j, digests, i * 243 + j, 1);
+ }
+ }
+ String address = Converter.trytes(address(digests));
+
+ return (expectedAddress.equals(address));
}
}
+
diff --git a/src/main/java/jota/utils/StopWatch.java b/src/main/java/jota/utils/StopWatch.java
new file mode 100644
index 0000000..699de6f
--- /dev/null
+++ b/src/main/java/jota/utils/StopWatch.java
@@ -0,0 +1,71 @@
+package jota.utils;
+
+/**
+ * Created by Adrian on 15.01.2017.
+ */
+public class StopWatch {
+ private long startTime = 0;
+ private boolean running = false;
+ private long currentTime = 0;
+
+ public StopWatch() {
+ this.startTime = System.currentTimeMillis();
+ this.running = true;
+ }
+
+ public void reStart() {
+ this.startTime = System.currentTimeMillis();
+ this.running = true;
+ }
+
+ public StopWatch stop() {
+ this.running = false;
+ return this;
+ }
+
+ public void pause() {
+ this.running = false;
+ currentTime = System.currentTimeMillis() - startTime;
+ }
+
+ public void resume() {
+ this.running = true;
+ this.startTime = System.currentTimeMillis() - currentTime;
+ }
+
+ //elaspsed time in milliseconds
+ public long getElapsedTimeMili() {
+ long elapsed = 0;
+ if (running) {
+ elapsed = (System.currentTimeMillis() - startTime);
+ }
+ return elapsed;
+ }
+
+ //elaspsed time in seconds
+ public long getElapsedTimeSecs() {
+ long elapsed = 0;
+ if (running) {
+ elapsed = (System.currentTimeMillis() - startTime) / 1000;
+ }
+ return elapsed;
+ }
+
+ //elaspsed time in minutes
+ public long getElapsedTimeMin() {
+ long elapsed = 0;
+ if (running) {
+ elapsed = (System.currentTimeMillis() - startTime) / 1000 / 60;
+ }
+ return elapsed;
+ }
+
+ //elaspsed time in hours
+ public long getElapsedTimeHour() {
+ long elapsed = 0;
+ if (running) {
+ elapsed = ((System.currentTimeMillis() - startTime) / 1000 / 3600);
+ }
+ return elapsed;
+ }
+}
diff --git a/src/main/java/jota/utils/TrytesConverter.java b/src/main/java/jota/utils/TrytesConverter.java
new file mode 100644
index 0000000..ab9d02a
--- /dev/null
+++ b/src/main/java/jota/utils/TrytesConverter.java
@@ -0,0 +1,91 @@
+package jota.utils;
+
+/**
+ * Created by pinpong on 01.12.16.
+ */
+public class TrytesConverter {
+
+ /**
+ * Conversion of ascii encoded bytes to trytes.
+ * Input is a string (can be stringified JSON object), return value is Trytes
+ *
+ * How the conversion works:
+ * 2 Trytes === 1 Byte
+ * There are a total of 27 different tryte values: 9ABCDEFGHIJKLMNOPQRSTUVWXYZ
+ *
+ * 1. We get the decimal value of an individual ASCII character
+ * 2. From the decimal value, we then derive the two tryte values by basically calculating the tryte equivalent (e.g. 100 === 19 + 3 * 27)
+ * a. The first tryte value is the decimal value modulo 27 (27 trytes)
+ * b. The second value is the remainder (decimal value - first value), divided by 27
+ * 3. The two values returned from Step 2. are then input as indices into the available values list ('9ABCDEFGHIJKLMNOPQRSTUVWXYZ') to get the correct tryte value
+ *
+ *
+ * EXAMPLE
+ *
+ * Lets say we want to convert the ASCII character "Z".
+ * 1. 'Z' has a decimal value of 90.
+ * 2. 90 can be represented as 9 + 3 * 27. To make it simpler:
+ * a. First value: 90 modulo 27 is 9. This is now our first value
+ * b. Second value: (90 - 9) / 27 is 3. This is our second value.
+ * 3. Our two values are now 9 and 3. To get the tryte value now we simply insert it as indices into '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ * a. The first tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[9] === "I"
+ * b. The second tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[3] === "C"
+ * Our tryte pair is "IC"
+ *
+ *
+ * @param inputString
+ * @return
+ * The ASCII char "Z" is represented as "IC" in trytes.
+ */
+ public static String toTrytes(String inputString) {
+
+ StringBuilder trytes = new StringBuilder();
+
+ for (int i = 0; i < inputString.length(); i++) {
+
+ char asciiValue = inputString.charAt(i);
+
+ // If not recognizable ASCII character, replace with space
+ if (asciiValue > 255) {
+ asciiValue = 32;
+ }
+
+ int firstValue = asciiValue % 27;
+ int secondValue = (asciiValue - firstValue) / 27;
+
+ String trytesValue = String.valueOf(Constants.TRYTE_ALPHABET.charAt(firstValue) + String.valueOf(Constants.TRYTE_ALPHABET.charAt(secondValue)));
+
+ trytes.append(trytesValue);
+ }
+
+ return trytes.toString();
+ }
+
+ /**
+ * Trytes to bytes
+ * Reverse operation from the byteToTrytes function in send.js
+ * 2 Trytes == 1 Byte
+ * We assume that the trytes are a JSON encoded object thus for our encoding:
+ * First character = {
+ * Last character = }
+ * Everything after that is 9's padding
+ */
+ public static String toString(String inputTrytes) {
+
+ StringBuilder string = new StringBuilder();
+
+ for (int i = 0; i < inputTrytes.length(); i += 2) {
+ // get a trytes pair
+
+ int firstValue = Constants.TRYTE_ALPHABET.indexOf(inputTrytes.charAt(i));
+ int secondValue = Constants.TRYTE_ALPHABET.indexOf(inputTrytes.charAt(i + 1));
+
+ int decimalValue = firstValue + secondValue * 27;
+
+ String character = Character.toString((char) decimalValue);
+ string.append(character);
+ }
+
+ return string.toString();
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/jota/ChecksumTest.java b/src/test/java/jota/ChecksumTest.java
new file mode 100644
index 0000000..1413f69
--- /dev/null
+++ b/src/test/java/jota/ChecksumTest.java
@@ -0,0 +1,30 @@
+package jota;
+
+import jota.utils.Checksum;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class ChecksumTest {
+
+ private static final String TEST_ADDRESS_WITHOUT_CHECKSUM = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVA";
+ private static final String TEST_ADDRESS_WITH_CHECKSUM = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAFOXM9MUBX";
+
+ @Test
+ public void shouldAddChecksum() {
+ assertEquals(Checksum.addChecksum(TEST_ADDRESS_WITHOUT_CHECKSUM), TEST_ADDRESS_WITH_CHECKSUM);
+ }
+
+ @Test
+ public void shouldRemoveChecksum() {
+ assertEquals(Checksum.removeChecksum(TEST_ADDRESS_WITH_CHECKSUM), TEST_ADDRESS_WITHOUT_CHECKSUM);
+ }
+
+ @Test
+ public void shouldIsValidChecksum() {
+ assertEquals(Checksum.isValidChecksum(TEST_ADDRESS_WITH_CHECKSUM), true);
+ }
+}
diff --git a/src/test/java/jota/InputValidatorTest.java b/src/test/java/jota/InputValidatorTest.java
new file mode 100644
index 0000000..b898baf
--- /dev/null
+++ b/src/test/java/jota/InputValidatorTest.java
@@ -0,0 +1,55 @@
+package jota;
+
+import jota.model.Transfer;
+import jota.utils.InputValidator;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by pinpong on 02.12.16.
+ */
+public class InputValidatorTest {
+
+ private static final String TEST_ADDRESS_WITHOUT_CHECKSUM = "PNGMCSNRCTRHCHPXYTPKEJYPCOWKOMRXZFHH9N9VDIKMNVAZCMIYRHVJIAZARZTUETJVFDMBEBIQE9QTHBFWDAOEFA";
+ private static final String TEST_ADDRESS_WITH_CHECKSUM = "PNGMCSNRCTRHCHPXYTPKEJYPCOWKOMRXZFHH9N9VDIKMNVAZCMIYRHVJIAZARZTUETJVFDMBEBIQE9QTHBFWDAOEFA";
+ private static final String TEST_TRYTES = "BYSWEAUTWXHXZ9YBZISEK9LUHWGMHXCGEVNZHRLUWQFCUSDXZHOFHWHL9MQPVJXXZLIXPXPXF9KYEREFSKCPKYIIKPZVLHUTDFQKKVVBBN9ATTLPCNPJDWDEVIYYLGPZGCWXOBDXMLJC9VO9QXTTBLAXTTBFUAROYEGQIVB9MJWJKXJMCUPTWAUGFZBTZCSJVRBGMYXTVBDDS9MYUJCPZ9YDWWQNIPUAIJXXSNLKUBSCOIJPCLEFPOXFJREXQCUVUMKSDOVQGGHRNILCO9GNCLWFM9APMNMWYASHXQAYBEXF9QRIHIBHYEJOYHRQJAOKAQ9AJJFQ9WEIWIJOTZATIBOXQLBMIJU9PCGBLVDDVFP9CFFSXTDUXMEGOOFXWRTLFGV9XXMYWEMGQEEEDBTIJ9OJOXFAPFQXCDAXOUDMLVYRMRLUDBETOLRJQAEDDLNVIRQJUBZBO9CCFDHIX9MSQCWYAXJVWHCUPTRSXJDESISQPRKZAFKFRULCGVRSBLVFOPEYLEE99JD9SEBALQINPDAZHFAB9RNBH9AZWIJOTLBZVIEJIAYGMC9AZGNFWGRSWAXTYSXVROVNKCOQQIWGPNQZKHUNODGYADPYLZZZUQRTJRTODOUKAOITNOMWNGHJBBA99QUMBHRENGBHTH9KHUAOXBVIVDVYYZMSEYSJWIOGGXZVRGN999EEGQMCOYVJQRIRROMPCQBLDYIGQO9AMORPYFSSUGACOJXGAQSPDY9YWRRPESNXXBDQ9OZOXVIOMLGTSWAMKMTDRSPGJKGBXQIVNRJRFRYEZ9VJDLHIKPSKMYC9YEGHFDS9SGVDHRIXBEMLFIINOHVPXIFAZCJKBHVMQZEVWCOSNWQRDYWVAIBLSCBGESJUIBWZECPUCAYAWMTQKRMCHONIPKJYYTEGZCJYCT9ABRWTJLRQXKMWY9GWZMHYZNWPXULNZAPVQLPMYQZCYNEPOCGOHBJUZLZDPIXVHLDMQYJUUBEDXXPXFLNRGIPWBRNQQZJSGSJTTYHIGGFAWJVXWL9THTPWOOHTNQWCNYOYZXALHAZXVMIZE9WMQUDCHDJMIBWKTYH9AC9AFOT9DPCADCV9ZWUTE9QNOMSZPTZDJLJZCJGHXUNBJFUBJWQUEZDMHXGBPTNSPZBR9TGSKVOHMOQSWPGFLSWNESFKSAZY9HHERAXALZCABFYPOVLAHMIHVDBGKUMDXC9WHHTIRYHZVWNXSVQUWCR9M9RAGMFEZZKZ9XEOQGOSLFQCHHOKLDSA9QCMDGCGMRYJZLBVIFOLBIJPROKMHOYTBTJIWUZWJMCTKCJKKTR9LCVYPVJI9AHGI9JOWMIWZAGMLDFJA9WU9QAMEFGABIBEZNNAL9OXSBFLOEHKDGHWFQSHMPLYFCNXAAZYJLMQDEYRGL9QKCEUEJ9LLVUOINVSZZQHCIKPAGMT9CAYIIMTTBCPKWTYHOJIIY9GYNPAJNUJ9BKYYXSV9JSPEXYMCFAIKTGNRSQGUNIYZCRT9FOWENSZQPD9ALUPYYAVICHVYELYFPUYDTWUSWNIYFXPX9MICCCOOZIWRNJIDALWGWRATGLJXNAYTNIZWQ9YTVDBOFZRKO9CFWRPAQQRXTPACOWCPRLYRYSJARRKSQPR9TCFXDVIXLP9XVL99ERRDSOHBFJDJQQGGGCZNDQ9NYCTQJWVZIAELCRBJJFDMCNZU9FIZRPGNURTXOCDSQGXTQHKHUECGWFUUYS9J9NYQ9U9P9UUP9YMZHWWWCIASCFLCMSKTELZWUGCDE9YOKVOVKTAYPHDF9ZCCQAYPJIJNGSHUIHHCOSSOOBUDOKE9CJZGYSSGNCQJVBEFTZFJ9SQUHOASKRRGBSHWKBCBWBTJHOGQ9WOMQFHWJVEG9NYX9KWBTCAIXNXHEBDIOFO9ALYMFGRICLCKKLG9FOBOX9PDWNQRGHBKHGKKRLWTBEQMCWQRLHAVYYZDIIPKVQTHYTWQMTOACXZOQCDTJTBAAUWXSGJF9PNQIJ9AJRUMUVCPWYVYVARKR9RKGOUHHNKNVGGPDDLGKPQNOYHNKAVVKCXWXOQPZNSLATUJT9AUWRMPPSWHSTTYDFAQDXOCYTZHOYYGAIM9CELMZ9AZPWB9MJXGHOKDNNSZVUDAGXTJJSSZCPZVPZBYNNTUQABSXQWZCHDQSLGK9UOHCFKBIBNETK999999999999999999999999999999999999999999999999999999999999999999999999999999999NOXDXXKUDWLOFJLIPQIBRBMGDYCPGDNLQOLQS99EQYKBIU9VHCJVIPFUYCQDNY9APGEVYLCENJIOBLWNB999999999XKBRHUD99C99999999NKZKEKWLDKMJCI9N9XQOLWEPAYWSH9999999999999999999999999KDDTGZLIPBNZKMLTOLOXQVNGLASESDQVPTXALEKRMIOHQLUHD9ELQDBQETS9QFGTYOYWLNTSKKMVJAUXSIROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999IROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999";
+ private static final String TEST_HASH = "OAATQS9VQLSXCLDJVJJVYUGONXAXOFMJOZNSYWRZSWECMXAQQURHQBJNLD9IOFEPGZEPEMPXCIVRX9999";
+ private static final String TEST_MESSAGE = "JOTA";
+ private static final String TEST_TAG = "JOTASPAM9999999999999999999";
+ @Test
+ public void shouldIsAddress() {
+ assertEquals(InputValidator.isAddress(TEST_ADDRESS_WITHOUT_CHECKSUM), true);
+ }
+
+ @Test
+ public void shouldCheckAddress() {
+ assertEquals(InputValidator.checkAddress(TEST_ADDRESS_WITHOUT_CHECKSUM), true);
+ }
+
+ @Test
+ public void shouldIsTrytes() {
+ assertEquals(InputValidator.isTrytes(TEST_TRYTES, TEST_TRYTES.length()), true);
+ }
+
+ @Test
+ public void shouldIsValue() {
+ assertEquals(InputValidator.isValue("1234"), true);
+ }
+
+ @Test
+ public void shouldIsArrayOfHashes() {
+ assertEquals(InputValidator.isArrayOfHashes(new String[]{TEST_HASH, TEST_HASH}), true);
+ }
+
+ @Test
+ public void shouldIsTransfersCollectionCorrect() {
+ List transfers = new ArrayList<>();
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITH_CHECKSUM, 0, TEST_MESSAGE, TEST_TAG));
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITH_CHECKSUM, 0, TEST_MESSAGE, TEST_TAG));
+ assertEquals(InputValidator.isTransfersCollectionCorrect(transfers), true);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/jota/IotaAPIProxyTest.java b/src/test/java/jota/IotaAPIProxyTest.java
deleted file mode 100644
index 5a9d1e4..0000000
--- a/src/test/java/jota/IotaAPIProxyTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-package jota;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import jota.dto.response.*;
-import jota.utils.IotaAPIUtils;
-import org.hamcrest.core.IsNull;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.junit.Assert.assertThat;
-
-/**
- * Let's do some integration test coverage against a default local real node.
- *
- * @author davassi
- */
-public class IotaAPIProxyTest {
-
- private static Gson gson = new GsonBuilder().create();
-
- private static final String TEST_SEED = "AAA999999999999999999999999999999999999999999999999999999999999999999999999999999";
- private static final String TEST_ADDRESS = "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAZETAIRPTM";
- private static final String TEST_HASH = "OAATQS9VQLSXCLDJVJJVYUGONXAXOFMJOZNSYWRZSWECMXAQQURHQBJNLD9IOFEPGZEPEMPXCIVRX9999";
- private static final String TEST_TRYTES = "BYSWEAUTWXHXZ9YBZISEK9LUHWGMHXCGEVNZHRLUWQFCUSDXZHOFHWHL9MQPVJXXZLIXPXPXF9KYEREFSKCPKYIIKPZVLHUTDFQKKVVBBN9ATTLPCNPJDWDEVIYYLGPZGCWXOBDXMLJC9VO9QXTTBLAXTTBFUAROYEGQIVB9MJWJKXJMCUPTWAUGFZBTZCSJVRBGMYXTVBDDS9MYUJCPZ9YDWWQNIPUAIJXXSNLKUBSCOIJPCLEFPOXFJREXQCUVUMKSDOVQGGHRNILCO9GNCLWFM9APMNMWYASHXQAYBEXF9QRIHIBHYEJOYHRQJAOKAQ9AJJFQ9WEIWIJOTZATIBOXQLBMIJU9PCGBLVDDVFP9CFFSXTDUXMEGOOFXWRTLFGV9XXMYWEMGQEEEDBTIJ9OJOXFAPFQXCDAXOUDMLVYRMRLUDBETOLRJQAEDDLNVIRQJUBZBO9CCFDHIX9MSQCWYAXJVWHCUPTRSXJDESISQPRKZAFKFRULCGVRSBLVFOPEYLEE99JD9SEBALQINPDAZHFAB9RNBH9AZWIJOTLBZVIEJIAYGMC9AZGNFWGRSWAXTYSXVROVNKCOQQIWGPNQZKHUNODGYADPYLZZZUQRTJRTODOUKAOITNOMWNGHJBBA99QUMBHRENGBHTH9KHUAOXBVIVDVYYZMSEYSJWIOGGXZVRGN999EEGQMCOYVJQRIRROMPCQBLDYIGQO9AMORPYFSSUGACOJXGAQSPDY9YWRRPESNXXBDQ9OZOXVIOMLGTSWAMKMTDRSPGJKGBXQIVNRJRFRYEZ9VJDLHIKPSKMYC9YEGHFDS9SGVDHRIXBEMLFIINOHVPXIFAZCJKBHVMQZEVWCOSNWQRDYWVAIBLSCBGESJUIBWZECPUCAYAWMTQKRMCHONIPKJYYTEGZCJYCT9ABRWTJLRQXKMWY9GWZMHYZNWPXULNZAPVQLPMYQZCYNEPOCGOHBJUZLZDPIXVHLDMQYJUUBEDXXPXFLNRGIPWBRNQQZJSGSJTTYHIGGFAWJVXWL9THTPWOOHTNQWCNYOYZXALHAZXVMIZE9WMQUDCHDJMIBWKTYH9AC9AFOT9DPCADCV9ZWUTE9QNOMSZPTZDJLJZCJGHXUNBJFUBJWQUEZDMHXGBPTNSPZBR9TGSKVOHMOQSWPGFLSWNESFKSAZY9HHERAXALZCABFYPOVLAHMIHVDBGKUMDXC9WHHTIRYHZVWNXSVQUWCR9M9RAGMFEZZKZ9XEOQGOSLFQCHHOKLDSA9QCMDGCGMRYJZLBVIFOLBIJPROKMHOYTBTJIWUZWJMCTKCJKKTR9LCVYPVJI9AHGI9JOWMIWZAGMLDFJA9WU9QAMEFGABIBEZNNAL9OXSBFLOEHKDGHWFQSHMPLYFCNXAAZYJLMQDEYRGL9QKCEUEJ9LLVUOINVSZZQHCIKPAGMT9CAYIIMTTBCPKWTYHOJIIY9GYNPAJNUJ9BKYYXSV9JSPEXYMCFAIKTGNRSQGUNIYZCRT9FOWENSZQPD9ALUPYYAVICHVYELYFPUYDTWUSWNIYFXPX9MICCCOOZIWRNJIDALWGWRATGLJXNAYTNIZWQ9YTVDBOFZRKO9CFWRPAQQRXTPACOWCPRLYRYSJARRKSQPR9TCFXDVIXLP9XVL99ERRDSOHBFJDJQQGGGCZNDQ9NYCTQJWVZIAELCRBJJFDMCNZU9FIZRPGNURTXOCDSQGXTQHKHUECGWFUUYS9J9NYQ9U9P9UUP9YMZHWWWCIASCFLCMSKTELZWUGCDE9YOKVOVKTAYPHDF9ZCCQAYPJIJNGSHUIHHCOSSOOBUDOKE9CJZGYSSGNCQJVBEFTZFJ9SQUHOASKRRGBSHWKBCBWBTJHOGQ9WOMQFHWJVEG9NYX9KWBTCAIXNXHEBDIOFO9ALYMFGRICLCKKLG9FOBOX9PDWNQRGHBKHGKKRLWTBEQMCWQRLHAVYYZDIIPKVQTHYTWQMTOACXZOQCDTJTBAAUWXSGJF9PNQIJ9AJRUMUVCPWYVYVARKR9RKGOUHHNKNVGGPDDLGKPQNOYHNKAVVKCXWXOQPZNSLATUJT9AUWRMPPSWHSTTYDFAQDXOCYTZHOYYGAIM9CELMZ9AZPWB9MJXGHOKDNNSZVUDAGXTJJSSZCPZVPZBYNNTUQABSXQWZCHDQSLGK9UOHCFKBIBNETK999999999999999999999999999999999999999999999999999999999999999999999999999999999NOXDXXKUDWLOFJLIPQIBRBMGDYCPGDNLQOLQS99EQYKBIU9VHCJVIPFUYCQDNY9APGEVYLCENJIOBLWNB999999999XKBRHUD99C99999999NKZKEKWLDKMJCI9N9XQOLWEPAYWSH9999999999999999999999999KDDTGZLIPBNZKMLTOLOXQVNGLASESDQVPTXALEKRMIOHQLUHD9ELQDBQETS9QFGTYOYWLNTSKKMVJAUXSIROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999IROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999";
- private static final String TEST_MILESTONE = "SMYMAKKPSUKCKDRUEYCGZJTYCZ9HHDMDUWBAPXARGURPQRHTAJDASRWMIDTPTBNDKDEFBUTBGGAFX9999";
- private static final Integer TEST_MILESTONE_INDEX = 8059;
-
- private IotaAPIProxy proxy;
-
- @Before
- public void createProxyInstance() {
- proxy = new IotaAPIProxy.Builder().build();
- }
-
- @Test
- public void shouldGetNodeInfo() {
- GetNodeInfoResponse nodeInfo = proxy.getNodeInfo();
- assertThat(nodeInfo.getAppVersion(), IsNull.notNullValue());
- }
-
- @Test
- public void shouldGetNeighbors() {
- GetNeighborsResponse neighbors = proxy.getNeighbors();
- assertThat(neighbors.getNeighbors(), IsNull.notNullValue());
- }
-
- @Test
- public void shouldAddNeighbors() {
- AddNeighborsResponse res = proxy.addNeighbors("udp://8.8.8.8:14265");
- assertThat(res, IsNull.notNullValue());
- }
-
- @Test
- public void shouldRemoveNeighbors() {
- RemoveNeighborsResponse res = proxy.removeNeighbors("udp://8.8.8.8:14265");
- assertThat(res, IsNull.notNullValue());
- }
-
- @Test
- public void shouldGetTips() {
- GetTipsResponse tips = proxy.getTips();
- assertThat(tips, IsNull.notNullValue());
- }
-
- @Test
- public void shouldFindTransactionsByAddresses() {
- FindTransactionResponse trans = proxy.findTransactionsByAddresses(TEST_ADDRESS);
- System.err.println(gson.toJson(trans));
- assertThat(trans, IsNull.notNullValue());
- }
-
- @Test
- public void shouldFindTransactionsByApprovees() {
- FindTransactionResponse trans = proxy.findTransactionsByApprovees(new String[]{"123ABC"});
- assertThat(trans, IsNull.notNullValue());
- }
-
- @Test
- public void shouldFindTransactionsByBundles() {
- FindTransactionResponse trans = proxy.findTransactionsByBundles(new String[]{"123ABC"});
- assertThat(trans, IsNull.notNullValue());
- }
-
- @Test
- public void shouldFindTransactionsByDigests() {
- FindTransactionResponse trans = proxy.findTransactionsByDigests(new String[]{"123ABC"});
- assertThat(trans, IsNull.notNullValue());
- }
-
-
- // ###
-
- @Test
- public void shouldGetTrytes() {
- GetTrytesResponse res = proxy.getTrytes(TEST_HASH);
- assertThat(res, IsNull.nullValue());
- }
-
- @Test
- public void shouldGetInclusionStates() {
- GetInclusionStateResponse res = proxy.getInclusionStates(new String[]{TEST_ADDRESS},
- new String[]{"123"});
- assertThat(res, IsNull.notNullValue());
- }
-
- @Test // very long execution
- public void shouldGetTransactionsToApprove() {
- GetTransactionsToApproveResponse res = proxy.getTransactionsToApprove(27);
- assertThat(res, IsNull.notNullValue());
- }
-
- @Test
- public void shouldGetBalances() {
- GetBalancesResponse res = proxy.getBalances(100, new String[]{"HBBYKAKTILIPVUKFOTSLHGENPTXYBNKXZFQFR9VQFWNBMTQNRVOUKPVPRNBSZVVILMAFBKOTBLGLWLOHQ"});
- System.err.println(res);
- assertThat(res, IsNull.notNullValue());
- }
-
- @Test
- public void shouldCreateIotaApiProxyInstanceWithDefaultValues() {
- IotaAPIProxy proxy = new IotaAPIProxy.Builder().build();
- assertThat(proxy, IsNull.notNullValue());
- }
-
- @Test
- public void shouldCreateANewAddress() {
- GetNewAddressResponse res = IotaAPIUtils.getNewAddress(TEST_SEED, 2);
- System.err.println(res);
- }
-
-}
\ No newline at end of file
diff --git a/src/test/java/jota/IotaAPITest.java b/src/test/java/jota/IotaAPITest.java
new file mode 100644
index 0000000..4ef7c92
--- /dev/null
+++ b/src/test/java/jota/IotaAPITest.java
@@ -0,0 +1,162 @@
+package jota;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import jota.dto.response.*;
+import jota.error.*;
+import jota.model.Bundle;
+import jota.model.Transaction;
+import jota.model.Transfer;
+import org.hamcrest.core.Is;
+import org.hamcrest.core.IsNull;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertThat;
+
+/**
+ * Let's do some integration test coverage against a default local real node.
+ *
+ * @author davassi
+ */
+public class IotaAPITest {
+
+ private static Gson gson = new GsonBuilder().create();
+
+ private static final String TEST_SEED1 = "AAA999999999999999999999999999999999999999999999999999999999999999999999999999999";
+ private static final String TEST_SEED2 = "IHDEENZYITYVYSPKAURUZAQKGVJEREFDJMYTANNXXGPZ9GJWTEOJJ9IPMXOGZNQLSNMFDSQOTZAEETUEA";
+ private static final String TEST_ADDRESS_WITHOUT_CHECKSUM = "PNGMCSNRCTRHCHPXYTPKEJYPCOWKOMRXZFHH9N9VDIKMNVAZCMIYRHVJIAZARZTUETJVFDMBEBIQE9QTH";
+ private static final String TEST_ADDRESS_WITH_CHECKSUM = "PNGMCSNRCTRHCHPXYTPKEJYPCOWKOMRXZFHH9N9VDIKMNVAZCMIYRHVJIAZARZTUETJVFDMBEBIQE9QTHBFWDAOEFA";
+ private static final String TEST_HASH = "CKZ9TYPLUWH9FUSYJMPIZBVHWFZXTZMVOJLC9KOICSTBBQWXYTOTMCVPSPMYNDONTXHRULRFAWD999999";
+ private static final String TEST_TRYTES = "BYSWEAUTWXHXZ9YBZISEK9LUHWGMHXCGEVNZHRLUWQFCUSDXZHOFHWHL9MQPVJXXZLIXPXPXF9KYEREFSKCPKYIIKPZVLHUTDFQKKVVBBN9ATTLPCNPJDWDEVIYYLGPZGCWXOBDXMLJC9VO9QXTTBLAXTTBFUAROYEGQIVB9MJWJKXJMCUPTWAUGFZBTZCSJVRBGMYXTVBDDS9MYUJCPZ9YDWWQNIPUAIJXXSNLKUBSCOIJPCLEFPOXFJREXQCUVUMKSDOVQGGHRNILCO9GNCLWFM9APMNMWYASHXQAYBEXF9QRIHIBHYEJOYHRQJAOKAQ9AJJFQ9WEIWIJOTZATIBOXQLBMIJU9PCGBLVDDVFP9CFFSXTDUXMEGOOFXWRTLFGV9XXMYWEMGQEEEDBTIJ9OJOXFAPFQXCDAXOUDMLVYRMRLUDBETOLRJQAEDDLNVIRQJUBZBO9CCFDHIX9MSQCWYAXJVWHCUPTRSXJDESISQPRKZAFKFRULCGVRSBLVFOPEYLEE99JD9SEBALQINPDAZHFAB9RNBH9AZWIJOTLBZVIEJIAYGMC9AZGNFWGRSWAXTYSXVROVNKCOQQIWGPNQZKHUNODGYADPYLZZZUQRTJRTODOUKAOITNOMWNGHJBBA99QUMBHRENGBHTH9KHUAOXBVIVDVYYZMSEYSJWIOGGXZVRGN999EEGQMCOYVJQRIRROMPCQBLDYIGQO9AMORPYFSSUGACOJXGAQSPDY9YWRRPESNXXBDQ9OZOXVIOMLGTSWAMKMTDRSPGJKGBXQIVNRJRFRYEZ9VJDLHIKPSKMYC9YEGHFDS9SGVDHRIXBEMLFIINOHVPXIFAZCJKBHVMQZEVWCOSNWQRDYWVAIBLSCBGESJUIBWZECPUCAYAWMTQKRMCHONIPKJYYTEGZCJYCT9ABRWTJLRQXKMWY9GWZMHYZNWPXULNZAPVQLPMYQZCYNEPOCGOHBJUZLZDPIXVHLDMQYJUUBEDXXPXFLNRGIPWBRNQQZJSGSJTTYHIGGFAWJVXWL9THTPWOOHTNQWCNYOYZXALHAZXVMIZE9WMQUDCHDJMIBWKTYH9AC9AFOT9DPCADCV9ZWUTE9QNOMSZPTZDJLJZCJGHXUNBJFUBJWQUEZDMHXGBPTNSPZBR9TGSKVOHMOQSWPGFLSWNESFKSAZY9HHERAXALZCABFYPOVLAHMIHVDBGKUMDXC9WHHTIRYHZVWNXSVQUWCR9M9RAGMFEZZKZ9XEOQGOSLFQCHHOKLDSA9QCMDGCGMRYJZLBVIFOLBIJPROKMHOYTBTJIWUZWJMCTKCJKKTR9LCVYPVJI9AHGI9JOWMIWZAGMLDFJA9WU9QAMEFGABIBEZNNAL9OXSBFLOEHKDGHWFQSHMPLYFCNXAAZYJLMQDEYRGL9QKCEUEJ9LLVUOINVSZZQHCIKPAGMT9CAYIIMTTBCPKWTYHOJIIY9GYNPAJNUJ9BKYYXSV9JSPEXYMCFAIKTGNRSQGUNIYZCRT9FOWENSZQPD9ALUPYYAVICHVYELYFPUYDTWUSWNIYFXPX9MICCCOOZIWRNJIDALWGWRATGLJXNAYTNIZWQ9YTVDBOFZRKO9CFWRPAQQRXTPACOWCPRLYRYSJARRKSQPR9TCFXDVIXLP9XVL99ERRDSOHBFJDJQQGGGCZNDQ9NYCTQJWVZIAELCRBJJFDMCNZU9FIZRPGNURTXOCDSQGXTQHKHUECGWFUUYS9J9NYQ9U9P9UUP9YMZHWWWCIASCFLCMSKTELZWUGCDE9YOKVOVKTAYPHDF9ZCCQAYPJIJNGSHUIHHCOSSOOBUDOKE9CJZGYSSGNCQJVBEFTZFJ9SQUHOASKRRGBSHWKBCBWBTJHOGQ9WOMQFHWJVEG9NYX9KWBTCAIXNXHEBDIOFO9ALYMFGRICLCKKLG9FOBOX9PDWNQRGHBKHGKKRLWTBEQMCWQRLHAVYYZDIIPKVQTHYTWQMTOACXZOQCDTJTBAAUWXSGJF9PNQIJ9AJRUMUVCPWYVYVARKR9RKGOUHHNKNVGGPDDLGKPQNOYHNKAVVKCXWXOQPZNSLATUJT9AUWRMPPSWHSTTYDFAQDXOCYTZHOYYGAIM9CELMZ9AZPWB9MJXGHOKDNNSZVUDAGXTJJSSZCPZVPZBYNNTUQABSXQWZCHDQSLGK9UOHCFKBIBNETK999999999999999999999999999999999999999999999999999999999999999999999999999999999NOXDXXKUDWLOFJLIPQIBRBMGDYCPGDNLQOLQS99EQYKBIU9VHCJVIPFUYCQDNY9APGEVYLCENJIOBLWNB999999999XKBRHUD99C99999999NKZKEKWLDKMJCI9N9XQOLWEPAYWSH9999999999999999999999999KDDTGZLIPBNZKMLTOLOXQVNGLASESDQVPTXALEKRMIOHQLUHD9ELQDBQETS9QFGTYOYWLNTSKKMVJAUXSIROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999IROUICDOXKSYZTDPEDKOQENTJOWJONDEWROCEJIEWFWLUAACVSJFTMCHHXJBJRKAAPUDXXVXFWP9X9999";
+ private static final String TEST_MILESTONE = "SMYMAKKPSUKCKDRUEYCGZJTYCZ9HHDMDUWBAPXARGURPQRHTAJDASRWMIDTPTBNDKDEFBUTBGGAFX9999";
+ private static final Integer TEST_MILESTONE_INDEX = 8059;
+ private static final String TEST_MESSAGE = "JOTA";
+ private static final String TEST_TAG = "JOTASPAM9999999999999999999";
+ private static final String[] TEST_ADDRESSES = new String[]{"KHJXD9XKXPIVQRGREUIPVJTMEY9L9MXZAAKBBRYNINTIOXWBRMNLLW9MLGAXMGQWDBZLCOGFCBNKTDLDC"
+ , "MQSAAEDPMKIAJPPGNLIHPQIVFGLHNEGG9JNMSHOQSVQHBQBMLNHY9WVRTCOYUOWOIJHBQXIQVFPDF9YRW"
+ , "RGYOHMECRNVPYYPIAKEWHSOLBYOQPRFRPOJGHUMEGLICCUIPTZEXLWDLLPBNRXONUTQGLSAJSLHRXFVQD"
+ , "FOJHXRVJRFMJTFDUWJYYZXCZIJXKQALLXMLKHZFDMHWTIBBXUKSNSUYJLKYRQBNXKRSUXZHDTPWXYD9YF"
+ , "B9YNPQO9EXID9RDEEGLCBJBYKBLWHTOQOZKTLJDFPJZOPKJJTNUYUVVTDJPBCBYIWGPSCMNRZFGFHFSXH"
+ , "NQEFOAFIYKZOUXDFQ9X9PHCNSDETRTJZINZ9EYGKU99QJLDSTSC9VTBAA9FHLNLNYQXWLTNPRJDWCGIPP"
+ , "CEGLBSXDJVXGKGOUHRGMAQDRVYXCQLXBKUDWKFFSIABCUYRATFPTEEDIFYGAASKFZYREHLBIXBTKP9KLC"
+ , "QLOXU9GIQXPPE9UUT9DSIDSIESRIXMTGZJMKLSJTNBCRELAVLWVJLUOLKGFCWAEPEQWZWPBV9YZJJEHUS"
+ , "XIRMYJSGQXMM9YPHJVVLAVGBBLEEMOOKHHBFWKEAXJFONZLNSLBCGPQEVDMMOGHFVRDSYTETIFOIVNCR9"
+ , "PDVVBYBXMHZKADPAYOKQNDPHRSWTHAWQ9GRVIBOIMZQTYCWEPCDWDVRSOUNASVBDLBOAMVLYEVVCMAM9N"
+ , "U9GAIAPUUQWJGISAZWPLHUELTZ9WSHWXS9JLPKOWHRRIVUKGWCTJMBULVMKTETTUNHZ9HWHBALUCJIROU"
+ , "VFPMKZLLMDUOEKNBEKQZPTNZJZF9UHRWSTHXLWQQ9OAXTZQHTZPAWNJNXKAZFSDFWKFQEKZIGJTLWQFLO"
+ , "IGHK9XIWOAYBZUEZHQLEXBPTXSWVANIOUZZCPNKUIJIJOJNAQCJWUJHYKCZOIKVAAHDGAWJZKLTPVQL9G"
+ , "LXQPWMNXSUZTEYNC9ZBBFHY9YWCCOVKBNIIOUSVXZJZMJKJFDUWGUVXYCHGKUHEEIDHSGEWFAHVJPRIJT"
+ , "AKFDX9PGGQLZUWRMZ9YBDF9CG9TWXCNALCSXSAWHFIMGXCSYCJLSWIQDGGVDRMNEKKECQEYAITGNLNJFQ"
+ , "YX9QSPYMSFVOW9UVZRDVOCPYYMUTDHCCPKHMXQSJQJYIXVCHILKW9GBYJTYGLIKBTRQMDCYBMLLNGSSIK"
+ , "DSYCJKNG9TAGJHSKZQ9XLKAKNSKJFZIPVEDGJFXRTFGENHZFQGXHWDBNXLLDABDMOYELPG9DIXSNJFWAR"
+ , "9ANNACZYLDDPZILLQBQG9YMG9XJUMTAENDFQ9HMSSEFWYOAXPJTUXBFTSAXDJPAO9FKTWBBSCSFMOUR9I"
+ , "WDTFFXHBHMFQQVXQLBFJFVVHVIIAVYM9PFAZCHMKET9ESMHIRHSMVDJBZTXPTAFVIASMSXRDCIYVWVQNO"
+ , "XCCPS9GMTSUB9DXPVKLTBDHOFX9PJMBYZQYQEXMRQDPGQPLWRGZGXODYJKGVFOHHYUJRCSXAIDGYSAWRB"
+ , "KVEBCGMEOPDPRCQBPIEMZTTXYBURGZVNH9PLHKPMM9D9FUKWIGLKZROGNSYIFHULLWQWXCNAW9HKKVIDC"};
+
+ private IotaAPI iotaClient;
+
+ @Before
+ public void createApiClientInstance() {
+ iotaClient = new IotaAPI.Builder().build();
+ }
+
+ @Test
+ public void shouldCreateIotaApiProxyInstanceWithDefaultValues() {
+ IotaAPI proxy = new IotaAPI.Builder().build();
+ assertThat(proxy, IsNull.notNullValue());
+ }
+
+
+ @Test
+ public void shouldGetInputs() {
+ GetBalancesAndFormatResponse res = iotaClient.getInputs(TEST_SEED1, 0, 0, 0);
+ System.out.println(res);
+ assertThat(res, IsNull.notNullValue());
+ assertThat(res.getTotalBalance(), IsNull.notNullValue());
+ assertThat(res.getInput(), IsNull.notNullValue());
+
+ }
+
+
+ @Test
+ public void shouldCreateANewAddress() {
+ final GetNewAddressResponse res = iotaClient.getNewAddress(TEST_SEED1, 0, false, 100, false);
+ assertThat(res.getAddresses().get(0), Is.is(TEST_ADDRESS_WITHOUT_CHECKSUM));
+ System.out.println(new Gson().toJson(res));
+ }
+
+ @Test
+ public void shouldPrepareTransfer() {
+ List transfers = new ArrayList<>();
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITH_CHECKSUM, 0, TEST_MESSAGE, TEST_TAG));
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITH_CHECKSUM, 1, TEST_MESSAGE, TEST_TAG));
+ List trytes = null;
+ try {
+ trytes = iotaClient.prepareTransfers(TEST_SEED1, transfers, null, null);
+ } catch (NotEnoughBalanceException e) {
+ e.printStackTrace();
+ }
+ Assert.assertNotNull(trytes);
+ assertThat(trytes.isEmpty(), Is.is(false));
+ }
+
+ @Test
+ public void shouldGetLastInclusionState() throws NoNodeInfoException {
+ GetInclusionStateResponse res = iotaClient.getLatestInclusion(new String[]{TEST_HASH});
+ assertThat(res.getStates(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactionObjects() {
+ List ftr = iotaClient.findTransactionObjects(TEST_ADDRESSES);
+ assertThat(ftr, IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldGetBundle() throws InvalidBundleException, ArgumentException, InvalidSignatureException {
+ GetBundleResponse gbr = iotaClient.getBundle(TEST_HASH);
+ assertThat(gbr, IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldGetTransfers() throws InvalidBundleException, ArgumentException, InvalidSignatureException, NoInclusionStatesExcpection, NoNodeInfoException {
+ GetTransferResponse gtr = iotaClient.getTransfers(TEST_SEED1, 0, 0, false);
+ assertThat(gtr.getTransfers(), IsNull.notNullValue());
+
+ for (Bundle test : gtr.getTransfers()) {
+ for (Transaction trx : test.getTransactions()) {
+ System.out.println(new Gson().toJson(trx));
+ }
+ }
+ }
+
+ @Ignore
+ @Test
+ public void shouldSendTrytes() {
+ iotaClient.sendTrytes(new String[]{TEST_TRYTES}, 9, 18);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldNotSendTransfer() throws ArgumentException, InvalidSignatureException, InvalidBundleException, NotEnoughBalanceException {
+ List transfers = new ArrayList<>();
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITHOUT_CHECKSUM, 10000990, "JUSTANOTHERTEST", TEST_TAG));
+ SendTransferResponse str = iotaClient.sendTransfer(TEST_SEED2, 9, 18, transfers, null, null);
+ assertThat(str.getSuccessfully(), IsNull.notNullValue());
+ }
+
+ @Ignore
+ @Test
+ public void shouldSendTransfer() throws ArgumentException, InvalidSignatureException, InvalidBundleException, NotEnoughBalanceException {
+ List transfers = new ArrayList<>();
+ transfers.add(new jota.model.Transfer(TEST_ADDRESS_WITHOUT_CHECKSUM, 0, "JUSTANOTHERTEST", TEST_TAG));
+ SendTransferResponse str = iotaClient.sendTransfer(TEST_SEED2, 9, 18, transfers, null, null);
+ assertThat(str.getSuccessfully(), IsNull.notNullValue());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/jota/IotaCoreApiTest.java b/src/test/java/jota/IotaCoreApiTest.java
new file mode 100644
index 0000000..8913816
--- /dev/null
+++ b/src/test/java/jota/IotaCoreApiTest.java
@@ -0,0 +1,138 @@
+package jota;
+
+import com.google.gson.Gson;
+import jota.dto.response.*;
+import org.hamcrest.core.IsNull;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertThat;
+
+/**
+ * Created by Adrian on 15.01.2017.
+ */
+public class IotaCoreApiTest {
+
+ private static final String TEST_BUNDLE = "XZKJUUMQOYUQFKMWQZNTFMSS9FKJLOEV9DXXXWPMQRTNCOUSUQNTBIJTVORLOQPLYZOTMLFRHYKMTGZZU";
+ private static final String TEST_ADDRESS_WITH_CHECKSUM = "PNGMCSNRCTRHCHPXYTPKEJYPCOWKOMRXZFHH9N9VDIKMNVAZCMIYRHVJIAZARZTUETJVFDMBEBIQE9QTHBFWDAOEFA";
+ private static final String TEST_HASH = "OAATQS9VQLSXCLDJVJJVYUGONXAXOFMJOZNSYWRZSWECMXAQQURHQBJNLD9IOFEPGZEPEMPXCIVRX9999";
+ private static IotaAPICore proxy;
+
+ @Before
+ public void createProxyInstance() {
+ proxy = new IotaAPICore.Builder().build();
+ }
+
+ @Test
+ public void shouldGetNodeInfo() {
+ GetNodeInfoResponse nodeInfo = proxy.getNodeInfo();
+ System.out.println(new Gson().toJson(nodeInfo));
+ assertThat(nodeInfo.getAppVersion(), IsNull.notNullValue());
+ assertThat(nodeInfo.getAppName(), IsNull.notNullValue());
+ assertThat(nodeInfo.getJreVersion(), IsNull.notNullValue());
+ assertThat(nodeInfo.getJreAvailableProcessors(), IsNull.notNullValue());
+ assertThat(nodeInfo.getJreFreeMemory(), IsNull.notNullValue());
+ assertThat(nodeInfo.getJreMaxMemory(), IsNull.notNullValue());
+ assertThat(nodeInfo.getJreTotalMemory(), IsNull.notNullValue());
+ assertThat(nodeInfo.getLatestMilestone(), IsNull.notNullValue());
+ assertThat(nodeInfo.getLatestMilestoneIndex(), IsNull.notNullValue());
+ assertThat(nodeInfo.getLatestSolidSubtangleMilestone(), IsNull.notNullValue());
+ assertThat(nodeInfo.getLatestSolidSubtangleMilestoneIndex(), IsNull.notNullValue());
+ assertThat(nodeInfo.getNeighbors(), IsNull.notNullValue());
+ assertThat(nodeInfo.getPacketsQueueSize(), IsNull.notNullValue());
+ assertThat(nodeInfo.getTime(), IsNull.notNullValue());
+ assertThat(nodeInfo.getTips(), IsNull.notNullValue());
+ assertThat(nodeInfo.getTransactionsToRequest(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldGetNeighbors() {
+ GetNeighborsResponse neighbors = proxy.getNeighbors();
+ assertThat(neighbors.getNeighbors(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldAddNeighbors() {
+ AddNeighborsResponse res = proxy.addNeighbors("udp://8.8.8.8:14265");
+ assertThat(res, IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldRemoveNeighbors() {
+ RemoveNeighborsResponse res = proxy.removeNeighbors("udp://8.8.8.8:14265");
+ assertThat(res, IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldGetTips() {
+ GetTipsResponse tips = proxy.getTips();
+ assertThat(tips, IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactionsByAddresses() {
+ FindTransactionResponse trans = proxy.findTransactionsByAddresses(TEST_ADDRESS_WITH_CHECKSUM);
+ assertThat(trans.getHashes(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactionsByApprovees() {
+ FindTransactionResponse trans = proxy.findTransactionsByApprovees(new String[]{TEST_HASH});
+ assertThat(trans.getHashes(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactionsByBundles() {
+ FindTransactionResponse trans = proxy.findTransactionsByBundles(TEST_HASH);
+ assertThat(trans.getHashes(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactionsByDigests() {
+ FindTransactionResponse trans = proxy.findTransactionsByDigests(TEST_HASH);
+ assertThat(trans.getHashes(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldGetTrytes() {
+ GetTrytesResponse res = proxy.getTrytes(TEST_HASH);
+ assertThat(res.getTrytes(), IsNull.notNullValue());
+
+ }
+
+ @Test
+ public void shouldGetInclusionStates() {
+ GetInclusionStateResponse res = proxy.getInclusionStates(new String[]{"DBPECSH9YLSSTQDGERUHJBBJTKVUDBMTJLG9WPHBINGHIFOSJMDJLARTVOXXWEFQJLLBINOHCZGYFSMUEXWPPMTOFW"}, new String[]{"EJDQOQHMLJGBMFWB9WJSPRCYIGNPO9WRHDCEQXIMPVPIJ9JV9RJGVHNX9EPGXFOOKBABCVMMAAX999999"});
+ assertThat(res.getStates(), IsNull.notNullValue());
+ }
+
+ @Test(expected = IllegalAccessError.class)
+ public void shouldNotGetInclusionStates() {
+ GetInclusionStateResponse res = proxy.getInclusionStates(new String[]{TEST_ADDRESS_WITH_CHECKSUM}, new String[]{"DNSBRJWNOVUCQPILOQIFDKBFJMVOTGHLIMLLRXOHFTJZGRHJUEDAOWXQRYGDI9KHYFGYDWQJZKX999999"});
+ assertThat(res.getStates(), IsNull.notNullValue());
+ }
+
+ @Test // very long execution
+ public void shouldGetTransactionsToApprove() {
+ GetTransactionsToApproveResponse res = proxy.getTransactionsToApprove(27);
+ assertThat(res.getTrunkTransaction(), IsNull.notNullValue());
+ assertThat(res.getBranchTransaction(), IsNull.notNullValue());
+ }
+
+ @Test
+ public void shouldFindTransactions() {
+ String test = TEST_BUNDLE;
+ FindTransactionResponse resp = proxy.findTransactions(new String[]{test}, new String[]{test}, new String[]{test}, new String[]{test});
+ System.out.println(new Gson().toJson(resp));
+ }
+
+ @Test
+ public void shouldGetBalances() {
+ GetBalancesResponse res = proxy.getBalances(100, new String[]{TEST_ADDRESS_WITH_CHECKSUM});
+ System.err.println(res);
+ assertThat(res.getBalances(), IsNull.notNullValue());
+ assertThat(res.getMilestone(), IsNull.notNullValue());
+ assertThat(res.getMilestoneIndex(), IsNull.notNullValue());
+
+ }
+}
diff --git a/src/test/java/jota/IotaUnitConverterTest.java b/src/test/java/jota/IotaUnitConverterTest.java
index 5078ac5..32976a6 100644
--- a/src/test/java/jota/IotaUnitConverterTest.java
+++ b/src/test/java/jota/IotaUnitConverterTest.java
@@ -35,4 +35,24 @@ public class IotaUnitConverterTest {
public void shouldConvertUnitTiToPi() {
assertEquals(IotaUnitConverter.convertUnits(1000, IotaUnits.TERA_IOTA, IotaUnits.PETA_IOTA), 1);
}
+
+ @Test
+ public void shouldFindOptimizeUnitToDisplay() {
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1), IotaUnits.IOTA);
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1000), IotaUnits.KILO_IOTA);
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1000000), IotaUnits.MEGA_IOTA);
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1000000000), IotaUnits.GIGA_IOTA);
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1000000000000L), IotaUnits.TERA_IOTA);
+ assertEquals(IotaUnitConverter.findOptimalIotaUnitToDisplay(1000000000000000L), IotaUnits.PETA_IOTA);
+ }
+
+ @Test
+ public void shouldConvertRawIotaAmountToDisplayText() {
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1,false), "1 i");
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1000,false), "1 Ki");
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1000000,false), "1 Mi" );
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1000000000,false), "1 Gi" );
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1000000000000L,false), "1 Ti");
+ assertEquals(IotaUnitConverter.convertRawIotaAmountToDisplayText(1000000000000000L,false), "1 Pi");
+ }
}
diff --git a/src/test/java/jota/SeedRandomGeneratorTest.java b/src/test/java/jota/SeedRandomGeneratorTest.java
new file mode 100644
index 0000000..b2ab6d2
--- /dev/null
+++ b/src/test/java/jota/SeedRandomGeneratorTest.java
@@ -0,0 +1,22 @@
+package jota;
+
+import jota.utils.Constants;
+import jota.utils.InputValidator;
+import jota.utils.SeedRandomGenerator;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by pinpong on 13.12.16.
+ */
+public class SeedRandomGeneratorTest {
+
+ @Test
+ public void shouldGenerateNewSeed() {
+
+ String generatedSeed = SeedRandomGenerator.generateNewSeed();
+ assertEquals(InputValidator.isAddress(generatedSeed), true);
+ assertEquals(generatedSeed.length(), Constants.SEED_LENGTH_MAX);
+ }
+}
diff --git a/src/test/java/jota/SendMessageTest.java b/src/test/java/jota/SendMessageTest.java
new file mode 100644
index 0000000..9a62c04
--- /dev/null
+++ b/src/test/java/jota/SendMessageTest.java
@@ -0,0 +1,12 @@
+package jota;
+
+import org.junit.Test;
+
+public class SendMessageTest {
+
+ @Test
+ public void sendMessage() {
+
+ }
+
+}
diff --git a/src/test/java/jota/TrytesConverterTest.java b/src/test/java/jota/TrytesConverterTest.java
new file mode 100644
index 0000000..f6403e5
--- /dev/null
+++ b/src/test/java/jota/TrytesConverterTest.java
@@ -0,0 +1,35 @@
+package jota;
+
+import jota.utils.TrytesConverter;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Created by pinpong on 01.12.16.
+ */
+public class TrytesConverterTest {
+
+ @Test
+ public void shouldConvertStringToTrytes() {
+ assertEquals(TrytesConverter.toTrytes("Z"), "IC");
+ assertEquals(TrytesConverter.toTrytes("JOTA JOTA"), "TBYBCCKBEATBYBCCKB");
+ }
+
+ @Test
+ public void shouldConvertTrytesToString() {
+ assertEquals(TrytesConverter.toString("IC"), "Z");
+ assertEquals(TrytesConverter.toString("TBYBCCKBEATBYBCCKB"), "JOTA JOTA");
+ }
+
+ @Test
+ public void shouldConvertBackAndForth() {
+ String str = RandomStringUtils.randomAlphabetic(1000).toUpperCase();
+ System.err.println(str);
+ String back = TrytesConverter.toString(TrytesConverter.toTrytes(str));
+
+ assertTrue(str.equals(back));
+ }
+}
\ No newline at end of file
diff --git a/teststore.txt b/teststore.txt
new file mode 100644
index 0000000..833978f
--- /dev/null
+++ b/teststore.txt
@@ -0,0 +1 @@
+curl http://localhost:14265 -X POST -H Content-Type: application/json -d {'command': 'storeTransactions', 'trytes': ['GYPRVHBEZOOFXSHQBLCYW9ICTCISLHDBNMMVYD9JJHQMPQCTIQAQTJNNNJ9IDXLRCCOYOXYPCLR9PBEY9ORZIEPPDNTI9CQWYZUOTAVBXPSBOFEQAPFLWXSWUIUSJMSJIIIZWIKIRH9GCOEVZFKNXEVCUCIIWZQCQEUVRZOCMEL9AMGXJNMLJCIA9UWGRPPHCEOPTSVPKPPPCMQXYBHMSODTWUOABPKWFFFQJHCBVYXLHEWPD9YUDFTGNCYAKQKVEZYRBQRBXIAUX9SVEDUKGMTWQIYXRGSWYRK9SRONVGTW9YGHSZRIXWGPCCUCDRMAXBPDFVHSRYWHGB9DQSQFQKSNICGPIPTRZINYRXQAFSWSEWIFRMSBMGTNYPRWFSOIIWWT9IDSELM9JUOOWFNCCSHUSMGNROBFJX9JQ9XT9PKEGQYQAWAFPRVRRVQPUQBHLSNTEFCDKBWRCDX9EYOBB9KPMTLNNQLADBDLZPRVBCKVCYQEOLARJYAGTBFR9QLPKZBOYWZQOVKCVYRGYI9ZEFIQRKYXLJBZJDBJDJVQZCGYQMROVHNDBLGNLQODPUXFNTADDVYNZJUVPGB9LVPJIYLAPBOEHPMRWUIAJXVQOEM9ROEYUOTNLXVVQEYRQWDTQGDLEYFIYNDPRAIXOZEBCS9P99AZTQQLKEILEVXMSHBIDHLXKUOMMNFKPYHONKEYDCHMUNTTNRYVMMEYHPGASPZXASKRUPWQSHDMU9VPS99ZZ9SJJYFUJFFMFORBYDILBXCAVJDPDFHTTTIYOVGLRDYRTKHXJORJVYRPTDH9ZCPZ9ZADXZFRSFPIQKWLBRNTWJHXTOAUOL9FVGTUMMPYGYICJDXMOESEVDJWLMCVTJLPIEKBE9JTHDQWV9MRMEWFLPWGJFLUXI9BXPSVWCMUWLZSEWHBDZKXOLYNOZAPOYLQVZAQMOHGTTQEUAOVKVRRGAHNGPUEKHFVPVCOYSJAWHZU9DRROHBETBAFTATVAUGOEGCAYUXACLSSHHVYDHMDGJP9AUCLWLNTFEVGQGHQXSKEMVOVSKQEEWHWZUDTYOBGCURRZSJZLFVQQAAYQO9TRLFFN9HTDQXBSPPJYXMNGLLBHOMNVXNOWEIDMJVCLLDFHBDONQJCJVLBLCSMDOUQCKKCQJMGTSTHBXPXAMLMSXRIPUBMBAWBFNLHLUJTRJLDERLZFUBUSMF999XNHLEEXEENQJNOFFPNPQ9PQICHSATPLZVMVIWLRTKYPIXNFGYWOJSQDAXGFHKZPFLPXQEHCYEAGTIWIJEZTAVLNUMAFWGGLXMBNUQTOFCNLJTCDMWVVZGVBSEBCPFSM99FLOIDTCLUGPSEDLOKZUAEVBLWNMODGZBWOVQT9DPFOTSKRABQAVOQ9RXWBMAKFYNDCZOJGTCIDMQSQQSODKDXTPFLNOKSIZEOY9HFUTLQRXQMEPGOXQGLLPNSXAUCYPGZMNWMQWSWCKAQYKXJTWINSGPPZG9HLDLEAWUWEVCTVRCBDFOXKUROXH9HXXAXVPEJFRSLOGRVGYZASTEBAQNXJJROCYRTDPYFUIQJVDHAKEG9YACV9HCPJUEUKOYFNWDXCCJBIFQKYOXGRDHVTHEQUMHO999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999RKWEEVD99A99999999A99999999NFDPEEZCWVYLKZGSLCQNOFUSENIXRHWWTZFBXMPSQHEDFWZULBZFEOMNLRNIDQKDNNIELAOXOVMYEI9PGTKORV9IKTJZQUBQAWTKBKZ9NEZHBFIMCLV9TTNJNQZUIJDFPTTCTKBJRHAITVSKUCUEMD9M9SQJ999999TKORV9IKTJZQUBQAWTKBKZ9NEZHBFIMCLV9TTNJNQZUIJDFPTTCTKBJRHAITVSKUCUEMD9M9SQJ999999999999999999999999999999999999999999999999999999999999999999999999999999999999999']}