Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH 292 - Remove SupportedTypes Constraint #74

Merged
merged 14 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
567 changes: 252 additions & 315 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ Here is an example of a credential the Civic Canister can issue:
"https://www.w3.org/ns/credentials/examples/v2"
],
"id": "urn:uuid:6a9c92a9-2530-4e2b-9776-530467e9bbe0",
"type": ["VerifiableCredential", "CivicUniquenessPass"],
"type": ["VerifiableCredential", "CivicPass"],
"issuer": "did:icp:v0:tglqb-kbqlj-to66e-3w5sg-kkz32-c6ffi-nsnta-vj2gf-vdcc5-5rzjk-jae",
"expiry": "2024-04-04T00:00:00Z",
"credentialSubject": {
"id": "did:icp:user-principal",
"CivicPass": {
"id": "did:example:c276e12ec21ebfeb1f712ebc6f1",
"name": "Civic Uniqueness Pass",
"expiry": "2024-04-04T00:00:00Z"
}
"passType": "uniqobk8oGh4XBLMqM68K8M2zNu3CdYX7q5go7whQiv",
"status": "ACTIVE",
"expirationDate": "2024-12-31T23:59:59Z"
}
}
```
Expand Down Expand Up @@ -80,8 +78,6 @@ To simplify the deployment of the canisters we provide a script `deploy-civic.sh
./scripts/deploy-civic.sh local
```

Note: The script stops dfx after it executes. If you want to call your canisters, you have to start it again as `dfx start --background`.

### Manual deployment
Steps for the manual deployment:
1. **Create canisters and start the local Internet Computer replica**:
Expand Down
8 changes: 5 additions & 3 deletions lib/vc_util/src/issuer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ pub struct PrepareCredentialRequest {
pub enum IssueCredentialError {
UnknownSubject(String),
UnauthorizedSubject(String),
UnauthorizedIssuer(String),
InvalidIdAlias(String),
SignatureNotFound(String),
Internal(String),
UnsupportedCredentialSpec(String),
CredentialNotFound(String)
}

#[derive(Clone, Debug, CandidType, Deserialize, Eq, PartialEq)]
Expand All @@ -47,14 +49,14 @@ pub struct IssuedCredentialData {
#[derive(Eq, PartialEq, Clone, Debug, CandidType, Deserialize)]
pub enum ArgumentValue {
String(String),
Int(i32),
Int(i32)
}

impl Display for ArgumentValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self {
ArgumentValue::String(s) => write!(f, "'{}'", s),
ArgumentValue::Int(i) => write!(f, "{}", i),
ArgumentValue::Int(i) => write!(f, "{}", i)
}
}
}
Expand All @@ -63,7 +65,7 @@ impl From<ArgumentValue> for Value {
fn from(argument_value: ArgumentValue) -> Self {
match argument_value {
ArgumentValue::String(s) => Value::String(s),
ArgumentValue::Int(i) => Value::Number(Number::from(i)),
ArgumentValue::Int(i) => Value::Number(Number::from(i))
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions scripts/deploy-civic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,8 @@ main() {
exit 1
fi

# Stop the local DFX environment if it was started
if [ "$network" = "local" ]; then
echo "Stopping local DFX environment..."
if ! dfx stop >>$log_file 2>&1; then
echo "Error: Failed to stop local DFX environment. Check $log_file for details."
exit 1
fi
fi

echo "Deployment completed successfully."
echo "Please check deploy.log for details."

}

# Execute main function with provided network argument
Expand Down
51 changes: 11 additions & 40 deletions src/civic_canister_backend/src/consent_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,42 @@ use std::fmt::{Display, Formatter};
use candid::candid_method;
use ic_cdk_macros::update;
use lazy_static::lazy_static;
use crate::credential::{SupportedCredentialType, verify_credential_spec};
use vc_util::issuer_api::{
CredentialSpec, Icrc21ConsentInfo,Icrc21VcConsentMessageRequest, Icrc21ConsentPreferences, Icrc21Error, Icrc21ErrorInfo,
Icrc21ConsentInfo,Icrc21VcConsentMessageRequest, Icrc21ConsentPreferences, Icrc21Error, Icrc21ErrorInfo,
};
use SupportedLanguage::{English, German};
use SupportedLanguage::English;

/// Consent messages for the VerifiedAdult VC to be shown and approved to the user during the VC sharing flow
const ADULT_VC_DESCRIPTION_EN: &str = r###"# Verified Adult
/// Consent messages for the CivicPass VC to be shown and approved to the user during the VC sharing flow
/// Currently we only support English language
const VC_DESCRIPTION_EN: &str = r###"# Verifiable Credential

Credential that states that the holder's age is at least 18 years."###;
const ADULT_VC_DESCRIPTION_DE: &str = r###"# Erwachsene Person

Ausweis, der bestätigt, dass der Besitzer oder die Besitzerin mindestens 18 Jahre alt ist."###;
Credential that states that the holder possesses a Verifiable Credential."###;
dankelleher marked this conversation as resolved.
Show resolved Hide resolved

lazy_static! {
static ref CONSENT_MESSAGE_TEMPLATES: HashMap<(CredentialTemplateType, SupportedLanguage), &'static str> =
HashMap::from([
(
(CredentialTemplateType::VerifiedAdult, English),
ADULT_VC_DESCRIPTION_EN
),
(
(CredentialTemplateType::VerifiedAdult, German),
ADULT_VC_DESCRIPTION_DE
(CredentialTemplateType::Credential, English),
VC_DESCRIPTION_EN
)
]);
}

/// Supported consent message types
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum CredentialTemplateType {
VerifiedAdult,
Credential,
}

/// Supported languages for consent messages
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum SupportedLanguage {
English,
German,
}

impl From<&SupportedCredentialType> for CredentialTemplateType {
fn from(value: &SupportedCredentialType) -> Self {
match value {
SupportedCredentialType::VerifiedAdult => CredentialTemplateType::VerifiedAdult,
}
}
}

impl From<Icrc21ConsentPreferences> for SupportedLanguage {
fn from(value: Icrc21ConsentPreferences) -> Self {
match &value.language.to_lowercase()[..2] {
"de" => German,
_ => English, // english is also the fallback
}
}
Expand All @@ -66,7 +49,6 @@ impl Display for SupportedLanguage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
English => write!(f, "en"),
German => write!(f, "de"),
}
}
}
Expand All @@ -79,38 +61,27 @@ async fn vc_consent_message(
req: Icrc21VcConsentMessageRequest,
) -> Result<Icrc21ConsentInfo, Icrc21Error> {
get_vc_consent_message(
&req.credential_spec,
&SupportedLanguage::from(req.preferences),
)
}

/// Retrieve the consent message for the given credential type and language.
fn get_vc_consent_message(
credential_spec: &CredentialSpec,
language: &SupportedLanguage,
) -> Result<Icrc21ConsentInfo, Icrc21Error> {
render_consent_message(credential_spec, language).map(|message| Icrc21ConsentInfo {
render_consent_message(language).map(|message| Icrc21ConsentInfo {
consent_message: message,
language: format!("{}", language),
})
}

/// Show the consent message with any arguments
fn render_consent_message(
credential_spec: &CredentialSpec,
language: &SupportedLanguage,
) -> Result<String, Icrc21Error> {
let credential_type = match verify_credential_spec(credential_spec) {
Ok(credential_type) => credential_type,
Err(err) => {
return Err(Icrc21Error::UnsupportedCanisterCall(Icrc21ErrorInfo {
description: err,
}));
}
};
let template = CONSENT_MESSAGE_TEMPLATES
.get(&(
CredentialTemplateType::from(&credential_type),
CredentialTemplateType::Credential,
dankelleher marked this conversation as resolved.
Show resolved Hide resolved
language.clone(),
))
.ok_or(Icrc21Error::ConsentMessageUnavailable(Icrc21ErrorInfo {
Expand Down
Loading