bsv-wallet-toolbox 0.2.23

Pure Rust BSV wallet-toolbox implementation
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
//! Storage processAction implementation.
//!
//! Commits a signed transaction to storage: creates ProvenTxReq record,
//! updates Transaction and Output records, handles status state machine.
//! Ported from wallet-toolbox/src/storage/methods/processAction.ts.

use chrono::Utc;

use crate::error::{WalletError, WalletResult};
use crate::status::{ProvenTxReqStatus, TransactionStatus};
use crate::storage::action_types::{StorageProcessActionArgs, StorageProcessActionResult};
use crate::storage::find_args::{
    FindOutputsArgs, FindTransactionsArgs, OutputPartial, TransactionPartial,
};
use crate::storage::traits::reader_writer::StorageReaderWriter;
use crate::storage::{verify_one, TrxToken};
use crate::tables::ProvenTxReq;

/// Status pair for ProvenTxReq and Transaction during processAction.
struct ReqTxStatus {
    req: ProvenTxReqStatus,
    tx: TransactionStatus,
}

/// Commit a signed transaction to storage.
///
/// Validates the transaction exists, determines the correct status based on
/// nosend/delayed/sendWith flags, creates a ProvenTxReq record, and updates
/// Transaction and Output records.
pub async fn storage_process_action<S: StorageReaderWriter + ?Sized>(
    storage: &S,
    user_id: i64,
    args: &StorageProcessActionArgs,
    _trx: Option<&TrxToken>,
) -> WalletResult<StorageProcessActionResult> {
    let r = StorageProcessActionResult {
        send_with_results: None,
        not_delayed_results: None,
        log: None,
    };

    if !args.is_new_tx {
        // If not a new tx, nothing to commit to storage
        return Ok(r);
    }

    let reference = args
        .reference
        .as_ref()
        .ok_or_else(|| WalletError::InvalidOperation("reference is required".to_string()))?;
    let txid = args
        .txid
        .as_ref()
        .ok_or_else(|| WalletError::InvalidOperation("txid is required".to_string()))?;
    let raw_tx = args
        .raw_tx
        .as_ref()
        .ok_or_else(|| WalletError::InvalidOperation("rawTx is required".to_string()))?;

    // Find the existing transaction by user and reference
    let find_tx_args = FindTransactionsArgs {
        partial: TransactionPartial {
            user_id: Some(user_id),
            reference: Some(reference.clone()),
            ..Default::default()
        },
        ..Default::default()
    };
    let transaction = verify_one(storage.find_transactions(&find_tx_args, _trx).await?)?;

    // Transaction must be outgoing
    if !transaction.is_outgoing {
        return Err(WalletError::InvalidOperation(
            "isOutgoing is not true".to_string(),
        ));
    }

    // Transaction must have unsigned or unprocessed status
    if transaction.status != TransactionStatus::Unsigned
        && transaction.status != TransactionStatus::Unprocessed
    {
        return Err(WalletError::InvalidOperation(format!(
            "invalid transaction status {}",
            transaction.status
        )));
    }

    let transaction_id = transaction.transaction_id;

    // Determine status based on nosend, isDelayed, isSendWith flags
    // Following TS exactly from processAction.ts:
    //   isNoSend && !isSendWith -> req: nosend, tx: nosend
    //   !isNoSend && isDelayed  -> req: unsent, tx: unprocessed
    //   !isNoSend && !isDelayed -> req: unprocessed, tx: unprocessed
    let status = if args.is_no_send && !args.is_send_with {
        ReqTxStatus {
            req: ProvenTxReqStatus::Nosend,
            tx: TransactionStatus::Nosend,
        }
    } else if !args.is_no_send && args.is_delayed {
        ReqTxStatus {
            req: ProvenTxReqStatus::Unsent,
            tx: TransactionStatus::Unprocessed,
        }
    } else if !args.is_no_send && !args.is_delayed {
        ReqTxStatus {
            req: ProvenTxReqStatus::Unprocessed,
            tx: TransactionStatus::Unprocessed,
        }
    } else {
        return Err(WalletError::Internal(
            "logic error in status determination".to_string(),
        ));
    };

    // Begin a database transaction for atomicity
    let db_trx = storage.begin_transaction().await?;

    // Create the ProvenTxReq record
    let now = Utc::now().naive_utc();
    let notify = serde_json::json!({
        "transactionIds": [transaction_id]
    });
    let new_req = ProvenTxReq {
        created_at: now,
        updated_at: now,
        proven_tx_req_id: 0,
        proven_tx_id: None,
        status: status.req,
        attempts: 0,
        notified: false,
        txid: txid.clone(),
        batch: None,
        history: "[]".to_string(),
        notify: serde_json::to_string(&notify).unwrap_or_default(),
        raw_tx: raw_tx.clone(),
        input_beef: transaction.input_beef.clone(),
    };
    let _req_id = storage
        .insert_proven_tx_req(&new_req, Some(&db_trx))
        .await?;

    // Update Transaction record: set txid, status, and store rawTx
    // (rawTx is also stored in ProvenTxReq; keeping it on the Transaction
    // record enables get_valid_beef_for_txid to build BEEF for later
    // createAction calls that spend this transaction's outputs)
    let tx_update = crate::storage::find_args::TransactionPartial {
        txid: Some(txid.clone()),
        status: Some(status.tx),
        raw_tx: Some(raw_tx.clone()),
        ..Default::default()
    };
    storage
        .update_transaction(transaction_id, &tx_update, Some(&db_trx))
        .await?;

    // Update Output records: set txid on all outputs belonging to this transaction
    let find_outputs_args = FindOutputsArgs {
        partial: OutputPartial {
            user_id: Some(user_id),
            transaction_id: Some(transaction_id),
            ..Default::default()
        },
        ..Default::default()
    };
    let outputs = storage
        .find_outputs(&find_outputs_args, Some(&db_trx))
        .await?;
    for output in &outputs {
        let output_update = crate::storage::find_args::OutputPartial {
            txid: Some(txid.clone()),
            spendable: Some(true),
            ..Default::default()
        };
        storage
            .update_output(output.output_id, &output_update, Some(&db_trx))
            .await?;
    }

    // Commit the database transaction
    storage.commit_transaction(db_trx).await?;

    Ok(r)
}

#[cfg(test)]
#[cfg(feature = "sqlite")]
mod tests {
    use super::*;
    use crate::status::{ProvenTxReqStatus, TransactionStatus};
    use crate::storage::action_types::StorageProcessActionArgs;
    use crate::storage::find_args::{
        FindOutputsArgs, FindProvenTxReqsArgs, FindTransactionsArgs, OutputPartial,
        ProvenTxReqPartial, TransactionPartial,
    };
    use crate::storage::sqlx_impl::SqliteStorage;
    use crate::storage::traits::provider::StorageProvider;
    use crate::storage::traits::reader::StorageReader;
    use crate::storage::traits::reader_writer::StorageReaderWriter;
    use crate::storage::StorageConfig;
    use crate::tables::{Output, Transaction};
    use crate::types::Chain;
    use chrono::Utc;

    /// Helper to set up a test storage with a user, transaction, and outputs.
    async fn setup_test_storage() -> (SqliteStorage, i64, i64, String) {
        let config = StorageConfig {
            url: "sqlite::memory:".to_string(),
            ..Default::default()
        };
        let storage = SqliteStorage::new_sqlite(config, Chain::Test)
            .await
            .expect("create storage");
        storage.migrate_database().await.expect("migrate");
        storage.make_available().await.expect("make available");

        // Create a user
        let (user, _) = storage
            .find_or_insert_user("test_identity_key", None)
            .await
            .expect("create user");
        let user_id = user.user_id;

        // Create a default basket
        let _basket = storage
            .find_or_insert_output_basket(user_id, "default", None)
            .await
            .expect("create basket");

        // Create a transaction in unsigned state
        let now = Utc::now().naive_utc();
        let reference = "test_ref_001".to_string();
        let tx = Transaction {
            created_at: now,
            updated_at: now,
            transaction_id: 0,
            user_id,
            proven_tx_id: None,
            status: TransactionStatus::Unsigned,
            reference: reference.clone(),
            is_outgoing: true,
            satoshis: -1000,
            description: "test transaction".to_string(),
            version: Some(1),
            lock_time: Some(0),
            txid: None,
            input_beef: Some(vec![0xDE, 0xAD]),
            raw_tx: None,
        };
        let tx_id = storage
            .insert_transaction(&tx, None)
            .await
            .expect("insert transaction");

        // Create some outputs for this transaction
        let output = Output {
            created_at: now,
            updated_at: now,
            output_id: 0,
            user_id,
            transaction_id: tx_id,
            basket_id: Some(_basket.basket_id),
            spendable: false,
            change: true,
            output_description: Some("test output".to_string()),
            vout: 0,
            satoshis: 500,
            provided_by: crate::types::StorageProvidedBy::Storage,
            purpose: "change".to_string(),
            output_type: "P2PKH".to_string(),
            txid: None,
            sender_identity_key: None,
            derivation_prefix: None,
            derivation_suffix: None,
            custom_instructions: None,
            spent_by: None,
            sequence_number: None,
            spending_description: None,
            script_length: None,
            script_offset: None,
            locking_script: Some(vec![0x76, 0xa9]),
        };
        storage
            .insert_output(&output, None)
            .await
            .expect("insert output");

        (storage, user_id, tx_id, reference)
    }

    #[tokio::test]
    async fn test_process_action_creates_proven_tx_req() {
        let (storage, user_id, _tx_id, reference) = setup_test_storage().await;

        let fake_txid =
            "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234".to_string();
        let fake_raw_tx = vec![0x01, 0x00, 0x00, 0x00, 0x00];

        let args = StorageProcessActionArgs {
            is_new_tx: true,
            is_send_with: false,
            is_no_send: false,
            is_delayed: false,
            reference: Some(reference),
            txid: Some(fake_txid.clone()),
            raw_tx: Some(fake_raw_tx),
            send_with: vec![],
        };

        let result = storage_process_action(&storage, user_id, &args, None)
            .await
            .expect("process_action should succeed");
        assert!(result.send_with_results.is_none());

        // Verify ProvenTxReq was created
        let req_args = FindProvenTxReqsArgs {
            partial: ProvenTxReqPartial {
                txid: Some(fake_txid.clone()),
                ..Default::default()
            },
            ..Default::default()
        };
        let reqs = storage
            .find_proven_tx_reqs(&req_args, None)
            .await
            .expect("find reqs");
        assert_eq!(reqs.len(), 1);
        assert_eq!(reqs[0].status, ProvenTxReqStatus::Unprocessed);
        assert_eq!(reqs[0].txid, fake_txid);

        // Verify Transaction was updated with txid and unprocessed status
        let tx_args = FindTransactionsArgs {
            partial: TransactionPartial {
                transaction_id: Some(_tx_id),
                ..Default::default()
            },
            ..Default::default()
        };
        let txs = storage
            .find_transactions(&tx_args, None)
            .await
            .expect("find txs");
        assert_eq!(txs.len(), 1);
        assert_eq!(txs[0].txid, Some(fake_txid));
        assert_eq!(txs[0].status, TransactionStatus::Unprocessed);
    }

    #[tokio::test]
    async fn test_process_action_nosend_status() {
        let (storage, user_id, _tx_id, reference) = setup_test_storage().await;

        let fake_txid =
            "1111222233334444111122223333444411112222333344441111222233334444".to_string();
        let fake_raw_tx = vec![0x01, 0x00, 0x00, 0x00, 0x00];

        let args = StorageProcessActionArgs {
            is_new_tx: true,
            is_send_with: false,
            is_no_send: true,
            is_delayed: false,
            reference: Some(reference),
            txid: Some(fake_txid.clone()),
            raw_tx: Some(fake_raw_tx),
            send_with: vec![],
        };

        storage_process_action(&storage, user_id, &args, None)
            .await
            .expect("process_action nosend should succeed");

        // Verify ProvenTxReq has nosend status
        let req_args = FindProvenTxReqsArgs {
            partial: ProvenTxReqPartial {
                txid: Some(fake_txid.clone()),
                ..Default::default()
            },
            ..Default::default()
        };
        let reqs = storage
            .find_proven_tx_reqs(&req_args, None)
            .await
            .expect("find reqs");
        assert_eq!(reqs.len(), 1);
        assert_eq!(reqs[0].status, ProvenTxReqStatus::Nosend);

        // Verify Transaction has nosend status
        let tx_args = FindTransactionsArgs {
            partial: TransactionPartial {
                transaction_id: Some(_tx_id),
                ..Default::default()
            },
            ..Default::default()
        };
        let txs = storage
            .find_transactions(&tx_args, None)
            .await
            .expect("find txs");
        assert_eq!(txs[0].status, TransactionStatus::Nosend);
    }

    #[tokio::test]
    async fn test_process_action_delayed_status() {
        let (storage, user_id, _tx_id, reference) = setup_test_storage().await;

        let fake_txid =
            "5555666677778888555566667777888855556666777788885555666677778888".to_string();
        let fake_raw_tx = vec![0x01, 0x00, 0x00, 0x00, 0x00];

        let args = StorageProcessActionArgs {
            is_new_tx: true,
            is_send_with: false,
            is_no_send: false,
            is_delayed: true,
            reference: Some(reference),
            txid: Some(fake_txid.clone()),
            raw_tx: Some(fake_raw_tx),
            send_with: vec![],
        };

        storage_process_action(&storage, user_id, &args, None)
            .await
            .expect("process_action delayed should succeed");

        // Verify ProvenTxReq has unsent status
        let req_args = FindProvenTxReqsArgs {
            partial: ProvenTxReqPartial {
                txid: Some(fake_txid.clone()),
                ..Default::default()
            },
            ..Default::default()
        };
        let reqs = storage
            .find_proven_tx_reqs(&req_args, None)
            .await
            .expect("find reqs");
        assert_eq!(reqs.len(), 1);
        assert_eq!(reqs[0].status, ProvenTxReqStatus::Unsent);

        // Verify Transaction has unprocessed status (delayed keeps tx as unprocessed)
        let tx_args = FindTransactionsArgs {
            partial: TransactionPartial {
                transaction_id: Some(_tx_id),
                ..Default::default()
            },
            ..Default::default()
        };
        let txs = storage
            .find_transactions(&tx_args, None)
            .await
            .expect("find txs");
        assert_eq!(txs[0].status, TransactionStatus::Unprocessed);
    }

    #[tokio::test]
    async fn test_process_action_updates_output_txid() {
        let (storage, user_id, _tx_id, reference) = setup_test_storage().await;

        let fake_txid =
            "aaaa1111bbbb2222aaaa1111bbbb2222aaaa1111bbbb2222aaaa1111bbbb2222".to_string();
        let fake_raw_tx = vec![0x01, 0x00, 0x00, 0x00, 0x00];

        let args = StorageProcessActionArgs {
            is_new_tx: true,
            is_send_with: false,
            is_no_send: false,
            is_delayed: false,
            reference: Some(reference),
            txid: Some(fake_txid.clone()),
            raw_tx: Some(fake_raw_tx),
            send_with: vec![],
        };

        storage_process_action(&storage, user_id, &args, None)
            .await
            .expect("process_action should succeed");

        // Verify outputs were updated with txid
        let output_args = FindOutputsArgs {
            partial: OutputPartial {
                user_id: Some(user_id),
                transaction_id: Some(_tx_id),
                ..Default::default()
            },
            ..Default::default()
        };
        let outputs = storage
            .find_outputs(&output_args, None)
            .await
            .expect("find outputs");
        assert_eq!(outputs.len(), 1);
        assert_eq!(outputs[0].txid, Some(fake_txid));
        assert!(outputs[0].spendable);
    }
}