Search in sources :

Example 16 with Transaction

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

the class UnlockAccount method processRequest.

@Override
JSONStreamAware processRequest(HttpServletRequest req, User user) throws IOException {
    String secretPhrase = req.getParameter("secretPhrase");
    // lock all other instances of this account being unlocked
    Users.getAllUsers().forEach(u -> {
        if (secretPhrase.equals(u.getSecretPhrase())) {
            u.lockAccount();
            if (!u.isInactive()) {
                u.enqueue(LOCK_ACCOUNT);
            }
        }
    });
    long accountId = user.unlockAccount(secretPhrase);
    JSONObject response = new JSONObject();
    response.put("response", "unlockAccount");
    response.put("account", Long.toUnsignedString(accountId));
    if (secretPhrase.length() < 30) {
        response.put("secretPhraseStrength", 1);
    } else {
        response.put("secretPhraseStrength", 5);
    }
    Account account = Account.getAccount(accountId);
    if (account == null) {
        response.put("balanceNQT", 0);
    } else {
        response.put("balanceNQT", account.getUnconfirmedBalanceNQT());
        JSONArray myTransactions = new JSONArray();
        byte[] accountPublicKey = Account.getPublicKey(accountId);
        try (DbIterator<? extends Transaction> transactions = Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) {
            while (transactions.hasNext()) {
                Transaction transaction = transactions.next();
                if (Arrays.equals(transaction.getSenderPublicKey(), accountPublicKey)) {
                    JSONObject myTransaction = new JSONObject();
                    myTransaction.put("index", Users.getIndex(transaction));
                    myTransaction.put("transactionTimestamp", transaction.getTimestamp());
                    myTransaction.put("deadline", transaction.getDeadline());
                    myTransaction.put("account", Long.toUnsignedString(transaction.getRecipientId()));
                    myTransaction.put("sentAmountNQT", transaction.getAmountNQT());
                    if (accountId == transaction.getRecipientId()) {
                        myTransaction.put("receivedAmountNQT", transaction.getAmountNQT());
                    }
                    myTransaction.put("feeNQT", transaction.getFeeNQT());
                    myTransaction.put("numberOfConfirmations", -1);
                    myTransaction.put("id", transaction.getStringId());
                    myTransactions.add(myTransaction);
                } else if (accountId == transaction.getRecipientId()) {
                    JSONObject myTransaction = new JSONObject();
                    myTransaction.put("index", Users.getIndex(transaction));
                    myTransaction.put("transactionTimestamp", transaction.getTimestamp());
                    myTransaction.put("deadline", transaction.getDeadline());
                    myTransaction.put("account", Long.toUnsignedString(transaction.getSenderId()));
                    myTransaction.put("receivedAmountNQT", transaction.getAmountNQT());
                    myTransaction.put("feeNQT", transaction.getFeeNQT());
                    myTransaction.put("numberOfConfirmations", -1);
                    myTransaction.put("id", transaction.getStringId());
                    myTransactions.add(myTransaction);
                }
            }
        }
        SortedSet<JSONObject> myTransactionsSet = new TreeSet<>(myTransactionsComparator);
        int blockchainHeight = Nxt.getBlockchain().getLastBlock().getHeight();
        try (DbIterator<? extends Block> blockIterator = Nxt.getBlockchain().getBlocks(accountId, 0)) {
            while (blockIterator.hasNext()) {
                Block block = blockIterator.next();
                if (block.getTotalFeeNQT() > 0) {
                    JSONObject myTransaction = new JSONObject();
                    myTransaction.put("index", "block" + Users.getIndex(block));
                    myTransaction.put("blockTimestamp", block.getTimestamp());
                    myTransaction.put("block", block.getStringId());
                    myTransaction.put("earnedAmountNQT", block.getTotalFeeNQT());
                    myTransaction.put("numberOfConfirmations", blockchainHeight - block.getHeight());
                    myTransaction.put("id", "-");
                    myTransaction.put("timestamp", block.getTimestamp());
                    myTransactionsSet.add(myTransaction);
                }
            }
        }
        try (DbIterator<? extends Transaction> transactionIterator = Nxt.getBlockchain().getTransactions(accountId, (byte) -1, (byte) -1, 0, false)) {
            while (transactionIterator.hasNext()) {
                Transaction transaction = transactionIterator.next();
                if (transaction.getSenderId() == accountId) {
                    JSONObject myTransaction = new JSONObject();
                    myTransaction.put("index", Users.getIndex(transaction));
                    myTransaction.put("blockTimestamp", transaction.getBlockTimestamp());
                    myTransaction.put("transactionTimestamp", transaction.getTimestamp());
                    myTransaction.put("account", Long.toUnsignedString(transaction.getRecipientId()));
                    myTransaction.put("sentAmountNQT", transaction.getAmountNQT());
                    if (accountId == transaction.getRecipientId()) {
                        myTransaction.put("receivedAmountNQT", transaction.getAmountNQT());
                    }
                    myTransaction.put("feeNQT", transaction.getFeeNQT());
                    myTransaction.put("numberOfConfirmations", blockchainHeight - transaction.getHeight());
                    myTransaction.put("id", transaction.getStringId());
                    myTransaction.put("timestamp", transaction.getTimestamp());
                    myTransactionsSet.add(myTransaction);
                } else if (transaction.getRecipientId() == accountId) {
                    JSONObject myTransaction = new JSONObject();
                    myTransaction.put("index", Users.getIndex(transaction));
                    myTransaction.put("blockTimestamp", transaction.getBlockTimestamp());
                    myTransaction.put("transactionTimestamp", transaction.getTimestamp());
                    myTransaction.put("account", Long.toUnsignedString(transaction.getSenderId()));
                    myTransaction.put("receivedAmountNQT", transaction.getAmountNQT());
                    myTransaction.put("feeNQT", transaction.getFeeNQT());
                    myTransaction.put("numberOfConfirmations", blockchainHeight - transaction.getHeight());
                    myTransaction.put("id", transaction.getStringId());
                    myTransaction.put("timestamp", transaction.getTimestamp());
                    myTransactionsSet.add(myTransaction);
                }
            }
        }
        Iterator<JSONObject> iterator = myTransactionsSet.iterator();
        while (myTransactions.size() < 1000 && iterator.hasNext()) {
            myTransactions.add(iterator.next());
        }
        if (myTransactions.size() > 0) {
            JSONObject response2 = new JSONObject();
            response2.put("response", "processNewData");
            response2.put("addedMyTransactions", myTransactions);
            user.enqueue(response2);
        }
    }
    return response;
}
Also used : Account(org.xel.Account) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Transaction(org.xel.Transaction) TreeSet(java.util.TreeSet) Block(org.xel.Block)

Example 17 with Transaction

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

the class BroadcastTransaction 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();
        Nxt.getTransactionProcessor().broadcast(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 18 with Transaction

use of org.xel.Transaction 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 19 with Transaction

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

the class SignTransactions method main.

public static void main(String[] args) {
    try {
        if (args.length != 2) {
            System.out.println("Usage: SignTransactions <unsigned transaction bytes file> <signed transaction bytes file>");
            System.exit(1);
        }
        File unsigned = new File(args[0]);
        if (!unsigned.exists()) {
            System.out.println("File not found: " + unsigned.getAbsolutePath());
            System.exit(1);
        }
        File signed = new File(args[1]);
        if (signed.exists()) {
            System.out.println("File already exists: " + signed.getAbsolutePath());
            System.exit(1);
        }
        String secretPhrase;
        Console console = System.console();
        if (console == null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                secretPhrase = reader.readLine();
            }
        } else {
            secretPhrase = new String(console.readPassword("Secret phrase: "));
        }
        int n = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(unsigned));
            BufferedWriter writer = new BufferedWriter(new FileWriter(signed))) {
            String line;
            while ((line = reader.readLine()) != null) {
                byte[] transactionBytes = Convert.parseHexString(line);
                Transaction.Builder builder = Nxt.newTransactionBuilder(transactionBytes);
                Transaction transaction = builder.build(secretPhrase);
                writer.write(Convert.toHexString(transaction.getBytes()));
                writer.newLine();
                n += 1;
            }
        }
        System.out.println("Signed " + n + " transactions");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter) Transaction(org.xel.Transaction) Console(java.io.Console) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 20 with Transaction

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

the class GetAccountPhasedTransactions method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long accountId = ParameterParser.getAccountId(req, true);
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    JSONArray transactions = new JSONArray();
    try (DbIterator<? extends Transaction> iterator = PhasingPoll.getAccountPhasedTransactions(accountId, firstIndex, lastIndex)) {
        while (iterator.hasNext()) {
            Transaction transaction = iterator.next();
            transactions.add(JSONData.transaction(transaction));
        }
    }
    JSONObject response = new JSONObject();
    response.put("transactions", transactions);
    return response;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

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