Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
private_key.cpp
Go to the documentation of this file.
2#include <fc/utility.hpp>
4
5namespace fc { namespace crypto {
6 using namespace std;
7
8 struct public_key_visitor : visitor<public_key::storage_type> {
9 template<typename KeyType>
10 public_key::storage_type operator()(const KeyType& key) const
11 {
12 return public_key::storage_type(key.get_public_key());
13 }
14 };
15
17 {
18 return public_key(std::visit(public_key_visitor(), _storage));
19 }
20
21 struct sign_visitor : visitor<signature::storage_type> {
22 sign_visitor( const sha256& digest, bool require_canonical )
24 ,_require_canonical(require_canonical)
25 {}
26
27 template<typename KeyType>
28 signature::storage_type operator()(const KeyType& key) const
29 {
30 return signature::storage_type(key.sign(_digest, _require_canonical));
31 }
32
35 };
36
37 signature private_key::sign( const sha256& digest, bool require_canonical ) const
38 {
39 return signature(std::visit(sign_visitor(digest, require_canonical), _storage));
40 }
41
44 :_pub_storage(pub_storage)
45 {}
46
47 template<typename KeyType>
48 sha512 operator()(const KeyType& key) const
49 {
50 using PublicKeyType = typename KeyType::public_key_type;
51 return key.generate_shared_secret(std::template get<PublicKeyType>(_pub_storage));
52 }
53
55 };
56
58 {
59 return std::visit(generate_shared_secret_visitor(pub._storage), _storage);
60 }
61
62 template<typename Data>
63 string to_wif( const Data& secret, const fc::yield_function_t& yield )
64 {
65 const size_t size_of_data_to_hash = sizeof(typename Data::data_type) + 1;
66 const size_t size_of_hash_bytes = 4;
67 char data[size_of_data_to_hash + size_of_hash_bytes];
68 data[0] = (char)0x80; // this is the Bitcoin MainNet code
69 memcpy(&data[1], (const char*)&secret.serialize(), sizeof(typename Data::data_type));
70 sha256 digest = sha256::hash(data, size_of_data_to_hash);
72 memcpy(data + size_of_data_to_hash, (char*)&digest, size_of_hash_bytes);
73 return to_base58(data, sizeof(data), yield);
74 }
75
76 template<typename Data>
77 Data from_wif( const string& wif_key )
78 {
79 auto wif_bytes = from_base58(wif_key);
80 FC_ASSERT(wif_bytes.size() >= 5);
81 auto key_bytes = vector<char>(wif_bytes.begin() + 1, wif_bytes.end() - 4);
82 fc::sha256 check = fc::sha256::hash(wif_bytes.data(), wif_bytes.size() - 4);
83 fc::sha256 check2 = fc::sha256::hash(check);
84
85 FC_ASSERT(memcmp( (char*)&check, wif_bytes.data() + wif_bytes.size() - 4, 4 ) == 0 ||
86 memcmp( (char*)&check2, wif_bytes.data() + wif_bytes.size() - 4, 4 ) == 0 );
87
88 return Data(fc::variant(key_bytes).as<typename Data::data_type>());
89 }
90
91 static private_key::storage_type priv_parse_base58(const string& base58str)
92 {
93 const auto pivot = base58str.find('_');
94
95 if (pivot == std::string::npos) {
96 // wif import
97 using default_type = std::variant_alternative_t<0, private_key::storage_type>;
99 } else {
100 constexpr auto prefix = config::private_key_base_prefix;
101 const auto prefix_str = base58str.substr(0, pivot);
102 FC_ASSERT(prefix == prefix_str, "Private Key has invalid prefix: ${str}", ("str", base58str)("prefix_str", prefix_str));
103
104 auto data_str = base58str.substr(pivot + 1);
105 FC_ASSERT(!data_str.empty(), "Private Key has no data: ${str}", ("str", base58str));
106 return base58_str_parser<private_key::storage_type, config::private_key_prefix>::apply(data_str);
107 }
108 }
109
110 private_key::private_key(const std::string& base58str)
111 :_storage(priv_parse_base58(base58str))
112 {}
113
114 std::string private_key::to_string(const fc::yield_function_t& yield) const
115 {
116 auto which = _storage.index();
117
118 if (which == 0) {
119 using default_type = std::variant_alternative_t<0, private_key::storage_type>;
120 return to_wif(std::template get<default_type>(_storage), yield);
121 }
122
123 auto data_str = std::visit(base58str_visitor<storage_type, config::private_key_prefix>(yield), _storage);
124 return std::string(config::private_key_base_prefix) + "_" + data_str;
125 }
126
127 std::ostream& operator<<(std::ostream& s, const private_key& k) {
128 s << "private_key(" << k.to_string() << ')';
129 return s;
130 }
131
132 bool operator == ( const private_key& p1, const private_key& p2) {
133 return eq_comparator<private_key::storage_type>::apply(p1._storage, p2._storage);
134 }
135
136 bool operator < ( const private_key& p1, const private_key& p2)
137 {
138 return less_comparator<private_key::storage_type>::apply(p1._storage, p2._storage);
139 }
140} } // fc::crypto
141
142namespace fc
143{
145 {
146 vo = var.to_string(yield);
147 }
148
150 {
152 }
153
154} // fc
std::string to_string(const fc::yield_function_t &yield=fc::yield_function_t()) const
std::variant< ecc::private_key_shim, r1::private_key_shim, em::private_key_shim > storage_type
public_key get_public_key() const
signature sign(const sha256 &digest, bool require_canonical=true) const
sha512 generate_shared_secret(const public_key &pub) const
std::variant< ecc::public_key_shim, r1::public_key_shim, webauthn::public_key, em::public_key_shim > storage_type
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
const char * data() const
Definition sha256.cpp:31
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
string to_wif(const Data &secret, const fc::yield_function_t &yield)
std::ostream & operator<<(std::ostream &s, const private_key &k)
bool operator<(const private_key &p1, const private_key &p2)
Data from_wif(const string &wif_key)
bool operator==(const private_key &p1, const private_key &p2)
namespace sysio::chain
Definition authority.cpp:3
std::string to_base58(const char *d, size_t s, const fc::yield_function_t &yield)
Definition base58.cpp:618
fc::sha256 digest(const T &value)
Definition digest.hpp:9
bytes signature
Definition pke.hpp:17
std::vector< char > from_base58(const std::string &base58_str)
Definition base58.cpp:628
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
Definition zm2.cpp:48
static bool apply(const T &a, const T &b)
Definition common.hpp:119
generate_shared_secret_visitor(const public_key::storage_type &pub_storage)
const public_key::storage_type & _pub_storage
sha512 operator()(const KeyType &key) const
static bool apply(const T &a, const T &b)
Definition common.hpp:147
public_key::storage_type operator()(const KeyType &key) const
signature::storage_type operator()(const KeyType &key) const
sign_visitor(const sha256 &digest, bool require_canonical)
bool pub
char * s
memcpy((char *) pInfo->slotDescription, s, l)