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
add ctype info in the storage
  • Loading branch information
ntn-x2 committed Aug 2, 2022
commit 6f5439d29e1a360bd38b3482ba0405c1e101d256
4 changes: 3 additions & 1 deletion pallets/public-credentials/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub struct Credential<CtypeHash, SubjectIdentifier, Claims> {
/// block. The block number is used to query the full content of the credential
/// from archive nodes.
#[derive(Encode, Decode, Clone, MaxEncodedLen, RuntimeDebug, PartialEq, Eq, PartialOrd, Ord, TypeInfo)]
pub struct CredentialEntry<Attester, BlockNumber, AccountId, Balance,> {
pub struct CredentialEntry<CTypeHash, Attester, BlockNumber, AccountId, Balance,> {
/// The hash of the CType used for this attestation.
pub ctype_hash: CTypeHash,
/// The attester of the credential.
pub attester: Attester,
/// A flag indicating the revocation status of the credential
Expand Down
5 changes: 3 additions & 2 deletions pallets/public-credentials/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub mod pallet {
use sp_runtime::traits::{Hash, SaturatedConversion};
use sp_std::vec::Vec;

use ctype::CtypeHashOf;
pub use ctype::CtypeHashOf;
use kilt_support::traits::CallSources;

/// The current storage version.
Expand All @@ -89,7 +89,7 @@ pub mod pallet {
pub type InputClaimsContentOf<T> = BoundedVec<u8, <T as Config>::MaxEncodedClaimsLength>;
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;
pub type CredentialEntryOf<T> = CredentialEntry<AttesterOf<T>, BlockNumberOf<T>, AccountIdOf<T>, BalanceOf<T>>;
pub type CredentialEntryOf<T> = CredentialEntry<CtypeHashOf<T>, AttesterOf<T>, BlockNumberOf<T>, AccountIdOf<T>, BalanceOf<T>>;
/// Type of an attester identifier.
pub type AttesterOf<T> = <T as Config>::AttesterId;
/// The type of account's balances.
Expand Down Expand Up @@ -271,6 +271,7 @@ pub mod pallet {
attester,
deposit,
block_number,
ctype_hash,
},
);
CredentialSubjects::<T>::insert(&credential_id, subject.clone());
Expand Down
6 changes: 3 additions & 3 deletions pallets/public-credentials/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
use codec::Encode;
use sp_runtime::traits::Hash;

use ctype::CtypeHashOf;

use crate::{AttesterOf, Config, CredentialIdOf, InputClaimsContentOf, InputCredentialOf, InputSubjectIdOf};
use crate::{AttesterOf, Config, CredentialIdOf, CtypeHashOf, InputClaimsContentOf, InputCredentialOf, InputSubjectIdOf};

// Generate a public credential using a many Default::default() as possible.
pub fn generate_base_public_credential_creation_op<T: Config>(
Expand Down Expand Up @@ -138,8 +136,10 @@ pub(crate) mod runtime {
payer: T::AccountId,
block_number: <T as frame_system::Config>::BlockNumber,
attester: T::AttesterId,
ctype_hash: Option<CtypeHashOf<T>>
) -> CredentialEntryOf<T> {
CredentialEntryOf::<T> {
ctype_hash: ctype_hash.unwrap_or_default(),
revoked: false,
attester,
block_number,
Expand Down
18 changes: 10 additions & 8 deletions pallets/public-credentials/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn add_successful() {
assert_eq!(stored_public_credential_details.attester, attester);
assert!(!stored_public_credential_details.revoked);
assert_eq!(stored_public_credential_details.block_number, 0);
assert_eq!(stored_public_credential_details.ctype_hash, ctype_hash_1);
assert_eq!(CredentialSubjects::<Test>::get(&credential_id_1), Some(subject_id));

// Check deposit reservation logic
Expand Down Expand Up @@ -106,6 +107,7 @@ fn add_successful() {
assert_eq!(stored_public_credential_details.attester, attester);
assert!(!stored_public_credential_details.revoked);
assert_eq!(stored_public_credential_details.block_number, 1);
assert_eq!(stored_public_credential_details.ctype_hash, ctype_hash_2);
assert_eq!(CredentialSubjects::<Test>::get(&credential_id_2), Some(subject_id));

// Deposit is 2x now
Expand Down Expand Up @@ -177,7 +179,7 @@ fn add_not_enough_balance() {
fn revoke_successful() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone());
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone(), None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand Down Expand Up @@ -232,7 +234,7 @@ fn revoke_unauthorised() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let wrong_attester = sr25519_did_from_seed(&BOB_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester);
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester, None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand All @@ -254,7 +256,7 @@ fn revoke_unauthorised() {
fn unrevoke_successful() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let mut new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone());
let mut new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone(), None);
new_credential.revoked = true;
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();
Expand Down Expand Up @@ -310,7 +312,7 @@ fn unrevoke_unauthorised() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let wrong_attester = sr25519_did_from_seed(&BOB_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester);
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester, None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand All @@ -332,7 +334,7 @@ fn unrevoke_unauthorised() {
fn remove_successful() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone());
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester.clone(), None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand Down Expand Up @@ -388,7 +390,7 @@ fn remove_unauthorized() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let wrong_attester = sr25519_did_from_seed(&BOB_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester);
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester, None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand All @@ -410,7 +412,7 @@ fn remove_unauthorized() {
fn reclaim_deposit_successful() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester);
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester, None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand Down Expand Up @@ -464,7 +466,7 @@ fn reclaim_deposit_credential_not_found() {
fn reclaim_deposit_unauthorized() {
let attester = sr25519_did_from_seed(&ALICE_SEED);
let subject_id: <Test as Config>::SubjectId = SUBJECT_ID_00;
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester);
let new_credential = generate_base_credential_entry::<Test>(ACCOUNT_00, 0, attester, None);
let credential_id: CredentialIdOf<Test> = CredentialIdOf::<Test>::default();
let deposit: Balance = <Test as Config>::Deposit::get();

Expand Down