/****************************************************************************** * 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.NxtException; import nxt.Shuffler; import nxt.Shuffling; import nxt.util.JSON; import org.json.simple.JSONObject; import org.json.simple.JSONStreamAware; import javax.servlet.http.HttpServletRequest; public final class StartShuffler extends APIServlet.APIRequestHandler { static final StartShuffler instance = new StartShuffler(); private StartShuffler() { super(new APITag[]{APITag.SHUFFLING}, "secretPhrase", "shufflingFullHash", "recipientSecretPhrase", "recipientPublicKey"); } @Override JSONStreamAware processRequest(HttpServletRequest req) throws NxtException { byte[] shufflingFullHash = ParameterParser.getBytes(req, "shufflingFullHash", true); String secretPhrase = ParameterParser.getSecretPhrase(req, true); byte[] recipientPublicKey = ParameterParser.getPublicKey(req, "recipient"); try { Shuffler shuffler = Shuffler.addOrGetShuffler(secretPhrase, recipientPublicKey, shufflingFullHash); return shuffler != null ? JSONData.shuffler(shuffler, false) : JSON.emptyJSON; } catch (Shuffler.ShufflerLimitException e) { JSONObject response = new JSONObject(); response.put("errorCode", 7); response.put("errorDescription", e.getMessage()); return JSON.prepare(response); } catch (Shuffler.DuplicateShufflerException e) { JSONObject response = new JSONObject(); response.put("errorCode", 8); response.put("errorDescription", e.getMessage()); return JSON.prepare(response); } catch (Shuffler.InvalidRecipientException e) { return JSONResponses.incorrect("recipientPublicKey", e.getMessage()); } catch (Shuffler.ControlledAccountException e) { JSONObject response = new JSONObject(); response.put("errorCode", 9); response.put("errorDescription", e.getMessage()); return JSON.prepare(response); } catch (Shuffler.ShufflerException e) { if (e.getCause() instanceof NxtException.InsufficientBalanceException) { Shuffling shuffling = Shuffling.getShuffling(shufflingFullHash); if (shuffling == null) { return JSONResponses.NOT_ENOUGH_FUNDS; } return JSONResponses.notEnoughHolding(shuffling.getHoldingType()); } JSONObject response = new JSONObject(); response.put("errorCode", 10); response.put("errorDescription", e.getMessage()); return JSON.prepare(response); } } @Override boolean requirePost() { return true; } @Override boolean allowRequiredBlockParameters() { return false; } }