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

How to generate a Nostr keypair #1157

Open
kfrancis opened this issue Dec 29, 2022 · 2 comments
Open

How to generate a Nostr keypair #1157

kfrancis opened this issue Dec 29, 2022 · 2 comments

Comments

@kfrancis
Copy link

Based on nostr-NIP01:

Each user has a keypair. Signatures, public key, and encodings are done according to the Schnorr signatures standard for the curve secp256k1.

What does that look like using this library? I tried using the portion from test_ecdsa_end_to_end but that gives me a key that is far too long, when I'm expecting something 64 bytes long.

@kfrancis
Copy link
Author

kfrancis commented Jan 5, 2023

As simple as this?

if (Context.Instance.TryCreateECPrivKey(GenerateScalar(), out privKey))
{
    var pubKey = privKey.CreateXOnlyPubKey();
    Span<byte> privKeyc = stackalloc byte[65];
    privKey.WriteToSpan(privKeyc);
    var pubKeyStr = pubKey.ToBytes().ToHex();
    var privKeyStr = privKeyc.ToHex()[..64];
}

Seems to work, though I'm having a hard time understanding the private key hex part.

@mjlamb
Copy link

mjlamb commented Mar 10, 2023

        if (Context.Instance.TryCreateECPrivKey(scalar, out privKey))
        {
            var pubKey = privKey.CreateXOnlyPubKey();
            var pubKeyStr = ToHex(pubKey);

            Span<byte> privKeyc = stackalloc byte[32];
            privKey.WriteToSpan(privKeyc);
            string hexString = BitConverter.ToString(privKeyc.ToArray()).Replace("-", "");
        }

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