@@ -9,6 +9,10 @@ pub trait Sign {
99 fn sign ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > > ;
1010}
1111
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.
1216#[ derive( PartialEq , Debug ) ]
1317pub enum Keypair {
1418 Secp256k1 ( secp256k1:: Keypair ) ,
@@ -45,6 +49,17 @@ impl Sign for Keypair {
4549}
4650
4751impl 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.
4863 pub fn generate < R > ( key_tag : KeyTag , csprng : & mut R ) -> Keypair
4964 where
5065 R : rand_core:: CryptoRng + rand_core:: RngCore ,
@@ -64,6 +79,17 @@ impl Keypair {
6479 }
6580 }
6681
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.
6793 pub fn generate_from_entropy ( key_tag : KeyTag , entropy : & [ u8 ] ) -> Result < Keypair > {
6894 match key_tag. key_type {
6995 KeyType :: EccCompact => Ok ( Self :: EccCompact (
@@ -87,6 +113,9 @@ impl Keypair {
87113 }
88114 }
89115
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.
90119 pub fn key_tag ( & self ) -> KeyTag {
91120 match self {
92121 Self :: Secp256k1 ( keypair) => keypair. key_tag ( ) ,
@@ -103,6 +132,9 @@ impl Keypair {
103132 }
104133 }
105134
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.
106138 pub fn public_key ( & self ) -> & PublicKey {
107139 match self {
108140 Self :: Secp256k1 ( keypair) => & keypair. public_key ,
@@ -119,6 +151,16 @@ impl Keypair {
119151 }
120152 }
121153
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.
122164 pub fn ecdh ( & self , public_key : & PublicKey ) -> Result < SharedSecret > {
123165 match self {
124166 Self :: EccCompact ( keypair) => Ok ( SharedSecret ( keypair. ecdh ( public_key) ?) ) ,
@@ -130,6 +172,10 @@ impl Keypair {
130172 }
131173 }
132174
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.
133179 pub fn to_vec ( & self ) -> Vec < u8 > {
134180 match self {
135181 Self :: Secp256k1 ( keypair) => keypair. to_vec ( ) ,
@@ -146,6 +192,13 @@ impl Keypair {
146192 }
147193 }
148194
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.
149202 pub fn secret_to_vec ( & self ) -> Vec < u8 > {
150203 match self {
151204 Self :: Secp256k1 ( keypair) => keypair. secret_to_vec ( ) ,
0 commit comments