MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 840 forks source link

How to generate a Nostr keypair #1157

Open kfrancis opened 1 year ago

kfrancis commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago
        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("-", "");
        }