onemoney-protocol 0.18.0

Official Rust SDK for OneMoney Protocol - L1 blockchain network client
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Error types for the OneMoney SDK.

use std::{array::TryFromSliceError, result::Result as StdResult};

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::crypto::CryptoError;

/// Result type alias for OneMoney SDK operations.
pub type Result<T> = StdResult<T, Error>;

/// Main error type for the OneMoney SDK.
#[derive(Error, Debug)]
pub enum Error {
    /// JSON serialization/deserialization error.
    #[error("JSON parsing failed: {0}")]
    Json(#[from] serde_json::Error),

    /// API error returned by the server.
    #[error("API error {status_code}: {error_code} - {message}")]
    Api {
        status_code: u16,
        error_code: String,
        message: String,
    },

    /// HTTP transport error with optional status code.
    #[error("HTTP transport error: {message}")]
    HttpTransport { message: String, status_code: Option<u16> },

    /// Request timeout error.
    #[error("Request timeout after {timeout_ms}ms to {endpoint}")]
    RequestTimeout { endpoint: String, timeout_ms: u64 },

    /// Connection error.
    #[error("Connection failed: {0}")]
    Connection(String),

    /// DNS resolution error.
    #[error("DNS resolution failed: {0}")]
    DnsResolution(String),

    /// Response deserialization error.
    #[error("Failed to deserialize {format} response: {error} - Response: {response}")]
    ResponseDeserialization {
        format: String,
        error: String,
        response: String,
    },

    /// Authentication error.
    #[error("Authentication failed: {0}")]
    Authentication(String),

    /// Authorization error.
    #[error("Authorization failed: {0}")]
    Authorization(String),

    /// Rate limit exceeded.
    #[error("Rate limit exceeded")]
    RateLimitExceeded { retry_after_seconds: Option<u64> },

    /// Invalid request parameter.
    #[error("Invalid parameter '{parameter}': {message}")]
    InvalidParameter { parameter: String, message: String },

    /// Resource not found.
    #[error("Resource not found: {resource_type} with {identifier}")]
    ResourceNotFound { resource_type: String, identifier: String },

    /// Business logic error.
    #[error("Business logic error: {operation} failed - {reason}")]
    BusinessLogic { operation: String, reason: String },

    /// Cryptographic operation errors.
    #[error("Cryptographic operation failed: {0}")]
    Crypto(#[from] CryptoError),

    /// Signature verification failed.
    #[error("Signature verification failed: {0}")]
    VerificationError(String),

    /// Client configuration errors.
    #[error("Client configuration error: {0}")]
    Config(#[from] ConfigError),

    /// URL parsing error.
    #[error("Invalid URL: {0}")]
    Url(#[from] url::ParseError),

    /// Hex decoding error.
    #[error("Hex decoding failed: {0}")]
    Hex(#[from] hex::FromHexError),

    /// Address parsing error.
    #[error("Invalid address format: {0}")]
    Address(String),

    /// Array conversion error.
    #[error("Array conversion failed: expected length {expected}, got {actual}")]
    ArrayConversion { expected: usize, actual: usize },

    /// Validation error for input parameters.
    #[error("Validation failed: {field} - {message}")]
    Validation { field: String, message: String },

    /// Generic error with custom message.
    #[error("{0}")]
    Custom(String),
}

/// Client configuration errors.
#[derive(Error, Debug)]
pub enum ConfigError {
    /// Invalid timeout value.
    #[error("Invalid timeout: {0}")]
    InvalidTimeout(String),

    /// Invalid network configuration.
    #[error("Invalid network configuration: {0}")]
    InvalidNetwork(String),

    /// Missing required configuration.
    #[error("Missing required configuration: {0}")]
    MissingConfig(String),

    /// HTTP client builder failed.
    #[error("Failed to build HTTP client: {0}")]
    ClientBuilder(String),
}

/// API error response structure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorResponse {
    pub error_code: String,
    pub message: String,
}

impl Error {
    /// Create a new API error.
    pub fn api(status_code: u16, error_code: String, message: String) -> Self {
        Self::Api {
            status_code,
            error_code,
            message,
        }
    }

    /// Create an address parsing error.
    pub fn address<T: Into<String>>(msg: T) -> Self {
        Self::Address(msg.into())
    }

    /// Create an array conversion error.
    pub fn array_conversion(expected: usize, actual: usize) -> Self {
        Self::ArrayConversion { expected, actual }
    }

    /// Create a validation error.
    pub fn validation<T: Into<String>, U: Into<String>>(field: T, message: U) -> Self {
        Self::Validation {
            field: field.into(),
            message: message.into(),
        }
    }

    /// Create a custom error.
    pub fn custom<T: Into<String>>(msg: T) -> Self {
        Self::Custom(msg.into())
    }

    /// Create a verification error.
    pub fn verification_error<T: Into<String>>(msg: T) -> Self {
        Self::VerificationError(msg.into())
    }

    /// Check if this is an API error.
    pub fn is_api_error(&self) -> bool {
        matches!(self, Self::Api { .. })
    }

    /// Check if this is a configuration error.
    pub fn is_config_error(&self) -> bool {
        matches!(self, Self::Config(_))
    }

    /// Check if this is a cryptographic error.
    pub fn is_crypto_error(&self) -> bool {
        matches!(self, Self::Crypto(_))
    }

    /// Get the status code if this is an API error.
    pub fn status_code(&self) -> Option<u16> {
        match self {
            Self::Api { status_code, .. } => Some(*status_code),
            _ => None,
        }
    }

    /// Get the error code if this is an API error.
    pub fn error_code(&self) -> Option<&str> {
        match self {
            Self::Api { error_code, .. } => Some(error_code),
            _ => None,
        }
    }

    /// Create an HTTP transport error.
    pub fn http_transport<T: Into<String>>(message: T, status_code: Option<u16>) -> Self {
        Self::HttpTransport {
            message: message.into(),
            status_code,
        }
    }

    /// Create a request timeout error.
    pub fn request_timeout<T: Into<String>>(endpoint: T, timeout_ms: u64) -> Self {
        Self::RequestTimeout {
            endpoint: endpoint.into(),
            timeout_ms,
        }
    }

    /// Create a connection error.
    pub fn connection<T: Into<String>>(message: T) -> Self {
        Self::Connection(message.into())
    }

    /// Create a DNS resolution error.
    pub fn dns_resolution<T: Into<String>>(message: T) -> Self {
        Self::DnsResolution(message.into())
    }

    /// Create a response deserialization error.
    pub fn response_deserialization<A: Into<String>, B: Into<String>, C: Into<String>>(
        format: A,
        error: B,
        response: C,
    ) -> Self {
        Self::ResponseDeserialization {
            format: format.into(),
            error: error.into(),
            response: response.into(),
        }
    }

    /// Create an authentication error.
    pub fn authentication<T: Into<String>>(message: T) -> Self {
        Self::Authentication(message.into())
    }

    /// Create an authorization error.
    pub fn authorization<T: Into<String>>(message: T) -> Self {
        Self::Authorization(message.into())
    }

    /// Create a rate limit exceeded error.
    pub fn rate_limit_exceeded(retry_after_seconds: Option<u64>) -> Self {
        Self::RateLimitExceeded { retry_after_seconds }
    }

    /// Create an invalid parameter error.
    pub fn invalid_parameter<A: Into<String>, B: Into<String>>(parameter: A, message: B) -> Self {
        Self::InvalidParameter {
            parameter: parameter.into(),
            message: message.into(),
        }
    }

    /// Create a resource not found error.
    pub fn resource_not_found<A: Into<String>, B: Into<String>>(resource_type: A, identifier: B) -> Self {
        Self::ResourceNotFound {
            resource_type: resource_type.into(),
            identifier: identifier.into(),
        }
    }

    /// Create a business logic error.
    pub fn business_logic<A: Into<String>, B: Into<String>>(operation: A, reason: B) -> Self {
        Self::BusinessLogic {
            operation: operation.into(),
            reason: reason.into(),
        }
    }
}

impl From<TryFromSliceError> for Error {
    fn from(_err: TryFromSliceError) -> Self {
        Self::ArrayConversion {
            expected: 32, // Most common case for crypto operations
            actual: 0,    // We don't have the actual length in TryFromSliceError
        }
    }
}

/// Enhanced reqwest error mapping with L1 compatibility.
impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        if err.is_timeout() {
            Error::request_timeout(
                err.url()
                    .map(|u| u.to_string())
                    .unwrap_or_else(|| "unknown".to_string()),
                30000, // Default timeout assumption
            )
        } else if err.is_connect() {
            Error::connection(format!("Connection failed: {}", err))
        } else if err.is_request() {
            Error::invalid_parameter("request", format!("Request error: {}", err))
        } else if err.is_decode() {
            Error::response_deserialization("JSON", err.to_string(), "Failed to decode response body")
        } else {
            // Check for specific HTTP status codes if available
            if let Some(status) = err.status() {
                Error::http_transport(err.to_string(), Some(status.as_u16()))
            } else {
                Error::http_transport(err.to_string(), None)
            }
        }
    }
}

impl ConfigError {
    /// Create an invalid timeout error.
    pub fn invalid_timeout<T: Into<String>>(msg: T) -> Self {
        Self::InvalidTimeout(msg.into())
    }

    /// Create an invalid network error.
    pub fn invalid_network<T: Into<String>>(msg: T) -> Self {
        Self::InvalidNetwork(msg.into())
    }

    /// Create a missing config error.
    pub fn missing_config<T: Into<String>>(msg: T) -> Self {
        Self::MissingConfig(msg.into())
    }

    /// Create a client builder error.
    pub fn client_builder<T: Into<String>>(msg: T) -> Self {
        Self::ClientBuilder(msg.into())
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error as StdError;

    use super::*;

    #[test]
    fn test_error_creation_methods() {
        // Test API error creation
        let api_error = Error::api(
            404,
            "resource_not_found".to_string(),
            "Transaction not found".to_string(),
        );
        assert!(matches!(api_error, Error::Api { status_code: 404, .. }));
        assert_eq!(api_error.status_code(), Some(404));
        assert_eq!(api_error.error_code(), Some("resource_not_found"));

        // Test address error creation
        let addr_error = Error::address("Invalid address format");
        assert!(matches!(addr_error, Error::Address(_)));

        // Test array conversion error creation
        let array_error = Error::array_conversion(32, 16);
        assert!(matches!(
            array_error,
            Error::ArrayConversion {
                expected: 32,
                actual: 16
            }
        ));

        // Test validation error creation
        let validation_error = Error::validation("email", "Invalid email format");
        assert!(matches!(validation_error, Error::Validation { .. }));

        // Test custom error creation
        let custom_error = Error::custom("Custom error message");
        assert!(matches!(custom_error, Error::Custom(_)));
    }

    #[test]
    fn test_http_transport_error_creation() {
        let error_with_status = Error::http_transport("Connection failed", Some(500));
        assert!(matches!(
            error_with_status,
            Error::HttpTransport {
                status_code: Some(500),
                ..
            }
        ));

        let error_without_status = Error::http_transport("Connection failed", None);
        assert!(matches!(
            error_without_status,
            Error::HttpTransport { status_code: None, .. }
        ));
    }

    #[test]
    fn test_request_timeout_error_creation() {
        let timeout_error = Error::request_timeout("/api/transactions", 30000);
        assert!(matches!(timeout_error, Error::RequestTimeout { timeout_ms: 30000, .. }));
    }

    #[test]
    fn test_authentication_and_authorization_errors() {
        let auth_error = Error::authentication("Invalid signature");
        assert!(matches!(auth_error, Error::Authentication(_)));

        let authz_error = Error::authorization("Insufficient permissions");
        assert!(matches!(authz_error, Error::Authorization(_)));
    }

    #[test]
    fn test_rate_limit_error_creation() {
        let rate_limit_with_retry = Error::rate_limit_exceeded(Some(60));
        assert!(matches!(
            rate_limit_with_retry,
            Error::RateLimitExceeded {
                retry_after_seconds: Some(60)
            }
        ));

        let rate_limit_without_retry = Error::rate_limit_exceeded(None);
        assert!(matches!(
            rate_limit_without_retry,
            Error::RateLimitExceeded {
                retry_after_seconds: None
            }
        ));
    }

    #[test]
    fn test_parameter_and_resource_errors() {
        let param_error = Error::invalid_parameter("amount", "Amount must be positive");
        assert!(matches!(param_error, Error::InvalidParameter { .. }));

        let resource_error = Error::resource_not_found("transaction", "0x123abc");
        assert!(matches!(resource_error, Error::ResourceNotFound { .. }));
    }

    #[test]
    fn test_business_logic_error_creation() {
        let business_error = Error::business_logic("transfer", "Insufficient balance");
        assert!(matches!(business_error, Error::BusinessLogic { .. }));
    }

    #[test]
    fn test_connection_and_dns_errors() {
        let conn_error = Error::connection("Failed to connect to server");
        assert!(matches!(conn_error, Error::Connection(_)));

        let dns_error = Error::dns_resolution("Could not resolve hostname");
        assert!(matches!(dns_error, Error::DnsResolution(_)));
    }

    #[test]
    fn test_response_deserialization_error() {
        let deser_error = Error::response_deserialization("JSON", "unexpected end of input", "{\"invalid\":");
        assert!(matches!(deser_error, Error::ResponseDeserialization { .. }));
    }

    #[test]
    fn test_error_type_checking_methods() {
        let api_error = Error::api(500, "server_error".to_string(), "Internal server error".to_string());
        assert!(api_error.is_api_error());
        assert!(!api_error.is_config_error());
        assert!(!api_error.is_crypto_error());

        let config_error = Error::Config(ConfigError::InvalidTimeout("Timeout too large".to_string()));
        assert!(!config_error.is_api_error());
        assert!(config_error.is_config_error());
        assert!(!config_error.is_crypto_error());

        let crypto_error = Error::Crypto(CryptoError::InvalidPrivateKey("Invalid key format".to_string()));
        assert!(!crypto_error.is_api_error());
        assert!(!crypto_error.is_config_error());
        assert!(crypto_error.is_crypto_error());
    }

    #[test]
    fn test_status_code_and_error_code_extraction() {
        let api_error = Error::api(422, "business_logic_error".to_string(), "Invalid operation".to_string());
        assert_eq!(api_error.status_code(), Some(422));
        assert_eq!(api_error.error_code(), Some("business_logic_error"));

        let non_api_error = Error::custom("Not an API error");
        assert_eq!(non_api_error.status_code(), None);
        assert_eq!(non_api_error.error_code(), None);
    }

    #[test]
    fn test_crypto_error_creation() {
        let invalid_private_key = CryptoError::invalid_private_key("Key too short");
        assert!(matches!(invalid_private_key, CryptoError::InvalidPrivateKey(_)));

        let invalid_public_key = CryptoError::invalid_public_key("Invalid format");
        assert!(matches!(invalid_public_key, CryptoError::InvalidPublicKey(_)));

        let signature_failed = CryptoError::signature_failed("Could not create signature");
        assert!(matches!(signature_failed, CryptoError::SignatureFailed(_)));

        let verification_failed = CryptoError::verification_failed("Signature mismatch");
        assert!(matches!(verification_failed, CryptoError::VerificationFailed(_)));

        let key_derivation = CryptoError::key_derivation("Derivation failed");
        assert!(matches!(key_derivation, CryptoError::KeyDerivation(_)));
    }

    #[test]
    fn test_config_error_creation() {
        let invalid_timeout = ConfigError::invalid_timeout("Timeout cannot be zero");
        assert!(matches!(invalid_timeout, ConfigError::InvalidTimeout(_)));

        let invalid_network = ConfigError::invalid_network("Unknown network");
        assert!(matches!(invalid_network, ConfigError::InvalidNetwork(_)));

        let missing_config = ConfigError::missing_config("API key required");
        assert!(matches!(missing_config, ConfigError::MissingConfig(_)));

        let client_builder = ConfigError::client_builder("Failed to build HTTP client");
        assert!(matches!(client_builder, ConfigError::ClientBuilder(_)));
    }

    #[test]
    fn test_error_display_formatting() {
        // Test different error display formats
        let api_error = Error::api(404, "not_found".to_string(), "Resource not found".to_string());
        let display_str = format!("{}", api_error);
        assert!(display_str.contains("API error 404"));
        assert!(display_str.contains("not_found"));
        assert!(display_str.contains("Resource not found"));

        let timeout_error = Error::request_timeout("/api/test", 5000);
        let timeout_str = format!("{}", timeout_error);
        assert!(timeout_str.contains("Request timeout after 5000ms"));
        assert!(timeout_str.contains("/api/test"));

        let param_error = Error::invalid_parameter("amount", "Must be positive");
        let param_str = format!("{}", param_error);
        assert!(param_str.contains("Invalid parameter 'amount'"));
        assert!(param_str.contains("Must be positive"));
    }

    #[test]
    fn test_error_from_conversions() {
        // Test From<CryptoError> conversion
        let crypto_error = CryptoError::invalid_private_key("Invalid key");
        let error: Error = crypto_error.into();
        assert!(matches!(error, Error::Crypto(_)));

        // Test From<ConfigError> conversion
        let config_error = ConfigError::invalid_timeout("Invalid timeout");
        let error: Error = config_error.into();
        assert!(matches!(error, Error::Config(_)));

        // Test From<TryFromSliceError> conversion
        // Create a TryFromSliceError by attempting to convert a slice that's too short
        let result: StdResult<[u8; 4], TryFromSliceError> = [0u8; 2].as_slice().try_into();
        let slice_error = result.unwrap_err();
        let error: Error = slice_error.into();
        assert!(matches!(
            error,
            Error::ArrayConversion {
                expected: 32,
                actual: 0
            }
        ));
    }

    #[test]
    fn test_error_response_structure() {
        let error_response = ErrorResponse {
            error_code: "validation_error".to_string(),
            message: "Invalid input parameters".to_string(),
        };

        // Test serialization
        let json = serde_json::to_string(&error_response).expect("Should serialize");
        assert!(json.contains("validation_error"));
        assert!(json.contains("Invalid input parameters"));

        // Test deserialization
        let deserialized: ErrorResponse = serde_json::from_str(&json).expect("Should deserialize");
        assert_eq!(deserialized.error_code, "validation_error");
        assert_eq!(deserialized.message, "Invalid input parameters");
    }

    #[test]
    fn test_reqwest_error_conversion() {
        // Note: These tests use mock errors since we can't easily create real
        // reqwest errors In practice, reqwest errors would be converted
        // automatically via the From trait

        // Test that the From<reqwest::Error> implementation exists and compiles
        // This ensures the conversion logic is syntactically correct
        // The implementation is tested indirectly through other integration
        // tests
    }

    #[test]
    fn test_error_debug_formatting() {
        let error = Error::api(500, "server_error".to_string(), "Internal error".to_string());
        let debug_str = format!("{:?}", error);
        assert!(debug_str.contains("Api"));
        assert!(debug_str.contains("status_code: 500"));

        let crypto_error = CryptoError::invalid_private_key("Invalid format");
        let crypto_debug = format!("{:?}", crypto_error);
        assert!(crypto_debug.contains("InvalidPrivateKey"));

        let config_error = ConfigError::invalid_network("Unknown network");
        let config_debug = format!("{:?}", config_error);
        assert!(config_debug.contains("InvalidNetwork"));
    }

    #[test]
    fn test_result_type_alias() {
        // Test that our Result type alias works correctly
        let success_result: Result<String> = Ok("success".to_string());
        assert!(success_result.is_ok());
        if let Ok(value) = success_result {
            assert_eq!(value, "success");
        }

        let error_result: Result<String> = Err(Error::custom("test error"));
        assert!(error_result.is_err());
        if let Err(error) = error_result {
            assert!(matches!(error, Error::Custom(_)));
        }
    }

    #[test]
    fn test_error_source_chain() {
        // Test that errors can be chained properly using the source() method from
        // std::error::Error
        let crypto_error = CryptoError::invalid_private_key("Base crypto error");
        let main_error = Error::Crypto(crypto_error);

        // The main error should have the crypto error as its source
        assert!(main_error.source().is_some());

        let config_error = ConfigError::invalid_timeout("Base config error");
        let main_error = Error::Config(config_error);

        // The main error should have the config error as its source
        assert!(main_error.source().is_some());
    }
}