rust_libindy_wrapper/
crypto.rs

1use {ErrorCode, IndyHandle};
2
3use std::ffi::CString;
4use std::time::Duration;
5
6use native::crypto;
7use native::{ResponseEmptyCB,
8          ResponseStringCB,
9          ResponseSliceCB,
10          ResponseBoolCB,
11          ResponseStringSliceCB};
12
13use utils::results::ResultHandler;
14use utils::callbacks::ClosureHandler;
15
16pub struct Key {}
17
18impl Key {
19    /// Creates key pair in wallet
20    /// # Arguments
21    /// * `wallet_handle` - wallet handle (created by Wallet::open)
22    /// * `my_key_json` - Optional key information as json. If none then defaults are used.
23    ///
24    /// # Example
25    /// my_key_json
26    /// {
27    ///     "seed": string, // Optional (if not set random one will be used); Seed information that allows deterministic key creation.
28    ///     "crypto_type": string, // Optional (if not set then ed25519 curve is used); Currently only 'ed25519' value is supported for this field.
29    /// }
30    /// # Returns
31    /// verkey of generated key pair, also used as key identifier
32    pub fn create(wallet_handle: IndyHandle, my_key_json: Option<&str>) -> Result<String, ErrorCode> {
33        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
34
35        let err = Key::_create(command_handle, wallet_handle, my_key_json, cb);
36
37        ResultHandler::one(err, receiver)
38    }
39
40    /// Creates key pair in wallet
41    /// # Arguments
42    /// * `wallet_handle` - wallet handle (created by Wallet::open)
43    /// * `my_key_json` - key information as json
44    /// * `timeout` - the maximum time this function waits for a response
45    ///
46    /// # Example
47    /// my_key_json
48    /// {
49    ///     "seed": string, // Optional (if not set random one will be used); Seed information that allows deterministic key creation.
50    ///     "crypto_type": string, // Optional (if not set then ed25519 curve is used); Currently only 'ed25519' value is supported for this field.
51    /// }
52    /// # Returns
53    /// verkey of generated key pair, also used as key identifier
54    pub fn create_timeout(wallet_handle: IndyHandle, my_key_json: Option<&str>, timeout: Duration) -> Result<String, ErrorCode> {
55        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
56
57        let err = Key::_create(command_handle, wallet_handle, my_key_json, cb);
58
59        ResultHandler::one_timeout(err, receiver, timeout)
60    }
61
62    /// Creates key pair in wallet
63    /// # Arguments
64    /// * `wallet_handle` - wallet handle (created by Wallet::open)
65    /// * `my_key_json` - Optional key information as json. If none then defaults are used.
66    /// * `closure` - The closure that is called when finished
67    ///
68    /// # Example
69    /// my_key_json
70    /// {
71    ///     "seed": string, // Optional (if not set random one will be used); Seed information that allows deterministic key creation.
72    ///     "crypto_type": string, // Optional (if not set then ed25519 curve is used); Currently only 'ed25519' value is supported for this field.
73    /// }
74    /// # Returns
75    /// errorcode from calling ffi function. The closure receives the return result
76    pub fn create_async<F: 'static>(wallet_handle: IndyHandle, my_key_json: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
77        let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));
78
79        Key::_create(command_handle, wallet_handle, my_key_json, cb)
80    }
81
82    fn _create(command_handle: IndyHandle, wallet_handle: IndyHandle, my_key_json: Option<&str>, cb: Option<ResponseStringCB>) -> ErrorCode {
83        let my_key_json = opt_c_str_json!(my_key_json);
84
85        ErrorCode::from(unsafe { crypto::indy_create_key(command_handle, wallet_handle, my_key_json.as_ptr(), cb) })
86    }
87
88    /// Saves/replaces the metadata for the `verkey` in the wallet
89    /// # Arguments
90    /// * `wallet_handle` - wallet handle (created by Wallet::open)
91    /// * `verkey` - the public key or key id where to store the metadata
92    /// * `metadata` - the metadata that will be stored with the key, can be empty string
93    pub fn set_metadata(wallet_handle: IndyHandle, verkey: &str, metadata: &str) -> Result<(), ErrorCode> {
94        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
95
96        let err = Key::_set_metadata(command_handle, wallet_handle, verkey, metadata, cb);
97
98        ResultHandler::empty(err, receiver)
99    }
100
101    /// Saves/replaces the metadata for the `verkey` in the wallet
102    /// # Arguments
103    /// * `wallet_handle` - wallet handle (created by Wallet::open)
104    /// * `verkey` - the public key or key id where to store the metadata
105    /// * `metadata` - the metadata that will be stored with the key, can be empty string
106    /// * `timeout` - the maximum time this function waits for a response
107    pub fn set_metadata_timeout(wallet_handle: IndyHandle, verkey: &str, metadata: &str, timeout: Duration) -> Result<(), ErrorCode> {
108        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
109
110        let err = Key::_set_metadata(command_handle, wallet_handle, verkey, metadata, cb);
111
112        ResultHandler::empty_timeout(err, receiver, timeout)
113    }
114
115    /// Saves/replaces the metadata for the `verkey` in the wallet
116    /// # Arguments
117    /// * `wallet_handle` - wallet handle (created by Wallet::open)
118    /// * `verkey` - the public key or key id where to store the metadata
119    /// * `metadata` - the metadata that will be stored with the key, can be empty string
120    /// * `closure` - The closure that is called when finished
121    pub fn set_metadata_async<F: 'static>(wallet_handle: IndyHandle, verkey: &str, metadata: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
122        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
123
124        Key::_set_metadata(command_handle, wallet_handle, verkey, metadata, cb)
125    }
126
127    fn _set_metadata(command_handle: IndyHandle, wallet_handle: IndyHandle, verkey: &str, metadata: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
128        let verkey = c_str!(verkey);
129        let metadata = c_str!(metadata);
130
131        ErrorCode::from(unsafe { crypto::indy_set_key_metadata(command_handle, wallet_handle, verkey.as_ptr(), metadata.as_ptr(), cb) })
132    }
133
134    /// Retrieves the metadata for the `verkey` in the wallet
135    /// # Argument
136    /// * `wallet_handle` - wallet handle (created by Wallet::open)
137    /// * `verkey` - the public key or key id to retrieve metadata
138    /// # Returns
139    /// metadata currently stored with the key; Can be empty if no metadata was saved for this key
140    pub fn get_metadata(wallet_handle: IndyHandle, verkey: &str) -> Result<String, ErrorCode> {
141        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
142
143        let err = Key::_get_metadata(command_handle, wallet_handle, verkey, cb);
144
145        ResultHandler::one(err, receiver)
146    }
147
148    /// Retrieves the metadata for the `verkey` in the wallet
149    /// # Argument
150    /// * `wallet_handle` - wallet handle (created by Wallet::open)
151    /// * `verkey` - the public key or key id to retrieve metadata
152    /// * `timeout` - the maximum time this function waits for a response
153    /// # Returns
154    /// metadata currently stored with the key; Can be empty if no metadata was saved for this key
155    pub fn get_metadata_timeout(wallet_handle: IndyHandle, verkey: &str, timeout: Duration) -> Result<String, ErrorCode> {
156        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
157
158        let err = Key::_get_metadata(command_handle, wallet_handle, verkey, cb);
159
160        ResultHandler::one_timeout(err, receiver, timeout)
161    }
162
163    /// Retrieves the metadata for the `verkey` in the wallet
164    /// # Argument
165    /// * `wallet_handle` - wallet handle (created by Wallet::open)
166    /// * `verkey` - the public key or key id to retrieve metadata
167    /// * `closure` - The closure that is called when finished
168    /// # Returns
169    /// errorcode from calling ffi function
170    pub fn get_metadata_async<F: 'static>(wallet_handle: IndyHandle, verkey: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
171        let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));
172
173        Key::_get_metadata(command_handle, wallet_handle, verkey, cb)
174    }
175
176    fn _get_metadata(command_handle: IndyHandle, wallet_handle: IndyHandle, verkey: &str, cb: Option<ResponseStringCB>) -> ErrorCode {
177        let verkey = c_str!(verkey);
178
179        ErrorCode::from(unsafe { crypto::indy_get_key_metadata(command_handle, wallet_handle, verkey.as_ptr(), cb) })
180    }
181}
182
183pub struct Crypto {}
184
185impl Crypto {
186    /// Signs a message with a key
187    /// # Arguments
188    /// * `wallet_handle` - wallet handle (created by Wallet::open)
189    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
190    /// * `message` - the data to be signed
191    /// # Returns
192    /// the signature
193    pub fn sign(wallet_handle: IndyHandle, signer_vk: &str, message: &[u8]) -> Result<Vec<u8>, ErrorCode> {
194        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
195
196        let err = Crypto::_sign(command_handle, wallet_handle, signer_vk, message, cb);
197
198        ResultHandler::one(err, receiver)
199    }
200
201    /// Signs a message with a key
202    /// # Arguments
203    /// * `wallet_handle` - wallet handle (created by Wallet::open)
204    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
205    /// * `message` - the data to be signed
206    /// * `timeout` - the maximum time this function waits for a response
207    /// # Returns
208    /// the signature
209    pub fn sign_timeout(wallet_handle: IndyHandle, signer_vk: &str, message: &[u8], timeout: Duration) -> Result<Vec<u8>, ErrorCode> {
210        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
211
212        let err = Crypto::_sign(command_handle, wallet_handle, signer_vk, message, cb);
213
214        ResultHandler::one_timeout(err, receiver, timeout)
215    }
216
217    /// Signs a message with a key
218    /// # Arguments
219    /// * `wallet_handle` - wallet handle (created by Wallet::open)
220    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
221    /// * `message` - the data to be signed
222    /// * `closure` - The closure that is called when finished
223    /// # Returns
224    /// errorcode from calling ffi function
225    pub fn sign_async<F: 'static>(wallet_handle: IndyHandle, signer_vk: &str, message: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, Vec<u8>) + Send {
226        let (command_handle, cb) = ClosureHandler::convert_cb_ec_slice(Box::new(closure));
227
228        Crypto::_sign(command_handle, wallet_handle, signer_vk, message, cb)
229    }
230
231    fn _sign(command_handle: IndyHandle, wallet_handle: IndyHandle, signer_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
232        let signer_vk = c_str!(signer_vk);
233        ErrorCode::from(unsafe {
234            crypto::indy_crypto_sign(command_handle, wallet_handle, signer_vk.as_ptr(),
235                             message.as_ptr() as *const u8,
236                             message.len() as u32,
237                             cb)
238        })
239    }
240
241    /// Verify a signature with a verkey
242    /// # Arguments
243    /// * `wallet_handle` - wallet handle (created by Wallet::open)
244    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
245    /// * `message` - the data that was signed
246    /// * `signature` - the signature to verify
247    /// # Returns
248    /// true if signature is valid, false otherwise
249    pub fn verify(signer_vk: &str, message: &[u8], signature: &[u8]) -> Result<bool, ErrorCode> {
250        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_bool();
251
252        let err = Crypto::_verify(command_handle, signer_vk, message, signature, cb);
253
254        ResultHandler::one(err, receiver)
255    }
256
257     /// Verify a signature with a verkey
258    /// # Arguments
259    /// * `wallet_handle` - wallet handle (created by Wallet::open)
260    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
261    /// * `message` - the data that was signed
262    /// * `signature` - the signature to verify
263    /// * `timeout` - the maximum time this function waits for a response
264    /// # Returns
265    /// true if signature is valid, false otherwise
266    pub fn verify_timeout(signer_vk: &str, message: &[u8], signature: &[u8], timeout: Duration) -> Result<bool, ErrorCode> {
267        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_bool();
268
269        let err = Crypto::_verify(command_handle, signer_vk, message, signature, cb);
270
271        ResultHandler::one_timeout(err, receiver, timeout)
272    }
273
274    /// Verify a signature with a verkey
275    /// # Arguments
276    /// * `wallet_handle` - wallet handle (created by Wallet::open)
277    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
278    /// * `message` - the data that was signed
279    /// * `signature` - the signature to verify
280    /// * `closure` - The closure that is called when finished
281    /// # Returns
282    /// errorcode from calling ffi function
283    pub fn verify_async<F: 'static>(signer_vk: &str, message: &[u8], signature: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, bool) + Send {
284        let (command_handle, cb) = ClosureHandler::convert_cb_ec_bool(Box::new(closure));
285
286        Crypto::_verify(command_handle, signer_vk, message, signature, cb)
287    }
288
289    fn _verify(command_handle: IndyHandle, signer_vk: &str, message: &[u8], signature: &[u8], cb: Option<ResponseBoolCB>) -> ErrorCode {
290        let signer_vk = c_str!(signer_vk);
291
292        ErrorCode::from(unsafe {
293            crypto::indy_crypto_verify(command_handle, signer_vk.as_ptr(),
294                               message.as_ptr() as *const u8, message.len() as u32,
295                               signature.as_ptr() as *const u8, signature.len() as u32, cb)
296        })
297    }
298
299    /// Encrypt a message by authenticated-encryption scheme.
300    ///
301    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
302    /// Using Recipient's public key, Sender can compute a shared secret key.
303    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
304    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
305    /// before eventually decrypting it.
306    ///
307    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
308    /// for specific DID.
309    /// # Arguments
310    /// * `wallet_handle` - wallet handle (created by Wallet::open)
311    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
312    /// * `recipient_vk` - key id or verkey of the other party's key
313    /// * `message` - the data to be encrypted
314    /// # Returns
315    /// the encrypted message
316    pub fn auth_crypt(wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8]) -> Result<Vec<u8>, ErrorCode> {
317        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
318
319        let err = Crypto::_auth_crypt(command_handle, wallet_handle, sender_vk, recipient_vk, message, cb);
320
321        ResultHandler::one(err, receiver)
322    }
323
324    /// Encrypt a message by authenticated-encryption scheme.
325    ///
326    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
327    /// Using Recipient's public key, Sender can compute a shared secret key.
328    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
329    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
330    /// before eventually decrypting it.
331    ///
332    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
333    /// for specific DID.
334    /// # Arguments
335    /// * `wallet_handle` - wallet handle (created by Wallet::open)
336    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
337    /// * `recipient_vk` - key id or verkey of the other party's key
338    /// * `message` - the data to be encrypted
339    /// * `timeout` - the maximum time this function waits for a response
340    /// # Returns
341    /// the encrypted message
342    pub fn auth_crypt_timeout(wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8], timeout: Duration) -> Result<Vec<u8>, ErrorCode> {
343        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
344
345        let err = Crypto::_auth_crypt(command_handle, wallet_handle, sender_vk, recipient_vk, message, cb);
346
347        ResultHandler::one_timeout(err, receiver, timeout)
348    }
349
350    /// Encrypt a message by authenticated-encryption scheme.
351    ///
352    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
353    /// Using Recipient's public key, Sender can compute a shared secret key.
354    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
355    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
356    /// before eventually decrypting it.
357    ///
358    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
359    /// for specific DID.
360    /// # Arguments
361    /// * `wallet_handle` - wallet handle (created by Wallet::open)
362    /// * `signer_vk` - key id or verkey of my key. The key must be created by calling Key::create or Did::new
363    /// * `recipient_vk` - key id or verkey of the other party's key
364    /// * `message` - the data to be encrypted
365    /// * `closure` - The closure that is called when finished
366    /// # Returns
367    /// errorcode from calling ffi function
368    pub fn auth_crypt_async<F: 'static>(wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, Vec<u8>) + Send {
369        let (command_handle, cb) = ClosureHandler::convert_cb_ec_slice(Box::new(closure));
370
371        Crypto::_auth_crypt(command_handle, wallet_handle, sender_vk, recipient_vk, message, cb)
372    }
373
374    fn _auth_crypt(command_handle: IndyHandle, wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
375        let sender_vk = c_str!(sender_vk);
376        let recipient_vk = c_str!(recipient_vk);
377        ErrorCode::from(unsafe {
378            crypto::indy_crypto_auth_crypt(command_handle, wallet_handle,
379                                   sender_vk.as_ptr(),
380                                    recipient_vk.as_ptr(),
381                                    message.as_ptr() as *const u8,
382                                    message.len() as u32, cb)
383        })
384    }
385
386    /// Decrypt a message by authenticated-encryption scheme.
387    ///
388    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
389    /// Using Recipient's public key, Sender can compute a shared secret key.
390    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
391    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
392    /// before eventually decrypting it.
393    ///
394    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
395    /// for specific DID.
396    ///
397    /// # Arguments
398    /// * `wallet_handle`: wallet handle (created by Wallet::open)
399    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
400    /// * `encrypted_message`: the message to be decrypted
401    /// # Returns
402    /// sender's verkey and decrypted message
403    pub fn auth_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Result<(String, Vec<u8>), ErrorCode> {
404        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string_slice();
405
406        let err = Crypto::_auth_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);
407
408        ResultHandler::two(err, receiver)
409    }
410
411    /// Decrypt a message by authenticated-encryption scheme.
412    ///
413    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
414    /// Using Recipient's public key, Sender can compute a shared secret key.
415    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
416    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
417    /// before eventually decrypting it.
418    ///
419    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
420    /// for specific DID.
421    ///
422    /// # Arguments
423    /// * `wallet_handle`: wallet handle (created by Wallet::open)
424    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
425    /// * `encrypted_message`: the message to be decrypted
426    /// * `timeout` - the maximum time this function waits for a response
427    /// # Returns
428    /// sender's verkey and decrypted message
429    pub fn auth_decrypt_timeout(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], timeout: Duration) -> Result<(String, Vec<u8>), ErrorCode> {
430        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string_slice();
431
432        let err = Crypto::_auth_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);
433
434        ResultHandler::two_timeout(err, receiver, timeout)
435    }
436
437    /// Decrypt a message by authenticated-encryption scheme.
438    ///
439    /// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
440    /// Using Recipient's public key, Sender can compute a shared secret key.
441    /// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
442    /// That shared secret key can be used to verify that the encrypted message was not tampered with,
443    /// before eventually decrypting it.
444    ///
445    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
446    /// for specific DID.
447    ///
448    /// # Arguments
449    /// * `wallet_handle`: wallet handle (created by Wallet::open)
450    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
451    /// * `encrypted_message`: the message to be decrypted
452    /// * `closure` - The closure that is called when finished
453    /// # Returns
454    /// errorcode from calling ffi function
455    pub fn auth_decrypt_async<F: 'static>(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, String, Vec<u8>) + Send {
456        let (command_handle, cb) = ClosureHandler::convert_cb_ec_string_slice(Box::new(closure));
457
458        Crypto::_auth_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb)
459    }
460
461    fn _auth_decrypt(command_handle: IndyHandle, wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], cb: Option<ResponseStringSliceCB>) -> ErrorCode {
462        let recipient_vk = c_str!(recipient_vk);
463        ErrorCode::from(unsafe {
464            crypto::indy_crypto_auth_decrypt(command_handle,
465                                     wallet_handle,
466                                     recipient_vk.as_ptr(),
467                                     encrypted_message.as_ptr() as *const u8,
468                                     encrypted_message.len() as u32, cb)
469        })
470    }
471
472    /// Encrypts a message by anonymous-encryption scheme.
473    ///
474    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
475    /// Only the Recipient can decrypt these messages, using its private key.
476    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
477    ///
478    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
479    /// for specific DID.
480    ///
481    /// # Arguments
482    /// * `wallet_handle`: wallet handle (created by Wallet::open)
483    /// * `recipient_vk`: verkey of message recipient
484    /// * `message`: a pointer to first byte of message that to be encrypted
485    ///
486    /// # Returns
487    /// the encrypted message
488    pub fn anon_crypt(recipient_vk: &str, message: &[u8]) -> Result<Vec<u8>, ErrorCode> {
489        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
490
491        let err = Crypto::_anon_crypt(command_handle, recipient_vk, message, cb);
492
493        ResultHandler::one(err, receiver)
494    }
495
496    /// Encrypts a message by anonymous-encryption scheme.
497    ///
498    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
499    /// Only the Recipient can decrypt these messages, using its private key.
500    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
501    ///
502    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
503    /// for specific DID.
504    ///
505    /// # Arguments
506    /// * `wallet_handle`: wallet handle (created by Wallet::open)
507    /// * `recipient_vk`: verkey of message recipient
508    /// * `message`: a pointer to first byte of message that to be encrypted
509    /// * `timeout` - the maximum time this function waits for a response
510    /// # Returns
511    /// the encrypted message
512    pub fn anon_crypt_timeout(recipient_vk: &str, message: &[u8], timeout: Duration) -> Result<Vec<u8>, ErrorCode> {
513        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
514
515        let err = Crypto::_anon_crypt(command_handle, recipient_vk, message, cb);
516
517        ResultHandler::one_timeout(err, receiver, timeout)
518    }
519
520    /// Encrypts a message by anonymous-encryption scheme.
521    ///
522    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
523    /// Only the Recipient can decrypt these messages, using its private key.
524    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
525    ///
526    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
527    /// for specific DID.
528    ///
529    /// # Arguments
530    /// * `wallet_handle`: wallet handle (created by Wallet::open)
531    /// * `recipient_vk`: verkey of message recipient
532    /// * `message`: a pointer to first byte of message that to be encrypted
533    /// * `closure` - The closure that is called when finished
534    /// # Returns
535    /// errorcode from calling ffi function
536    pub fn anon_crypt_async<F: 'static>(recipient_vk: &str, message: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, Vec<u8>) + Send {
537        let (command_handle, cb) = ClosureHandler::convert_cb_ec_slice(Box::new(closure));
538
539        Crypto::_anon_crypt(command_handle, recipient_vk, message, cb)
540    }
541
542    fn _anon_crypt(command_handle: IndyHandle, recipient_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
543        let recipient_vk = c_str!(recipient_vk);
544        ErrorCode::from(unsafe {
545            crypto::indy_crypto_anon_crypt(command_handle,
546                                   recipient_vk.as_ptr(),
547                                   message.as_ptr() as *const u8,
548                                    message.len() as u32,
549                                    cb)
550        })
551    }
552
553    /// Decrypts a message by anonymous-encryption scheme.
554    ///
555    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
556    /// Only the Recipient can decrypt these messages, using its private key.
557    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
558    ///
559    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
560    /// for specific DID.
561    ///
562    /// # Arguments
563    /// * `wallet_handle`: wallet handle (created by Wallet::open).
564    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
565    /// * `encrypted_message`: a pointer to first byte of message that to be decrypted
566    ///
567    /// # Returns
568    /// decrypted message
569    pub fn anon_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Result<Vec<u8>, ErrorCode> {
570        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
571
572        let err = Crypto::_anon_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);
573
574        ResultHandler::one(err, receiver)
575    }
576
577    /// Decrypts a message by anonymous-encryption scheme.
578    ///
579    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
580    /// Only the Recipient can decrypt these messages, using its private key.
581    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
582    ///
583    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
584    /// for specific DID.
585    ///
586    /// # Arguments
587    /// * `wallet_handle`: wallet handle (created by Wallet::open).
588    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
589    /// * `encrypted_message`: a pointer to first byte of message that to be decrypted
590    /// * `timeout` - the maximum time this function waits for a response
591    /// # Returns
592    /// decrypted message
593    pub fn anon_decrypt_timeout(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], timeout: Duration) -> Result<Vec<u8>, ErrorCode> {
594        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
595
596        let err = Crypto::_anon_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);
597
598        ResultHandler::one_timeout(err, receiver, timeout)
599    }
600
601    /// Decrypts a message by anonymous-encryption scheme.
602    ///
603    /// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
604    /// Only the Recipient can decrypt these messages, using its private key.
605    /// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
606    ///
607    /// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
608    /// for specific DID.
609    ///
610    /// # Arguments
611    /// * `wallet_handle`: wallet handle (created by Wallet::open).
612    /// * `recipient_vk`: key id or verkey of my key. The key must be created by calling Key::create or Did::new
613    /// * `encrypted_message`: a pointer to first byte of message that to be decrypted
614    /// * `closure` - The closure that is called when finished
615    /// # Returns
616    /// decrypted message
617    pub fn anon_decrypt_async<F: 'static>(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], closure: F) -> ErrorCode where F: FnMut(ErrorCode, Vec<u8>) + Send {
618        let (command_handle, cb) = ClosureHandler::convert_cb_ec_slice(Box::new(closure));
619
620        Crypto::_anon_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb)
621    }
622
623    fn _anon_decrypt(command_handle: IndyHandle, wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
624        let recipient_vk = c_str!(recipient_vk);
625        ErrorCode::from(unsafe {
626            crypto::indy_crypto_anon_decrypt(command_handle,
627                                     wallet_handle,
628                                     recipient_vk.as_ptr(),
629                                     encrypted_message.as_ptr() as *const u8,
630                                     encrypted_message.len() as u32, cb)
631        })
632    }
633}
634