Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
change get_credential to only take a credential ID parameter
  • Loading branch information
ntn-x2 committed Aug 4, 2022
commit 3def3d6a3754414f86c2e30e91b74898152b08b7
7 changes: 3 additions & 4 deletions pallets/public-credentials/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,10 @@ pub mod pallet {

// This map ensures that at any time a credential is only linked (i.e., issued)
// to a single subject, as it maps from a credential ID to the subject it was
// issued to. Not exposed to the outside world.
// issued to.
#[pallet::storage]
#[pallet::getter(fn get_credential_subject)]
pub(crate) type CredentialSubjects<T> =
StorageMap<_, Blake2_128Concat, CredentialIdOf<T>, <T as Config>::SubjectId>;
pub type CredentialSubjects<T> = StorageMap<_, Blake2_128Concat, CredentialIdOf<T>, <T as Config>::SubjectId>;

/// The events generated by this pallet.
#[pallet::event]
Expand Down Expand Up @@ -527,7 +526,7 @@ pub mod pallet {
credential.authorization_id.as_ref().ok_or(Error::<T>::Unauthorized)?;
authorization
.ok_or(Error::<T>::Unauthorized)?
.can_revoke(&caller, &credential.ctype_hash, &credential_id, credential_auth_id)
.can_revoke(caller, &credential.ctype_hash, credential_id, credential_auth_id)
.map_err(|_| Error::<T>::Unauthorized)?
};
// If authorization checks are ok, update the revocation status.
Expand Down
2 changes: 1 addition & 1 deletion rpc/public-credentials/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sp_api::decl_runtime_apis! {
CredentialId: Codec,
CredentialEntry: Codec
{
fn get_credential(subject: SubjectId, credential_id: CredentialId) -> Option<CredentialEntry>;
fn get_credential(credential_id: CredentialId) -> Option<CredentialEntry>;
fn get_credentials(subject: SubjectId) -> Vec<(CredentialId, CredentialEntry)>;
}
}
15 changes: 2 additions & 13 deletions rpc/public-credentials/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ pub trait PublicCredentialsFilter<Credential> {

#[rpc(client, server)]
pub trait PublicCredentialsApi<BlockHash, OuterSubjectId, OuterCredentialId, OuterCredentialEntry, CredentialFilter> {
/// Return a credential that matches the provided root hash and issued to
/// the provided subject, if found.
/// Return a credential that matches the provided credential ID, if found.
#[method(name = "get_credential")]
fn get_credential(
&self,
subject: OuterSubjectId,
credential_id: OuterCredentialId,
at: Option<BlockHash>,
) -> RpcResult<Option<OuterCredentialEntry>>;
Expand Down Expand Up @@ -167,21 +165,12 @@ impl<
{
fn get_credential(
&self,
subject: OuterSubjectId,
credential_id: OuterCredentialId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<OuterCredentialEntry>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

let into_subject: SubjectId = subject.try_into().map_err(|_| {
CallError::Custom(ErrorObject::owned(
Error::Conversion as i32,
"Unable to convert input to a valid subject ID.",
Option::<String>::None,
))
})?;

let into_credential_id: CredentialId = credential_id.try_into().map_err(|_| {
CallError::Custom(ErrorObject::owned(
Error::Conversion as i32,
Expand All @@ -190,7 +179,7 @@ impl<
))
})?;

let credential = api.get_credential(&at, into_subject, into_credential_id).map_err(|_| {
let credential = api.get_credential(&at, into_credential_id).map_err(|_| {
CallError::Custom(ErrorObject::owned(
Error::Runtime as i32,
"Unable to get credential.",
Expand Down
3 changes: 2 additions & 1 deletion runtimes/peregrine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,8 @@ impl_runtime_apis! {
}

impl public_credentials_runtime_api::PublicCredentialsApi<Block, <Runtime as public_credentials::Config>::SubjectId, attestation::ClaimHashOf<Runtime>, public_credentials::CredentialEntryOf<Runtime>> for Runtime {
fn get_credential(subject: <Runtime as public_credentials::Config>::SubjectId, credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
fn get_credential(credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
let subject = public_credentials::CredentialSubjects::<Runtime>::get(&credential_id)?;
public_credentials::Credentials::<Runtime>::get(&subject, &credential_id)
}

Expand Down
3 changes: 2 additions & 1 deletion runtimes/spiritnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,8 @@ impl_runtime_apis! {
}

impl public_credentials_runtime_api::PublicCredentialsApi<Block, <Runtime as public_credentials::Config>::SubjectId, attestation::ClaimHashOf<Runtime>, public_credentials::CredentialEntryOf<Runtime>> for Runtime {
fn get_credential(subject: <Runtime as public_credentials::Config>::SubjectId, credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
fn get_credential(credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
let subject = public_credentials::CredentialSubjects::<Runtime>::get(&credential_id)?;
public_credentials::Credentials::<Runtime>::get(&subject, &credential_id)
}

Expand Down
3 changes: 2 additions & 1 deletion runtimes/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,8 @@ impl_runtime_apis! {
}

impl public_credentials_runtime_api::PublicCredentialsApi<Block, <Runtime as public_credentials::Config>::SubjectId, attestation::ClaimHashOf<Runtime>, public_credentials::CredentialEntryOf<Runtime>> for Runtime {
fn get_credential(subject: <Runtime as public_credentials::Config>::SubjectId, credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
fn get_credential(credential_id: attestation::ClaimHashOf<Runtime>) -> Option<public_credentials::CredentialEntryOf<Runtime>> {
let subject = public_credentials::CredentialSubjects::<Runtime>::get(&credential_id)?;
public_credentials::Credentials::<Runtime>::get(&subject, &credential_id)
}

Expand Down