Companion PR to #5182 - Substrate offchain signing (#985)

* Implement offchain::SendTransactionTypes

* Fix test-runtime

* Fix Kusama

* Add SendTransactiontypes to Kusama

* impl SendTransactiontypes for parachains and registrar

* Fix typos

* Provided extrinsic type

* Implement AppCrypto for signing transactions

* Add signing capabilities to runtime

* Fix test runtimes

* Implement CreateSignedtransaction for Test

* Implement signing types for test runtimes

* Fix test runtime create transaction impl

* Fix build

* Bump kusama's spec version

* Update substrate

* Use all_accounts
This commit is contained in:
Rakan Alhneiti
2020-04-21 18:50:32 +02:00
committed by GitHub
parent 765c456a10
commit 2e521d30ea
7 changed files with 346 additions and 243 deletions
+32 -14
View File
@@ -31,7 +31,7 @@ use runtime_common::{attestations, claims, parachains, registrar, slots,
BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength,
};
use sp_core::sr25519;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
ApplyExtrinsicResult, Perbill, Perquintill, RuntimeDebug, KeyTypeId,
@@ -41,7 +41,7 @@ use sp_runtime::{
curve::PiecewiseLinear,
traits::{
BlakeTwo256, Block as BlockT, StaticLookup, SignedExtension, OpaqueKeys, ConvertInto,
DispatchInfoOf, Extrinsic as ExtrinsicT, SaturatedConversion,
DispatchInfoOf, Extrinsic as ExtrinsicT, SaturatedConversion, Verify,
},
};
use version::RuntimeVersion;
@@ -56,7 +56,6 @@ use frame_support::{
};
use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
use session::historical as session_historical;
use system::offchain::TransactionSubmitter;
#[cfg(feature = "std")]
pub use staking::StakerStatus;
@@ -153,6 +152,13 @@ impl system::Trait for Runtime {
type OnKilledAccount = ();
}
impl<C> system::offchain::SendTransactionTypes<C> for Runtime where
Call: From<C>,
{
type OverarchingCall = Call;
type Extrinsic = UncheckedExtrinsic;
}
parameter_types! {
pub const EpochDuration: u64 = EPOCH_DURATION_IN_BLOCKS as u64;
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
@@ -303,7 +309,6 @@ impl staking::Trait for Runtime {
type NextNewSession = Session;
type ElectionLookahead = ElectionLookahead;
type Call = Call;
type SubmitTransaction = system::offchain::TransactionSubmitter<(), Runtime, Extrinsic>;
type UnsignedPriority = StakingUnsignedPriority;
}
@@ -331,6 +336,7 @@ parameter_types! {
}
impl parachains::Trait for Runtime {
type AuthorityId = FishermanAuthorityId;
type Origin = Origin;
type Call = Call;
type ParachainCurrency = Balances;
@@ -352,17 +358,22 @@ impl parachains::Trait for Runtime {
>::IdentificationTuple;
type ReportOffence = Offences;
type BlockHashConversion = sp_runtime::traits::Identity;
type SubmitSignedTransaction = TransactionSubmitter<parachain::FishermanId, Runtime, UncheckedExtrinsic>;
}
impl system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtime {
type Public = <primitives::Signature as sp_runtime::traits::Verify>::Signer;
type Signature = primitives::Signature;
pub struct FishermanAuthorityId;
impl system::offchain::AppCrypto<<Signature as Verify>::Signer, Signature> for FishermanAuthorityId {
type RuntimeAppPublic = parachain::FishermanId;
type GenericSignature = sr25519::Signature;
type GenericPublic = sr25519::Public;
}
fn create_transaction<TSigner: system::offchain::Signer<Self::Public, Self::Signature>>(
call: <UncheckedExtrinsic as ExtrinsicT>::Call,
public: Self::Public,
account: <Runtime as system::Trait>::AccountId,
impl<LocalCall> system::offchain::CreateSignedTransaction<LocalCall> for Runtime where
Call: From<LocalCall>,
{
fn create_transaction<C: system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
public: <Signature as Verify>::Signer,
account: AccountId,
nonce: <Runtime as system::Trait>::Index,
) -> Option<(Call, <UncheckedExtrinsic as ExtrinsicT>::SignaturePayload)> {
let period = BlockHashCount::get()
@@ -386,15 +397,22 @@ impl system::offchain::CreateTransaction<Runtime, UncheckedExtrinsic> for Runtim
parachains::ValidateDoubleVoteReports::<Runtime>::new(),
);
let raw_payload = SignedPayload::new(call, extra).map_err(|e| {
debug::warn!("Unable to create signed payload: {:?}", e)
debug::warn!("Unable to create signed payload: {:?}", e);
}).ok()?;
let signature = TSigner::sign(public, &raw_payload)?;
let signature = raw_payload.using_encoded(|payload| {
C::sign(payload, public)
})?;
let (call, extra, _) = raw_payload.deconstruct();
let address = Indices::unlookup(account);
Some((call, (address, signature, extra)))
}
}
impl system::offchain::SigningTypes for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
}
impl offences::Trait for Runtime {
type Event = Event;
type IdentificationTuple = session::historical::IdentificationTuple<Self>;