88#include < fc/variant_object.hpp>
99#include < fc/reflect/variant.hpp>
1010
11- #include < shared_mutex>
12-
1311namespace beekeeper {
1412
1513#define HIVE_BEEKEEPER_API_NAME " beekeeper_api"
@@ -26,17 +24,19 @@ class beekeeper_api_impl
2624
2725 extended_api ex_api;
2826
29- std::shared_mutex mtx;
30-
3127 public_key_type create ( const std::string& source )
3228 {
3329 return utility::public_key::create ( source, prefix );
3430 }
3531
3632 public:
37- beekeeper_api_impl ( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, uint64_t unlock_interval )
33+ beekeeper_api_impl ( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, std::shared_ptr<mutex_handler> mtx_handler, appbase::application& app, uint64_t unlock_interval )
3834 : prefix( HIVE_ADDRESS_PREFIX/* At now this is only one allowed prefix, by maybe in the future custom prefixes could be used as well.*/ ),
39- ex_api ( unlock_interval ), _wallet_mgr( wallet_mgr ) {}
35+ ex_api ( unlock_interval ), _wallet_mgr( wallet_mgr ), _mtx_handler( mtx_handler )
36+ {
37+ FC_ASSERT ( _wallet_mgr );
38+ FC_ASSERT ( _mtx_handler );
39+ }
4040
4141 DECLARE_API_IMPL
4242 (
@@ -66,58 +66,59 @@ class beekeeper_api_impl
6666 )
6767
6868 std::shared_ptr<beekeeper::beekeeper_wallet_manager> _wallet_mgr;
69+ std::shared_ptr<mutex_handler> _mtx_handler;
6970};
7071
7172DEFINE_API_IMPL ( beekeeper_api_impl, create )
7273{
73- std::unique_lock guard ( mtx );
74+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
7475
7576 return { _wallet_mgr->create ( args.token , args.wallet_name , args.password , args.is_temporary ) };
7677}
7778
7879DEFINE_API_IMPL ( beekeeper_api_impl, open )
7980{
80- std::unique_lock guard ( mtx );
81+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
8182
8283 _wallet_mgr->open ( args.token , args.wallet_name );
8384 return open_return ();
8485}
8586
8687DEFINE_API_IMPL ( beekeeper_api_impl, close )
8788{
88- std::unique_lock guard ( mtx );
89+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
8990
9091 _wallet_mgr->close ( args.token , args.wallet_name );
9192 return close_return ();
9293}
9394
9495DEFINE_API_IMPL ( beekeeper_api_impl, set_timeout )
9596{
96- std::unique_lock guard ( mtx );
97+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
9798
9899 _wallet_mgr->set_timeout ( args.token , args.seconds );
99100 return set_timeout_return ();
100101}
101102
102103DEFINE_API_IMPL ( beekeeper_api_impl, lock_all )
103104{
104- std::unique_lock guard ( mtx );
105+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
105106
106107 _wallet_mgr->lock_all ( args.token );
107108 return lock_all_return ();
108109}
109110
110111DEFINE_API_IMPL ( beekeeper_api_impl, lock )
111112{
112- std::unique_lock guard ( mtx );
113+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
113114
114115 _wallet_mgr->lock ( args.token , args.wallet_name );
115116 return lock_return ();
116117}
117118
118119DEFINE_API_IMPL ( beekeeper_api_impl, unlock )
119120{
120- std::unique_lock guard ( mtx );
121+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
121122
122123 if ( ex_api.unlock_allowed () )
123124 {
@@ -138,124 +139,125 @@ DEFINE_API_IMPL( beekeeper_api_impl, unlock )
138139
139140DEFINE_API_IMPL ( beekeeper_api_impl, import_key )
140141{
141- std::unique_lock guard ( mtx );
142+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
142143
143144 return { _wallet_mgr->import_key ( args.token , args.wallet_name , args.wif_key , prefix ) };
144145}
145146
146147DEFINE_API_IMPL ( beekeeper_api_impl, import_keys )
147148{
148- std::unique_lock guard ( mtx );
149+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
149150
150151 return { _wallet_mgr->import_keys ( args.token , args.wallet_name , args.wif_keys , prefix ) };
151152}
152153
153154DEFINE_API_IMPL ( beekeeper_api_impl, remove_key )
154155{
155- std::unique_lock guard ( mtx );
156+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
156157
157158 _wallet_mgr->remove_key ( args.token , args.wallet_name , create ( args.public_key ) );
158159 return remove_key_return ();
159160}
160161
161162DEFINE_API_IMPL ( beekeeper_api_impl, list_wallets )
162163{
163- std::shared_lock guard ( mtx );
164+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
164165
165166 return { _wallet_mgr->list_wallets ( args.token ) };
166167}
167168
168169DEFINE_API_IMPL ( beekeeper_api_impl, list_created_wallets )
169170{
170- std::shared_lock guard ( mtx );
171+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
171172
172173 return { _wallet_mgr->list_created_wallets ( args.token ) };
173174}
174175
175176DEFINE_API_IMPL ( beekeeper_api_impl, get_public_keys )
176177{
177- std::shared_lock guard ( mtx );
178+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
178179
179180 auto _keys = _wallet_mgr->get_public_keys ( args.token , args.wallet_name );
180181 return { utility::get_public_keys ( _keys ) };
181182}
182183
183184DEFINE_API_IMPL ( beekeeper_api_impl, sign_digest )
184185{
185- std::shared_lock guard ( mtx );
186+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
186187
187188 using namespace beekeeper ;
188189 return { _wallet_mgr->sign_digest ( args.token , args.wallet_name , args.sig_digest , create ( args.public_key ), prefix ) };
189190}
190191
191192DEFINE_API_IMPL ( beekeeper_api_impl, get_info )
192193{
193- std::shared_lock guard ( mtx );
194+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
194195
195196 return _wallet_mgr->get_info ( args.token );
196197}
197198
198199DEFINE_API_IMPL ( beekeeper_api_impl, create_session )
199200{
200- std::unique_lock guard ( mtx );
201+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
201202
202203 return { _wallet_mgr->create_session ( args.salt , args.notifications_endpoint ) };
203204}
204205
205206DEFINE_API_IMPL ( beekeeper_api_impl, close_session )
206207{
207- std::unique_lock guard ( mtx );
208+ std::unique_lock guard ( _mtx_handler-> get_mutex () );
208209
209210 _wallet_mgr->close_session ( args.token );
210211 return close_session_return ();
211212}
212213
213214DEFINE_API_IMPL ( beekeeper_api_impl, has_matching_private_key )
214215{
215- std::shared_lock guard ( mtx );
216+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
216217
217218 return { _wallet_mgr->has_matching_private_key ( args.token , args.wallet_name , create ( args.public_key ) ) };
218219}
219220
220221DEFINE_API_IMPL ( beekeeper_api_impl, encrypt_data )
221222{
222- std::shared_lock guard ( mtx );
223+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
223224
224225 return { _wallet_mgr->encrypt_data ( args.token , create ( args.from_public_key ), create ( args.to_public_key ), args.wallet_name , args.content , args.nonce , prefix ) };
225226}
226227
227228DEFINE_API_IMPL ( beekeeper_api_impl, decrypt_data )
228229{
229- std::shared_lock guard ( mtx );
230+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
230231
231232 return { _wallet_mgr->decrypt_data ( args.token , create ( args.from_public_key ), create ( args.to_public_key ), args.wallet_name , args.encrypted_content ) };
232233}
233234
234235DEFINE_API_IMPL ( beekeeper_api_impl, get_version )
235236{
236- std::shared_lock guard ( mtx );
237+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
237238
238239 return _wallet_mgr->get_version ();
239240}
240241
241242DEFINE_API_IMPL ( beekeeper_api_impl, has_wallet )
242243{
243- std::shared_lock guard ( mtx );
244+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
244245
245246 return { _wallet_mgr->has_wallet ( args.token , args.wallet_name ) };
246247}
247248
248249DEFINE_API_IMPL ( beekeeper_api_impl, is_wallet_unlocked )
249250{
250- std::shared_lock guard ( mtx );
251+ std::shared_lock guard ( _mtx_handler-> get_mutex () );
251252
252253 return _wallet_mgr->is_wallet_unlocked ( args.token , args.wallet_name );
253254}
254255
255256} // detail
256257
257- beekeeper_wallet_api::beekeeper_wallet_api ( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, appbase::application& app, uint64_t unlock_interval )
258- : my( new detail::beekeeper_api_impl( wallet_mgr, unlock_interval ) )
258+ beekeeper_wallet_api::beekeeper_wallet_api ( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, std::shared_ptr<mutex_handler> mtx_handler,
259+ appbase::application& app, uint64_t unlock_interval )
260+ : my( new detail::beekeeper_api_impl( wallet_mgr, mtx_handler, app, unlock_interval ) )
259261{
260262 JSON_RPC_REGISTER_API ( HIVE_BEEKEEPER_API_NAME );
261263}
0 commit comments