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

use std::marker::PhantomData;

use crate::iota_interaction_adapter::AdapterError;
use crate::iota_interaction_adapter::AdapterNativeResponse;
use crate::iota_interaction_adapter::IdentityMoveCallsAdapter;
use crate::iota_interaction_adapter::IotaTransactionBlockResponseAdapter;
use identity_iota_interaction::IdentityMoveCalls;
use identity_iota_interaction::IotaKeySignature;
use identity_iota_interaction::IotaTransactionBlockResponseT;

use crate::rebased::client::IdentityClient;
use crate::rebased::migration::Proposal;
use crate::rebased::transaction::ProtoTransaction;
use crate::rebased::transaction::TransactionInternal;
use crate::rebased::transaction::TransactionOutputInternal;
use crate::rebased::Error;
use async_trait::async_trait;
use identity_iota_interaction::rpc_types::IotaObjectRef;
use identity_iota_interaction::rpc_types::OwnedObjectRef;
use identity_iota_interaction::types::base_types::IotaAddress;
use identity_iota_interaction::types::base_types::ObjectID;
use identity_iota_interaction::types::transaction::Argument;
use identity_iota_interaction::types::TypeTag;
use identity_iota_interaction::MoveType;
use secret_storage::Signer;
use serde::Deserialize;
use serde::Serialize;

use super::CreateProposalTx;
use super::ExecuteProposalTx;
use super::OnChainIdentity;
use super::ProposalBuilder;
use super::ProposalT;
use super::UserDrivenTx;

cfg_if::cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
      use iota_interaction_ts::NativeTsTransactionBuilderBindingWrapper as Ptb;
      /// Instances of ControllerIntentFnT can be used as user-provided function to describe how
      /// a borrowed identity's controller capability will be used.
      pub trait ControllerIntentFnT: FnOnce(&mut Ptb, &Argument) {}
      impl<T> ControllerIntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
      #[allow(unreachable_pub)]
      /// Boxed dynamic trait object of {@link ControllerIntentFnT}
      pub type ControllerIntentFn = Box<dyn ControllerIntentFnT + Send>;
    } else {
      use identity_iota_interaction::types::programmable_transaction_builder::ProgrammableTransactionBuilder as Ptb;
      /// Instances of ControllerIntentFnT can be used as user-provided function to describe how
      /// a borrowed identity's controller capability will be used.
      pub trait ControllerIntentFnT: FnOnce(&mut Ptb, &Argument) {}
      impl<T> ControllerIntentFnT for T where T: FnOnce(&mut Ptb, &Argument) {}
      #[allow(unreachable_pub)]
      /// Boxed dynamic trait object of {@link ControllerIntentFnT}
      pub type ControllerIntentFn = Box<dyn ControllerIntentFnT + Send>;
    }
}

/// Borrow an [`OnChainIdentity`]'s controller capability to exert control on
/// a sub-owned identity.
#[derive(Debug, Deserialize, Serialize)]
pub struct ControllerExecution<F = ControllerIntentFn> {
  controller_cap: ObjectID,
  identity: IotaAddress,
  #[serde(skip, default = "Option::default")]
  intent_fn: Option<F>,
}

/// A [`ControllerExecution`] action coupled with a user-provided function to describe how
/// the borrowed identity's controller capability will be used.
pub struct ControllerExecutionWithIntent<F>(ControllerExecution<F>)
where
  F: FnOnce(&mut Ptb, &Argument);

impl<F> ControllerExecutionWithIntent<F>
where
  F: ControllerIntentFnT,
{
  fn new(action: ControllerExecution<F>) -> Self {
    debug_assert!(action.intent_fn.is_some());
    Self(action)
  }
}

impl<F> ControllerExecution<F> {
  /// Creates a new [`ControllerExecution`] action, allowing a controller of `identity` to
  /// borrow `identity`'s controller cap for a transaction.
  pub fn new(controller_cap: ObjectID, identity: &OnChainIdentity) -> Self {
    Self {
      controller_cap,
      identity: identity.id().into(),
      intent_fn: None,
    }
  }

  /// Specifies how the borrowed `ControllerCap` should be used in the transaction.
  /// This is only useful if the controller creating this proposal has enough voting
  /// power to carry out it out immediately.
  pub fn with_intent<F1>(self, intent_fn: F1) -> ControllerExecution<F1>
  where
    F1: FnOnce(&mut Ptb, &Argument),
  {
    let Self {
      controller_cap,
      identity,
      ..
    } = self;
    ControllerExecution {
      controller_cap,
      identity,
      intent_fn: Some(intent_fn),
    }
  }
}

impl<'i, F> ProposalBuilder<'i, ControllerExecution<F>> {
  /// Specifies how the borrowed `ControllerCap` should be used in the transaction.
  /// This is only useful if the controller creating this proposal has enough voting
  /// power to carry out it out immediately.
  pub fn with_intent<F1>(self, intent_fn: F1) -> ProposalBuilder<'i, ControllerExecution<F1>>
  where
    F1: FnOnce(&mut Ptb, &Argument),
  {
    let ProposalBuilder {
      identity,
      expiration,
      action,
    } = self;
    ProposalBuilder {
      identity,
      expiration,
      action: action.with_intent(intent_fn),
    }
  }
}

impl MoveType for ControllerExecution {
  fn move_type(package: ObjectID) -> TypeTag {
    use std::str::FromStr;

    TypeTag::from_str(&format!("{package}::controller_proposal::ControllerExecution")).expect("valid move type")
  }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<F> ProposalT for Proposal<ControllerExecution<F>>
where
  F: ControllerIntentFnT + Send,
{
  type Action = ControllerExecution<F>;
  type Output = ();
  type Response = IotaTransactionBlockResponseAdapter;

  async fn create<'i, S>(
    action: Self::Action,
    expiration: Option<u64>,
    identity: &'i mut OnChainIdentity,
    client: &IdentityClient<S>,
  ) -> Result<CreateProposalTx<'i, Self::Action>, Error>
  where
    S: Signer<IotaKeySignature> + Sync,
  {
    let identity_ref = client
      .get_object_ref_by_id(identity.id())
      .await?
      .expect("identity exists on-chain");
    let controller_cap_ref = identity.get_controller_cap(client).await?;
    let chained_execution = action.intent_fn.is_some()
      && identity
        .controller_voting_power(controller_cap_ref.0)
        .expect("is an identity's controller")
        >= identity.threshold();

    let tx = if chained_execution {
      let borrowing_controller_cap_ref = client
        .get_object_ref_by_id(action.controller_cap)
        .await?
        .map(|OwnedObjectRef { reference, .. }| {
          let IotaObjectRef {
            object_id,
            version,
            digest,
          } = reference;
          (object_id, version, digest)
        })
        .ok_or_else(|| Error::ObjectLookup(format!("object {} doesn't exist", action.controller_cap)))?;

      IdentityMoveCallsAdapter::create_and_execute_controller_execution(
        identity_ref,
        controller_cap_ref,
        expiration,
        borrowing_controller_cap_ref,
        action.intent_fn.unwrap(),
        client.package_id(),
      )
    } else {
      IdentityMoveCallsAdapter::propose_controller_execution(
        identity_ref,
        controller_cap_ref,
        action.controller_cap,
        expiration,
        client.package_id(),
      )
    }
    .map_err(|e| Error::TransactionBuildingFailed(e.to_string()))?;

    Ok(CreateProposalTx {
      identity,
      tx,
      chained_execution,
      _action: PhantomData,
    })
  }

  async fn into_tx<'i, S>(
    self,
    identity: &'i mut OnChainIdentity,
    _: &IdentityClient<S>,
  ) -> Result<UserDrivenTx<'i, Self::Action>, Error>
  where
    S: Signer<IotaKeySignature> + Sync,
  {
    let proposal_id = self.id();
    let controller_execution_action = self.into_action();

    Ok(UserDrivenTx {
      identity,
      proposal_id,
      action: controller_execution_action,
    })
  }

  fn parse_tx_effects_internal(
    _tx_response: &dyn IotaTransactionBlockResponseT<Error = AdapterError, NativeResponse = AdapterNativeResponse>,
  ) -> Result<Self::Output, Error> {
    Ok(())
  }
}

impl<'i, F> UserDrivenTx<'i, ControllerExecution<F>> {
  /// Defines how the borrowed assets should be used.
  pub fn with_intent<F1>(self, intent_fn: F1) -> UserDrivenTx<'i, ControllerExecutionWithIntent<F1>>
  where
    F1: ControllerIntentFnT,
  {
    let UserDrivenTx {
      identity,
      action,
      proposal_id,
    } = self;

    UserDrivenTx {
      identity,
      proposal_id,
      action: ControllerExecutionWithIntent::new(action.with_intent(intent_fn)),
    }
  }
}

impl<'i, F> ProtoTransaction for UserDrivenTx<'i, ControllerExecution<F>> {
  type Input = ControllerIntentFn;
  type Tx = UserDrivenTx<'i, ControllerExecutionWithIntent<ControllerIntentFn>>;

  fn with(self, input: Self::Input) -> Self::Tx {
    self.with_intent(input)
  }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<F> TransactionInternal for UserDrivenTx<'_, ControllerExecutionWithIntent<F>>
where
  F: ControllerIntentFnT + Send,
{
  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 Self {
      identity,
      action,
      proposal_id,
    } = self;
    let identity_ref = client
      .get_object_ref_by_id(identity.id())
      .await?
      .expect("identity exists on-chain");
    let controller_cap_ref = identity.get_controller_cap(client).await?;

    let borrowing_cap_id = action.0.controller_cap;
    let borrowing_controller_cap_ref = client
      .get_object_ref_by_id(borrowing_cap_id)
      .await?
      .map(|OwnedObjectRef { reference, .. }| {
        let IotaObjectRef {
          object_id,
          version,
          digest,
        } = reference;
        (object_id, version, digest)
      })
      .ok_or_else(|| Error::ObjectLookup(format!("object {borrowing_cap_id} doesn't exist")))?;

    let tx = IdentityMoveCallsAdapter::execute_controller_execution(
      identity_ref,
      controller_cap_ref,
      proposal_id,
      borrowing_controller_cap_ref,
      action
        .0
        .intent_fn
        .expect("BorrowActionWithIntent makes sure intent_fn is present"),
      client.package_id(),
    )
    .map_err(|e| Error::TransactionBuildingFailed(e.to_string()))?;

    ExecuteProposalTx {
      identity,
      tx,
      _action: PhantomData::<ControllerExecution>,
    }
    .execute_with_opt_gas_internal(gas_budget, client)
    .await
  }
}