Rust Key Generation – How to Generate Public Key from Private Key in Rust?

ecdsarustsecp256k1

How to generate Ethereum public key from private key in Rust?

I found rust-secp256k1 and it seems to be what I need, but there is no documentation at all, which makes this crate for me, Rust newbie, nightmare.

Appreciate any help.

Best Answer

I'm also not a Rust programmer, so someone will probably have a better answer, but have a look in Parity's keypair.rs, which itself uses rust-secp256k1.

Of interest is probably the KeyPair implementation.

impl KeyPair {
    /// Create a pair from secret key
    pub fn from_secret(secret: Secret) -> Result<KeyPair, Error> {
        let context = &SECP256K1;
        let s: key::SecretKey = key::SecretKey::from_slice(context, &secret[..])?;
        let pub_key = key::PublicKey::from_secret_key(context, &s)?;
        let serialized = pub_key.serialize_vec(context, false);

        let mut public = Public::default();
        public.copy_from_slice(&serialized[1..65]);

        let keypair = KeyPair {
            secret: secret,
            public: public,
        };

        Ok(keypair)
    }
...
Related Topic