Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

 My code is as follows:

static void Main(string[] args)

{

    try

    {

        Key privateKey = new Key(); // generate a random private key

        PubKey publicKey = privateKey.PubKey;

        Console.WriteLine(publicKey); // 0251036303164f6c458e9f7abecb4e55e5ce9ec2b2f1d06d633c9653a07976560c

        Console.WriteLine(publicKey.GetAddress(Network.Main)); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY

        Console.WriteLine(publicKey.GetAddress(Network.TestNet)); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq

      var publicKeyHash = publicKey.Hash;

        Console.WriteLine(publicKeyHash); // f6889b21b5540353a29ed18c45ea0031280c42cf

        var mainNetAddress = publicKeyHash.GetAddress(Network.Main);

        var testNetAddress = publicKeyHash.GetAddress(Network.TestNet);

        Console.WriteLine(mainNetAddress); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY

        Console.WriteLine(testNetAddress); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq

   Console.ReadLine();

    }

    catch (Exception)

    {    

        throw;

    }   

}

However, on the first line, I am getting this error:

You must set the RNG (RandomUtils.Random) before generating random numbers

closed

1 Answer

0 votes
by (29.5k points)
selected by
 
Best answer

You haven't initialized the random number generator that the crypto library will use. For testing, you can use the UnsecureRandom class that comes with the NBitcoin library you are using. You can refer to the code below:

RandomUtils.Random = new UnsecureRandom(); // set the random number generator.
Key privateKey = new Key(); // generate a random private key

Browse Categories

...