Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Jan 7, 2024
1 parent 7b53d68 commit 235f269
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 41 deletions.
50 changes: 44 additions & 6 deletions pallets/core/src/common/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use super::{Limits, ToStateChange};
use crate::util::btree_set;

use crate::{
common::{AuthorizeTarget, Signature},
common::{AuthorizeTarget, ForSigType, Signature},
did::{self, Did, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
util::{Action, ActionExecutionError, ActionWithNonce, NonceError, StorageRef, WithNonce},
util::{
Action, ActionExecutionError, ActionWithNonce, NonceError, StorageRef, Types, WithNonce,
},
};
use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -157,7 +159,43 @@ impl<T> AuthorizeTarget<T, DidKey> for PolicyExecutor {}
impl<T> AuthorizeTarget<T, DidMethodKey> for PolicyExecutor {}

/// `DID`s signature along with the nonce.
pub type DidSignatureWithNonce<T> = WithNonce<T, DidOrDidMethodKeySignature<PolicyExecutor>>;
#[derive(
Encode, Decode, CloneNoBound, PartialEqNoBound, EqNoBound, DebugNoBound, MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[derive(scale_info_derive::TypeInfo)]
#[scale_info(skip_type_params(T))]
#[scale_info(omit_prefix)]
pub struct DidSignatureWithNonce<T: Types> {
sig: DidOrDidMethodKeySignature<PolicyExecutor>,
nonce: T::BlockNumber,
}

impl<T: Types> DidSignatureWithNonce<T> {
pub fn new(sig: DidOrDidMethodKeySignature<PolicyExecutor>, nonce: T::BlockNumber) -> Self {
Self { sig, nonce }
}

pub fn into_data(self) -> DidOrDidMethodKeySignature<PolicyExecutor> {
self.sig
}
}

impl<T: Types> ForSigType for DidSignatureWithNonce<T> {
fn for_sig_type<R>(
&self,
for_sr25519: impl FnOnce() -> R,
for_ed25519: impl FnOnce() -> R,
for_secp256k1: impl FnOnce() -> R,
) -> Option<R> {
self.sig
.for_sig_type(for_sr25519, for_ed25519, for_secp256k1)
}
}

/// Denotes an entity which has an associated `Policy`.
pub trait HasPolicy<T: Limits>: Sized {
Expand Down Expand Up @@ -195,7 +233,7 @@ pub trait HasPolicy<T: Limits>: Sized {
proof.len() == 1
&& controllers.contains(
&*proof[0]
.data()
.sig
.signer()
.ok_or(PolicyExecutionError::InvalidSigner)?
),
Expand Down Expand Up @@ -238,7 +276,7 @@ pub trait HasPolicy<T: Limits>: Sized {
proof.len() == 1
&& controllers.contains(
&*proof[0]
.data()
.sig
.signer()
.ok_or(PolicyExecutionError::InvalidSigner)?
),
Expand Down Expand Up @@ -288,7 +326,7 @@ pub trait HasPolicy<T: Limits>: Sized {
proof.len() == 1
&& controllers.contains(
&*proof[0]
.data()
.sig
.signer()
.ok_or(PolicyExecutionError::InvalidSigner)?
),
Expand Down
4 changes: 2 additions & 2 deletions pallets/core/src/modules/accumulator/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T: Config> Pallet<T> {
params_ref: (did, counter),
..
}: RemoveAccumulatorParams<T>,
_: (),
(): (),
owner: AccumulatorOwner,
) -> DispatchResult {
// Only the DID that added the param can remove it
Expand All @@ -57,7 +57,7 @@ impl<T: Config> Pallet<T> {
key_ref: (did, counter),
..
}: RemoveAccumulatorPublicKey<T>,
_: (),
(): (),
owner: AccumulatorOwner,
) -> DispatchResult {
ensure!(did == owner, Error::<T>::NotAccumulatorOwner);
Expand Down
18 changes: 0 additions & 18 deletions pallets/core/src/modules/did/base/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::common::{
Authorization, AuthorizeSignedAction, AuthorizeTarget, DidMethodKeySigValue, ForSigType,
SigValue, Signature, ToStateChange,
};
use frame_support::traits::Get;

/// Either `DidKey` or `DidMethodKey`.
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, Copy, MaxEncodedLen)]
Expand Down Expand Up @@ -122,23 +121,6 @@ impl<D: Into<DidMethodKey>> ForSigType for DidKeySignature<D> {
}

impl<D: Into<DidOrDidMethodKey>> ForSigType for DidOrDidMethodKeySignature<D> {
fn weight_for_sig_type<T: frame_system::Config>(
&self,
for_sr25519: impl FnOnce() -> Weight,
for_ed25519: impl FnOnce() -> Weight,
for_secp256k1: impl FnOnce() -> Weight,
) -> Weight {
match self {
Self::DidSignature(sig) => {
sig.weight_for_sig_type::<T>(for_sr25519, for_ed25519, for_secp256k1)
}
Self::DidMethodKeySignature(sig) => sig
.weight_for_sig_type::<T>(for_sr25519, for_ed25519, for_secp256k1)
.saturating_sub(T::DbWeight::get().reads(1)),
_ => Default::default(),
}
}

fn for_sig_type<R>(
&self,
for_sr25519: impl FnOnce() -> R,
Expand Down
2 changes: 1 addition & 1 deletion pallets/core/src/modules/offchain_signatures/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<T: Config> Pallet<T> {
params_ref: (did, counter),
..
}: RemoveOffchainSignatureParams<T>,
_: (),
(): (),
owner: SignatureParamsOwner,
) -> DispatchResult {
// Only the DID that added the param can it
Expand Down
6 changes: 3 additions & 3 deletions pallets/core/src/modules/revoke/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ crate::bench_with_all_pairs! {
let signature = DidSignature::new(did, 1u32, sig).into();

super::Pallet::<T>::new_registry_(AddRegistry { id: reg_id, new_registry: RevocationRegistry { policy: Policy::one_of([DidOrDidMethodKey::from(did)]).unwrap(), add_only: false } }).unwrap();
}: revoke(RawOrigin::Signed(caller), revoke_raw, vec![DidSignatureWithNonce::new_with_nonce(signature, 1u32.into())])
}: revoke(RawOrigin::Signed(caller), revoke_raw, vec![DidSignatureWithNonce::new(signature, 1u32.into())])
verify {
assert!(revoke_ids
.iter()
Expand Down Expand Up @@ -101,7 +101,7 @@ crate::bench_with_all_pairs! {
let unrevoke = UnRevoke::new_with_nonce(unrevoke_raw.clone(), 1u32.into());
let sig = pair.sign(&unrevoke.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: unrevoke(RawOrigin::Signed(caller), unrevoke_raw, vec![DidSignatureWithNonce::new_with_nonce(signature, 1u32.into())])
}: unrevoke(RawOrigin::Signed(caller), unrevoke_raw, vec![DidSignatureWithNonce::new(signature, 1u32.into())])
verify {
assert!(revoke_ids
.iter()
Expand Down Expand Up @@ -149,7 +149,7 @@ crate::bench_with_all_pairs! {
let rem_reg = RemoveRegistry::new_with_nonce(rem_reg_raw.clone(), 1u32.into());
let sig = pair.sign(&rem_reg.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: remove_registry(RawOrigin::Signed(caller), rem_reg_raw, vec![DidSignatureWithNonce::new_with_nonce(signature, 1u32.into())])
}: remove_registry(RawOrigin::Signed(caller), rem_reg_raw, vec![DidSignatureWithNonce::new(signature, 1u32.into())])
verify {
assert!(Registries::<T>::get(reg_id).is_none());
};
Expand Down
6 changes: 3 additions & 3 deletions pallets/core/src/modules/revoke/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<T: Config> SubstrateWeight<T> {
fn revoke(revoke: &RevokeRaw<T>, sig: &DidSignatureWithNonce<T>) -> Weight {
let len = revoke.len();

sig.data().weight_for_sig_type::<T>(
sig.weight_for_sig_type::<T>(
|| Self::revoke_sr25519(len),
|| Self::revoke_ed25519(len),
|| Self::revoke_secp256k1(len),
Expand All @@ -322,15 +322,15 @@ impl<T: Config> SubstrateWeight<T> {
fn unrevoke(unrevoke: &UnRevokeRaw<T>, sig: &DidSignatureWithNonce<T>) -> Weight {
let len = unrevoke.len();

sig.data().weight_for_sig_type::<T>(
sig.weight_for_sig_type::<T>(
|| Self::unrevoke_sr25519(len),
|| Self::unrevoke_ed25519(len),
|| Self::unrevoke_secp256k1(len),
)
}

fn remove_registry(sig: &DidSignatureWithNonce<T>) -> Weight {
sig.data().weight_for_sig_type::<T>(
sig.weight_for_sig_type::<T>(
Self::remove_registry_sr25519,
Self::remove_registry_ed25519,
Self::remove_registry_secp256k1,
Expand Down
2 changes: 1 addition & 1 deletion pallets/core/src/modules/revoke/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
let sp = WithNonce::<Test, _>::new_with_nonce(action.clone(), next_nonce);
let sig = did_sig_on_bytes(&sp.to_state_change().encode(), kp, *did, 1).into();

WithNonce::new_with_nonce(sig, next_nonce)
DidSignatureWithNonce::new(sig, next_nonce)
})
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/core/src/modules/status_list_credential/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ crate::bench_with_all_pairs! {

let sig = pair.sign(&update.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: update(RawOrigin::Signed(caller), update.into_data(), vec![DidSignatureWithNonce::new_with_nonce(signature, 1u32.into())])
}: update(RawOrigin::Signed(caller), update.into_data(), vec![DidSignatureWithNonce::new(signature, 1u32.into())])
verify {
assert_eq!(StatusListCredentials::get(id).unwrap(), StatusListCredentialWithPolicy {
status_list_credential: StatusListCredential::<T>::StatusList2021Credential(BoundedBytes((0..r).map(|v| v as u8).try_collect().unwrap())),
Expand Down Expand Up @@ -87,7 +87,7 @@ crate::bench_with_all_pairs! {

let sig = pair.sign(&remove.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: remove(RawOrigin::Signed(caller), remove.into_data(), vec![DidSignatureWithNonce::new_with_nonce(signature, 1u32.into())])
}: remove(RawOrigin::Signed(caller), remove.into_data(), vec![DidSignatureWithNonce::new(signature, 1u32.into())])
verify {
assert_eq!(StatusListCredentials::<T>::get(id), None);
};
Expand Down
4 changes: 2 additions & 2 deletions pallets/core/src/modules/status_list_credential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ impl<T: Config> SubstrateWeight<T> {
sig: &DidSignatureWithNonce<T>,
UpdateStatusListCredentialRaw { credential, .. }: &UpdateStatusListCredentialRaw<T>,
) -> Weight {
sig.data().weight_for_sig_type::<T>(
sig.weight_for_sig_type::<T>(
|| Self::update_sr25519(credential.len()),
|| Self::update_ed25519(credential.len()),
|| Self::update_secp256k1(credential.len()),
)
}

fn remove(sig: &DidSignatureWithNonce<T>) -> Weight {
sig.data().weight_for_sig_type::<T>(
sig.weight_for_sig_type::<T>(
Self::remove_sr25519,
Self::remove_ed25519,
Self::remove_secp256k1,
Expand Down
2 changes: 1 addition & 1 deletion pallets/core/src/modules/status_list_credential/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
let state_change = action_with_nonce.to_state_change().encode();
let sig = did_sig_on_bytes(&state_change, kp, *did, 1).into();

WithNonce::new_with_nonce(sig, next_nonce)
DidSignatureWithNonce::new(sig, next_nonce)
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/core/src/modules/trust_registry/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<T: Config> Pallet<T> {
delegated,
..
}: UpdateDelegatedIssuers<T>,
_: (),
(): (),
issuer: Issuer,
) -> DispatchResult {
ensure!(
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("dock-pos-dev-runtime"),
impl_name: create_runtime_str!("Dock"),
authoring_version: 1,
spec_version: 48,
spec_version: 50,
impl_version: 2,
transaction_version: 2,
apis: RUNTIME_API_VERSIONS,
Expand Down

0 comments on commit 235f269

Please sign in to comment.