use of jota.error.ArgumentException 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();
}
use of jota.error.ArgumentException 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;
}
use of jota.error.ArgumentException in project run-wallet-android by runplay.
the class AutoNudger method main.
public static void main(String[] args) {
try {
String protocol = "";
String host = "";
String port = "";
IotaAPI api = new IotaAPI.Builder().localPoW(new PearlDiverLocalPoW()).protocol(protocol).host(host).port(port).build();
System.out.println("AutoNudger connecting to host " + api.getHost() + " Port: " + api.getPort() + " Protocol: " + api.getProtocol());
GetNodeInfoResponse response = api.getNodeInfo();
String seed1 = "VEEMLFEYESWZPGXPQLV9GPUVFWTBYXZNSDPXKLLQUQTGFVXRNWKJLDCBAAQKVEWWCDLXU9BGRTR9QCMS9";
long counter = 0;
double avgTxTime = 0;
long totalTxTime = 0;
while (1 == 1) {
long yourmilliseconds = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
Date resultdate = new Date(yourmilliseconds);
System.out.println("---------------------------------------------------------------------------------------------------------------");
System.out.println("LatestMilestoneIndex " + response.getLatestMilestoneIndex() + " LatestSolidSubtangleMilestoneIndex " + response.getLatestSolidSubtangleMilestoneIndex());
System.out.println("Tips " + response.getTips());
Random r = new Random();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9";
String addrString = "WONKYTONKY9HELLO9WORLD";
StringBuilder stringBuilder = new StringBuilder();
String finalString = stringBuilder.toString();
for (int i = 0; i < 59; i++) {
stringBuilder.append(alphabet.charAt(r.nextInt(alphabet.length())));
}
// addrString +=
// "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
addrString += stringBuilder.toString();
String address = Checksum.addChecksum(addrString);
// System.out.println(address);
List<Transfer> transfers = new ArrayList<>();
transfers.add(new Transfer(address, 0, TEST_MESSAGE, TEST_TAG));
System.out.println("new Address : " + address);
SendTransferResponse str = api.sendTransfer(seed1, 2, DEPTH, MIN_WEIGHT_MAGNITUDE, transfers, null, null, false, false);
if (str.getSuccessfully() != null && str.getSuccessfully().length > 0)
System.out.println("success? " + str.getSuccessfully()[0]);
else
System.out.println("success? " + "false");
counter++;
System.out.println("Counter tx's: " + counter);
yourmilliseconds = System.currentTimeMillis();
Date resultdate2 = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate2));
long seconds = (resultdate2.getTime() - resultdate.getTime()) / 1000;
totalTxTime += seconds;
avgTxTime = totalTxTime / counter;
System.out.println("last tx time: " + seconds + " sec");
System.out.println("average tx time: " + avgTxTime + " sec");
}
} catch (ArgumentException e) {
Log.e("SPA", "" + e.getMessage());
}
}
use of jota.error.ArgumentException in project run-wallet-android by runplay.
the class IotaMsgCore method attachToTangle.
/**
* Attaches the specified transactions (trytes) to the Tangle by doing Proof of Work.
*
* @param trunkTransaction The trunk transaction to approve.
* @param branchTransaction The branch transaction to approve.
* @param minWeightMagnitude The Proof of Work intensity.
* @param trytes A List of trytes (raw transaction data) to attach to the tangle.
*/
public GetAttachToTangleResponse attachToTangle(String trunkTransaction, String branchTransaction, Integer minWeightMagnitude, String... trytes) throws ArgumentException {
if (!InputValidator.isHash(trunkTransaction)) {
throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
}
if (!InputValidator.isHash(branchTransaction)) {
throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
}
if (!InputValidator.isArrayOfTrytes(trytes)) {
throw new ArgumentException(INVALID_TRYTES_INPUT_ERROR);
}
if (localPoW != null) {
final String[] resultTrytes = new String[trytes.length];
String previousTransaction = null;
for (int i = 0; i < trytes.length; i++) {
Transaction txn = new Transaction(trytes[i]);
txn.setTrunkTransaction(previousTransaction == null ? trunkTransaction : previousTransaction);
txn.setBranchTransaction(previousTransaction == null ? branchTransaction : trunkTransaction);
if (txn.getTag().isEmpty() || txn.getTag().matches("9*"))
txn.setTag(txn.getObsoleteTag());
txn.setAttachmentTimestamp(System.currentTimeMillis());
txn.setAttachmentTimestampLowerBound(0);
txn.setAttachmentTimestampUpperBound(3_812_798_742_493L);
resultTrytes[i] = localPoW.performPoW(txn.toTrytes(), minWeightMagnitude);
previousTransaction = new Transaction(resultTrytes[i]).getHash();
}
return new GetAttachToTangleResponse(resultTrytes);
}
final Call<GetAttachToTangleResponse> res = service.attachToTangle(IotaAttachToTangleRequest.createAttachToTangleRequest(trunkTransaction, branchTransaction, minWeightMagnitude, trytes));
return wrapCheckedException(res).body();
}
use of jota.error.ArgumentException in project run-wallet-android by runplay.
the class IotaMsg method getBundle.
/**
* Gets the associated bundle transactions of a single transaction.
* Does validation of signatures, total sum as well as bundle order.
*
* @param transaction The transaction encoded in trytes.
* @return an array of bundle, if there are multiple arrays it means that there are conflicting bundles.
* @throws ArgumentException is thrown when the specified input is not valid.
*/
public GetBundleResponse getBundle(String transaction) throws ArgumentException {
if (!InputValidator.isHash(transaction)) {
throw new ArgumentException(INVALID_HASHES_INPUT_ERROR);
}
Bundle bundle = traverseBundle(transaction, null, new Bundle());
if (bundle == null) {
throw new ArgumentException(INVALID_BUNDLE_ERROR);
}
StopWatch stopWatch = new StopWatch();
long totalSum = 0;
String bundleHash = bundle.getTransactions().get(0).getBundle();
ICurl curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
curl.reset();
List<Signature> signaturesToValidate = new ArrayList<>();
for (int i = 0; i < bundle.getTransactions().size(); i++) {
Transaction trx = bundle.getTransactions().get(i);
Long bundleValue = trx.getValue();
totalSum += bundleValue;
if (i != bundle.getTransactions().get(i).getCurrentIndex()) {
throw new ArgumentException(INVALID_BUNDLE_ERROR);
}
String trxTrytes = trx.toTrytes().substring(2187, 2187 + 162);
// Absorb bundle hash + value + timestamp + lastIndex + currentIndex trytes.
curl.absorb(Converter.trits(trxTrytes));
// Check if input transaction
if (bundleValue < 0) {
String address = trx.getAddress();
Signature sig = new Signature();
sig.setAddress(address);
sig.getSignatureFragments().add(trx.getSignatureFragments());
// Find the subsequent txs with the remaining signature fragment
for (int y = i + 1; y < bundle.getTransactions().size(); y++) {
Transaction newBundleTx = bundle.getTransactions().get(y);
// Check if new tx is part of the signature fragment
if (newBundleTx.getAddress().equals(address) && newBundleTx.getValue() == 0) {
if (sig.getSignatureFragments().indexOf(newBundleTx.getSignatureFragments()) == -1)
sig.getSignatureFragments().add(newBundleTx.getSignatureFragments());
}
}
signaturesToValidate.add(sig);
}
}
// Check for total sum, if not equal 0 return error
if (totalSum != 0)
throw new ArgumentException(INVALID_BUNDLE_SUM_ERROR);
int[] bundleFromTrxs = new int[243];
curl.squeeze(bundleFromTrxs);
String bundleFromTxString = Converter.trytes(bundleFromTrxs);
// Check if bundle hash is the same as returned by tx object
if (!bundleFromTxString.equals(bundleHash))
throw new ArgumentException(INVALID_BUNDLE_HASH_ERROR);
// Last tx in the bundle should have currentIndex === lastIndex
bundle.setLength(bundle.getTransactions().size());
if (!(bundle.getTransactions().get(bundle.getLength() - 1).getCurrentIndex() == (bundle.getTransactions().get(bundle.getLength() - 1).getLastIndex())))
throw new ArgumentException(INVALID_BUNDLE_ERROR);
// Validate the signatures
for (Signature aSignaturesToValidate : signaturesToValidate) {
String[] signatureFragments = aSignaturesToValidate.getSignatureFragments().toArray(new String[aSignaturesToValidate.getSignatureFragments().size()]);
String address = aSignaturesToValidate.getAddress();
boolean isValidSignature = new Signing(customCurl.clone()).validateSignatures(address, signatureFragments, bundleHash);
if (!isValidSignature)
throw new ArgumentException(INVALID_SIGNATURES_ERROR);
}
return GetBundleResponse.create(bundle.getTransactions(), stopWatch.getElapsedTimeMili());
}
Aggregations