MetacoSA / NBitcoin

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

Question: How to load privKey from wif #293

Closed jhdscript closed 6 years ago

jhdscript commented 6 years ago

Hello, and sorry for the disturb and my bad english (frenchie powa).

I read all the ebook and I don't find how to generate a privateKey from WIF in string format.

Maybe could you help me. Thanx in advance and enjoy for your very great job !

NicolasDorier commented 6 years ago
var secret = new BitcoinSecret(wif, Network.Main);

Please read https://programmingblockchain.gitbooks.io/programmingblockchain/

jhdscript commented 6 years ago

Yeah thanx. I currently generated a lot of address and it appears that the constructeur of base58 consumes a lot of cpu time. Is it possible to use a shared base58 object ?

NicolasDorier commented 6 years ago

In the backend you should never use base58 encoding. Store the ScriptPubKey instead (or raw hex for private keys). This is more flexible, more performant and no downside. (you can convert it to bitcoin address again)

Base58 is only a UI layer concern, and should be used only when the user is asking you to see the data in a compatible way.

jhdscript commented 6 years ago

I have another question: i want to purpose my users to generate an address for a password. How to do this ? Like brainwallet. Regards

NicolasDorier commented 6 years ago

Don't do it, they will loose their money.

You can encrypt a key with a password (BIP38), or generate words randomly to represent a key (BIP39) + a password on top (both documented on https://programmingblockchain.gitbooks.io/programmingblockchain/), but you can't do brainwallet, as it is fundamentally insecure.

jhdscript commented 6 years ago

yes i understand but my problem is simple: I try to generate the maximum public addresses in less time possible. Actually i do this:

For i as integer = 0 to 100000 Dim privateKey As New Key() Console.log(privateKey.GetWif(Network.Main).ToString()) Console.log(privateKey.PubKey.GetAddress(Network.Main).ToString()) Next

But it s very slow. Maybe have you a trick to optimize thousand public address generation ?

NicolasDorier commented 6 years ago

Base58 is not meant to be used in your algorithms, no algorithm should depends on it. Only when you show to users. Just use Key.ToBytes() to store a private key and Key.PubKey.ToBytes() to store a public key. Never store Base58 data.

There is normally never any need to generate 10000 addresses fast, as no human can read them as fast and make use of it.

jhdscript commented 6 years ago

I want to make a vanity generator for a TP with my students. But performance are very low. Have you an idea to boost generation ?

jhdscript commented 6 years ago

I think i can accelerate the process. is it possible to generate a pubkey from a public string address ?

NicolasDorier commented 6 years ago

ha it is for vanity generator. No, you can't generate a pubkey from an address. (an address is hash on pubkey)

What you can do though, is to take the current Base58Encoder in NBitcoin and modify it such that is stop computing the address as soon as it is clear that it does not start by the right number.

jhdscript commented 6 years ago

Nicolas you say: Store the ScriptPubKey instead (or raw hex for private keys). This is more flexible, more performant and no downside. (you can convert it to bitcoin address again)

It's a very good idea. But how to generate public Address from scriptpubkey ? I read the ebook but i donna find it and i think a function exists.

Regards

NicolasDorier commented 6 years ago

From scriptPubKey you can create an address with scriptPubKey.GetDestinationAddress(Network.Main). It works even for BECH32 addresses. :)

This returns null if the scriptPubKey can't be represented by an address.

NicolasDorier commented 6 years ago

@jhdscript should I close this issue?