identity_iota_core/rebased/
transaction.rs

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
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::ops::Deref;

#[cfg(not(target_arch = "wasm32"))]
use identity_iota_interaction::rpc_types::IotaTransactionBlockResponse;
#[cfg(not(target_arch = "wasm32"))]
use identity_iota_interaction::types::transaction::ProgrammableTransaction;
#[cfg(target_arch = "wasm32")]
use iota_interaction_ts::ProgrammableTransaction;

use crate::iota_interaction_adapter::IotaTransactionBlockResponseAdaptedTraitObj;
use crate::rebased::client::IdentityClient;
use crate::rebased::Error;
use async_trait::async_trait;
use identity_iota_interaction::IotaKeySignature;
use identity_iota_interaction::ProgrammableTransactionBcs;
use secret_storage::Signer;

/// The output type of a [`Transaction`].
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone)]
pub struct TransactionOutput<T> {
  /// The parsed Transaction output. See [`Transaction::Output`].
  pub output: T,
  /// The "raw" transaction execution response received.
  pub response: IotaTransactionBlockResponse,
}

#[cfg(not(target_arch = "wasm32"))]
impl<T> Deref for TransactionOutput<T> {
  type Target = T;
  fn deref(&self) -> &Self::Target {
    &self.output
  }
}

/// Interface for operations that interact with the ledger through transactions.
#[cfg(not(target_arch = "wasm32"))]
#[async_trait::async_trait]
pub trait Transaction: Sized {
  /// The result of performing the operation.
  type Output;

  /// Executes this operation using the given `client` and an optional `gas_budget`.
  /// If no value for `gas_budget` is provided, an estimated value will be used.
  async fn execute_with_opt_gas<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutput<Self::Output>, Error>;

  /// Executes this operation using `client`.
  async fn execute<S: Signer<IotaKeySignature> + Sync>(
    self,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutput<Self::Output>, Error> {
    self.execute_with_opt_gas(None, client).await
  }

  /// Executes this operation using `client` and a well defined `gas_budget`.
  async fn execute_with_gas<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: u64,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutput<Self::Output>, Error> {
    self.execute_with_opt_gas(Some(gas_budget), client).await
  }
}

pub(crate) struct TransactionOutputInternal<T> {
  pub output: T,
  pub response: IotaTransactionBlockResponseAdaptedTraitObj,
}

impl<T> Deref for TransactionOutputInternal<T> {
  type Target = T;
  fn deref(&self) -> &Self::Target {
    &self.output
  }
}

#[cfg(not(target_arch = "wasm32"))]
impl<T> From<TransactionOutputInternal<T>> for TransactionOutput<T> {
  fn from(value: TransactionOutputInternal<T>) -> Self {
    let TransactionOutputInternal::<T> {
      output: out,
      response: internal_response,
    } = value;
    let response = internal_response.clone_native_response();
    TransactionOutput { output: out, response }
  }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub(crate) trait TransactionInternal: Sized {
  type Output;

  async fn execute_with_opt_gas_internal<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error>;

  #[cfg(target_arch = "wasm32")]
  async fn execute<S: Signer<IotaKeySignature> + Sync>(
    self,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error> {
    self.execute_with_opt_gas_internal(None, client).await
  }

  #[cfg(target_arch = "wasm32")]
  async fn execute_with_gas<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: u64,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error> {
    self.execute_with_opt_gas_internal(Some(gas_budget), client).await
  }
}

#[cfg(not(target_arch = "wasm32"))]
#[async_trait::async_trait]
impl<T: TransactionInternal<Output = O> + Send, O> Transaction for T {
  type Output = O;

  async fn execute_with_opt_gas<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutput<O>, Error> {
    let tx_output = self.execute_with_opt_gas_internal(gas_budget, client).await?;
    Ok(tx_output.into())
  }
}

#[cfg(not(target_arch = "wasm32"))]
#[async_trait::async_trait]
impl TransactionInternal for ProgrammableTransaction {
  type Output = ();
  async fn execute_with_opt_gas_internal<S>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error>
  where
    S: Signer<IotaKeySignature> + Sync,
  {
    let tx_bcs = bcs::to_bytes(&self)?;
    let response = client.execute_transaction(tx_bcs, gas_budget).await?;
    Ok(TransactionOutputInternal { output: (), response })
  }
}

#[cfg(target_arch = "wasm32")]
#[async_trait::async_trait(?Send)]
impl TransactionInternal for ProgrammableTransaction {
  type Output = ();
  async fn execute_with_opt_gas_internal<S>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error>
  where
    S: Signer<IotaKeySignature> + Sync,
  {
    unimplemented!("TransactionInternal::execute_with_opt_gas_internal for ProgrammableTransaction");
  }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl TransactionInternal for ProgrammableTransactionBcs {
  type Output = ();

  async fn execute_with_opt_gas_internal<S: Signer<IotaKeySignature> + Sync>(
    self,
    gas_budget: Option<u64>,
    client: &IdentityClient<S>,
  ) -> Result<TransactionOutputInternal<Self::Output>, Error> {
    // For wasm32 targets, the following line will result in a compiler error[E0412]
    // TODO: Implement wasm-bindings for the ProgrammableTransaction TS equivalent
    //       and use them to do the BCS serialization
    let self_tx = bcs::from_bytes::<ProgrammableTransaction>(&self)?;
    self_tx.execute_with_opt_gas_internal(gas_budget, client).await
  }
}

/// Interface to describe an operation that can eventually
/// be turned into a [`Transaction`], given the right input.
pub trait ProtoTransaction {
  /// The input required by this operation.
  type Input;
  /// This operation's next state. Can either be another [`ProtoTransaction`]
  /// or a whole [`Transaction`] ready to be executed.
  type Tx: ProtoTransaction;

  /// Feed this operation with its required input, advancing its
  /// state to another [`ProtoTransaction`] that may or may not
  /// be ready for execution.
  fn with(self, input: Self::Input) -> Self::Tx;
}

// Every Transaction is a QuasiTransaction that requires no input
// and that has itself as its next state.
impl<T> ProtoTransaction for T
where
  T: TransactionInternal,
{
  type Input = ();
  type Tx = Self;

  fn with(self, _: Self::Input) -> Self::Tx {
    self
  }
}