#![warn(missing_docs)]
use alloc::{boxed::Box, collections::btree_set::BTreeSet, vec::Vec};
use codec::Encode;
use scale_info::TypeInfo;
use sp_runtime::{
app_crypto::RuntimeAppPublic,
traits::{ExtrinsicLike, IdentifyAccount, One},
RuntimeDebug,
};
pub struct ForAll {}
pub struct ForAny {}
pub struct SubmitTransaction<T: CreateTransactionBase<RuntimeCall>, RuntimeCall> {
_phantom: core::marker::PhantomData<(T, RuntimeCall)>,
}
impl<T, LocalCall> SubmitTransaction<T, LocalCall>
where
T: CreateTransactionBase<LocalCall>,
{
pub fn submit_transaction(xt: T::Extrinsic) -> Result<(), ()> {
sp_io::offchain::submit_transaction(xt.encode())
}
}
#[derive(RuntimeDebug)]
pub struct Signer<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>, X = ForAny> {
accounts: Option<Vec<T::Public>>,
_phantom: core::marker::PhantomData<(X, C)>,
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>, X> Default for Signer<T, C, X> {
fn default() -> Self {
Self { accounts: Default::default(), _phantom: Default::default() }
}
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>, X> Signer<T, C, X> {
pub fn all_accounts() -> Signer<T, C, ForAll> {
Default::default()
}
pub fn any_account() -> Signer<T, C, ForAny> {
Default::default()
}
pub fn with_filter(mut self, accounts: Vec<T::Public>) -> Self {
self.accounts = Some(accounts);
self
}
pub fn can_sign(&self) -> bool {
self.accounts_from_keys().count() > 0
}
pub fn accounts_from_keys<'a>(&'a self) -> Box<dyn Iterator<Item = Account<T>> + 'a> {
let keystore_accounts = Self::keystore_accounts();
match self.accounts {
None => Box::new(keystore_accounts),
Some(ref keys) => {
let keystore_lookup: BTreeSet<<T as SigningTypes>::Public> =
keystore_accounts.map(|account| account.public).collect();
Box::new(
keys.iter()
.enumerate()
.map(|(index, key)| {
let account_id = key.clone().into_account();
Account::new(index, account_id, key.clone())
})
.filter(move |account| keystore_lookup.contains(&account.public)),
)
},
}
}
pub fn keystore_accounts() -> impl Iterator<Item = Account<T>> {
C::RuntimeAppPublic::all().into_iter().enumerate().map(|(index, key)| {
let generic_public = C::GenericPublic::from(key);
let public: T::Public = generic_public.into();
let account_id = public.clone().into_account();
Account::new(index, account_id, public)
})
}
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>> Signer<T, C, ForAll> {
fn for_all<F, R>(&self, f: F) -> Vec<(Account<T>, R)>
where
F: Fn(&Account<T>) -> Option<R>,
{
let accounts = self.accounts_from_keys();
accounts
.into_iter()
.filter_map(|account| f(&account).map(|res| (account, res)))
.collect()
}
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>> Signer<T, C, ForAny> {
fn for_any<F, R>(&self, f: F) -> Option<(Account<T>, R)>
where
F: Fn(&Account<T>) -> Option<R>,
{
let accounts = self.accounts_from_keys();
for account in accounts.into_iter() {
let res = f(&account);
if let Some(res) = res {
return Some((account, res))
}
}
None
}
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>> SignMessage<T>
for Signer<T, C, ForAll>
{
type SignatureData = Vec<(Account<T>, T::Signature)>;
fn sign_message(&self, message: &[u8]) -> Self::SignatureData {
self.for_all(|account| C::sign(message, account.public.clone()))
}
fn sign<TPayload, F>(&self, f: F) -> Self::SignatureData
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>,
{
self.for_all(|account| f(account).sign::<C>())
}
}
impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>> SignMessage<T>
for Signer<T, C, ForAny>
{
type SignatureData = Option<(Account<T>, T::Signature)>;
fn sign_message(&self, message: &[u8]) -> Self::SignatureData {
self.for_any(|account| C::sign(message, account.public.clone()))
}
fn sign<TPayload, F>(&self, f: F) -> Self::SignatureData
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>,
{
self.for_any(|account| f(account).sign::<C>())
}
}
impl<
T: CreateSignedTransaction<LocalCall> + SigningTypes,
C: AppCrypto<T::Public, T::Signature>,
LocalCall,
> SendSignedTransaction<T, C, LocalCall> for Signer<T, C, ForAny>
{
type Result = Option<(Account<T>, Result<(), ()>)>;
fn send_signed_transaction(&self, f: impl Fn(&Account<T>) -> LocalCall) -> Self::Result {
self.for_any(|account| {
let call = f(account);
self.send_single_signed_transaction(account, call)
})
}
}
impl<
T: SigningTypes + CreateSignedTransaction<LocalCall>,
C: AppCrypto<T::Public, T::Signature>,
LocalCall,
> SendSignedTransaction<T, C, LocalCall> for Signer<T, C, ForAll>
{
type Result = Vec<(Account<T>, Result<(), ()>)>;
fn send_signed_transaction(&self, f: impl Fn(&Account<T>) -> LocalCall) -> Self::Result {
self.for_all(|account| {
let call = f(account);
self.send_single_signed_transaction(account, call)
})
}
}
impl<
T: SigningTypes + CreateInherent<LocalCall>,
C: AppCrypto<T::Public, T::Signature>,
LocalCall,
> SendUnsignedTransaction<T, LocalCall> for Signer<T, C, ForAny>
{
type Result = Option<(Account<T>, Result<(), ()>)>;
fn send_unsigned_transaction<TPayload, F>(
&self,
f: F,
f2: impl Fn(TPayload, T::Signature) -> LocalCall,
) -> Self::Result
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>,
{
self.for_any(|account| {
let payload = f(account);
let signature = payload.sign::<C>()?;
let call = f2(payload, signature);
self.submit_unsigned_transaction(call)
})
}
}
impl<
T: SigningTypes + CreateInherent<LocalCall>,
C: AppCrypto<T::Public, T::Signature>,
LocalCall,
> SendUnsignedTransaction<T, LocalCall> for Signer<T, C, ForAll>
{
type Result = Vec<(Account<T>, Result<(), ()>)>;
fn send_unsigned_transaction<TPayload, F>(
&self,
f: F,
f2: impl Fn(TPayload, T::Signature) -> LocalCall,
) -> Self::Result
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>,
{
self.for_all(|account| {
let payload = f(account);
let signature = payload.sign::<C>()?;
let call = f2(payload, signature);
self.submit_unsigned_transaction(call)
})
}
}
#[derive(RuntimeDebug, PartialEq)]
pub struct Account<T: SigningTypes> {
pub index: usize,
pub id: T::AccountId,
pub public: T::Public,
}
impl<T: SigningTypes> Account<T> {
pub fn new(index: usize, id: T::AccountId, public: T::Public) -> Self {
Self { index, id, public }
}
}
impl<T: SigningTypes> Clone for Account<T>
where
T::AccountId: Clone,
T::Public: Clone,
{
fn clone(&self) -> Self {
Self { index: self.index, id: self.id.clone(), public: self.public.clone() }
}
}
pub trait AppCrypto<Public, Signature> {
type RuntimeAppPublic: RuntimeAppPublic;
type GenericPublic: From<Self::RuntimeAppPublic>
+ Into<Self::RuntimeAppPublic>
+ TryFrom<Public>
+ Into<Public>;
type GenericSignature: From<<Self::RuntimeAppPublic as RuntimeAppPublic>::Signature>
+ Into<<Self::RuntimeAppPublic as RuntimeAppPublic>::Signature>
+ TryFrom<Signature>
+ Into<Signature>;
fn sign(payload: &[u8], public: Public) -> Option<Signature> {
let p: Self::GenericPublic = public.try_into().ok()?;
let x = Into::<Self::RuntimeAppPublic>::into(p);
x.sign(&payload)
.map(|x| {
let sig: Self::GenericSignature = x.into();
sig
})
.map(Into::into)
}
fn verify(payload: &[u8], public: Public, signature: Signature) -> bool {
let p: Self::GenericPublic = match public.try_into() {
Ok(a) => a,
_ => return false,
};
let x = Into::<Self::RuntimeAppPublic>::into(p);
let signature: Self::GenericSignature = match signature.try_into() {
Ok(a) => a,
_ => return false,
};
let signature =
Into::<<Self::RuntimeAppPublic as RuntimeAppPublic>::Signature>::into(signature);
x.verify(&payload, &signature)
}
}
pub trait SigningTypes: crate::Config {
type Public: Clone
+ PartialEq
+ IdentifyAccount<AccountId = Self::AccountId>
+ core::fmt::Debug
+ codec::Codec
+ Ord
+ scale_info::TypeInfo;
type Signature: Clone + PartialEq + core::fmt::Debug + codec::Codec + scale_info::TypeInfo;
}
pub trait CreateTransactionBase<LocalCall> {
type Extrinsic: ExtrinsicLike + Encode;
type RuntimeCall: From<LocalCall> + Encode;
}
pub trait CreateTransaction<LocalCall>: CreateTransactionBase<LocalCall> {
type Extension: TypeInfo;
fn create_transaction(
call: <Self as CreateTransactionBase<LocalCall>>::RuntimeCall,
extension: Self::Extension,
) -> Self::Extrinsic;
}
pub trait CreateSignedTransaction<LocalCall>:
CreateTransactionBase<LocalCall> + SigningTypes
{
fn create_signed_transaction<C: AppCrypto<Self::Public, Self::Signature>>(
call: <Self as CreateTransactionBase<LocalCall>>::RuntimeCall,
public: Self::Public,
account: Self::AccountId,
nonce: Self::Nonce,
) -> Option<Self::Extrinsic>;
}
pub trait CreateInherent<LocalCall>: CreateTransactionBase<LocalCall> {
fn create_inherent(call: Self::RuntimeCall) -> Self::Extrinsic;
}
pub trait SignMessage<T: SigningTypes> {
type SignatureData;
fn sign_message(&self, message: &[u8]) -> Self::SignatureData;
fn sign<TPayload, F>(&self, f: F) -> Self::SignatureData
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>;
}
pub trait SendSignedTransaction<
T: CreateSignedTransaction<LocalCall>,
C: AppCrypto<T::Public, T::Signature>,
LocalCall,
>
{
type Result;
fn send_signed_transaction(&self, f: impl Fn(&Account<T>) -> LocalCall) -> Self::Result;
fn send_single_signed_transaction(
&self,
account: &Account<T>,
call: LocalCall,
) -> Option<Result<(), ()>> {
let mut account_data = crate::Account::<T>::get(&account.id);
log::debug!(
target: "runtime::offchain",
"Creating signed transaction from account: {:?} (nonce: {:?})",
account.id,
account_data.nonce,
);
let transaction = T::create_signed_transaction::<C>(
call.into(),
account.public.clone(),
account.id.clone(),
account_data.nonce,
)?;
let res = SubmitTransaction::<T, LocalCall>::submit_transaction(transaction);
if res.is_ok() {
account_data.nonce += One::one();
crate::Account::<T>::insert(&account.id, account_data);
}
Some(res)
}
}
pub trait SendUnsignedTransaction<T: SigningTypes + CreateInherent<LocalCall>, LocalCall> {
type Result;
fn send_unsigned_transaction<TPayload, F>(
&self,
f: F,
f2: impl Fn(TPayload, T::Signature) -> LocalCall,
) -> Self::Result
where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>;
fn submit_unsigned_transaction(&self, call: LocalCall) -> Option<Result<(), ()>> {
let xt = T::create_inherent(call.into());
Some(SubmitTransaction::<T, LocalCall>::submit_transaction(xt))
}
}
pub trait SignedPayload<T: SigningTypes>: Encode {
fn public(&self) -> T::Public;
fn sign<C: AppCrypto<T::Public, T::Signature>>(&self) -> Option<T::Signature> {
self.using_encoded(|payload| C::sign(payload, self.public()))
}
fn verify<C: AppCrypto<T::Public, T::Signature>>(&self, signature: T::Signature) -> bool {
self.using_encoded(|payload| C::verify(payload, self.public(), signature))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mock::{RuntimeCall, Test as TestRuntime, CALL};
use codec::Decode;
use sp_core::offchain::{testing, TransactionPoolExt};
use sp_runtime::testing::{TestSignature, TestXt, UintAuthorityId};
impl SigningTypes for TestRuntime {
type Public = UintAuthorityId;
type Signature = TestSignature;
}
type Extrinsic = TestXt<RuntimeCall, ()>;
impl CreateTransactionBase<RuntimeCall> for TestRuntime {
type Extrinsic = Extrinsic;
type RuntimeCall = RuntimeCall;
}
impl CreateInherent<RuntimeCall> for TestRuntime {
fn create_inherent(call: Self::RuntimeCall) -> Self::Extrinsic {
Extrinsic::new_bare(call)
}
}
#[derive(codec::Encode, codec::Decode)]
struct SimplePayload {
pub public: UintAuthorityId,
pub data: Vec<u8>,
}
impl SignedPayload<TestRuntime> for SimplePayload {
fn public(&self) -> UintAuthorityId {
self.public.clone()
}
}
struct DummyAppCrypto;
impl AppCrypto<UintAuthorityId, TestSignature> for DummyAppCrypto {
type RuntimeAppPublic = UintAuthorityId;
type GenericPublic = UintAuthorityId;
type GenericSignature = TestSignature;
}
fn assert_account(next: Option<(Account<TestRuntime>, Result<(), ()>)>, index: usize, id: u64) {
assert_eq!(next, Some((Account { index, id, public: id.into() }, Ok(()))));
}
#[test]
fn should_send_unsigned_with_signed_payload_with_all_accounts() {
let (pool, pool_state) = testing::TestTransactionPoolExt::new();
let mut t = sp_io::TestExternalities::default();
t.register_extension(TransactionPoolExt::new(pool));
UintAuthorityId::set_all_keys(vec![0xf0, 0xf1, 0xf2]);
t.execute_with(|| {
let result = Signer::<TestRuntime, DummyAppCrypto>::all_accounts()
.send_unsigned_transaction(
|account| SimplePayload { data: vec![1, 2, 3], public: account.public.clone() },
|_payload, _signature| CALL.clone(),
);
let mut res = result.into_iter();
assert_account(res.next(), 0, 0xf0);
assert_account(res.next(), 1, 0xf1);
assert_account(res.next(), 2, 0xf2);
assert_eq!(res.next(), None);
let tx1 = pool_state.write().transactions.pop().unwrap();
let _tx2 = pool_state.write().transactions.pop().unwrap();
let _tx3 = pool_state.write().transactions.pop().unwrap();
assert!(pool_state.read().transactions.is_empty());
let tx1 = Extrinsic::decode(&mut &*tx1).unwrap();
assert!(tx1.is_inherent());
});
}
#[test]
fn should_send_unsigned_with_signed_payload_with_any_account() {
let (pool, pool_state) = testing::TestTransactionPoolExt::new();
let mut t = sp_io::TestExternalities::default();
t.register_extension(TransactionPoolExt::new(pool));
UintAuthorityId::set_all_keys(vec![0xf0, 0xf1, 0xf2]);
t.execute_with(|| {
let result = Signer::<TestRuntime, DummyAppCrypto>::any_account()
.send_unsigned_transaction(
|account| SimplePayload { data: vec![1, 2, 3], public: account.public.clone() },
|_payload, _signature| CALL.clone(),
);
let mut res = result.into_iter();
assert_account(res.next(), 0, 0xf0);
assert_eq!(res.next(), None);
let tx1 = pool_state.write().transactions.pop().unwrap();
assert!(pool_state.read().transactions.is_empty());
let tx1 = Extrinsic::decode(&mut &*tx1).unwrap();
assert!(tx1.is_inherent());
});
}
#[test]
fn should_send_unsigned_with_signed_payload_with_all_account_and_filter() {
let (pool, pool_state) = testing::TestTransactionPoolExt::new();
let mut t = sp_io::TestExternalities::default();
t.register_extension(TransactionPoolExt::new(pool));
UintAuthorityId::set_all_keys(vec![0xf0, 0xf1, 0xf2]);
t.execute_with(|| {
let result = Signer::<TestRuntime, DummyAppCrypto>::all_accounts()
.with_filter(vec![0xf2.into(), 0xf1.into()])
.send_unsigned_transaction(
|account| SimplePayload { data: vec![1, 2, 3], public: account.public.clone() },
|_payload, _signature| CALL.clone(),
);
let mut res = result.into_iter();
assert_account(res.next(), 0, 0xf2);
assert_account(res.next(), 1, 0xf1);
assert_eq!(res.next(), None);
let tx1 = pool_state.write().transactions.pop().unwrap();
let _tx2 = pool_state.write().transactions.pop().unwrap();
assert!(pool_state.read().transactions.is_empty());
let tx1 = Extrinsic::decode(&mut &*tx1).unwrap();
assert!(tx1.is_inherent());
});
}
#[test]
fn should_send_unsigned_with_signed_payload_with_any_account_and_filter() {
let (pool, pool_state) = testing::TestTransactionPoolExt::new();
let mut t = sp_io::TestExternalities::default();
t.register_extension(TransactionPoolExt::new(pool));
UintAuthorityId::set_all_keys(vec![0xf0, 0xf1, 0xf2]);
t.execute_with(|| {
let result = Signer::<TestRuntime, DummyAppCrypto>::any_account()
.with_filter(vec![0xf2.into(), 0xf1.into()])
.send_unsigned_transaction(
|account| SimplePayload { data: vec![1, 2, 3], public: account.public.clone() },
|_payload, _signature| CALL.clone(),
);
let mut res = result.into_iter();
assert_account(res.next(), 0, 0xf2);
assert_eq!(res.next(), None);
let tx1 = pool_state.write().transactions.pop().unwrap();
assert!(pool_state.read().transactions.is_empty());
let tx1 = Extrinsic::decode(&mut &*tx1).unwrap();
assert!(tx1.is_inherent());
});
}
}