Search in sources :

Example 21 with Transaction

use of org.xel.Transaction in project elastic-core-maven by OrdinaryDude.

the class GetUnconfirmedTransactionIds method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    Set<Long> accountIds = Convert.toSet(ParameterParser.getAccountIds(req, false));
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    JSONArray transactionIds = new JSONArray();
    if (accountIds.isEmpty()) {
        try (DbIterator<? extends Transaction> transactionsIterator = Nxt.getTransactionProcessor().getAllUnconfirmedTransactions(firstIndex, lastIndex)) {
            while (transactionsIterator.hasNext()) {
                Transaction transaction = transactionsIterator.next();
                transactionIds.add(transaction.getStringId());
            }
        }
    } else {
        try (FilteringIterator<? extends Transaction> transactionsIterator = new FilteringIterator<>(Nxt.getTransactionProcessor().getAllUnconfirmedTransactions(0, -1), transaction -> accountIds.contains(transaction.getSenderId()) || accountIds.contains(transaction.getRecipientId()), firstIndex, lastIndex)) {
            while (transactionsIterator.hasNext()) {
                Transaction transaction = transactionsIterator.next();
                transactionIds.add(transaction.getStringId());
            }
        }
    }
    JSONObject response = new JSONObject();
    response.put("unconfirmedTransactionIds", transactionIds);
    return response;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) FilteringIterator(org.xel.db.FilteringIterator) JSONArray(org.json.simple.JSONArray)

Example 22 with Transaction

use of org.xel.Transaction in project elastic-core-maven by OrdinaryDude.

the class SignTransaction method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    String transactionJSON = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON"));
    String transactionBytes = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
    String prunableAttachmentJSON = Convert.emptyToNull(req.getParameter("prunableAttachmentJSON"));
    Transaction.Builder builder = ParameterParser.parseTransaction(transactionJSON, transactionBytes, prunableAttachmentJSON);
    String secretPhrase = ParameterParser.getSecretPhrase(req, true);
    boolean validate = !"false".equalsIgnoreCase(req.getParameter("validate"));
    JSONObject response = new JSONObject();
    try {
        Transaction transaction = builder.build(secretPhrase);
        JSONObject signedTransactionJSON = JSONData.unconfirmedTransaction(transaction);
        if (validate) {
            transaction.validate();
            response.put("verify", transaction.verifySignature());
        }
        response.put("transactionJSON", signedTransactionJSON);
        response.put("fullHash", signedTransactionJSON.get("fullHash"));
        response.put("signatureHash", signedTransactionJSON.get("signatureHash"));
        response.put("transaction", transaction.getStringId());
        response.put("transactionBytes", Convert.toHexString(transaction.getBytes()));
        JSONData.putPrunableAttachment(response, transaction);
    } catch (NxtException.ValidationException | RuntimeException e) {
        JSONData.putException(response, e, "Incorrect unsigned transaction json or bytes");
    }
    return response;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject)

Example 23 with Transaction

use of org.xel.Transaction in project elastic-core-maven by OrdinaryDude.

the class VerifyPrunableMessage method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    Transaction transaction = Nxt.getBlockchain().getTransaction(transactionId);
    if (transaction == null) {
        return UNKNOWN_TRANSACTION;
    }
    Appendix.PrunablePlainMessage plainMessage = (Appendix.PrunablePlainMessage) ParameterParser.getPlainMessage(req, true);
    Appendix.PrunableEncryptedMessage encryptedMessage = (Appendix.PrunableEncryptedMessage) ParameterParser.getEncryptedMessage(req, null, true);
    if (plainMessage == null && encryptedMessage == null) {
        return MISSING_MESSAGE_ENCRYPTED_MESSAGE;
    }
    if (plainMessage != null && encryptedMessage != null) {
        return EITHER_MESSAGE_ENCRYPTED_MESSAGE;
    }
    if (plainMessage != null) {
        Appendix.PrunablePlainMessage myPlainMessage = transaction.getPrunablePlainMessage();
        if (myPlainMessage == null) {
            return NO_SUCH_PLAIN_MESSAGE;
        }
        if (!Arrays.equals(myPlainMessage.getHash(), plainMessage.getHash())) {
            return JSONResponses.HASHES_MISMATCH;
        }
        JSONObject response = myPlainMessage.getJSONObject();
        response.put("verify", true);
        return response;
    } else if (encryptedMessage != null) {
        Appendix.PrunableEncryptedMessage myEncryptedMessage = transaction.getPrunableEncryptedMessage();
        if (myEncryptedMessage == null) {
            return NO_SUCH_ENCRYPTED_MESSAGE;
        }
        if (!Arrays.equals(myEncryptedMessage.getHash(), encryptedMessage.getHash())) {
            return JSONResponses.HASHES_MISMATCH;
        }
        JSONObject response = myEncryptedMessage.getJSONObject();
        response.put("verify", true);
        return response;
    }
    return JSON.emptyJSON;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) Appendix(org.xel.Appendix)

Example 24 with Transaction

use of org.xel.Transaction in project elastic-core-maven by OrdinaryDude.

the class SendTransaction method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    String transactionJSON = Convert.emptyToNull(req.getParameter("transactionJSON"));
    String transactionBytes = Convert.emptyToNull(req.getParameter("transactionBytes"));
    String prunableAttachmentJSON = Convert.emptyToNull(req.getParameter("prunableAttachmentJSON"));
    JSONObject response = new JSONObject();
    try {
        Transaction.Builder builder = ParameterParser.parseTransaction(transactionJSON, transactionBytes, prunableAttachmentJSON);
        Transaction transaction = builder.build();
        Peers.sendToSomePeers(Collections.singletonList(transaction));
        response.put("transaction", transaction.getStringId());
        response.put("fullHash", transaction.getFullHash());
    } catch (NxtException.ValidationException | RuntimeException e) {
        JSONData.putException(response, e, "Failed to broadcast transaction");
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Transaction(org.xel.Transaction)

Example 25 with Transaction

use of org.xel.Transaction in project elastic-core-maven by OrdinaryDude.

the class VerifyTaggedData method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    Transaction transaction = Nxt.getBlockchain().getTransaction(transactionId);
    if (transaction == null) {
        return UNKNOWN_TRANSACTION;
    }
    Attachment.TaggedDataUpload taggedData = ParameterParser.getTaggedData(req);
    Attachment attachment = transaction.getAttachment();
    if (!(attachment instanceof Attachment.TaggedDataUpload)) {
        return INCORRECT_TRANSACTION;
    }
    Attachment.TaggedDataUpload myTaggedData = (Attachment.TaggedDataUpload) attachment;
    if (!Arrays.equals(myTaggedData.getHash(), taggedData.getHash())) {
        return HASHES_MISMATCH;
    }
    JSONObject response = myTaggedData.getJSONObject();
    response.put("verify", true);
    return response;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) Attachment(org.xel.Attachment)

Aggregations

Transaction (org.xel.Transaction)27 JSONObject (org.json.simple.JSONObject)23 JSONArray (org.json.simple.JSONArray)12 Account (org.xel.Account)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 Console (java.io.Console)2 File (java.io.File)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 InputStreamReader (java.io.InputStreamReader)2 Appendix (org.xel.Appendix)2 Attachment (org.xel.Attachment)2 Block (org.xel.Block)2 FilteringIterator (org.xel.db.FilteringIterator)2 MessageDigest (java.security.MessageDigest)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Blockchain (org.xel.Blockchain)1 NxtException (org.xel.NxtException)1