Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception on Ubuntu 20.04 #1050

Closed
xclud opened this issue Aug 13, 2021 · 5 comments
Closed

Exception on Ubuntu 20.04 #1050

xclud opened this issue Aug 13, 2021 · 5 comments

Comments

@xclud
Copy link

xclud commented Aug 13, 2021

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.

@xclud xclud mentioned this issue Aug 14, 2021
@NicolasDorier
Copy link
Collaborator

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

@xclud
Copy link
Author

xclud commented Sep 26, 2021

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
Copy link
Author

xclud commented Sep 26, 2021

Here is the source of the exception:

var home = Environment.GetEnvironmentVariable("HOME");

@augustoproiete
Copy link
Contributor

@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 #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
Copy link
Collaborator

NicolasDorier commented Sep 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants