BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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 } } // graphene::protocol
92 
93 namespace fc
94 {
95  using namespace std;
96  void to_variant( const graphene::protocol::public_key_type& var, fc::variant& vo, uint32_t max_depth )
97  {
98  vo = std::string( var );
99  }
100 
101  void from_variant( const fc::variant& var, graphene::protocol::public_key_type& vo, uint32_t max_depth )
102  {
104  }
105 
106  void from_variant( const fc::variant& var, std::shared_ptr<const graphene::protocol::fee_schedule>& vo,
107  uint32_t max_depth ) {
108  // If it's null, just make a new one
109  if (!vo) vo = std::make_shared<const graphene::protocol::fee_schedule>();
110  // Convert the non-const shared_ptr<const fee_schedule> to a non-const fee_schedule& so we can write it
111  // Don't decrement max_depth since we're not actually deserializing at this step
112  from_variant(var, const_cast<graphene::protocol::fee_schedule&>(*vo), max_depth);
113  }
114 
115 namespace raw {
116  template void pack( datastream<size_t>& s, const graphene::protocol::public_key_type& tx,
117  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
118  template void pack( datastream<char*>& s, const graphene::protocol::public_key_type& tx,
119  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
121  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
122 } } // fc::raw
template void unpack(datastream< const char * > &s, graphene::protocol::public_key_type &tx, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
public_key_data serialize() const
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
Definition: api.cpp:56
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
#define GRAPHENE_ADDRESS_PREFIX
Definition: config.hpp:27
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::ecc::public_key_data key_data
Definition: types.hpp:256
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
friend bool operator==(const public_key_type &p1, const fc::ecc::public_key &p2)
Definition: types.cpp:76
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
Defines exception&#39;s used by fc.
zero_initialized_array< unsigned char, 33 > public_key_data
Definition: elliptic.hpp:23
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
friend bool operator!=(const public_key_type &p1, const public_key_type &p2)
Definition: types.cpp:86
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
Definition: api.hpp:15
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
template void pack(datastream< char * > &s, const graphene::protocol::public_key_type &tx, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
std::string as_string() const
Definition: variant.cpp:469