Search in sources :

Example 11 with ApiResponse

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

the class MessageFirstLoadRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest request) {
    try {
        // Log.e("FIRST-TIME-MSG","called");
        StopWatch stopWatch = new StopWatch();
        MessageFirstLoadRequest firstLoadRequest = (MessageFirstLoadRequest) request;
        GetTransferResponse gtr = null;
        List<Address> allAddresses = new ArrayList<>();
        int start = 0;
        final int addcount = 5;
        while (true) {
            GetNewAddressResponse gnr = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, MsgStore.getSeed())), firstLoadRequest.getSecurity(), start, false, start + addcount, true);
            for (String add : gnr.getAddresses()) {
                // Log.e("FIRST-TIME-MSG","CALC ADDRESS: "+allAddresses+" -- "+stopWatch.getElapsedTimeSecs());
                final FindTransactionResponse tr = apiProxy.findTransactionsByAddresses(add);
                Address newaddress = new Address(add, false, true);
                if (tr.getHashes().length == 0) {
                    newaddress.setAttached(false);
                }
                allAddresses.add(newaddress);
            }
            int countempty = 0;
            for (int i = allAddresses.size() - 1; i >= 0 && i >= allAddresses.size() - 4; i--) {
                if (!allAddresses.get(i).isAttached()) {
                    countempty++;
                }
            }
            if (countempty >= 2) {
                break;
            }
            start += addcount;
        }
        List<String> transactionaddresses = new ArrayList<>();
        for (Address add : allAddresses) {
            if (add.isAttached())
                transactionaddresses.add(add.getAddress());
        }
        if (!transactionaddresses.isEmpty()) {
            try {
                Bundle[] bundles = apiProxy.bundlesFromAddresses(transactionaddresses.toArray(new String[transactionaddresses.size()]), true);
                gtr = GetTransferResponse.create(bundles, stopWatch.getElapsedTimeMili());
            } catch (Exception e) {
                Log.e("FIRST-LOAD-MSG", "ex: " + e.getMessage());
            }
        } else {
            gtr = GetTransferResponse.create(new Bundle[] {}, stopWatch.getElapsedTimeMili());
        }
        List<Transfer> transfers = new ArrayList<>();
        long seedTotal = 0;
        Wallet wallet = new Wallet(MsgStore.getSeed().id, seedTotal, System.currentTimeMillis());
        // Audit.setTransfersToAddresses(firstLoadRequest.getSeed(),gtr,transfers,allAddresses,wallet);
        MsgStore.updateMessageData(context, wallet, transfers, allAddresses);
    // AppService.generateMessageNewAddress(context);
    // if()
    } catch (ArgumentException e) {
        Log.e("FIRST-TIME-MSG", "ex: " + e.getMessage());
        return new NetworkError();
    }
    return new ApiResponse();
}
Also used : GetNewAddressResponse(jota.dto.response.GetNewAddressResponse) FindTransactionResponse(jota.dto.response.FindTransactionResponse) GetTransferResponse(jota.dto.response.GetTransferResponse) Address(run.wallet.iota.model.Address) MessageFirstLoadRequest(run.wallet.iota.api.requests.MessageFirstLoadRequest) Bundle(jota.model.Bundle) Wallet(run.wallet.iota.model.Wallet) ArrayList(java.util.ArrayList) NetworkError(run.wallet.iota.api.responses.error.NetworkError) ArgumentException(jota.error.ArgumentException) ApiResponse(run.wallet.iota.api.responses.ApiResponse) StopWatch(jota.utils.StopWatch) Transfer(run.wallet.iota.model.Transfer) ArgumentException(jota.error.ArgumentException)

Example 12 with ApiResponse

use of run.wallet.iota.api.responses.ApiResponse 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 13 with ApiResponse

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

the class RefreshUsedAddressesHandler method handle.

@Override
public ApiResponse handle(ApiRequest inrequest) {
    RefreshUsedAddressesRequest request = (RefreshUsedAddressesRequest) inrequest;
    NodeInfoRequestHandler.getNodeInfo(apiProxy, context);
    if (Store.isNodeSynced()) {
        if (request.getSeed() != null) {
            // Log.e("CHK-USED","SINGLE");
            checkUsedAddressForSeed(request.getSeed(), true);
        } else {
            Store.updateLastUsedCheck(context);
            for (int i = 0; i < Store.getSeedList().size(); i++) {
                // Log.e("CHK-USED","MULTI: "+i);
                checkUsedAddressForSeed(Store.getSeedList().get(i), false);
            }
        }
    }
    return new ApiResponse();
}
Also used : RefreshUsedAddressesRequest(run.wallet.iota.api.requests.RefreshUsedAddressesRequest) ApiResponse(run.wallet.iota.api.responses.ApiResponse)

Example 14 with ApiResponse

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

the class IotaApiProvider method processRequest.

@Override
public ApiResponse processRequest(ApiRequest apiRequest) {
    ApiResponse response = null;
    try {
        // Log.e("iotaApiProv",apiRequest.getClass()+" - "+this.requestHandlerMap.containsKey(apiRequest.getClass()));
        if (this.requestHandlerMap.containsKey(apiRequest.getClass())) {
            RequestHandler requestHandler = this.requestHandlerMap.get(apiRequest.getClass());
            response = requestHandler.handle(apiRequest);
        }
    } catch (IllegalAccessError e) {
        NetworkError error = new NetworkError();
        error.setErrorType(NetworkErrorType.ACCESS_ERROR);
        error.setMessage(e.getMessage());
        response = error;
        // if(error.getErrorType()==401 && e.getMessage().contains("getNeighbours"))
        Log.e("ERR-API1", "" + e.getMessage());
    } catch (Exception e) {
        Log.e("ERR-API2", e.toString() + " -- " + e.getMessage());
        response = new NetworkError();
    }
    return response == null ? new NetworkError() : response;
}
Also used : GetAccountDataRequestHandler(run.wallet.iota.api.handler.GetAccountDataRequestHandler) GetBundleRequestHandler(run.wallet.iota.api.handler.GetBundleRequestHandler) RemoveNeighborsRequestHandler(run.wallet.iota.api.handler.RemoveNeighborsRequestHandler) GetNewAddressRequestHandler(run.wallet.iota.api.handler.GetNewAddressRequestHandler) GetFirstLoadRequestHandler(run.wallet.iota.api.handler.GetFirstLoadRequestHandler) GetBalanceAndFormatRequestHandler(run.wallet.iota.api.handler.GetBalanceAndFormatRequestHandler) ReplayBundleRequestHandler(run.wallet.iota.api.handler.ReplayBundleRequestHandler) NudgeRequestHandler(run.wallet.iota.api.handler.NudgeRequestHandler) FindTransactionsRequestHandler(run.wallet.iota.api.handler.FindTransactionsRequestHandler) GetNeighborsRequestHandler(run.wallet.iota.api.handler.GetNeighborsRequestHandler) AddressSecurityChangeRequestHandler(run.wallet.iota.api.handler.AddressSecurityChangeRequestHandler) AuditAddressesRequestHandler(run.wallet.iota.api.handler.AuditAddressesRequestHandler) RequestHandler(run.wallet.iota.api.handler.RequestHandler) NodeInfoRequestHandler(run.wallet.iota.api.handler.NodeInfoRequestHandler) AddNeighborsRequestHandler(run.wallet.iota.api.handler.AddNeighborsRequestHandler) SendTransferRequestHandler(run.wallet.iota.api.handler.SendTransferRequestHandler) NetworkError(run.wallet.iota.api.responses.error.NetworkError) ApiResponse(run.wallet.iota.api.responses.ApiResponse)

Example 15 with ApiResponse

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

the class IotaRequestTask 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("NODE ON:",node.protocol+":"+node.ip+":"+node.port);
        if (node != null) {
            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 req at", start.getTime() + "");
            }
            ApiRequest apiRequest = params[0];
            tag = apiRequest.getClass().getCanonicalName();
            ApiProvider apiProvider = new IotaApiProvider(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) Date(java.util.Date) ApiResponse(run.wallet.iota.api.responses.ApiResponse)

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