Search in sources :

Example 1 with Transaction

use of jota.model.Transaction in project run-wallet-android by runplay.

the class Audit method populateTxToTransfers.

public static void populateTxToTransfers(List<Transaction> inTransactions, NodeInfoResponse nodeInfo, List<Transfer> transfers, List<Address> allAddresses) {
    long totalValueTransfer = 0;
    long timestamp = 0;
    String address = "";
    String hash = "";
    Boolean persistence = false;
    long value = 0;
    String tag = Constants.NEW_TRANSFER_TAG;
    String destinationAddress = "";
    List<TransferTransaction> transactions = new ArrayList<>();
    List<TransferTransaction> othertransactions = new ArrayList<>();
    String message = defmessage;
    for (Transaction trx : inTransactions) {
        try {
            tag = trx.getTag();
            address = trx.getAddress();
            persistence = trx.getPersistence();
            value = trx.getValue();
            Address hasAddress = Store.isAlreadyAddress(address, allAddresses);
            if (hasAddress != null) {
                hasAddress.setAttached(true);
            }
            if (value != 0 && hasAddress != null) {
                transactions.add(new TransferTransaction(hasAddress.getAddress(), value));
                totalValueTransfer += value;
            } else if (value != 0) {
                othertransactions.add(new TransferTransaction(trx.getAddress(), value));
            }
            if (trx.getCurrentIndex() == 0) {
                message = Utils.removeTrailingNines(trx.getSignatureFragments());
                timestamp = trx.getAttachmentTimestamp();
                destinationAddress = address;
                hash = trx.getHash();
            }
            if (hasAddress != null) {
                boolean isRemainder = (trx.getCurrentIndex() == trx.getLastIndex()) && trx.getLastIndex() != 0;
                if (value < 0 && !isRemainder) {
                    hasAddress.setUsed(true);
                } else {
                    // hasAddress.setUsed(false);
                    hasAddress.setAttached(true);
                }
            }
        } catch (Exception e) {
            Log.e("AUDIT", "exception: " + e.getMessage());
        }
    }
    Transfer addtransfer = new Transfer(timestamp, destinationAddress, hash, persistence, totalValueTransfer, message, tag);
    if (nodeInfo != null) {
        addtransfer.setMilestone(nodeInfo.getLatestMilestoneIndex());
        addtransfer.setMilestoneCreated(nodeInfo.getLatestMilestoneIndex());
    }
    addtransfer.setTransactions(transactions);
    addtransfer.setOtherTransactions(othertransactions);
    transfers.add(addtransfer);
}
Also used : TransferTransaction(run.wallet.iota.model.TransferTransaction) Transaction(jota.model.Transaction) Address(run.wallet.iota.model.Address) ArrayList(java.util.ArrayList) Transfer(run.wallet.iota.model.Transfer) NudgeTransfer(run.wallet.iota.model.NudgeTransfer) TransferTransaction(run.wallet.iota.model.TransferTransaction)

Example 2 with Transaction

use of jota.model.Transaction in project run-wallet-android by runplay.

the class IotaMsgCore method attachToTangle.

/**
 * Attaches the specified transactions (trytes) to the Tangle by doing Proof of Work.
 *
 * @param trunkTransaction The trunk transaction to approve.
 * @param branchTransaction The branch transaction to approve.
 * @param minWeightMagnitude The Proof of Work intensity.
 * @param trytes A List of trytes (raw transaction data) to attach to the tangle.
 */
public GetAttachToTangleResponse attachToTangle(String trunkTransaction, String branchTransaction, Integer minWeightMagnitude, String... trytes) throws ArgumentException {
    if (!InputValidator.isHash(trunkTransaction)) {
        throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
    }
    if (!InputValidator.isHash(branchTransaction)) {
        throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
    }
    if (!InputValidator.isArrayOfTrytes(trytes)) {
        throw new ArgumentException(INVALID_TRYTES_INPUT_ERROR);
    }
    if (localPoW != null) {
        final String[] resultTrytes = new String[trytes.length];
        String previousTransaction = null;
        for (int i = 0; i < trytes.length; i++) {
            Transaction txn = new Transaction(trytes[i]);
            txn.setTrunkTransaction(previousTransaction == null ? trunkTransaction : previousTransaction);
            txn.setBranchTransaction(previousTransaction == null ? branchTransaction : trunkTransaction);
            if (txn.getTag().isEmpty() || txn.getTag().matches("9*"))
                txn.setTag(txn.getObsoleteTag());
            txn.setAttachmentTimestamp(System.currentTimeMillis());
            txn.setAttachmentTimestampLowerBound(0);
            txn.setAttachmentTimestampUpperBound(3_812_798_742_493L);
            resultTrytes[i] = localPoW.performPoW(txn.toTrytes(), minWeightMagnitude);
            previousTransaction = new Transaction(resultTrytes[i]).getHash();
        }
        return new GetAttachToTangleResponse(resultTrytes);
    }
    final Call<GetAttachToTangleResponse> res = service.attachToTangle(IotaAttachToTangleRequest.createAttachToTangleRequest(trunkTransaction, branchTransaction, minWeightMagnitude, trytes));
    return wrapCheckedException(res).body();
}
Also used : Transaction(jota.model.Transaction) ArgumentException(jota.error.ArgumentException)

Example 3 with Transaction

use of jota.model.Transaction in project run-wallet-android by runplay.

the class RunIotaAPICore method attachToTangle.

/**
 * Attaches the specified transactions (trytes) to the Tangle by doing Proof of Work.
 *
 * @param trunkTransaction The trunk transaction to approve.
 * @param branchTransaction The branch transaction to approve.
 * @param minWeightMagnitude The Proof of Work intensity.
 * @param trytes A List of trytes (raw transaction data) to attach to the tangle.
 */
public GetAttachToTangleResponse attachToTangle(String trunkTransaction, String branchTransaction, Integer minWeightMagnitude, String... trytes) throws ArgumentException {
    if (!InputValidator.isHash(trunkTransaction)) {
        throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
    }
    if (!InputValidator.isHash(branchTransaction)) {
        throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
    }
    if (!InputValidator.isArrayOfTrytes(trytes)) {
        throw new ArgumentException(INVALID_TRYTES_INPUT_ERROR);
    }
    if (localPoW != null) {
        final String[] resultTrytes = new String[trytes.length];
        String previousTransaction = null;
        for (int i = 0; i < trytes.length; i++) {
            Transaction txn = new Transaction(trytes[i]);
            txn.setTrunkTransaction(previousTransaction == null ? trunkTransaction : previousTransaction);
            txn.setBranchTransaction(previousTransaction == null ? branchTransaction : trunkTransaction);
            if (txn.getTag().isEmpty() || txn.getTag().matches("9*"))
                txn.setTag(txn.getObsoleteTag());
            txn.setAttachmentTimestamp(System.currentTimeMillis());
            txn.setAttachmentTimestampLowerBound(0);
            txn.setAttachmentTimestampUpperBound(3_812_798_742_493L);
            resultTrytes[i] = localPoW.performPoW(txn.toTrytes(), minWeightMagnitude);
            previousTransaction = new Transaction(resultTrytes[i]).getHash();
        }
        return new GetAttachToTangleResponse(resultTrytes);
    }
    final Call<GetAttachToTangleResponse> res = service.attachToTangle(IotaAttachToTangleRequest.createAttachToTangleRequest(trunkTransaction, branchTransaction, minWeightMagnitude, trytes));
    return wrapCheckedException(res).body();
}
Also used : Transaction(jota.model.Transaction) ArgumentException(jota.error.ArgumentException)

Example 4 with Transaction

use of jota.model.Transaction in project isf-jclient by mikrohash.

the class NodeManager method isNodeSynced.

private static String isNodeSynced(int api) {
    GetNodeInfoResponse getNodeInfoResponse = null;
    try {
        getNodeInfoResponse = getNodeInfo(api, false);
    } catch (Throwable e) {
        return e.getClass().getName() + ": " + e.getMessage();
    }
    if (getNodeInfoResponse == null)
        return "did not receive getNodeInfoResponse response within " + NODEINFO_DURATION_TOLERANCE + " seconds";
    if (Math.abs(getNodeInfoResponse.getLatestSolidSubtangleMilestoneIndex() - getNodeInfoResponse.getLatestMilestoneIndex()) > 3)
        return "solid subtangle is not updated: lacking " + (getNodeInfoResponse.getLatestMilestoneIndex() - getNodeInfoResponse.getLatestSolidSubtangleMilestoneIndex()) + " milestones behind";
    String milestone = getNodeInfoResponse.getLatestSolidSubtangleMilestone();
    long secondsBehind = System.currentTimeMillis() / 1000 - findTractionsByHashes(new String[] { milestone }, api).get(0).getTimestamp();
    if (secondsBehind > 600)
        return "lacking " + (secondsBehind / 60) + " minutes behind";
    GetTransactionsToApproveResponse getTransactionsToApproveResponse = null;
    try {
        getTransactionsToApproveResponse = apis[api].getTransactionsToApprove(DEPTH);
    } catch (Throwable e) {
        return e.getClass().getName() + ": " + e.getMessage();
    }
    if (getTransactionsToApproveResponse == null)
        return "getTransactionsToApproveResponse == null";
    String[] hashes = { getTransactionsToApproveResponse.getBranchTransaction(), getTransactionsToApproveResponse.getTrunkTransaction() };
    List<Transaction> transactions = findTractionsByHashes(hashes, api);
    if (transactions.size() == 0)
        return "silly node pretends to not know tips it just provided";
    long newerTimestamp = Math.max(transactions.get(0).getAttachmentTimestamp(), transactions.get(1).getAttachmentTimestamp());
    long tipAge = System.currentTimeMillis() / 1000 - newerTimestamp;
    lastSyncCheck[api] = System.currentTimeMillis();
    return tipAge > TIP_AGE_TOLERANCE ? "tips are " + tipAge + "s old, tolerance is set to " + TIP_AGE_TOLERANCE + "s" : null;
}
Also used : Transaction(jota.model.Transaction) GetNodeInfoResponse(jota.dto.response.GetNodeInfoResponse) GetTransactionsToApproveResponse(jota.dto.response.GetTransactionsToApproveResponse)

Example 5 with Transaction

use of jota.model.Transaction in project run-wallet-android by runplay.

the class RunIotaApiUtils method signInputsAndReturn.

public static List<String> signInputsAndReturn(final String seed, final List<Input> inputs, final Bundle bundle, final List<String> signatureFragments, ICurl curl) throws ArgumentException {
    bundle.finalize(curl);
    // Log.e("SIGN","add trytes "+signatureFragments.size());
    bundle.addTrytes(signatureFragments);
    // Log.e("SIGN","start: "+bundle.getTransactions().size());
    for (int i = 0; i < bundle.getTransactions().size(); i++) {
        // Log.e("SIGN","next: "+bundle.getTransactions().get(i).getValue());
        if (bundle.getTransactions().get(i).getValue() < 0) {
            String thisAddress = bundle.getTransactions().get(i).getAddress();
            // Get the corresponding keyIndex of the address
            int keyIndex = 0;
            int keySecurity = 0;
            for (Input input : inputs) {
                if (input.getAddress().equals(thisAddress)) {
                    keyIndex = input.getKeyIndex();
                    keySecurity = input.getSecurity();
                }
            }
            // Log.e("SIGN","address: "+thisAddress+" - sec: "+keySecurity+" - ind: "+keyIndex);
            String bundleHash = bundle.getTransactions().get(i).getBundle();
            // Get corresponding private key of address
            int[] key = new Signing(curl).key(Converter.trits(seed), keyIndex, keySecurity);
            // Log.e("SIGN","GO 1");
            // First 6561 trits for the firstFragment
            int[] firstFragment = Arrays.copyOfRange(key, 0, 6561);
            // Log.e("SIGN","GO 2");
            // Get the normalized bundle hash
            int[] normalizedBundleHash = bundle.normalizedBundle(bundleHash);
            // Log.e("SIGN","GO 3");
            // First bundle fragment uses 27 trytes
            int[] firstBundleFragment = Arrays.copyOfRange(normalizedBundleHash, 0, 27);
            // Log.e("SIGN","GO 4");
            // Calculate the new signatureFragment with the first bundle fragment
            int[] firstSignedFragment = new Signing(curl).signatureFragment(firstBundleFragment, firstFragment);
            // Log.e("SIGN","GO 5");
            // Convert signature to trytes and assign the new signatureFragment
            bundle.getTransactions().get(i).setSignatureFragments(Converter.trytes(firstSignedFragment));
            // for each security level, add an additional signature
            for (int j = 1; j < keySecurity; j++) {
                // Same address as well as value = 0 (as we already spent the input)
                if (bundle.getTransactions().get(i + j).getAddress().equals(thisAddress) && bundle.getTransactions().get(i + j).getValue() == 0) {
                    // Use the second 6562 trits
                    int[] secondFragment = Arrays.copyOfRange(key, 6561 * j, 6561 * (j + 1));
                    // The second 27 to 54 trytes of the bundle hash
                    int[] secondBundleFragment = Arrays.copyOfRange(normalizedBundleHash, 27 * j, 27 * (j + 1));
                    // 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(i + j).setSignatureFragments(Converter.trytes(secondSignedFragment));
                }
            }
        }
    }
    List<String> bundleTrytes = new ArrayList<>();
    // Convert all bundle entries into trytes
    for (Transaction tx : bundle.getTransactions()) {
        bundleTrytes.add(tx.toTrytes());
    }
    // Log.e("SIGN","FINISHED..............");
    Collections.reverse(bundleTrytes);
    return bundleTrytes;
}
Also used : Input(jota.model.Input) Transaction(jota.model.Transaction) ArrayList(java.util.ArrayList)

Aggregations

Transaction (jota.model.Transaction)5 ArrayList (java.util.ArrayList)2 ArgumentException (jota.error.ArgumentException)2 GetNodeInfoResponse (jota.dto.response.GetNodeInfoResponse)1 GetTransactionsToApproveResponse (jota.dto.response.GetTransactionsToApproveResponse)1 Input (jota.model.Input)1 Address (run.wallet.iota.model.Address)1 NudgeTransfer (run.wallet.iota.model.NudgeTransfer)1 Transfer (run.wallet.iota.model.Transfer)1 TransferTransaction (run.wallet.iota.model.TransferTransaction)1