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
//! Transaction-related API operations.

use std::{str::FromStr, time::Duration};

use alloy_primitives::B256;
use om_primitives_types::transaction::{envelope::RawTransactionEnvelope, payload::PaymentPayload};
use om_rest_types::{
    FinalizedTransaction, Transaction,
    requests::{FeeEstimateRequest, PaymentTransactionRequest},
    responses::{FeeEstimate, TransactionReceipt, TransactionResponse},
};
use tokio::time::{Instant, sleep};

use crate::{
    client::{
        Client,
        config::{
            API_VERSION, api_path,
            endpoints::transactions::{BY_HASH, ESTIMATE_FEE, FINALIZED_BY_HASH, PAYMENT, RAW, RECEIPT_BY_HASH},
        },
    },
    crypto::sign_transaction_payload,
    error::{Error, Result},
    utils::{signature_hash_for_counter_sign, verify_bls_aggregate_signature},
};

const DEFAULT_RECEIPT_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_RECEIPT_POLL_INTERVAL: Duration = Duration::from_millis(50);

impl Client {
    /// Send a payment transaction.
    ///
    /// # Arguments
    ///
    /// * `payload` - Payment transaction parameters
    /// * `private_key` - Private key for signing the transaction
    ///
    /// # Returns
    ///
    /// The payment response containing the transaction hash.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use std::str::FromStr;
    ///
    /// use alloy_primitives::{Address, U256};
    /// use onemoney_protocol::{Client, NamedChain, PaymentPayload};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::mainnet()?;
    ///
    ///     let payload = PaymentPayload {
    ///         chain_id: NamedChain::TESTNET_CHAIN_ID,
    ///         nonce: 0,
    ///         recipient: Address::from_str("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0")?,
    ///         value: U256::from(1000000000000000000u64), // 1 token
    ///         token: Address::from_str("0x1234567890abcdef1234567890abcdef12345678")?,
    ///     };
    ///
    ///     let private_key = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
    ///     let result = client.send_payment(payload, private_key).await?;
    ///     println!("Transaction hash: {}", result.hash);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn send_payment(&self, data: PaymentPayload, private_key: &str) -> Result<TransactionResponse> {
        let signature = sign_transaction_payload(&data, private_key)?;
        let request = PaymentTransactionRequest { data, signature };

        let path = api_path(PAYMENT);
        self.post(&path, &request).await
    }

    /// Submit a raw transaction envelope.
    ///
    /// This is used for advanced transaction types such as multi-sig creation
    /// and multi-sig payments.
    pub async fn submit_raw_transaction(&self, envelope: RawTransactionEnvelope) -> Result<TransactionResponse> {
        let path = api_path(RAW);
        self.post(&path, &envelope).await
    }

    /// Get transaction by hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - Transaction hash
    ///
    /// # Returns
    ///
    /// The transaction details.
    pub async fn get_transaction_by_hash(&self, hash: &str) -> Result<Transaction> {
        let path = format!("{}{}?hash={}", API_VERSION, BY_HASH, hash);
        self.get(&path).await
    }

    /// Get transaction receipt by hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - Transaction hash
    ///
    /// # Returns
    ///
    /// The transaction receipt.
    pub async fn get_transaction_receipt_by_hash(&self, hash: &str) -> Result<TransactionReceipt> {
        let path = format!("{}{}?hash={}", API_VERSION, RECEIPT_BY_HASH, hash);
        self.get(&path).await
    }

    /// Wait for a transaction receipt using the default timeout.
    ///
    /// This method polls the receipt endpoint every 50ms for up to 30 seconds.
    pub async fn wait_for_transaction_receipt(&self, hash: &str) -> Result<TransactionReceipt> {
        self.wait_for_transaction_receipt_with_timeout(hash, DEFAULT_RECEIPT_TIMEOUT)
            .await
    }

    /// Wait for a transaction receipt with a custom timeout.
    ///
    /// # Arguments
    /// * `hash` - Transaction hash
    /// * `timeout` - Maximum duration to poll before returning a timeout error
    pub async fn wait_for_transaction_receipt_with_timeout(
        &self,
        hash: &str,
        timeout: Duration,
    ) -> Result<TransactionReceipt> {
        let hash_owned = hash.to_string();
        let request_path = format!("{}{}?hash={}", API_VERSION, RECEIPT_BY_HASH, hash);

        poll_for_transaction_receipt(
            || async { self.get_transaction_receipt_by_hash(&hash_owned).await },
            request_path,
            timeout,
            DEFAULT_RECEIPT_POLL_INTERVAL,
        )
        .await
    }

    /// Estimate transaction fee.
    ///
    /// # Arguments
    ///
    /// * `request` - Fee estimation parameters
    ///
    /// # Returns
    ///
    /// The estimated fee.
    pub async fn estimate_fee(&self, request: FeeEstimateRequest) -> Result<FeeEstimate> {
        let path = api_path(ESTIMATE_FEE);
        let full_path = format!(
            "{}?from={}&to={}&token={}&value={}",
            path, request.from, request.to, request.token, request.value,
        );
        self.get(&full_path).await
    }

    /// Get finalized transaction and receipt by hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - Transaction hash
    ///
    /// # Returns
    ///
    /// The finalized transaction and receipt.
    pub async fn get_finalized_transaction_by_hash(&self, hash: &str) -> Result<FinalizedTransaction> {
        let path = format!("{}{}?hash={}", API_VERSION, FINALIZED_BY_HASH, hash);
        self.get(&path).await
    }

    /// Get and verify finalized transaction by hash with BLS signature
    /// verification.
    ///
    /// This is a convenience method that fetches the finalized transaction and
    /// automatically verifies the BLS aggregate signature from validators.
    ///
    /// # Arguments
    ///
    /// * `hash` - Transaction hash (with or without 0x prefix)
    ///
    /// # Returns
    ///
    /// The finalized transaction if signature verification passes, error
    /// otherwise.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use onemoney_protocol::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::testnet()?;
    ///
    ///     // This will fetch AND verify the transaction
    ///     let finalized_tx = client
    ///         .get_and_verify_finalized_transaction_by_hash("0x1234...")
    ///         .await?;
    ///
    ///     println!("Transaction verified! Epoch: {}", finalized_tx.epoch);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_and_verify_finalized_transaction_by_hash(&self, hash: &str) -> Result<FinalizedTransaction> {
        // Fetch the finalized transaction
        let finalized_tx = self.get_finalized_transaction_by_hash(hash).await?;

        // Parse transaction hash
        let tx_hash =
            B256::from_str(hash).map_err(|e| Error::validation("hash", format!("Invalid transaction hash: {}", e)))?;

        // Compute the counter-sign message hash
        let message_hash = signature_hash_for_counter_sign(&tx_hash, &finalized_tx.epoch);

        // Verify the BLS signature
        verify_bls_aggregate_signature(&message_hash, &finalized_tx.counter_signature)?;

        Ok(finalized_tx)
    }
}

async fn poll_for_transaction_receipt<F, Fut>(
    mut fetch_receipt: F,
    request_path: String,
    timeout: Duration,
    poll_interval: Duration,
) -> Result<TransactionReceipt>
where
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<TransactionReceipt>>,
{
    if timeout.is_zero() {
        return Err(Error::invalid_parameter("timeout", "Timeout must be greater than zero"));
    }
    if poll_interval.is_zero() {
        return Err(Error::invalid_parameter(
            "poll_interval",
            "Poll interval must be greater than zero",
        ));
    }

    let start = Instant::now();

    loop {
        match fetch_receipt().await {
            Ok(receipt) => return Ok(receipt),
            Err(err) => {
                if !matches!(err, Error::ResourceNotFound { .. }) {
                    return Err(err);
                }
            }
        }

        let elapsed = start.elapsed();
        if elapsed >= timeout {
            return Err(Error::request_timeout(
                request_path.clone(),
                duration_to_millis(timeout),
            ));
        }

        if let Some(remaining) = timeout.checked_sub(elapsed) {
            let sleep_duration = poll_interval.min(remaining);
            sleep(sleep_duration).await;
        } else {
            return Err(Error::request_timeout(
                request_path.clone(),
                duration_to_millis(timeout),
            ));
        }
    }
}

fn duration_to_millis(duration: Duration) -> u64 {
    duration.as_millis().min(u128::from(u64::MAX)) as u64
}

#[cfg(test)]
mod tests {
    use std::{collections::VecDeque, str::FromStr, sync::Mutex, time::Duration};

    use alloy_primitives::{Address, B256, U256};

    use super::*;
    use crate::NamedChain;

    #[test]
    fn test_payment_payload_alloy_rlp() {
        use alloy_rlp::Encodable as AlloyEncodable;

        let payload = PaymentPayload {
            chain_id: NamedChain::TESTNET_CHAIN_ID,
            nonce: 0,
            recipient: Address::from_str("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0")
                .expect("Test data should be valid"),
            value: U256::from(1000000000000000000u64),
            token: Address::from_str("0x1234567890abcdef1234567890abcdef12345678").expect("Test data should be valid"),
        };

        let mut encoded = Vec::new();
        payload.encode(&mut encoded);
        assert!(!encoded.is_empty());
    }

    #[test]
    fn test_fee_estimate_request() {
        let request = FeeEstimateRequest {
            from: "0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0".to_string(),
            to: "0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc1".to_string(),
            token: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
            value: "1000000000000000000".to_string(),
        };

        // Test serialization
        let json = serde_json::to_string(&request).expect("Should serialize");
        assert!(json.contains("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0"));
        assert!(json.contains("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc1"));
        assert!(json.contains("0x1234567890abcdef1234567890abcdef12345678"));
        assert!(json.contains("1000000000000000000"));
    }

    #[test]
    fn test_finalized_transaction_api_path_construction() {
        let hash = "0x902006665c369834a0cf52eea2780f934a90b3c86a3918fb57371ac1fbbd7777";
        let expected_path = format!("{}{}?hash={}", API_VERSION, FINALIZED_BY_HASH, hash);

        assert!(expected_path.contains("/v1"));
        assert!(expected_path.contains("/transactions/finalized/by_hash"));
        assert!(expected_path.contains("hash=0x902006665c369834a0cf52eea2780f934a90b3c86a3918fb57371ac1fbbd7777"));
    }

    #[test]
    fn test_finalized_transaction_structure() {
        use alloy_primitives::{Address, B256};
        use om_rest_types::{FinalizedTransaction, RestBlsAggregateSignature, responses::TransactionReceipt};

        let finalized_tx = FinalizedTransaction {
            epoch: 100,
            receipt: TransactionReceipt {
                success: true,
                transaction_hash: B256::from_str("0x902006665c369834a0cf52eea2780f934a90b3c86a3918fb57371ac1fbbd7777")
                    .expect("Test data should be valid"),
                transaction_index: Some(5),
                checkpoint_hash: Some(
                    B256::from_str("0x20e081da293ae3b81e30f864f38f6911663d7f2cf98337fca38db3cf5bbe7a8f")
                        .expect("Test data should be valid"),
                ),
                checkpoint_number: Some(1500),
                fee_used: 1000000,
                from: Address::from_str("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0")
                    .expect("Test data should be valid"),
                recipient: Some(
                    Address::from_str("0x1234567890abcdef1234567890abcdef12345678").expect("Test data should be valid"),
                ),
                token_address: None,
                success_info: None,
            },
            counter_signature: RestBlsAggregateSignature::default(),
        };

        assert_eq!(finalized_tx.epoch, 100);
        assert!(finalized_tx.receipt.success);
        assert_eq!(finalized_tx.receipt.fee_used, 1000000);
    }

    #[test]
    fn test_finalized_transaction_json_output() {
        use alloy_primitives::{Address, B256};
        use om_rest_types::{FinalizedTransaction, RestBlsAggregateSignature, responses::TransactionReceipt};

        let finalized_tx = FinalizedTransaction {
            epoch: 200,
            receipt: TransactionReceipt {
                success: true,
                transaction_hash: B256::from_str("0x902006665c369834a0cf52eea2780f934a90b3c86a3918fb57371ac1fbbd7777")
                    .expect("Test data should be valid"),
                transaction_index: Some(0),
                checkpoint_hash: Some(
                    B256::from_str("0x20e081da293ae3b81e30f864f38f6911663d7f2cf98337fca38db3cf5bbe7a8f")
                        .expect("Test data should be valid"),
                ),
                checkpoint_number: Some(1500),
                fee_used: 1000000,
                from: Address::from_str("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0")
                    .expect("Test data should be valid"),
                recipient: Some(
                    Address::from_str("0x1234567890abcdef1234567890abcdef12345678").expect("Test data should be valid"),
                ),
                token_address: Some(
                    Address::from_str("0xabcdef1234567890abcdef1234567890abcdef12").expect("Test data should be valid"),
                ),
                success_info: None,
            },
            counter_signature: RestBlsAggregateSignature::new(
                "0xff".to_string(),
                "0x1234".to_string(),
                vec!["0xpubkey1".to_string()],
            ),
        };

        let json = serde_json::to_string(&finalized_tx).expect("Should serialize to JSON");

        assert!(json.contains("\"epoch\":200"));
        assert!(
            json.contains(
                "\"transaction_hash\":\"0x902006665c369834a0cf52eea2780f934a90b3c86a3918fb57371ac1fbbd7777\""
            )
        );
        assert!(json.contains("\"success\":true"));
        assert!(json.contains("\"fee_used\":\"1000000\""));
        assert!(json.contains("\"counter_signature\""));
    }

    fn sample_receipt(hash: &str) -> TransactionReceipt {
        TransactionReceipt {
            success: true,
            transaction_hash: B256::from_str(hash).expect("valid hash"),
            transaction_index: Some(0),
            checkpoint_hash: None,
            checkpoint_number: Some(42),
            fee_used: 1,
            from: Address::from_str("0x0000000000000000000000000000000000000001").expect("valid address"),
            recipient: Some(Address::from_str("0x0000000000000000000000000000000000000002").expect("valid address")),
            token_address: Some(
                Address::from_str("0x0000000000000000000000000000000000000003").expect("valid address"),
            ),
            success_info: None,
        }
    }

    #[tokio::test]
    async fn test_wait_for_transaction_receipt_eventually_succeeds() {
        let tx_hash = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        let request_path = format!("/v1/transactions/receipt/by_hash?hash={tx_hash}");

        let responses = Mutex::new(VecDeque::from([
            Err(Error::resource_not_found("receipt", "pending")),
            Ok(sample_receipt(tx_hash)),
        ]));

        let receipt = poll_for_transaction_receipt(
            || {
                let result = responses
                    .lock()
                    .expect("lock poisoned")
                    .pop_front()
                    .expect("response available");
                async move { result }
            },
            request_path,
            Duration::from_millis(100),
            Duration::from_millis(10),
        )
        .await
        .expect("should eventually succeed");

        assert!(receipt.success);
        assert_eq!(receipt.checkpoint_number, Some(42));
        assert_eq!(
            receipt.recipient,
            Some(Address::from_str("0x0000000000000000000000000000000000000002").unwrap())
        );
    }

    #[tokio::test]
    async fn test_wait_for_transaction_receipt_respects_errors() {
        let request_path = "/v1/transactions/receipt/by_hash?hash=0xbb".to_string();
        let responses = Mutex::new(VecDeque::from([Err(Error::http_transport("boom", Some(500)))]));

        let err = poll_for_transaction_receipt(
            || {
                let result = responses
                    .lock()
                    .expect("lock poisoned")
                    .pop_front()
                    .expect("response available");
                async move { result }
            },
            request_path,
            Duration::from_millis(50),
            Duration::from_millis(10),
        )
        .await
        .expect_err("should propagate error");

        assert!(matches!(err, Error::HttpTransport { .. }));
    }

    #[tokio::test]
    async fn test_wait_for_transaction_receipt_with_zero_timeout_is_rejected() {
        let err = poll_for_transaction_receipt(
            || async {
                Ok(sample_receipt(
                    "0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
                ))
            },
            "/v1/transactions/receipt/by_hash?hash=0xcc".to_string(),
            Duration::from_secs(0),
            Duration::from_millis(10),
        )
        .await
        .expect_err("zero timeout invalid");

        assert!(matches!(err, Error::InvalidParameter { .. }));
    }
}