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

Remove useless Result aliases #2148

Merged
merged 2 commits into from
Dec 19, 2023
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
28 changes: 13 additions & 15 deletions demos/vc_issuer/app/generated/vc_issuer_idl.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export const idlFactory = ({ IDL }) => {
'UnknownSubject' : IDL.Text,
'UnsupportedCredentialSpec' : IDL.Text,
});
const GetCredentialResponse = IDL.Variant({
'Ok' : IssuedCredentialData,
'Err' : IssueCredentialError,
});
const HeaderField = IDL.Tuple(IDL.Text, IDL.Text);
const HttpRequest = IDL.Record({
'url' : IDL.Text,
Expand All @@ -47,10 +43,6 @@ export const idlFactory = ({ IDL }) => {
const PreparedCredentialData = IDL.Record({
'prepared_context' : IDL.Opt(IDL.Vec(IDL.Nat8)),
});
const PrepareCredentialResponse = IDL.Variant({
'Ok' : PreparedCredentialData,
'Err' : IssueCredentialError,
});
const Icrc21ConsentPreferences = IDL.Record({ 'language' : IDL.Text });
const Icrc21VcConsentMessageRequest = IDL.Record({
'preferences' : Icrc21ConsentPreferences,
Expand All @@ -69,29 +61,35 @@ export const idlFactory = ({ IDL }) => {
'UnsupportedCanisterCall' : Icrc21ErrorInfo,
'ConsentMessageUnavailable' : Icrc21ErrorInfo,
});
const Icrc21ConsentMessageResponse = IDL.Variant({
'Ok' : Icrc21ConsentInfo,
'Err' : Icrc21Error,
});
return IDL.Service({
'add_adult' : IDL.Func([IDL.Principal], [IDL.Text], []),
'add_employee' : IDL.Func([IDL.Principal], [IDL.Text], []),
'add_graduate' : IDL.Func([IDL.Principal], [IDL.Text], []),
'configure' : IDL.Func([IssuerConfig], [], []),
'get_credential' : IDL.Func(
[GetCredentialRequest],
[GetCredentialResponse],
[
IDL.Variant({
'Ok' : IssuedCredentialData,
'Err' : IssueCredentialError,
}),
],
['query'],
),
'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']),
'prepare_credential' : IDL.Func(
[PrepareCredentialRequest],
[PrepareCredentialResponse],
[
IDL.Variant({
'Ok' : PreparedCredentialData,
'Err' : IssueCredentialError,
}),
],
[],
),
'vc_consent_message' : IDL.Func(
[Icrc21VcConsentMessageRequest],
[Icrc21ConsentMessageResponse],
[IDL.Variant({ 'Ok' : Icrc21ConsentInfo, 'Err' : Icrc21Error })],
[],
),
});
Expand Down
18 changes: 9 additions & 9 deletions demos/vc_issuer/app/generated/vc_issuer_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export interface GetCredentialRequest {
'prepared_context' : [] | [Uint8Array | number[]],
'credential_spec' : CredentialSpec,
}
export type GetCredentialResponse = { 'Ok' : IssuedCredentialData } |
{ 'Err' : IssueCredentialError };
export type HeaderField = [string, string];
export interface HttpRequest {
'url' : string,
Expand All @@ -32,8 +30,6 @@ export interface Icrc21ConsentInfo {
'consent_message' : string,
'language' : string,
}
export type Icrc21ConsentMessageResponse = { 'Ok' : Icrc21ConsentInfo } |
{ 'Err' : Icrc21Error };
export interface Icrc21ConsentPreferences { 'language' : string }
export type Icrc21Error = {
'GenericError' : { 'description' : string, 'error_code' : bigint }
Expand All @@ -60,8 +56,6 @@ export interface PrepareCredentialRequest {
'signed_id_alias' : SignedIdAlias,
'credential_spec' : CredentialSpec,
}
export type PrepareCredentialResponse = { 'Ok' : PreparedCredentialData } |
{ 'Err' : IssueCredentialError };
export interface PreparedCredentialData {
'prepared_context' : [] | [Uint8Array | number[]],
}
Expand All @@ -71,15 +65,21 @@ export interface _SERVICE {
'add_employee' : ActorMethod<[Principal], string>,
'add_graduate' : ActorMethod<[Principal], string>,
'configure' : ActorMethod<[IssuerConfig], undefined>,
'get_credential' : ActorMethod<[GetCredentialRequest], GetCredentialResponse>,
'get_credential' : ActorMethod<
[GetCredentialRequest],
{ 'Ok' : IssuedCredentialData } |
{ 'Err' : IssueCredentialError }
>,
'http_request' : ActorMethod<[HttpRequest], HttpResponse>,
'prepare_credential' : ActorMethod<
[PrepareCredentialRequest],
PrepareCredentialResponse
{ 'Ok' : PreparedCredentialData } |
{ 'Err' : IssueCredentialError }
>,
'vc_consent_message' : ActorMethod<
[Icrc21VcConsentMessageRequest],
Icrc21ConsentMessageResponse
{ 'Ok' : Icrc21ConsentInfo } |
{ 'Err' : Icrc21Error }
>,
}
export declare const idlFactory: IDL.InterfaceFactory;
53 changes: 32 additions & 21 deletions demos/vc_issuer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashSet;
use vc_util::issuer_api::{
ArgumentValue, CredentialSpec, GetCredentialRequest, GetCredentialResponse, Icrc21ConsentInfo,
Icrc21Error, Icrc21ErrorInfo, Icrc21VcConsentMessageRequest, IssueCredentialError,
IssuedCredentialData, PrepareCredentialRequest, PrepareCredentialResponse,
PreparedCredentialData, SignedIdAlias,
ArgumentValue, CredentialSpec, GetCredentialRequest, Icrc21ConsentInfo, Icrc21Error,
Icrc21ErrorInfo, Icrc21VcConsentMessageRequest, IssueCredentialError, IssuedCredentialData,
PrepareCredentialRequest, PreparedCredentialData, SignedIdAlias,
};
use vc_util::{
did_for_principal, get_verified_id_alias_from_jws, vc_jwt_to_jws, vc_signing_input,
Expand Down Expand Up @@ -188,23 +187,23 @@ fn extract_id_alias(

#[update]
#[candid_method]
async fn prepare_credential(req: PrepareCredentialRequest) -> PrepareCredentialResponse {
async fn prepare_credential(
req: PrepareCredentialRequest,
) -> Result<PreparedCredentialData, IssueCredentialError> {
let alias_tuple = match authorize_vc_request(&req.signed_id_alias, time().into()) {
Ok(alias_tuple) => alias_tuple,
Err(err) => return PrepareCredentialResponse::Err(err),
Err(err) => return Err(err),
};
let credential_type = match verify_credential_spec(&req.credential_spec) {
Ok(credential_type) => credential_type,
Err(err) => {
return PrepareCredentialResponse::Err(
IssueCredentialError::UnsupportedCredentialSpec(err),
);
return Err(IssueCredentialError::UnsupportedCredentialSpec(err));
}
};

let credential = match prepare_credential_payload(&credential_type, &alias_tuple) {
Ok(credential) => credential,
Err(err) => return PrepareCredentialResponse::Err(err),
Err(err) => return Result::<PreparedCredentialData, IssueCredentialError>::Err(err),
};
let seed = calculate_seed(&alias_tuple.id_alias);
let canister_id = ic_cdk::id();
Expand All @@ -221,7 +220,7 @@ async fn prepare_credential(req: PrepareCredentialRequest) -> PrepareCredentialR
add_signature(&mut sigs, msg_hash, seed);
});
update_root_hash();
PrepareCredentialResponse::Ok(PreparedCredentialData {
Ok(PreparedCredentialData {
prepared_context: Some(ByteBuf::from(credential_jwt.as_bytes())),
})
}
Expand All @@ -242,25 +241,35 @@ fn update_root_hash() {

#[query]
#[candid_method(query)]
fn get_credential(req: GetCredentialRequest) -> GetCredentialResponse {
fn get_credential(req: GetCredentialRequest) -> Result<IssuedCredentialData, IssueCredentialError> {
let alias_tuple = match authorize_vc_request(&req.signed_id_alias, time().into()) {
Ok(alias_tuple) => alias_tuple,
Err(err) => return GetCredentialResponse::Err(err),
Err(err) => return Result::<IssuedCredentialData, IssueCredentialError>::Err(err),
};
if let Err(err) = verify_credential_spec(&req.credential_spec) {
return GetCredentialResponse::Err(IssueCredentialError::UnsupportedCredentialSpec(err));
return Result::<IssuedCredentialData, IssueCredentialError>::Err(
IssueCredentialError::UnsupportedCredentialSpec(err),
);
}
let subject_principal = alias_tuple.id_alias;
let seed = calculate_seed(&subject_principal);
let canister_id = ic_cdk::id();
let canister_sig_pk = CanisterSigPublicKey::new(canister_id, seed.to_vec());
let prepared_context = match req.prepared_context {
Some(context) => context,
None => return GetCredentialResponse::Err(internal_error("missing prepared_context")),
None => {
return Result::<IssuedCredentialData, IssueCredentialError>::Err(internal_error(
"missing prepared_context",
))
}
};
let credential_jwt = match String::from_utf8(prepared_context.into_vec()) {
Ok(s) => s,
Err(_) => return GetCredentialResponse::Err(internal_error("invalid prepared_context")),
Err(_) => {
return Result::<IssuedCredentialData, IssueCredentialError>::Err(internal_error(
"invalid prepared_context",
))
}
};
let signing_input =
vc_signing_input(&credential_jwt, &canister_sig_pk).expect("failed getting signing_input");
Expand All @@ -278,15 +287,17 @@ fn get_credential(req: GetCredentialRequest) -> GetCredentialResponse {
let sig = match sig_result {
Ok(sig) => sig,
Err(e) => {
return GetCredentialResponse::Err(IssueCredentialError::SignatureNotFound(format!(
"signature not prepared or expired: {}",
e
)));
return Result::<IssuedCredentialData, IssueCredentialError>::Err(
IssueCredentialError::SignatureNotFound(format!(
"signature not prepared or expired: {}",
e
)),
);
}
};
let vc_jws =
vc_jwt_to_jws(&credential_jwt, &canister_sig_pk, &sig).expect("failed constructing JWS");
GetCredentialResponse::Ok(IssuedCredentialData { vc_jws })
Result::<IssuedCredentialData, IssueCredentialError>::Ok(IssuedCredentialData { vc_jws })
}

#[update]
Expand Down
Loading