use futures::Future;
use {ErrorCode, IndyError};
use std::ffi::CString;
use std::ptr::null;
use utils::callbacks::{ClosureHandler, ResultHandler};
use ffi::{wallet, non_secrets};
use ffi::{ResponseEmptyCB,
ResponseStringCB,
ResponseI32CB};
use {CommandHandle, WalletHandle, SearchHandle};
pub fn register_wallet_storage(xtype: &str,
create: Option<wallet::WalletCreate>,
open: Option<wallet::WalletOpen>,
close: Option<wallet::WalletClose>,
delete: Option<wallet::WalletDelete>,
add_record: Option<wallet::WalletAddRecord>,
update_record_value: Option<wallet::WalletUpdateRecordValue>,
update_record_tags: Option<wallet::WalletUpdateRecordTags>,
add_record_tags: Option<wallet::WalletAddRecordTags>,
delete_record_tags: Option<wallet::WalletDeleteRecordTags>,
delete_record: Option<wallet::WalletDeleteRecord>,
get_record: Option<wallet::WalletGetRecord>,
get_record_id: Option<wallet::WalletGetRecordId>,
get_record_type: Option<wallet::WalletGetRecordType>,
get_record_value: Option<wallet::WalletGetRecordValue>,
get_record_tags: Option<wallet::WalletGetRecordTags>,
free_record: Option<wallet::WalletFreeRecord>,
get_storage_metadata: Option<wallet::WalletGetStorageMetadata>,
set_storage_metadata: Option<wallet::WalletSetStorageMetadata>,
free_storage_metadata: Option<wallet::WalletFreeStorageMetadata>,
search_records: Option<wallet::WalletSearchRecords>,
search_all_records: Option<wallet::WalletSearchAllRecords>,
get_search_total_count: Option<wallet::WalletGetSearchTotalCount>,
fetch_search_next_record: Option<wallet::WalletFetchSearchNextRecord>,
free_search: Option<wallet::WalletFreeSearch>) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _register_storage(command_handle,
xtype,
create,
open,
close,
delete,
add_record,
update_record_value,
update_record_tags,
add_record_tags,
delete_record_tags,
delete_record,
get_record,
get_record_id,
get_record_type,
get_record_value,
get_record_tags,
free_record,
get_storage_metadata,
set_storage_metadata,
free_storage_metadata,
search_records,
search_all_records,
get_search_total_count,
fetch_search_next_record,
free_search,
cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _register_storage(command_handle: CommandHandle,
xtype: &str,
create: Option<wallet::WalletCreate>,
open: Option<wallet::WalletOpen>,
close: Option<wallet::WalletClose>,
delete: Option<wallet::WalletDelete>,
add_record: Option<wallet::WalletAddRecord>,
update_record_value: Option<wallet::WalletUpdateRecordValue>,
update_record_tags: Option<wallet::WalletUpdateRecordTags>,
add_record_tags: Option<wallet::WalletAddRecordTags>,
delete_record_tags: Option<wallet::WalletDeleteRecordTags>,
delete_record: Option<wallet::WalletDeleteRecord>,
get_record: Option<wallet::WalletGetRecord>,
get_record_id: Option<wallet::WalletGetRecordId>,
get_record_type: Option<wallet::WalletGetRecordType>,
get_record_value: Option<wallet::WalletGetRecordValue>,
get_record_tags: Option<wallet::WalletGetRecordTags>,
free_record: Option<wallet::WalletFreeRecord>,
get_storage_metadata: Option<wallet::WalletGetStorageMetadata>,
set_storage_metadata: Option<wallet::WalletSetStorageMetadata>,
free_storage_metadata: Option<wallet::WalletFreeStorageMetadata>,
search_records: Option<wallet::WalletSearchRecords>,
search_all_records: Option<wallet::WalletSearchAllRecords>,
get_search_total_count: Option<wallet::WalletGetSearchTotalCount>,
fetch_search_next_record: Option<wallet::WalletFetchSearchNextRecord>,
free_search: Option<wallet::WalletFreeSearch>,
cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
ErrorCode::from(unsafe {
wallet::indy_register_wallet_storage(command_handle,
xtype.as_ptr(),
create,
open,
close,
delete,
add_record,
update_record_value,
update_record_tags,
add_record_tags,
delete_record_tags,
delete_record,
get_record,
get_record_id,
get_record_type,
get_record_value,
get_record_tags,
free_record,
get_storage_metadata,
set_storage_metadata,
free_storage_metadata,
search_records,
search_all_records,
get_search_total_count,
fetch_search_next_record,
free_search,
cb)
})
}
pub fn create_wallet(config: &str, credentials: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _create_wallet(command_handle, config, credentials, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _create_wallet(command_handle: CommandHandle, config: &str, credentials: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let config = c_str!(config);
let credentials = c_str!(credentials);
ErrorCode::from(unsafe {
wallet::indy_create_wallet(command_handle, config.as_ptr(), credentials.as_ptr(), cb)
})
}
pub fn open_wallet(config: &str, credentials: &str) -> Box<dyn Future<Item=WalletHandle, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_handle();
let err = _open_wallet(command_handle, config, credentials, cb);
ResultHandler::handle(command_handle, err, receiver)
}
fn _open_wallet(command_handle: CommandHandle, config: &str, credentials: &str, cb: Option<ResponseI32CB>) -> ErrorCode {
let config = c_str!(config);
let credentials = c_str!(credentials);
ErrorCode::from(unsafe {
wallet::indy_open_wallet(command_handle, config.as_ptr(), credentials.as_ptr(), cb)
})
}
pub fn export_wallet(wallet_handle: WalletHandle, export_config: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _export_wallet(command_handle, wallet_handle, export_config, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _export_wallet(command_handle: CommandHandle, wallet_handle: WalletHandle, export_config: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let export_config = c_str!(export_config);
ErrorCode::from(unsafe {
wallet::indy_export_wallet(command_handle, wallet_handle, export_config.as_ptr(), cb)
})
}
pub fn import_wallet(config: &str, credentials: &str, import_config: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _import_wallet(command_handle, config, credentials, import_config, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _import_wallet(command_handle: CommandHandle, config: &str, credentials: &str, import_config: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let config = c_str!(config);
let credentials = c_str!(credentials);
let import_config = c_str!(import_config);
ErrorCode::from(unsafe {
wallet::indy_import_wallet(command_handle, config.as_ptr(), credentials.as_ptr(), import_config.as_ptr(), cb)
})
}
pub fn delete_wallet(config: &str, credentials: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _delete_wallet(command_handle, config, credentials, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _delete_wallet(command_handle: CommandHandle, config: &str, credentials: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let config = c_str!(config);
let credentials = c_str!(credentials);
ErrorCode::from(unsafe {
wallet::indy_delete_wallet(command_handle, config.as_ptr(), credentials.as_ptr(), cb)
})
}
pub fn close_wallet(wallet_handle: WalletHandle) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _close_wallet(command_handle, wallet_handle, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _close_wallet(command_handle: CommandHandle, wallet_handle: WalletHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
ErrorCode::from(unsafe { wallet::indy_close_wallet(command_handle, wallet_handle, cb) })
}
pub fn add_wallet_record(wallet_handle: WalletHandle, xtype: &str, id: &str, value: &str, tags_json: Option<&str>) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _add_wallet_record(command_handle, wallet_handle, xtype, id, value, tags_json, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _add_wallet_record(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, value: &str, tags_json: Option<&str>, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let value = c_str!(value);
let tags_json_str = opt_c_str!(tags_json);
ErrorCode::from(unsafe {
non_secrets::indy_add_wallet_record(command_handle,
wallet_handle,
xtype.as_ptr(),
id.as_ptr(),
value.as_ptr(),
opt_c_ptr!(tags_json, tags_json_str),
cb)
})
}
pub fn update_wallet_record_value(wallet_handle: WalletHandle, xtype: &str, id: &str, value: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _update_wallet_record_value(command_handle, wallet_handle, xtype, id, value, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _update_wallet_record_value(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, value: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let value = c_str!(value);
ErrorCode::from(unsafe{
non_secrets::indy_update_wallet_record_value(command_handle,
wallet_handle,
xtype.as_ptr(),
id.as_ptr(),
value.as_ptr(),
cb)
})
}
pub fn update_wallet_record_tags(wallet_handle: WalletHandle, xtype: &str, id: &str, tags_json: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _update_wallet_record_tags(command_handle, wallet_handle, xtype, id, tags_json, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _update_wallet_record_tags(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, tags_json: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let tags_json = c_str!(tags_json);
ErrorCode::from(unsafe {
non_secrets::indy_update_wallet_record_tags(command_handle, wallet_handle, xtype.as_ptr(), id.as_ptr(), tags_json.as_ptr(), cb)
})
}
pub fn add_wallet_record_tags(wallet_handle: WalletHandle, xtype: &str, id: &str, tags_json: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _add_wallet_record_tags(command_handle, wallet_handle, xtype, id, tags_json, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _add_wallet_record_tags(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, tags_json: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let tags_json = c_str!(tags_json);
ErrorCode::from(unsafe {
non_secrets::indy_add_wallet_record_tags(command_handle, wallet_handle, xtype.as_ptr(), id.as_ptr(), tags_json.as_ptr(), cb)
})
}
pub fn delete_wallet_record_tags(wallet_handle: WalletHandle, xtype: &str, id: &str, tag_names_json: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _delete_wallet_record_tags(command_handle, wallet_handle, xtype, id, tag_names_json, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _delete_wallet_record_tags(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, tag_names_json: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let tag_names_json = c_str!(tag_names_json);
ErrorCode::from(unsafe {
non_secrets::indy_delete_wallet_record_tags(command_handle, wallet_handle, xtype.as_ptr(), id.as_ptr(), tag_names_json.as_ptr(), cb)
})
}
pub fn delete_wallet_record(wallet_handle: WalletHandle, xtype: &str, id: &str) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _delete_wallet_record(command_handle, wallet_handle, xtype, id, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _delete_wallet_record(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
ErrorCode::from(unsafe {
non_secrets::indy_delete_wallet_record(command_handle, wallet_handle, xtype.as_ptr(), id.as_ptr(), cb)
})
}
pub fn get_wallet_record(wallet_handle: WalletHandle, xtype: &str, id: &str, options_json: &str) -> Box<dyn Future<Item=String, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
let err = _get_wallet_record(command_handle, wallet_handle, xtype, id, options_json, cb);
ResultHandler::str(command_handle, err, receiver)
}
fn _get_wallet_record(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, id: &str, options_json: &str, cb: Option<ResponseStringCB>) -> ErrorCode {
let xtype = c_str!(xtype);
let id = c_str!(id);
let options_json = c_str!(options_json);
ErrorCode::from(unsafe {
non_secrets::indy_get_wallet_record(command_handle, wallet_handle, xtype.as_ptr(), id.as_ptr(), options_json.as_ptr(), cb)
})
}
pub fn open_wallet_search(wallet_handle: WalletHandle, xtype: &str, query_json: &str, options_json: &str) -> Box<dyn Future<Item=SearchHandle, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_handle();
let err = _open_wallet_search(command_handle, wallet_handle, xtype, query_json, options_json, cb);
ResultHandler::handle(command_handle, err, receiver)
}
fn _open_wallet_search(command_handle: CommandHandle, wallet_handle: WalletHandle, xtype: &str, query_json: &str, options_json: &str, cb: Option<ResponseI32CB>) -> ErrorCode {
let xtype = c_str!(xtype);
let query_json = c_str!(query_json);
let options_json = c_str!(options_json);
ErrorCode::from(unsafe {
non_secrets::indy_open_wallet_search(command_handle, wallet_handle, xtype.as_ptr(), query_json.as_ptr(), options_json.as_ptr(), cb)
})
}
pub fn fetch_wallet_search_next_records(wallet_handle: WalletHandle, wallet_search_handle: SearchHandle, count: usize) -> Box<dyn Future<Item=String, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
let err = _fetch_wallet_search_next_records(command_handle, wallet_handle, wallet_search_handle, count, cb);
ResultHandler::str(command_handle, err, receiver)
}
fn _fetch_wallet_search_next_records(command_handle: CommandHandle, wallet_handle: WalletHandle, wallet_search_handle: SearchHandle, count: usize, cb: Option<ResponseStringCB>) -> ErrorCode {
ErrorCode::from(unsafe {
non_secrets::indy_fetch_wallet_search_next_records(command_handle, wallet_handle, wallet_search_handle, count, cb)
})
}
pub fn close_wallet_search(wallet_search_handle: SearchHandle) -> Box<dyn Future<Item=(), Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
let err = _close_wallet_search(command_handle, wallet_search_handle, cb);
ResultHandler::empty(command_handle, err, receiver)
}
fn _close_wallet_search(command_handle: CommandHandle, wallet_search_handle: SearchHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
ErrorCode::from(unsafe {
non_secrets::indy_close_wallet_search(command_handle, wallet_search_handle, cb)
})
}
fn _default_credentials(credentials: Option<&str>) -> CString {
match credentials {
Some(s) => c_str!(s),
None => c_str!(r#"{"key":""}"#)
}
}
pub fn generate_wallet_key(config: Option<&str>) -> Box<dyn Future<Item=String, Error=IndyError>> {
let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
let err = _generate_wallet_key(command_handle, config, cb);
ResultHandler::str(command_handle, err, receiver)
}
fn _generate_wallet_key(command_handle: CommandHandle, config: Option<&str>, cb: Option<ResponseStringCB>) -> ErrorCode {
let config = opt_c_str_json!(config);
ErrorCode::from(unsafe { wallet::indy_generate_wallet_key(command_handle, config.as_ptr(), cb) })
}