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

generate_mnemonic will block #1

Closed
paizzj opened this issue Aug 8, 2020 · 6 comments
Closed

generate_mnemonic will block #1

paizzj opened this issue Aug 8, 2020 · 6 comments
Assignees
Milestone

Comments

@paizzj
Copy link

paizzj commented Aug 8, 2020

my $mnemonic = Bitcoin::Crypto::Key::ExtPrivate->generate_mnemonic();
say "your mnemonic code is: $mnemonic";

above example. when execute twice , the first returns right, but the second the programe will block and it don't return any more.

@bbrtj
Copy link
Member

bbrtj commented Aug 8, 2020

Hello, thank you for your report. Unfortunately I'm not able to reproduce the problem you're describing on perl 5.32.0 with the module version 0.993.

Can you provide some more information about your environment? Also, did the module pass the test suite? There are a couple of mnemonics generated in there, so it should block there as well.

Lastly, I think this problem is likely not caused by Bitcoin::Crypto itself but a dependency module Bitcoin::BIP39 which the method you're using is wrapping around. Can you try the gen_bip39_mnemonic function from it directly? Run the following snippet and see if it works

use Bitcoin::BIP39 qw(gen_bip39_mnemonic);

for (1..3) {
	my $mnemonic = gen_bip39_mnemonic;
	say "your mnemonic code is: $mnemonic->{mnemonic}";
}

@paizzj
Copy link
Author

paizzj commented Aug 9, 2020

Hello, thank you for your report. Unfortunately I'm not able to reproduce the problem you're describing on perl 5.32.0 with the module version 0.993.

Can you provide some more information about your environment? Also, did the module pass the test suite? There are a couple of mnemonics generated in there, so it should block there as well.

Lastly, I think this problem is likely not caused by Bitcoin::Crypto itself but a dependency module Bitcoin::BIP39 which the method you're using is wrapping around. Can you try the gen_bip39_mnemonic function from it directly? Run the following snippet and see if it works

use Bitcoin::BIP39 qw(gen_bip39_mnemonic);

for (1..3) {
	my $mnemonic = gen_bip39_mnemonic;
	say "your mnemonic code is: $mnemonic->{mnemonic}";
}

right, it also blocked. but how to reslove it ?

@paizzj
Copy link
Author

paizzj commented Aug 9, 2020

my perl version is 5.26.1, and Bitcoin::Crypto module installed by metacpan.
os : ubuntu 18.04 server

@bbrtj
Copy link
Member

bbrtj commented Aug 9, 2020

Since I can't personally reproduce the problem and it isn't with my code directly I can't really fix it for you unless I drop the dependency. I'm happy to help you troubleshoot tho, but you should later file a bug report to the author of Bitcoin::BIP39 (or its dependency, Bytes::Random::Secure).

You forgot to mention if the test suite has successfully passed for you when you were installing the module, but since you don't mention any --force flags on metacpan I'm taking it as a yes.

Is the place in code you're having a hang a part of a bigger script or does it also block when all you have in a script is mnemonic generation?

Is your perl installation coming from ubuntu's apt or do you use something like perlbrew? You could try installing perl with perlbrew and running the code with it, it will compile on your machine and likely be a bit more stable than a precompiled binary.

Looking at the gen_bip39_mnemonic function in Bitcoin::BIP39 I can't say which part of it may be problematic, but I'd check Bytes::Random::Secure::random_bytes to see if it blocks. Please take a look at this documentation note: https://metacpan.org/pod/release/DAVIDO/Bytes-Random-Secure-0.29/lib/Bytes/Random/Secure.pm#BLOCKING-ENTROPY-SOURCE

@bbrtj
Copy link
Member

bbrtj commented Aug 9, 2020

After having a thought about this and reading a bit more of Bytes::Random::Secure docs, I'm pretty sure its all about a blocking source of entropy, and seems like it's non-blocking on FreeBSD which I am currently using, so no wonder I can't reproduce it locally. Unfortunately, Bitcoin::BIP39 does not give us any way to parametrize the random number generator, so the workaround below is not so straightforward.

For now the solution would be to generate entropy yourself, for example via the OO interface of Bytes::Random::Secure, so then you can specify the entropy source to be non-blocking with a constructor parameter. With a generated entropy you'll be able to get a mnemonic with entropy_to_bip39_mnemonic from Bitcoin::BIP39 and turn it into a master key with from_mnemonic method of Bitcoin::Crypto::Key::ExtPrivate.

Of course it is a lot of modules to use, so I'll add an option to specify your own entropy for generate_mnemonic method in the next version and document the problem. You'll still have to generate the entropy yourself, but at least there will be no need to use Bitcoin::BIP39 directly.

bbrtj added a commit that referenced this issue Aug 9, 2020
@bbrtj bbrtj added this to the 0.994 milestone Aug 11, 2020
@bbrtj bbrtj self-assigned this Aug 11, 2020
bbrtj added a commit that referenced this issue Aug 15, 2020
@bbrtj
Copy link
Member

bbrtj commented Sep 6, 2020

Commits for this issue have been released to CPAN. Feel free to re-open the issue if you encounter any more problems.

@bbrtj bbrtj closed this as completed Sep 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants