Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): add MultiSigner helper #1683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod bits;
mod era;
mod multi_address;
mod multi_signature;
mod multi_signer;
mod static_type;
mod unchecked_extrinsic;
mod wrapper_opaque;
Expand All @@ -26,6 +27,7 @@ pub use account_id20::AccountId20;
pub use era::Era;
pub use multi_address::MultiAddress;
pub use multi_signature::MultiSignature;
pub use multi_signer::MultiSigner;
pub use primitive_types::{H160, H256, H512};
pub use static_type::Static;
pub use unchecked_extrinsic::UncheckedExtrinsic;
Expand Down
58 changes: 58 additions & 0 deletions core/src/utils/multi_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019-2024 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

//! The "default" Substrate/Polkadot Signer type. This is used in signature verification and related bits.
//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_runtime::MultiSigner`
//! for instance, to gain functionality without forcing a dependency on Substrate crates here.

use codec::{Decode, Encode};

/// Signer container that can store known signer types. This is a simplified version of
/// `sp_runtime::MultiSigner`. To obtain more functionality, convert this into that type.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, scale_info::TypeInfo)]
pub enum MultiSigner {
/// An Ed25519 public key.
Ed25519([u8; 32]),
/// An Sr25519 public key.
Sr25519([u8; 32]),
/// An ECDSA/SECP256k1 public key.
Ecdsa([u8; 33]),
}

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

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

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

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

impl From<sp_core::ecdsa::Public> for MultiSigner {
fn from(value: sp_core::ecdsa::Public) -> Self {
let signer: sp_runtime::MultiSigner = value.into();
signer.into()
}
}
}
4 changes: 2 additions & 2 deletions subxt/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use url::Url;

pub use subxt_core::utils::{
bits, strip_compact_prefix, to_hex, AccountId32, Encoded, Era, KeyedVec, MultiAddress,
MultiSignature, PhantomDataSendSync, Static, UncheckedExtrinsic, WrapperKeepOpaque, Yes, H160,
H256, H512,
MultiSignature, MultiSigner, PhantomDataSendSync, Static, UncheckedExtrinsic, WrapperKeepOpaque,
Yes, H160, H256, H512,
};

cfg_jsonrpsee! {
Expand Down
Loading