rust_libindy_wrapper/
lib.rs

1#[macro_use]
2extern crate lazy_static;
3#[macro_use]
4extern crate log;
5
6extern crate num_traits;
7#[macro_use]
8extern crate num_derive;
9
10#[macro_use]
11mod macros;
12
13pub mod anoncreds;
14pub mod blob_storage;
15pub mod crypto;
16pub mod did;
17pub mod ledger;
18pub mod payments;
19pub mod pairwise;
20pub mod pool;
21pub mod wallet;
22pub mod utils;
23pub mod native;
24
25use std::sync::mpsc;
26
27pub type IndyHandle = i32;
28
29#[derive(Debug, PartialEq, Copy, Clone, FromPrimitive, ToPrimitive)]
30#[repr(i32)]
31#[allow(dead_code)]
32pub enum ErrorCode
33{
34    Success = 0,
35
36    // Common errors
37
38    // Caller passed invalid value as param 1 (null, invalid json and etc..)
39    CommonInvalidParam1 = 100,
40
41    // Caller passed invalid value as param 2 (null, invalid json and etc..)
42    CommonInvalidParam2 = 101,
43
44    // Caller passed invalid value as param 3 (null, invalid json and etc..)
45    CommonInvalidParam3 = 102,
46
47    // Caller passed invalid value as param 4 (null, invalid json and etc..)
48    CommonInvalidParam4 = 103,
49
50    // Caller passed invalid value as param 5 (null, invalid json and etc..)
51    CommonInvalidParam5 = 104,
52
53    // Caller passed invalid value as param 6 (null, invalid json and etc..)
54    CommonInvalidParam6 = 105,
55
56    // Caller passed invalid value as param 7 (null, invalid json and etc..)
57    CommonInvalidParam7 = 106,
58
59    // Caller passed invalid value as param 8 (null, invalid json and etc..)
60    CommonInvalidParam8 = 107,
61
62    // Caller passed invalid value as param 9 (null, invalid json and etc..)
63    CommonInvalidParam9 = 108,
64
65    // Caller passed invalid value as param 10 (null, invalid json and etc..)
66    CommonInvalidParam10 = 109,
67
68    // Caller passed invalid value as param 11 (null, invalid json and etc..)
69    CommonInvalidParam11 = 110,
70
71    // Caller passed invalid value as param 11 (null, invalid json and etc..)
72    CommonInvalidParam12 = 111,
73
74    // Invalid library state was detected in runtime. It signals library bug
75    CommonInvalidState = 112,
76
77    // Object (json, config, key, credential and etc...) passed by library caller has invalid structure
78    CommonInvalidStructure = 113,
79
80    // IO Error
81    CommonIOError = 114,
82
83    // Caller passed invalid value as param 13 (null, invalid json and etc..)
84    CommonInvalidParam13 = 115,
85
86    // Caller passed invalid value as param 14 (null, invalid json and etc..)
87    CommonInvalidParam14 = 116,
88
89    // Caller passed invalid value as param 15 (null, invalid json and etc..)
90    CommonInvalidParam15 = 117,
91
92    // Caller passed invalid value as param 16 (null, invalid json and etc..)
93    CommonInvalidParam16 = 118,
94
95    // Caller passed invalid value as param 17 (null, invalid json and etc..)
96    CommonInvalidParam17 = 119,
97
98    // Caller passed invalid value as param 18 (null, invalid json and etc..)
99    CommonInvalidParam18 = 120,
100
101    // Caller passed invalid value as param 19 (null, invalid json and etc..)
102    CommonInvalidParam19 = 121,
103
104    // Caller passed invalid value as param 20 (null, invalid json and etc..)
105    CommonInvalidParam20 = 122,
106
107    // Caller passed invalid value as param 21 (null, invalid json and etc..)
108    CommonInvalidParam21 = 123,
109
110    // Caller passed invalid value as param 22 (null, invalid json and etc..)
111    CommonInvalidParam22 = 124,
112
113    // Caller passed invalid value as param 23 (null, invalid json and etc..)
114    CommonInvalidParam23 = 125,
115
116    // Caller passed invalid value as param 24 (null, invalid json and etc..)
117    CommonInvalidParam24 = 126,
118
119    // Caller passed invalid value as param 25 (null, invalid json and etc..)
120    CommonInvalidParam25 = 127,
121
122    // Caller passed invalid value as param 26 (null, invalid json and etc..)
123    CommonInvalidParam26 = 128,
124
125    // Caller passed invalid value as param 27 (null, invalid json and etc..)
126    CommonInvalidParam27 = 129,
127
128    // Wallet errors
129    // Caller passed invalid wallet handle
130    WalletInvalidHandle = 200,
131
132    // Unknown type of wallet was passed on create_wallet
133    WalletUnknownTypeError = 201,
134
135    // Attempt to register already existing wallet type
136    WalletTypeAlreadyRegisteredError = 202,
137
138    // Attempt to create wallet with name used for another exists wallet
139    WalletAlreadyExistsError = 203,
140
141    // Requested entity id isn't present in wallet
142    WalletNotFoundError = 204,
143
144    // Trying to use wallet with pool that has different name
145    WalletIncompatiblePoolError = 205,
146
147    // Trying to open wallet that was opened already
148    WalletAlreadyOpenedError = 206,
149
150    // Attempt to open encrypted wallet with invalid credentials
151    WalletAccessFailed = 207,
152
153    // Input provided to wallet operations is considered not valid
154    WalletInputError = 208,
155
156    // Decoding of wallet data during input/output failed
157    WalletDecodingError = 209,
158
159    // Storage error occurred during wallet operation
160    WalletStorageError = 210,
161
162    // Error during encryption-related operations
163    WalletEncryptionError = 211,
164
165    // Requested wallet item not found
166    WalletItemNotFound = 212,
167
168    // Returned if wallet's add_record operation is used with record name that already exists
169    WalletItemAlreadyExists = 213,
170
171    // Returned if provided wallet query is invalid
172    WalletQueryError = 214,
173
174    // Ledger errors
175    // Trying to open pool ledger that wasn't created before
176    PoolLedgerNotCreatedError = 300,
177
178    // Caller passed invalid pool ledger handle
179    PoolLedgerInvalidPoolHandle = 301,
180
181    // Pool ledger terminated
182    PoolLedgerTerminated = 302,
183
184    // No concensus during ledger operation
185    LedgerNoConsensusError = 303,
186
187    // Attempt to parse invalid transaction response
188    LedgerInvalidTransaction = 304,
189
190    // Attempt to send transaction without the necessary privileges
191    LedgerSecurityError = 305,
192
193    // Attempt to create pool ledger config with name used for another existing pool
194    PoolLedgerConfigAlreadyExistsError = 306,
195
196    // Timeout for action
197    PoolLedgerTimeout = 307,
198
199    // Attempt to open Pool for witch Genesis Transactions are not compatible with set Protocol version.
200    // Call pool.indy_set_protocol_version to set correct Protocol version.
201    PoolIncompatibleProtocolVersion = 308,
202
203    // Revocation registry is full and creation of new registry is necessary
204    AnoncredsRevocationRegistryFullError = 400,
205
206    AnoncredsInvalidUserRevocIndex = 401,
207
208    // Attempt to generate master secret with duplicated name
209    AnoncredsMasterSecretDuplicateNameError = 404,
210
211    AnoncredsProofRejected = 405,
212
213    AnoncredsCredentialRevoked = 406,
214
215    // Attempt to create credential definition with duplicated did schema pair
216    AnoncredsCredDefAlreadyExistsError = 407,
217
218    // Signus errors
219    // Unknown format of DID entity keys
220    UnknownCryptoTypeError = 500,
221
222    // Attempt to create duplicate did
223    DidAlreadyExistsError = 600,
224
225    // Unknown payment method was given
226    UnknownPaymentMethod = 700,
227
228    //No method were scraped from inputs/outputs or more than one were scraped
229    IncompatiblePaymentError = 701,
230
231    // Insufficient funds on inputs
232    PaymentInsufficientFundsError = 702,
233    
234    // No such source on a ledger
235    PaymentSourceDoesNotExistError = 703,
236
237    // Extra funds on inputs
238    PaymentExtraFundsError = 705,
239}
240
241impl ErrorCode {
242    #[allow(non_snake_case)]
243    #[allow(dead_code)]
244    #[allow(unused)]
245    pub fn description(&self) -> &'static str {
246        match self {
247            CommonInvalidParam1 => "Caller passed invalid value as param 1",
248            CommonInvalidParam2 => "Caller passed invalid value as param 2",
249            CommonInvalidParam3 => "Caller passed invalid value as param 3",
250            CommonInvalidParam4 => "Caller passed invalid value as param 4",
251            CommonInvalidParam5 => "Caller passed invalid value as param 5",
252            CommonInvalidParam6 => "Caller passed invalid value as param 6",
253            CommonInvalidParam7 => "Caller passed invalid value as param 7",
254            CommonInvalidParam8 => "Caller passed invalid value as param 8",
255            CommonInvalidParam9 => "Caller passed invalid value as param 9",
256            CommonInvalidParam10 => "Caller passed invalid value as param 10",
257            CommonInvalidParam11 => "Caller passed invalid value as param 11",
258            CommonInvalidParam12 => "Caller passed invalid value as param 12",
259            CommonInvalidParam13 => "Caller passed invalid value as param 13",
260            CommonInvalidParam14 => "Caller passed invalid value as param 14",
261            CommonInvalidParam15 => "Caller passed invalid value as param 15",
262            CommonInvalidParam16 => "Caller passed invalid value as param 16",
263            CommonInvalidParam17 => "Caller passed invalid value as param 17",
264            CommonInvalidParam18 => "Caller passed invalid value as param 18",
265            CommonInvalidParam19 => "Caller passed invalid value as param 19",
266            CommonInvalidParam20 => "Caller passed invalid value as param 20",
267            CommonInvalidParam21 => "Caller passed invalid value as param 21",
268            CommonInvalidParam22 => "Caller passed invalid value as param 22",
269            CommonInvalidParam23 => "Caller passed invalid value as param 23",
270            CommonInvalidParam24 => "Caller passed invalid value as param 24",
271            CommonInvalidParam25 => "Caller passed invalid value as param 25",
272            CommonInvalidParam26 => "Caller passed invalid value as param 26",
273            CommonInvalidParam27 => "Caller passed invalid value as param 27",
274            CommonInvalidState => "Invalid library state was detected in runtime. It signals library bug",
275            CommonInvalidStructure => "Object (json, config, key, credential and etc...) passed by library caller has invalid structure",
276            CommonIOError => "IO Error",
277            WalletInvalidHandle => "Caller passed invalid wallet handle",
278            WalletUnknownTypeError => "Caller passed invalid wallet handle",
279            WalletTypeAlreadyRegisteredError => "Attempt to register already existing wallet type",
280            WalletAlreadyExistsError => "Attempt to create wallet with name used for another exists wallet",
281            WalletNotFoundError => "Requested entity id isn't present in wallet",
282            WalletIncompatiblePoolError => "Trying to use wallet with pool that has different name",
283            WalletAccessFailed => "Trying to open wallet encrypted wallet with invalid credentials",
284            WalletAlreadyOpenedError => "Trying to open wallet that was opened already",
285            WalletInputError => "Input provided to wallet operations is considered not valid",
286            WalletDecodingError => "Decoding of wallet data during input/output failed",
287            WalletStorageError => "Storage error occurred during wallet operation",
288            WalletEncryptionError => "Error during encryption-related operations",
289            WalletItemNotFound => "Requested wallet item not found",
290            WalletItemAlreadyExists => "Returned if wallet's add_record operation is used with record name that already exists",
291            WalletQueryError => "Returned if provided wallet query is invalid",
292            PoolLedgerNotCreatedError => "Trying to open pool ledger that wasn't created before",
293            PoolLedgerInvalidPoolHandle => "Caller passed invalid pool ledger handle",
294            PoolLedgerTerminated => "Pool ledger terminated",
295            LedgerNoConsensusError => "No concensus during ledger operation",
296            LedgerInvalidTransaction => "Attempt to send unknown or incomplete transaction message",
297            LedgerSecurityError => "Attempt to send transaction without the necessary privileges",
298            PoolLedgerConfigAlreadyExistsError => "Attempt to create pool ledger config with name used for another existing pool",
299            PoolLedgerTimeout => "Timeout for action",
300            PoolIncompatibleProtocolVersion => "Attempt to open Pool for witch Genesis Transactions are not compatible with set Protocol version. Set the correct Protocol version first.",
301            AnoncredsRevocationRegistryFullError => "Revocation registry is full and creation of new registry is necessary",
302            AnoncredsInvalidUserRevocIndex => "Invalid user revocation index",
303            AnoncredsMasterSecretDuplicateNameError => "Attempt to generate master secret with duplicated name",
304            AnoncredsProofRejected => "Proof rejected",
305            AnoncredsCredentialRevoked => "Credential revoked",
306            AnoncredsCredDefAlreadyExistsError => "Credential definition already exists",
307            UnknownCryptoTypeError => "Unknown format of DID entity keys",
308            DidAlreadyExistsError => "Did already exists",
309            UnknownPaymentMethod => "Unknown payment method was given",
310            IncompatiblePaymentError => "Multiple different payment methods were specified",
311            PaymentInsufficientFundsError => "Payment cannot be processed because there was insufficient funds",
312            PaymentSourceDoesNotExistError => "No such source on a ledger.",
313            PaymentExtraFundsError => "Payment cannot be processed because there were more funds than required",
314        }
315    }
316
317    pub fn is_ok(&self) -> bool {
318        *self == ErrorCode::Success
319    }
320
321    pub fn is_err(&self) -> bool {
322        *self != ErrorCode::Success
323    }
324
325    pub fn try_err(&self) -> Result<(), ErrorCode> {
326        if self.is_err() {
327            return Err(*self)
328        }
329        Ok(())
330    }
331}
332
333impl From<i32> for ErrorCode {
334    fn from(i: i32) -> Self {
335        let conversion = num_traits::FromPrimitive::from_i32(i);
336        if conversion.is_some() {
337            conversion.unwrap()
338        } else {
339            panic!("Unable to convert from {}, unknown error code", i)
340        }
341    }
342}
343
344impl Into<i32> for ErrorCode {
345    fn into(self) -> i32 {
346        num_traits::ToPrimitive::to_i32(&self).unwrap()
347    }
348}
349
350impl From<mpsc::RecvTimeoutError> for ErrorCode {
351    fn from(err: mpsc::RecvTimeoutError) -> Self {
352        match err {
353            mpsc::RecvTimeoutError::Timeout => {
354                warn!("Timed out waiting for libindy to call back");
355                ErrorCode::CommonIOError
356            },
357            mpsc::RecvTimeoutError::Disconnected => {
358                warn!("Channel to libindy was disconnected unexpectedly");
359                ErrorCode::CommonIOError
360            }
361        }
362    }
363}
364
365impl From<mpsc::RecvError> for ErrorCode {
366    fn from(e: mpsc::RecvError) -> Self {
367        warn!("Channel returned an error - {:?}", e);
368        ErrorCode::CommonIOError
369    }
370}