Skip to content
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

Add StaticExtrinsicSigner struct #511

Merged
merged 5 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pallet-staking = { optional = true, git = "https://github.com/paritytech/substra

[dev-dependencies]
node-template-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "master" }

[features]
default = ["std"]
Expand Down
76 changes: 72 additions & 4 deletions primitives/src/extrinsics/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use crate::FrameSystemConfig;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use sp_core::Pair;
use sp_runtime::traits::StaticLookup;
use sp_core::{crypto::AccountId32, Pair};
use sp_runtime::{traits::StaticLookup, MultiAddress};

pub trait SignExtrinsic<AccountId: Clone + Encode> {
type Signature: Encode;
Expand Down Expand Up @@ -102,11 +102,61 @@ where
}
}

/// Extrinsic Signer implementation, that does not enforce Runtime as input.
/// This is especially useful in no-std environments, where the runtime is not
/// available.
#[derive(Encode, Decode, Clone, PartialEq)]
pub struct StaticExtrinsicSigner<Signer, Signature> {
signer: Signer,
account_id: AccountId32,
extrinsic_address: MultiAddress<AccountId32, ()>,
_phantom: PhantomData<Signature>,
}

impl<Signer, Signature> StaticExtrinsicSigner<Signer, Signature>
where
Signer: Pair,
Signer::Public: Into<AccountId32>,
Signature: From<Signer::Signature> + Encode + Clone,
{
pub fn new(signer: Signer) -> Self {
let account_id = signer.public().into();
let extrinsic_address = MultiAddress::from(account_id.clone());
Self { signer, account_id, extrinsic_address, _phantom: Default::default() }
}

pub fn signer(&self) -> &Signer {
&self.signer
}
}

impl<Signer, Signature> SignExtrinsic<AccountId32> for StaticExtrinsicSigner<Signer, Signature>
where
Signer: Pair,
Signature: From<Signer::Signature> + Encode + Clone,
{
type Signature = Signature;
type ExtrinsicAddress = MultiAddress<AccountId32, ()>;

fn sign(&self, payload: &[u8]) -> Self::Signature {
self.signer.sign(payload).into()
}

fn public_account_id(&self) -> &AccountId32 {
&self.account_id
}

fn extrinsic_address(&self) -> Self::ExtrinsicAddress {
self.extrinsic_address.clone()
}
}

#[cfg(test)]
mod tests {
use crate::ExtrinsicSigner;
use node_template_runtime::Runtime;
use super::*;
use node_template_runtime::{Runtime, Signature};
use sp_core::{sr25519, Pair};
use sp_keyring::AccountKeyring;

#[test]
fn test_extrinsic_signer_from_sr25519_pair() {
Expand All @@ -122,4 +172,22 @@ mod tests {

assert_eq!(es_converted.signer.public(), es_new.signer.public());
}

// This test does not work. See issue #504.

// #[test]
// fn test_extrinsic_signer_clone() {
// let pair = AccountKeyring::Alice.pair();
// let signer = ExtrinsicSigner::<_, Signature, Runtime>::new(pair);
//
// let signer2 = signer.clone();
// }

#[test]
fn test_static_extrinsic_signer_clone() {
let pair = AccountKeyring::Alice.pair();
let signer = StaticExtrinsicSigner::<_, Signature>::new(pair);

let _signer2 = signer.clone();
}
}