timechain_runtime/
offchain.rsuse scale_codec::Encode;
use polkadot_sdk::*;
use sp_runtime::{
generic::{self, Era},
traits::{SaturatedConversion, StaticLookup, Verify},
};
use time_primitives::Signature;
use super::{
AccountId, BlockHashCount, Nonce, Runtime, RuntimeCall, SignedPayload, System,
UncheckedExtrinsic,
};
#[cfg(feature = "testnet")]
use super::{PrevalidateFeeless, RuntimeSignedExtra};
#[cfg(feature = "testnet")]
impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
type Extension = RuntimeSignedExtra;
fn create_transaction(call: RuntimeCall, extension: RuntimeSignedExtra) -> UncheckedExtrinsic {
generic::UncheckedExtrinsic::new_transaction(call, extension).into()
}
}
impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_signed_transaction<
C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>,
>(
call: RuntimeCall,
public: <Signature as Verify>::Signer,
account: AccountId,
nonce: Nonce,
) -> Option<UncheckedExtrinsic> {
let tip = 0;
let period =
BlockHashCount::get().checked_next_power_of_two().map(|c| c / 2).unwrap_or(2) as u64;
let current_block = System::block_number()
.saturated_into::<u64>()
.saturating_sub(1);
let era = Era::mortal(period, current_block);
let extra = (
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(era),
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
frame_metadata_hash_extension::CheckMetadataHash::new(false),
#[cfg(feature = "testnet")]
PrevalidateFeeless::<Runtime>::new(),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
log::warn!("Unable to create signed payload: {:?}", e);
})
.ok()?;
let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
let address = <Runtime as frame_system::Config>::Lookup::unlookup(account);
let (call, extra, _) = raw_payload.deconstruct();
let transaction =
generic::UncheckedExtrinsic::new_signed(call, address, signature, extra).into();
Some(transaction)
}
}
impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_inherent(call: RuntimeCall) -> UncheckedExtrinsic {
generic::UncheckedExtrinsic::new_bare(call).into()
}
}
impl frame_system::offchain::SigningTypes for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
}
impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
where
RuntimeCall: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type RuntimeCall = RuntimeCall;
}