Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Jan 23, 2024
1 parent 727fc60 commit 74ecc63
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 174 deletions.
6 changes: 3 additions & 3 deletions pallets/core/src/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{common::Signature, did, util::Action};
use codec::Encode;
use core::ops::Deref;

use super::ToStateChange;
use super::{GetKey, ToStateChange};

/// Authorizes action performed by `Self` over supplied target using given key.
pub trait AuthorizeTarget<Target, Key> {
Expand All @@ -20,7 +20,7 @@ type AuthorizationResult<T, S> =
Result<Option<Authorization<<S as Signature>::Signer, <S as Signature>::Key>>, did::Error<T>>;

/// Signature that can authorize a signed action.
pub trait AuthorizeSignedAction<A: Action>: Signature
pub trait AuthorizeSignedAction<A: Action>: Signature + GetKey<Self::Key>
where
Self::Signer: AuthorizeTarget<A::Target, Self::Key> + Deref,
<Self::Signer as Deref>::Target: AuthorizeTarget<A::Target, Self::Key>,
Expand Down Expand Up @@ -48,7 +48,7 @@ where

impl<A: Action, S> AuthorizeSignedAction<A> for S
where
S: Signature,
S: Signature + GetKey<S::Key>,
S::Signer: AuthorizeTarget<A::Target, S::Key> + Deref,
<S::Signer as Deref>::Target: AuthorizeTarget<A::Target, S::Key>,
{
Expand Down
23 changes: 6 additions & 17 deletions pallets/core/src/common/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::keys::PublicKey;
use crate::{
did::{self, DidMethodKey},
did::DidMethodKey,
util::{Bytes64, Bytes65},
};
use codec::{Decode, Encode, MaxEncodedLen};
Expand All @@ -10,18 +10,12 @@ use sp_core::{ed25519, sr25519, Pair};
use sp_runtime::traits::Verify;
use sp_std::{borrow::Borrow, convert::TryInto};

/// Signature entity.
pub trait Signature: Sized {
type Signer: Clone;
type Key;
pub use crate::util::signature::*;

fn signer(&self) -> Option<Self::Signer>;

fn key<T: did::Config>(&self) -> Option<Self::Key>;

fn verify_bytes<M>(&self, message: M, key: &Self::Key) -> Result<bool, VerificationError>
where
M: AsRef<[u8]>;
/// Attempts to retrieve a key associated with the given entity.
pub trait GetKey<K> {
/// Attempts to get an associated key.
fn key<T: crate::did::Config>(&self) -> Option<K>;
}

#[derive(PartialEq, Eq, Encode, Decode, Clone, Debug, Default)]
Expand Down Expand Up @@ -181,11 +175,6 @@ pub enum SigValue {
Secp256k1(Bytes65),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerificationError {
IncompatibleKey,
}

impl SigValue {
/// Get weight for signature verification.
/// Considers the type of signature. Disregards message size as messages are hashed giving the
Expand Down
10 changes: 5 additions & 5 deletions pallets/core/src/common/signed_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::{Authorization, AuthorizeSignedAction, AuthorizeTarget, ToStateChange},
did::*,
util::{action::*, with_nonce::*, WrappedActionWithNonce},
util::{action::*, with_nonce::*, ActionWrapper},
};
use core::ops::Deref;

Expand Down Expand Up @@ -30,8 +30,8 @@ where
.authorizes_signed_action(&action)?
.ok_or(Error::<T>::InvalidSignature)?;

WrappedActionWithNonce::<T, _, _>::new(action.nonce(), (*signer).clone(), action)
.execute_and_increase_nonce(|WrappedActionWithNonce { action, .. }, _| {
ActionWrapper::<T, _, _>::new(action.nonce(), (*signer).clone(), action)
.execute_and_increase_nonce(|ActionWrapper { action, .. }, _| {
action.execute_readonly(|action, target_data| f(action, target_data, signer))
})
.map_err(Into::into)
Expand Down Expand Up @@ -68,8 +68,8 @@ where
.authorizes_signed_action(&action)?
.ok_or(Error::<T>::InvalidSignature)?;

WrappedActionWithNonce::<T, _, _>::new(action.nonce(), (*signer).clone(), action)
.execute_and_increase_nonce(|WrappedActionWithNonce { action, .. }, _| {
ActionWrapper::<T, _, _>::new(action.nonce(), (*signer).clone(), action)
.execute_and_increase_nonce(|ActionWrapper { action, .. }, _| {
action.execute_removable(|action, target_data| f(action, target_data, signer))
})
.map_err(Into::into)
Expand Down
28 changes: 14 additions & 14 deletions pallets/core/src/modules/accumulator/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ crate::bench_with_all_pairs! {
Default::default(),
).unwrap();

WrappedActionWithNonce::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
ActionWrapper::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
params: AccumulatorParameters {
curve_type: CurveType::Bls12381,
bytes: vec![3; MAX_PARAMS as usize].try_into().unwrap(),
Expand Down Expand Up @@ -98,7 +98,7 @@ crate::bench_with_all_pairs! {
Default::default(),
).unwrap();

WrappedActionWithNonce::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
ActionWrapper::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
params: AccumulatorParameters {
curve_type: CurveType::Bls12381,
bytes: vec![3; MAX_PARAMS as usize].try_into().unwrap(),
Expand Down Expand Up @@ -138,20 +138,20 @@ crate::bench_with_all_pairs! {
Default::default(),
).unwrap();

WrappedActionWithNonce::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
ActionWrapper::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
params: AccumulatorParameters {
curve_type: CurveType::Bls12381,
bytes: vec![3; MAX_PARAMS as usize].try_into().unwrap(),
label: Some(vec![1; MAX_LABEL as usize].try_into().unwrap()),
},
nonce: 1u8.into()
}).execute::<T, _, _, _, _>(
|WrappedActionWithNonce { action, .. }, accumulator| {
|ActionWrapper { action, .. }, accumulator| {
Pallet::<T>::add_params_(action, accumulator, AccumulatorOwner(did.into()))
},
).unwrap();

WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorPublicKey {
Expand All @@ -163,7 +163,7 @@ crate::bench_with_all_pairs! {
},
nonce: 1u8.into()
}).execute::<T, _, _, _, _>(
|WrappedActionWithNonce { action, .. }, accumulator| {
|ActionWrapper { action, .. }, accumulator| {
Pallet::<T>::add_public_key_(action, accumulator, AccumulatorOwner(did.into()))
}
).unwrap();
Expand Down Expand Up @@ -202,20 +202,20 @@ crate::bench_with_all_pairs! {

let acc_id: AccumulatorId = AccumulatorId([1; 32]);

WrappedActionWithNonce::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
ActionWrapper::<T, _, _>::new(1u8.into(), AccumulatorOwner(did.into()), AddAccumulatorParams {
params: AccumulatorParameters {
curve_type: CurveType::Bls12381,
bytes: vec![3; MAX_PARAMS as usize].try_into().unwrap(),
label: Some(vec![1; MAX_LABEL as usize].try_into().unwrap()),
},
nonce: 1u8.into()
}).execute::<T, _, _, _, _>(
|WrappedActionWithNonce { action, .. }, accumulator| {
|ActionWrapper { action, .. }, accumulator| {
Pallet::<T>::add_params_(action, accumulator, AccumulatorOwner(did.into()))
},
).unwrap();

WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorPublicKey {
Expand All @@ -227,7 +227,7 @@ crate::bench_with_all_pairs! {
},
nonce: 1u8.into()
}).execute::<T, _, _, _, _>(
|WrappedActionWithNonce { action, .. }, accumulator| {
|ActionWrapper { action, .. }, accumulator| {
Pallet::<T>::add_public_key_(action, accumulator, AccumulatorOwner(did.into()))
}
).unwrap();
Expand Down Expand Up @@ -272,7 +272,7 @@ crate::bench_with_all_pairs! {

let acc_id: AccumulatorId = AccumulatorId([1; 32]);

WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorParams {
Expand All @@ -286,7 +286,7 @@ crate::bench_with_all_pairs! {
).execute::<T, _, _, _, _>(|action, counters| Pallet::<T>::add_params_(action.action, counters, AccumulatorOwner(did.into()))).unwrap();


WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorPublicKey {
Expand Down Expand Up @@ -343,7 +343,7 @@ crate::bench_with_all_pairs! {
let acc_id: AccumulatorId = AccumulatorId([2; 32]);


WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorParams {
Expand All @@ -357,7 +357,7 @@ crate::bench_with_all_pairs! {
).execute::<T, _, _, _, _>(|action, counters| Pallet::<T>::add_params_(action.action, counters, AccumulatorOwner(did.into()))).unwrap();


WrappedActionWithNonce::<T, _, _>::new(
ActionWrapper::<T, _, _>::new(
1u8.into(),
AccumulatorOwner(did.into()),
AddAccumulatorPublicKey {
Expand Down
32 changes: 8 additions & 24 deletions pallets/core/src/modules/accumulator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
common::{self, signatures::ForSigType, CurveType, Signature},
common::{self, signatures::ForSigType, CurveType},
did,
did::{Did, DidOrDidMethodKeySignature},
util::{ActionWithNonce, Bytes, IncId, WrappedActionWithNonce},
util::{ActionWithNonce, ActionWrapper, Bytes, IncId},
};
pub use actions::*;
use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -154,17 +154,9 @@ pub mod pallet {
) -> DispatchResult {
ensure_signed(origin)?;

WrappedActionWithNonce::new(
params.nonce(),
signature.signer().ok_or(did::Error::<T>::InvalidSigner)?,
params,
)
.signed(signature)
.execute(
|WrappedActionWithNonce { action, .. }, counters, accumulator_owner| {
Self::add_params_(action, counters, accumulator_owner)
},
)
params
.signed_with_signer_target(signature)?
.execute(ActionWrapper::wrap_fn(Self::add_params_))
}

#[pallet::weight(SubstrateWeight::<T>::add_public(public_key, signature))]
Expand All @@ -175,17 +167,9 @@ pub mod pallet {
) -> DispatchResult {
ensure_signed(origin)?;

WrappedActionWithNonce::new(
public_key.nonce(),
signature.signer().ok_or(did::Error::<T>::InvalidSigner)?,
public_key,
)
.signed(signature)
.execute(
|WrappedActionWithNonce { action, .. }, counters, accumulator_owner| {
Self::add_public_key_(action, counters, accumulator_owner)
},
)
public_key
.signed_with_signer_target(signature)?
.execute(ActionWrapper::wrap_fn(Self::add_public_key_))
}

#[pallet::weight(SubstrateWeight::<T>::remove_params(remove, signature))]
Expand Down
16 changes: 5 additions & 11 deletions pallets/core/src/modules/attest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! method by specifying an Iri.
use crate::{
common::{signatures::ForSigType, AuthorizeTarget, Limits, Signature, TypesAndLimits},
common::{signatures::ForSigType, AuthorizeTarget, Limits, TypesAndLimits},
did::{self, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
util::{ActionWithNonce, BoundedBytes, OptionExt, StorageRef, WrappedActionWithNonce},
util::{ActionWithNonce, ActionWrapper, BoundedBytes, OptionExt, StorageRef},
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
Expand Down Expand Up @@ -164,15 +164,9 @@ pub mod pallet {
) -> DispatchResult {
ensure_signed(origin)?;

WrappedActionWithNonce::new(
attests.nonce(),
signature.signer().ok_or(did::Error::<T>::InvalidSigner)?,
attests,
)
.signed(signature)
.execute(|WrappedActionWithNonce { action, .. }, attest, attester| {
Self::set_claim_(action, attest, attester)
})
attests
.signed_with_signer_target(signature)?
.execute(ActionWrapper::wrap_fn(Self::set_claim_))
}
}

Expand Down
Loading

0 comments on commit 74ecc63

Please sign in to comment.