use of run.wallet.iota.api.responses.NudgeResponse 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;
}
use of run.wallet.iota.api.responses.NudgeResponse 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;
}
use of run.wallet.iota.api.responses.NudgeResponse 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;
}
Aggregations