mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2026-10-04 13:37:00 +00:00
fixed sendtransfer
This commit is contained in:
@@ -216,7 +216,7 @@ public class IotaAPIProxy {
|
||||
*/
|
||||
public GetNewAddressResponse getNewAddress(final String seed, final int index, final boolean checksum, final int total, final boolean returnAll) {
|
||||
|
||||
final List<String> allAddresses = new ArrayList<>();
|
||||
List<String> allAddresses = new ArrayList<>();
|
||||
|
||||
// If total number of addresses to generate is supplied, simply generate
|
||||
// and return the list of all addresses
|
||||
@@ -241,7 +241,7 @@ public class IotaAPIProxy {
|
||||
|
||||
// If !returnAll return only the last address that was generated
|
||||
if (!returnAll) {
|
||||
allAddresses.subList(0, allAddresses.size() - 1).clear();
|
||||
allAddresses = allAddresses.subList(allAddresses.size() - 2, allAddresses.size() - 1);
|
||||
}
|
||||
return GetNewAddressResponse.create(allAddresses);
|
||||
}
|
||||
@@ -267,14 +267,14 @@ public class IotaAPIProxy {
|
||||
*/
|
||||
|
||||
/**
|
||||
* @method getTransfers
|
||||
* @param {string} seed
|
||||
* @param {object} options
|
||||
* @param {function} callback
|
||||
* @property {int} start Starting key index
|
||||
* @property {int} end Ending key index
|
||||
* @property {bool} inclusionStates returns confirmation status of all transactions
|
||||
* @returns {object} success
|
||||
* @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 {
|
||||
start = start != null ? 0 : start;
|
||||
@@ -292,7 +292,7 @@ public class IotaAPIProxy {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Bundle[] bundlesFromAddresses(String[] addresses, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException{
|
||||
public Bundle[] bundlesFromAddresses(String[] addresses, Boolean inclusionStates) throws ArgumentException, InvalidBundleException, InvalidSignatureException {
|
||||
|
||||
List<Transaction> trxs = findTransactionObjects(addresses);
|
||||
// set of tail transactions
|
||||
@@ -808,11 +808,11 @@ public class IotaAPIProxy {
|
||||
return getInclusionStates(hashes, latestMilestone);
|
||||
}
|
||||
|
||||
public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMagnitude, Transfer[] transactions, Input[] inputs, String address) {
|
||||
public SendTransferResponse sendTransfer(String seed, int depth, int minWeightMagnitude, final List<Transfer> transfers, Input[] inputs, String address) {
|
||||
|
||||
List<String> trytes = prepareTransfers(seed, Arrays.asList(transactions), address, inputs == null ? null : Arrays.asList(inputs));
|
||||
List<String> trytes = prepareTransfers(seed, transfers, address, inputs == null ? null : Arrays.asList(inputs));
|
||||
List<Transaction> trxs = sendTrytes(trytes.toArray(new String[trytes.size()]), minWeightMagnitude);
|
||||
return SendTransferResponse.create(trxs.toArray(new Transaction[trxs.size()]));
|
||||
return SendTransferResponse.create(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -861,6 +861,58 @@ public class IotaAPIProxy {
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> addRemainder(final String seed,
|
||||
final List<Input> inputs,
|
||||
final Bundle bundle,
|
||||
final String tag,
|
||||
final long totalValue,
|
||||
final String remainderAddress,
|
||||
final List<String> signatureFragments) {
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
long thisBalance = inputs.get(i).getBalance();
|
||||
long totalTransferValue = totalValue;
|
||||
long toSubtract = 0 - thisBalance;
|
||||
long timestamp = (new Date()).getTime();
|
||||
|
||||
// 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);
|
||||
} 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);
|
||||
} else {
|
||||
// If there is no remainder, do not add transaction to bundle
|
||||
// simply sign and return
|
||||
return IotaAPIUtils.signInputsAndReturn(seed, inputs, bundle, signatureFragments);
|
||||
}
|
||||
|
||||
// If multiple inputs provided, subtract the totalTransferValue by
|
||||
// the inputs balance
|
||||
} else {
|
||||
totalTransferValue -= thisBalance;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
String protocol, host, port;
|
||||
|
||||
@@ -15,7 +15,7 @@ public class GetTransferResponse {
|
||||
|
||||
public static GetTransferResponse create(Bundle[] transfers) {
|
||||
GetTransferResponse res = new GetTransferResponse();
|
||||
res.transfers = transfers;
|
||||
//res.transfers = transfers;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,10 +46,9 @@ public class Bundle {
|
||||
for (int i = 0; i < signatureMessageLength; i++) {
|
||||
|
||||
List<Transaction> transactions = new ArrayList<>(getTransactions());
|
||||
transactions.add(new Transaction(address, String.valueOf(i == 0 ? value : 0), tag, String.valueOf(timestamp)));
|
||||
|
||||
Transaction trx = new Transaction(address, String.valueOf(i == 0 ? value : 0), tag, String.valueOf(timestamp));
|
||||
transactions.add(trx);
|
||||
setTransactions(transactions);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ public class Bundle {
|
||||
|
||||
for (int i = 0; i < this.getTransactions().size(); i++) {
|
||||
|
||||
int[] valueTrits = Converter.trits(this.getTransactions().get(i).getValue(),81);
|
||||
int[] valueTrits = Converter.trits(this.getTransactions().get(i).getValue(), 81);
|
||||
|
||||
int[] timestampTrits = Converter.trits(this.getTransactions().get(i).getTimestamp(), 27);
|
||||
|
||||
@@ -73,7 +72,7 @@ public class Bundle {
|
||||
curl.absorb(t, 0, t.length);
|
||||
}
|
||||
|
||||
int[] hash = new int[90];
|
||||
int[] hash = new int[243];
|
||||
curl.squeeze(hash, 0, hash.length);
|
||||
String hashInTrytes = Converter.trytes(hash);
|
||||
|
||||
@@ -94,7 +93,7 @@ public class Bundle {
|
||||
for (int i = 0; i < this.getTransactions().size(); i++) {
|
||||
|
||||
// Fill empty signatureMessageFragment
|
||||
this.getTransactions().get(i).setSignatureFragments(signatureFragments.get(i) == null ? signatureFragments.get(i) : emptySignatureFragment);
|
||||
this.getTransactions().get(i).setSignatureFragments(!signatureFragments.get(i).isEmpty() ? signatureFragments.get(i) : emptySignatureFragment);
|
||||
// Fill empty trunkTransaction
|
||||
this.getTransactions().get(i).setTrunkTransaction(emptyHash);
|
||||
|
||||
|
||||
@@ -93,11 +93,10 @@ public class Converter {
|
||||
}
|
||||
|
||||
public static int[] trits(final String trytes) {
|
||||
final int[] trits = new int[trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE];
|
||||
|
||||
final List<Integer> trits = new LinkedList<>();
|
||||
if (InputValidator.isValue(trytes)) {
|
||||
|
||||
int value = Integer.parseInt(trytes);
|
||||
long value = Long.parseLong(trytes);
|
||||
|
||||
long absoluteValue = value < 0 ? -value : value;
|
||||
|
||||
@@ -113,22 +112,21 @@ public class Converter {
|
||||
absoluteValue++;
|
||||
}
|
||||
|
||||
trits[position++] = remainder;
|
||||
trits.add(position++,remainder);
|
||||
}
|
||||
if (value < 0) {
|
||||
|
||||
for (int i = 0; i < trits.length; i++) {
|
||||
|
||||
trits[i] = -trits[i];
|
||||
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, trits, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
|
||||
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 trits;
|
||||
return convertToIntArray(trits);
|
||||
}
|
||||
|
||||
public static void copyTrits(final long value, final int[] destination, final int offset, final int size) {
|
||||
@@ -213,10 +211,10 @@ public class Converter {
|
||||
int[] timestampTrits = Converter.trits(trx.getTimestamp(), 27);
|
||||
|
||||
|
||||
int[] currentIndexTrits = Converter.trits(trx.getTimestamp(), 27);
|
||||
int[] currentIndexTrits = Converter.trits(trx.getCurrentIndex(), 27);
|
||||
|
||||
|
||||
int[] lastIndexTrits = Converter.trits(trx.getCurrentIndex(), 27);
|
||||
int[] lastIndexTrits = Converter.trits(trx.getLastIndex(), 27);
|
||||
|
||||
|
||||
return trx.getSignatureFragments()
|
||||
|
||||
@@ -2,6 +2,8 @@ package jota.utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import jota.IotaAPIProxy;
|
||||
import jota.dto.response.GetNewAddressResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -40,56 +42,6 @@ public class IotaAPIUtils {
|
||||
return address;
|
||||
}
|
||||
|
||||
public static List<String> addRemainder(final String seed,
|
||||
final List<Input> inputs,
|
||||
final Bundle bundle,
|
||||
final String tag,
|
||||
final long totalValue,
|
||||
final String remainderAddress,
|
||||
final List<String> signatureFragments) {
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
long thisBalance = inputs.get(i).getBalance();
|
||||
long totalTransferValue = totalValue;
|
||||
long toSubtract = 0 - thisBalance;
|
||||
long timestamp = (new Date()).getTime();
|
||||
|
||||
// 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 signInputsAndReturn(seed, inputs, bundle, signatureFragments);
|
||||
} else if (remainder > 0) {
|
||||
// Generate a new Address by calling getNewAddress
|
||||
String address = newAddress(seed, 0, false);
|
||||
// Remainder bundle entry
|
||||
bundle.addEntry(1, address, remainder, tag, timestamp);
|
||||
// Final function for signing inputs
|
||||
return signInputsAndReturn(seed, inputs, bundle, signatureFragments);
|
||||
} else {
|
||||
// If there is no remainder, do not add transaction to bundle
|
||||
// simply sign and return
|
||||
return signInputsAndReturn(seed, inputs, bundle, signatureFragments);
|
||||
}
|
||||
|
||||
// If multiple inputs provided, subtract the totalTransferValue by
|
||||
// the inputs balance
|
||||
} else {
|
||||
totalTransferValue -= thisBalance;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> signInputsAndReturn(final String seed,
|
||||
final List<Input> inputs,
|
||||
final Bundle bundle,
|
||||
|
||||
Reference in New Issue
Block a user