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

use identity_core::common::Object;
use identity_core::common::Url;

use crate::document::CoreDocument;
use crate::error::Result;
use crate::service::Service;
use identity_did::CoreDID;
use identity_verification::MethodRef;
use identity_verification::VerificationMethod;

/// A `DocumentBuilder` is used to generate a customized [`Document`](crate::document::CoreDocument).
#[derive(Clone, Debug)]
pub struct DocumentBuilder {
  pub(crate) id: Option<CoreDID>,
  pub(crate) controller: Vec<CoreDID>,
  pub(crate) also_known_as: Vec<Url>,
  pub(crate) verification_method: Vec<VerificationMethod>,
  pub(crate) authentication: Vec<MethodRef>,
  pub(crate) assertion_method: Vec<MethodRef>,
  pub(crate) key_agreement: Vec<MethodRef>,
  pub(crate) capability_delegation: Vec<MethodRef>,
  pub(crate) capability_invocation: Vec<MethodRef>,
  pub(crate) service: Vec<Service>,
  pub(crate) properties: Object,
}

impl DocumentBuilder {
  /// Creates a new `DocumentBuilder`.
  pub fn new(properties: Object) -> Self {
    Self {
      id: None,
      controller: Vec::new(),
      also_known_as: Vec::new(),
      verification_method: Vec::new(),
      authentication: Vec::new(),
      assertion_method: Vec::new(),
      key_agreement: Vec::new(),
      capability_delegation: Vec::new(),
      capability_invocation: Vec::new(),
      service: Vec::new(),
      properties,
    }
  }

  /// Sets the `id` value.
  #[must_use]
  pub fn id(mut self, value: CoreDID) -> Self {
    self.id = Some(value);
    self
  }

  /// Adds a value to the `controller` set.
  #[must_use]
  pub fn controller(mut self, value: CoreDID) -> Self {
    self.controller.push(value);
    self
  }

  /// Adds a value to the `alsoKnownAs` set.
  #[must_use]
  pub fn also_known_as(mut self, value: Url) -> Self {
    self.also_known_as.push(value);
    self
  }

  /// Adds a value to the `verificationMethod` set.
  #[must_use]
  pub fn verification_method(mut self, value: VerificationMethod) -> Self {
    self.verification_method.push(value);
    self
  }

  /// Adds a value to the `authentication` set.
  #[must_use]
  pub fn authentication(mut self, value: impl Into<MethodRef>) -> Self {
    self.authentication.push(value.into());
    self
  }

  /// Adds a value to the `assertionMethod` set.
  #[must_use]
  pub fn assertion_method(mut self, value: impl Into<MethodRef>) -> Self {
    self.assertion_method.push(value.into());
    self
  }

  /// Adds a value to the `keyAgreement` set.
  #[must_use]
  pub fn key_agreement(mut self, value: impl Into<MethodRef>) -> Self {
    self.key_agreement.push(value.into());
    self
  }

  /// Adds a value to the `capabilityDelegation` set.
  #[must_use]
  pub fn capability_delegation(mut self, value: impl Into<MethodRef>) -> Self {
    self.capability_delegation.push(value.into());
    self
  }

  /// Adds a value to the `capabilityInvocation` set.
  #[must_use]
  pub fn capability_invocation(mut self, value: impl Into<MethodRef>) -> Self {
    self.capability_invocation.push(value.into());
    self
  }

  /// Adds a value to the `service` set.
  #[must_use]
  pub fn service(mut self, value: Service) -> Self {
    self.service.push(value);
    self
  }

  /// Returns a new `Document` based on the `DocumentBuilder` configuration.
  pub fn build(self) -> Result<CoreDocument> {
    CoreDocument::from_builder(self)
  }
}

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

#[cfg(test)]
mod tests {
  use super::*;
  use crate::Error;
  use identity_did::DID;
  use identity_verification::MethodData;
  use identity_verification::MethodType;

  #[test]
  fn test_missing_id() {
    let result: Result<CoreDocument> = DocumentBuilder::default().build();
    assert!(matches!(result.unwrap_err(), Error::InvalidDocument(_, None)));
  }

  #[test]
  fn duplicate_id_different_scopes() {
    let did: CoreDID = "did:example:1234".parse().unwrap();
    let fragment = "#key1";
    let id = did.to_url().join(fragment).unwrap();

    let method1: VerificationMethod = VerificationMethod::builder(Default::default())
      .id(id.clone())
      .controller(did.clone())
      .type_(MethodType::ED25519_VERIFICATION_KEY_2018)
      .data(MethodData::PublicKeyBase58(
        "3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J".into(),
      ))
      .build()
      .unwrap();

    let method2: VerificationMethod = VerificationMethod::builder(Default::default())
      .id(id)
      .controller(did.clone())
      .type_(MethodType::X25519_KEY_AGREEMENT_KEY_2019)
      .data(MethodData::PublicKeyBase58(
        "FbQWLPRhTH95MCkQUeFYdiSoQt8zMwetqfWoxqPgaq7x".into(),
      ))
      .build()
      .unwrap();

    let result: Result<CoreDocument> = DocumentBuilder::default()
      .id(did)
      .verification_method(method1)
      .key_agreement(method2)
      .build();
    assert!(result.is_err());
  }
}