<commit_before>/**
 * Copyright (c) 2016-2017 mvs developers 
 *
 * This file is part of metaverse-explorer.
 *
 * metaverse-explorer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <boost/property_tree/ptree.hpp>      
#include <boost/property_tree/json_parser.hpp>

#include <metaverse/explorer/extensions/account_helper.hpp>
#include <metaverse/explorer/prop_tree.hpp>
#include <metaverse/explorer/dispatch.hpp>

namespace libbitcoin {
namespace explorer {
namespace commands {

namespace pt = boost::property_tree;

/************************ importaccount *************************/
console_result importaccount::invoke (std::ostream& output,
        std::ostream& cerr, bc::blockchain::block_chain_impl& blockchain)
{
    std::istringstream sin("");
    std::ostringstream lang("");
    std::ostringstream sout("");
    
    // parameter account name check
    if (blockchain.is_account_exist(auth_.name))
        throw std::logic_error{"account already exist"};

    if (argument_.words.size() > 24)
        throw std::logic_error{"word count must be less than or equal 24"};
    
    for(auto& i : argument_.words){
        sout<<i<<" ";
    }
    
    auto mnemonic = sout.str().substr(0, sout.str().size()-1); // remove last space
    
    // 1. parameter mnemonic check
    lang<<option_.language;
    sin.str("");
    sout.str("");
    //const char* cmds[256]{"mnemonic-to-seed", "-l", lang.str().c_str()};
    const char* cmds[64]{0x00};
    int i = 0;
    cmds[i++] = "mnemonic-to-seed";
    cmds[i++] = "-l";
    auto s_lang = lang.str();
    cmds[i++] = s_lang.c_str();
    // 
    for(auto& word : argument_.words){
        cmds[i++] = word.c_str();
    }

    if( console_result::okay != dispatch_command(i, cmds , sin, sout, sout)) {
        output<<sout.str();<commit_msg>#33 add word size check for importaccount command<commit_after>/**
 * Copyright (c) 2016-2017 mvs developers 
 *
 * This file is part of metaverse-explorer.
 *
 * metaverse-explorer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <boost/property_tree/ptree.hpp>      
#include <boost/property_tree/json_parser.hpp>

#include <metaverse/explorer/extensions/account_helper.hpp>
#include <metaverse/explorer/prop_tree.hpp>
#include <metaverse/explorer/dispatch.hpp>

namespace libbitcoin {
namespace explorer {
namespace commands {

namespace pt = boost::property_tree;

/************************ importaccount *************************/
console_result importaccount::invoke (std::ostream& output,
        std::ostream& cerr, bc::blockchain::block_chain_impl& blockchain)
{
    std::istringstream sin("");
    std::ostringstream lang("");
    std::ostringstream sout("");
    
    // parameter account name check
    if (blockchain.is_account_exist(auth_.name))
        throw std::logic_error{"account already exist"};

    if (argument_.words.size() > 24)
        throw std::logic_error{"word count must be less than or equal 24"};
    
    for(auto& i : argument_.words){
        sout<<i<<" ";
    }
    
    auto mnemonic = sout.str().substr(0, sout.str().size()-1); // remove last space
    
    // 1. parameter mnemonic check
    lang<<option_.language;
    sin.str("");
    sout.str("");
    //const char* cmds[256]{"mnemonic-to-seed", "-l", lang.str().c_str()};
    const char* cmds[64]{0x00};
    int i = 0;
    cmds[i++] = "mnemonic-to-seed";
    cmds[i++] = "-l";
    auto s_lang = lang.str();
    cmds[i++] = s_lang.c_str();
    // words size check
    std::vector<std::string> tokens;
    if(argument_.words.size() == 24) {
	    for(auto& word : argument_.words){
	        cmds[i++] = word.c_str();
	    }
	} else if(argument_.words.size() == 1) { // all words include in ""
		tokens = split(argument_.words.at(0));
	    for(auto& word : tokens){
	        cmds[i++] = word.c_str();
	    }
	} else {
		throw std::logic_error{"words count should be 24, not " + std::to_string(argument_.words.size())};
	}

    if( console_result::okay != dispatch_command(i, cmds , sin, sout, sout)) {
        output<<sout.str();
{'fraction_non_alphanumeric': 0.14890456739695507, 'fraction_numerical': 0.010768659487560341, 'mean_word_length': 3.515507124895222, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '245590', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1607, 'n_words': 498}