Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keypair encoding / serialization to store the private key only #310

Closed
kchalkias opened this issue Dec 20, 2022 · 1 comment
Closed

Keypair encoding / serialization to store the private key only #310

kchalkias opened this issue Dec 20, 2022 · 1 comment
Assignees

Comments

@kchalkias
Copy link
Collaborator

This is better hygiene, to also defend against the private - public key mismatching we found on ed25519 libs. The pub key can be derived during deserialization. Creating a similar issue in Sui repo.

@joyqvq
Copy link
Collaborator

joyqvq commented Dec 21, 2022

we actually already does this when decoding from base64:

in fastcrypto (errors instead of silently expanding from privkey):

    let secret = <T as KeyPair>::PrivKey::from_bytes(&bytes[..sk_length])?;
    let kp: T = secret.into();
    if kp.public().as_ref() != &bytes[sk_length..] {
        return Err(eyre::eyre!("Invalid keypair"));
    }

in sui (only reads the privkey bytes and expand the kp):

                    let sk = Secp256k1PrivateKey::from_bytes(
                        bytes
                            .get(1 + Secp256k1PublicKey::LENGTH..)
                            .ok_or_else(|| eyre::eyre!("Invalid length"))?,
                    )?;
                    Ok(SuiKeyPair::Secp256k1(<Secp256k1KeyPair as From<
                        Secp256k1PrivateKey,
                    >>::from(sk)))

we can do this for encoding as well

            SuiKeyPair::Ed25519(kp) => {
                let kp1 = kp.copy();
                bytes.extend_from_slice(&[self.public().flag()]);
                bytes.extend_from_slice(kp.public().as_ref()); // change this to pubkey::from(kp.private())
                bytes.extend_from_slice(kp1.private().as_ref());
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants