1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::{
    common::{CurveType, Limits},
    offchain_signatures::SignatureParams,
    util::BoundedBytes,
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{CloneNoBound, DebugNoBound, EqNoBound, PartialEqNoBound};
use sp_runtime::traits::CheckedConversion;

use super::{Config, OffchainSignatureParams, SignatureParamsStorageKey};
use crate::offchain_signatures::OffchainPublicKey;

/// Identifier of the participant used in the threshold issuance.
pub type ParticipantId = u16;

/// Defines public key and signature params for the given signature scheme.
macro_rules! def_signature_scheme_key_and_params {
    (for $scheme: ident: $(#[$key_meta:meta])* $key: ident<$key_byte_size: ident>, $(#[$params_meta:meta])* $params: ident<$params_byte_size: ident>) => {
        $(#[$key_meta])*
        #[derive(scale_info_derive::TypeInfo, Encode, Decode, CloneNoBound, PartialEqNoBound, EqNoBound, DebugNoBound, MaxEncodedLen)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
        #[cfg_attr(
            feature = "serde",
            serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
        )]
        #[scale_info(skip_type_params(T))]
        #[scale_info(omit_prefix)]
        pub struct $key<T: Limits> {
            /// The public key should be for the same curve as the parameters but a public key might not have
            /// parameters on chain
            pub(crate) curve_type: CurveType,
            pub(crate) bytes: BoundedBytes<T::$key_byte_size>,
            /// The params used to generate the public key
            pub(crate) params_ref: Option<SignatureParamsStorageKey>,
            /// Optional participant id used in threshold issuance.
            pub(crate) participant_id: Option<ParticipantId>,
        }

        impl<T: Limits> $key<T> {
            /// Instantiates new public key for the signature scheme.
            /// This function doesn't validate supplied bytes.
            pub fn new(
                bytes: BoundedBytes<T::$key_byte_size>,
                params_ref: impl Into<Option<SignatureParamsStorageKey>>,
                curve_type: CurveType,
            ) -> Self {
                Self {
                    bytes,
                    params_ref: params_ref.into(),
                    curve_type,
                    participant_id: None,
                }
            }

            /// Instantiates new public key with participant id for the signature scheme.
            /// This function doesn't validate supplied bytes.
            /// Participant id implies the usage of this key in threshold issuance.
            pub fn new_with_participant_id(
                bytes: BoundedBytes<T::$key_byte_size>,
                params_ref: impl Into<Option<SignatureParamsStorageKey>>,
                curve_type: CurveType,
                participant_id: ParticipantId,
            ) -> Self {
                let mut this = Self::new(bytes, params_ref, curve_type);
                this.participant_id = Some(participant_id);

                this
            }

            /// Combines key with signature params (if exist and have same scheme).
            pub fn with_params(self) -> ($key<T>, Option<$params<T>>) where T: Config {
                let params = self
                    .params_ref
                    .as_ref()
                    .and_then(|(did, params_id)| SignatureParams::<T>::get(did, params_id))
                    .and_then(OffchainSignatureParams::checked_into);

                (self, params)
            }
        }

        impl<T: Config> From<$key<T>> for ($key<T>, Option<$params<T>>) {
            fn from(key: $key<T>) -> ($key<T>, Option<$params<T>>) {
                key.with_params()
            }
        }

        impl<T: Limits> From<$key<T>> for OffchainPublicKey<T> {
            fn from(key: $key<T>) -> Self {
                Self::$scheme(key)
            }
        }

        impl<T: Limits> TryFrom<OffchainPublicKey<T>> for $key<T> {
            type Error = OffchainPublicKey<T>;

            fn try_from(key: OffchainPublicKey<T>) -> Result<$key<T>, OffchainPublicKey<T>> {
                match key {
                    OffchainPublicKey::$scheme(key) => Ok(key),
                    other => Err(other),
                }
            }
        }

        impl<T: Config> TryFrom<OffchainPublicKey<T>> for ($key<T>, Option<$params<T>>) {
            type Error = OffchainPublicKey<T>;

            fn try_from(key: OffchainPublicKey<T>) -> Result<($key<T>, Option<$params<T>>), OffchainPublicKey<T>> {
                match key {
                    OffchainPublicKey::$scheme(key) => Ok(key.with_params()),
                    other => Err(other),
                }
            }
        }

        $(#[$params_meta])*
        #[derive(scale_info_derive::TypeInfo, Encode, Decode, CloneNoBound, PartialEqNoBound, EqNoBound, DebugNoBound, MaxEncodedLen)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
        #[cfg_attr(
            feature = "serde",
            serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
        )]
        #[scale_info(skip_type_params(T))]
        #[scale_info(omit_prefix)]
        pub struct $params<T: Limits> {
            /// The label (generating string) used to generate the params
            pub(crate) label: Option<BoundedBytes<T::MaxOffchainParamsLabelSize>>,
            pub(crate) curve_type: CurveType,
            pub(crate) bytes: BoundedBytes<T::$params_byte_size>,
        }

        impl<T: Limits> $params<T> {
            /// Instantiates new parameters for the signature scheme.
            /// This function doesn't validate supplied bytes.
            pub fn new(
                label: impl Into<Option<BoundedBytes<T::MaxOffchainParamsLabelSize>>>,
                bytes: BoundedBytes<T::$params_byte_size>,
                curve_type: CurveType,
            ) -> Self {
                Self {
                    label: label.into(),
                    curve_type,
                    bytes,
                }
            }
        }

        impl<T: Limits> From<$params<T>> for OffchainSignatureParams<T> {
            fn from(params: $params<T>) -> Self {
                Self::$scheme(params)
            }
        }

        impl<T: Limits> TryFrom<OffchainSignatureParams<T>> for $params<T> {
            type Error = OffchainSignatureParams<T>;

            fn try_from(key: OffchainSignatureParams<T>) -> Result<$params<T>, OffchainSignatureParams<T>> {
                match key {
                    OffchainSignatureParams::$scheme(params) => Ok(params),
                    other => Err(other),
                }
            }
        }
    }
}

def_signature_scheme_key_and_params! {
    for BBS:
        /// Public key for the BBS signature scheme.
        BBSPublicKey<MaxBBSPublicKeySize>,
        /// Signature parameters for the BBS signature scheme.
        BBSParameters<MaxOffchainParamsBytesSize>
}

def_signature_scheme_key_and_params! {
    for BBSPlus:
        /// Public key for the BBS+ signature scheme.
        BBSPlusPublicKey<MaxBBSPlusPublicKeySize>,
        /// Signature parameters for the BBS+ signature scheme.
        BBSPlusParameters<MaxOffchainParamsBytesSize>
}

def_signature_scheme_key_and_params! {
    for PS:
        /// Public key for the PS signature scheme.
        PSPublicKey<MaxPSPublicKeySize>,
        /// Signature parameters for the PS signature scheme.
        PSParameters<MaxOffchainParamsBytesSize>
}

def_signature_scheme_key_and_params! {
    for BBDT16:
        /// Public key for the BBDT16 signature scheme.
        BBDT16PublicKey<MaxBBDT16PublicKeySize>,
        /// Signature parameters for the BBDT16 signature scheme.
        BBDT16Parameters<MaxOffchainParamsBytesSize>
}