0

I have a randomly generated 128 bit guid (cryptographically secure). How can I use this as a seed to generate a public and private key for Bitcoin, using C#? By seed, I mean that every time I use the same guid as input, it should result in the same public/private keys.

I have looked at NBitcoin, but don't understand how to pull it off.

Björn Morén
  • 693
  • 5
  • 14
  • Do you need 1 key pair from guid, or more? 128 bits are not enough for security. Bitcoin addresses are 160 bits long (result of RIPEMD160 hash function). – Zergatul Nov 13 '21 at 13:28
  • Thanks @Zergatul. Yes 1 key pair. I need a function that accepts a randomly generated number and returns a private + public key. I can generate numbers with any number of bits, 128 was just an example. – Björn Morén Nov 13 '21 at 14:03

1 Answers1

-1

You can directly create 32 random bytes to be your private key. Example below. But it is VERY important: these 32 bytes must be from cryptographically-secure pseudorandom generator. For example, if you use C# built-in Random class, anyone will be able to restore your private keys with regular computer. You need to be very careful if you plan to use this in real bitcoin network. I am not sure if Guid generation is cryptographically-secure.

static void Main(string[] args)
{
    byte[] GetRawKey()
    {
        // private key=1 in this example
        byte[] data = new byte[32];
        data[^1] = 1;
        return data;
    }

    var key = new Key(GetRawKey()); // private key
    var pair = key.CreateKeyPair(); // key pair (pub+priv keys)
    var addrP2pkh = key.GetAddress(ScriptPubKeyType.Legacy, Network.Main); // P2PKH address
    var addrP2sh = key.GetAddress(ScriptPubKeyType.SegwitP2SH, Network.Main); // Segwit P2SH address
    Console.WriteLine(addrP2pkh.ToString());
    Console.WriteLine(addrP2sh.ToString());
}
Zergatul
  • 1,957
  • 1
  • 18
  • 28