@Override JSONStreamAware processRequest(HttpServletRequest req) { String accountIdString = Convert.emptyToNull(req.getParameter("account")); Long accountId = null; if (accountIdString != null) { try { accountId = Convert.parseUnsignedLong(accountIdString); } catch (RuntimeException e) { return INCORRECT_ACCOUNT; } } JSONArray transactionIds = new JSONArray(); for (Transaction transaction : Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) { if (accountId != null && !(accountId.equals(transaction.getSenderId()) || accountId.equals(transaction.getRecipientId()))) { continue; } transactionIds.add(transaction.getStringId()); } JSONObject response = new JSONObject(); response.put("unconfirmedTransactionIds", transactionIds); return response; }
@Override JSONStreamAware processRequest(HttpServletRequest req) throws NxtException.ValidationException { String transactionBytes = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes")); String transactionJSON = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON")); if (transactionBytes == null && transactionJSON == null) { return MISSING_UNSIGNED_BYTES; } String secretPhrase = Convert.emptyToNull(req.getParameter("secretPhrase")); if (secretPhrase == null) { return MISSING_SECRET_PHRASE; } try { Transaction transaction; if (transactionBytes != null) { byte[] bytes = Convert.parseHexString(transactionBytes); transaction = Nxt.getTransactionProcessor().parseTransaction(bytes); } else { JSONObject json = (JSONObject) JSONValue.parse(transactionJSON); transaction = Nxt.getTransactionProcessor().parseTransaction(json); } transaction.validate(); if (transaction.getSignature() != null) { JSONObject response = new JSONObject(); response.put("errorCode", 4); response.put( "errorDescription", "Incorrect \"unsignedTransactionBytes\" - transaction is already signed"); return response; } transaction.sign(secretPhrase); JSONObject response = new JSONObject(); response.put("transaction", transaction.getStringId()); response.put("fullHash", transaction.getFullHash()); response.put("transactionBytes", Convert.toHexString(transaction.getBytes())); response.put( "signatureHash", Convert.toHexString(Crypto.sha256().digest(transaction.getSignature()))); response.put("verify", transaction.verifySignature()); return response; } catch (NxtException.ValidationException | RuntimeException e) { // Logger.logDebugMessage(e.getMessage(), e); return INCORRECT_UNSIGNED_BYTES; } }
@Override JSONStreamAware processRequest(HttpServletRequest req) throws NxtException { String transactionBytes = Convert.emptyToNull(req.getParameter("transactionBytes")); String transactionJSON = Convert.emptyToNull(req.getParameter("transactionJSON")); String prunableAttachmentJSON = Convert.emptyToNull(req.getParameter("prunableAttachmentJSON")); Transaction transaction = ParameterParser.parseTransaction(transactionJSON, transactionBytes, prunableAttachmentJSON) .build(); JSONObject response = JSONData.unconfirmedTransaction(transaction); try { transaction.validate(); } catch (NxtException.ValidationException | RuntimeException e) { Logger.logDebugMessage(e.getMessage(), e); response.put("validate", false); JSONData.putException(response, e, "Invalid transaction"); } response.put("verify", transaction.verifySignature()); return response; }
@Override protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException { DigitalGoodsStore.Goods goods = ParameterParser.getGoods(req); if (goods.isDelisted()) { return UNKNOWN_GOODS; } int quantity = ParameterParser.getGoodsQuantity(req); if (quantity > goods.getQuantity()) { return INCORRECT_PURCHASE_QUANTITY; } long priceNQT = ParameterParser.getPriceNQT(req); if (priceNQT != goods.getPriceNQT()) { return INCORRECT_PURCHASE_PRICE; } String deliveryDeadlineString = Convert.emptyToNull(req.getParameter("deliveryDeadlineTimestamp")); if (deliveryDeadlineString == null) { return MISSING_DELIVERY_DEADLINE_TIMESTAMP; } int deliveryDeadline; try { deliveryDeadline = Integer.parseInt(deliveryDeadlineString); if (deliveryDeadline <= Nxt.getEpochTime()) { return INCORRECT_DELIVERY_DEADLINE_TIMESTAMP; } } catch (NumberFormatException e) { return INCORRECT_DELIVERY_DEADLINE_TIMESTAMP; } Account buyerAccount = ParameterParser.getSenderAccount(req); Account sellerAccount = Account.getAccount(goods.getSellerId()); Attachment attachment = new Attachment.DigitalGoodsPurchase(goods.getId(), quantity, priceNQT, deliveryDeadline); try { return createTransaction(req, buyerAccount, sellerAccount.getId(), 0, attachment); } catch (NxtException.InsufficientBalanceException e) { return JSONResponses.NOT_ENOUGH_FUNDS; } }
@Override public Transaction parseTransaction(byte[] bytes) throws NxtException.ValidationException { try { boolean useNQT = Nxt.getBlockchain().getLastBlock().getHeight() >= Constants.NQT_BLOCK; ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(ByteOrder.LITTLE_ENDIAN); byte type = buffer.get(); byte subtype = buffer.get(); int timestamp = buffer.getInt(); short deadline = buffer.getShort(); byte[] senderPublicKey = new byte[32]; buffer.get(senderPublicKey); Long recipientId = buffer.getLong(); long amountNQT; long feeNQT; String referencedTransactionFullHash = null; Long referencedTransactionId = null; if (!useNQT) { amountNQT = ((long) buffer.getInt()) * Constants.ONE_NXT; feeNQT = ((long) buffer.getInt()) * Constants.ONE_NXT; referencedTransactionId = Convert.zeroToNull(buffer.getLong()); } else { amountNQT = buffer.getLong(); feeNQT = buffer.getLong(); byte[] referencedTransactionFullHashBytes = new byte[32]; buffer.get(referencedTransactionFullHashBytes); if (Convert.emptyToNull(referencedTransactionFullHashBytes) != null) { referencedTransactionFullHash = Convert.toHexString(referencedTransactionFullHashBytes); referencedTransactionId = Convert.fullHashToId(referencedTransactionFullHash); } } byte[] signature = new byte[64]; buffer.get(signature); signature = Convert.emptyToNull(signature); TransactionType transactionType = TransactionType.findTransactionType(type, subtype); TransactionImpl transaction; if (!useNQT) { transaction = new TransactionImpl( transactionType, timestamp, deadline, senderPublicKey, recipientId, amountNQT, feeNQT, referencedTransactionId, signature); } else { transaction = new TransactionImpl( transactionType, timestamp, deadline, senderPublicKey, recipientId, amountNQT, feeNQT, referencedTransactionFullHash, signature); } transactionType.loadAttachment(transaction, buffer); return transaction; } catch (RuntimeException e) { throw new NxtException.ValidationException(e.toString(), e); } }
void setAccountInfo(String name, String description) { this.name = Convert.emptyToNull(name.trim()); this.description = Convert.emptyToNull(description.trim()); accountTable.insert(this); }