MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.87k stars 845 forks source link

Exception on Ubuntu 20.04 #1050

Closed xclud closed 2 years ago

xclud commented 3 years ago

I run the following code from the blockchain book:

var privateKey = new Key(); // generate a random private key
var publicKey = privateKey.PubKey;

return publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString();

And i get the following exception:

at NBitcoin.Bitcoin.InitSignet() in NBitcoin.dll:token 0x60002fa+0x0
at NBitcoin.Network..cctor() in NBitcoin.dll:token 0x60006be+0x108
--- End of inner exception stack trace ---
at NBitcoin.Network.get_Main() in NBitcoin.dll:token 0x60006bf+0x0

I tried 5.x.x and 6.x.x versions of the library and i get the same results.

NicolasDorier commented 3 years ago

@xclud can you update and try again? I fixed this bug normally.

xclud commented 2 years ago

Nop. This is not working.

I installed the version 6.0.9. My Main() has only one line:

void Main()
{
  NBitcoin.Network network = NBitcoin.Network.Main;
}

and I get this exception:

---> System.ArgumentNullException: Value cannot be null. (Param>
  at System.IO.Path.Combine(String path1, String path2, String >
  at NBitcoin.Bitcoin.CreateSignet()
  at NBitcoin.Bitcoin.InitSignet()
  at NBitcoin.Network..cctor()
  --- End of inner exception stack trace ---
  at NBitcoin.Network.get_Main()

I have this project as a daemon (background service) in Ubuntu. My guess is this project is trying to get the user's Home directory and do System.IO.Path.Combine. Home directory does not mean anything in a background service as we are not logging in with a user account. So the value null is passed to the System.IO.Path.Combine.

BTW, what I dislike about NBitcoin is it relates to the filesystem and network and even thought I only want to generate an offline wallet, it still initializes some directories and network connections without developer's consent.

xclud commented 2 years ago

Here is the source of the exception:

https://github.com/MetacoSA/NBitcoin/blob/17ce930b9029ceeed3d0a9d8e60fda2f8631f86e/NBitcoin/Network.cs#L2075

augustoproiete commented 2 years ago

@xclud As you noticed, the exception is not specific to Ubuntu, but due to the fact your environment doesn't have either HOME or APPDATA environment variables which CreateSignet relies on.

I've just submitted PR https://github.com/MetacoSA/NBitcoin/pull/1063 which uses the operating system's TEMP folder as a fallback, which is more likely to exist in scenarios like yours.

In the meantime, a simple workaround would be for you to set one of these environment variables to an existing path for your process (preferably a folder where the user running the daemon has write access to) at the start of your app, before you access any of the NBitcoin classes. e.g.

Environment.SetEnvironmentVariable("HOME", System.IO.Path.GetTempPath(),
    EnvironmentVariableTarget.Process);
NicolasDorier commented 2 years ago

Pushed 6.0.10 fixing this bug, thanks @augustoproiete to spot the source!