Search in sources :

Example 1 with NudgeRequest

use of run.wallet.iota.api.requests.NudgeRequest in project run-wallet-android by runplay.

the class AutoNudgeHandler method doNudge.

public static ApiResponse doNudge(RunIotaAPI apiProxy, Context context, ApiRequest inrequest) {
    ApiResponse response = new ApiResponse();
    Store.init(context, false);
    Store.loadNudgeTransfers(context);
    List<NudgeTransfer> nudgeTransfers = Store.getNudgeTransfers();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int nudgeAttempts = Sf.toInt(prefs.getString(Constants.PREF_TRANSFER_NUDGE_ATTEMPTS, "" + Constants.PREF_TRANSFER_NUDGE_ATTEMPTS_VALUE));
    if (nudgeAttempts > 0) {
        Nodes.Node node = Store.getNode();
        if (node != null) {
            RunIotaAPI api = new RunIotaAPI.Builder().protocol(node.protocol).host(node.ip).port(((Integer) node.port).toString()).build();
            jota.dto.response.GetNodeInfoResponse nir = null;
            try {
                nir = api.getNodeInfo();
            } catch (Exception e) {
                node = Store.getNode();
                try {
                    nir = api.getNodeInfo();
                } catch (Exception e2) {
                }
            }
            if (nir != null) {
                if (nir.getLatestMilestoneIndex() == nir.getLatestSolidSubtangleMilestoneIndex()) {
                    Map<String, NudgeTransfer> refreshSeedShorts = new HashMap<>();
                    List<NudgeTransfer> removeFromNudges = new ArrayList<>();
                    int len = nudgeTransfers.size();
                    // strictly this way incase one gets added from another method
                    for (int i = 0; i < len; i++) {
                        NudgeTransfer ntran = nudgeTransfers.get(i);
                        if (ntran.transfer.getMilestone() < nir.getLatestMilestoneIndex() && ntran.transfer.getNudgeCount() < nudgeAttempts) {
                            try {
                                Seeds.Seed seed = null;
                                for (Seeds.Seed tseed : Store.getSeedList()) {
                                    if (tseed.getSystemShortValue().equals(ntran.seedShort)) {
                                        seed = tseed;
                                    }
                                }
                                if (seed != null) {
                                    boolean hascompleted = false;
                                    List<Transfer> allTransfers = Store.getTransfers(context, seed);
                                    List<String> hashes = new ArrayList<>();
                                    for (Transfer transfer : allTransfers) {
                                        if (transfer.getValue() == ntran.transfer.getValue() && transfer.getAddress().equals(ntran.transfer.getAddress())) {
                                            if (transfer.isCompleted()) {
                                                hascompleted = true;
                                            }
                                            hashes.add(transfer.getHash());
                                        }
                                    }
                                    List<jota.model.Transaction> transactions = new ArrayList<>();
                                    if (!hascompleted) {
                                        transactions = api.findTransactionsObjectsByHashes(hashes.toArray(new String[hashes.size()]));
                                        if (!transactions.isEmpty()) {
                                            for (jota.model.Transaction transaction : transactions) {
                                                if (transaction.getPersistence() != null && transaction.getPersistence().booleanValue()) {
                                                    hascompleted = true;
                                                }
                                            }
                                        }
                                    }
                                    if (hascompleted) {
                                        refreshSeedShorts.put(ntran.seedShort, ntran);
                                    } else if (hascompleted || hashes.isEmpty() || transactions.isEmpty()) {
                                    // do nothing
                                    } else {
                                        ApiResponse resp = NudgeRequestHandler.doNudge(api, context, new NudgeRequest(seed, ntran.transfer));
                                        if (resp instanceof NudgeResponse) {
                                            NudgeResponse nresponse = (NudgeResponse) resp;
                                            if (nresponse.getSuccessfully()) {
                                                refreshSeedShorts.put(ntran.seedShort, ntran);
                                            }
                                        }
                                    }
                                }
                            } catch (Exception e) {
                            // removeFromNudges.add(ntran);
                            }
                        }
                        removeFromNudges.add(ntran);
                    }
                    if (!refreshSeedShorts.isEmpty()) {
                        List<Seeds.Seed> seeds = Store.getSeedList();
                        for (String seedShort : refreshSeedShorts.keySet()) {
                            for (Seeds.Seed seed : seeds) {
                                if (String.valueOf(Store.getSeedRaw(context, seed)).startsWith(seedShort)) {
                                    AppService.getAccountData(context, seed);
                                    break;
                                }
                            }
                        }
                        Store.saveNudgeTransfers(context);
                    }
                    if (!removeFromNudges.isEmpty()) {
                        Store.removeNudgeTransfer(context, removeFromNudges);
                    }
                }
            }
        }
    } else {
        Store.removeNudgeTransfer(context, nudgeTransfers);
    }
    return response;
}
Also used : Seeds(run.wallet.iota.model.Seeds) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NudgeResponse(run.wallet.iota.api.responses.NudgeResponse) ApiResponse(run.wallet.iota.api.responses.ApiResponse) SharedPreferences(android.content.SharedPreferences) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest) AutoNudgeRequest(run.wallet.iota.api.requests.AutoNudgeRequest) Nodes(run.wallet.iota.model.Nodes) RunIotaAPI(jota.RunIotaAPI) NudgeTransfer(run.wallet.iota.model.NudgeTransfer) Transfer(run.wallet.iota.model.Transfer) NudgeTransfer(run.wallet.iota.model.NudgeTransfer)

Example 2 with NudgeRequest

use of run.wallet.iota.api.requests.NudgeRequest in project run-wallet-android by runplay.

the class NudgeRequestHandlerOld method doNudge.

public static ApiResponse doNudge(RunIotaAPI apiProxy, Context context, ApiRequest inrequest) {
    ApiResponse response;
    int notificationId = Utils.createNewID();
    // int notificationId = Utils.createNewID();
    NudgeRequest request = (NudgeRequest) inrequest;
    // NotificationHelper.requestNotification(context, R.drawable.send_white, context.getString(R.string.notification_replay_bundle_request_title), notificationId);
    Transfer nudgeMe = request.getTransfer();
    try {
        List<Address> alreadyAddress = Store.getAddresses(context, request.getSeed());
        String useAddress = null;
        Address fullAddress = null;
        List<Address> emptyAttached = Store.getEmptyAttached(alreadyAddress);
        // allows up to 5 pre hidden addresses
        int max = Store.getAutoAttach() + 5;
        if (emptyAttached.size() <= max) {
            GetNewAddressRequest gnr = new GetNewAddressRequest(request.getSeed());
            gnr.setIndex(alreadyAddress.size());
            final GetNewAddressResponse gna = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, request.getSeed())), gnr.getSecurity(), alreadyAddress.size(), gnr.isChecksum(), 1, gnr.isReturnAll());
            run.wallet.iota.api.responses.GetNewAddressResponse gnar = new run.wallet.iota.api.responses.GetNewAddressResponse(request.getSeed(), gna);
            Store.addAddress(context, gnr, gnar);
            alreadyAddress = Store.getAddresses(context, request.getSeed());
            useAddress = gnar.getAddresses().get(0);
            fullAddress = Store.isAlreadyAddress(useAddress, alreadyAddress);
        } else {
            fullAddress = emptyAttached.get(0);
            useAddress = fullAddress.getAddress();
        }
        // useAddress=nudgeMe
        // }
        NudgeResponse nresp = null;
        if (useAddress != null) {
            List<Transfer> transfers = new ArrayList<>();
            List<Transfer> alreadyTransfers = Store.getTransfers(context, request.getSeed());
            Transfer already = Store.isAlreadyTransfer(nudgeMe.getHash(), alreadyTransfers);
            if (already != null) {
                RunSendTransferResponse rstr = apiProxy.sendNudgeTransfer(String.valueOf(Store.getSeedRaw(context, request.getSeed())), nudgeMe.getHash(), useAddress, fullAddress.getIndex(), fullAddress.getSecurity(), request.getDepth(), request.getMinWeightMagnitude());
                nresp = new NudgeResponse(rstr);
                String gotHash = null;
                if (nresp != null && nresp.getSuccessfully()) {
                    gotHash = nresp.getHashes().get(0);
                    already.addNudgeHash(gotHash);
                } else {
                    already.addNudgeHash("Failed nudge");
                }
                jota.dto.response.GetNodeInfoResponse nodeInfo = apiProxy.getNodeInfo();
                if (nodeInfo != null) {
                    already.setMilestone(nodeInfo.getLatestMilestoneIndex());
                }
                if (gotHash != null) {
                    Transfer transfer = new Transfer(useAddress, 0, "RUN9NUDGE9HASH9" + nudgeMe.getHash() + "9END", RunIotaAPI.NUDGE_TAG);
                    transfer.setHash(gotHash);
                    transfer.setTimestamp(System.currentTimeMillis());
                    if (nodeInfo != null) {
                        transfer.setMilestoneCreated(nodeInfo.getLatestMilestoneIndex());
                    }
                    transfers.add(transfer);
                    Wallet wallet = Store.getWallet(context, request.getSeed());
                    Audit.setTransfersToAddresses(request.getSeed(), transfers, alreadyAddress, wallet, alreadyTransfers);
                    Audit.processNudgeAttempts(context, request.getSeed(), transfers);
                    Store.updateAccountData(context, request.getSeed(), wallet, transfers, alreadyAddress);
                }
                if (!AppService.isAppStarted()) {
                    NotificationHelper.responseNotification(context, R.drawable.nudge_orange, context.getString(R.string.notification_nudge_succeeded_title), notificationId);
                } else {
                    NotificationHelper.vibrate(context);
                }
                return nresp;
            }
        }
        // if(nresp==null) {
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.INVALID_HASH_ERROR);
        return error;
    } catch (ArgumentException e) {
        Log.e("NUDGE", "error: " + e.getMessage());
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.INVALID_HASH_ERROR);
        response = error;
    }
    return response;
}
Also used : GetNewAddressResponse(jota.dto.response.GetNewAddressResponse) Address(run.wallet.iota.model.Address) GetNewAddressRequest(run.wallet.iota.api.requests.GetNewAddressRequest) ArrayList(java.util.ArrayList) NudgeResponse(run.wallet.iota.api.responses.NudgeResponse) ApiResponse(run.wallet.iota.api.responses.ApiResponse) ArgumentException(jota.error.ArgumentException) Wallet(run.wallet.iota.model.Wallet) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest) NetworkError(run.wallet.iota.api.responses.error.NetworkError) RunSendTransferResponse(jota.dto.response.RunSendTransferResponse) Transfer(run.wallet.iota.model.Transfer)

Example 3 with NudgeRequest

use of run.wallet.iota.api.requests.NudgeRequest in project run-wallet-android by runplay.

the class NudgeRequestHandler method doNudge.

public static ApiResponse doNudge(RunIotaAPI apiProxy, Context context, ApiRequest inrequest) {
    ApiResponse response;
    int notificationId = Utils.createNewID();
    NudgeRequest request = (NudgeRequest) inrequest;
    Transfer nudgeMe = request.getTransfer();
    try {
        List<Address> alreadyAddress = Store.getAddresses(context, request.getSeed());
        // String useAddress="RUN9IOTA9WALLET9NUDGE9PROMOTE9TRANSFER9ADDRESSRUN9IOTA9WALLET9NUDGE9PROMOTE9TRANS";
        String random = SeedRandomGenerator.generateNewSeed();
        String useAddress = "RUN9IOTA9WALLET9NUDGE9PROMOTE9TRANSFER99" + Sf.restrictLength(random, 41);
        NudgeResponse nresp = null;
        if (useAddress != null) {
            List<Transfer> transfers = new ArrayList<>();
            List<Transfer> alreadyTransfers = Store.getTransfers(context, request.getSeed());
            Transfer already = Store.isAlreadyTransfer(nudgeMe.getHash(), alreadyTransfers);
            if (already != null) {
                RunSendTransferResponse rstr = apiProxy.sendNudgeTransfer(String.valueOf(Store.getSeedRaw(context, request.getSeed())), nudgeMe.getHash(), useAddress, 1, 2, request.getDepth(), request.getMinWeightMagnitude());
                nresp = new NudgeResponse(rstr);
                String gotHash = null;
                if (nresp != null && nresp.getSuccessfully()) {
                    gotHash = nresp.getHashes().get(0);
                    already.addNudgeHash(gotHash);
                } else {
                    already.addNudgeHash("Failed nudge");
                }
                jota.dto.response.GetNodeInfoResponse nodeInfo = apiProxy.getNodeInfo();
                if (nodeInfo != null) {
                    already.setMilestone(nodeInfo.getLatestMilestoneIndex());
                }
                if (gotHash != null) {
                    Wallet wallet = Store.getWallet(context, request.getSeed());
                    Audit.setTransfersToAddresses(request.getSeed(), transfers, alreadyAddress, wallet, alreadyTransfers);
                    Audit.processNudgeAttempts(context, request.getSeed(), transfers);
                    Store.updateAccountData(context, request.getSeed(), wallet, transfers, alreadyAddress);
                }
                if (!AppService.isAppStarted()) {
                    NotificationHelper.responseNotification(context, R.drawable.nudge_orange, context.getString(R.string.notification_nudge_succeeded_title), notificationId);
                } else {
                    NotificationHelper.vibrate(context);
                }
                return nresp;
            }
        }
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.INVALID_HASH_ERROR);
        return error;
    } catch (ArgumentException e) {
        Log.e("NUDGE", "error: " + e.getMessage());
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.INVALID_HASH_ERROR);
        response = error;
    }
    return response;
}
Also used : Address(run.wallet.iota.model.Address) Wallet(run.wallet.iota.model.Wallet) ArrayList(java.util.ArrayList) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest) NetworkError(run.wallet.iota.api.responses.error.NetworkError) NudgeResponse(run.wallet.iota.api.responses.NudgeResponse) ApiResponse(run.wallet.iota.api.responses.ApiResponse) RunSendTransferResponse(jota.dto.response.RunSendTransferResponse) Transfer(run.wallet.iota.model.Transfer) ArgumentException(jota.error.ArgumentException)

Example 4 with NudgeRequest

use of run.wallet.iota.api.requests.NudgeRequest in project run-wallet-android by runplay.

the class AppService method nudgeTransaction.

public static void nudgeTransaction(Context context, Seeds.Seed seed, Transfer transfer) {
    if (Validator.isValidCaller() && Store.getCurrentSeed() != null) {
        NudgeRequest rtr = new NudgeRequest(seed, transfer);
        TaskManager rt = new TaskManager(SERVICE);
        runTask(rt, rtr);
    }
}
Also used : TaskManager(run.wallet.iota.api.TaskManager) AutoNudgeRequest(run.wallet.iota.api.requests.AutoNudgeRequest) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest)

Example 5 with NudgeRequest

use of run.wallet.iota.api.requests.NudgeRequest in project run-wallet-android by runplay.

the class UiManager method displayInfoBar.

public static void displayInfoBar(Activity context, LinearLayout forView) {
    makeElements(context);
    forView.setBackgroundColor(B.getColor(context, AppTheme.getPrimaryDark()));
    try {
        List<ApiRequest> attaching = AppService.getRunningTasks();
        List<LinearLayout> processing = new ArrayList<>();
        if (!attaching.isEmpty()) {
            Seeds.Seed currentSeed = Store.getCurrentSeed();
            for (ApiRequest req : attaching) {
                if (req instanceof SendTransferRequest) {
                    SendTransferRequest str = (SendTransferRequest) req;
                    if (str.getSeed().id.equals(currentSeed.id)) {
                        if (str.getValue() == 0) {
                            processing.add(createProcessRunningPod(context, R.drawable.tran_white, context.getString(R.string.info_new_attach), 0));
                        } else {
                            processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_transfer), str.getValue()));
                        }
                    }
                } else if (req instanceof GetAccountDataRequest) {
                    GetAccountDataRequest request = (GetAccountDataRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_refresh), 0));
                    }
                } else if (req instanceof GetNewAddressRequest) {
                    GetNewAddressRequest request = (GetNewAddressRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.tran_white, context.getString(R.string.info_new_address), 0));
                    }
                } else if (req instanceof GetFirstLoadRequest) {
                    GetFirstLoadRequest request = (GetFirstLoadRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_first_load), 0));
                    }
                } else if (req instanceof AuditAddressesRequest) {
                    AuditAddressesRequest request = (AuditAddressesRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_audit), 0));
                    }
                } else if (req instanceof ReplayBundleRequest) {
                    ReplayBundleRequest request = (ReplayBundleRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_resend), 0));
                    }
                } else if (req instanceof NudgeRequest) {
                    NudgeRequest request = (NudgeRequest) req;
                    if (request.getSeed().id.equals(currentSeed.id)) {
                        processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_nudge), 0));
                    }
                } else if (req instanceof AutoNudgeRequest) {
                    // if(request.getSeed().id.equals(currentSeed.id)) {
                    processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.auto), 0));
                // }
                } else if (req instanceof RefreshUsedAddressesRequest) {
                    // if(request.getSeed().id.equals(currentSeed.id)) {
                    processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_audit), 0));
                // }
                }
            }
        }
        forView.removeAllViews();
        if (!processing.isEmpty()) {
            for (LinearLayout addview : processing) {
                forView.addView(addview);
            }
            forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
            if (forView.getVisibility() != View.VISIBLE) {
                forView.setVisibility(View.VISIBLE);
                AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
            }
        } else if (Store.getUsedAddressCheckResult() != null) {
            Snackbar.make(forView, Store.getUsedAddressCheckResult(), Snackbar.LENGTH_LONG).show();
            Store.setUsedAddressCheckResult(null);
        } else if (Store.getCurrentSeed().warnUsed) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean showused = prefs.getBoolean(Constants.PREFERENCES_SHOW_USED, true);
            TextView messy = new TextView(context);
            if (showused) {
                messy.setText(context.getString(R.string.usedAddressWarn));
            } else {
                messy.setText(context.getString(R.string.usedAddressSettings));
            }
            messy.setTextColor(Color.WHITE);
            messy.setPadding(30, 30, 30, 30);
            messy.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent settings = new Intent(context, SettingsActivity.class);
                    context.startActivityForResult(settings, 0);
                }
            });
            forView.addView(messy);
            forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
            if (forView.getVisibility() != View.VISIBLE) {
                AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
            }
        } else {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
            if (prefs.getBoolean(Constants.PREFERENCES_SHOW_CANCELLED, true) && !prefs.getBoolean(Constants.PREFERENCES_SHOW_ATTACH, true) && Store.getTransfers().size() > 15) {
                int count = prefs.getInt(Constants.PREF_MSG_MESSY, 0);
                if (count < 2) {
                    TextView messy = new TextView(context);
                    messy.setText(context.getString(R.string.message_messy_view));
                    messy.setTextColor(Color.WHITE);
                    messy.setPadding(30, 30, 30, 30);
                    // messy.setText
                    messy.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent settings = new Intent(context, SettingsActivity.class);
                            context.startActivityForResult(settings, 0);
                        }
                    });
                    forView.addView(messy);
                    forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
                    if (forView.getVisibility() != View.VISIBLE) {
                        AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
                    }
                    prefs.edit().putInt(Constants.PREF_MSG_MESSY, ++count).commit();
                }
            } else {
                AnimationUtils.loadAnimation(context, R.anim.slide_out_to_bottom);
            }
        }
    } catch (Exception e) {
    }
}
Also used : Seeds(run.wallet.iota.model.Seeds) GetNewAddressRequest(run.wallet.iota.api.requests.GetNewAddressRequest) ArrayList(java.util.ArrayList) AuditAddressesRequest(run.wallet.iota.api.requests.AuditAddressesRequest) ApiRequest(run.wallet.iota.api.requests.ApiRequest) RefreshUsedAddressesRequest(run.wallet.iota.api.requests.RefreshUsedAddressesRequest) SendTransferRequest(run.wallet.iota.api.requests.SendTransferRequest) TextView(android.widget.TextView) SharedPreferences(android.content.SharedPreferences) AutoNudgeRequest(run.wallet.iota.api.requests.AutoNudgeRequest) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) GetAccountDataRequest(run.wallet.iota.api.requests.GetAccountDataRequest) ReplayBundleRequest(run.wallet.iota.api.requests.ReplayBundleRequest) LinearLayout(android.widget.LinearLayout) AutoNudgeRequest(run.wallet.iota.api.requests.AutoNudgeRequest) SettingsActivity(run.wallet.iota.ui.activity.SettingsActivity) GetFirstLoadRequest(run.wallet.iota.api.requests.GetFirstLoadRequest)

Aggregations

NudgeRequest (run.wallet.iota.api.requests.NudgeRequest)5 ArrayList (java.util.ArrayList)4 AutoNudgeRequest (run.wallet.iota.api.requests.AutoNudgeRequest)3 ApiResponse (run.wallet.iota.api.responses.ApiResponse)3 NudgeResponse (run.wallet.iota.api.responses.NudgeResponse)3 Transfer (run.wallet.iota.model.Transfer)3 SharedPreferences (android.content.SharedPreferences)2 RunSendTransferResponse (jota.dto.response.RunSendTransferResponse)2 ArgumentException (jota.error.ArgumentException)2 GetNewAddressRequest (run.wallet.iota.api.requests.GetNewAddressRequest)2 NetworkError (run.wallet.iota.api.responses.error.NetworkError)2 Address (run.wallet.iota.model.Address)2 Seeds (run.wallet.iota.model.Seeds)2 Wallet (run.wallet.iota.model.Wallet)2 Intent (android.content.Intent)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 HashMap (java.util.HashMap)1