<commit_before>/**
 * Copyright (c) 2011-2015 metaverse developers (see AUTHORS)
 *
 * This file is part of mvs-node.
 *
 * libbitcoin 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 <bitcoin/bitcoin/chain/attachment/account/account_address.hpp>

#include <sstream>
#include <boost/iostreams/stream.hpp>
#include <bitcoin/bitcoin/utility/container_sink.hpp>
#include <bitcoin/bitcoin/utility/container_source.hpp>
#include <bitcoin/bitcoin/utility/istream_reader.hpp>
#include <bitcoin/bitcoin/utility/ostream_writer.hpp>

#ifdef MVS_DEBUG
#include <json/minijson_writer.hpp>
#endif

#include <bitcoin/bitcoin.hpp>
using namespace libbitcoin::wallet;

namespace libbitcoin {
namespace chain {

account_address::account_address()
{
	reset();
}

account_address::account_address(std::string name, std::string prv_key, 
	std::string pub_key, uint32_t hd_index, uint64_t balance, std::string alias, 
	std::string address, uint8_t status) :
	name(name), prv_key(prv_key), pub_key(pub_key), hd_index(hd_index), balance(balance),
	alias(alias), address(address), status_(status)
{
}

account_address::account_address(const account_address& other)
{
    name = other.name;
    prv_key = other.prv_key;
    pub_key = other.pub_key;
    hd_index = other.hd_index;
	balance = other.balance;
	alias = other.alias;
	address = other.address;
	status_ = other.status_;
}
account_address account_address::factory_from_data(const data_chunk& data)
{
    account_address instance;
    instance.from_data(data);
    return instance;
}

account_address account_address::factory_from_data(std::istream& stream)
{
    account_address instance;
    instance.from_data(stream);
    return instance;
}

account_address account_address::factory_from_data(reader& source)
{
    account_address instance;
    instance.from_data(source);
    return instance;
}

bool account_address::is_valid() const
{
    return true;
}

void account_address::reset()
{	
    name = "";
    prv_key = "";
    pub_key = "";
    hd_index = 0;
	balance = 0;
	alias = "";
	address = "";
	status_ = 0;
}

bool account_address::from_data(const data_chunk& data)
{
    data_source istream(data);
    return from_data(istream);
}

bool account_address::from_data(std::istream& stream)
{
    istream_reader source(stream);
    return from_data(source);
}

bool account_address::from_data(reader& source)
{
    reset();
    name = source.read_fixed_string(ADDRESS_NAME_FIX_SIZE);
    prv_key = source.read_fixed_string(ADDRESS_PRV_KEY_FIX_SIZE);
	pub_key = source.read_fixed_string(ADDRESS_PUB_KEY_FIX_SIZE);
    hd_index = source.read_4_bytes_little_endian();
	balance = source.read_8_bytes_little_endian();
	alias = source.read_fixed_string(ADDRESS_ALIAS_FIX_SIZE);
	address = source.read_fixed_string(ADDRESS_ADDRESS_FIX_SIZE);
    status_ = source.read_byte();
    return true;	
}

data_chunk account_address::to_data() const
{
    data_chunk data;
    data_sink ostream(data);
    to_data(ostream);
    ostream.flush();
    //BITCOIN_ASSERT(data.size() == serialized_size());
    return data;
}

void account_address::to_data(std::ostream& stream) const
{
    ostream_writer sink(stream);
    to_data(sink);
}

void account_address::to_data(writer& sink) const
{
    sink.write_fixed_string(name, ADDRESS_NAME_FIX_SIZE);
	sink.write_fixed_string(prv_key, ADDRESS_PRV_KEY_FIX_SIZE);
	sink.write_fixed_string(pub_key, ADDRESS_PUB_KEY_FIX_SIZE);
	sink.write_4_bytes_little_endian(hd_index);
	sink.write_8_bytes_little_endian(balance);
	sink.write_fixed_string(alias, ADDRESS_ALIAS_FIX_SIZE);
	sink.write_fixed_string(address, ADDRESS_ADDRESS_FIX_SIZE);
	sink.write_byte(status_);
}

uint64_t account_address::serialized_size() const
{
    return name.size() + prv_key.size() + pub_key.size() + 4 + 8 
		+ alias.size() + address.size() + 1 
		+ 5; // 5 "string length" byte
}

#ifdef MVS_DEBUG
std::string account_address::to_string() 
{
    std::ostringstream ss;

    ss << "\t name = " << name << "\n"
		<< "\t prv_key = " << prv_key << "\n"<commit_msg>#4 encrypt account mnenomic&private key<commit_after>/**
 * Copyright (c) 2011-2015 metaverse developers (see AUTHORS)
 *
 * This file is part of mvs-node.
 *
 * libbitcoin 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 <bitcoin/bitcoin/chain/attachment/account/account_address.hpp>

#include <sstream>
#include <boost/iostreams/stream.hpp>
#include <bitcoin/bitcoin/utility/container_sink.hpp>
#include <bitcoin/bitcoin/utility/container_source.hpp>
#include <bitcoin/bitcoin/utility/istream_reader.hpp>
#include <bitcoin/bitcoin/utility/ostream_writer.hpp>

#ifdef MVS_DEBUG
#include <json/minijson_writer.hpp>
#endif

#include <bitcoin/bitcoin.hpp>
using namespace libbitcoin::wallet;

namespace libbitcoin {
namespace chain {

account_address::account_address()
{
	reset();
}

account_address::account_address(std::string name, std::string prv_key, 
	std::string pub_key, uint32_t hd_index, uint64_t balance, std::string alias, 
	std::string address, uint8_t status) :
	name(name), prv_key(prv_key), pub_key(pub_key), hd_index(hd_index), balance(balance),
	alias(alias), address(address), status_(status)
{
}

account_address::account_address(const account_address& other)
{
    name = other.name;
    prv_key = other.prv_key;
    pub_key = other.pub_key;
    hd_index = other.hd_index;
	balance = other.balance;
	alias = other.alias;
	address = other.address;
	status_ = other.status_;
}
account_address account_address::factory_from_data(const data_chunk& data)
{
    account_address instance;
    instance.from_data(data);
    return instance;
}

account_address account_address::factory_from_data(std::istream& stream)
{
    account_address instance;
    instance.from_data(stream);
    return instance;
}

account_address account_address::factory_from_data(reader& source)
{
    account_address instance;
    instance.from_data(source);
    return instance;
}

bool account_address::is_valid() const
{
    return true;
}

void account_address::reset()
{	
    name = "";
    prv_key = "";
    pub_key = "";
    hd_index = 0;
	balance = 0;
	alias = "";
	address = "";
	status_ = 0;
}

bool account_address::from_data(const data_chunk& data)
{
    data_source istream(data);
    return from_data(istream);
}

bool account_address::from_data(std::istream& stream)
{
    istream_reader source(stream);
    return from_data(source);
}

bool account_address::from_data(reader& source)
{
    reset();
    name = source.read_fixed_string(ADDRESS_NAME_FIX_SIZE);
    //prv_key = source.read_fixed_string(ADDRESS_PRV_KEY_FIX_SIZE);
    
	// read encrypted private key
	auto size = source.read_variable_uint_little_endian();
	data_chunk string_bytes = source.read_data(size);
	std::string result(string_bytes.begin(), string_bytes.end());
	prv_key = result;
	//log::trace("from_data prv")<<prv_key;

	pub_key = source.read_fixed_string(ADDRESS_PUB_KEY_FIX_SIZE);
    hd_index = source.read_4_bytes_little_endian();
	balance = source.read_8_bytes_little_endian();
	alias = source.read_fixed_string(ADDRESS_ALIAS_FIX_SIZE);
	address = source.read_fixed_string(ADDRESS_ADDRESS_FIX_SIZE);
    status_ = source.read_byte();
    return true;	
}

data_chunk account_address::to_data() const
{
    data_chunk data;
    data_sink ostream(data);
    to_data(ostream);
    ostream.flush();
    //BITCOIN_ASSERT(data.size() == serialized_size());
    return data;
}

void account_address::to_data(std::ostream& stream) const
{
    ostream_writer sink(stream);
    to_data(sink);
}

void account_address::to_data(writer& sink) const
{
    sink.write_fixed_string(name, ADDRESS_NAME_FIX_SIZE);
	//sink.write_fixed_string(prv_key, ADDRESS_PRV_KEY_FIX_SIZE);
	sink.write_string(prv_key);
	sink.write_fixed_string(pub_key, ADDRESS_PUB_KEY_FIX_SIZE);
	sink.write_4_bytes_little_endian(hd_index);
	sink.write_8_bytes_little_endian(balance);
	sink.write_fixed_string(alias, ADDRESS_ALIAS_FIX_SIZE);
	sink.write_fixed_string(address, ADDRESS_ADDRESS_FIX_SIZE);
	sink.write_byte(status_);
}

uint64_t account_address::serialized_size() const
{
    return name.size() + prv_key.size() + pub_key.size() + 4 + 8 
		+ alias.size() + address.size() + 1 
		+ 5; // 5 "string length" byte
}

#ifdef MVS_DEBUG
std::string account_address::to_string() 
{
    std::ostringstream ss;

    ss << "\t name = " << name << "\n"
		<< "\t prv_key = " << prv_key << "\n"
{'fraction_non_alphanumeric': 0.11304616652926629, 'fraction_numerical': 0.005873866446826051, 'mean_word_length': 5.084639498432602, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 23, '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': '6887455', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3272, 'n_words': 715}