-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add DID key verification relationship logic #502
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3b6a7eb
wip
ntn-x2 9fa1afb
Compilation working
ntn-x2 2714833
Rename Proof to Details
ntn-x2 40b6037
Add comments
ntn-x2 f97f428
Remove unnecessary parameters from pallet-dip-receiver
ntn-x2 7f81bbf
Cleanup
ntn-x2 14acb44
Change associated type to generic type
ntn-x2 bd1ec65
Add utility pallet to receiver template and implement nested call logic
ntn-x2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,19 @@ | |
|
|
||
| // If you feel like getting in touch with us, you can do so at [email protected] | ||
|
|
||
| use runtime_common::dip::{receiver::DidMerkleProofVerifier, ProofLeaf}; | ||
| use did::DidVerificationKeyRelationship; | ||
| use pallet_dip_receiver::traits::DipCallOriginFilter; | ||
| use runtime_common::dip::{ | ||
| receiver::{DidMerkleProofVerifier, VerificationResult}, | ||
| ProofLeaf, | ||
| }; | ||
| use sp_std::vec::Vec; | ||
|
|
||
| use crate::{BlockNumber, DidIdentifier, Hash, Hasher, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin}; | ||
|
|
||
| impl pallet_dip_receiver::Config for Runtime { | ||
| type BlindedValue = Vec<Vec<u8>>; | ||
| type DipCallOriginFilter = DipCallFilter; | ||
| type Identifier = DidIdentifier; | ||
| type ProofLeaf = ProofLeaf<Hash, BlockNumber>; | ||
| type ProofDigest = Hash; | ||
|
|
@@ -31,3 +37,90 @@ impl pallet_dip_receiver::Config for Runtime { | |
| type RuntimeEvent = RuntimeEvent; | ||
| type RuntimeOrigin = RuntimeOrigin; | ||
| } | ||
|
|
||
| fn derive_verification_key_relationship(call: &RuntimeCall) -> Option<DidVerificationKeyRelationship> { | ||
| match call { | ||
| RuntimeCall::DidLookup { .. } => Some(DidVerificationKeyRelationship::Authentication), | ||
| RuntimeCall::Utility(pallet_utility::Call::batch { calls }) => single_key_relationship(calls).ok(), | ||
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => single_key_relationship(calls).ok(), | ||
| RuntimeCall::Utility(pallet_utility::Call::force_batch { calls }) => single_key_relationship(calls).ok(), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| // Taken and adapted from `impl | ||
| // did::DeriveDidCallAuthorizationVerificationKeyRelationship for RuntimeCall` | ||
| // in Spiritnet/Peregrine runtime. | ||
| fn single_key_relationship(calls: &[RuntimeCall]) -> Result<DidVerificationKeyRelationship, ()> { | ||
| let first_call_relationship = calls.get(0).and_then(derive_verification_key_relationship).ok_or(())?; | ||
| calls | ||
| .iter() | ||
| .skip(1) | ||
| .map(derive_verification_key_relationship) | ||
| .try_fold(first_call_relationship, |acc, next| { | ||
| if next == Some(acc) { | ||
| Ok(acc) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| pub struct DipCallFilter; | ||
|
|
||
| impl DipCallOriginFilter<RuntimeCall> for DipCallFilter { | ||
| type Error = (); | ||
| type Proof = VerificationResult<BlockNumber>; | ||
| type Success = (); | ||
|
|
||
| // Accepts only a DipOrigin for the DidLookup pallet calls. | ||
| fn check_proof(call: RuntimeCall, proof: Self::Proof) -> Result<Self::Success, Self::Error> { | ||
| let key_relationship = single_key_relationship(&[call])?; | ||
| if proof.0.iter().any(|l| l.relationship == key_relationship.into()) { | ||
| Ok(()) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod dip_call_origin_filter_tests { | ||
| use super::*; | ||
|
|
||
| use frame_support::assert_err; | ||
|
|
||
| #[test] | ||
| fn test_key_relationship_derivation() { | ||
| // Can call DidLookup functions with an authentication key | ||
| let did_lookup_call = RuntimeCall::DidLookup(pallet_did_lookup::Call::associate_sender {}); | ||
| assert_eq!( | ||
| single_key_relationship(&[did_lookup_call]), | ||
| Ok(DidVerificationKeyRelationship::Authentication) | ||
| ); | ||
| // Can't call System functions with a DID key (hence a DIP origin) | ||
| let system_call = RuntimeCall::System(frame_system::Call::remark { remark: vec![] }); | ||
| assert_err!(single_key_relationship(&[system_call]), ()); | ||
| // Can't call empty batch with a DID key | ||
| let empty_batch_call = RuntimeCall::Utility(pallet_utility::Call::batch_all { calls: vec![] }); | ||
| assert_err!(single_key_relationship(&[empty_batch_call]), ()); | ||
| // Can call batch with a DipLookup with an authentication key | ||
| let did_lookup_batch_call = RuntimeCall::Utility(pallet_utility::Call::batch_all { | ||
| calls: vec![pallet_did_lookup::Call::associate_sender {}.into()], | ||
| }); | ||
| assert_eq!( | ||
| single_key_relationship(&[did_lookup_batch_call]), | ||
| Ok(DidVerificationKeyRelationship::Authentication) | ||
| ); | ||
| // Can't call a batch with different required keys | ||
| let did_lookup_batch_call = RuntimeCall::Utility(pallet_utility::Call::batch_all { | ||
| calls: vec![ | ||
| // Authentication key | ||
| pallet_did_lookup::Call::associate_sender {}.into(), | ||
| // No key | ||
| frame_system::Call::remark { remark: vec![] }.into(), | ||
| ], | ||
| }); | ||
| assert_err!(single_key_relationship(&[did_lookup_batch_call]), ()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI This could get simplified with reduce once it's not experimental... https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_reduce