@@ -27,16 +27,10 @@ use witnet_protected::ProtectedString;
27
27
28
28
/// Start the signature manager
29
29
pub fn start ( ) {
30
- let addr = SignatureManager :: start_default ( ) ;
30
+ let addr = SignatureManagerAdapter :: start_default ( ) ;
31
31
actix:: SystemRegistry :: set ( addr) ;
32
32
}
33
33
34
- /// Set the key used to sign
35
- pub fn set_key ( key : SK ) -> impl Future < Item = ( ) , Error = failure:: Error > {
36
- let addr = SignatureManager :: from_registry ( ) ;
37
- addr. send ( SetKey ( key) ) . flatten ( )
38
- }
39
-
40
34
/// Sign a piece of (Hashable) data with the stored key.
41
35
///
42
36
/// This might fail if the manager has not been initialized with a key
@@ -53,31 +47,31 @@ where
53
47
///
54
48
/// This might fail if the manager has not been initialized with a key
55
49
pub fn sign_data ( data : [ u8 ; 32 ] ) -> impl Future < Item = KeyedSignature , Error = failure:: Error > {
56
- let addr = SignatureManager :: from_registry ( ) ;
50
+ let addr = SignatureManagerAdapter :: from_registry ( ) ;
57
51
addr. send ( Sign ( data. to_vec ( ) ) ) . flatten ( )
58
52
}
59
53
60
54
/// Get the public key hash.
61
55
///
62
56
/// This might fail if the manager has not been initialized with a key
63
57
pub fn pkh ( ) -> impl Future < Item = PublicKeyHash , Error = failure:: Error > {
64
- let addr = SignatureManager :: from_registry ( ) ;
58
+ let addr = SignatureManagerAdapter :: from_registry ( ) ;
65
59
addr. send ( GetPkh ) . flatten ( )
66
60
}
67
61
68
62
/// Get the public key.
69
63
///
70
64
/// This might fail if the manager has not been initialized with a key
71
65
pub fn public_key ( ) -> impl Future < Item = PublicKey , Error = failure:: Error > {
72
- let addr = SignatureManager :: from_registry ( ) ;
66
+ let addr = SignatureManagerAdapter :: from_registry ( ) ;
73
67
addr. send ( GetPublicKey ) . flatten ( )
74
68
}
75
69
76
70
/// Create a VRF proof for the provided message with the stored key
77
71
pub fn vrf_prove (
78
72
message : VrfMessage ,
79
73
) -> impl Future < Item = ( VrfProof , Hash ) , Error = failure:: Error > {
80
- let addr = SignatureManager :: from_registry ( ) ;
74
+ let addr = SignatureManagerAdapter :: from_registry ( ) ;
81
75
addr. send ( VrfProve ( message) ) . flatten ( )
82
76
}
83
77
@@ -100,9 +94,13 @@ impl SignatureManager {
100
94
}
101
95
102
96
struct SetKey ( SK ) ;
97
+
103
98
struct Sign ( Vec < u8 > ) ;
99
+
104
100
struct GetPkh ;
101
+
105
102
struct GetPublicKey ;
103
+
106
104
struct VrfProve ( VrfMessage ) ;
107
105
108
106
fn persist_master_key ( master_key : ExtendedSK ) -> impl Future < Item = ( ) , Error = failure:: Error > {
@@ -134,7 +132,7 @@ fn create_master_key() -> Box<dyn Future<Item = SK, Error = failure::Error>> {
134
132
}
135
133
136
134
impl Actor for SignatureManager {
137
- type Context = Context < Self > ;
135
+ type Context = SyncContext < Self > ;
138
136
139
137
fn started ( & mut self , ctx : & mut Self :: Context ) {
140
138
log:: debug!( "Signature Manager actor has been started!" ) ;
@@ -148,29 +146,9 @@ impl Actor for SignatureManager {
148
146
. ok ( ) ;
149
147
150
148
self . secp = Some ( CryptoEngine :: new ( ) ) ;
151
-
152
- storage_mngr:: get :: < _ , ExtendedSecretKey > ( & MASTER_KEY )
153
- . and_then ( move |master_key_from_storage| {
154
- master_key_from_storage. map_or_else ( create_master_key, |master_key| {
155
- let master_key: ExtendedSK = master_key. into ( ) ;
156
- let fut = futures:: future:: ok ( master_key. into ( ) ) ;
157
-
158
- Box :: new ( fut)
159
- } )
160
- } )
161
- . map_err ( |e| log:: error!( "Couldn't initialize Signature Manager: {}" , e) )
162
- . into_actor ( self )
163
- . map ( |secret_key, act, _ctx| {
164
- act. set_key ( secret_key) ;
165
- } )
166
- . wait ( ctx) ;
167
149
}
168
150
}
169
151
170
- impl Supervised for SignatureManager { }
171
-
172
- impl SystemService for SignatureManager { }
173
-
174
152
impl Message for SetKey {
175
153
type Result = Result < ( ) , failure:: Error > ;
176
154
}
@@ -267,3 +245,84 @@ impl Handler<VrfProve> for SignatureManager {
267
245
}
268
246
}
269
247
}
248
+
249
+ struct SignatureManagerAdapter {
250
+ crypto : Addr < SignatureManager > ,
251
+ }
252
+
253
+ impl Supervised for SignatureManagerAdapter { }
254
+
255
+ impl SystemService for SignatureManagerAdapter { }
256
+
257
+ impl Default for SignatureManagerAdapter {
258
+ fn default ( ) -> Self {
259
+ let crypto = SyncArbiter :: start ( 1 , SignatureManager :: default) ;
260
+ Self { crypto }
261
+ }
262
+ }
263
+
264
+ impl Actor for SignatureManagerAdapter {
265
+ type Context = Context < Self > ;
266
+
267
+ fn started ( & mut self , ctx : & mut Self :: Context ) {
268
+ log:: debug!( "Signature Manager Adapter actor has been started!" ) ;
269
+ let crypto = self . crypto . clone ( ) ;
270
+
271
+ storage_mngr:: get :: < _ , ExtendedSecretKey > ( & MASTER_KEY )
272
+ . and_then ( move |master_key_from_storage| {
273
+ master_key_from_storage. map_or_else ( create_master_key, |master_key| {
274
+ let master_key: ExtendedSK = master_key. into ( ) ;
275
+ let fut = futures:: future:: ok ( master_key. into ( ) ) ;
276
+
277
+ Box :: new ( fut)
278
+ } )
279
+ } )
280
+ . and_then ( move |master_key| crypto. send ( SetKey ( master_key) ) . flatten ( ) )
281
+ . map_err ( |err| {
282
+ log:: error!( "Failed to configure master key: {}" , err) ;
283
+ System :: current ( ) . stop_with_code ( 1 ) ;
284
+ } )
285
+ . into_actor ( self )
286
+ . wait ( ctx) ;
287
+ }
288
+ }
289
+
290
+ impl Handler < SetKey > for SignatureManagerAdapter {
291
+ type Result = ResponseFuture < ( ) , failure:: Error > ;
292
+
293
+ fn handle ( & mut self , msg : SetKey , _ctx : & mut Self :: Context ) -> Self :: Result {
294
+ Box :: new ( self . crypto . send ( msg) . flatten ( ) )
295
+ }
296
+ }
297
+
298
+ impl Handler < Sign > for SignatureManagerAdapter {
299
+ type Result = ResponseFuture < KeyedSignature , failure:: Error > ;
300
+
301
+ fn handle ( & mut self , msg : Sign , _ctx : & mut Self :: Context ) -> Self :: Result {
302
+ Box :: new ( self . crypto . send ( msg) . flatten ( ) )
303
+ }
304
+ }
305
+
306
+ impl Handler < GetPkh > for SignatureManagerAdapter {
307
+ type Result = ResponseFuture < PublicKeyHash , failure:: Error > ;
308
+
309
+ fn handle ( & mut self , msg : GetPkh , _ctx : & mut Self :: Context ) -> Self :: Result {
310
+ Box :: new ( self . crypto . send ( msg) . flatten ( ) )
311
+ }
312
+ }
313
+
314
+ impl Handler < GetPublicKey > for SignatureManagerAdapter {
315
+ type Result = ResponseFuture < PublicKey , failure:: Error > ;
316
+
317
+ fn handle ( & mut self , msg : GetPublicKey , _ctx : & mut Self :: Context ) -> Self :: Result {
318
+ Box :: new ( self . crypto . send ( msg) . flatten ( ) )
319
+ }
320
+ }
321
+
322
+ impl Handler < VrfProve > for SignatureManagerAdapter {
323
+ type Result = ResponseFuture < ( VrfProof , Hash ) , failure:: Error > ;
324
+
325
+ fn handle ( & mut self , msg : VrfProve , _ctx : & mut Self :: Context ) -> Self :: Result {
326
+ Box :: new ( self . crypto . send ( msg) . flatten ( ) )
327
+ }
328
+ }
0 commit comments