forked from jaybny/fantasybit_dac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
76 lines (61 loc) · 2.26 KB
/
types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bts/blockchain/config.hpp>
#include <bts/blockchain/types.hpp>
#include <fc/crypto/base58.hpp>
#include <fc/exception/exception.hpp>
#include <fc/io/raw.hpp>
namespace bts { namespace blockchain {
public_key_type::public_key_type():key_data(){};
public_key_type::public_key_type( const fc::ecc::public_key_data& data )
:key_data( data ) {};
public_key_type::public_key_type( const fc::ecc::public_key& pubkey )
:key_data( pubkey ) {};
public_key_type::public_key_type( const std::string& base58str )
{
static const size_t prefix_len = strlen(BTS_ADDRESS_PREFIX);
FC_ASSERT( base58str.size() > prefix_len );
FC_ASSERT( base58str.substr( 0, prefix_len ) == BTS_ADDRESS_PREFIX, "", ("base58str", base58str) );
auto bin = fc::from_base58( base58str.substr( prefix_len ) );
auto bin_key = fc::raw::unpack<binary_key>(bin);
key_data = bin_key.data;
FC_ASSERT( fc::ripemd160::hash( key_data.data, key_data.size() )._hash[0] == bin_key.check );
};
public_key_type::operator fc::ecc::public_key_data() const
{
return key_data;
};
public_key_type::operator fc::ecc::public_key() const
{
return fc::ecc::public_key( key_data );
};
public_key_type::operator std::string() const
{
binary_key k;
k.data = key_data;
k.check = fc::ripemd160::hash( k.data.data, k.data.size() )._hash[0];
auto data = fc::raw::pack( k );
return BTS_ADDRESS_PREFIX + fc::to_base58( data.data(), data.size() );
}
bool operator == ( const public_key_type& p1, const fc::ecc::public_key& p2)
{
return p1.key_data == p2.serialize();
}
bool operator == ( const public_key_type& p1, const public_key_type& p2)
{
return p1.key_data == p2.key_data;
}
bool operator != ( const public_key_type& p1, const public_key_type& p2)
{
return p1.key_data != p2.key_data;
}
} } // bts::blockchain
namespace fc
{
void to_variant( const bts::blockchain::public_key_type& var, fc::variant& vo )
{
vo = std::string(var);
}
void from_variant( const fc::variant& var, bts::blockchain::public_key_type& vo )
{
vo = bts::blockchain::public_key_type( var.as_string() );
}
} // fc