1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use ffi::crypto;
use ffi::{ResponseEmptyCB,
          ResponseStringCB,
          ResponseSliceCB,
          ResponseBoolCB,
          ResponseStringSliceCB};

use futures::Future;

use std::ffi::CString;

use {ErrorCode, IndyHandle};
use utils::callbacks::{ClosureHandler, ResultHandler};

/// Creates key pair in wallet
/// # Arguments
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `my_key_json` - Optional key information as json. If none then defaults are used.
///
/// # Example
/// my_key_json
/// {
///     "seed": string, (optional) Seed that allows deterministic key creation (if not set random one will be created).
///                                Can be UTF-8, base64 or hex string.
///     "crypto_type": string, // Optional (if not set then ed25519 curve is used); Currently only 'ed25519' value is supported for this field.
/// }
/// # Returns
/// verkey of generated key pair, also used as key identifier
pub fn create_key(wallet_handle: IndyHandle, my_key_json: Option<&str>) -> Box<Future<Item=String, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();

    let err = _create_key(command_handle, wallet_handle, my_key_json, cb);

    ResultHandler::str(command_handle, err, receiver)
}

fn _create_key(command_handle: IndyHandle, wallet_handle: IndyHandle, my_key_json: Option<&str>, cb: Option<ResponseStringCB>) -> ErrorCode {
    let my_key_json = opt_c_str_json!(my_key_json);

    ErrorCode::from(unsafe { crypto::indy_create_key(command_handle, wallet_handle, my_key_json.as_ptr(), cb) })
}

/// Saves/replaces the metadata for the `verkey` in the wallet
/// # Arguments
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `verkey` - the public key or key id where to store the metadata
/// * `metadata` - the metadata that will be stored with the key, can be empty string
pub fn set_key_metadata(wallet_handle: IndyHandle, verkey: &str, metadata: &str) -> Box<Future<Item=(), Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

    let err = _set_key_metadata(command_handle, wallet_handle, verkey, metadata, cb);

    ResultHandler::empty(command_handle, err, receiver)
}

fn _set_key_metadata(command_handle: IndyHandle, wallet_handle: IndyHandle, verkey: &str, metadata: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
    let verkey = c_str!(verkey);
    let metadata = c_str!(metadata);

    ErrorCode::from(unsafe { crypto::indy_set_key_metadata(command_handle, wallet_handle, verkey.as_ptr(), metadata.as_ptr(), cb) })
}

/// Retrieves the metadata for the `verkey` in the wallet
/// # Argument
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `verkey` - the public key or key id to retrieve metadata
/// # Returns
/// metadata currently stored with the key; Can be empty if no metadata was saved for this key
pub fn get_key_metadata(wallet_handle: IndyHandle, verkey: &str) -> Box<Future<Item=String, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();

    let err = _get_key_metadata(command_handle, wallet_handle, verkey, cb);

    ResultHandler::str(command_handle, err, receiver)
}

fn _get_key_metadata(command_handle: IndyHandle, wallet_handle: IndyHandle, verkey: &str, cb: Option<ResponseStringCB>) -> ErrorCode {
    let verkey = c_str!(verkey);

    ErrorCode::from(unsafe { crypto::indy_get_key_metadata(command_handle, wallet_handle, verkey.as_ptr(), cb) })
}

/// Signs a message with a key
/// # Arguments
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `signer_vk` - key id or verkey of my key. The key must be created by calling create_key or Did::new
/// * `message` - the data to be signed
/// # Returns
/// the signature
pub fn sign(wallet_handle: IndyHandle, signer_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();

    let err = _sign(command_handle, wallet_handle, signer_vk, message, cb);

    ResultHandler::slice(command_handle, err, receiver)
}

fn _sign(command_handle: IndyHandle, wallet_handle: IndyHandle, signer_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
    let signer_vk = c_str!(signer_vk);
    ErrorCode::from(unsafe {
        crypto::indy_crypto_sign(command_handle, wallet_handle, signer_vk.as_ptr(),
                         message.as_ptr() as *const u8,
                         message.len() as u32,
                         cb)
    })
}

/// Verify a signature with a verkey
/// # Arguments
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `signer_vk` - key id or verkey of my key. The key must be created by calling create_key or Did::new
/// * `message` - the data that was signed
/// * `signature` - the signature to verify
/// # Returns
/// true if signature is valid, false otherwise
pub fn verify(signer_vk: &str, message: &[u8], signature: &[u8]) -> Box<Future<Item=bool, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_bool();

    let err = _verify(command_handle, signer_vk, message, signature, cb);

    ResultHandler::bool(command_handle, err, receiver)
}

fn _verify(command_handle: IndyHandle, signer_vk: &str, message: &[u8], signature: &[u8], cb: Option<ResponseBoolCB>) -> ErrorCode {
    let signer_vk = c_str!(signer_vk);

    ErrorCode::from(unsafe {
        crypto::indy_crypto_verify(command_handle, signer_vk.as_ptr(),
                           message.as_ptr() as *const u8, message.len() as u32,
                           signature.as_ptr() as *const u8, signature.len() as u32, cb)
    })
}

/// Encrypt a message by authenticated-encryption scheme.
///
/// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
/// Using Recipient's public key, Sender can compute a shared secret key.
/// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
/// That shared secret key can be used to verify that the encrypted message was not tampered with,
/// before eventually decrypting it.
///
/// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
/// for specific DID.
/// # Arguments
/// * `wallet_handle` - wallet handle (created by Wallet::open)
/// * `signer_vk` - key id or verkey of my key. The key must be created by calling create_key or Did::new
/// * `recipient_vk` - key id or verkey of the other party's key
/// * `message` - the data to be encrypted
/// # Returns
/// the encrypted message
pub fn auth_crypt(wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();

    let err = _auth_crypt(command_handle, wallet_handle, sender_vk, recipient_vk, message, cb);

    ResultHandler::slice(command_handle, err, receiver)
}

fn _auth_crypt(command_handle: IndyHandle, wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
    let sender_vk = c_str!(sender_vk);
    let recipient_vk = c_str!(recipient_vk);
    ErrorCode::from(unsafe {
        crypto::indy_crypto_auth_crypt(command_handle, wallet_handle,
                               sender_vk.as_ptr(),
                                recipient_vk.as_ptr(),
                                message.as_ptr() as *const u8,
                                message.len() as u32, cb)
    })
}

/// Decrypt a message by authenticated-encryption scheme.
///
/// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
/// Using Recipient's public key, Sender can compute a shared secret key.
/// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
/// That shared secret key can be used to verify that the encrypted message was not tampered with,
/// before eventually decrypting it.
///
/// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
/// for specific DID.
///
/// # Arguments
/// * `wallet_handle`: wallet handle (created by Wallet::open)
/// * `recipient_vk`: key id or verkey of my key. The key must be created by calling create_key or Did::new
/// * `encrypted_message`: the message to be decrypted
/// # Returns
/// sender's verkey and decrypted message
pub fn auth_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Box<Future<Item=(String, Vec<u8>), Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string_slice();

    let err = _auth_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);

    ResultHandler::str_slice(command_handle, err, receiver)
}

fn _auth_decrypt(command_handle: IndyHandle, wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], cb: Option<ResponseStringSliceCB>) -> ErrorCode {
    let recipient_vk = c_str!(recipient_vk);
    ErrorCode::from(unsafe {
        crypto::indy_crypto_auth_decrypt(command_handle,
                                 wallet_handle,
                                 recipient_vk.as_ptr(),
                                 encrypted_message.as_ptr() as *const u8,
                                 encrypted_message.len() as u32, cb)
    })
}

/// Encrypts a message by anonymous-encryption scheme.
///
/// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
/// Only the Recipient can decrypt these messages, using its private key.
/// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
///
/// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
/// for specific DID.
///
/// # Arguments
/// * `wallet_handle`: wallet handle (created by Wallet::open)
/// * `recipient_vk`: verkey of message recipient
/// * `message`: a pointer to first byte of message that to be encrypted
///
/// # Returns
/// the encrypted message
pub fn anon_crypt(recipient_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();

    let err = _anon_crypt(command_handle, recipient_vk, message, cb);

    ResultHandler::slice(command_handle, err, receiver)
}

fn _anon_crypt(command_handle: IndyHandle, recipient_vk: &str, message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
    let recipient_vk = c_str!(recipient_vk);
    ErrorCode::from(unsafe {
        crypto::indy_crypto_anon_crypt(command_handle,
                               recipient_vk.as_ptr(),
                               message.as_ptr() as *const u8,
                                message.len() as u32,
                                cb)
    })
}

/// Decrypts a message by anonymous-encryption scheme.
///
/// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
/// Only the Recipient can decrypt these messages, using its private key.
/// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
///
/// Note to use DID keys with this function you can call Did::get_ver_key to get key id (verkey)
/// for specific DID.
///
/// # Arguments
/// * `wallet_handle`: wallet handle (created by Wallet::open).
/// * `recipient_vk`: key id or verkey of my key. The key must be created by calling create_key or Did::new
/// * `encrypted_message`: a pointer to first byte of message that to be decrypted
///
/// # Returns
/// decrypted message
pub fn anon_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=ErrorCode>> {
    let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();

    let err = _anon_decrypt(command_handle, wallet_handle, recipient_vk, encrypted_message, cb);

    ResultHandler::slice(command_handle, err, receiver)
}

fn _anon_decrypt(command_handle: IndyHandle, wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
    let recipient_vk = c_str!(recipient_vk);
    ErrorCode::from(unsafe {
        crypto::indy_crypto_anon_decrypt(command_handle,
                                 wallet_handle,
                                 recipient_vk.as_ptr(),
                                 encrypted_message.as_ptr() as *const u8,
                                 encrypted_message.len() as u32, cb)
    })
}