@@ -9,6 +9,10 @@ pub trait Sign {
9
9
fn sign ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > > ;
10
10
}
11
11
12
+ /// Represents a cryptographic keypair for any supported key type.
13
+ ///
14
+ /// This enum acts as a type-erased wrapper for all supported keypair types (e.g., Ed25519, Secp256k1, ECC Compact, etc.),
15
+ /// allowing generic handling of key generation, signing, and public key extraction.
12
16
#[ derive( PartialEq , Debug ) ]
13
17
pub enum Keypair {
14
18
Secp256k1 ( secp256k1:: Keypair ) ,
@@ -45,6 +49,17 @@ impl Sign for Keypair {
45
49
}
46
50
47
51
impl Keypair {
52
+ /// Generates a new keypair for the specified key type and network using the provided CSPRNG.
53
+ ///
54
+ /// # Arguments
55
+ /// * `key_tag` - The key tag specifying the network and key type.
56
+ /// * `csprng` - A cryptographically secure random number generator.
57
+ ///
58
+ /// # Returns
59
+ /// A new `Keypair` instance for the requested type and network.
60
+ ///
61
+ /// # Panics
62
+ /// Panics if the key type is not supported or if key generation fails.
48
63
pub fn generate < R > ( key_tag : KeyTag , csprng : & mut R ) -> Keypair
49
64
where
50
65
R : rand_core:: CryptoRng + rand_core:: RngCore ,
@@ -64,6 +79,17 @@ impl Keypair {
64
79
}
65
80
}
66
81
82
+ /// Generates a new keypair from the provided entropy for the specified key type and network.
83
+ ///
84
+ /// # Arguments
85
+ /// * `key_tag` - The key tag specifying the network and key type.
86
+ /// * `entropy` - A byte slice containing sufficient entropy for key generation.
87
+ ///
88
+ /// # Returns
89
+ /// A new `Keypair` instance if the entropy is valid for the requested type and network.
90
+ ///
91
+ /// # Errors
92
+ /// Returns an error if the entropy is invalid or the key type is not supported.
67
93
pub fn generate_from_entropy ( key_tag : KeyTag , entropy : & [ u8 ] ) -> Result < Keypair > {
68
94
match key_tag. key_type {
69
95
KeyType :: EccCompact => Ok ( Self :: EccCompact (
@@ -87,6 +113,9 @@ impl Keypair {
87
113
}
88
114
}
89
115
116
+ /// Returns the key tag for this keypair, encoding the network and key type.
117
+ ///
118
+ /// The key tag is used to identify the network and cryptographic algorithm associated with this keypair.
90
119
pub fn key_tag ( & self ) -> KeyTag {
91
120
match self {
92
121
Self :: Secp256k1 ( keypair) => keypair. key_tag ( ) ,
@@ -103,6 +132,9 @@ impl Keypair {
103
132
}
104
133
}
105
134
135
+ /// Returns a reference to the public key associated with this keypair.
136
+ ///
137
+ /// The returned public key can be used for signature verification or key exchange.
106
138
pub fn public_key ( & self ) -> & PublicKey {
107
139
match self {
108
140
Self :: Secp256k1 ( keypair) => & keypair. public_key ,
@@ -119,6 +151,16 @@ impl Keypair {
119
151
}
120
152
}
121
153
154
+ /// Performs an Elliptic Curve Diffie-Hellman (ECDH) key exchange with the given public key.
155
+ ///
156
+ /// # Arguments
157
+ /// * `public_key` - The peer's public key.
158
+ ///
159
+ /// # Returns
160
+ /// A shared secret if ECDH is supported for this key type.
161
+ ///
162
+ /// # Errors
163
+ /// Returns an error if ECDH is not supported for this key type or if the operation fails.
122
164
pub fn ecdh ( & self , public_key : & PublicKey ) -> Result < SharedSecret > {
123
165
match self {
124
166
Self :: EccCompact ( keypair) => Ok ( SharedSecret ( keypair. ecdh ( public_key) ?) ) ,
@@ -130,6 +172,10 @@ impl Keypair {
130
172
}
131
173
}
132
174
175
+ /// Serializes the keypair to its binary representation.
176
+ ///
177
+ /// # Returns
178
+ /// A vector of bytes containing the serialized keypair, including the key tag and secret key material.
133
179
pub fn to_vec ( & self ) -> Vec < u8 > {
134
180
match self {
135
181
Self :: Secp256k1 ( keypair) => keypair. to_vec ( ) ,
@@ -146,6 +192,13 @@ impl Keypair {
146
192
}
147
193
}
148
194
195
+ /// Serializes the secret key material to its binary representation.
196
+ ///
197
+ /// # Returns
198
+ /// A vector of bytes containing the secret key material only (excluding the key tag).
199
+ ///
200
+ /// # Security
201
+ /// Handle this output with care, as it contains sensitive private key material.
149
202
pub fn secret_to_vec ( & self ) -> Vec < u8 > {
150
203
match self {
151
204
Self :: Secp256k1 ( keypair) => keypair. secret_to_vec ( ) ,
0 commit comments