Search in sources :

Example 1 with GetNewAddressResponse

use of jota.dto.response.GetNewAddressResponse in project run-wallet-android by runplay.

the class GetFirstLoadRequestHandler method handle.

@Override
public ApiResponse handle(ApiRequest request) {
    FirstTimeHolder holder = new FirstTimeHolder();
    // StopWatch stopWatch = new StopWatch();
    GetFirstLoadRequest firstLoadRequest = (GetFirstLoadRequest) request;
    Wallet wallet = null;
    List<Address> allAddresses = new ArrayList<>();
    List<Transfer> transfers = new ArrayList<>();
    long started = System.currentTimeMillis();
    if (!firstLoadRequest.getSeed().isappgenerated) {
        holders.put(firstLoadRequest.getSeed().id, holder);
        while (holder.userConfirmedBalance == null || started < System.currentTimeMillis() - 60000) {
            try {
                this.wait(1000);
            } catch (Exception e) {
            }
        }
    } else {
        holder.userConfirmedBalance = false;
        holders.put(firstLoadRequest.getSeed().id, holder);
    }
    boolean userDeclaredBalance = holder.userConfirmedBalance == null ? false : holder.userConfirmedBalance.booleanValue();
    holder.userConfirmedBalance = userDeclaredBalance ? Boolean.TRUE : Boolean.FALSE;
    // Log.e("FL","uc="+holder.userConfirmedBalance+" - ud="+userDeclaredBalance);
    if (!userDeclaredBalance || firstLoadRequest.getSeed().isappgenerated) {
        try {
            final GetNewAddressResponse gna = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, firstLoadRequest.getSeed())), firstLoadRequest.getSecurity(), 0, false, 1, false);
            for (String add : gna.getAddresses()) {
                Address newaddress = new Address(add, false, false);
                newaddress.setIndexName(1);
                allAddresses.add(newaddress);
            }
        } catch (Exception e) {
        }
        wallet = new Wallet(firstLoadRequest.getSeed().id, 0, System.currentTimeMillis());
    } else {
        long timestamp = System.currentTimeMillis();
        holder.showWaitMessage = true;
        holder.predictaddress = 0;
        Map<Integer, Address> already = new HashMap<>();
        int start = 0;
        boolean stop = false;
        List<Bundle> allbundles = new ArrayList<>();
        Map<String, Boolean> hasalready = new HashMap<>();
        wallet = new Wallet(((GetFirstLoadRequest) request).getSeed().id, 0, System.currentTimeMillis());
        List<Transfer> addInTransfers = new ArrayList<>();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final int maxAddresses = Sf.toInt(prefs.getString(Constants.PREF_FIRST_LOAD_ATTEMPTS, "" + Constants.PREF_FIRST_LOAD_ATTEMPTS_DEFAULT));
        final int stopWhenCountEmpty = Sf.toInt(prefs.getString(Constants.PREF_FIRST_LOAD_RANGE, "" + Constants.PREF_FIRST_LOAD_RANGE_DEFAULT));
        while (!stop) {
            Address alreadyAddress = already.get(start);
            Map<String, Boolean> snapshotBalanceAlreadyAddresses = new HashMap();
            boolean foundTransfers = false;
            if (alreadyAddress != null) {
                holder.countaddress++;
                alreadyAddress.setIndex(start);
                alreadyAddress.setIndexName(start + 1);
                allAddresses.add(alreadyAddress);
            } else {
                Address newaddress = null;
                long useBalance = 0L;
                try {
                    GetNewAddressResponse gnr = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, firstLoadRequest.getSeed())), firstLoadRequest.getSecurity(), start, false, 1, false);
                    String add = gnr.getAddresses().get(0);
                    // Log.e("FIRST-TIME", "CALC ADDRESS: " + add + " -- " + start+"-");
                    newaddress = new Address(add, false, false);
                    holder.countaddress++;
                    newaddress.setIndex(start);
                    newaddress.setIndexName(start + 1);
                    allAddresses.add(newaddress);
                    List<String> tmpadd = new ArrayList<>();
                    tmpadd.add(newaddress.getAddress());
                    GetBalancesResponse gbal = apiProxy.getBalances(100, tmpadd);
                    useBalance = Sf.toLong(gbal.getBalances()[0]);
                    newaddress.setValue(useBalance);
                    newaddress.setLastMilestone(gbal.getMilestoneIndex());
                    newaddress.setAttached(true);
                    Bundle[] bundles = apiProxy.bundlesFromAddresses(new String[] { newaddress.getAddress() }, true);
                    if (bundles != null && bundles.length > 0) {
                        newaddress.setAttached(true);
                        foundTransfers = true;
                        for (int i = 0; i < bundles.length; i++) {
                            String hash = bundles[i].getTransactions().get(0).getHash();
                            if (hasalready.get(hash) == null) {
                                hasalready.put(hash, true);
                                allbundles.add(bundles[i]);
                            }
                        }
                    }
                    long oldWallet = wallet.getBalance();
                    long expecting = oldWallet + useBalance;
                    holder.predictaddress = expecting;
                    transfers.clear();
                    Audit.bundlePopulateTransfers(allbundles.toArray(new Bundle[allbundles.size()]), transfers, allAddresses);
                    Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
                    if (wallet.getBalance() != expecting && useBalance > 0) {
                        if (snapshotBalanceAlreadyAddresses.get(newaddress.getAddress()) == null) {
                            long theBalance = expecting - wallet.getBalance();
                            // Log.e("MK-SNAP","snap::: set-bal: "+theBalance+" - wallet:"+wallet.getBalance()+" - expect:"+expecting+" - add-bal"+useBalance+" - snap-add-tran-value:"+newaddress.getValue()+"  - on-address:"+newaddress.getAddress());
                            Transfer addInTran = new Transfer(timestamp, newaddress.getAddress(), "SNAP" + SeedRandomGenerator.generateNewSeed().substring(0, 23), true, theBalance, "Snapshot balance confirmed", "SNAP999999999999999999");
                            TransferTransaction tt = new TransferTransaction(newaddress.getAddress(), theBalance);
                            List<TransferTransaction> tmptt = new ArrayList<>();
                            tmptt.add(tt);
                            addInTran.setTransactions(tmptt);
                            addInTransfers.add(addInTran);
                            Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
                        }
                    }
                    snapshotBalanceAlreadyAddresses.put(newaddress.getAddress(), true);
                    holder.counttransfers = transfers.size();
                } catch (Exception e) {
                    Log.e("ERR-FLR020", "address index: " + start + ", ex: " + e.getMessage());
                }
            }
            int lastComplete = 0;
            // boolean hasLastComplete=false;
            for (int i = 0; i < allAddresses.size(); i++) {
                Address tmpadd = allAddresses.get(i);
                if (tmpadd.getValue() != 0) {
                    lastComplete = i;
                }
            }
            // Log.e("WALLET","current value: "+wallet.getBalance()+" - "+wallet.getBalancePendingIn()+" -- "+wallet.getBalancePendingOut());
            int countempty = 0;
            if (!foundTransfers && lastComplete > 0 && allAddresses.size() >= stopWhenCountEmpty) {
                for (int i = allAddresses.size() - 1; i >= 0 && i > lastComplete; i--) {
                    countempty++;
                }
            }
            start++;
            // Log.e("COUNT_EMPTY",allAddresses.size()+" addresses, empty: "+countempty);
            if (countempty >= stopWhenCountEmpty || start > maxAddresses) {
                stop = true;
            }
        }
        for (Transfer tran : transfers) {
            if (!tran.getTransactions().isEmpty()) {
                if (tran.getTimestamp() < timestamp)
                    timestamp = tran.getTimestamp();
            }
        }
        timestamp = timestamp - 600000;
        for (Transfer addIn : addInTransfers) {
            addIn.setTimestamp(--timestamp);
        }
        allAddresses = allAddresses.subList(0, allAddresses.size() - (stopWhenCountEmpty));
        Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
    }
    boolean hasTransfer = false;
    for (int i = allAddresses.size() - 1; i >= 0; i--) {
        Address address = allAddresses.get(i);
        if (address.getValue() != 0 || address.getPendingValue() != 0) {
            hasTransfer = true;
        } else if (hasTransfer) {
            address.setUsed(true);
        }
    }
    Store.setAccountData(context, ((GetFirstLoadRequest) request).getSeed(), wallet, transfers, allAddresses);
    holder.isFinished = true;
    AppService.auditAddressesWithDelay(context, ((GetFirstLoadRequest) request).getSeed());
    return new GetFirstLoadResponse();
}
Also used : GetNewAddressResponse(jota.dto.response.GetNewAddressResponse) GetBalancesResponse(jota.dto.response.GetBalancesResponse) Address(run.wallet.iota.model.Address) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SharedPreferences(android.content.SharedPreferences) Wallet(run.wallet.iota.model.Wallet) Bundle(jota.model.Bundle) Transfer(run.wallet.iota.model.Transfer) GetFirstLoadResponse(run.wallet.iota.api.responses.GetFirstLoadResponse) TransferTransaction(run.wallet.iota.model.TransferTransaction) GetFirstLoadRequest(run.wallet.iota.api.requests.GetFirstLoadRequest)

Example 2 with GetNewAddressResponse

use of jota.dto.response.GetNewAddressResponse 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 GetNewAddressResponse

use of jota.dto.response.GetNewAddressResponse 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 4 with GetNewAddressResponse

use of jota.dto.response.GetNewAddressResponse 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)

Aggregations

ArrayList (java.util.ArrayList)4 GetNewAddressResponse (jota.dto.response.GetNewAddressResponse)4 Address (run.wallet.iota.model.Address)4 ApiResponse (run.wallet.iota.api.responses.ApiResponse)3 Transfer (run.wallet.iota.model.Transfer)3 Wallet (run.wallet.iota.model.Wallet)3 FindTransactionResponse (jota.dto.response.FindTransactionResponse)2 ArgumentException (jota.error.ArgumentException)2 Bundle (jota.model.Bundle)2 GetNewAddressRequest (run.wallet.iota.api.requests.GetNewAddressRequest)2 NetworkError (run.wallet.iota.api.responses.error.NetworkError)2 SharedPreferences (android.content.SharedPreferences)1 HashMap (java.util.HashMap)1 GetBalancesResponse (jota.dto.response.GetBalancesResponse)1 GetNodeInfoResponse (jota.dto.response.GetNodeInfoResponse)1 GetTransferResponse (jota.dto.response.GetTransferResponse)1 RunSendTransferResponse (jota.dto.response.RunSendTransferResponse)1 StopWatch (jota.utils.StopWatch)1 AuditAddressesRequest (run.wallet.iota.api.requests.AuditAddressesRequest)1 GetFirstLoadRequest (run.wallet.iota.api.requests.GetFirstLoadRequest)1