-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,162 additions
and
969 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::{common::Signature, did, util::Action}; | ||
use codec::Encode; | ||
|
||
use super::ToStateChange; | ||
|
||
/// Authorizes action performed by `Self` over supplied target using given key. | ||
pub trait AuthorizeTarget<Target, Key> { | ||
fn ensure_authorizes_target<T: crate::did::Config, A>( | ||
&self, | ||
_: &Key, | ||
_: &A, | ||
) -> Result<(), crate::did::Error<T>> | ||
where | ||
A: Action<Target = Target>, | ||
{ | ||
Ok(()) | ||
} | ||
} | ||
|
||
type AuthorizationResult<T, S> = Result< | ||
Option<Authorization<<S as Signature>::Signer, <S as Signature>::Key>>, | ||
crate::did::Error<T>, | ||
>; | ||
|
||
/// Authorizes signed action. | ||
pub trait AuthorizeSignedAction<A: Action>: Signature | ||
where | ||
Self::Signer: AuthorizeTarget<A::Target, Self::Key>, | ||
{ | ||
fn authorizes_signed_action<T: crate::did::Config>( | ||
&self, | ||
action: &A, | ||
) -> AuthorizationResult<T, Self> | ||
where | ||
A: ToStateChange<T>, | ||
{ | ||
let signer_pubkey = self.key::<T>().ok_or(did::Error::<T>::NoKeyForDid)?; | ||
let encoded_state_change = action.to_state_change().encode(); | ||
|
||
self.signer() | ||
.ensure_authorizes_target(&signer_pubkey, action)?; | ||
|
||
self.verify_raw_bytes(&encoded_state_change, &signer_pubkey) | ||
.map_err(Into::into) | ||
.map(|yes| { | ||
yes.then(|| Authorization { | ||
signer: self.signer(), | ||
key: signer_pubkey, | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
/// Successfully authorized signer along with its key. | ||
pub struct Authorization<S, K> { | ||
pub signer: S, | ||
pub key: K, | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{ | ||
common::{Authorization, AuthorizeSignedAction, AuthorizeTarget, ToStateChange}, | ||
did::*, | ||
util::{action::*, with_nonce::*, WrappedActionWithNonce}, | ||
}; | ||
use core::ops::Deref; | ||
|
||
impl<T: Config, A, Sig> SignedActionWithNonce<T, A, Sig> | ||
where | ||
A: ActionWithNonce<T> + ToStateChange<T>, | ||
Sig: AuthorizeSignedAction<A>, | ||
Sig::Signer: AuthorizeTarget<A::Target, Sig::Key> + Deref, | ||
{ | ||
/// Verifies signer's signature and nonce, then executes given action providing a mutable reference to the | ||
/// value associated with the target. | ||
/// In case of a successful result, commits all storage changes and increases the signer's nonce. | ||
pub fn execute<F, S, R, E>(self, f: F) -> Result<R, E> | ||
where | ||
F: FnOnce(A, &mut <A::Target as StorageRef<T>>::Value, Sig::Signer) -> Result<R, E>, | ||
E: From<ActionExecutionError> + From<NonceError> + From<Error<T>>, | ||
A::Target: StorageRef<T>, | ||
<Sig::Signer as Deref>::Target: StorageRef<T, Value = WithNonce<T, S>> + Clone, | ||
{ | ||
self.execute_removable(|action, data, actor| f(action, data.as_mut().unwrap(), actor)) | ||
} | ||
|
||
/// Verifies signer's signature and nonce, then executes given action providing a mutable reference to the | ||
/// option containing a value associated with the target. | ||
/// In case of a successful result, commits all storage changes and increases the signer's nonce. | ||
pub fn execute_removable<F, S, R, E>(self, f: F) -> Result<R, E> | ||
where | ||
F: FnOnce(A, &mut Option<<A::Target as StorageRef<T>>::Value>, Sig::Signer) -> Result<R, E>, | ||
E: From<ActionExecutionError> + From<NonceError> + From<Error<T>>, | ||
A::Target: StorageRef<T>, | ||
<Sig::Signer as Deref>::Target: StorageRef<T, Value = WithNonce<T, S>> + Clone, | ||
{ | ||
let SignedActionWithNonce { | ||
action, signature, .. | ||
} = self; | ||
|
||
let Authorization { signer, .. } = signature | ||
.authorizes_signed_action(&action)? | ||
.ok_or(Error::<T>::InvalidSignature)?; | ||
|
||
WrappedActionWithNonce::<T, _, _>::new(action.nonce(), (*signer).clone(), action) | ||
.execute_and_increase_nonce(|WrappedActionWithNonce { action, .. }, _| { | ||
action.execute_removable(|action, target_data| f(action, target_data, signer)) | ||
}) | ||
.map_err(Into::into) | ||
} | ||
} |
Oops, something went wrong.