pub struct KeyPair { /* private fields */ }Expand description
An ed25519 secret key and public key.
Applications should always sign with a key pair rather than passing in the secret key and public key separately, to avoid attacks like attacker controlled pubkey signing.
Implementations§
Source§impl KeyPair
impl KeyPair
Sourcepub fn from_seed(seed: &[u8; 32]) -> Self
pub fn from_seed(seed: &[u8; 32]) -> Self
Create a new ed25519::KeyPair from a random 32-byte seed.
Use this when deriving a key pair from a KDF like RootSeed.
pub fn from_seed_owned(seed: [u8; 32]) -> Self
Sourcepub fn from_seed_and_pubkey(
seed: &[u8; 32],
expected_pubkey: &[u8; 32],
) -> Result<Self, Error>
pub fn from_seed_and_pubkey( seed: &[u8; 32], expected_pubkey: &[u8; 32], ) -> Result<Self, Error>
Create a new ed25519::KeyPair from a random 32-byte seed and the
expected public key. Will return an error if the derived public key
doesn’t match.
Sourcepub fn from_rng(rng: &mut dyn Crng) -> Self
pub fn from_rng(rng: &mut dyn Crng) -> Self
Sample a new ed25519::KeyPair from a cryptographic RNG.
Use this when sampling a key pair for the first time or sampling an ephemeral key pair.
Sourcepub fn to_ring(&self) -> Ed25519KeyPair
pub fn to_ring(&self) -> Ed25519KeyPair
Convert the current ed25519::KeyPair into a
ring::signature::Ed25519KeyPair.
Requires a small intermediate serialization step since ring key
pairs can’t be cloned.
Sourcepub fn into_ring(self) -> Ed25519KeyPair
pub fn into_ring(self) -> Ed25519KeyPair
Convert the current ed25519::KeyPair into a
ring::signature::Ed25519KeyPair without an intermediate
serialization step.
Sourcepub fn for_test(id: u64) -> Self
pub fn for_test(id: u64) -> Self
Create a new ed25519::KeyPair from a short id number.
NOTE: this should only be used in tests.
Sourcepub fn serialize_pkcs8_der(&self) -> [u8; 83]
pub fn serialize_pkcs8_der(&self) -> [u8; 83]
Serialize the ed25519::KeyPair into PKCS#8 DER bytes.
Sourcepub fn deserialize_pkcs8_der(bytes: &[u8]) -> Result<Self, Error>
pub fn deserialize_pkcs8_der(bytes: &[u8]) -> Result<Self, Error>
Deserialize an ed25519::KeyPair from PKCS#8 DER bytes.
Sourcepub fn secret_key(&self) -> &[u8; 32]
pub fn secret_key(&self) -> &[u8; 32]
The secret key or “seed” that generated this ed25519::KeyPair.
Sourcepub fn public_key(&self) -> &PublicKey
pub fn public_key(&self) -> &PublicKey
The PublicKey for this KeyPair.
Sourcepub fn sign_struct<'a, T: Signable + Serialize>(
&self,
value: &'a T,
) -> Result<(Vec<u8>, Signed<&'a T>), Error>
pub fn sign_struct<'a, T: Signable + Serialize>( &self, value: &'a T, ) -> Result<(Vec<u8>, Signed<&'a T>), Error>
Canonically serialize and then sign a Signable struct T with this
ed25519::KeyPair.
Returns a buffer that contains the signer PublicKey and generated
Signature pre-pended in front of the serialized T. Also returns a
Signed “proof” that asserts this T was signed by this key pair.
Values are serialized using bcs, a small binary format intended for
cryptographic canonical serialization.
You can verify this signed struct using
ed25519::verify_signed_struct