-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmod.rs
221 lines (207 loc) · 6.88 KB
/
mod.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
// KILT Blockchain – <https://kilt.io>
// Copyright (C) 2025, KILT Foundation
// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// If you feel like getting in touch with us, you can do so at <hello@kilt.io>
use did::{
did_details::{DidPublicKey, DidPublicKeyDetails},
DidSignature,
};
use frame_support::ensure;
use sp_core::ConstU32;
use sp_runtime::{traits::SaturatedConversion, BoundedVec};
use sp_std::vec::Vec;
use crate::{
merkle_proofs::v0::{
input_common::TimeBoundDidSignature,
output_common::{DidKeyRelationship, DipOriginInfo, RevealedDidKey, RevealedDidMerkleProofLeaf},
},
Error,
};
#[cfg(test)]
mod tests;
/// A DIP proof whose information has been verified but that contains a
/// cross-chain [`TimeBoundDidSignature`] that still needs verification.
///
/// The generic types indicate the following:
/// * `KiltDidKeyId`: The DID key ID type configured by the KILT chain.
/// * `KiltAccountId`: The `AccountId` type configured by the KILT chain.
/// * `KiltBlockNumber`: The `BlockNumber` type configured by the KILT chain.
/// * `KiltWeb3Name`: The web3name type configured by the KILT chain.
/// * `KiltLinkableAccountId`: The linkable account ID type configured by the
/// KILT chain.
/// * `ConsumerBlockNumber`: The `BlockNumber` definition of the consumer
/// parachain.
/// * `MAX_REVEALED_LEAVES_COUNT`: The maximum number of leaves revealable in
/// the proof.
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(Clone))]
pub struct DipRevealedDetailsAndUnverifiedDidSignature<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
ConsumerBlockNumber,
const MAX_REVEALED_LEAVES_COUNT: u32,
> {
/// The parts of the subject's DID details revealed in the DIP proof.
pub(crate) revealed_leaves: BoundedVec<
RevealedDidMerkleProofLeaf<KiltDidKeyId, KiltAccountId, KiltBlockNumber, KiltWeb3Name, KiltLinkableAccountId>,
ConstU32<MAX_REVEALED_LEAVES_COUNT>,
>,
/// The cross-chain DID signature.
pub(crate) signature: TimeBoundDidSignature<ConsumerBlockNumber>,
}
impl<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
ConsumerBlockNumber,
const MAX_REVEALED_LEAVES_COUNT: u32,
>
DipRevealedDetailsAndUnverifiedDidSignature<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
ConsumerBlockNumber,
MAX_REVEALED_LEAVES_COUNT,
> where
ConsumerBlockNumber: PartialOrd,
{
/// Verifies that the DIP proof signature is anchored to a block that has
/// not passed on the consumer chain.
pub fn verify_signature_time(
self,
block_number: &ConsumerBlockNumber,
) -> Result<
DipRevealedDetailsAndVerifiedDidSignatureFreshness<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
MAX_REVEALED_LEAVES_COUNT,
>,
Error,
> {
ensure!(self.signature.valid_until >= *block_number, Error::InvalidSignatureTime);
Ok(DipRevealedDetailsAndVerifiedDidSignatureFreshness {
revealed_leaves: self.revealed_leaves,
signature: self.signature.signature,
})
}
}
/// A DIP proof whose information has been verified and whose signature has been
/// verified not to be expired, but that yet does not contain information as to
/// which of the revealed keys has generated the signature.
///
/// The generic types indicate the following:
/// * `KiltDidKeyId`: The DID key ID type configured by the KILT chain.
/// * `KiltAccountId`: The `AccountId` type configured by the KILT chain.
/// * `KiltBlockNumber`: The `BlockNumber` type configured by the KILT chain.
/// * `KiltWeb3Name`: The web3name type configured by the KILT chain.
/// * `KiltLinkableAccountId`: The linkable account ID type configured by the
/// KILT chain.
/// * `MAX_REVEALED_LEAVES_COUNT`: The maximum number of leaves revealable in
/// the proof.
#[derive(Debug, PartialEq, Eq)]
pub struct DipRevealedDetailsAndVerifiedDidSignatureFreshness<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
const MAX_REVEALED_LEAVES_COUNT: u32,
> {
/// The parts of the subject's DID details revealed in the DIP proof.
pub(crate) revealed_leaves: BoundedVec<
RevealedDidMerkleProofLeaf<KiltDidKeyId, KiltAccountId, KiltBlockNumber, KiltWeb3Name, KiltLinkableAccountId>,
ConstU32<MAX_REVEALED_LEAVES_COUNT>,
>,
/// The cross-chain DID signature without time information.
pub(crate) signature: DidSignature,
}
impl<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
const MAX_REVEALED_LEAVES_COUNT: u32,
>
DipRevealedDetailsAndVerifiedDidSignatureFreshness<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
MAX_REVEALED_LEAVES_COUNT,
>
{
/// Iterates over the revealed DID leaves to find the ones that generated a
/// valid signature for the provided payload.
pub fn retrieve_signing_leaves_for_payload(
self,
payload: &[u8],
) -> Result<
DipOriginInfo<
KiltDidKeyId,
KiltAccountId,
KiltBlockNumber,
KiltWeb3Name,
KiltLinkableAccountId,
MAX_REVEALED_LEAVES_COUNT,
>,
Error,
> {
let revealed_verification_keys = self.revealed_leaves.iter().filter(|leaf| {
matches!(
leaf,
RevealedDidMerkleProofLeaf::DidKey(RevealedDidKey {
relationship: DidKeyRelationship::Verification(_),
..
})
)
});
let signing_leaves_indices: Vec<_> = revealed_verification_keys
.enumerate()
.filter(|(_, revealed_verification_key)| {
let RevealedDidMerkleProofLeaf::DidKey(RevealedDidKey {
details:
DidPublicKeyDetails {
key: DidPublicKey::PublicVerificationKey(verification_key),
..
},
..
}) = revealed_verification_key
else {
return false;
};
verification_key.verify_signature(payload, &self.signature).is_ok()
})
.map(|(index, _)| u32::saturated_from(index))
.collect();
ensure!(!signing_leaves_indices.is_empty(), Error::InvalidDidKeyRevealed);
let signing_leaves_indices_vector = signing_leaves_indices.try_into().map_err(|_| {
log::error!(target: "dip::consumer::DipRevealedDetailsAndVerifiedDidSignatureFreshnessV0", "Failed to convert vector of signing leaf indices into BoundedVec<u8, {MAX_REVEALED_LEAVES_COUNT}>.");
Error::Internal
})?;
Ok(DipOriginInfo {
revealed_leaves: self.revealed_leaves,
signing_leaves_indices: signing_leaves_indices_vector,
})
}
}