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

#[cfg(feature = "test")]
use iota_sdk::client::Client;

use crate::block::address::Address;
use crate::block::output::feature::SenderFeature;
use crate::block::output::unlock_condition::GovernorAddressUnlockCondition;
use crate::block::output::unlock_condition::StateControllerAddressUnlockCondition;
use crate::block::output::AliasId;
use crate::block::output::AliasOutput;
use crate::block::output::AliasOutputBuilder;
use crate::block::output::Feature;
use crate::block::output::OutputId;
use crate::block::output::RentStructure;
use crate::block::output::UnlockCondition;
use crate::block::protocol::ProtocolParameters;
use crate::Error;
use crate::IotaDID;
use crate::IotaDocument;
use crate::NetworkName;
use crate::Result;

/// Helper functions necessary for the [`IotaIdentityClientExt`] trait.
#[cfg_attr(feature = "send-sync-client-ext", async_trait::async_trait)]
#[cfg_attr(not(feature = "send-sync-client-ext"), async_trait::async_trait(?Send))]
pub trait IotaIdentityClient {
  /// Resolve an Alias identifier, returning its latest [`OutputId`] and [`AliasOutput`].
  async fn get_alias_output(&self, alias_id: AliasId) -> Result<(OutputId, AliasOutput)>;
  /// Get the protocol parameters of the node we are trying to connect to.
  async fn get_protocol_parameters(&self) -> Result<ProtocolParameters>;
}

/// An extension trait that provides helper functions for publication
/// and resolution of DID documents in Alias Outputs.
///
/// This trait is not intended to be implemented directly, a blanket implementation is
/// provided for [`IotaIdentityClient`] implementers.
#[cfg_attr(feature = "send-sync-client-ext", async_trait::async_trait)]
#[cfg_attr(not(feature = "send-sync-client-ext"), async_trait::async_trait(?Send))]
pub trait IotaIdentityClientExt: IotaIdentityClient {
  /// Create a DID with a new Alias Output containing the given `document`.
  ///
  /// The `address` will be set as the state controller and governor unlock conditions.
  /// The minimum required token deposit amount will be set according to the given
  /// `rent_structure`, which will be fetched from the node if not provided.
  /// The returned Alias Output can be further customised before publication, if desired.
  ///
  /// NOTE: This does *not* publish the Alias Output.
  ///
  /// # Errors
  ///
  /// - [`Error::DIDUpdateError`] when retrieving the `RentStructure` fails.
  /// - [`Error::AliasOutputBuildError`] when building the Alias Output fails.
  async fn new_did_output(
    &self,
    address: Address,
    document: IotaDocument,
    rent_structure: Option<RentStructure>,
  ) -> Result<AliasOutput> {
    let rent_structure: RentStructure = if let Some(rent) = rent_structure {
      rent
    } else {
      self.get_rent_structure().await?
    };

    AliasOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AliasId::null())
      .with_state_index(0)
      .with_foundry_counter(0)
      .with_state_metadata(document.pack()?)
      .add_feature(Feature::Sender(SenderFeature::new(address)))
      .add_unlock_condition(UnlockCondition::StateControllerAddress(
        StateControllerAddressUnlockCondition::new(address),
      ))
      .add_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
        address,
      )))
      .finish()
      .map_err(Error::AliasOutputBuildError)
  }

  /// Fetches the associated Alias Output and updates it with `document` in its state metadata.
  /// The storage deposit on the output is left unchanged. If the size of the document increased,
  /// the amount should be increased manually.
  ///
  /// NOTE: This does *not* publish the updated Alias Output.
  ///
  /// # Errors
  ///
  /// Returns `Err` when failing to resolve the DID contained in `document`.
  async fn update_did_output(&self, document: IotaDocument) -> Result<AliasOutput> {
    let id: AliasId = AliasId::from(document.id());
    let (_, alias_output) = self.get_alias_output(id).await?;

    let mut alias_output_builder: AliasOutputBuilder = AliasOutputBuilder::from(&alias_output)
      .with_state_index(alias_output.state_index() + 1)
      .with_state_metadata(document.pack()?);

    if alias_output.alias_id().is_null() {
      alias_output_builder = alias_output_builder.with_alias_id(id);
    }

    alias_output_builder.finish().map_err(Error::AliasOutputBuildError)
  }

  /// Removes the DID document from the state metadata of its Alias Output,
  /// effectively deactivating it. The storage deposit on the output is left unchanged,
  /// and should be reallocated manually.
  ///
  /// Deactivating does not destroy the output. Hence, it can be re-activated by publishing
  /// an update containing a DID document.
  ///
  /// NOTE: this does *not* publish the updated Alias Output.
  ///
  /// # Errors
  ///
  /// Returns `Err` when failing to resolve the `did`.
  async fn deactivate_did_output(&self, did: &IotaDID) -> Result<AliasOutput> {
    let alias_id: AliasId = AliasId::from(did);
    let (_, alias_output) = self.get_alias_output(alias_id).await?;

    let mut alias_output_builder: AliasOutputBuilder = AliasOutputBuilder::from(&alias_output)
      .with_state_index(alias_output.state_index() + 1)
      .with_state_metadata(Vec::new());

    if alias_output.alias_id().is_null() {
      alias_output_builder = alias_output_builder.with_alias_id(alias_id);
    }

    alias_output_builder.finish().map_err(Error::AliasOutputBuildError)
  }

  /// Resolve a [`IotaDocument`]. Returns an empty, deactivated document if the state metadata
  /// of the Alias Output is empty.
  ///
  /// # Errors
  ///
  /// - [`NetworkMismatch`](Error::NetworkMismatch) if the network of the DID and client differ.
  /// - [`NotFound`](iota_sdk::client::Error::NoOutput) if the associated Alias Output was not found.
  async fn resolve_did(&self, did: &IotaDID) -> Result<IotaDocument> {
    validate_network(self, did).await?;

    let id: AliasId = AliasId::from(did);
    let (_, alias_output) = self.get_alias_output(id).await?;
    IotaDocument::unpack_from_output(did, &alias_output, true)
  }

  /// Fetches the [`AliasOutput`] associated with the given DID.
  ///
  /// # Errors
  ///
  /// - [`NetworkMismatch`](Error::NetworkMismatch) if the network of the DID and client differ.
  /// - [`NotFound`](iota_sdk::client::Error::NoOutput) if the associated Alias Output was not found.
  async fn resolve_did_output(&self, did: &IotaDID) -> Result<AliasOutput> {
    validate_network(self, did).await?;

    let id: AliasId = AliasId::from(did);
    self.get_alias_output(id).await.map(|(_, alias_output)| alias_output)
  }

  /// Returns the network name of the client, which is the
  /// Bech32 human-readable part (HRP) of the network.
  ///
  /// E.g. "iota", "atoi", "smr", "rms".
  async fn network_name(&self) -> Result<NetworkName> {
    self.get_network_hrp().await.and_then(NetworkName::try_from)
  }

  /// Return the rent structure of the network, indicating the byte costs for outputs.
  async fn get_rent_structure(&self) -> Result<RentStructure> {
    self
      .get_protocol_parameters()
      .await
      .map(|parameters| *parameters.rent_structure())
  }

  /// Gets the token supply of the node we're connecting to.
  async fn get_token_supply(&self) -> Result<u64> {
    self
      .get_protocol_parameters()
      .await
      .map(|parameters| parameters.token_supply())
  }

  /// Return the Bech32 human-readable part (HRP) of the network.
  ///
  /// E.g. "iota", "atoi", "smr", "rms".
  async fn get_network_hrp(&self) -> Result<String> {
    self
      .get_protocol_parameters()
      .await
      .map(|parameters| parameters.bech32_hrp().to_string())
  }
}

#[cfg(not(feature = "test"))]
impl<T> IotaIdentityClientExt for T where T: IotaIdentityClient {}
#[cfg(feature = "test")]
impl IotaIdentityClientExt for Client {}

pub(super) async fn validate_network<T>(client: &T, did: &IotaDID) -> Result<()>
where
  T: IotaIdentityClient + ?Sized,
{
  let network_hrp: String = client
    .get_protocol_parameters()
    .await
    .map(|parameters| parameters.bech32_hrp().to_string())?;
  if did.network_str() != network_hrp.as_str() {
    return Err(Error::NetworkMismatch {
      expected: did.network_str().to_owned(),
      actual: network_hrp,
    });
  };
  Ok(())
}