concordium-rust-sdk 9.0.1

An SDK to use the Concordium blockchain.
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Functionality for requesting and verifying V1 Concordium verifiable presentations.
//!
//! A verification flow consists of multiple stages:
//!
//! 1. Create a [`VerificationRequest`]: A verification flow is started by constructing [`VerificationRequestData`]
//!    and creating the [`VerificationRequest`] with [`create_verification_request_and_submit_request_anchor`] which also
//!    submits the corresponding [`VerificationRequestAnchor`] (VRA) on chain.
//!
//! 2. Generate and prove [`VerifiablePresentationV1`]: The claims in the [`VerificationRequest`] are
//!    proved by a credential holder in the context specified in the request and
//!    embedded in a [`VerifiablePresentationV1`] together with the context and proofs.
//!    The prover is implemented in [`VerifiablePresentationRequestV1::prove`](anchor::VerifiablePresentationRequestV1::prove).
//!
//! 3. Verify a [`VerifiablePresentationV1`]: The presentation can be verified together with
//!    the verification request with [`verify_presentation_and_submit_audit_anchor`], which submits
//!    and [`VerificationAuditRecord`] (VAA) on chain and returns the [`VerificationAuditRecord`] to
//!    be stored locally by the verifier.
//!
//! 4. Verify an [`VerificationAuditRecord`]: The stored audit record can be re-verified with
//!    [`verify_audit_record`] if/when needed.
//!
//! The example `web3id_v1_verification_flow` demonstrates the verification flow.

use crate::types::{AccountTransactionEffects, BlockItemSummaryDetails};
use crate::v2;

use crate::endpoints::RPCError;
use crate::smart_contracts::common::AccountAddress;
use crate::v2::{AccountIdentifier, BlockIdentifier, IntoBlockIdentifier, QueryError};
use concordium_base::base::{CredentialRegistrationID, Nonce};
use concordium_base::common::cbor;
use concordium_base::common::cbor::CborSerializationError;
use concordium_base::common::upward::UnknownDataError;
use concordium_base::hashes::TransactionHash;
use concordium_base::id::types;
use concordium_base::id::types::{AccountCredentialWithoutProofs, ArInfos, IpIdentity};
use concordium_base::transactions::{
    send, BlockItem, ExactSizeTransactionSigner, RegisteredData, TooLargeError,
};
use concordium_base::web3id::v1::anchor::{
    self as anchor, CredentialValidityType, PresentationVerificationResult,
    VerifiablePresentationV1, VerificationAuditRecord, VerificationContext, VerificationMaterial,
    VerificationMaterialWithValidity, VerificationRequest, VerificationRequestAnchor,
    VerificationRequestAnchorAndBlockHash, VerificationRequestData,
};
use concordium_base::web3id::v1::{
    AccountCredentialVerificationMaterial, CredentialMetadataTypeV1, CredentialMetadataV1,
    IdentityCredentialVerificationMaterial,
};
use concordium_base::{hashes, web3id};

use concordium_base::common::types::TransactionTime;
use futures::StreamExt;
use futures::{future, TryStreamExt};
use std::collections::{BTreeMap, HashMap};

/// Data returned from verifying a presentation against the corresponding verification request.
/// Contains the verification result, the audit record and the transaction hash
/// for the transaction registering the verification audit anchor (VAA) on-chain in case
/// the verification was successful.
/// The audit record should be stored in an off-chain database for regulatory purposes
/// and should generally be kept private.
#[derive(Debug, Clone, PartialEq)]
pub struct PresentationVerificationData {
    // Whether the verification was successful. If not [`PresentationVerificationResult::Verified`],
    // the verifiable presentation is not valid and the credentials and claims in it are not verified to be true.
    pub verification_result: PresentationVerificationResult,
    /// The verification audit record. Notice that the existence of the audit record
    /// does not mean that verification was successful, that is specified
    /// by [`Self::verification_result`]. The audit record should be stored in an off-chain database for regulatory purposes
    /// and should generally be kept private.
    /// A corresponding [`VerificationRequestAnchor`] (VAA) is submitted
    /// on chain, if the verification is successful.
    pub audit_record: VerificationAuditRecord,
    /// Blockchain transaction hash for the transaction that registers
    /// the [verification audit record](anchor::VerificationAuditAnchor) (VAA) on-chain.
    /// Notice that this transaction may not have been finalized yet. Also, the anchor is
    /// only submitted if the verification is successful.
    pub anchor_transaction_hash: Option<hashes::TransactionHash>,
}

/// Error verifying presentation
#[derive(thiserror::Error, Debug)]
pub enum VerifyError {
    #[error("on-chain request anchor transaction is of invalid type")]
    InvalidRequestAnchor,
    #[error("on-chain request anchor transaction not finalized yet")]
    RequestAnchorNotFinalized,
    #[error("unknown data error: {0}")]
    UnknownDataError(#[from] UnknownDataError),
    #[error("node query error: {0}")]
    Query(#[from] v2::QueryError),
    #[error("create anchor: {0}")]
    Anchor(#[from] CreateAnchorError),
    #[error("CBOR serialization error: {0}")]
    CborSerialization(#[from] CborSerializationError),
    #[error("unknown identity provider: {0}")]
    UnknownIdentityProvider(IpIdentity),
    #[error("credential {cred_id} no longer present on account: {account}")]
    CredentialNotPresent {
        cred_id: CredentialRegistrationID,
        account: AccountAddress,
    },
    #[error("initial credential {cred_id} cannot be used.")]
    InitialCredential { cred_id: CredentialRegistrationID },
}

/// Metadata for transaction submission.
pub struct AuditRecordArgument<S: ExactSizeTransactionSigner> {
    /// Id of the audit record to create. Is fully determined by the verifier/caller.
    pub audit_record_id: String,
    /// Public information to be included in the audit record anchor (VAA) on-chain.
    pub public_info: Option<HashMap<String, cbor::value::Value>>,
    /// Metadata for the anchor transaction that submits the audit record anchor (VAA) on-chain.
    pub audit_record_anchor_transaction_metadata: AnchorTransactionMetadata<S>,
}

/// Verify the given verifiable presentation together with the given verification request.
/// The aspects validated are documented on [`anchor::verify_presentation_with_request_anchor`].
/// The verification returns an audit record containing the presentation and verification request.
/// In case the verification is successful, a [verification audit record](anchor::VerificationAuditAnchor)
/// (VRA) is submitted on chain.
/// Notice that when this method returns, the anchor is only submitted. The returned anchor transaction
/// hash must be tracked in order to determine if it is finalized.
pub async fn verify_presentation_and_submit_audit_anchor(
    client: &mut v2::Client,
    network: web3id::did::Network,
    block_identifier: impl IntoBlockIdentifier,
    verification_request: VerificationRequest,
    verifiable_presentation: VerifiablePresentationV1,
    audit_record_arg: AuditRecordArgument<impl ExactSizeTransactionSigner>,
) -> Result<PresentationVerificationData, VerifyError> {
    let block_identifier = block_identifier.into_block_identifier();
    let global_context = client
        .get_cryptographic_parameters(block_identifier)
        .await?
        .response;

    let block_info = client.get_block_info(block_identifier).await?.response;

    let request_anchor = lookup_request_anchor(client, &verification_request).await?;

    let verification_material = lookup_verification_materials_and_validity(
        client,
        block_identifier,
        &verifiable_presentation,
    )
    .await?;

    let context = VerificationContext {
        network,
        validity_time: block_info.block_slot_time,
    };

    let verification_result = anchor::verify_presentation_with_request_anchor(
        &global_context,
        &context,
        &verification_request,
        &verifiable_presentation,
        &request_anchor,
        &verification_material,
    );

    let audit_record = VerificationAuditRecord::new(
        audit_record_arg.audit_record_id,
        verification_request,
        verifiable_presentation,
    );

    let anchor_transaction_hash = if verification_result.is_success() {
        let txn_hash = submit_verification_audit_record_anchor(
            client,
            audit_record_arg.audit_record_anchor_transaction_metadata,
            &audit_record,
            audit_record_arg.public_info,
        )
        .await?;
        Some(txn_hash)
    } else {
        None
    };

    Ok(PresentationVerificationData {
        verification_result,
        audit_record,
        anchor_transaction_hash,
    })
}

/// Verify the verifiable presentation in the audit record together with the
/// verification request in the audit record. The aspects validated are documented on
/// [`anchor::verify_presentation_with_request_anchor`].
pub async fn verify_audit_record(
    client: &mut v2::Client,
    network: web3id::did::Network,
    block_identifier: impl IntoBlockIdentifier,
    verification_audit_record: &VerificationAuditRecord,
) -> Result<PresentationVerificationResult, VerifyError> {
    let block_identifier = block_identifier.into_block_identifier();
    let global_context = client
        .get_cryptographic_parameters(block_identifier)
        .await?
        .response;

    let block_info = client.get_block_info(block_identifier).await?.response;

    let request_anchor = lookup_request_anchor(client, &verification_audit_record.request).await?;

    let verification_material = lookup_verification_materials_and_validity(
        client,
        block_identifier,
        &verification_audit_record.presentation,
    )
    .await?;

    let context = VerificationContext {
        network,
        validity_time: block_info.block_slot_time,
    };

    Ok(anchor::verify_presentation_with_request_anchor(
        &global_context,
        &context,
        &verification_audit_record.request,
        &verification_audit_record.presentation,
        &request_anchor,
        &verification_material,
    ))
}

/// Looks up the verifiable request anchor (VRA) from the verification
/// request.
pub async fn lookup_request_anchor(
    client: &mut v2::Client,
    verification_request: &VerificationRequest,
) -> Result<VerificationRequestAnchorAndBlockHash, VerifyError> {
    // Fetch the transaction
    let item_status = client
        .get_block_item_status(&verification_request.anchor_transaction_hash)
        .await?;

    let (block_hash, summary) = item_status
        .is_finalized()
        .ok_or(VerifyError::RequestAnchorNotFinalized)?;

    // Extract account transaction
    let BlockItemSummaryDetails::AccountTransaction(anchor_tx) =
        summary.details.as_ref().known_or_err()?
    else {
        return Err(VerifyError::InvalidRequestAnchor);
    };

    // Extract data registered payload
    let AccountTransactionEffects::DataRegistered { data } =
        anchor_tx.effects.as_ref().known_or_err()?
    else {
        return Err(VerifyError::InvalidRequestAnchor);
    };

    // Decode anchor hash
    let verification_request_anchor: VerificationRequestAnchor = cbor::cbor_decode(data.as_ref())?;

    Ok(VerificationRequestAnchorAndBlockHash {
        verification_request_anchor,
        block_hash: *block_hash,
    })
}

/// Lookup the verification material needed to verify the presentation.
pub async fn lookup_verification_materials_and_validity(
    client: &mut v2::Client,
    block_identifier: BlockIdentifier,
    presentation: &VerifiablePresentationV1,
) -> Result<Vec<VerificationMaterialWithValidity>, VerifyError> {
    let verification_material =
        future::try_join_all(presentation.metadata().map(|cred_metadata| {
            let mut client = client.clone();
            async move {
                lookup_verification_material_and_validity(
                    &mut client,
                    block_identifier,
                    &cred_metadata,
                )
                .await
            }
        }))
        .await?;
    Ok(verification_material)
}

/// Lookup verification material for presentation
async fn lookup_verification_material_and_validity(
    client: &mut v2::Client,
    block_identifier: BlockIdentifier,
    cred_metadata: &CredentialMetadataV1,
) -> Result<VerificationMaterialWithValidity, VerifyError> {
    Ok(match &cred_metadata.cred_metadata {
        CredentialMetadataTypeV1::Account(metadata) => {
            let account_info = client
                .get_account_info(
                    &AccountIdentifier::CredId(metadata.cred_id),
                    block_identifier,
                )
                .await?;

            let Some(account_cred) =
                account_info
                    .response
                    .account_credentials
                    .values()
                    .find_map(|cred| {
                        cred.value
                            .as_ref()
                            .known()
                            .and_then(|c| (c.cred_id() == metadata.cred_id.as_ref()).then_some(c))
                    })
            else {
                return Err(VerifyError::CredentialNotPresent {
                    cred_id: metadata.cred_id,
                    account: account_info.response.account_address,
                });
            };

            match account_cred {
                AccountCredentialWithoutProofs::Initial { .. } => {
                    return Err(VerifyError::InitialCredential {
                        cred_id: metadata.cred_id,
                    })
                }
                AccountCredentialWithoutProofs::Normal { cdv, commitments } => {
                    let credential_validity = types::CredentialValidity {
                        created_at: account_cred.policy().created_at,
                        valid_to: cdv.policy.valid_to,
                    };

                    VerificationMaterialWithValidity {
                        verification_material: VerificationMaterial::Account(
                            AccountCredentialVerificationMaterial {
                                issuer: cdv.ip_identity,
                                attribute_commitments: commitments.cmm_attributes.clone(),
                            },
                        ),
                        validity: CredentialValidityType::ValidityPeriod(credential_validity),
                    }
                }
            }
        }
        CredentialMetadataTypeV1::Identity(metadata) => {
            let ip_info = client
                .get_identity_providers(block_identifier)
                .await?
                .response
                .try_filter(|ip| future::ready(ip.ip_identity == metadata.issuer))
                .next()
                .await
                .ok_or(VerifyError::UnknownIdentityProvider(metadata.issuer))?
                .map_err(|status| QueryError::RPCError(RPCError::CallError(status)))?;

            let ars_infos: BTreeMap<_, _> = client
                .get_anonymity_revokers(block_identifier)
                .await?
                .response
                .map_ok(|ar_info| (ar_info.ar_identity, ar_info))
                .try_collect()
                .await
                .map_err(|status| QueryError::RPCError(RPCError::CallError(status)))?;

            VerificationMaterialWithValidity {
                verification_material: VerificationMaterial::Identity(
                    IdentityCredentialVerificationMaterial {
                        ip_info,
                        ars_infos: ArInfos {
                            anonymity_revokers: ars_infos,
                        },
                    },
                ),
                validity: CredentialValidityType::ValidityPeriod(metadata.validity.clone()),
            }
        }
    })
}

/// Error creating and registering anchor.
#[derive(thiserror::Error, Debug)]
pub enum CreateAnchorError {
    #[error("node query error: {0}")]
    Query(#[from] v2::QueryError),
    #[error("data register transaction data is too large: {0}")]
    TooLarge(#[from] TooLargeError),
    #[error("CBOR serialization error: {0}")]
    CborSerialization(#[from] CborSerializationError),
}

impl From<RPCError> for CreateAnchorError {
    fn from(err: RPCError) -> Self {
        CreateAnchorError::Query(err.into())
    }
}

/// Metadata for anchor transaction submission.
pub struct AnchorTransactionMetadata<S: ExactSizeTransactionSigner> {
    /// The signer object used to sign the on-chain anchor transaction. This must correspond to the `sender` account below.
    pub signer: S,
    /// The sender account of the anchor transaction.
    pub sender: AccountAddress,
    /// The sequence number for the sender account to use.
    pub account_sequence_number: Nonce,
    /// The transaction expiry time.
    pub expiry: TransactionTime,
}

/// Submit verification request anchor (VRA) and return the verification request.
///
/// Notice that the VRA will only be submitted, it is not included on-chain yet when
/// the function returns. The transaction hash is returned
/// in [`VerificationRequest::anchor_transaction_hash`] and the transaction must
/// be tracked until finalization before the verification request is usable
/// (waiting for finalization can be done in the app that receives the verification request
/// to create a verifiable presentation).
pub async fn create_verification_request_and_submit_request_anchor<
    S: ExactSizeTransactionSigner,
>(
    client: &mut v2::Client,
    anchor_transaction_metadata: AnchorTransactionMetadata<S>,
    verification_request_data: VerificationRequestData,
    public_info: Option<HashMap<String, cbor::value::Value>>,
) -> Result<VerificationRequest, CreateAnchorError> {
    let verification_request_anchor = verification_request_data.to_anchor(public_info);
    let cbor = cbor::cbor_encode(&verification_request_anchor)?;
    let register_data = RegisteredData::try_from(cbor)?;

    let tx = send::register_data(
        &anchor_transaction_metadata.signer,
        anchor_transaction_metadata.sender,
        anchor_transaction_metadata.account_sequence_number,
        anchor_transaction_metadata.expiry,
        register_data,
    );
    let block_item = BlockItem::AccountTransaction(tx);

    // Submit the transaction to the chain.
    let transaction_hash = client.send_block_item(&block_item).await?;

    Ok(VerificationRequest {
        context: verification_request_data.context,
        subject_claims: verification_request_data.subject_claims,
        anchor_transaction_hash: transaction_hash,
    })
}

/// Submit verification audit anchor (VAA).
///
/// Notice that the VAA will only be submitted, it is not included on-chain yet when
/// the function returns. The transaction must
/// be tracked until finalization for the audit record to be registered successfully.
pub async fn submit_verification_audit_record_anchor<S: ExactSizeTransactionSigner>(
    client: &mut v2::Client,
    anchor_transaction_metadata: AnchorTransactionMetadata<S>,
    verification_audit_record: &VerificationAuditRecord,
    public_info: Option<HashMap<String, cbor::value::Value>>,
) -> Result<TransactionHash, CreateAnchorError> {
    let verification_audit_anchor = verification_audit_record.to_anchor(public_info);
    let cbor = cbor::cbor_encode(&verification_audit_anchor)?;
    let register_data = RegisteredData::try_from(cbor)?;

    let tx = send::register_data(
        &anchor_transaction_metadata.signer,
        anchor_transaction_metadata.sender,
        anchor_transaction_metadata.account_sequence_number,
        anchor_transaction_metadata.expiry,
        register_data,
    );
    let item = BlockItem::AccountTransaction(tx);

    // Submit the transaction to the chain.
    let transaction_hash = client.send_block_item(&item).await?;

    Ok(transaction_hash)
}