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

use core::fmt::Display;
use core::fmt::Formatter;
use core::slice::Iter;

use serde;
use serde::Deserialize;
use serde::Serialize;

use identity_core::convert::FmtJson;
use identity_iota_core::did::IotaDID;
use identity_iota_core::diff::DiffMessage;
use identity_iota_core::tangle::Message;
use identity_iota_core::tangle::MessageId;
use identity_iota_core::tangle::MessageIdExt;

use crate::chain::milestone::sort_by_milestone;
use crate::chain::IntegrationChain;
use crate::document::ResolvedIotaDocument;
use crate::error::Error;
use crate::error::Result;
use crate::tangle::Client;
use crate::tangle::MessageExt;
use crate::tangle::MessageIndex;
use crate::tangle::PublishType;
use crate::tangle::TangleRef;

#[deprecated(since = "0.5.0", note = "diff chain features are slated for removal")]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
pub struct DiffChain {
  inner: Vec<DiffMessage>,
}

impl DiffChain {
  /// Constructs a new [`DiffChain`] for the given [`IntegrationChain`] from a slice of [`Messages`][Message].
  pub async fn try_from_messages(
    integration_chain: &IntegrationChain,
    messages: &[Message],
    client: &Client,
  ) -> Result<Self> {
    let did: &IotaDID = integration_chain.current().document.id();

    let index: MessageIndex<DiffMessage> = messages
      .iter()
      .flat_map(|message| message.try_extract_diff(did))
      .collect();

    log::debug!("[Diff] Valid Messages = {}/{}", messages.len(), index.len());

    Self::try_from_index(integration_chain, index, client).await
  }

  /// Constructs a new [`DiffChain`] for the given [`IntegrationChain`] from the given [`MessageIndex`].
  pub async fn try_from_index(
    integration_chain: &IntegrationChain,
    index: MessageIndex<DiffMessage>,
    client: &Client,
  ) -> Result<Self> {
    log::trace!("[Diff] Message Index = {:#?}", index);
    Self::try_from_index_with_document(integration_chain.current(), index, client).await
  }

  /// Constructs a new [`DiffChain`] from the given [`MessageIndex`], using an integration document
  /// to validate.
  pub(in crate::chain) async fn try_from_index_with_document(
    integration_document: &ResolvedIotaDocument,
    mut index: MessageIndex<DiffMessage>,
    client: &Client,
  ) -> Result<Self> {
    if index.is_empty() {
      return Ok(Self::new());
    }

    let mut this: Self = Self::new();
    let mut current_document: ResolvedIotaDocument = integration_document.clone();
    while let Some(diffs) = index.remove(
      this
        .current_message_id()
        .unwrap_or_else(|| current_document.message_id()),
    ) {
      // Extract diffs that reference the last message (either the integration message or the
      // diff message from the previous iteration). If more than one references the
      // same message, they are conflicting.
      let expected_prev_message_id: &MessageId = this
        .current_message_id()
        .unwrap_or_else(|| current_document.message_id());
      // Filter out diffs with invalid signatures.
      let valid_diffs: Vec<DiffMessage> = diffs
        .into_iter()
        .filter(|diff| Self::verify_diff(diff, integration_document, expected_prev_message_id).is_ok())
        .collect();

      // Sort and apply the diff referenced by the oldest milestone.
      if let Some((diff, merged_document)) = sort_by_milestone(valid_diffs, client)
        .await?
        .into_iter()
        .filter_map(|diff|
          // Try merge the diff changes into the document. Important to prevent changes to the
          // signing verification methods and reject invalid changes to non-existent sections.
          match Self::try_merge(&diff, &current_document) {
            Ok(merged_document) => Some((diff, merged_document)),
            _ => None,
          })
        .next()
      {
        // Update the document for the next diff to allow updating sections added by previous diffs.
        current_document = merged_document;
        // Checked by verify_diff and try_merge above.
        this.push_unchecked(diff);
      }
      // If no diff is appended, the chain ends.
    }

    Ok(this)
  }

  /// Creates a new [`DiffChain`].
  pub fn new() -> Self {
    Self { inner: Vec::new() }
  }

  /// Returns the total number of diffs.
  pub fn len(&self) -> usize {
    self.inner.len()
  }

  /// Returns `true` if the [`DiffChain`] is empty.
  pub fn is_empty(&self) -> bool {
    self.inner.is_empty()
  }

  /// Empties the [`DiffChain`], removing all diffs.
  pub fn clear(&mut self) {
    self.inner.clear();
  }

  /// Returns an iterator yielding references to [`DiffMessages`][DiffMessage].
  pub fn iter(&self) -> Iter<'_, DiffMessage> {
    self.inner.iter()
  }

  /// Returns the [`MessageId`] of the latest diff in the chain, if any.
  pub fn current_message_id(&self) -> Option<&MessageId> {
    self.inner.last().map(|diff| diff.message_id())
  }

  /// Adds a new diff to the [`DiffChain`] while merging it with the given integration document.
  /// Returns the merged document if valid.
  ///
  /// # Errors
  ///
  /// Fails if the diff signature is invalid, the Tangle message
  /// references within the diff are invalid, or the merge fails.
  pub fn try_push_and_merge(
    &mut self,
    diff: DiffMessage,
    integration_document: &ResolvedIotaDocument,
  ) -> Result<ResolvedIotaDocument> {
    let expected_prev_message_id: &MessageId = self
      .current_message_id()
      .unwrap_or_else(|| integration_document.message_id());
    let updated_document: ResolvedIotaDocument =
      Self::check_valid_addition(&diff, integration_document, expected_prev_message_id)?;
    self.push_unchecked(diff);

    Ok(updated_document)
  }

  /// Adds a new diff to the [`DiffChain`] without performing any validation checks on
  /// the [`DiffMessage`].
  fn push_unchecked(&mut self, diff: DiffMessage) {
    self.inner.push(diff);
  }

  /// Checks whether the [`DiffMessage`] attributes and signature are valid.
  ///
  /// NOTE: does not verify the changes contained in the diff are valid.
  /// See [`DiffChain::try_merge`].
  pub fn verify_diff(
    diff: &DiffMessage,
    document: &ResolvedIotaDocument,
    expected_prev_message_id: &MessageId,
  ) -> Result<()> {
    if document.document.id() != diff.id() {
      return Err(Error::ChainError { error: "invalid DID" });
    }

    if diff.message_id().is_null() {
      return Err(Error::ChainError {
        error: "invalid message id",
      });
    }

    if diff.previous_message_id().is_null() {
      return Err(Error::ChainError {
        error: "invalid previous message id",
      });
    }

    if diff.previous_message_id() != expected_prev_message_id {
      return Err(Error::ChainError {
        error: "invalid previous message id",
      });
    }

    if document.document.verify_diff(diff).is_err() {
      return Err(Error::ChainError {
        error: "invalid diff signature",
      });
    }

    Ok(())
  }

  /// Attempts to merge the [`DiffMessage`] changes into the given document and returns the
  /// resulting [`ResolvedIotaDocument`] if valid.
  ///
  /// NOTE: does not verify the signature and attributes.
  /// See [`DiffChain::verify_diff`].
  pub fn try_merge(diff: &DiffMessage, document: &ResolvedIotaDocument) -> Result<ResolvedIotaDocument> {
    let mut updated_document: ResolvedIotaDocument = document.clone();
    updated_document.merge_diff_message(diff)?;
    if let Some(PublishType::Integration) = PublishType::new(&document.document, &updated_document.document) {
      return Err(Error::ChainError {
        error: "diff cannot alter update signing methods",
      });
    }

    Ok(updated_document)
  }

  /// Checks if the [`DiffMessage`] can be added to the [`DiffChain`]. Returns the merged
  /// document if valid.
  ///
  /// Equivalent to calling [`DiffChain::verify_diff`] then [`DiffChain::try_merge`].
  ///
  /// # Errors
  ///
  /// Fails if the [`DiffMessage`] is not a valid addition.
  pub fn check_valid_addition(
    diff: &DiffMessage,
    document: &ResolvedIotaDocument,
    expected_prev_message_id: &MessageId,
  ) -> Result<ResolvedIotaDocument> {
    Self::verify_diff(diff, document, expected_prev_message_id)?;
    Self::try_merge(diff, document)
  }
}

impl Default for DiffChain {
  fn default() -> Self {
    Self::new()
  }
}

impl Display for DiffChain {
  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
    self.fmt_json(f)
  }
}

impl From<DiffChain> for Vec<DiffMessage> {
  fn from(diff_chain: DiffChain) -> Self {
    diff_chain.inner
  }
}

#[cfg(test)]
mod tests {
  use identity_core::convert::FromJson;
  use identity_core::crypto::KeyPair;
  use identity_core::crypto::KeyType;
  use identity_core::json;
  use identity_did::did::DID;
  use identity_did::service::Service;
  use identity_iota_core::diff::DiffMessage;
  use identity_iota_core::document::IotaDocument;
  use identity_iota_core::document::IotaService;
  use identity_iota_core::tangle::MessageId;

  use crate::document::ResolvedIotaDocument;
  use crate::tangle::ClientBuilder;
  use crate::tangle::MessageIndex;
  use crate::tangle::TangleRef;

  use super::*;

  fn create_document() -> (ResolvedIotaDocument, KeyPair) {
    let keypair: KeyPair = KeyPair::new(KeyType::Ed25519).unwrap();
    let mut document: IotaDocument = IotaDocument::new(&keypair).unwrap();
    document
      .sign_self(
        keypair.private(),
        document.default_signing_method().unwrap().id().clone(),
      )
      .unwrap();
    let mut resolved: ResolvedIotaDocument = ResolvedIotaDocument::from(document);
    resolved.set_message_id(MessageId::new([1; 32]));
    (resolved, keypair)
  }

  #[tokio::test]
  async fn test_diff_chain_add_remove_service() {
    let (original, keypair) = create_document();

    // Add a new service in a diff update.
    let mut updated1: IotaDocument = original.document.clone();
    let service: IotaService = Service::from_json_value(json!({
      "id": updated1.id().to_url().join("#linked-domain").unwrap(),
      "type": "LinkedDomains",
      "serviceEndpoint": "https://iota.org"
    }))
    .unwrap();
    assert!(updated1.insert_service(service));
    let mut diff_add: DiffMessage = original
      .document
      .diff(
        &updated1,
        original.integration_message_id,
        keypair.private(),
        original.document.default_signing_method().unwrap().id(),
      )
      .unwrap();
    diff_add.set_message_id(MessageId::new([2; 32]));

    // Remove the same service in a diff update.
    let mut updated2: IotaDocument = updated1.clone();
    assert!(updated2.remove_service(&updated1.id().to_url().join("#linked-domain").unwrap()));
    let mut diff_delete: DiffMessage = updated1
      .diff(
        &updated2,
        *diff_add.message_id(),
        keypair.private(),
        updated1.default_signing_method().unwrap().id(),
      )
      .unwrap();
    diff_delete.set_message_id(MessageId::new([3; 32]));

    // Ensure both diffs are resolved by the DiffChain.
    let mut message_index: MessageIndex<DiffMessage> = MessageIndex::new();
    message_index.insert(diff_add.clone());
    message_index.insert(diff_delete.clone());
    let client: Client = ClientBuilder::new().node_sync_disabled().build().await.unwrap();
    let diff_chain: DiffChain = DiffChain::try_from_index_with_document(&original, message_index, &client)
      .await
      .unwrap();
    assert_eq!(diff_chain.len(), 2);
    assert_eq!(diff_chain.inner.get(0), Some(&diff_add));
    assert_eq!(diff_chain.inner.get(1), Some(&diff_delete));

    // Ensure the merged result does not have the service.
    let mut merged: ResolvedIotaDocument = original.clone();
    for diff in diff_chain.iter() {
      merged.merge_diff_message(diff).unwrap();
    }
    assert!(merged.document.service().is_empty());
  }

  #[tokio::test]
  async fn test_diff_chain_add_edit_service() {
    let (original, keypair) = create_document();

    // Add a new service in a diff update.
    let mut updated1: IotaDocument = original.document.clone();
    let service: IotaService = Service::from_json_value(json!({
      "id": updated1.id().to_url().join("#linked-domain").unwrap(),
      "type": "LinkedDomains",
      "serviceEndpoint": "https://iota.org"
    }))
    .unwrap();
    assert!(updated1.insert_service(service.clone()));
    let mut diff_add: DiffMessage = original
      .document
      .diff(
        &updated1,
        original.integration_message_id,
        keypair.private(),
        original.document.default_signing_method().unwrap().id(),
      )
      .unwrap();
    diff_add.set_message_id(MessageId::new([2; 32]));

    // Edit the same service in a diff update.
    let mut updated2: IotaDocument = updated1.clone();
    let service_updated: IotaService = Service::from_json_value(json!({
      "id": updated1.id().to_url().join("#linked-domain").unwrap(),
      "type": "LinkedDomains",
      "serviceEndpoint": ["https://example.com", "https://example.org"],
    }))
    .unwrap();
    updated2
      .core_document_mut()
      .service_mut()
      .replace(&service, service_updated.clone());
    let mut diff_edit: DiffMessage = updated1
      .diff(
        &updated2,
        *diff_add.message_id(),
        keypair.private(),
        updated1.default_signing_method().unwrap().id(),
      )
      .unwrap();
    diff_edit.set_message_id(MessageId::new([3; 32]));

    // Ensure both diffs are resolved by the DiffChain.
    let mut message_index: MessageIndex<DiffMessage> = MessageIndex::new();
    message_index.insert(diff_add.clone());
    message_index.insert(diff_edit.clone());
    let client: Client = ClientBuilder::new().node_sync_disabled().build().await.unwrap();
    let diff_chain: DiffChain = DiffChain::try_from_index_with_document(&original, message_index, &client)
      .await
      .unwrap();
    assert_eq!(diff_chain.len(), 2);
    assert_eq!(diff_chain.inner.get(0), Some(&diff_add));
    assert_eq!(diff_chain.inner.get(1), Some(&diff_edit));

    // Ensure the merged result has the updated service.
    let mut merged: ResolvedIotaDocument = original.clone();
    for diff in diff_chain.iter() {
      merged.merge_diff_message(diff).unwrap();
    }
    assert_ne!(merged.document.service().first().unwrap(), &service);
    assert_eq!(merged.document.service().first().unwrap(), &service_updated);
  }
}