gear_subxt/utils/
multi_signature.rs

1// Copyright 2019-2023 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5//! The "default" Substrate/Polkadot Signature type. This is used in codegen, as well as signing related bits.
6//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_runtime::MultiSignature`
7//! for instance, to gain functionality without forcing a dependency on Substrate crates here.
8
9use codec::{Decode, Encode};
10
11/// Signature container that can store known signature types. This is a simplified version of
12/// `sp_runtime::MultiSignature`. To obtain more functionality, convert this into that type.
13#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug)]
14pub enum MultiSignature {
15    /// An Ed25519 signature.
16    Ed25519([u8; 64]),
17    /// An Sr25519 signature.
18    Sr25519([u8; 64]),
19    /// An ECDSA/SECP256k1 signature (a 512-bit value, plus 8 bits for recovery ID).
20    Ecdsa([u8; 65]),
21}
22
23// Improve compat with the substrate version if we're using those crates:
24#[cfg(feature = "substrate-compat")]
25mod substrate_impls {
26    use super::*;
27
28    impl From<sp_runtime::MultiSignature> for MultiSignature {
29        fn from(value: sp_runtime::MultiSignature) -> Self {
30            match value {
31                sp_runtime::MultiSignature::Ed25519(s) => Self::Ed25519(s.0),
32                sp_runtime::MultiSignature::Sr25519(s) => Self::Sr25519(s.0),
33                sp_runtime::MultiSignature::Ecdsa(s) => Self::Ecdsa(s.0),
34            }
35        }
36    }
37
38    impl From<sp_core::ed25519::Signature> for MultiSignature {
39        fn from(value: sp_core::ed25519::Signature) -> Self {
40            let sig: sp_runtime::MultiSignature = value.into();
41            sig.into()
42        }
43    }
44
45    impl From<sp_core::sr25519::Signature> for MultiSignature {
46        fn from(value: sp_core::sr25519::Signature) -> Self {
47            let sig: sp_runtime::MultiSignature = value.into();
48            sig.into()
49        }
50    }
51
52    impl From<sp_core::ecdsa::Signature> for MultiSignature {
53        fn from(value: sp_core::ecdsa::Signature) -> Self {
54            let sig: sp_runtime::MultiSignature = value.into();
55            sig.into()
56        }
57    }
58}