mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 15:01:06 +00:00
Bring back SubmitSignedTransaction trait. (#3908)
* Bring back SubmitSignedTransaction. * Fix long lines. * Add missing docs. * Update core/primitives/src/crypto.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update node/runtime/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update core/primitives/src/crypto.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
Gavin Wood
parent
e05e624a3a
commit
cecf3a1438
@@ -790,6 +790,9 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
|
|||||||
root.derive(path, Some(seed)).map_err(|_| SecretStringError::InvalidPath)
|
root.derive(path, Some(seed)).map_err(|_| SecretStringError::InvalidPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Interprets the string `s` in order to generate a key pair.
|
||||||
|
///
|
||||||
|
/// See [`from_string_with_seed`](Self::from_string_with_seed) for more extensive documentation.
|
||||||
fn from_string(s: &str, password_override: Option<&str>) -> Result<Self, SecretStringError> {
|
fn from_string(s: &str, password_override: Option<&str>) -> Result<Self, SecretStringError> {
|
||||||
Self::from_string_with_seed(s, password_override).map(|x| x.0)
|
Self::from_string_with_seed(s, password_override).map(|x| x.0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,18 +255,39 @@ impl From<ed25519::Public> for MultiSigner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<MultiSigner> for ed25519::Public {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
|
||||||
|
if let MultiSigner::Ed25519(x) = m { Ok(x) } else { Err(()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<sr25519::Public> for MultiSigner {
|
impl From<sr25519::Public> for MultiSigner {
|
||||||
fn from(x: sr25519::Public) -> Self {
|
fn from(x: sr25519::Public) -> Self {
|
||||||
MultiSigner::Sr25519(x)
|
MultiSigner::Sr25519(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<MultiSigner> for sr25519::Public {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
|
||||||
|
if let MultiSigner::Sr25519(x) = m { Ok(x) } else { Err(()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<ecdsa::Public> for MultiSigner {
|
impl From<ecdsa::Public> for MultiSigner {
|
||||||
fn from(x: ecdsa::Public) -> Self {
|
fn from(x: ecdsa::Public) -> Self {
|
||||||
MultiSigner::Ecdsa(x)
|
MultiSigner::Ecdsa(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<MultiSigner> for ecdsa::Public {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(m: MultiSigner) -> Result<Self, Self::Error> {
|
||||||
|
if let MultiSigner::Ecdsa(x) = m { Ok(x) } else { Err(()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl std::fmt::Display for MultiSigner {
|
impl std::fmt::Display for MultiSigner {
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ use sr_primitives::curve::PiecewiseLinear;
|
|||||||
use sr_primitives::transaction_validity::TransactionValidity;
|
use sr_primitives::transaction_validity::TransactionValidity;
|
||||||
use sr_primitives::weights::Weight;
|
use sr_primitives::weights::Weight;
|
||||||
use sr_primitives::traits::{
|
use sr_primitives::traits::{
|
||||||
BlakeTwo256, Block as BlockT, NumberFor, StaticLookup,
|
self, BlakeTwo256, Block as BlockT, NumberFor, StaticLookup, SaturatedConversion,
|
||||||
};
|
};
|
||||||
use version::RuntimeVersion;
|
use version::RuntimeVersion;
|
||||||
#[cfg(any(feature = "std", test))]
|
#[cfg(any(feature = "std", test))]
|
||||||
@@ -459,6 +459,36 @@ impl finality_tracker::Trait for Runtime {
|
|||||||
type ReportLatency = ReportLatency;
|
type ReportLatency = ReportLatency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtime {
|
||||||
|
type Public = <Signature as traits::Verify>::Signer;
|
||||||
|
type Signature = Signature;
|
||||||
|
|
||||||
|
fn create_transaction<F: system::offchain::Signer<Self::Public, Self::Signature>>(
|
||||||
|
call: Call,
|
||||||
|
public: Self::Public,
|
||||||
|
account: AccountId,
|
||||||
|
index: Index,
|
||||||
|
) -> Option<(Call, <UncheckedExtrinsic as traits::Extrinsic>::SignaturePayload)> {
|
||||||
|
let period = 1 << 8;
|
||||||
|
let current_block = System::block_number().saturated_into::<u64>();
|
||||||
|
let tip = 0;
|
||||||
|
let extra: SignedExtra = (
|
||||||
|
system::CheckVersion::<Runtime>::new(),
|
||||||
|
system::CheckGenesis::<Runtime>::new(),
|
||||||
|
system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
|
||||||
|
system::CheckNonce::<Runtime>::from(index),
|
||||||
|
system::CheckWeight::<Runtime>::new(),
|
||||||
|
transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
|
||||||
|
Default::default(),
|
||||||
|
);
|
||||||
|
let raw_payload = SignedPayload::new(call, extra).ok()?;
|
||||||
|
let signature = F::sign(public, &raw_payload)?;
|
||||||
|
let address = Indices::unlookup(account);
|
||||||
|
let (call, extra, _) = raw_payload.deconstruct();
|
||||||
|
Some((call, (address, signature, extra)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
construct_runtime!(
|
construct_runtime!(
|
||||||
pub enum Runtime where
|
pub enum Runtime where
|
||||||
Block = Block,
|
Block = Block,
|
||||||
@@ -669,3 +699,24 @@ impl_runtime_apis! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use system::offchain::SubmitSignedTransaction;
|
||||||
|
|
||||||
|
fn is_submit_signed_transaction<T>(_arg: T) where
|
||||||
|
T: SubmitSignedTransaction<
|
||||||
|
Runtime,
|
||||||
|
Call,
|
||||||
|
Extrinsic=UncheckedExtrinsic,
|
||||||
|
CreateTransaction=Runtime,
|
||||||
|
Signer=ImOnlineId,
|
||||||
|
>,
|
||||||
|
{}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_bounds() {
|
||||||
|
let x = SubmitTransaction::default();
|
||||||
|
is_submit_signed_transaction(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,12 +96,6 @@ pub mod sr25519 {
|
|||||||
mod app_sr25519 {
|
mod app_sr25519 {
|
||||||
use app_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
|
use app_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
|
||||||
app_crypto!(sr25519, IM_ONLINE);
|
app_crypto!(sr25519, IM_ONLINE);
|
||||||
|
|
||||||
impl From<Signature> for sr_primitives::AnySignature {
|
|
||||||
fn from(sig: Signature) -> Self {
|
|
||||||
sr25519::Signature::from(sig).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An i'm online keypair using sr25519 as its crypto.
|
/// An i'm online keypair using sr25519 as its crypto.
|
||||||
@@ -119,12 +113,6 @@ pub mod ed25519 {
|
|||||||
mod app_ed25519 {
|
mod app_ed25519 {
|
||||||
use app_crypto::{app_crypto, key_types::IM_ONLINE, ed25519};
|
use app_crypto::{app_crypto, key_types::IM_ONLINE, ed25519};
|
||||||
app_crypto!(ed25519, IM_ONLINE);
|
app_crypto!(ed25519, IM_ONLINE);
|
||||||
|
|
||||||
impl From<Signature> for sr_primitives::AnySignature {
|
|
||||||
fn from(sig: Signature) -> Self {
|
|
||||||
ed25519::Signature::from(sig).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An i'm online keypair using ed25519 as its crypto.
|
/// An i'm online keypair using ed25519 as its crypto.
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
//! Module helpers for offchain calls.
|
//! Module helpers for offchain calls.
|
||||||
|
|
||||||
use codec::Encode;
|
use codec::Encode;
|
||||||
use sr_primitives::app_crypto::RuntimeAppPublic;
|
use sr_primitives::app_crypto::{self, RuntimeAppPublic};
|
||||||
use sr_primitives::traits::Extrinsic as ExtrinsicT;
|
use sr_primitives::traits::{Extrinsic as ExtrinsicT, IdentifyAccount};
|
||||||
|
|
||||||
/// A trait responsible for signing a payload using given account.
|
/// A trait responsible for signing a payload using given account.
|
||||||
pub trait Signer<Public, Signature> {
|
pub trait Signer<Public, Signature> {
|
||||||
@@ -28,17 +28,96 @@ pub trait Signer<Public, Signature> {
|
|||||||
fn sign<Payload: Encode>(public: Public, payload: &Payload) -> Option<Signature>;
|
fn sign<Payload: Encode>(public: Public, payload: &Payload) -> Option<Signature>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A `Signer` implementation for any `AppPublic` type.
|
||||||
|
///
|
||||||
|
/// This implementation additionaly supports conversion to/from multi-signature/multi-signer
|
||||||
|
/// wrappers.
|
||||||
|
/// If the wrapped crypto doesn't match `AppPublic`s crypto `None` is returned.
|
||||||
impl<Public, Signature, AppPublic> Signer<Public, Signature> for AppPublic where
|
impl<Public, Signature, AppPublic> Signer<Public, Signature> for AppPublic where
|
||||||
AppPublic: RuntimeAppPublic + From<Public>,
|
AppPublic: RuntimeAppPublic
|
||||||
AppPublic::Signature: Into<Signature>,
|
+ app_crypto::AppPublic
|
||||||
|
+ From<<AppPublic as app_crypto::AppPublic>::Generic>,
|
||||||
|
<AppPublic as RuntimeAppPublic>::Signature: app_crypto::AppSignature,
|
||||||
|
Signature: From<
|
||||||
|
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>::Generic
|
||||||
|
>,
|
||||||
|
Public: rstd::convert::TryInto<<AppPublic as app_crypto::AppPublic>::Generic>
|
||||||
{
|
{
|
||||||
fn sign<Payload: Encode>(public: Public, raw_payload: &Payload) -> Option<Signature> {
|
fn sign<Payload: Encode>(public: Public, raw_payload: &Payload) -> Option<Signature> {
|
||||||
raw_payload.using_encoded(|payload| {
|
raw_payload.using_encoded(|payload| {
|
||||||
AppPublic::from(public).sign(&payload).map(Into::into)
|
let public = public.try_into().ok()?;
|
||||||
|
AppPublic::from(public).sign(&payload)
|
||||||
|
.map(
|
||||||
|
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>
|
||||||
|
::Generic::from
|
||||||
|
)
|
||||||
|
.map(Signature::from)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates runtime-specific signed transaction.
|
||||||
|
pub trait CreateTransaction<T: crate::Trait, Extrinsic: ExtrinsicT> {
|
||||||
|
/// A `Public` key representing a particular `AccountId`.
|
||||||
|
type Public;
|
||||||
|
/// A `Signature` generated by the `Signer`.
|
||||||
|
type Signature;
|
||||||
|
|
||||||
|
/// Attempt to create signed extrinsic data that encodes call from given account.
|
||||||
|
///
|
||||||
|
/// Runtime implementation is free to construct the payload to sign and the signature
|
||||||
|
/// in any way it wants.
|
||||||
|
/// Returns `None` if signed extrinsic could not be created (either because signing failed
|
||||||
|
/// or because of any other runtime-specific reason).
|
||||||
|
fn create_transaction<F: Signer<Self::Public, Self::Signature>>(
|
||||||
|
call: Extrinsic::Call,
|
||||||
|
public: Self::Public,
|
||||||
|
account: T::AccountId,
|
||||||
|
nonce: T::Index,
|
||||||
|
) -> Option<(Extrinsic::Call, Extrinsic::SignaturePayload)>;
|
||||||
|
}
|
||||||
|
|
||||||
|
type PublicOf<T, Call, X> = <
|
||||||
|
<X as SubmitSignedTransaction<T, Call>>::CreateTransaction as CreateTransaction<
|
||||||
|
T,
|
||||||
|
<X as SubmitSignedTransaction<T, Call>>::Extrinsic,
|
||||||
|
>
|
||||||
|
>::Public;
|
||||||
|
|
||||||
|
/// A trait to sign and submit transactions in offchain calls.
|
||||||
|
pub trait SubmitSignedTransaction<T: crate::Trait, Call>
|
||||||
|
where
|
||||||
|
PublicOf<T, Call, Self>: IdentifyAccount<AccountId=T::AccountId> + Clone,
|
||||||
|
{
|
||||||
|
/// Unchecked extrinsic type.
|
||||||
|
type Extrinsic: ExtrinsicT<Call=Call> + codec::Encode;
|
||||||
|
|
||||||
|
/// A runtime-specific type to produce signed data for the extrinsic.
|
||||||
|
type CreateTransaction: CreateTransaction<T, Self::Extrinsic>;
|
||||||
|
|
||||||
|
/// A type used to sign transactions created using `CreateTransaction`.
|
||||||
|
type Signer: Signer<
|
||||||
|
PublicOf<T, Call, Self>,
|
||||||
|
<Self::CreateTransaction as CreateTransaction<T, Self::Extrinsic>>::Signature,
|
||||||
|
>;
|
||||||
|
|
||||||
|
/// Sign given call and submit it to the transaction pool.
|
||||||
|
///
|
||||||
|
/// Returns `Ok` if the transaction was submitted correctly
|
||||||
|
/// and `Err` if the key for given `id` was not found or the
|
||||||
|
/// transaction was rejected from the pool.
|
||||||
|
fn sign_and_submit(call: impl Into<Call>, public: PublicOf<T, Call, Self>) -> Result<(), ()> {
|
||||||
|
let call = call.into();
|
||||||
|
let id = public.clone().into_account();
|
||||||
|
let expected = <crate::Module<T>>::account_nonce(&id);
|
||||||
|
let (call, signature_data) = Self::CreateTransaction
|
||||||
|
::create_transaction::<Self::Signer>(call, public, id, expected)
|
||||||
|
.ok_or(())?;
|
||||||
|
let xt = Self::Extrinsic::new(call, Some(signature_data)).ok_or(())?;
|
||||||
|
runtime_io::submit_transaction(xt.encode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A trait to submit unsigned transactions in offchain calls.
|
/// A trait to submit unsigned transactions in offchain calls.
|
||||||
pub trait SubmitUnsignedTransaction<T: crate::Trait, Call> {
|
pub trait SubmitUnsignedTransaction<T: crate::Trait, Call> {
|
||||||
/// Unchecked extrinsic type.
|
/// Unchecked extrinsic type.
|
||||||
@@ -67,6 +146,19 @@ impl<S, C, E> Default for TransactionSubmitter<S, C, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A blanket implementation to simplify creation of transaction signer & submitter in the runtime.
|
||||||
|
impl<T, E, S, C, Call> SubmitSignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
|
||||||
|
T: crate::Trait,
|
||||||
|
C: CreateTransaction<T, E>,
|
||||||
|
S: Signer<<C as CreateTransaction<T, E>>::Public, <C as CreateTransaction<T, E>>::Signature>,
|
||||||
|
E: ExtrinsicT<Call=Call> + codec::Encode,
|
||||||
|
<C as CreateTransaction<T, E>>::Public: IdentifyAccount<AccountId=T::AccountId> + Clone,
|
||||||
|
{
|
||||||
|
type Extrinsic = E;
|
||||||
|
type CreateTransaction = C;
|
||||||
|
type Signer = S;
|
||||||
|
}
|
||||||
|
|
||||||
/// A blanket impl to use the same submitter for usigned transactions as well.
|
/// A blanket impl to use the same submitter for usigned transactions as well.
|
||||||
impl<T, E, S, C, Call> SubmitUnsignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
|
impl<T, E, S, C, Call> SubmitUnsignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
|
||||||
T: crate::Trait,
|
T: crate::Trait,
|
||||||
|
|||||||
Reference in New Issue
Block a user