ootle_rs/types/transaction/
request.rs1use tari_ootle_transaction::{Transaction, UnsealedTransaction, UnsignedTransaction};
5
6use crate::{
7 transaction::TransactionSealSigner,
8 wallet::{TransactionAuthorization, WalletResult},
9};
10
11#[derive(Clone, Debug, Default)]
12pub struct Initial;
13pub struct WithTx(UnsignedTransaction);
14
15#[derive(Clone, Debug, Default)]
16pub struct TransactionRequest<State = Initial> {
17 state: State,
18 authorizations: Vec<TransactionAuthorization>,
19}
20
21impl TransactionRequest<Initial> {
22 pub fn new() -> Self {
23 Self {
24 state: Initial,
25 authorizations: Vec::new(),
26 }
27 }
28
29 pub fn with_transaction(self, builder: UnsignedTransaction) -> TransactionRequest<WithTx> {
30 TransactionRequest {
31 state: WithTx(builder),
32 authorizations: self.authorizations,
33 }
34 }
35}
36
37impl<State> TransactionRequest<State> {
38 pub fn add_authorization(mut self, auth: TransactionAuthorization) -> Self {
39 self.authorizations.push(auth);
40 self
41 }
42
43 pub fn with_authorizations<I>(mut self, auths: I) -> Self
44 where I: IntoIterator<Item = TransactionAuthorization> {
45 self.authorizations.extend(auths);
46 self
47 }
48}
49
50impl TransactionRequest<WithTx> {
51 pub fn build_unsealed(self) -> UnsealedTransaction {
52 self.state.0.finish()
53 }
54
55 pub async fn build(self, seal_signer: &dyn TransactionSealSigner) -> WalletResult<Transaction> {
56 let builder = self.state.0;
57 let unsealed = builder.with_signatures(self.authorizations.into_iter().map(|a| a.into_signature()).collect());
58 let final_tx = seal_signer.seal_transaction(unsealed).await?;
59 Ok(final_tx)
60 }
61}