use ffi::crypto;
use ffi::{ResponseEmptyCB,
ResponseStringCB,
ResponseSliceCB,
ResponseBoolCB,
ResponseStringSliceCB};
use futures::Future;
use std::ffi::CString;
use std::ptr::null;
use {ErrorCode, IndyHandle, IndyError};
use utils::callbacks::{ClosureHandler, ResultHandler};
pub fn create_key(wallet_handle: IndyHandle, my_key_json: Option<&str>) -> Box<Future<Item=String, Error=IndyError>> {
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) })
}
pub fn set_key_metadata(wallet_handle: IndyHandle, verkey: &str, metadata: &str) -> Box<Future<Item=(), Error=IndyError>> {
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) })
}
pub fn get_key_metadata(wallet_handle: IndyHandle, verkey: &str) -> Box<Future<Item=String, Error=IndyError>> {
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) })
}
pub fn sign(wallet_handle: IndyHandle, signer_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
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)
})
}
pub fn verify(signer_vk: &str, message: &[u8], signature: &[u8]) -> Box<Future<Item=bool, Error=IndyError>> {
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)
})
}
pub fn auth_crypt(wallet_handle: IndyHandle, sender_vk: &str, recipient_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
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)
})
}
pub fn auth_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Box<Future<Item=(String, Vec<u8>), Error=IndyError>> {
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)
})
}
pub fn anon_crypt(recipient_vk: &str, message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
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)
})
}
pub fn anon_decrypt(wallet_handle: IndyHandle, recipient_vk: &str, encrypted_message: &[u8]) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
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)
})
}
pub fn pack_message(wallet_handle: IndyHandle, message: &[u8], receiver_keys: &str, sender: Option<&str>) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
let err= _pack_message(command_handle, wallet_handle, message, receiver_keys, sender, cb);
ResultHandler::slice(command_handle, err, receiver)
}
fn _pack_message(command_handle: IndyHandle, wallet_handle: IndyHandle, message: &[u8], receiver_keys: &str, sender: Option<&str>, cb: Option<ResponseSliceCB>) -> ErrorCode {
let receiver_keys = c_str!(receiver_keys);
let sender_str = opt_c_str!(sender);
ErrorCode::from(unsafe {
crypto::indy_pack_message(command_handle,
wallet_handle,
message.as_ptr() as *const u8,
message.len() as u32,
receiver_keys.as_ptr(),
opt_c_ptr!(sender, sender_str),
cb)
})
}
pub fn unpack_message(wallet_handle: IndyHandle, jwe: &[u8]) -> Box<Future<Item=Vec<u8>, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_slice();
let err= _unpack_message(command_handle, wallet_handle, jwe, cb);
ResultHandler::slice(command_handle, err, receiver)
}
fn _unpack_message(command_handle: IndyHandle, wallet_handle: IndyHandle, jwe: &[u8], cb: Option<ResponseSliceCB>) -> ErrorCode {
ErrorCode::from(unsafe {
crypto::indy_unpack_message(command_handle,
wallet_handle,
jwe.as_ptr() as *const u8,
jwe.len() as u32,
cb)
})
}