1

I'm trying to write a class that can be used to generate public/private key pairs for bitcoin (and any of its 'clones'), I found some code elsewhere that uses BouncyCastle to generate keys, here's the part that is currently generating a private key:

protected void GeneratePrivateKeyHex()
    {
        ECKeyPairGenerator gen = new ECKeyPairGenerator();
        var secureRandom = new SecureRandom();
        var ps = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
        var ecParams = new ECDomainParameters(ps.Curve, ps.G, ps.N, ps.H);
        var keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom);
        gen.Init(keyGenParam);
        AsymmetricCipherKeyPair kp = gen.GenerateKeyPair();
        ECPrivateKeyParameters priv = (ECPrivateKeyParameters)kp.Private;
        PrivateKeyBytes = priv.D.ToByteArrayUnsigned();
        PrivateKeyHex = ByteArrayToString(PrivateKeyBytes);
    }

I'd quite like to be able to create hierarchical deterministic keys using a seed and return a list of those (either by randomly generating a seed or by having a predefined one from a user) but I'm not entirely sure how to implement this!

Assuming I already have a seed, how would I go about generating the addresses for it using bouncycastle? Preferably while avoiding any bitcoin specific libraries as I'm trying to keep this class as generic as possible.

  • did you look at how NBitcoin library does it? – knocte Feb 6 '18 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.