Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
wallet_api_plugin.cpp
Go to the documentation of this file.
5
6#include <fc/variant.hpp>
7#include <fc/io/json.hpp>
8
9#include <chrono>
10
11namespace sysio { namespace detail {
13}}
14
16
17namespace sysio {
18
19static appbase::abstract_plugin& _wallet_api_plugin = app().register_plugin<wallet_api_plugin>();
20
21using namespace sysio;
22
23#define CALL_WITH_400(api_name, api_handle, call_name, INVOKE, http_response_code) \
24{std::string("/v1/" #api_name "/" #call_name), \
25 [&api_handle](string, string body, url_response_callback cb) mutable { \
26 try { \
27 INVOKE \
28 cb(http_response_code, fc::variant(result)); \
29 } catch (...) { \
30 http_plugin::handle_exception(#api_name, #call_name, body, cb); \
31 } \
32 }}
33
34#define INVOKE_R_R(api_handle, call_name, in_param) \
35 auto params = parse_params<in_param, http_params_types::params_required>(body);\
36 auto result = api_handle.call_name( std::move(params) );
37
38#define INVOKE_R_R_R(api_handle, call_name, in_param0, in_param1) \
39 const auto& params = parse_params<fc::variants, http_params_types::params_required>(body);\
40 if (params.size() != 2) { \
41 SYS_THROW(chain::invalid_http_request, "Missing valid input from POST body"); \
42 } \
43 auto result = api_handle.call_name(params.at(0).as<in_param0>(), params.at(1).as<in_param1>());
44
45// chain_id_type does not have default constructor, keep it unchanged
46#define INVOKE_R_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2) \
47 const auto& params = parse_params<fc::variants, http_params_types::params_required>(body);\
48 if (params.size() != 3) { \
49 SYS_THROW(chain::invalid_http_request, "Missing valid input from POST body"); \
50 } \
51 auto result = api_handle.call_name(params.at(0).as<in_param0>(), params.at(1).as<in_param1>(), params.at(2).as<in_param2>());
52
53#define INVOKE_R_V(api_handle, call_name) \
54 body = parse_params<std::string, http_params_types::no_params>(body); \
55 auto result = api_handle.call_name();
56
57#define INVOKE_V_R(api_handle, call_name, in_param) \
58 auto params = parse_params<in_param, http_params_types::params_required>(body);\
59 api_handle.call_name( std::move(params) ); \
60 sysio::detail::wallet_api_plugin_empty result;
61
62#define INVOKE_V_R_R(api_handle, call_name, in_param0, in_param1) \
63 const auto& params = parse_params<fc::variants, http_params_types::params_required>(body);\
64 if (params.size() != 2) { \
65 SYS_THROW(chain::invalid_http_request, "Missing valid input from POST body"); \
66 } \
67 api_handle.call_name(params.at(0).as<in_param0>(), params.at(1).as<in_param1>()); \
68 sysio::detail::wallet_api_plugin_empty result;
69
70#define INVOKE_V_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2) \
71 const auto& params = parse_params<fc::variants, http_params_types::params_required>(body);\
72 if (params.size() != 3) { \
73 SYS_THROW(chain::invalid_http_request, "Missing valid input from POST body"); \
74 } \
75 api_handle.call_name(params.at(0).as<in_param0>(), params.at(1).as<in_param1>(), params.at(2).as<in_param2>()); \
76 sysio::detail::wallet_api_plugin_empty result;
77
78#define INVOKE_V_V(api_handle, call_name) \
79 body = parse_params<std::string, http_params_types::no_params>(body); \
80 api_handle.call_name(); \
81 sysio::detail::wallet_api_plugin_empty result;
82
84 ilog("starting wallet_api_plugin");
85 // lifetime of plugin is lifetime of application
86 auto& wallet_mgr = app().get_plugin<wallet_plugin>().get_wallet_manager();
87
88 app().get_plugin<http_plugin>().add_api({
89 CALL_WITH_400(wallet, wallet_mgr, set_timeout,
90 INVOKE_V_R(wallet_mgr, set_timeout, int64_t), 200),
91 // chain::chain_id_type has an inaccessible default constructor
92 CALL_WITH_400(wallet, wallet_mgr, sign_transaction,
93 INVOKE_R_R_R_R(wallet_mgr, sign_transaction, chain::signed_transaction, chain::flat_set<public_key_type>, chain::chain_id_type), 201),
94 CALL_WITH_400(wallet, wallet_mgr, sign_digest,
95 INVOKE_R_R_R(wallet_mgr, sign_digest, chain::digest_type, public_key_type), 201),
96 CALL_WITH_400(wallet, wallet_mgr, create,
97 INVOKE_R_R(wallet_mgr, create, std::string), 201),
98 CALL_WITH_400(wallet, wallet_mgr, open,
99 INVOKE_V_R(wallet_mgr, open, std::string), 200),
100 CALL_WITH_400(wallet, wallet_mgr, lock_all,
101 INVOKE_V_V(wallet_mgr, lock_all), 200),
102 CALL_WITH_400(wallet, wallet_mgr, lock,
103 INVOKE_V_R(wallet_mgr, lock, std::string), 200),
104 CALL_WITH_400(wallet, wallet_mgr, unlock,
105 INVOKE_V_R_R(wallet_mgr, unlock, std::string, std::string), 200),
106 CALL_WITH_400(wallet, wallet_mgr, import_key,
107 INVOKE_V_R_R(wallet_mgr, import_key, std::string, std::string), 201),
108 CALL_WITH_400(wallet, wallet_mgr, remove_key,
109 INVOKE_V_R_R_R(wallet_mgr, remove_key, std::string, std::string, std::string), 201),
110 CALL_WITH_400(wallet, wallet_mgr, create_key,
111 INVOKE_R_R_R(wallet_mgr, create_key, std::string, std::string), 201),
112 CALL_WITH_400(wallet, wallet_mgr, list_wallets,
113 INVOKE_R_V(wallet_mgr, list_wallets), 200),
114 CALL_WITH_400(wallet, wallet_mgr, list_keys,
115 INVOKE_R_R_R(wallet_mgr, list_keys, std::string, std::string), 200),
116 CALL_WITH_400(wallet, wallet_mgr, get_public_keys,
117 INVOKE_R_V(wallet_mgr, get_public_keys), 200)
118 });
119}
120
121void wallet_api_plugin::plugin_initialize(const variables_map& options) {
122 try {
123 const auto& _http_plugin = app().get_plugin<http_plugin>();
124 if( !_http_plugin.is_on_loopback()) {
125 if( !_http_plugin.is_secure()) {
126 elog( "\n"
127 "********!!!SECURITY ERROR!!!********\n"
128 "* *\n"
129 "* -- Wallet API -- *\n"
130 "* - EXPOSED to the LOCAL NETWORK - *\n"
131 "* - HTTP RPC is NOT encrypted - *\n"
132 "* - Password and/or Private Keys - *\n"
133 "* - are at HIGH risk of exposure - *\n"
134 "* *\n"
135 "************************************\n" );
136 } else {
137 wlog( "\n"
138 "**********SECURITY WARNING**********\n"
139 "* *\n"
140 "* -- Wallet API -- *\n"
141 "* - EXPOSED to the LOCAL NETWORK - *\n"
142 "* - Password and/or Private Keys - *\n"
143 "* - are at risk of exposure - *\n"
144 "* *\n"
145 "************************************\n" );
146 }
147 }
149}
150
151
152#undef INVOKE_R_R
153#undef INVOKE_R_R_R_R
154#undef INVOKE_R_V
155#undef INVOKE_V_R
156#undef INVOKE_V_R_R
157#undef INVOKE_V_V
158#undef CALL
159
160}
#define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code, params_type)
abstract_plugin & get_plugin(const string &name) const
void plugin_initialize(const variables_map &vm)
#define INVOKE_R_V(api_handle, call_name)
#define FC_LOG_AND_RETHROW()
#define wlog(FORMAT,...)
Definition logger.hpp:124
#define ilog(FORMAT,...)
Definition logger.hpp:118
#define elog(FORMAT,...)
Definition logger.hpp:130
application & app()
#define INVOKE_V_R(api_handle, call_name, in_param)
#define INVOKE_V_V(api_handle, call_name)
#define INVOKE_R_R(api_handle, call_name, in_param)
void sign_transaction(signed_transaction &trx, fc::variant &required_keys, const chain_id_type &chain_id)
Definition main.cpp:319
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
signed __int64 int64_t
Definition stdint.h:135
#define INVOKE_V_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2)
#define INVOKE_V_R_R(api_handle, call_name, in_param0, in_param1)
#define INVOKE_R_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2)
#define INVOKE_R_R_R(api_handle, call_name, in_param0, in_param1)