0

I am looking to generate a Bitcoin Address from an user input text (like the good old days Brainwallet). User input a "Text string" and based on this text string generate the corresponding Bitcoin Address. My google search lead me to NBitcoin and to this post: Generate address of 'private key' from user input

The user SVVC asked the same question got a reply from Prayank with a little code snippet but here I am stuck because the snippet didn't work and the Debugger gave me an Exception Unhandled "System.FormatException: 'Invalid hash of the base 58 string"

Anyone have an idea?

Here is my code in VisualBasic .Net with NBitcoin

Dim pk As String = "LoveIsTheKey"
Dim bitcoinPrivateKey = New BitcoinSecret(pk, Network.Main)
Dim legacy_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy)
MessageBox.Show("Legacy Address : " + legacy_address.ToString)

Thanks guys

3
  • ”Like the good old days Brainwallet” - you mean when users were encouraged to create inherently insecure wallets, leading to an irrecoverable loss of funds? I assume that you won’t be taking the user input as the sole source of entropy for privkey generation?
    – chytrik
    Aug 25 at 23:38
  • There will be no user to my program I just want to make it for myself for research purposes. Aug 25 at 23:51
  • Do you have any idea? Aug 26 at 0:19
0

The code works if you enter any private key in WIF format:

  1. Copy private key for an address from Electrum(testnet)

electrum-privatekey

  1. Run the below code

    using System;
    using NBitcoin;
    
    namespace NPrivKeyAdd
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter private key:");
                string private_key = Console.ReadLine();
                var bitcoinPrivateKey = new BitcoinSecret(private_key, Network.TestNet);
    
                var legacy_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Legacy);
                var segwitp2sh_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.SegwitP2SH);
                var nativesegwit_address = bitcoinPrivateKey.GetAddress(ScriptPubKeyType.Segwit);
    
                Console.WriteLine("Private Key :" + bitcoinPrivateKey);
                Console.WriteLine("Legacy Address :" + legacy_address);
                Console.WriteLine("Segwit-P2SH Address :" + segwitp2sh_address);
                Console.WriteLine("Bech32 Address :" + nativesegwit_address);
            }
        }
    }
    
    

    addresses

If you want to experiment with random strings that can be used to spend bitcoin, this is an interesting example in which knowing birth date is equivalent to private key:

https://programmingblockchain.gitbook.io/programmingblockchain/other_types_of_ownership/arbitrary

1
  • Ok, but how I can enter simply a TextString to be converted to a WIF Key? I didn't understand the sample with the BirthDay. Aug 27 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.