Search in sources :

Example 1 with PrunableMessage

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

the class DownloadPrunableMessage method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request, HttpServletResponse response) throws NxtException {
    long transactionId = ParameterParser.getUnsignedLong(request, "transaction", true);
    boolean retrieve = "true".equalsIgnoreCase(request.getParameter("retrieve"));
    PrunableMessage prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    if (prunableMessage == null && retrieve) {
        if (Nxt.getBlockchainProcessor().restorePrunedTransaction(transactionId) == null) {
            return PRUNED_TRANSACTION;
        }
        prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    }
    String secretPhrase = ParameterParser.getSecretPhrase(request, false);
    byte[] sharedKey = ParameterParser.getBytes(request, "sharedKey", false);
    if (sharedKey.length != 0 && secretPhrase != null) {
        return JSONResponses.either("secretPhrase", "sharedKey");
    }
    byte[] data = null;
    if (prunableMessage != null) {
        try {
            if (secretPhrase != null) {
                data = prunableMessage.decrypt(secretPhrase);
            } else if (sharedKey.length > 0) {
                data = prunableMessage.decrypt(sharedKey);
            } else {
                data = prunableMessage.getMessage();
            }
        } catch (RuntimeException e) {
            Logger.logDebugMessage("Decryption of message to recipient failed: " + e.toString());
            return JSONResponses.error("Wrong secretPhrase or sharedKey");
        }
    }
    if (data == null) {
        data = Convert.EMPTY_BYTE;
    }
    String contentDisposition = "true".equalsIgnoreCase(request.getParameter("save")) ? "attachment" : "inline";
    response.setHeader("Content-Disposition", contentDisposition + "; filename=" + Long.toUnsignedString(transactionId));
    response.setContentLength(data.length);
    try (OutputStream out = response.getOutputStream()) {
        try {
            out.write(data);
        } catch (IOException e) {
            throw new ParameterException(JSONResponses.RESPONSE_WRITE_ERROR);
        }
    } catch (IOException e) {
        throw new ParameterException(JSONResponses.RESPONSE_STREAM_ERROR);
    }
    return null;
}
Also used : OutputStream(java.io.OutputStream) PrunableMessage(org.xel.PrunableMessage) IOException(java.io.IOException)

Example 2 with PrunableMessage

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

the class GetPrunableMessage method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    String secretPhrase = ParameterParser.getSecretPhrase(req, false);
    byte[] sharedKey = ParameterParser.getBytes(req, "sharedKey", false);
    if (sharedKey.length != 0 && secretPhrase != null) {
        return JSONResponses.either("secretPhrase", "sharedKey");
    }
    boolean retrieve = "true".equalsIgnoreCase(req.getParameter("retrieve"));
    PrunableMessage prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    if (prunableMessage == null && retrieve) {
        if (Nxt.getBlockchainProcessor().restorePrunedTransaction(transactionId) == null) {
            return PRUNED_TRANSACTION;
        }
        prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    }
    if (prunableMessage != null) {
        return JSONData.prunableMessage(prunableMessage, secretPhrase, sharedKey);
    }
    return JSON.emptyJSON;
}
Also used : PrunableMessage(org.xel.PrunableMessage)

Example 3 with PrunableMessage

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

the class ReadMessage method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    boolean retrieve = "true".equalsIgnoreCase(req.getParameter("retrieve"));
    Transaction transaction = Nxt.getBlockchain().getTransaction(transactionId);
    if (transaction == null) {
        return UNKNOWN_TRANSACTION;
    }
    PrunableMessage prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    if (prunableMessage == null && (transaction.getPrunablePlainMessage() != null || transaction.getPrunableEncryptedMessage() != null) && retrieve) {
        if (Nxt.getBlockchainProcessor().restorePrunedTransaction(transactionId) == null) {
            return PRUNED_TRANSACTION;
        }
        prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    }
    JSONObject response = new JSONObject();
    Appendix.Message message = transaction.getMessage();
    Appendix.EncryptedMessage encryptedMessage = transaction.getEncryptedMessage();
    Appendix.EncryptToSelfMessage encryptToSelfMessage = transaction.getEncryptToSelfMessage();
    if (message == null && encryptedMessage == null && encryptToSelfMessage == null && prunableMessage == null) {
        return NO_MESSAGE;
    }
    if (message != null) {
        response.put("message", Convert.toString(message.getMessage(), message.isText()));
        response.put("messageIsPrunable", false);
    } else if (prunableMessage != null && prunableMessage.getMessage() != null) {
        response.put("message", Convert.toString(prunableMessage.getMessage(), prunableMessage.messageIsText()));
        response.put("messageIsPrunable", true);
    }
    String secretPhrase = ParameterParser.getSecretPhrase(req, false);
    byte[] sharedKey = ParameterParser.getBytes(req, "sharedKey", false);
    if (sharedKey.length != 0 && secretPhrase != null) {
        return JSONResponses.either("secretPhrase", "sharedKey");
    }
    if (secretPhrase != null || sharedKey.length > 0) {
        EncryptedData encryptedData = null;
        boolean isText = false;
        boolean uncompress = true;
        if (encryptedMessage != null) {
            encryptedData = encryptedMessage.getEncryptedData();
            isText = encryptedMessage.isText();
            uncompress = encryptedMessage.isCompressed();
            response.put("encryptedMessageIsPrunable", false);
        } else if (prunableMessage != null && prunableMessage.getEncryptedData() != null) {
            encryptedData = prunableMessage.getEncryptedData();
            isText = prunableMessage.encryptedMessageIsText();
            uncompress = prunableMessage.isCompressed();
            response.put("encryptedMessageIsPrunable", true);
        }
        if (encryptedData != null) {
            try {
                byte[] decrypted = null;
                if (secretPhrase != null) {
                    byte[] readerPublicKey = Crypto.getPublicKey(secretPhrase);
                    byte[] senderPublicKey = Account.getPublicKey(transaction.getSenderId());
                    byte[] recipientPublicKey = Account.getPublicKey(transaction.getRecipientId());
                    byte[] publicKey = Arrays.equals(senderPublicKey, readerPublicKey) ? recipientPublicKey : senderPublicKey;
                    if (publicKey != null) {
                        decrypted = Account.decryptFrom(publicKey, encryptedData, secretPhrase, uncompress);
                    }
                } else {
                    decrypted = Crypto.aesDecrypt(encryptedData.getData(), sharedKey);
                    if (uncompress) {
                        decrypted = Convert.uncompress(decrypted);
                    }
                }
                response.put("decryptedMessage", Convert.toString(decrypted, isText));
            } catch (RuntimeException e) {
                Logger.logDebugMessage("Decryption of message to recipient failed: " + e.toString());
                JSONData.putException(response, e, "Wrong secretPhrase or sharedKey");
            }
        }
        if (encryptToSelfMessage != null && secretPhrase != null) {
            byte[] publicKey = Crypto.getPublicKey(secretPhrase);
            try {
                byte[] decrypted = Account.decryptFrom(publicKey, encryptToSelfMessage.getEncryptedData(), secretPhrase, encryptToSelfMessage.isCompressed());
                response.put("decryptedMessageToSelf", Convert.toString(decrypted, encryptToSelfMessage.isText()));
            } catch (RuntimeException e) {
                Logger.logDebugMessage("Decryption of message to self failed: " + e.toString());
            }
        }
    }
    return response;
}
Also used : Appendix(org.xel.Appendix) Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) PrunableMessage(org.xel.PrunableMessage) EncryptedData(org.xel.crypto.EncryptedData)

Example 4 with PrunableMessage

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

the class GetAllPrunableMessages method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    final int timestamp = ParameterParser.getTimestamp(req);
    JSONObject response = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    response.put("prunableMessages", jsonArray);
    try (DbIterator<PrunableMessage> messages = PrunableMessage.getAll(firstIndex, lastIndex)) {
        while (messages.hasNext()) {
            PrunableMessage prunableMessage = messages.next();
            if (prunableMessage.getBlockTimestamp() < timestamp) {
                break;
            }
            jsonArray.add(JSONData.prunableMessage(prunableMessage, null, null));
        }
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) PrunableMessage(org.xel.PrunableMessage)

Example 5 with PrunableMessage

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

the class GetPrunableMessages method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long accountId = ParameterParser.getAccountId(req, true);
    String secretPhrase = ParameterParser.getSecretPhrase(req, false);
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    final int timestamp = ParameterParser.getTimestamp(req);
    long otherAccountId = ParameterParser.getAccountId(req, "otherAccount", false);
    JSONObject response = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    response.put("prunableMessages", jsonArray);
    try (DbIterator<PrunableMessage> messages = otherAccountId == 0 ? PrunableMessage.getPrunableMessages(accountId, firstIndex, lastIndex) : PrunableMessage.getPrunableMessages(accountId, otherAccountId, firstIndex, lastIndex)) {
        while (messages.hasNext()) {
            PrunableMessage prunableMessage = messages.next();
            if (prunableMessage.getBlockTimestamp() < timestamp) {
                break;
            }
            jsonArray.add(JSONData.prunableMessage(prunableMessage, secretPhrase, null));
        }
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) PrunableMessage(org.xel.PrunableMessage)

Aggregations

PrunableMessage (org.xel.PrunableMessage)5 JSONObject (org.json.simple.JSONObject)3 JSONArray (org.json.simple.JSONArray)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Appendix (org.xel.Appendix)1 Transaction (org.xel.Transaction)1 EncryptedData (org.xel.crypto.EncryptedData)1