Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
b92c4e1
New approach to offchain signing.
tomusdrw Feb 25, 2020
c27c7bd
Use in im-online
tomusdrw Feb 26, 2020
1b817e3
Rewrite to use Account<T>
tomusdrw Feb 26, 2020
c1e4fb1
DRY signing.
tomusdrw Feb 26, 2020
033a325
Implement send_raw_unsigned_transaction
rakanalh Feb 26, 2020
459c5dd
WiP
tomusdrw Mar 2, 2020
8f7f9ae
Merge.
tomusdrw Mar 2, 2020
f1b2d62
Expunge LocalCall
tomusdrw Mar 2, 2020
e4db565
Expunge LocalCall
tomusdrw Mar 2, 2020
3e11852
Fix compilation.
tomusdrw Mar 2, 2020
726061b
Solve call.
tomusdrw Mar 2, 2020
6bce38e
Make it compile again.
tomusdrw Mar 2, 2020
775df42
Finalize implementation.
tomusdrw Mar 2, 2020
930a7cb
Change CreateTransaction
tomusdrw Mar 2, 2020
31846d9
Clear CreateTransaction.
tomusdrw Mar 2, 2020
25896f0
Add price payload
rakanalh Mar 3, 2020
a62d00d
Send raw transaction
rakanalh Mar 3, 2020
71325fb
Submit signed payload / unsigned transaction (WIP)
rakanalh Mar 3, 2020
3324ef7
Supertrait requirements on T::Signature
rakanalh Mar 3, 2020
b58512c
Validate signature of payload on an unsigned transaction
rakanalh Mar 3, 2020
b850dfd
Fix encoding - part 1
rakanalh Mar 3, 2020
1c8281a
Merge branch 'master' into td-offchain-signing
tomusdrw Mar 3, 2020
5aa5f0f
Make it compile.
tomusdrw Mar 3, 2020
59ce1ba
Fix compilation of unsigned validator.
tomusdrw Mar 3, 2020
748d065
Pass price payload to the transaction
rakanalh Mar 3, 2020
b4e7d43
Merge branch 'td-offchain-signing' of github.com:paritytech/substrate…
rakanalh Mar 3, 2020
064812d
Make block number part of the signed payload
rakanalh Mar 3, 2020
88c95b3
Send signed transaction
rakanalh Mar 4, 2020
83fe399
Implement all_accounts, any_account
rakanalh Mar 4, 2020
e46b0ee
Fix formatting
rakanalh Mar 4, 2020
763f1d9
Implement submit_transaction
rakanalh Mar 4, 2020
eb28754
Submit signed transaction (ForAll, ForAny)
rakanalh Mar 4, 2020
2e27baa
Fix formatting
rakanalh Mar 6, 2020
5c86a37
Implement CreateSignedTransaction
rakanalh Mar 6, 2020
6c3f8b3
Move sign and verify to AppCrypto
tomusdrw Mar 6, 2020
428b3da
Sign transaction
rakanalh Mar 6, 2020
f8b8dd1
Call `use_encoded`
rakanalh Mar 6, 2020
e6b2f33
Remove SubmitAndSignTransaction
rakanalh Mar 7, 2020
1dc8bf0
Implement runtime using new SigningTypes
rakanalh Mar 7, 2020
9f66858
Adapt offchain example to changes
rakanalh Mar 7, 2020
6e14b44
Fix im-online pallet
rakanalh Mar 7, 2020
a3442cb
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 13, 2020
2c75fb5
Quick fix: rename AuthorityId2
rakanalh Mar 13, 2020
1f54eae
Fix offchain example tests
rakanalh Mar 16, 2020
887e0e9
Add a comment on why keystore is required in unsigned transaction test
rakanalh Mar 16, 2020
34afbf2
Use UintAuthorityId instead of u64
rakanalh Mar 18, 2020
d04bd9a
WIP
rakanalh Mar 18, 2020
824940a
Remove IdentifyAccount from UintAuthorityId
rakanalh Mar 18, 2020
259cb98
Implement PublicWrapper type
rakanalh Mar 18, 2020
ea0ffae
Fix im-online tests
rakanalh Mar 18, 2020
853c1f3
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 18, 2020
5fd154d
Fix runtime test
rakanalh Mar 18, 2020
f629bab
Bump spec version
rakanalh Mar 18, 2020
6951887
Fix executor tests
rakanalh Mar 18, 2020
b8702ac
Rename ImOnlineAuthId -> ImOnlineAuthorityId and formatting
rakanalh Mar 23, 2020
09820d1
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 23, 2020
670e26b
Fix merge
rakanalh Mar 23, 2020
f5939cd
Documentation
rakanalh Mar 24, 2020
0ffd1b9
Revert u64 -> UintAuthorityId conversion
rakanalh Mar 24, 2020
324d436
Fix string errors
rakanalh Mar 25, 2020
9fe4409
Document public members in offchain module
rakanalh Mar 25, 2020
dcee66a
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 25, 2020
54cfe80
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 25, 2020
686b218
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 30, 2020
884dc5c
Introduce SubmitTransaction
rakanalh Mar 31, 2020
ac8845f
Update pallets to use SubmitTransaction
rakanalh Mar 31, 2020
55f3fce
WIP
rakanalh Mar 31, 2020
c3df880
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 31, 2020
fda2dd3
Use SubmitTransaction in offchain
rakanalh Mar 31, 2020
44b0701
Use `submit_unsigned_transaction`
rakanalh Mar 31, 2020
ab6c763
Fix tests
rakanalh Mar 31, 2020
0278c8e
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Mar 31, 2020
5fbfe1a
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 1, 2020
de959d6
Update docs
rakanalh Apr 1, 2020
6d3c36b
Remove SigningTypes requirement from `SendTransactionTypes`
rakanalh Apr 2, 2020
f37c783
Fix tests
rakanalh Apr 2, 2020
7279077
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 2, 2020
df8e52c
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 6, 2020
fd70a12
Update frame/system/src/offchain.rs
rakanalh Apr 7, 2020
848adbf
Update frame/system/src/offchain.rs
rakanalh Apr 7, 2020
b4c5765
Update frame/example-offchain-worker/src/tests.rs
rakanalh Apr 7, 2020
f88c1c1
Update frame/system/src/offchain.rs
rakanalh Apr 7, 2020
13a8c84
Update frame/system/src/offchain.rs
rakanalh Apr 7, 2020
61e6987
Remove leftover from previous iterations
rakanalh Apr 7, 2020
c163b22
Change enum to struct
rakanalh Apr 7, 2020
81ba429
Remove public
rakanalh Apr 7, 2020
89b9471
Move mock to node/executor/tests
rakanalh Apr 7, 2020
9f79dc9
Merge branch 'td-offchain-signing' of github.com:paritytech/substrate…
rakanalh Apr 7, 2020
e69848e
Cleanup test-helpers
rakanalh Apr 7, 2020
ad83ed5
Make `application-crypto` `std` feature internal
bkchr Apr 7, 2020
a26379a
Merge remote-tracking branch 'upstream/bkchr-application-crypto-std' …
rakanalh Apr 7, 2020
143bfbe
Revert cargo lock update
rakanalh Apr 7, 2020
9644f85
Use TestAuthorityId from common
rakanalh Apr 7, 2020
aaae8ee
Restore members of account to public
rakanalh Apr 7, 2020
1f39e03
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 7, 2020
917b8a2
Tidy up imports
rakanalh Apr 7, 2020
cdd1af9
Fix benchmarking pallet
rakanalh Apr 7, 2020
ae477bb
Add tests demonstrating ForAll, ForAny on signer
rakanalh Apr 7, 2020
583cdc3
Move definition of AppCrypto
rakanalh Apr 7, 2020
87be138
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 8, 2020
42f4802
Cleanup stray comment
rakanalh Apr 8, 2020
536c8c3
Fix ValidTransaction
rakanalh Apr 8, 2020
2c8676f
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
rakanalh Apr 9, 2020
3253d1f
Re-fix CreateSignedTransaction
rakanalh Apr 9, 2020
27f900e
Address PR feedback
rakanalh Apr 13, 2020
40c2335
Add can_sign method to signer
Apr 14, 2020
9d72eb8
Propagate error
Apr 14, 2020
de39bed
Improve documentation
Apr 14, 2020
4001eda
Fix vec! macro not available
Apr 14, 2020
7ac2b29
Document SendTransactiontypes
Apr 14, 2020
19ce6d8
Add some docs.
tomusdrw Apr 15, 2020
cbb909a
Split signing examples
Apr 15, 2020
79f54ab
Add tests for signing examples
Apr 15, 2020
8491fbc
WIP can_sign - PR feedback
Apr 15, 2020
fe5e74f
Merge branch 'td-offchain-signing' of github.com:paritytech/substrate…
Apr 15, 2020
9d125a1
WIP
Apr 15, 2020
507ef71
Split for_any / for_all into different calls
Apr 16, 2020
5732ff1
Verify payload and signature in test
Apr 16, 2020
2dde0c0
Fix can_sign implementation
Apr 16, 2020
c216977
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
Apr 16, 2020
c378199
Fix impl_version
Apr 16, 2020
4a6d268
Import Box from sp_std
Apr 16, 2020
a94fecc
Create issues for TODOs
Apr 16, 2020
4a61334
Ignore doctest.
tomusdrw Apr 16, 2020
5722163
Add test directly to system. Adjust UintTypes.
tomusdrw Apr 16, 2020
19494c3
Add some tests to account filtering.
tomusdrw Apr 17, 2020
9bdbbc9
Remove code samples and point to example offchain worker
Apr 17, 2020
d35669b
Merge branch 'td-offchain-signing' of github.com:paritytech/substrate…
Apr 17, 2020
6a94d35
Merge remote-tracking branch 'upstream/master' into td-offchain-signing
Apr 17, 2020
2436ca7
Fix doc links
Apr 17, 2020
dec2490
Fix im-online tests using signatures.
tomusdrw Apr 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve documentation
  • Loading branch information
Rakan Alhneiti committed Apr 14, 2020
commit de39bed513dc6f32c0a68d78d6da68ee03d82c8f
1 change: 0 additions & 1 deletion frame/example-offchain-worker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
type Extrinsic = Extrinsic;
}


impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Test where
Call<Test>: From<LocalCall>,
{
Expand Down
118 changes: 117 additions & 1 deletion frame/system/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,76 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Module helpers for off-chain calls.
//!
//! ## Overview
//!
//! This module provides transaction related helpers to:
//! - Submit a raw unsigned transaction
//! - Submit an unsigned transaction with a signed payload
//! - Submit a signed transction.
//!
//! ## Usage
//!
//! ### Submit a raw unsigned transaction
//!
//! To submit a raw unsigned transaction, [`SubmitTransaction`](./struct.SubmitTransaction.html)
//! can be used.
//!
//! ```rust
//! SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call)
//! ```
//!
//! ### Signing transactions
//!
//! To be able to use signing, the following trait should be implemented:
//!
//! - [`AppCrypto`](./trait.AppCrypto.html): where an application-specific key
//! is defined and can be used by this module's helpers for signing.
//! - [`CreateSignedTransaction`](./trait.CreateSignedTransaction.html): where
//! the manner in which the transaction is constructed is defined.
//!
//! #### Submit an unsigned transaction with a signed payload
//!
//! Initially, a payload instance that implements the `SignedPayload` trait should be defined.
//! If we take the [`PricePayload`](../../example-offchain-worker/struct.PricePayload.html)
//! defined in the example-offchain-worker pallet, we see the following:
//!
//! ```rust
//! #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
//! pub struct PricePayload<Public, BlockNumber> {
//! block_number: BlockNumber,
//! price: u32,
//! public: Public,
//! }
//!
//! impl<T: SigningTypes> SignedPayload<T> for PricePayload<T::Public, T::BlockNumber> {
//! fn public(&self) -> T::Public {
//! self.public.clone()
//! }
//! }
//! ```
//!
//! An object from the defined payload can then be signed and submitted onchain.
//!
//! ```
//! Signer::<T, T::AuthorityId>::all_accounts().send_unsigned_transaction(
//! |account| PricePayload {
//! price,
//! block_number,
//! public: account.public.clone()
//! },
//! |payload, signature| {
//! Call::submit_price_unsigned_with_signed_payload(payload, signature)
//! }
//! )
//! ```
//!
//! #### Submit a signed transaction
//!
//! ```
//! Signer::<T, T::AuthorityId>::all_accounts().send_signed_transaction(
//! |account| Call::submit_price(price)
//! );

use codec::Encode;
use sp_std::convert::{TryInto, TryFrom};
Expand Down Expand Up @@ -42,6 +112,7 @@ impl<T, LocalCall> SubmitTransaction<T, LocalCall>
where
T: SendTransactionTypes<LocalCall>,
{
/// Submit transaction onchain by providing the call and an optional signature
pub fn submit_transaction(
call: <T as SendTransactionTypes<LocalCall>>::OverarchingCall,
signature: Option<<T::Extrinsic as ExtrinsicT>::SignaturePayload>,
Expand All @@ -50,6 +121,7 @@ where
sp_io::offchain::submit_transaction(xt.encode())
}

/// A convenience method to submit an unsigned transaction onchain.
pub fn submit_unsigned_transaction(
call: <T as SendTransactionTypes<LocalCall>>::OverarchingCall,
) -> Result<(), ()> {
Expand All @@ -64,7 +136,7 @@ where
///
/// - All supported keys in the keystore
/// - Any of the supported keys in the keystore
/// - A list of provided keys
/// - An intersection of in-keystore keys and the list of provided keys
///
/// The signer is then able to:
/// - Submit a unsigned transaction with a signed payload
Expand All @@ -84,14 +156,21 @@ impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>, X> Default for Sign
}

impl<T: SigningTypes, C: AppCrypto<T::Public, T::Signature>, X> Signer<T, C, X> {
/// Use all available keys for signing.
pub fn all_accounts() -> Signer<T, C, ForAll> {
Default::default()
}

/// Use any of the available keys for signing.
pub fn any_account() -> Signer<T, C, ForAny> {
Default::default()
}

/// Use provided `accounts` for signing.
///
/// Note that not all keys will be necessarily used. The provided
/// vector of accounts will be intersected with the supported keys
/// in the keystore and the resulting list will be used for signing.
pub fn with_filter(mut self, accounts: Vec<T::Public>) -> Self {
self.accounts = Some(accounts);
self
Expand Down Expand Up @@ -290,6 +369,7 @@ pub struct Account<T: SigningTypes> {
}

impl<T: SigningTypes> Account<T> {
/// Create a new Account instance
pub fn new(index: usize, id: T::AccountId, public: T::Public) -> Self {
Self { index, id, public }
}
Expand Down Expand Up @@ -340,6 +420,7 @@ pub trait AppCrypto<Public, Signature> {
+ TryFrom<Signature>
+ Into<Signature>;

/// Sign payload with the private key to maps to the provided public key.
fn sign(payload: &[u8], public: Public) -> Option<Signature> {
let p: Self::GenericPublic = public.try_into().ok()?;
let x = Into::<Self::RuntimeAppPublic>::into(p);
Expand All @@ -351,6 +432,7 @@ pub trait AppCrypto<Public, Signature> {
.map(Into::into)
}

/// Verify signature against the provided public key.
fn verify(payload: &[u8], public: Public, signature: Signature) -> bool {
let p: Self::GenericPublic = match public.try_into() {
Ok(a) => a,
Expand Down Expand Up @@ -414,8 +496,18 @@ pub trait CreateSignedTransaction<LocalCall>: SendTransactionTypes<LocalCall> +
pub trait SignMessage<T: SigningTypes> {
type Result;

/// Sign message
///
/// Implementation of this method should return
/// a result containing the signature
fn sign_message(&self, message: &[u8]) -> Self::Result;

/// Sign payload
///
/// This method expects `f` to return a `SignedPayload`
/// object which is then used for signing.
///
/// Returns a result that contains the signature of the payload.
fn sign<TPayload, F>(&self, f: F) -> Self::Result where
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>,
Expand All @@ -430,11 +522,23 @@ pub trait SendSignedTransaction<
> {
type Result;

/// Send a signed onchain transaction
///
/// Calls `f` and expects a Call object to be returned.
/// The call object is then signed and submitted onchain.
///
/// Returns a result of the onchain submission.
fn send_signed_transaction(
&self,
f: impl Fn(&Account<T>) -> LocalCall,
) -> Self::Result;

/// Performs signing and submitting the transaction onchain.
///
/// This method can be used by implementations of `send_signed_transaction`
/// to actually sign and submit the signed transaction.
///
/// Returns a result of the onchain submittion.
fn submit_signed_transaction(
&self,
account: &Account<T>,
Expand Down Expand Up @@ -474,6 +578,11 @@ pub trait SendUnsignedTransaction<
> {
type Result;

/// Send an unsigned transaction with a signed payload.
///
/// This method takes `f` and `f2` where:
/// - `f` is called and expected to return a `SignedPayload` object.
/// - `f2` is called with the SignedPayload returned by `f` and expected to return a Call object.
fn send_unsigned_transaction<TPayload, F>(
&self,
f: F,
Expand All @@ -483,6 +592,7 @@ pub trait SendUnsignedTransaction<
F: Fn(&Account<T>) -> TPayload,
TPayload: SignedPayload<T>;

/// Submits an unsigned transaction onchain.
fn submit_unsigned_transaction(
&self,
call: LocalCall
Expand All @@ -497,10 +607,16 @@ pub trait SendUnsignedTransaction<
pub trait SignedPayload<T: SigningTypes>: Encode {
fn public(&self) -> T::Public;

/// Sign the payload using the implementor's provided public key.
///
/// Returns `Some(signature)` if public key is supported.
fn sign<C: AppCrypto<T::Public, T::Signature>>(&self) -> Option<T::Signature> {
self.using_encoded(|payload| C::sign(payload, self.public()))
}

/// Verify signature against payload.
///
/// Returns a bool indicating whether the signature is valid or not.
fn verify<C: AppCrypto<T::Public, T::Signature>>(&self, signature: T::Signature) -> bool {
self.using_encoded(|payload| C::verify(payload, self.public(), signature))
}
Expand Down