BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
pts_address.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  */
25 
26 #include <fc/crypto/base58.hpp>
27 #include <fc/crypto/elliptic.hpp>
28 #include <fc/crypto/ripemd160.hpp>
29 #include <fc/io/raw.hpp>
30 #include <algorithm>
31 
32 namespace graphene { namespace protocol {
33 
35  {
36  memset( addr.data(), 0, addr.size() );
37  }
38 
39  pts_address::pts_address( const std::string& base58str )
40  {
41  std::vector<char> v = fc::from_base58( fc::string(base58str) );
42  if( v.size() )
43  memcpy( addr.data(), v.data(), std::min<size_t>( v.size(), addr.size() ) );
44 
45  FC_ASSERT(is_valid(), "invalid pts_address ${a}", ("a", base58str));
46  }
47 
48  pts_address::pts_address( const fc::ecc::public_key& pub, bool compressed, uint8_t version )
49  {
50  fc::sha256 sha2;
51  if( compressed )
52  {
53  auto dat = pub.serialize();
54  sha2 = fc::sha256::hash((char*) dat.data(), dat.size() );
55  }
56  else
57  {
58  auto dat = pub.serialize_ecc_point();
59  sha2 = fc::sha256::hash((char*) dat.data(), dat.size() );
60  }
61  auto rep = fc::ripemd160::hash((char*)&sha2,sizeof(sha2));
62  addr[0] = version;
63  memcpy( addr.data() + 1, (char*)&rep, sizeof(rep) );
64  auto check = fc::sha256::hash( addr.data(), sizeof(rep)+1 );
65  check = fc::sha256::hash(check);
66  memcpy( addr.data() + 1 + sizeof(rep), (char*)&check, 4 );
67  }
68 
74  {
75  auto check = fc::sha256::hash( addr.data(), sizeof(fc::ripemd160)+1 );
76  check = fc::sha256::hash(check);
77  return memcmp( addr.data() + 1 + sizeof(fc::ripemd160), (char*)&check, 4 ) == 0;
78  }
79 
80  pts_address::operator std::string()const
81  {
82  return fc::to_base58( addr.data(), addr.size() );
83  }
84 
85 } } // namespace graphene
86 
87 namespace fc
88 {
89  void to_variant( const graphene::protocol::pts_address& var, variant& vo, uint32_t max_depth )
90  {
91  vo = std::string(var);
92  }
93  void from_variant( const variant& var, graphene::protocol::pts_address& vo, uint32_t max_depth )
94  {
96  }
97 
98 namespace raw {
99  template void pack( datastream<size_t>& s, const graphene::protocol::pts_address& tx,
100  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
101  template void pack( datastream<char*>& s, const graphene::protocol::pts_address& tx,
102  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
103  template void unpack( datastream<const char*>& s, graphene::protocol::pts_address& tx,
104  uint32_t _max_depth=FC_PACK_MAX_DEPTH );
105 } } // fc::raw
FC_PACK_MAX_DEPTH
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
graphene::protocol::pts_address::addr
std::array< char, 25 > addr
binary representation of address, 0-initialized
Definition: pts_address.hpp:57
fc::to_base58
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
pts_address.hpp
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc
Definition: api.hpp:15
fc::from_base58
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
graphene::protocol::pts_address
Definition: pts_address.hpp:41
fc::sha256
Definition: sha256.hpp:10
graphene::protocol::pts_address::version
uint8_t version() const
Definition: pts_address.hpp:52
fc::variant::as_string
std::string as_string() const
Definition: variant.cpp:469
fc::ecc::public_key
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::ecc::public_key::serialize_ecc_point
public_key_point_data serialize_ecc_point() const
Definition: elliptic_secp256k1.cpp:124
graphene::protocol::pts_address::is_valid
bool is_valid() const
Definition: pts_address.cpp:73
fc::ripemd160
Definition: ripemd160.hpp:11
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
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
fc::ripemd160::hash
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
elliptic.hpp
graphene::protocol::pts_address::pts_address
pts_address()
constructs empty / null address
Definition: pts_address.cpp:34
fc::sha256::hash
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
graphene
Definition: api.cpp:48
fc::ecc::public_key::serialize
public_key_data serialize() const
Definition: elliptic_secp256k1.cpp:118
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
raw.hpp