Skip to content

Commit

Permalink
remove substrate compat (#1850)
Browse files Browse the repository at this point in the history
* remove substrate compat

* add hacky example

* simplify substrate-compat example

* simplify substrate-compat example

* cargo fmt

* fix build

* add more examples

* fix nit

* fix test build

* Update subxt/examples/substrate_compat_signer.rs

Co-authored-by: James Wilson <[email protected]>

* keep only polkadot signer example

* remove more substrate compat related stuff

* fix example

* link to substrate signer example in book

* Update subxt/src/book/usage/transactions.rs

* Update subxt/src/book/usage/transactions.rs

* address grumbles

* fix nits

---------

Co-authored-by: James Wilson <[email protected]>
  • Loading branch information
niklasad1 and jsdw authored Nov 27, 2024
1 parent 09ab839 commit f5e9ce0
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 305 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ std = [
"impl-serde/std",
"primitive-types/std",
]
substrate-compat = ["polkadot-sdk/sp-core", "polkadot-sdk/sp-runtime", "polkadot-sdk/std"]

[dependencies]
codec = { package = "parity-scale-codec", workspace = true, default-features = false, features = ["derive"] }
Expand All @@ -44,6 +43,8 @@ derive-where = { workspace = true }
hex = { workspace = true, default-features = false, features = ["alloc"] }
serde = { workspace = true, default-features = false, features = ["derive"] }
serde_json = { workspace = true, default-features = false, features = ["raw_value", "alloc"] }
tracing = { workspace = true, default-features = false }
polkadot-sdk = { workspace = true, features = ["sp-crypto-hashing"] }
hashbrown = { workspace = true }
thiserror = { workspace = true, default-features = false }

Expand All @@ -55,10 +56,6 @@ blake2 = { workspace = true }
# Provides some deserialization, types like U256/H256 and hashing impls like twox/blake256:
impl-serde = { workspace = true, default-features = false }
primitive-types = { workspace = true, default-features = false, features = ["codec", "serde_no_std", "scale-info"] }
polkadot-sdk = { workspace = true, features = ["sp-crypto-hashing"] }

# Included if the "substrate-compat" feature is enabled.
tracing = { workspace = true, default-features = false }

# AccountId20
keccak-hash = { workspace = true}
Expand Down
29 changes: 0 additions & 29 deletions core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub mod polkadot;
pub mod signed_extensions;
pub mod substrate;

use crate::macros::cfg_substrate_compat;
use codec::{Decode, Encode};
use core::fmt::Debug;
use scale_decode::DecodeAsType;
Expand Down Expand Up @@ -127,31 +126,3 @@ pub trait Header: Sized + Encode + Decode {
Self::Hasher::hash_of(self)
}
}

cfg_substrate_compat! {
/// implement subxt's Hasher and Header traits for some substrate structs
mod substrate_impls {
use super::*;
use polkadot_sdk::sp_runtime;

impl<T: sp_runtime::traits::Header> Header for T
where
<T as sp_runtime::traits::Header>::Number: Into<u64>,
{
type Number = T::Number;
type Hasher = T::Hashing;

fn number(&self) -> Self::Number {
*self.number()
}
}

impl<T: sp_runtime::traits::Hash> Hasher for T {
type Output = T::Output;

fn hash(s: &[u8]) -> Self::Output {
<T as sp_runtime::traits::Hash>::hash(s)
}
}
}
}
8 changes: 0 additions & 8 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub extern crate alloc;

#[macro_use]
mod macros;

pub mod blocks;
pub mod client;
pub mod config;
Expand All @@ -50,9 +47,4 @@ pub mod ext {
pub use scale_decode;
pub use scale_encode;
pub use scale_value;

cfg_substrate_compat! {
pub use polkadot_sdk::sp_runtime;
pub use polkadot_sdk::sp_core;
}
}
21 changes: 0 additions & 21 deletions core/src/macros.rs

This file was deleted.

75 changes: 0 additions & 75 deletions core/src/tx/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! A library to **sub**mit e**xt**rinsics to a
//! [substrate](https://github.com/paritytech/substrate) node via RPC.
use crate::macros::cfg_substrate_compat;
use crate::Config;

/// Signing transactions requires a [`Signer`]. This is responsible for
Expand All @@ -24,77 +23,3 @@ pub trait Signer<T: Config> {
/// refused the operation.
fn sign(&self, signer_payload: &[u8]) -> T::Signature;
}

cfg_substrate_compat! {
pub use pair_signer::PairSigner;
}

// A signer suitable for substrate based chains. This provides compatibility with Substrate
// packages like sp_keyring and such, and so relies on sp_core and sp_runtime to be included.
#[cfg(feature = "substrate-compat")]
mod pair_signer {
use super::Signer;
use crate::Config;
use polkadot_sdk::sp_core::Pair as PairT;
use polkadot_sdk::sp_runtime::{
traits::{IdentifyAccount, Verify},
AccountId32 as SpAccountId32, MultiSignature as SpMultiSignature,
};

/// A [`Signer`] implementation that can be constructed from an [`sp_core::Pair`].
#[derive(Clone, Debug)]
pub struct PairSigner<T: Config, Pair> {
account_id: T::AccountId,
signer: Pair,
}

impl<T, Pair> PairSigner<T, Pair>
where
T: Config,
Pair: PairT,
// We go via an `sp_runtime::MultiSignature`. We can probably generalise this
// by implementing some of these traits on our built-in MultiSignature and then
// requiring them on all T::Signatures, to avoid any go-between.
<SpMultiSignature as Verify>::Signer: From<Pair::Public>,
T::AccountId: From<SpAccountId32>,
{
/// Creates a new [`Signer`] from an [`sp_core::Pair`].
pub fn new(signer: Pair) -> Self {
let account_id =
<SpMultiSignature as Verify>::Signer::from(signer.public()).into_account();
Self {
account_id: account_id.into(),
signer,
}
}

/// Returns the [`sp_core::Pair`] implementation used to construct this.
pub fn signer(&self) -> &Pair {
&self.signer
}

/// Return the account ID.
pub fn account_id(&self) -> &T::AccountId {
&self.account_id
}
}

impl<T, Pair> Signer<T> for PairSigner<T, Pair>
where
T: Config,
Pair: PairT,
Pair::Signature: Into<T::Signature>,
{
fn account_id(&self) -> T::AccountId {
self.account_id.clone()
}

fn address(&self) -> T::Address {
self.account_id.clone().into()
}

fn sign(&self, signer_payload: &[u8]) -> T::Signature {
self.signer.sign(signer_payload).into()
}
}
}
28 changes: 0 additions & 28 deletions core/src/utils/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,35 +160,9 @@ impl core::str::FromStr for AccountId32 {
}
}

// Improve compat with the substrate version if we're using those crates:
#[cfg(feature = "substrate-compat")]
mod substrate_impls {
use super::*;
use polkadot_sdk::{sp_core, sp_runtime};

impl From<sp_runtime::AccountId32> for AccountId32 {
fn from(value: sp_runtime::AccountId32) -> Self {
Self(value.into())
}
}
impl From<sp_core::sr25519::Public> for AccountId32 {
fn from(value: sp_core::sr25519::Public) -> Self {
let acc: sp_runtime::AccountId32 = value.into();
acc.into()
}
}
impl From<sp_core::ed25519::Public> for AccountId32 {
fn from(value: sp_core::ed25519::Public) -> Self {
let acc: sp_runtime::AccountId32 = value.into();
acc.into()
}
}
}

#[cfg(test)]
mod test {
use super::*;

use polkadot_sdk::sp_core::{self, crypto::Ss58Codec};
use polkadot_sdk::sp_keyring::AccountKeyring;

Expand All @@ -202,8 +176,6 @@ mod test {

for keyring in keyrings {
let substrate_account = keyring.to_account_id();
// Avoid "From" impl hidden behind "substrate-compat" feature so that this test
// can work either way:
let local_account = AccountId32(substrate_account.clone().into());

// Both should encode to ss58 the same way:
Expand Down
32 changes: 1 addition & 31 deletions core/src/utils/multi_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use alloc::vec::Vec;
use codec::{Decode, Encode};

/// A multi-format address wrapper for on-chain accounts. This is a simplified version of Substrate's
/// `sp_runtime::MultiAddress`. To obtain more functionality, convert this into that type (this conversion
/// functionality is provided via `From` impls if the `substrate-compat` feature is enabled).
/// `sp_runtime::MultiAddress`.
#[derive(
Clone,
Eq,
Expand Down Expand Up @@ -43,32 +42,3 @@ impl<AccountId, AccountIndex> From<AccountId> for MultiAddress<AccountId, Accoun
Self::Id(a)
}
}

// Improve compat with the substrate version if we're using those crates:
#[cfg(feature = "substrate-compat")]
mod substrate_impls {
use super::{super::AccountId32, *};
use polkadot_sdk::sp_runtime;

impl<N> From<sp_runtime::AccountId32> for MultiAddress<AccountId32, N> {
fn from(value: sp_runtime::AccountId32) -> Self {
let val: AccountId32 = value.into();
val.into()
}
}

impl<Id, N> From<sp_runtime::MultiAddress<Id, N>> for MultiAddress<AccountId32, N>
where
Id: Into<AccountId32>,
{
fn from(value: sp_runtime::MultiAddress<Id, N>) -> Self {
match value {
sp_runtime::MultiAddress::Id(v) => Self::Id(v.into()),
sp_runtime::MultiAddress::Index(v) => Self::Index(v),
sp_runtime::MultiAddress::Raw(v) => Self::Raw(v),
sp_runtime::MultiAddress::Address32(v) => Self::Address32(v),
sp_runtime::MultiAddress::Address20(v) => Self::Address20(v),
}
}
}
}
38 changes: 0 additions & 38 deletions core/src/utils/multi_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,3 @@ pub enum MultiSignature {
/// An ECDSA/SECP256k1 signature (a 512-bit value, plus 8 bits for recovery ID).
Ecdsa([u8; 65]),
}

// Improve compat with the substrate version if we're using those crates:
#[cfg(feature = "substrate-compat")]
mod substrate_impls {
use super::*;
use polkadot_sdk::{sp_core, sp_runtime};

impl From<sp_runtime::MultiSignature> for MultiSignature {
fn from(value: sp_runtime::MultiSignature) -> Self {
match value {
sp_runtime::MultiSignature::Ed25519(s) => Self::Ed25519(s.0),
sp_runtime::MultiSignature::Sr25519(s) => Self::Sr25519(s.0),
sp_runtime::MultiSignature::Ecdsa(s) => Self::Ecdsa(s.0),
}
}
}

impl From<sp_core::ed25519::Signature> for MultiSignature {
fn from(value: sp_core::ed25519::Signature) -> Self {
let sig: sp_runtime::MultiSignature = value.into();
sig.into()
}
}

impl From<sp_core::sr25519::Signature> for MultiSignature {
fn from(value: sp_core::sr25519::Signature) -> Self {
let sig: sp_runtime::MultiSignature = value.into();
sig.into()
}
}

impl From<sp_core::ecdsa::Signature> for MultiSignature {
fn from(value: sp_core::ecdsa::Signature) -> Self {
let sig: sp_runtime::MultiSignature = value.into();
sig.into()
}
}
}
2 changes: 1 addition & 1 deletion metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ criterion = { workspace = true }
scale-info = { workspace = true, features = ["bit-vec"] }

[lib]
# Without this, libtest cli opts interfere with criteron benches:
# Without this, libtest cli opts interfere with criterion benches:
bench = false

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion signer/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

//! An ecdsa keypair implementation.
use codec::Encode;
use polkadot_sdk::sp_crypto_hashing;

use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
use core::str::FromStr;
use hex::FromHex;
use polkadot_sdk::sp_crypto_hashing;
use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey};
use secrecy::ExposeSecret;

Expand Down
Loading

0 comments on commit f5e9ce0

Please sign in to comment.