extern crate futures;
#[macro_use]
extern crate lazy_static;
extern crate log;
extern crate libc;
extern crate failure;
extern crate num_traits;
#[macro_use]
extern crate num_derive;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate indy_sys as ffi;
#[macro_use]
mod macros;
pub use futures::future;
use libc::c_char;
pub mod anoncreds;
pub mod blob_storage;
pub mod crypto;
pub mod did;
pub mod ledger;
pub mod logger;
pub mod payments;
pub mod pairwise;
pub mod pool;
pub mod wallet;
pub mod cache;
mod utils;
use std::ffi::CString;
use std::fmt;
use std::ptr;
use std::ffi::CStr;
use failure::{Backtrace, Fail};
pub use ffi::{
RecordHandle,
TailWriterHandle,
BlobStorageReaderHandle,
BlobStorageReaderCfgHandle,
MetadataHandle,
Timeout,
TailsWriterHandle,
IndyHandle,
CommandHandle,
WalletHandle,
PoolHandle,
SearchHandle,
StorageHandle,
INVALID_WALLET_HANDLE,
INVALID_POOL_HANDLE,
INVALID_COMMAND_HANDLE
};
pub fn set_runtime_config(config: &str) -> ErrorCode {
let config = c_str!(config);
ErrorCode::from(unsafe {
ffi::indy_set_runtime_config(config.as_ptr())
})
}
#[derive(Fail, Debug, PartialEq, Copy, Clone, FromPrimitive, ToPrimitive)]
#[repr(i32)]
#[allow(dead_code)]
pub enum ErrorCode
{
#[fail(display = "Success")]
Success = 0,
#[fail(display = "CommonInvalidParam1")]
CommonInvalidParam1 = 100,
#[fail(display = "CommonInvalidParam2")]
CommonInvalidParam2 = 101,
#[fail(display = "CommonInvalidParam3")]
CommonInvalidParam3 = 102,
#[fail(display = "CommonInvalidParam4")]
CommonInvalidParam4 = 103,
#[fail(display = "CommonInvalidParam5")]
CommonInvalidParam5 = 104,
#[fail(display = "CommonInvalidParam6")]
CommonInvalidParam6 = 105,
#[fail(display = "CommonInvalidParam7")]
CommonInvalidParam7 = 106,
#[fail(display = "CommonInvalidParam8")]
CommonInvalidParam8 = 107,
#[fail(display = "CommonInvalidParam9")]
CommonInvalidParam9 = 108,
#[fail(display = "CommonInvalidParam10")]
CommonInvalidParam10 = 109,
#[fail(display = "CommonInvalidParam11")]
CommonInvalidParam11 = 110,
#[fail(display = "CommonInvalidParam12")]
CommonInvalidParam12 = 111,
#[fail(display = "CommonInvalidState")]
CommonInvalidState = 112,
#[fail(display = "CommonInvalidStructure")]
CommonInvalidStructure = 113,
#[fail(display = "CommonIOError")]
CommonIOError = 114,
#[fail(display = "CommonInvalidParam13")]
CommonInvalidParam13 = 115,
#[fail(display = "CommonInvalidParam14")]
CommonInvalidParam14 = 116,
#[fail(display = "CommonInvalidParam15")]
CommonInvalidParam15 = 117,
#[fail(display = "CommonInvalidParam16")]
CommonInvalidParam16 = 118,
#[fail(display = "CommonInvalidParam17")]
CommonInvalidParam17 = 119,
#[fail(display = "CommonInvalidParam18")]
CommonInvalidParam18 = 120,
#[fail(display = "CommonInvalidParam19")]
CommonInvalidParam19 = 121,
#[fail(display = "CommonInvalidParam20")]
CommonInvalidParam20 = 122,
#[fail(display = "CommonInvalidParam21")]
CommonInvalidParam21 = 123,
#[fail(display = "CommonInvalidParam22")]
CommonInvalidParam22 = 124,
#[fail(display = "CommonInvalidParam23")]
CommonInvalidParam23 = 125,
#[fail(display = "CommonInvalidParam24")]
CommonInvalidParam24 = 126,
#[fail(display = "CommonInvalidParam25")]
CommonInvalidParam25 = 127,
#[fail(display = "CommonInvalidParam26")]
CommonInvalidParam26 = 128,
#[fail(display = "CommonInvalidParam27")]
CommonInvalidParam27 = 129,
#[fail(display = "WalletInvalidHandle")]
WalletInvalidHandle = 200,
#[fail(display = "WalletUnknownTypeError")]
WalletUnknownTypeError = 201,
#[fail(display = "WalletTypeAlreadyRegisteredError")]
WalletTypeAlreadyRegisteredError = 202,
#[fail(display = "WalletAlreadyExistsError")]
WalletAlreadyExistsError = 203,
#[fail(display = "WalletNotFoundError")]
WalletNotFoundError = 204,
#[fail(display = "WalletIncompatiblePoolError")]
WalletIncompatiblePoolError = 205,
#[fail(display = "WalletAlreadyOpenedError")]
WalletAlreadyOpenedError = 206,
#[fail(display = "WalletAccessFailed")]
WalletAccessFailed = 207,
#[fail(display = "WalletInputError")]
WalletInputError = 208,
#[fail(display = "WalletDecodingError")]
WalletDecodingError = 209,
#[fail(display = "WalletStorageError")]
WalletStorageError = 210,
#[fail(display = "WalletEncryptionError")]
WalletEncryptionError = 211,
#[fail(display = "WalletItemNotFound")]
WalletItemNotFound = 212,
#[fail(display = "WalletItemAlreadyExists")]
WalletItemAlreadyExists = 213,
#[fail(display = "WalletQueryError")]
WalletQueryError = 214,
#[fail(display = "PoolLedgerNotCreatedError")]
PoolLedgerNotCreatedError = 300,
#[fail(display = "PoolLedgerInvalidPoolHandle")]
PoolLedgerInvalidPoolHandle = 301,
#[fail(display = "PoolLedgerTerminated")]
PoolLedgerTerminated = 302,
#[fail(display = "LedgerNoConsensusError")]
LedgerNoConsensusError = 303,
#[fail(display = "LedgerInvalidTransaction")]
LedgerInvalidTransaction = 304,
#[fail(display = "LedgerSecurityError")]
LedgerSecurityError = 305,
#[fail(display = "PoolLedgerConfigAlreadyExistsError")]
PoolLedgerConfigAlreadyExistsError = 306,
#[fail(display = "PoolLedgerTimeout")]
PoolLedgerTimeout = 307,
#[fail(display = "PoolIncompatibleProtocolVersion")]
PoolIncompatibleProtocolVersion = 308,
#[fail(display = "LedgerNotFound")]
LedgerNotFound = 309,
#[fail(display = "AnoncredsRevocationRegistryFullError")]
AnoncredsRevocationRegistryFullError = 400,
#[fail(display = "AnoncredsInvalidUserRevocId")]
AnoncredsInvalidUserRevocId = 401,
#[fail(display = "AnoncredsMasterSecretDuplicateNameError")]
AnoncredsMasterSecretDuplicateNameError = 404,
#[fail(display = "AnoncredsProofRejected")]
AnoncredsProofRejected = 405,
#[fail(display = "AnoncredsCredentialRevoked")]
AnoncredsCredentialRevoked = 406,
#[fail(display = "AnoncredsCredDefAlreadyExistsError")]
AnoncredsCredDefAlreadyExistsError = 407,
#[fail(display = "UnknownCryptoTypeError")]
UnknownCryptoTypeError = 500,
#[fail(display = "DidAlreadyExistsError")]
DidAlreadyExistsError = 600,
#[fail(display = "UnknownPaymentMethod")]
UnknownPaymentMethod = 700,
#[fail(display = "IncompatiblePaymentError")]
IncompatiblePaymentError = 701,
#[fail(display = "PaymentInsufficientFundsError")]
PaymentInsufficientFundsError = 702,
#[fail(display = "PaymentSourceDoesNotExistError")]
PaymentSourceDoesNotExistError = 703,
#[fail(display = "PaymentOperationNotSupportedError")]
PaymentOperationNotSupportedError = 704,
#[fail(display = "PaymentExtraFundsError")]
PaymentExtraFundsError = 705,
#[fail(display = "The transaction is not allowed to a requester")]
TransactionNotAllowed,
}
impl From<i32> for ErrorCode {
fn from(i: i32) -> Self {
let conversion = num_traits::FromPrimitive::from_i32(i);
if conversion.is_some() {
conversion.unwrap()
} else {
panic!("Unable to convert from {}, unknown error code", i)
}
}
}
impl Into<i32> for ErrorCode {
fn into(self) -> i32 {
num_traits::ToPrimitive::to_i32(&self).unwrap()
}
}
#[derive(Debug)]
pub struct IndyError {
pub error_code: ErrorCode,
pub message: String,
pub indy_backtrace: Option<String>
}
impl Fail for IndyError {
fn cause(&self) -> Option<&dyn Fail> {
self.error_code.cause()
}
fn backtrace(&self) -> Option<&Backtrace> { self.error_code.backtrace() }
}
impl fmt::Display for IndyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{}", self.message)?;
Ok(())
}
}
impl IndyError {
pub(crate) fn new(error_code: ErrorCode) -> Self {
let mut error_json_p: *const c_char = ptr::null();
unsafe { ffi::indy_get_current_error(&mut error_json_p); }
let error_json = opt_rust_str!(error_json_p);
let error_json = match error_json {
Some(error_json_) => error_json_,
None => {
return IndyError {
error_code: ErrorCode::CommonInvalidState,
message: String::from("Invalid ErrorMessage pointer"),
indy_backtrace: None,
};
}
};
match ::serde_json::from_str::<ErrorDetails>(&error_json) {
Ok(error) => IndyError {
error_code,
message: error.message,
indy_backtrace: error.backtrace,
},
Err(err) => IndyError {
error_code: ErrorCode::CommonInvalidState,
message: err.to_string(),
indy_backtrace: None,
}
}
}
}
#[derive(Deserialize)]
struct ErrorDetails {
message: String,
backtrace: Option<String>
}