Search in sources :

Example 16 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse in project run-wallet-android by runplay.

the class MessageRequestTask method doInBackground.

@Override
protected ApiResponse doInBackground(ApiRequest... params) {
    Context context = this.context.get();
    if (context != null) {
        // SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        // String protocol = prefs.getString(Constants.PREFERENCE_NODE_PROTOCOL, Constants.PREFERENCE_NODE_DEFAULT_PROTOCOL);
        // String host = prefs.getString(Constants.PREFERENCE_NODE_IP, Constants.PREFERENCE_NODE_DEFAULT_IP);
        // int port = Integer.parseInt(prefs.getString(Constants.PREFERENCE_NODE_PORT, Constants.PREFERENCE_NODE_DEFAULT_PORT));
        Nodes.Node node = Store.getNode();
        // Log.e("MESSAGE REQUEST NODE:",node.protocol+":"+node.ip+":"+node.port);
        String protocol = node.protocol;
        String host = node.ip;
        int port = node.port;
        // if (IOTA.DEBUG) {
        // Log.e("ApiRequest", params[0].toString());
        // start = new Date();
        // Log.e("started at", start.getTime() + "");
        // }
        ApiRequest apiRequest = params[0];
        tag = apiRequest.getClass().getCanonicalName();
        ApiProvider apiProvider = new IotaMessageApiProvider(protocol, host, port, context);
        ApiResponse response = apiProvider.processRequest(apiRequest);
        return response;
    }
    TaskManager.removeTask(tag);
    return null;
}
Also used : Context(android.content.Context) ApiRequest(run.wallet.iota.api.requests.ApiRequest) Nodes(run.wallet.iota.model.Nodes) ApiResponse(run.wallet.iota.api.responses.ApiResponse)

Example 17 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse in project run-wallet-android by runplay.

the class AuditAddressesRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest inrequest) {
    AuditAddressesRequest request = (AuditAddressesRequest) inrequest;
    // check less than 2, 1..iteself, and no others
    if (AppService.countSeedRunningTasks(request.getSeed()) < 2) {
        List<Address> addresses = Store.getAddresses(context, request.getSeed());
        List<Address> getaddresses = new ArrayList<>();
        if (!addresses.isEmpty()) {
            for (Address address : addresses) {
                if (!address.isAttached()) {
                    getaddresses.add(address);
                }
            }
        }
        if (!getaddresses.isEmpty()) {
            GetNodeInfoResponse info = apiProxy.getNodeInfo();
            if (info != null && info.getLatestMilestoneIndex() == info.getLatestSolidSubtangleMilestoneIndex()) {
                // List<String> addstr = new ArrayList<>();
                int count = 0;
                for (Address add : getaddresses) {
                    FindTransactionResponse tr1 = null;
                    try {
                        tr1 = apiProxy.findTransactionsByAddresses(add.getAddress());
                    } catch (Exception e) {
                    }
                    if (tr1 != null) {
                        if (tr1.getHashes().length == 0) {
                            AppService.attachNewAddress(context, request.getSeed(), add.getAddress());
                        } else {
                            add.setAttached(true);
                            Store.updateAddress(context, request.getSeed(), add);
                            AppService.refreshEvent();
                        }
                    // addstr.add(add.getAddress());
                    }
                    if (++count > 1 || tr1 == null)
                        ;
                    break;
                }
            }
        } else {
            int countemptyattached = 0;
            for (Address address : addresses) {
                if (address.isAttached() && address.getValue() == 0 && !address.isUsed() && !address.isPig()) {
                    countemptyattached++;
                }
            }
            Store.loadDefaults(context);
            int countmin = Store.getAutoAttach();
            if (countemptyattached < countmin) {
                // Log.e("AUDIT","Gen new address");
                try {
                    GetNewAddressRequest gnr = new GetNewAddressRequest(request.getSeed());
                    gnr.setIndex(addresses.size());
                    final GetNewAddressResponse gna = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, request.getSeed())), gnr.getSecurity(), addresses.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);
                    AppService.auditAddressesWithDelay(context, request.getSeed());
                } catch (Exception e) {
                }
            } else {
            // Log.e("AUDIT","no address need creating");
            }
        }
    } else {
    // Log.e("AUDIT","Called but skipped.. hopefully good");
    }
    return new ApiResponse();
}
Also used : FindTransactionResponse(jota.dto.response.FindTransactionResponse) GetNewAddressResponse(jota.dto.response.GetNewAddressResponse) Address(run.wallet.iota.model.Address) GetNewAddressRequest(run.wallet.iota.api.requests.GetNewAddressRequest) ArrayList(java.util.ArrayList) AuditAddressesRequest(run.wallet.iota.api.requests.AuditAddressesRequest) ApiResponse(run.wallet.iota.api.responses.ApiResponse) GetNodeInfoResponse(jota.dto.response.GetNodeInfoResponse)

Example 18 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse in project run-wallet-android by runplay.

the class MessageSendRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest request) {
    int notificationId = Utils.createNewID();
    ApiResponse response;
    Log.e("IotaMsg", "MessageSendRequestHandler handle: " + request.toString());
    // if we generate a new address the tag == address
    if (((MessageSendRequest) request).getValue().equals("0") && ((MessageSendRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG)) {
        NotificationHelper.requestNotification(context, R.drawable.ic_add, context.getString(R.string.notification_attaching_new_address_request_title), notificationId);
    } else {
        NotificationHelper.requestNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_request_title), notificationId);
    }
    try {
        response = new MessageSendResponse(context, ((MessageSendRequest) request).getSeed(), apiProxy.sendTransfer(String.valueOf(Store.getSeedRaw(context, ((MessageSendRequest) request).getSeed())), ((MessageSendRequest) request).getSecurity(), ((MessageSendRequest) request).getDepth(), ((MessageSendRequest) request).getMinWeightMagnitude(), ((MessageSendRequest) request).prepareTransfer(), // inputs
        null, // remainder address
        null, false, true));
    } catch (ArgumentException | IllegalAccessError e) {
        NetworkError error = new NetworkError();
        Log.e("IotaMsg", "MessageSendRequestHandler exception: " + e.getMessage());
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            Log.e("IotaMsg", "MessageSendRequestHandler exception: 1");
            mNotificationManager.cancel(notificationId);
        }
        if (e instanceof ArgumentException) {
            Log.e("IotaMsg", "MessageSendRequestHandler exception: 2: " + ((ArgumentException) e).getMessage());
            if (e.getMessage().contains("Sending to a used address.") || e.getMessage().contains("Private key reuse detect!")) {
                final Activity activity = (Activity) context;
                Bundle bundle = new Bundle();
                bundle.putString("error", e.getMessage());
                KeyReuseDetectedDialog dialog = new KeyReuseDetectedDialog();
                dialog.setArguments(bundle);
                dialog.show(activity.getFragmentManager(), null);
                error.setErrorType(NetworkErrorType.KEY_REUSE_ERROR);
            }
        }
        if (e instanceof IllegalAccessError) {
            Log.e("IotaMsg", "MessageSendRequestHandler exception: 3: " + ((IllegalAccessError) e).getMessage());
            error.setErrorType(NetworkErrorType.ACCESS_ERROR);
            if (((MessageSendRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG))
                NotificationHelper.responseNotification(context, R.drawable.ic_error, context.getString(R.string.notification_address_attach_to_tangle_blocked_title), notificationId);
            else
                NotificationHelper.responseNotification(context, R.drawable.ic_error, context.getString(R.string.notification_transfer_attach_to_tangle_blocked_title), notificationId);
        } else {
            Log.e("IotaMsg", "MessageSendRequestHandler exception: 4");
            if (error.getErrorType() != NetworkErrorType.KEY_REUSE_ERROR) {
                error.setErrorType(NetworkErrorType.NETWORK_ERROR);
            }
            if (((MessageSendRequest) request).getValue().equals("0") && ((MessageSendRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG)) {
                NotificationHelper.responseNotification(context, R.drawable.ic_address, context.getString(R.string.notification_attaching_new_address_response_failed_title), notificationId);
            } else {
                NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_failed_title), notificationId);
            }
        }
        Log.e("IotaMsg", "MessageSendRequestHandler exception: " + error.toString());
        response = error;
    }
    if (response instanceof SendTransferResponse && ((MessageSendRequest) request).getValue().equals("0") && ((MessageSendRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG)) {
        Log.e("IotaMsg", "MessageSendRequestHandler instanceof SendTransferResponse");
        if (Arrays.asList(((SendTransferResponse) response).getSuccessfully()).contains(true))
            NotificationHelper.responseNotification(context, R.drawable.ic_address, context.getString(R.string.notification_attaching_new_address_response_succeeded_title), notificationId);
        else
            NotificationHelper.responseNotification(context, R.drawable.ic_address, context.getString(R.string.notification_attaching_new_address_response_failed_title), notificationId);
    } else if (response instanceof SendTransferResponse) {
        Log.e("IotaMsg", "MessageSendRequestHandler instanceof SendTransferResponse2");
        if (Arrays.asList(((SendTransferResponse) response).getSuccessfully()).contains(true))
            NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_succeeded_title), notificationId);
        else
            NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_failed_title), notificationId);
    }
    return response;
}
Also used : SendTransferResponse(run.wallet.iota.api.responses.SendTransferResponse) NotificationManager(android.app.NotificationManager) Bundle(android.os.Bundle) NetworkError(run.wallet.iota.api.responses.error.NetworkError) Activity(android.app.Activity) ApiResponse(run.wallet.iota.api.responses.ApiResponse) MessageSendResponse(run.wallet.iota.api.responses.MessageSendResponse) MessageSendRequest(run.wallet.iota.api.requests.MessageSendRequest) ArgumentException(jota.error.ArgumentException) KeyReuseDetectedDialog(run.wallet.iota.ui.dialog.KeyReuseDetectedDialog)

Example 19 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse in project run-wallet-android by runplay.

the class ReplayBundleRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest request) {
    ApiResponse response;
    int notificationId = Utils.createNewID();
    // NotificationHelper.requestNotification(context, R.drawable.send_white, context.getString(R.string.notification_replay_bundle_request_title), notificationId);
    if (Store.getNodeInfo() == null) {
        NodeInfoRequestHandler.getNodeInfo(apiProxy, context);
    }
    try {
        RunReplayBundleResponse jresponse = apiProxy.replayBundle(((ReplayBundleRequest) request).getHash(), ((ReplayBundleRequest) request).getDepth(), ((ReplayBundleRequest) request).getMinWeightMagnitude());
        response = new ReplayBundleResponse(context, ((ReplayBundleRequest) request).getSeed(), jresponse);
    } catch (ArgumentException e) {
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.INVALID_HASH_ERROR);
        response = error;
    }
    if (!AppService.isAppStarted()) {
        if (response instanceof ReplayBundleResponse && Arrays.asList(((ReplayBundleResponse) response).getSuccessfully()).contains(true)) {
            NotificationHelper.responseNotification(context, R.drawable.ic_replay, context.getString(R.string.notification_replay_bundle_response_succeeded_title), notificationId);
        } else if (response instanceof NetworkError) {
            NotificationHelper.responseNotification(context, R.drawable.ic_replay, context.getString(R.string.notification_replay_bundle_response_failed_title), notificationId);
        }
    } else {
        NotificationHelper.vibrate(context);
    }
    return response;
}
Also used : ReplayBundleResponse(run.wallet.iota.api.responses.ReplayBundleResponse) RunReplayBundleResponse(jota.dto.response.RunReplayBundleResponse) ReplayBundleRequest(run.wallet.iota.api.requests.ReplayBundleRequest) NetworkError(run.wallet.iota.api.responses.error.NetworkError) ArgumentException(jota.error.ArgumentException) RunReplayBundleResponse(jota.dto.response.RunReplayBundleResponse) ApiResponse(run.wallet.iota.api.responses.ApiResponse)

Example 20 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse in project run-wallet-android by runplay.

the class SendTransferRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest request) {
    int notificationId = Utils.createNewID();
    ApiResponse response;
    try {
        List<Transfer> transfers = ((SendTransferRequest) request).prepareTransfers();
        List<Input> inputs = null;
        String remainder = null;
        if (((SendTransferRequest) request).getFromAddresses() != null) {
            inputs = new ArrayList<>();
            for (Address address : ((SendTransferRequest) request).getFromAddresses()) {
                Input inp = new Input(address.getAddress(), address.getValue(), address.getIndex(), address.getSecurity());
                inputs.add(inp);
            }
            remainder = ((SendTransferRequest) request).getRemainder().getAddress();
        }
        try {
            response = new SendTransferResponse(context, ((SendTransferRequest) request).getSeed(), apiProxy.sendTransfer(String.valueOf(Store.getSeedRaw(context, ((SendTransferRequest) request).getSeed())), ((SendTransferRequest) request).getSecurity(), ((SendTransferRequest) request).getDepth(), ((SendTransferRequest) request).getMinWeightMagnitude(), transfers, // inputs
            inputs, // remainder address
            remainder, true, false));
        } catch (Exception e) {
            // currently this just waits 10 seconds and re-tries
            try {
                wait(10000);
            } catch (Exception ew) {
            }
            response = new SendTransferResponse(context, ((SendTransferRequest) request).getSeed(), apiProxy.sendTransfer(String.valueOf(Store.getSeedRaw(context, ((SendTransferRequest) request).getSeed())), ((SendTransferRequest) request).getSecurity(), ((SendTransferRequest) request).getDepth(), ((SendTransferRequest) request).getMinWeightMagnitude(), transfers, // inputs
            inputs, // remainder address
            remainder, true, false));
        }
        AppService.setFastMode();
    } catch (ArgumentException | IllegalAccessError e) {
        // Log.e("SNT","ex: "+e.getMessage());
        NetworkError error = new NetworkError();
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.cancel(notificationId);
        }
        if (e instanceof IllegalStateException) {
            // if (e.getMessage().contains("Sending to a used address.") || e.getMessage().contains("Private key reuse detect!")) {
            final Activity activity = (Activity) context;
            Bundle bundle = new Bundle();
            bundle.putString("error", e.getMessage());
            KeyReuseDetectedDialog dialog = new KeyReuseDetectedDialog();
            dialog.setArguments(bundle);
            dialog.show(activity.getFragmentManager(), null);
            error.setErrorType(NetworkErrorType.KEY_REUSE_ERROR);
        // }
        }
        if (e instanceof ArgumentException) {
            if (e.getMessage().contains("Sending to a used address.") || e.getMessage().contains("Private key reuse detect!")) {
                final Activity activity = (Activity) context;
                Bundle bundle = new Bundle();
                bundle.putString("error", e.getMessage());
                KeyReuseDetectedDialog dialog = new KeyReuseDetectedDialog();
                dialog.setArguments(bundle);
                dialog.show(activity.getFragmentManager(), null);
                error.setErrorType(NetworkErrorType.KEY_REUSE_ERROR);
            }
        }
        if (e instanceof IllegalAccessError) {
            error.setErrorType(NetworkErrorType.ACCESS_ERROR);
            if (((SendTransferRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG))
                NotificationHelper.responseNotification(context, R.drawable.ic_error, context.getString(R.string.notification_address_attach_to_tangle_blocked_title), notificationId);
            else
                NotificationHelper.responseNotification(context, R.drawable.ic_error, context.getString(R.string.notification_transfer_attach_to_tangle_blocked_title), notificationId);
        } else {
            if (error.getErrorType() != NetworkErrorType.KEY_REUSE_ERROR) {
                error.setErrorType(NetworkErrorType.NETWORK_ERROR);
            }
            if (((SendTransferRequest) request).getValue() == 0 && ((SendTransferRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG)) {
                NotificationHelper.responseNotification(context, R.drawable.ic_address, context.getString(R.string.notification_attaching_new_address_response_failed_title), notificationId);
            } else {
                NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_failed_title), notificationId);
            }
        }
        response = error;
    }
    if (response instanceof SendTransferResponse && ((SendTransferRequest) request).getValue() == 0 && ((SendTransferRequest) request).getTag().equals(Constants.NEW_ADDRESS_TAG)) {
    } else if (response instanceof SendTransferResponse) {
        if (Arrays.asList(((SendTransferResponse) response).getSuccessfully()).contains(true)) {
            if (AppService.isAppStarted()) {
                NotificationHelper.vibrate(context);
            } else {
                NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_succeeded_title), notificationId);
            }
        } else {
            NotificationHelper.responseNotification(context, R.drawable.ic_fab_send, context.getString(R.string.notification_send_transfer_response_failed_title), notificationId);
        }
    }
    return response;
}
Also used : SendTransferResponse(run.wallet.iota.api.responses.SendTransferResponse) Address(run.wallet.iota.model.Address) NotificationManager(android.app.NotificationManager) Bundle(android.os.Bundle) NetworkError(run.wallet.iota.api.responses.error.NetworkError) Activity(android.app.Activity) ApiResponse(run.wallet.iota.api.responses.ApiResponse) ArgumentException(jota.error.ArgumentException) SendTransferRequest(run.wallet.iota.api.requests.SendTransferRequest) Input(jota.model.Input) Transfer(jota.model.Transfer) ArgumentException(jota.error.ArgumentException) KeyReuseDetectedDialog(run.wallet.iota.ui.dialog.KeyReuseDetectedDialog)

Aggregations

ApiResponse (run.wallet.iota.api.responses.ApiResponse)20 NetworkError (run.wallet.iota.api.responses.error.NetworkError)13 ArgumentException (jota.error.ArgumentException)11 Address (run.wallet.iota.model.Address)8 ArrayList (java.util.ArrayList)6 Transfer (run.wallet.iota.model.Transfer)5 Wallet (run.wallet.iota.model.Wallet)4 Activity (android.app.Activity)3 Context (android.content.Context)3 Bundle (android.os.Bundle)3 GetNewAddressResponse (jota.dto.response.GetNewAddressResponse)3 StopWatch (jota.utils.StopWatch)3 RequestHandler (run.wallet.iota.api.handler.RequestHandler)3 ApiRequest (run.wallet.iota.api.requests.ApiRequest)3 GetNewAddressRequest (run.wallet.iota.api.requests.GetNewAddressRequest)3 NudgeRequest (run.wallet.iota.api.requests.NudgeRequest)3 NudgeResponse (run.wallet.iota.api.responses.NudgeResponse)3 NotificationManager (android.app.NotificationManager)2 Date (java.util.Date)2 FindTransactionResponse (jota.dto.response.FindTransactionResponse)2