<commit_before>/**
 * Copyright (c) 2016-2018 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 <metaverse/explorer/dispatch.hpp>
#include <metaverse/explorer/extensions/commands/getnewaddress.hpp>
#include <metaverse/explorer/extensions/command_extension_func.hpp>
#include <metaverse/explorer/extensions/command_assistant.hpp>
#include <metaverse/explorer/extensions/exception.hpp>

namespace libbitcoin {
namespace explorer {
namespace commands {


/************************ getnewaddress *************************/

console_result getnewaddress::invoke (Json::Value& jv_output,
         libbitcoin::server::server_node& node)
{
    auto& blockchain = node.chain_impl();
    auto acc = blockchain.is_account_passwd_valid(auth_.name, auth_.auth);
    std::string mnemonic;
    acc->get_mnemonic(auth_.auth, mnemonic);
    if (mnemonic.empty()) { throw mnemonicwords_empty_exception("mnemonic empty"); }
    if (!option_.count || (option_.count & 0xfff00000)) { throw address_amount_exception("invalid address number parameter"); }
    
    std::vector<std::string> words;
    words.reserve(24);

    //split mnemonic to words
    string::size_type pos1, pos2;
    std::string c{" "};
    pos2 = mnemonic.find(c);
    pos1 = 0;
    while(string::npos != pos2)
    {
        words.push_back(mnemonic.substr(pos1, pos2-pos1));

        pos1 = pos2 + c.size();
        pos2 = mnemonic.find(c, pos1);
    }
    if(pos1 != mnemonic.length())
        words.push_back(mnemonic.substr(pos1));
    //end split

    if ((words.size() % bc::wallet::mnemonic_word_multiple) != 0) {
        throw mnemonicwords_amount_exception{"invliad size of backup words."};
    }

    uint32_t idx = 0;
    auto& aroot = jv_output;
    Json::Value addresses;

    std::vector<std::shared_ptr<account_address>> account_addresses;
    account_addresses.reserve(option_.count);
    const auto seed = decode_mnemonic(words);
    libbitcoin::config::base16 bs(seed);
    const data_chunk& ds = static_cast<const data_chunk&>(bs);
    const auto prefixes = bc::wallet::hd_private::to_prefixes(76066276, 0);//76066276 is HD private key version
    const bc::wallet::hd_private private_key(ds, prefixes);
    // mainnet payment address version
    auto payment_version = 50;

    if (blockchain.chain_settings().use_testnet_rules){
         // testnetpayment address version
         payment_version = 127;
    }

    for ( idx = 0; idx < option_.count; idx++ ) {

        auto addr = std::make_shared<bc::chain::account_address>();
        addr->set_name(auth_.name);

        const auto child_private_key = private_key.derive_private(acc->get_hd_index());<commit_msg>refactor: use boost::split instead of reinvent the wheel<commit_after>/**
 * Copyright (c) 2016-2018 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 <metaverse/explorer/dispatch.hpp>
#include <metaverse/explorer/extensions/commands/getnewaddress.hpp>
#include <metaverse/explorer/extensions/command_extension_func.hpp>
#include <metaverse/explorer/extensions/command_assistant.hpp>
#include <metaverse/explorer/extensions/exception.hpp>

namespace libbitcoin {
namespace explorer {
namespace commands {


/************************ getnewaddress *************************/

console_result getnewaddress::invoke (Json::Value& jv_output,
         libbitcoin::server::server_node& node)
{
    auto& blockchain = node.chain_impl();
    auto acc = blockchain.is_account_passwd_valid(auth_.name, auth_.auth);
    std::string mnemonic;
    acc->get_mnemonic(auth_.auth, mnemonic);
    if (mnemonic.empty()) { throw mnemonicwords_empty_exception("mnemonic empty"); }
    if (!option_.count || (option_.count & 0xfff00000)) { throw address_amount_exception("invalid address number parameter"); }
    
    std::vector<std::string> words;
    words.reserve(24);

    //split mnemonic to words
    boost::split(words, mnemonic, boost::is_any_of(" "));

    if ((words.size() % bc::wallet::mnemonic_word_multiple) != 0) {
        throw mnemonicwords_amount_exception{"invliad size of backup words."};
    }

    uint32_t idx = 0;
    auto& aroot = jv_output;
    Json::Value addresses;

    std::vector<std::shared_ptr<account_address>> account_addresses;
    account_addresses.reserve(option_.count);
    const auto seed = decode_mnemonic(words);
    libbitcoin::config::base16 bs(seed);
    const data_chunk& ds = static_cast<const data_chunk&>(bs);
    const auto prefixes = bc::wallet::hd_private::to_prefixes(76066276, 0);//76066276 is HD private key version
    const bc::wallet::hd_private private_key(ds, prefixes);
    // mainnet payment address version
    auto payment_version = 50;

    if (blockchain.chain_settings().use_testnet_rules){
         // testnetpayment address version
         payment_version = 127;
    }

    for ( idx = 0; idx < option_.count; idx++ ) {

        auto addr = std::make_shared<bc::chain::account_address>();
        addr->set_name(auth_.name);

        const auto child_private_key = private_key.derive_private(acc->get_hd_index());
{'fraction_non_alphanumeric': 0.12227140894519921, 'fraction_numerical': 0.016333384216150205, 'mean_word_length': 4.11875, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '6983259', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1906, 'n_words': 537}