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
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH

// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at info@botlabs.org

use frame_support::{traits::Get, weights::Weight};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{traits::Hash, DispatchError};

use kilt_support::{traits::StorageDepositCollector, Deposit};

use crate::{
	AttesterOf, BalanceOf, Config, CredentialEntryOf, CredentialIdOf, CredentialSubjects, Credentials, CtypeHashOf,
	InputClaimsContentOf, InputCredentialOf, InputSubjectIdOf, PublicCredentialDepositCollector,
	PublicCredentialsAccessControl,
};

// Generate a public credential using a many Default::default() as possible.
pub fn generate_base_public_credential_creation_op<T: Config>(
	subject_id: InputSubjectIdOf<T>,
	ctype_hash: CtypeHashOf<T>,
	claims: InputClaimsContentOf<T>,
) -> InputCredentialOf<T> {
	InputCredentialOf::<T> {
		ctype_hash,
		subject: subject_id,
		claims,
		authorization: None,
	}
}

pub fn generate_credential_id<T: Config>(
	input_credential: &InputCredentialOf<T>,
	attester: &AttesterOf<T>,
) -> CredentialIdOf<T> {
	T::CredentialHash::hash(&[&input_credential.encode()[..], &attester.encode()[..]].concat()[..])
}

/// Generates a basic credential entry using the provided input parameters
/// and the default value for the other ones. The credential will be marked
/// as non-revoked and with no authorization ID associated with it.
pub fn generate_base_credential_entry<T: Config>(
	payer: T::AccountId,
	block_number: BlockNumberFor<T>,
	attester: T::AttesterId,
	ctype_hash: Option<CtypeHashOf<T>>,
	deposit: Option<Deposit<T::AccountId, BalanceOf<T>>>,
) -> CredentialEntryOf<T> {
	CredentialEntryOf::<T> {
		ctype_hash: ctype_hash.unwrap_or_default(),
		revoked: false,
		attester,
		block_number,
		deposit: deposit.unwrap_or(Deposit::<T::AccountId, BalanceOf<T>> {
			owner: payer,
			amount: <T as Config>::Deposit::get(),
		}),
		authorization_id: None,
	}
}

pub fn insert_public_credentials<T: Config>(
	subject_id: T::SubjectId,
	credential_id: CredentialIdOf<T>,
	credential_entry: CredentialEntryOf<T>,
) {
	PublicCredentialDepositCollector::<T>::create_deposit(
		credential_entry.deposit.owner.clone(),
		credential_entry.deposit.amount,
	)
	.expect("Attester should have enough balance");

	Credentials::<T>::insert(&subject_id, &credential_id, credential_entry);
	CredentialSubjects::<T>::insert(credential_id, subject_id);
}

/// Authorize iff the subject of the origin and the provided attester id
/// match.
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[scale_info(skip_type_params(T))]
pub struct MockAccessControl<T: Config>(pub T::AttesterId);

impl<T> PublicCredentialsAccessControl<T::AttesterId, T::AuthorizationId, CtypeHashOf<T>, CredentialIdOf<T>>
	for MockAccessControl<T>
where
	T: Config<AuthorizationId = <T as Config>::AttesterId>,
{
	fn can_issue(
		&self,
		who: &T::AttesterId,
		_ctype: &CtypeHashOf<T>,
		_credential_id: &CredentialIdOf<T>,
	) -> Result<Weight, DispatchError> {
		if who == &self.0 {
			Ok(Weight::zero())
		} else {
			Err(DispatchError::Other("Unauthorized"))
		}
	}

	fn can_revoke(
		&self,
		who: &T::AttesterId,
		_ctype: &CtypeHashOf<T>,
		_credential_id: &CredentialIdOf<T>,
		authorization_id: &T::AuthorizationId,
	) -> Result<Weight, DispatchError> {
		if authorization_id == who {
			Ok(Weight::zero())
		} else {
			Err(DispatchError::Other("Unauthorized"))
		}
	}

	fn can_unrevoke(
		&self,
		who: &T::AttesterId,
		_ctype: &CtypeHashOf<T>,
		_credential_id: &CredentialIdOf<T>,
		authorization_id: &T::AuthorizationId,
	) -> Result<Weight, DispatchError> {
		if authorization_id == who {
			Ok(Weight::zero())
		} else {
			Err(DispatchError::Other("Unauthorized"))
		}
	}

	fn can_remove(
		&self,
		who: &T::AttesterId,
		_ctype: &CtypeHashOf<T>,
		_credential_id: &CredentialIdOf<T>,
		authorization_id: &T::AuthorizationId,
	) -> Result<Weight, DispatchError> {
		if authorization_id == who {
			Ok(Weight::zero())
		} else {
			Err(DispatchError::Other("Unauthorized"))
		}
	}

	fn authorization_id(&self) -> T::AuthorizationId {
		self.0.clone()
	}

	fn can_issue_weight(&self) -> Weight {
		Weight::zero()
	}
	fn can_revoke_weight(&self) -> Weight {
		Weight::zero()
	}
	fn can_unrevoke_weight(&self) -> Weight {
		Weight::zero()
	}
	fn can_remove_weight(&self) -> Weight {
		Weight::zero()
	}
}

#[cfg(test)]
pub use crate::mock::runtime::*;

// Mocks that are only used internally
#[cfg(test)]
pub(crate) mod runtime {
	use super::*;

	use frame_support::{
		traits::{ConstU128, ConstU16, ConstU32, ConstU64},
		weights::constants::RocksDbWeight,
	};
	use frame_system::EnsureSigned;
	use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
	use scale_info::TypeInfo;
	use sp_core::{sr25519, Pair};
	use sp_runtime::{
		traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify},
		BuildStorage, MultiSignature, MultiSigner, RuntimeDebug,
	};

	use kilt_support::mock::{mock_origin, SubjectId};

	use ctype::{CtypeCreatorOf, CtypeEntryOf, CtypeHashOf};

	use crate::{self as public_credentials, Config, CredentialEntryOf, Error, InputSubjectIdOf};

	pub(crate) type Balance = u128;
	pub(crate) type Hash = sp_core::H256;
	pub(crate) type AccountPublic = <MultiSignature as Verify>::Signer;
	pub(crate) type AccountId = <AccountPublic as IdentifyAccount>::AccountId;

	#[derive(
		Default, Clone, Copy, Encode, Decode, MaxEncodedLen, RuntimeDebug, Eq, PartialEq, Ord, PartialOrd, TypeInfo,
	)]
	pub struct TestSubjectId(pub [u8; 32]);

	impl TryFrom<Vec<u8>> for TestSubjectId {
		type Error = Error<Test>;

		// Test failure for subject input. Fails if the input vector is too long or if
		// the first byte is 255.
		fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
			let inner: [u8; 32] = value.try_into().map_err(|_| Error::<Test>::InvalidInput)?;
			if inner[0] == 255 {
				Err(Error::<Test>::InvalidInput)
			} else {
				Ok(Self(inner))
			}
		}
	}

	impl From<TestSubjectId> for Vec<u8> {
		fn from(value: TestSubjectId) -> Self {
			value.0.into()
		}
	}

	#[cfg(feature = "runtime-benchmarks")]
	impl<Context> kilt_support::traits::GetWorstCase<Context> for TestSubjectId {
		// Only used for benchmark testing, not really relevant.
		type Output = Self;

		fn worst_case(_context: Context) -> Self::Output {
			crate::mock::TestSubjectId::default()
		}
	}

	impl From<TestSubjectId> for InputSubjectIdOf<Test> {
		fn from(value: TestSubjectId) -> Self {
			value
				.0
				.to_vec()
				.try_into()
				.expect("Test subject ID should fit into the expected input subject ID of for the test runtime.")
		}
	}

	impl From<[u8; 32]> for TestSubjectId {
		fn from(value: [u8; 32]) -> Self {
			Self(value)
		}
	}

	pub(crate) const MILLI_UNIT: Balance = 10u128.pow(12);
	type Block = frame_system::mocking::MockBlock<Test>;

	frame_support::construct_runtime!(
		pub enum Test
		{
			System: frame_system,
			Ctype: ctype,
			Balances: pallet_balances,
			MockOrigin: mock_origin,
			PublicCredentials: public_credentials,
		}
	);

	impl mock_origin::Config for Test {
		type RuntimeOrigin = RuntimeOrigin;
		type AccountId = AccountId;
		type SubjectId = SubjectId;
	}

	impl frame_system::Config for Test {
		type RuntimeTask = ();
		type RuntimeOrigin = RuntimeOrigin;
		type RuntimeCall = RuntimeCall;
		type Block = Block;
		type Nonce = u64;
		type Hash = Hash;
		type Hashing = BlakeTwo256;
		type AccountId = AccountId;
		type Lookup = IdentityLookup<Self::AccountId>;
		type RuntimeEvent = ();
		type BlockHashCount = ConstU64<250>;
		type DbWeight = RocksDbWeight;
		type Version = ();

		type PalletInfo = PalletInfo;
		type AccountData = pallet_balances::AccountData<Balance>;
		type OnNewAccount = ();
		type OnKilledAccount = ();
		type BaseCallFilter = frame_support::traits::Everything;
		type SystemWeightInfo = ();
		type BlockWeights = ();
		type BlockLength = ();
		type SS58Prefix = ConstU16<38>;
		type OnSetCode = ();
		type MaxConsumers = ConstU32<16>;
	}

	impl pallet_balances::Config for Test {
		type RuntimeFreezeReason = RuntimeFreezeReason;
		type FreezeIdentifier = RuntimeFreezeReason;
		type RuntimeHoldReason = RuntimeHoldReason;
		type MaxFreezes = ConstU32<10>;
		type Balance = Balance;
		type DustRemoval = ();
		type RuntimeEvent = ();
		type ExistentialDeposit = ConstU128<MILLI_UNIT>;
		type AccountStore = System;
		type WeightInfo = ();
		type MaxLocks = ConstU32<5>;
		type MaxReserves = ConstU32<5>;
		type ReserveIdentifier = [u8; 8];
	}

	impl ctype::Config for Test {
		type CtypeCreatorId = SubjectId;
		type EnsureOrigin = mock_origin::EnsureDoubleOrigin<AccountId, Self::CtypeCreatorId>;
		type OriginSuccess = mock_origin::DoubleOrigin<AccountId, Self::CtypeCreatorId>;
		type OverarchingOrigin = EnsureSigned<AccountId>;
		type RuntimeEvent = ();
		type WeightInfo = ();

		type Currency = Balances;
		type Fee = ConstU128<500>;
		type FeeCollector = ();
	}

	impl Config for Test {
		type AccessControl = MockAccessControl<Self>;
		type AttesterId = SubjectId;
		type AuthorizationId = SubjectId;
		type CredentialId = Hash;
		type RuntimeHoldReason = RuntimeHoldReason;
		type CredentialHash = BlakeTwo256;
		type Currency = Balances;
		type Deposit = ConstU128<{ 10 * MILLI_UNIT }>;
		type EnsureOrigin = mock_origin::EnsureDoubleOrigin<AccountId, Self::AttesterId>;
		type RuntimeEvent = ();
		type MaxEncodedClaimsLength = ConstU32<500>;
		type MaxSubjectIdLength = ConstU32<100>;
		type OriginSuccess = mock_origin::DoubleOrigin<AccountId, Self::AttesterId>;
		type SubjectId = TestSubjectId;
		type WeightInfo = ();
		type BalanceMigrationManager = ();
	}

	pub(crate) const ACCOUNT_00: AccountId = AccountId::new([1u8; 32]);
	pub(crate) const ACCOUNT_01: AccountId = AccountId::new([2u8; 32]);
	// Min Balance has to be >= [ExistentialDeposit]
	pub(crate) const MIN_BALANCE: Balance = MILLI_UNIT;

	pub(crate) const ALICE_SEED: [u8; 32] = [0u8; 32];
	pub(crate) const BOB_SEED: [u8; 32] = [1u8; 32];

	pub(crate) const SUBJECT_ID_00: TestSubjectId = TestSubjectId([100u8; 32]);
	pub(crate) const SUBJECT_ID_01: TestSubjectId = TestSubjectId([1u8; 32]);
	pub(crate) const INVALID_SUBJECT_ID: TestSubjectId = TestSubjectId([255u8; 32]);

	pub(crate) fn sr25519_did_from_seed(seed: &[u8; 32]) -> SubjectId {
		MultiSigner::from(sr25519::Pair::from_seed(seed).public())
			.into_account()
			.into()
	}

	#[derive(Clone, Default)]
	pub(crate) struct ExtBuilder {
		/// initial ctypes & owners
		ctypes: Vec<(CtypeHashOf<Test>, CtypeCreatorOf<Test>)>,
		/// endowed accounts with balances
		balances: Vec<(AccountId, Balance)>,
		public_credentials: Vec<(
			<Test as Config>::SubjectId,
			CredentialIdOf<Test>,
			CredentialEntryOf<Test>,
		)>,
	}

	impl ExtBuilder {
		#[must_use]
		pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf<Test>, CtypeCreatorOf<Test>)>) -> Self {
			self.ctypes = ctypes;
			self
		}

		#[must_use]
		pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
			self.balances = balances;
			self
		}

		#[must_use]
		pub fn with_public_credentials(
			mut self,
			credentials: Vec<(
				<Test as Config>::SubjectId,
				CredentialIdOf<Test>,
				CredentialEntryOf<Test>,
			)>,
		) -> Self {
			self.public_credentials = credentials;
			self
		}

		pub(crate) fn build(self) -> sp_io::TestExternalities {
			let mut storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
			pallet_balances::GenesisConfig::<Test> {
				balances: self.balances.clone(),
			}
			.assimilate_storage(&mut storage)
			.expect("assimilate should not fail");

			let mut ext = sp_io::TestExternalities::new(storage);

			ext.execute_with(|| {
				for ctype in self.ctypes {
					ctype::Ctypes::<Test>::insert(
						ctype.0,
						CtypeEntryOf::<Test> {
							creator: ctype.1.clone(),
							created_at: System::block_number(),
						},
					);
				}

				for (subject_id, credential_id, credential_entry) in self.public_credentials {
					insert_public_credentials::<Test>(subject_id, credential_id, credential_entry);
				}
			});

			ext
		}

		pub fn build_and_execute_with_sanity_tests(self, test: impl FnOnce()) {
			self.build().execute_with(|| {
				test();
				crate::try_state::do_try_state::<Test>().expect("Sanity test for public credential failed.");
			})
		}

		#[cfg(feature = "runtime-benchmarks")]
		pub(crate) fn build_with_keystore(self) -> sp_io::TestExternalities {
			let mut ext = self.build();

			let keystore = sp_keystore::testing::MemoryKeystore::new();
			ext.register_extension(sp_keystore::KeystoreExt(std::sync::Arc::new(keystore)));

			ext
		}
	}
}