Search in sources :

Example 11 with Transaction

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

the class RetrievePrunedTransaction method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    Transaction transaction = Nxt.getBlockchain().getTransaction(transactionId);
    if (transaction == null) {
        return UNKNOWN_TRANSACTION;
    }
    transaction = Nxt.getBlockchainProcessor().restorePrunedTransaction(transactionId);
    if (transaction == null) {
        return PRUNED_TRANSACTION;
    }
    return JSONData.transaction(transaction);
}
Also used : Transaction(org.xel.Transaction)

Example 12 with Transaction

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

the class GetTransaction method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) {
    String transactionIdString = Convert.emptyToNull(req.getParameter("transaction"));
    String transactionFullHash = Convert.emptyToNull(req.getParameter("fullHash"));
    if (transactionIdString == null && transactionFullHash == null) {
        return MISSING_TRANSACTION;
    }
    boolean includePhasingResult = "true".equalsIgnoreCase(req.getParameter("includePhasingResult"));
    long transactionId = 0;
    Transaction transaction;
    try {
        if (transactionIdString != null) {
            transactionId = Convert.parseUnsignedLong(transactionIdString);
            transaction = Nxt.getBlockchain().getTransaction(transactionId);
        } else {
            transaction = Nxt.getBlockchain().getTransactionByFullHash(transactionFullHash);
            if (transaction == null) {
                return UNKNOWN_TRANSACTION;
            }
        }
    } catch (RuntimeException e) {
        return INCORRECT_TRANSACTION;
    }
    if (transaction == null) {
        transaction = Nxt.getTransactionProcessor().getUnconfirmedTransaction(transactionId);
        if (transaction == null) {
            return UNKNOWN_TRANSACTION;
        }
        return JSONData.unconfirmedTransaction(transaction);
    } else {
        return JSONData.transaction(transaction, includePhasingResult);
    }
}
Also used : Transaction(org.xel.Transaction)

Example 13 with Transaction

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

the class GetTransactions method processRequest.

@Override
JSONStreamAware processRequest(JSONObject request, Peer peer) {
    if (!Constants.INCLUDE_EXPIRED_PRUNABLE) {
        return PeerServlet.UNSUPPORTED_REQUEST_TYPE;
    }
    JSONObject response = new JSONObject();
    JSONArray transactionArray = new JSONArray();
    JSONArray transactionIds = (JSONArray) request.get("transactionIds");
    Blockchain blockchain = Nxt.getBlockchain();
    // 
    if (transactionIds != null) {
        transactionIds.forEach(transactionId -> {
            long id = Long.parseUnsignedLong((String) transactionId);
            Transaction transaction = blockchain.getTransaction(id);
            if (transaction != null) {
                transaction.getAppendages(true);
                JSONObject transactionJSON = transaction.getJSONObject();
                transactionArray.add(transactionJSON);
            }
        });
    }
    response.put("transactions", transactionArray);
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) Transaction(org.xel.Transaction) Blockchain(org.xel.Blockchain) JSONArray(org.json.simple.JSONArray)

Example 14 with Transaction

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

the class SignTransactionJSON method main.

public static void main(String[] args) {
    try {
        Logger.setLevel(Logger.Level.ERROR);
        if (args.length == 0 || args.length > 2) {
            System.out.println("Usage: SignTransactionJSON <unsigned transaction json file> <signed transaction json 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;
        if (args.length == 2) {
            signed = new File(args[1]);
        } else if (unsigned.getName().startsWith("unsigned.")) {
            signed = new File(unsigned.getParentFile(), unsigned.getName().substring(2));
        } else {
            signed = new File(unsigned.getParentFile(), "signed." + unsigned.getName());
        }
        if (signed.exists()) {
            System.out.println("File already exists: " + signed.getAbsolutePath());
            System.exit(1);
        }
        try (BufferedReader reader = new BufferedReader(new FileReader(unsigned));
            BufferedWriter writer = new BufferedWriter(new FileWriter(signed))) {
            JSONObject json = (JSONObject) JSONValue.parseWithException(reader);
            byte[] publicKeyHash = Crypto.sha256().digest(Convert.parseHexString((String) json.get("senderPublicKey")));
            String senderRS = Convert.rsAccount(Convert.fullHashToId(publicKeyHash));
            String secretPhrase;
            Console console = System.console();
            if (console == null) {
                try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in))) {
                    secretPhrase = inputReader.readLine();
                }
            } else {
                secretPhrase = new String(console.readPassword("Secret phrase for account " + senderRS + ": "));
            }
            Transaction.Builder builder = Nxt.newTransactionBuilder(json);
            Transaction transaction = builder.build(secretPhrase);
            writer.write(transaction.getJSONObject().toJSONString());
            writer.newLine();
            System.out.println("Signed transaction JSON saved as: " + signed.getAbsolutePath());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter) JSONObject(org.json.simple.JSONObject) Transaction(org.xel.Transaction) BufferedReader(java.io.BufferedReader) Console(java.io.Console) FileReader(java.io.FileReader) File(java.io.File)

Example 15 with Transaction

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

the class GetInitialData method processRequest.

@Override
JSONStreamAware processRequest(HttpServletRequest req, User user) throws IOException {
    JSONArray unconfirmedTransactions = new JSONArray();
    JSONArray activePeers = new JSONArray(), knownPeers = new JSONArray(), blacklistedPeers = new JSONArray();
    JSONArray recentBlocks = new JSONArray();
    try (DbIterator<? extends Transaction> transactions = Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) {
        while (transactions.hasNext()) {
            Transaction transaction = transactions.next();
            JSONObject unconfirmedTransaction = new JSONObject();
            unconfirmedTransaction.put("index", Users.getIndex(transaction));
            unconfirmedTransaction.put("timestamp", transaction.getTimestamp());
            unconfirmedTransaction.put("deadline", transaction.getDeadline());
            unconfirmedTransaction.put("recipient", Long.toUnsignedString(transaction.getRecipientId()));
            unconfirmedTransaction.put("amountNQT", transaction.getAmountNQT());
            unconfirmedTransaction.put("feeNQT", transaction.getFeeNQT());
            unconfirmedTransaction.put("sender", Long.toUnsignedString(transaction.getSenderId()));
            unconfirmedTransaction.put("id", transaction.getStringId());
            unconfirmedTransactions.add(unconfirmedTransaction);
        }
    }
    for (Peer peer : Peers.getAllPeers()) {
        if (peer.isBlacklisted()) {
            JSONObject blacklistedPeer = new JSONObject();
            blacklistedPeer.put("index", Users.getIndex(peer));
            blacklistedPeer.put("address", peer.getHost());
            blacklistedPeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            blacklistedPeer.put("software", peer.getSoftware());
            blacklistedPeers.add(blacklistedPeer);
        } else if (peer.getState() == Peer.State.NON_CONNECTED) {
            JSONObject knownPeer = new JSONObject();
            knownPeer.put("index", Users.getIndex(peer));
            knownPeer.put("address", peer.getHost());
            knownPeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            knownPeer.put("software", peer.getSoftware());
            knownPeers.add(knownPeer);
        } else {
            JSONObject activePeer = new JSONObject();
            activePeer.put("index", Users.getIndex(peer));
            if (peer.getState() == Peer.State.DISCONNECTED) {
                activePeer.put("disconnected", true);
            }
            activePeer.put("address", peer.getHost());
            activePeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            activePeer.put("weight", peer.getWeight());
            activePeer.put("downloaded", peer.getDownloadedVolume());
            activePeer.put("uploaded", peer.getUploadedVolume());
            activePeer.put("software", peer.getSoftware());
            activePeers.add(activePeer);
        }
    }
    try (DbIterator<? extends Block> lastBlocks = Nxt.getBlockchain().getBlocks(0, 59)) {
        for (Block block : lastBlocks) {
            JSONObject recentBlock = new JSONObject();
            recentBlock.put("index", Users.getIndex(block));
            recentBlock.put("timestamp", block.getTimestamp());
            recentBlock.put("numberOfTransactions", block.getTransactions().size());
            recentBlock.put("totalAmountNQT", block.getTotalAmountNQT());
            recentBlock.put("totalFeeNQT", block.getTotalFeeNQT());
            recentBlock.put("payloadLength", block.getPayloadLength());
            recentBlock.put("generator", Long.toUnsignedString(block.getGeneratorId()));
            recentBlock.put("height", block.getHeight());
            recentBlock.put("version", block.getVersion());
            recentBlock.put("block", block.getStringId());
            recentBlock.put("baseTarget", BigInteger.valueOf(block.getBaseTarget()).multiply(BigInteger.valueOf(100000)).divide(BigInteger.valueOf(Constants.INITIAL_BASE_TARGET)));
            recentBlocks.add(recentBlock);
        }
    }
    JSONObject response = new JSONObject();
    response.put("response", "processInitialData");
    response.put("version", Nxt.VERSION);
    if (unconfirmedTransactions.size() > 0) {
        response.put("unconfirmedTransactions", unconfirmedTransactions);
    }
    if (activePeers.size() > 0) {
        response.put("activePeers", activePeers);
    }
    if (knownPeers.size() > 0) {
        response.put("knownPeers", knownPeers);
    }
    if (blacklistedPeers.size() > 0) {
        response.put("blacklistedPeers", blacklistedPeers);
    }
    if (recentBlocks.size() > 0) {
        response.put("recentBlocks", recentBlocks);
    }
    return response;
}
Also used : Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) Peer(org.xel.peer.Peer) JSONArray(org.json.simple.JSONArray) Block(org.xel.Block)

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