bool checkpoint_sync_unsigned::decode(data_buffer & buffer)
{
    m_version = buffer.read_int32();
    m_hash_checkpoint = buffer.read_sha256();
    
    return true;
}
bool checkpoint_sync::decode(data_buffer & buffer)
{
    auto len = buffer.read_var_int();
    
    if (len > 0)
    {
        m_message.resize(len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_message[0]), m_message.size()
        );
    }
    
    len = buffer.read_var_int();
    
    if (len > 0)
    {
        m_signature.resize(len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_signature[0]), m_signature.size()
        );
    }
    
    return true;
}
Esempio n. 3
0
void zerotime_lock::encode(data_buffer & buffer)
{
    /**
     * Encode the version.
     */
    buffer.write_uint32(m_version);

    /**
     * Encode the transaction.
     */
    m_transaction.encode(buffer);

    /**
     * Encode the transaction hash.
     */
    buffer.write_bytes(
        reinterpret_cast<const char *> (m_hash_tx.digest()),
        sha256::digest_length
    );

    /**
     * Encode the expiration.
     */
    buffer.write_uint64(m_expiration);

    /**
     * No signature is required because:
     * 1. The receiver may want to lock a non-zerotime transaction.
     * 2. It causes no harm to let other's lock the transaction.
     * 3. It conserves bandwidth and processing power.
     */
}
Esempio n. 4
0
bool zerotime_question::decode(data_buffer & buffer)
{
    /**
     * Decode the version.
     */
    m_version = buffer.read_uint32();
    
    assert(m_version == current_version);
    
    /**
     * Decode the transaction inputs length.
     */
    auto len = buffer.read_var_int();

    /**
     * Allocate the transaction inputs.
     */
    m_transactions_in.resize(len);
    
    for (auto i = 0; i < len; i++)
    {
        /**
         * Decode the transaction_in.
         */
        m_transactions_in[i].decode(buffer);
    }
    
    return true;
}
void transaction_out::decode(data_buffer & buffer)
{
    /**
     * Decode the value.
     */
    m_value = buffer.read_int64();
    
    /**
     * Read the var_int.
     */
    auto len = buffer.read_var_int();
    
    if (len > 0)
    {
        /**
         * Read the script.
         */
        auto bytes = buffer.read_bytes(len);
        
        /**
         * Insert the script.
         */
        m_script_public_key.insert(
            m_script_public_key.begin(), bytes.begin(), bytes.end()
        );
    }
}
bool inventory_vector::decode(data_buffer & buffer)
{
    m_type = static_cast<type_t> (buffer.read_uint32());
    m_hash = buffer.read_sha256();
    
    return true;
}
bool inventory_vector::encode(data_buffer & buffer)
{
    buffer.write_uint32(m_type);
    buffer.write_sha256(m_hash);
    
    return true;
}
Esempio n. 8
0
void chainblender_join::encode(data_buffer & buffer)
{
    /**
     * Encode the version.
     */
    buffer.write_uint32(m_version);
    
    /**
     * The session id must be null for a cbjoin.
     */
    if (m_hash_session_id.is_empty() == false)
    {
        log_error(
            "ChainBlender join message has invalid hash session "
            "id = " << m_hash_session_id.to_string() << "."
        );
    }
    
    /**
     * Encode the session id.
     */
    buffer.write_sha256(m_hash_session_id);
    
    /**
     * Encode the denomination.
     */
    buffer.write_int64(m_denomination);
}
Esempio n. 9
0
offs_t cop420_disassembler::disassemble(std::ostream &stream, offs_t pc, const data_buffer &opcodes, const data_buffer &params)
{
	uint8_t opcode = opcodes.r8(pc);
	uint8_t next_opcode = opcodes.r8(pc+1);
	uint16_t address;
	uint32_t flags = 0;
	int bytes = 1;

	if ((opcode >= 0x80 && opcode <= 0xBE) || (opcode >= 0xC0 && opcode <= 0xFE))
	{
		int page = pc >> 6;

		if (page == 2 || page == 3) //JP pages 2,3
		{
			address = (uint16_t)((pc & 0x380) | (opcode & 0x7F));
			util::stream_format(stream, "JP %03X", address);
		}
		else
		{
			if ((opcode & 0xC0) == 0xC0) //JP other pages
			{
				address = (uint16_t)((pc & 0x3C0) | (opcode & 0x3F));
				util::stream_format(stream, "JP %03X", address);
			}
			else                    //JSRP
			{
				address = (uint16_t)(0x80 | (opcode & 0x3F));
				util::stream_format(stream, "JSRP %03X", address);
				flags = STEP_OVER;
			}
		}
	}
bool merkle_tree_partial::decode(data_buffer & buffer)
{
    m_total_transactions = buffer.read_uint32();
    
    auto count = buffer.read_var_int();
    
    for (auto i = 0; i < count; i++)
    {
        m_hashes.push_back(buffer.read_sha256());
    }
    
    std::vector<std::uint8_t> bytes;
    
    auto len = buffer.read_var_int();
    
    if (len > 0)
    {
        bytes.resize(len);
        
        buffer.read_bytes(reinterpret_cast<char *>(&bytes[0]), len);
    }
    
    m_flags.resize(bytes.size() * 8);
    
    for (auto i = 0; i < m_flags.size(); i++)
    {
        m_flags[i] = (bytes[i / 8] & (1 << (i % 8))) != 0;
    }
    
    is_invalid_ = false;
    
    return true;
}
Esempio n. 11
0
bool erase_begin(data_buffer& buffer, index123_type& index123)
{
  data_pointer ptr = buffer.begin();
  auto itr = index123.find( ptr.get_offset() /*static_cast<offset_t>( static_cast<size_t>(ptr) )*/);
  index123.erase(itr);
  buffer.deallocate(ptr, 1);
  return true;
}
Esempio n. 12
0
void hd_configuration::encode(data_buffer & buffer) const
{
    assert(m_id_key_master.digest().size() == ripemd160::digest_length);
    buffer.write_uint32(m_version);
    buffer.write_uint32(m_index);
    buffer.write_bytes(
        reinterpret_cast<const char *> (&m_id_key_master.digest()[0]),
        ripemd160::digest_length
    );
}
Esempio n. 13
0
            /**
             * Copy constructor
             * @param other The other data_buffer.
             */
			data_buffer(const data_buffer & other)
			    : m_read_ptr(0)
                , file_offset_(other.file_offset_)
                , file_(other.file_)
			{
                clear();
                
			    write_bytes(other.data(), other.size());
                
                m_read_ptr = m_data.size() > 0 ? &m_data[0] : 0;
			}
Esempio n. 14
0
bool hd_configuration::decode(data_buffer & buffer)
{
    m_version = buffer.read_uint32();
    m_index = buffer.read_uint32();
    buffer.read_bytes(
        reinterpret_cast<char *> (&m_id_key_master.digest()[0]),
        ripemd160::digest_length
    );

    return true;
}
Esempio n. 15
0
void checkpoint_sync::encode(data_buffer & buffer)
{
    buffer.write_var_int(m_message.size());
    buffer.write_bytes(
        reinterpret_cast<char *>(&m_message[0]), m_message.size()
    );
    
    buffer.write_var_int(m_signature.size());
    buffer.write_bytes(
        reinterpret_cast<char *>(&m_signature[0]), m_signature.size()
    );
}
Esempio n. 16
0
bool incentive_vote::verify(data_buffer & buffer)
{
    auto ret = false;
    
    /**
     * Calculate the hash of the nonce hash.
     */
    auto digest = hash::sha256d(
        m_hash_nonce.digest(), sha256::digest_length
    );

    /**
     * Hash the encoded message buffer.
     */
    sha256 hash_value = sha256::from_digest(&digest[0]);
    
    /**
     * Read the signature.
     */
    auto signature_len = buffer.read_var_int();

    if (signature_len > 0)
    {
        m_signature.resize(signature_len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_signature[0]), m_signature.size()
        );

        if (
            incentive::instance().verify(m_public_key, hash_value,
            m_signature) == true
            )
        {
            ret = true;
            
            log_debug(
                "Incentive vote verified value (" <<
                hash_value.to_string().substr(0, 8) << ")."
            );
        }
        else
        {
            log_error(
                "Incentive vote failed to verify value (" <<
                hash_value.to_string().substr(0, 8) << ")."
            );
        }
    }
    
    return ret;
}
Esempio n. 17
0
offs_t capricorn_disassembler::param_dr_lit(std::ostream &stream, offs_t pc, const data_buffer &opcodes)
{
	stream << "DR,=";
	// Here we assume that multi-byte instructions operate on 2 bytes because we
	// have no way of knowing how many they are (the actual number of bytes is
	// dynamically determined by the value of DRP register at run-time)
	unsigned bytes = BIT(opcodes.r8(pc), 0) ? 2 : 1;

	for (unsigned i = 1; i <= bytes; i++) {
		util::stream_format(stream, "$%02x ", opcodes.r8(pc+i));
	}

	return bytes;
}
Esempio n. 18
0
bool db_tx::read(const data_buffer & key, T & value)
{
    if (m_Db == 0)
    {
        return false;
    }
    
    Dbt dbt_key(key.data(), static_cast<std::uint32_t> (key.size()));

    Dbt dbt_value;
    
    dbt_value.set_flags(DB_DBT_MALLOC);
    
    auto ret = m_Db->get(m_DbTxn, &dbt_key, &dbt_value, 0);
    
    std::memset(dbt_key.get_data(), 0, dbt_key.get_size());
    
    if (dbt_value.get_data() == 0)
    {
        return false;
    }
    
    try
    {
        /**
         * Allocate the data_buffer.
         */
        data_buffer buffer(
            static_cast<char *>(dbt_value.get_data()), dbt_value.get_size()
        );
        
        /**
         * Decode the value from the buffer.
         */
        value.decode(buffer);
    }
    catch (std::exception & e)
    {
        log_error("DB TX read failed, what = " << e.what() << ".");
        
        return false;
    }

    std::memset(dbt_value.get_data(), 0, dbt_value.get_size());
    
    free(dbt_value.get_data());
    
    return ret == 0;
}
Esempio n. 19
0
bool alert::decode(data_buffer & buffer)
{
    auto len = buffer.read_var_int();
    
    if (len > 0)
    {
        m_message.resize(len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_message[0]), m_message.size()
        );
    }
    
    len = buffer.read_var_int();
    
    if (len > 0)
    {
        m_signature.resize(len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_signature[0]), m_signature.size()
        );
    }
    
    /**
     * If we have a message, decode it.
     */
    if (m_message.size() > 0)
    {
        /**
         * Allocate the message buffer.
         */
        data_buffer buffer_message;
        
        /**
         * Write the message into the buffer.
         */
        buffer_message.write_bytes(
            reinterpret_cast<const char *>(&m_message[0]), m_message.size()
        );
        
        /**
         * Decode the message.
         */
        alert_unsigned::decode(buffer_message);
    }
    
    return true;
}
Esempio n. 20
0
bool zerotime_lock::decode(data_buffer & buffer)
{
    /**
     * Decode the version.
     */
    m_version = buffer.read_uint32();

    assert(m_version == current_version);

    /**
     * Decode the transaction.
     */
    m_transaction.decode(buffer);

    /**
     * Decode the transaction hash.
     */
    buffer.read_bytes(
        reinterpret_cast<char *> (m_hash_tx.digest()), sha256::digest_length
    );

    assert(m_transaction.get_hash() == m_hash_tx);

    /**
     * Decode the expiration.
     */
    m_expiration = buffer.read_uint64();

    /**
     * Enforce the expiration.
     */
    if (
        m_expiration < time::instance().get_adjusted() + interval_min_expire ||
        m_expiration > time::instance().get_adjusted() + interval_max_expire
    )
    {
        m_expiration = time::instance().get_adjusted() + interval_min_expire;
    }

    /**
     * No signature is required because:
     * 1. The receiver may want to lock a non-zerotime transaction.
     * 2. It causes no harm to let other's lock the transaction.
     * 3. It conserves bandwidth and processing power.
     */

    return m_transaction.get_hash() == m_hash_tx;
}
Esempio n. 21
0
offs_t capricorn_disassembler::disassemble(std::ostream &stream, offs_t pc, const data_buffer &opcodes, const data_buffer &params)
{
	const dis_entry_t *p;
	uint8_t opcode = opcodes.r8(pc);

	for (p = dis_table; p->m_op_mask; p++) {
		if ((opcode & p->m_op_mask) == p->m_opcode) {
			offs_t res = 1 | p->m_dasm_flags | SUPPORTED;
			stream << p->m_mnemonic;
			if (p->m_has_mb) {
				stream << (BIT(opcode, 0) ? 'M' : 'B');
			}
			if (p->m_addr_mode != '\0') {
				stream << p->m_addr_mode;
			}
			if (p->m_param_fn != nullptr) {
				stream << " ";
				res += (this->*(p->m_param_fn))(stream, pc, opcodes);
			}
			return res;
		}
	}

	// Unknown opcode
	stream << "???";
	return 1 | SUPPORTED;
}
Esempio n. 22
0
offs_t arc_disassembler::disassemble(std::ostream &stream, offs_t pc, const data_buffer &opcodes, const data_buffer &params)
{
	uint32_t op = opcodes.r32(pc);

	uint8_t opcode = ARC_OPERATION;

	switch (opcode)
	{
		case 0x04: // B
		case 0x05: // BL
		util::stream_format(stream, "%s(%s)(%s) %08x", basic[opcode], conditions[ARC_CONDITION], delaytype[ARC_BRANCH_DELAY], (ARC_BRANCH_ADDR<<2)+pc+4);
		break;

		case 0x08: // ADD
		// todo, short / long immediate formats
		util::stream_format(stream, "%s %s , %s , %s (%08x)", basic[opcode], regnames[ARC_REGOP_DEST], regnames[ARC_REGOP_OP1], regnames[ARC_REGOP_OP2], op &~ 0xfffffe00);
		break;


		default:
		util::stream_format(stream, "%s (%08x)", basic[opcode], op &~ 0xf8000000);
		break;
	}

	return 4 | SUPPORTED;
}
Esempio n. 23
0
bool create(data_buffer& buffer, index123_type& index123)
{
  data_pointer ptr = buffer.allocate(1);
  *ptr = generate();
  auto itr = index123.find( ptr.get_offset() );
  if (itr == index123.end())
  {
    index123.insert( ptr.get_offset() );
  }
  else
  {
    buffer.deallocate(ptr, 1);
  }

  return itr == index123.end();
}
    bool packedmessage_scan<MessageType>::pack(data_buffer& buffer)const
    {

        if(!msgs)
            return false;

        unsigned msg_size = msgs->ByteSize();
        buffer.resize(HEADER_SIZE + msg_size);
        //Included header file.
        encode_header(buffer, msg_size);

				LOG(INFO)<<" Pack message, msg_size : "<< 
					msg_size <<", buffer : "<<buffer.size();

        return msgs->SerializeToArray(&buffer[HEADER_SIZE], msg_size);

    }
Esempio n. 25
0
bool incentive_vote::sign(data_buffer & buffer)
{
    auto ret = false;
    
    /**
     * Calculate the hash of the nonce hash.
     */
    auto digest = hash::sha256d(
        m_hash_nonce.digest(), sha256::digest_length
    );

    /**
     * Hash the encoded message buffer.
     */
    sha256 hash_value = sha256::from_digest(&digest[0]);
    
    if (incentive::instance().sign(hash_value, m_signature) == true)
    {
        /**
         * Write the signature length.
         */
        buffer.write_var_int(m_signature.size());
        
        /**
         * Write the signature.
         */
        buffer.write_bytes(
            reinterpret_cast<char *>(&m_signature[0]),
            m_signature.size()
        );

        log_debug(
            "Incentive vote signed value (" <<
            hash_value.to_string().substr(0, 8) << ")."
        );
        
        ret = true;
    }
    else
    {
        log_error("Incentive vote failed to sign value.");
    }
    
    return ret;
}
Esempio n. 26
0
bool incentive_answer::verify(data_buffer & buffer)
{
    auto ret = false;
    
    /**
     * Hash the encoded message buffer.
     */
    sha256 hash_value = m_public_key.get_hash();
    
    /**
     * Read the signature.
     */
    auto signature_len = buffer.read_var_int();

    if (signature_len > 0)
    {
        m_signature.resize(signature_len);
        
        buffer.read_bytes(
            reinterpret_cast<char *>(&m_signature[0]), m_signature.size()
        );
        
        if (
            incentive::instance().verify(
            m_public_key, hash_value, m_signature) == true
            )
        {
            ret = true;
            
            log_debug(
                "Incentive answer verified value (" <<
                hash_value.to_string().substr(0, 8) << ")."
            );
        }
        else
        {
            log_error(
                "Incentive answer failed to verify value (" <<
                hash_value.to_string().substr(0, 8) << ")."
            );
        }
    }
    
    return ret;
}
Esempio n. 27
0
offs_t capricorn_disassembler::param_jmp_off(std::ostream &stream, offs_t pc, const data_buffer &opcodes)
{
	uint16_t off = opcodes.r8(pc+1);
	if (BIT(off, 7)) {
		off -= 0x100;
	}
	util::stream_format(stream, "$%04x", (pc + 2 + off) & 0xffff);
	return 1;
}
Esempio n. 28
0
void incentive_answer::encode(data_buffer & buffer, const bool & is_copy )
{
    /**
     * Encode the version.
     */
    buffer.write_uint32(m_version);

    /**
     * Encode the public key.
     */
    m_public_key.encode(buffer);

    /**
     * Encode the transaction_in.
     */
    m_transaction_in.encode(buffer);

    /**
     * If we are encoding a copy reuse the existing signature.
     */
    if (is_copy == true)
    {
        /**
         * Write the signature length.
         */
        buffer.write_var_int(m_signature.size());
        
        /**
         * Write the signature.
         */
        buffer.write_bytes(
            reinterpret_cast<char *>(&m_signature[0]),
            m_signature.size()
        );
    }
    else
    {
        /**
         * Sign the message.
         */
        sign(buffer);
    }
}
Esempio n. 29
0
bool init(data_buffer& buffer, index123_type& index123)
{
  for (int i = 0; i < TEST_COUNT; )
  {
    if ( create(buffer, index123) )
    {
      ++i;
      if (i % 1000 == 0)
      {
        buffer.buffer().reserve( buffer.buffer().size() + 100);
        if( !check(buffer, index123) )
        {
          return false;
        }
      }
    }
  }
  return true;
}
Esempio n. 30
0
void transaction::encode(
    data_buffer & buffer, const bool & encode_version
    ) const
{
    if (encode_version)
    {
        /**
         * Write the version.
         */
        buffer.write_uint32(m_version);
    }
    
    /**
     * Write the time.
     */
    buffer.write_uint32(m_time);
    
    /**
     * Write the m_transactions_in size.
     */
    buffer.write_var_int(m_transactions_in.size());
    
    for (auto & i : m_transactions_in)
    {
        i.encode(buffer);
    }
    
    /**
     * Write the m_transactions_out size.
     */
    buffer.write_var_int(m_transactions_out.size());
    
    for (auto & i : m_transactions_out)
    {
        i.encode(buffer);
    }
    
    /**
     * Write the time lock.
     */
    buffer.write_uint32(m_time_lock);
}