mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2026-08-31 05:10:25 +00:00
refactoring, moving getNewAddress to Proxy class
This commit is contained in:
@@ -14,7 +14,9 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -184,19 +186,69 @@ public class IotaAPIProxy {
|
||||
return wrapCheckedException(res).body();
|
||||
}
|
||||
|
||||
// end of proxied calls.
|
||||
|
||||
public BroadcastTransactionsResponse broadcastTransactions(String... trytes) {
|
||||
final Call<BroadcastTransactionsResponse> res = service.broadcastTransactions(IotaBroadcastTransactionRequest.createBroadcastTransactionsRequest(trytes));
|
||||
return wrapCheckedException(res).body();
|
||||
}
|
||||
|
||||
// end of proxied calls.
|
||||
|
||||
public GetBundleResponse getBundle(String transaction) {
|
||||
return IotaAPIUtils.getBundle(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(String seed, Integer index, boolean checksum, int total, boolean returnAll) {
|
||||
return IotaAPIUtils.getNewAddress(seed, index, checksum, total, returnAll);
|
||||
public GetNewAddressResponse getNewAddress(final String seed, final int index, final boolean checksum, final int total, final boolean returnAll) {
|
||||
|
||||
final List<String> allAddresses = new ArrayList<>();
|
||||
// Case 1: total
|
||||
//
|
||||
// If total number of addresses to generate is supplied, simply generate
|
||||
// and return the list of all addresses
|
||||
|
||||
if (total != 0) {
|
||||
// Increase index with each iteration
|
||||
for (int i = index; i < index + total; i++) {
|
||||
allAddresses.add(IotaAPIUtils.newAddress(seed, i, checksum));
|
||||
}
|
||||
return GetNewAddressResponse.create(allAddresses);
|
||||
}
|
||||
|
||||
// Case 2: no total provided
|
||||
//
|
||||
// Continue calling findTransactions to see if address was already created
|
||||
// if null, return list of addresses
|
||||
|
||||
for (int i = index; ; i++) {
|
||||
String newAddress = IotaAPIUtils.newAddress(seed, i, checksum);
|
||||
|
||||
final FindTransactionResponse response = findTransactionsByAddresses(new String[]{newAddress});
|
||||
|
||||
allAddresses.add(newAddress);
|
||||
|
||||
if (response.getHashes().length == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If returnAll, return list of allAddresses
|
||||
// else return only the last address that was generated
|
||||
if (!returnAll) {
|
||||
allAddresses.subList(0, allAddresses.size()-1).clear();
|
||||
}
|
||||
|
||||
return GetNewAddressResponse.create(allAddresses);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package jota.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Created by pinpong on 02.12.16.
|
||||
*/
|
||||
@@ -15,7 +17,8 @@ public class Checksum {
|
||||
public static String removeChecksum(String addressWithChecksum) {
|
||||
if (isAddressWithChecksum(addressWithChecksum)) {
|
||||
return getAddress(addressWithChecksum);
|
||||
} else return "";
|
||||
}
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
private static String getAddress(String addressWithChecksum) {
|
||||
|
||||
@@ -11,8 +11,9 @@ public class InputValidator {
|
||||
}
|
||||
|
||||
public static boolean checkAddress(String address) {
|
||||
if (!isAddress(address))
|
||||
if (!isAddress(address)) {
|
||||
throw new RuntimeException("Invalid address: " + address);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package jota.utils;
|
||||
|
||||
import jota.IotaAPIProxy;
|
||||
import jota.dto.response.FindTransactionResponse;
|
||||
import jota.dto.response.GetBundleResponse;
|
||||
import jota.dto.response.GetNewAddressResponse;
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jota.dto.response.GetBundleResponse;
|
||||
|
||||
/**
|
||||
* Client Side computation service
|
||||
@@ -20,66 +15,6 @@ public class IotaAPIUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(IotaAPIUtils.class);
|
||||
|
||||
/**
|
||||
* 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 static GetNewAddressResponse getNewAddress(final String seed, final int index, final boolean checksum, final int total, final boolean returnAll) {
|
||||
|
||||
final List<String> allAddresses = new ArrayList<>();
|
||||
// Case 1: total
|
||||
//
|
||||
// If total number of addresses to generate is supplied, simply generate
|
||||
// and return the list of all addresses
|
||||
//
|
||||
//
|
||||
if (total != 0) {
|
||||
// Increase index with each iteration
|
||||
for (int i = index; i < index + total; i++) {
|
||||
allAddresses.add(newAddress(seed, i, checksum));
|
||||
}
|
||||
}
|
||||
// Case 2: no total provided
|
||||
//
|
||||
// Continue calling findTransactions to see if address was already created
|
||||
// if null, return list of addresses
|
||||
//
|
||||
else {
|
||||
|
||||
// TODO init with params
|
||||
IotaAPIProxy proxy = new IotaAPIProxy.Builder().build();
|
||||
|
||||
for (int i = index; ; i++) {
|
||||
String newAddress = newAddress(seed, i, checksum);
|
||||
|
||||
FindTransactionResponse response = proxy.findTransactions(null, new String[]{newAddress}, null, null);
|
||||
|
||||
// If returnAll, return list of allAddresses
|
||||
// else return only the last address that was generated
|
||||
|
||||
if (!returnAll) {
|
||||
allAddresses.clear();
|
||||
}
|
||||
|
||||
allAddresses.add(newAddress);
|
||||
|
||||
if (response.getHashes().length == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return GetNewAddressResponse.create(allAddresses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new address
|
||||
*
|
||||
@@ -88,7 +23,7 @@ public class IotaAPIUtils {
|
||||
* @param checksum
|
||||
* @return an String with address
|
||||
*/
|
||||
private static String newAddress(String seed, int index, boolean checksum) {
|
||||
public static String newAddress(String seed, int index, boolean checksum) {
|
||||
|
||||
final int[] key = Signing.key(Converter.trits(seed), index, 2);
|
||||
log.debug("key Length = {}", key.length );
|
||||
@@ -97,6 +32,8 @@ public class IotaAPIUtils {
|
||||
log.debug("digests Length = {}", digests.length );
|
||||
|
||||
final int[] addressTrits = Signing.address(digests);
|
||||
log.debug("addressTrits Length = {}", addressTrits.length );
|
||||
|
||||
String address = Converter.trytes(addressTrits);
|
||||
|
||||
if (checksum) {
|
||||
|
||||
@@ -36,7 +36,7 @@ public class TrytesConverter {
|
||||
|
||||
public static String toTrytes(String inputString) {
|
||||
|
||||
String trytes = "";
|
||||
StringBuilder trytes = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < inputString.length(); i++) {
|
||||
|
||||
@@ -52,10 +52,10 @@ public class TrytesConverter {
|
||||
|
||||
String trytesValue = String.valueOf(Constants.TRYTE_ALPHABET.charAt(firstValue) + String.valueOf(Constants.TRYTE_ALPHABET.charAt(secondValue)));
|
||||
|
||||
trytes += trytesValue;
|
||||
trytes.append(trytesValue);
|
||||
}
|
||||
|
||||
return trytes;
|
||||
return trytes.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user