BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
types.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
27 
28 #include <fc/crypto/base58.hpp>
29 #include <fc/crypto/ripemd160.hpp>
31 #include <fc/io/raw.hpp>
32 
33 namespace graphene { namespace protocol {
34 
36 
38  :key_data( data ) {};
39 
41  :key_data( pubkey ) {};
42 
43  public_key_type::public_key_type( const std::string& base58str )
44  {
45  // TODO: Refactor syntactic checks into static is_valid()
46  // to make public_key_type API more similar to address API
47  std::string prefix( GRAPHENE_ADDRESS_PREFIX );
48  const size_t prefix_len = prefix.size();
49  FC_ASSERT( base58str.size() > prefix_len );
50  FC_ASSERT( base58str.substr( 0, prefix_len ) == prefix , "", ("base58str", base58str) );
51  auto bin = fc::from_base58( base58str.substr( prefix_len ) );
52  auto bin_key = fc::raw::unpack<binary_key>(bin);
53  key_data = bin_key.data;
54  FC_ASSERT( fc::ripemd160::hash( (char*) key_data.data(), key_data.size() )._hash[0].value() == bin_key.check );
55  };
56 
57  public_key_type::operator fc::ecc::public_key_data() const
58  {
59  return key_data;
60  };
61 
62  public_key_type::operator fc::ecc::public_key() const
63  {
64  return fc::ecc::public_key( key_data );
65  };
66 
67  public_key_type::operator std::string() const
68  {
69  binary_key k;
70  k.data = key_data;
71  k.check = fc::ripemd160::hash( (char*) k.data.data(), k.data.size() )._hash[0].value();
72  auto data = fc::raw::pack( k );
73  return GRAPHENE_ADDRESS_PREFIX + fc::to_base58( data.data(), data.size() );
74  }
75 
76  bool operator == ( const public_key_type& p1, const fc::ecc::public_key& p2)
77  {
78  return p1.key_data == p2.serialize();
79  }
80 
81  bool operator == ( const public_key_type& p1, const public_key_type& p2)
82  {
83  return p1.key_data == p2.key_data;
84  }
85 
86  bool operator != ( const public_key_type& p1, const public_key_type& p2)
87  {
88  return p1.key_data != p2.key_data;
89  }
90 
91  bool operator < ( const public_key_type& p1, const public_key_type& p2)
92  {
93  return address(p1) < address(p2);
94  }
95 
96 } } // graphene::protocol
97 
98 namespace fc
99 {
100  using namespace std;
101  void to_variant( const graphene::protocol::public_key_type& var, fc::variant& vo, uint32_t max_depth )
102  {
103  vo = std::string( var );
104  }
105 
106  void from_variant( const fc::variant& var, graphene::protocol::public_key_type& vo, uint32_t max_depth )
107  {
109  }
110 
111  void from_variant( const fc::variant& var, std::shared_ptr<const graphene::protocol::fee_schedule>& vo,
112  uint32_t max_depth ) {
113  // If it's null, just make a new one
114  if (!vo) vo = std::make_shared<const graphene::protocol::fee_schedule>();
115  // Convert the non-const shared_ptr<const fee_schedule> to a non-const fee_schedule& so we can write it
116  // Don't decrement max_depth since we're not actually deserializing at this step
117  from_variant(var, const_cast<graphene::protocol::fee_schedule&>(*vo), max_depth);
118  }
119 
120 namespace raw {
121  template void pack( datastream<size_t>& s, const graphene::protocol::public_key_type& tx,
122  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
123  template void pack( datastream<char*>& s, const graphene::protocol::public_key_type& tx,
124  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
125  template void unpack( datastream<const char*>& s, graphene::protocol::public_key_type& tx,
126  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
127 } } // fc::raw
graphene::protocol::operator<
bool operator<(const price &a, const price &b)
Definition: asset.cpp:45
FC_PACK_MAX_DEPTH
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
fc::to_base58
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
fc::raw::unpack
template void unpack(datastream< const char * > &s, graphene::protocol::public_key_type &tx, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
graphene::protocol::fee_schedule
contains all of the parameters necessary to calculate the fee for any operation
Definition: fee_schedule.hpp:173
graphene::protocol::public_key_type::key_data
fc::ecc::public_key_data key_data
Definition: types.hpp:318
fc::raw::pack
template void pack(datastream< char * > &s, const graphene::protocol::public_key_type &tx, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
fc
Definition: api.hpp:15
fc::from_base58
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
fee_schedule.hpp
fc::variant::as_string
std::string as_string() const
Definition: variant.cpp:469
fc::zero_initialized_array< unsigned char, 33 >
fc::ecc::public_key
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
graphene::protocol::public_key_type::binary_key::check
uint32_t check
Definition: types.hpp:315
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
graphene::protocol::public_key_type
Definition: types.hpp:312
fc::ripemd160::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: ripemd160.hpp:71
graphene::protocol::public_key_type::binary_key::data
fc::ecc::public_key_data data
Definition: types.hpp:316
types.hpp
fc::ecc::public_key_data
zero_initialized_array< unsigned char, 33 > public_key_data
Definition: elliptic.hpp:23
graphene::protocol::operator==
bool operator==(const price &a, const price &b)
Definition: asset.cpp:34
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
graphene::protocol::public_key_type::binary_key
Definition: types.hpp:313
ripemd160.hpp
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
base58.hpp
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
std
Definition: zeroed_array.hpp:76
GRAPHENE_ADDRESS_PREFIX
#define GRAPHENE_ADDRESS_PREFIX
Definition: config.hpp:27
fc::ripemd160::hash
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
graphene::protocol::public_key_type::public_key_type
public_key_type()
Definition: types.cpp:35
graphene
Definition: api.cpp:48
graphene::protocol::address
a 160 bit hash of a public key
Definition: address.hpp:44
fc::ecc::public_key::serialize
public_key_data serialize() const
Definition: elliptic_secp256k1.cpp:118
graphene::protocol::operator!=
bool operator!=(const address &a, const address &b)
Definition: address.hpp:61
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
raw.hpp