Skip to main content

KeyPair

Struct KeyPair 

Source
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

Source

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.

Source

pub fn from_seed_owned(seed: [u8; 32]) -> Self

Source

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.

Source

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.

Source

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.

Source

pub fn into_ring(self) -> Ed25519KeyPair

Convert the current ed25519::KeyPair into a ring::signature::Ed25519KeyPair without an intermediate serialization step.

Source

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.

Source

pub fn serialize_pkcs8_der(&self) -> [u8; 83]

Serialize the ed25519::KeyPair into PKCS#8 DER bytes.

Source

pub fn deserialize_pkcs8_der(bytes: &[u8]) -> Result<Self, Error>

Deserialize an ed25519::KeyPair from PKCS#8 DER bytes.

Source

pub fn secret_key(&self) -> &[u8; 32]

The secret key or “seed” that generated this ed25519::KeyPair.

Source

pub fn public_key(&self) -> &PublicKey

The PublicKey for this KeyPair.

Source

pub fn sign_raw(&self, msg: &[u8]) -> Signature

Sign a raw message with this KeyPair.

Source

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

Trait Implementations§

Source§

impl Debug for KeyPair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromHex for KeyPair

Source§

impl FromStr for KeyPair

Source§

type Err = DecodeError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<F, T, U> Apply<F, U> for T
where F: FnOnce(T) -> U,

Source§

fn apply(self, f: F) -> U

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.