From 549a8e61f5954571ffadd17829972c81ca050b2f Mon Sep 17 00:00:00 2001 From: Florian Franzen Date: Wed, 17 Jul 2024 03:23:00 +0200 Subject: [PATCH] feat(utils): add MultiSigner helper --- core/src/utils/mod.rs | 2 ++ core/src/utils/multi_signer.rs | 58 ++++++++++++++++++++++++++++++++++ subxt/src/utils/mod.rs | 4 +-- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 core/src/utils/multi_signer.rs diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index 42c499f85c..60c74dffc6 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -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; @@ -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; diff --git a/core/src/utils/multi_signer.rs b/core/src/utils/multi_signer.rs new file mode 100644 index 0000000000..920b8907b7 --- /dev/null +++ b/core/src/utils/multi_signer.rs @@ -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 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 for MultiSigner { + fn from(value: sp_core::ed25519::Public) -> Self { + let signer: sp_runtime::MultiSigner = value.into(); + signer.into() + } + } + + impl From for MultiSigner { + fn from(value: sp_core::sr25519::Public) -> Self { + let signer: sp_runtime::MultiSigner = value.into(); + signer.into() + } + } + + impl From for MultiSigner { + fn from(value: sp_core::ecdsa::Public) -> Self { + let signer: sp_runtime::MultiSigner = value.into(); + signer.into() + } + } +} diff --git a/subxt/src/utils/mod.rs b/subxt/src/utils/mod.rs index 08ae07ca33..28e950de32 100644 --- a/subxt/src/utils/mod.rs +++ b/subxt/src/utils/mod.rs @@ -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! {