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
use serde::Serialize;
use crate::did::CoreDID;
use crate::did::DID;
use identity_core::common::KeyComparable;
use identity_core::common::Object;
use identity_core::common::Timestamp;
use identity_core::crypto::Ed25519;
use identity_core::crypto::JcsEd25519;
use identity_core::crypto::PrivateKey;
use identity_core::crypto::ProofOptions;
use identity_core::crypto::ProofPurpose;
use identity_core::crypto::SetSignature;
use identity_core::crypto::Signer;
use crate::document::CoreDocument;
use crate::utils::DIDUrlQuery;
use crate::verification::MethodType;
use crate::verification::TryMethod;
use crate::verification::VerificationMethod;
use crate::Error;
use crate::Result;
pub struct DocumentSigner<'base, 'query, D = CoreDID, T = Object, U = Object, V = Object>
where
D: DID + KeyComparable,
{
document: &'base CoreDocument<D, T, U, V>,
private: &'base PrivateKey,
method: Option<DIDUrlQuery<'query>>,
options: ProofOptions,
}
impl<'base, D, T, U, V> DocumentSigner<'base, '_, D, T, U, V>
where
D: DID + KeyComparable,
{
pub fn new(document: &'base CoreDocument<D, T, U, V>, private: &'base PrivateKey) -> Self {
Self {
document,
private,
method: None,
options: ProofOptions::default(),
}
}
#[must_use]
pub fn options(mut self, options: ProofOptions) -> Self {
self.options = options;
self
}
#[must_use]
pub fn created(mut self, created: Timestamp) -> Self {
self.options = self.options.created(created);
self
}
#[must_use]
pub fn expires(mut self, expires: Timestamp) -> Self {
self.options = self.options.expires(expires);
self
}
#[must_use]
pub fn challenge(mut self, challenge: String) -> Self {
self.options = self.options.challenge(challenge);
self
}
#[must_use]
pub fn domain(mut self, domain: String) -> Self {
self.options = self.options.domain(domain);
self
}
#[must_use]
pub fn purpose(mut self, purpose: ProofPurpose) -> Self {
self.options = self.options.purpose(purpose);
self
}
}
impl<'base, 'query, D, T, U, V> DocumentSigner<'base, 'query, D, T, U, V>
where
D: DID + KeyComparable,
{
#[must_use]
pub fn method<Q>(mut self, value: Q) -> Self
where
Q: Into<DIDUrlQuery<'query>>,
{
self.method = Some(value.into());
self
}
}
impl<D, T, U, V> DocumentSigner<'_, '_, D, T, U, V>
where
D: DID + KeyComparable,
{
pub fn sign<X>(&self, that: &mut X) -> Result<()>
where
X: Serialize + SetSignature + TryMethod,
{
let query: DIDUrlQuery<'_> = self.method.clone().ok_or(Error::MethodNotFound)?;
let method: &VerificationMethod<D, U> = self.document.resolve_method(query, None).ok_or(Error::MethodNotFound)?;
let method_uri: String = X::try_method(method)?;
match method.type_() {
MethodType::Ed25519VerificationKey2018 => {
JcsEd25519::<Ed25519>::create_signature(that, method_uri, self.private.as_ref(), self.options.clone())?;
}
MethodType::X25519KeyAgreementKey2019 => {
return Err(Error::InvalidMethodType);
}
}
Ok(())
}
}