Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
public_key.cpp
Go to the documentation of this file.
4
5namespace fc { namespace crypto {
6
7 struct recovery_visitor : fc::visitor<public_key::storage_type> {
8 recovery_visitor(const sha256& digest, bool check_canonical)
10 ,_check_canonical(check_canonical)
11 {}
12
13 template<typename SignatureType>
14 public_key::storage_type operator()(const SignatureType& s) const {
16 }
17
20 };
21
22 public_key::public_key( const signature& c, const sha256& digest, bool check_canonical )
23 :_storage(std::visit(recovery_visitor(digest, check_canonical), c._storage))
24 {
25 }
26
27 size_t public_key::which() const {
28 return _storage.index();
29 }
30
31 static public_key::storage_type parse_base58(const std::string& base58str)
32 {
33 constexpr auto legacy_prefix = config::public_key_legacy_prefix;
34 if(prefix_matches(legacy_prefix, base58str) && base58str.find('_') == std::string::npos ) {
35 auto sub_str = base58str.substr(const_strlen(legacy_prefix));
36 using default_type = typename std::variant_alternative_t<0, public_key::storage_type>; //public_key::storage_type::template type_at<0>;
37 using data_type = default_type::data_type;
38 using wrapper = checksummed_data<data_type>;
39 auto bin = fc::from_base58(sub_str);
40 FC_ASSERT(bin.size() == sizeof(data_type) + sizeof(uint32_t), "");
41 auto wrapped = fc::raw::unpack<wrapper>(bin);
42 FC_ASSERT(wrapper::calculate_checksum(wrapped.data) == wrapped.check);
43 return public_key::storage_type(default_type(wrapped.data));
44 } else {
45 constexpr auto prefix = config::public_key_base_prefix;
46
47 const auto pivot = base58str.find('_');
48 FC_ASSERT(pivot != std::string::npos, "No delimiter in string, cannot determine data type: ${str}", ("str", base58str));
49
50 const auto prefix_str = base58str.substr(0, pivot);
51 FC_ASSERT(prefix == prefix_str, "Public Key has invalid prefix: ${str}", ("str", base58str)("prefix_str", prefix_str));
52
53 auto data_str = base58str.substr(pivot + 1);
54 FC_ASSERT(!data_str.empty(), "Public Key has no data: ${str}", ("str", base58str));
55 return base58_str_parser<public_key::storage_type, config::public_key_prefix>::apply(data_str);
56 }
57 }
58
59 public_key::public_key(const std::string& base58str)
60 :_storage(parse_base58(base58str))
61 {}
62
63 struct is_valid_visitor : public fc::visitor<bool> {
64 template< typename KeyType >
65 bool operator()( const KeyType& key )const {
66 return key.valid();
67 }
68 };
69
71 {
72 return std::visit(is_valid_visitor(), _storage);
73 }
74
75 std::string public_key::to_string(const fc::yield_function_t& yield) const
76 {
78
79 auto which = _storage.index();
80 if (which == 0) {
81 return std::string(config::public_key_legacy_prefix) + data_str;
82 } else {
83 return std::string(config::public_key_base_prefix) + "_" + data_str;
84 }
85 }
86
87 std::ostream& operator<<(std::ostream& s, const public_key& k) {
88 s << "public_key(" << k.to_string() << ')';
89 return s;
90 }
91
95
96 bool operator != ( const public_key& p1, const public_key& p2) {
97 return !(p1 == p2);
98 }
99
100 bool operator < ( const public_key& p1, const public_key& p2)
101 {
103 }
104} } // fc::crypto
105
106namespace fc
107{
108 using namespace std;
110 {
111 vo = var.to_string(yield);
112 }
113
115 {
117 }
118} // fc
std::variant< ecc::public_key_shim, r1::public_key_shim, webauthn::public_key, em::public_key_shim > storage_type
size_t which() const
std::string to_string(const fc::yield_function_t &yield=fc::yield_function_t()) const
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.
std::ostream & operator<<(std::ostream &s, const private_key &k)
bool operator<(const private_key &p1, const private_key &p2)
bool operator!=(const public_key &p1, const public_key &p2)
bool operator==(const private_key &p1, const private_key &p2)
bool prefix_matches(const char *prefix, const std::string &str)
Definition common.hpp:27
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
namespace sysio::chain
Definition authority.cpp:3
fc::sha256 digest(const T &value)
Definition digest.hpp:9
constexpr size_t const_strlen(const char *str)
Definition utility.hpp:142
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
unsigned int uint32_t
Definition stdint.h:126
static bool apply(const T &a, const T &b)
Definition common.hpp:119
bool operator()(const KeyType &key) const
static bool apply(const T &a, const T &b)
Definition common.hpp:147
recovery_visitor(const sha256 &digest, bool check_canonical)
Definition public_key.cpp:8
public_key::storage_type operator()(const SignatureType &s) const
char * s