/******************************************************************************
* Copyright © 2013-2016 The Nxt Core Developers. *
* *
* See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Nxt software, including this file, may be copied, modified, propagated, *
* or distributed except according to the terms contained in the LICENSE.txt *
* file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
package nxt.http;
import nxt.Account;
import nxt.Attachment;
import nxt.Constants;
import nxt.NxtException;
import nxt.util.Convert;
import org.json.simple.JSONStreamAware;
import javax.servlet.http.HttpServletRequest;
import static nxt.http.JSONResponses.INCORRECT_ASSET_DESCRIPTION;
import static nxt.http.JSONResponses.INCORRECT_ASSET_NAME;
import static nxt.http.JSONResponses.INCORRECT_ASSET_NAME_LENGTH;
import static nxt.http.JSONResponses.INCORRECT_DECIMALS;
import static nxt.http.JSONResponses.MISSING_NAME;
public final class IssueAsset extends CreateTransaction {
static final IssueAsset instance = new IssueAsset();
private IssueAsset() {
super(new APITag[] {APITag.AE, APITag.CREATE_TRANSACTION}, "name", "description", "quantityQNT", "decimals");
}
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
String name = req.getParameter("name");
String description = req.getParameter("description");
String decimalsValue = Convert.emptyToNull(req.getParameter("decimals"));
if (name == null) {
return MISSING_NAME;
}
name = name.trim();
if (name.length() < Constants.MIN_ASSET_NAME_LENGTH || name.length() > Constants.MAX_ASSET_NAME_LENGTH) {
return INCORRECT_ASSET_NAME_LENGTH;
}
String normalizedName = name.toLowerCase();
for (int i = 0; i < normalizedName.length(); i++) {
if (Constants.ALPHABET.indexOf(normalizedName.charAt(i)) < 0) {
return INCORRECT_ASSET_NAME;
}
}
if (description != null && description.length() > Constants.MAX_ASSET_DESCRIPTION_LENGTH) {
return INCORRECT_ASSET_DESCRIPTION;
}
byte decimals = 0;
if (decimalsValue != null) {
try {
decimals = Byte.parseByte(decimalsValue);
if (decimals < 0 || decimals > 8) {
return INCORRECT_DECIMALS;
}
} catch (NumberFormatException e) {
return INCORRECT_DECIMALS;
}
}
long quantityQNT = ParameterParser.getQuantityQNT(req);
Account account = ParameterParser.getSenderAccount(req);
Attachment attachment = new Attachment.ColoredCoinsAssetIssuance(name, description, quantityQNT, decimals);
return createTransaction(req, account, attachment);
}
}