diff --git a/Cargo.lock b/Cargo.lock index e97834a..d3f5b3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3199,6 +3199,7 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", + "log", "pallet-aura", "pallet-balances", "pallet-grandpa", diff --git a/pallets/iso-8583/src/lib.rs b/pallets/iso-8583/src/lib.rs index e955c02..8670109 100644 --- a/pallets/iso-8583/src/lib.rs +++ b/pallets/iso-8583/src/lib.rs @@ -65,9 +65,9 @@ pub mod crypto { }; app_crypto!(sr25519, KEY_TYPE); - pub struct TestAuthId; + pub struct Iso8583AuthId; - impl frame_system::offchain::AppCrypto for TestAuthId { + impl frame_system::offchain::AppCrypto for Iso8583AuthId { type RuntimeAppPublic = Public; type GenericSignature = sp_core::sr25519::Signature; type GenericPublic = sp_core::sr25519::Public; @@ -75,7 +75,7 @@ pub mod crypto { // implemented for mock runtime in test impl frame_system::offchain::AppCrypto<::Signer, Sr25519Signature> - for TestAuthId + for Iso8583AuthId { type RuntimeAppPublic = Public; type GenericSignature = sp_core::sr25519::Signature; diff --git a/pallets/iso-8583/src/mock.rs b/pallets/iso-8583/src/mock.rs index d660615..ad75ace 100644 --- a/pallets/iso-8583/src/mock.rs +++ b/pallets/iso-8583/src/mock.rs @@ -113,7 +113,7 @@ where } impl crate::Config for Test { - type AuthorityId = crypto::TestAuthId; + type AuthorityId = crypto::Iso8583AuthId; type RuntimeEvent = RuntimeEvent; type Currency = Balances; type PalletAccount = PalletAccount; diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index bb25fdb..6608373 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { workspace = true, package = "parity-scale-codec" } scale-info = { workspace = true } +log = { workspace = true } pallet-aura = { workspace = true } pallet-balances = { workspace = true } @@ -85,6 +86,7 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "substrate-wasm-builder", + "log/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index aefba6c..e9130d0 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -6,16 +6,19 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +use codec::Encode; use frame_support::PalletId; use pallet_grandpa::AuthorityId as GrandpaId; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ - create_runtime_str, generic, impl_opaque_keys, + create_runtime_str, + generic::{self, Era}, + impl_opaque_keys, traits::{ AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, - NumberFor, One, Verify, + NumberFor, One, SaturatedConversion, Verify, }, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, MultiSignature, @@ -216,7 +219,7 @@ impl pallet_timestamp::Config for Runtime { } /// Existential deposit. -pub const EXISTENTIAL_DEPOSIT: u128 = 500; +pub const EXISTENTIAL_DEPOSIT: u128 = 1; impl pallet_balances::Config for Runtime { type MaxLocks = ConstU32<50>; @@ -257,6 +260,64 @@ impl pallet_sudo::Config for Runtime { type WeightInfo = pallet_sudo::weights::SubstrateWeight; } +impl frame_system::offchain::SigningTypes for Runtime { + type Public = ::Signer; + type Signature = Signature; +} + +impl frame_system::offchain::SendTransactionTypes for Runtime +where + RuntimeCall: From, +{ + type OverarchingCall = RuntimeCall; + type Extrinsic = UncheckedExtrinsic; +} + +impl frame_system::offchain::CreateSignedTransaction for Runtime +where + RuntimeCall: From, +{ + fn create_transaction>( + call: RuntimeCall, + public: ::Signer, + account: AccountId, + nonce: Nonce, + ) -> Option<( + RuntimeCall, + ::SignaturePayload, + )> { + let tip = 0; + // take the biggest period possible. + 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::() + // The `System::block_number` is initialized with `n+1`, + // so the actual block number is `n`. + .saturating_sub(1); + let era = Era::mortal(period, current_block); + let extra = ( + frame_system::CheckNonZeroSender::::new(), + frame_system::CheckSpecVersion::::new(), + frame_system::CheckTxVersion::::new(), + frame_system::CheckGenesis::::new(), + frame_system::CheckEra::::from(era), + frame_system::CheckNonce::::from(nonce), + frame_system::CheckWeight::::new(), + pallet_transaction_payment::ChargeTransactionPayment::::from(tip), + ); + 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 = sp_runtime::MultiAddress::Id(account); + let (call, extra, _) = raw_payload.deconstruct(); + Some((call, (address, signature, extra))) + } +} + parameter_types! { /// Pallet account ID pub PalletAccount: AccountId = PalletId(*b"py/iso85").into_account_truncating(); @@ -267,6 +328,7 @@ parameter_types! { impl pallet_iso_8583::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; + type AuthorityId = pallet_iso_8583::crypto::Iso8583AuthId; type PalletAccount = PalletAccount; type MaxStringSize = ConstU32<1024>; type WeightToFee = WeightToFee;