jota.model.Transaction Java Examples

The following examples show how to use jota.model.Transaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TangleEntity.java    From qupla with Apache License 2.0 6 votes vote down vote up
private void onSubscribe(final TritVector address, final TritVector id)
{
  idMap.put(address, id);

  try
  {
    // we just got informed that this address is interesting to us
    final List<Transaction> transactions = iotaAPI.findTransactionObjectsByAddresses(new String[] { address.toTrytes() });
    if (transactions.size() != 0)
    {
      int breakPoint = 0;
    }
  }
  catch (ArgumentException e)
  {
    e.printStackTrace();
  }
}
 
Example #2
Source File: TangleEntity.java    From qupla with Apache License 2.0 6 votes vote down vote up
private void process(final String zmqMessage)
{
  Qupla.log(zmqMessage);

  try
  {
    final String[] messageFragments = zmqMessage.split(" ");
    String addressTrytes = messageFragments[1];
    String txHashTrytes = messageFragments[2];
    final List<Transaction> transactions = iotaAPI.findTransactionsObjectsByHashes(new String[] { txHashTrytes });
    if (transactions.size() < 2)
    {
      return;
    }

    processTransaction(addressTrytes, transactions);
  }
  catch (final ArgumentException e)
  {
    Qupla.log(zmqMessage);
    e.printStackTrace();
  }
}
 
Example #3
Source File: TangleInterface.java    From qupla with Apache License 2.0 6 votes vote down vote up
private List<Transaction> fetchOrderedBundleTransactions(String bundleHash)
{
  List<Transaction> orderedBundleTxs;
  try
  {
    String[] txHashes = iotaAPI.findTransactionsByBundles(bundleHash).getHashes();
    List<Transaction> unorderedBundleTxs = iotaAPI.findTransactionsObjectsByHashes(txHashes);
    orderedBundleTxs = orderTransactionsByIndex(unorderedBundleTxs);
  }
  catch (ArgumentException e)
  {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
  return orderedBundleTxs;
}
 
Example #4
Source File: TestnetCoordinator.java    From private-iota-testnet with MIT License 6 votes vote down vote up
private static boolean isConfirming(IotaAPI api, String confirming, String confirmed) throws Exception {
	// getInclusionStates is broken for non-milestones, therefore approximate the result by a (depth-limited) breadth-first search.
	if (confirming.equals(confirmed))
		return true;
	Set<String> currentDepth = new HashSet<>();
	Set<String> seen = new HashSet<>();
	currentDepth.add(confirming);
	seen.add(confirming);
	for (int depth = 0; depth < 10; depth++) {
		Set<String> nextDepth = new HashSet<>();
		for (String hash : currentDepth) {
			if (hash.equals(confirmed))
				return true;
			if (hash.equals(NULL_HASH))
				continue;
			Transaction tx = new Transaction(api.getTrytes(hash).getTrytes()[0]);
			if (seen.add(tx.getTrunkTransaction()))
				nextDepth.add(tx.getTrunkTransaction());
			if (seen.add(tx.getBranchTransaction()))
				nextDepth.add(tx.getBranchTransaction());
		}
		currentDepth = nextDepth;
	}
	// not found, so we assume it is not
	return false;
}
 
Example #5
Source File: TangleEntity.java    From qupla with Apache License 2.0 5 votes vote down vote up
private void processTransaction(final String addressTrytes, final List<Transaction> transactions)
{
  final Transaction tx0 = transactions.get(0);
  final Transaction tx1 = transactions.get(1);

  final TritVector id = new TritVector(HASH_SIZE, TritVector.TRIT_ZERO);
  final TritVector map = TritVector.fromTrytes(tx0.getSignatureFragments());
  final TritVector signature = TritVector.fromTrytes(tx1.getSignatureFragments().substring(0, 81));
  final TritVector address = TritVector.fromTrytes(addressTrytes);
  final TritVector cmd = new TritVector(1, TritVector.TRIT_ZERO);
  final TritVector message = TritVector.concat(TritVector.concat(TritVector.concat(TritVector.concat(id, map), signature), address), cmd);
  golStore.affect(message, 0);
}
 
Example #6
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
private static String accumulateTransactionMessages(List<Transaction> transactions)
{
  StringBuilder sb = new StringBuilder();
  for (Transaction transaction : transactions)
  {
    sb.append(transaction.getSignatureFragments().substring(0, SIGNATURE_FRAGMENTS_LENGTH));
  }
  return sb.toString();
}
 
Example #7
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
private static List<Transaction> orderTransactionsByIndex(List<Transaction> unordered)
{
  Transaction[] ordered = new Transaction[unordered.size()];
  for (Transaction transaction : unordered)
  {
    ordered[(int) transaction.getCurrentIndex()] = transaction;
  }
  return Arrays.asList(ordered);
}
 
Example #8
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
/**
 * @param address Address from which transactions shall be fetched.
 * @return Message of fetched transactions interpreted as ASCII.
 * @throws RuntimeException if argument is invalid.
 **/
public String[] fetchByAddress(String address)
{
  List<Transaction> transactions;
  try
  {
    transactions = iotaAPI.findTransactionObjectsByAddresses(new String[] { address });
  }
  catch (ArgumentException e)
  {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
  return readAsciiTxMsg(transactions);
}
 
Example #9
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
/**
 * @param hash Hash of transaction to fetch.
 * @return Message of fetched transaction interpreted as ASCII.
 * @throws RuntimeException if argument is invalid.
 **/
public String fetchByHash(String hash)
{
  Transaction transaction;
  try
  {
    transaction = iotaAPI.findTransactionsObjectsByHashes(new String[] { hash }).get(0);
  }
  catch (ArgumentException e)
  {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
  return readAsciiTxMsg(transaction);
}
 
Example #10
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
public String fetchMessageFromBundle(String bundleHash)
{
  List<Transaction> orderedBundleTxs = fetchOrderedBundleTransactions(bundleHash);
  String messagePacket = accumulateTransactionMessages(orderedBundleTxs);
  String header = messagePacket.substring(0, MESSAGE_PACKET_HEADER_LENGTH);
  int contentLength = Converter.value(Converter.trits(header));
  return messagePacket.substring(MESSAGE_PACKET_HEADER_LENGTH, MESSAGE_PACKET_HEADER_LENGTH + contentLength);
}
 
Example #11
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
/**
 * Same as {@link #readAsciiTxMsg(Transaction)} but for multiple transactions.
 */
private String[] readAsciiTxMsg(List<Transaction> transactions)
{
  String[] extracted = new String[transactions.size()];
  for (int i = 0; i < extracted.length; i++)
  {
    extracted[i] = readAsciiTxMsg(transactions.get(i));
  }
  return extracted;
}
 
Example #12
Source File: TangleInterface.java    From qupla with Apache License 2.0 5 votes vote down vote up
/**
 * @param transaction Transaction whose message shall be converted to ASCII.
 * @return Transaction message decoded to ASCII.
 */
private String readAsciiTxMsg(Transaction transaction)
{
  String trytes = transaction.getSignatureFragments();
  String trimmedTrytes = unpadTrytes(trytes);
  return TrytesConverter.trytesToAscii(trimmedTrytes);
}
 
Example #13
Source File: GetAccountDataResponse.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public GetAccountDataResponse(jota.dto.response.GetAccountDataResponse apiResponse) throws ArgumentException {

        attachedAddresses = apiResponse.getAddresses();
        Collections.reverse(attachedAddresses);

        Bundle[] transferBundle = apiResponse.getTransfers();

        if (transferBundle != null) {
            for (Bundle aTransferBundle : transferBundle) {

                totalValue = 0;

                for (Transaction trx : aTransferBundle.getTransactions()) {

                    address = trx.getAddress();
                    persistence = trx.getPersistence();
                    value = trx.getValue();

                    if (value != 0 && attachedAddresses.contains(Checksum.addChecksum(address)))
                        totalValue += value;

                    if (trx.getCurrentIndex() == 0) {
                        timestamp = trx.getAttachmentTimestamp() / 1000;
                        tag = trx.getTag();
                        destinationAddress = address;
                        hash = trx.getHash();
                    }

                    // check if sent transaction
                    if (attachedAddresses.contains(Checksum.addChecksum(address))) {
                        boolean isRemainder = (trx.getCurrentIndex() == trx.getLastIndex()) && trx.getLastIndex() != 0;
                        if (value < 0 && !isRemainder) {

                            if (addresses.contains(new Address(Checksum.addChecksum(address), false)))
                                addresses.remove(new Address(Checksum.addChecksum(address), false));

                            if (!addresses.contains(new Address(Checksum.addChecksum(address), true)))
                                addresses.add(new Address(Checksum.addChecksum(address), true));
                        } else {
                            if (!addresses.contains(new Address(Checksum.addChecksum(address), true)) &&
                                    !addresses.contains(new Address(Checksum.addChecksum(address), false)))
                                addresses.add(new Address(Checksum.addChecksum(address), false));
                        }
                    }
                }

                transfers.add(new Transfer(timestamp, destinationAddress, hash, persistence, totalValue, message, tag));

            }

            // sort the addresses
            final Map<String, Integer> map = new HashMap<>();
            for (int i = 0; i < attachedAddresses.toArray().length; ++i)
                map.put(attachedAddresses.get(i), i);
            Collections.sort(addresses, new Comparator<Address>() {
                @Override
                public int compare(Address add1, Address add2) {
                    return map.get(add1.getAddress()) - map.get(add2.getAddress());
                }
            });

            Collections.sort(transfers);

            //Don't know why, but in ver. 1.5.5 correct balance = value, before it was apiResponse.getBalance()
            setBalance(value);
            setDuration(apiResponse.getDuration());
        }
    }
 
Example #14
Source File: GetAccountDataResponse.java    From android-wallet-app with GNU General Public License v3.0 4 votes vote down vote up
public GetAccountDataResponse(jota.dto.response.GetAccountDataResponse apiResponse) throws ArgumentException {

        attachedAddresses = apiResponse.getAddresses();
        Collections.reverse(attachedAddresses);

        Bundle[] transferBundle = apiResponse.getTransfers();

        if (transferBundle != null) {
            for (Bundle aTransferBundle : transferBundle) {

                totalValue = 0;

                for (Transaction trx : aTransferBundle.getTransactions()) {

                    address = trx.getAddress();
                    persistence = trx.getPersistence();
                    value = trx.getValue();

                    if (value != 0 && attachedAddresses.contains(Checksum.addChecksum(address)))
                        totalValue += value;

                    if (trx.getCurrentIndex() == 0) {
                        timestamp = trx.getAttachmentTimestamp() / 1000;
                        tag = trx.getTag();
                        destinationAddress = address;
                        hash = trx.getHash();
                    }

                    // check if sent transaction
                    if (attachedAddresses.contains(Checksum.addChecksum(address))) {
                        boolean isRemainder = (trx.getCurrentIndex() == trx.getLastIndex()) && trx.getLastIndex() != 0;
                        if (value < 0 && !isRemainder) {

                            if (addresses.contains(new Address(Checksum.addChecksum(address), false)))
                                addresses.remove(new Address(Checksum.addChecksum(address), false));

                            if (!addresses.contains(new Address(Checksum.addChecksum(address), true)))
                                addresses.add(new Address(Checksum.addChecksum(address), true));
                        } else {
                            if (!addresses.contains(new Address(Checksum.addChecksum(address), true)) &&
                                    !addresses.contains(new Address(Checksum.addChecksum(address), false)))
                                addresses.add(new Address(Checksum.addChecksum(address), false));
                        }
                    }
                }

                transfers.add(new Transfer(timestamp, destinationAddress, hash, persistence, totalValue, message, tag));

            }

            // sort the addresses
            final Map<String, Integer> map = new HashMap<>();
            for (int i = 0; i < attachedAddresses.toArray().length; ++i)
                map.put(attachedAddresses.get(i), i);
            Collections.sort(addresses, new Comparator<Address>() {
                @Override
                public int compare(Address add1, Address add2) {
                    return map.get(add1.getAddress()) - map.get(add2.getAddress());
                }
            });

            Collections.sort(transfers);

            setBalance(apiResponse.getBalance());
            setDuration(apiResponse.getDuration());
        }
    }