What is needed to generate a bech32 address?


So I wrote a vanity address generator in c# with the NBitcoin lib. When I use the private key used to generate the address and input it in Mycelium it outputs a different address. This does not occur when working with legacy addresses, so I'm guessing I need something more than just a private key in order to generate a segwit address. Was it a redeem script or something? How can I make sure I get the same address in a wallet when I input the privkey?


Posted 2020-11-01T21:21:33.623

Reputation: 11

For a typical single key segwit address (P2WPKH), it is just a key. How are you creating the address? Can you give an example of the key you use, the address Mycelium creates, and the address that you create? – Andrew Chow – 2020-11-01T22:50:03.863

My program outputs: Address: bc1q00dnht0ymgkxp8cxlcg5e7mty0w3w9mnvt49vkn75hc809t7nmrq5g9djj Private Key: KyaVpeyGfVRgWUtY2t9HYa7rssFkEcWeE53rNudbEAVRdg3EPpQs while Mycelium outputs bc1qa59kdryyyh43gyekxswjj3dwsrzs273rs97puv how do I make new lines without submitting the comment? – Relaxo143 – 2020-11-01T23:44:43.360

You can edit your question with the additional information. – Andrew Chow – 2020-11-02T01:23:38.163



            var bitcoinPrivateKey = new BitcoinSecret("cPof7e5g6xfgB6AZrc6XVTVwA4efLJurh9kVxa6FRChbr8Jyqaon", 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);


Mycelium testnet bitcoin wallet:



Posted 2020-11-01T21:21:33.623

Reputation: 2 423

how are you generating var bitcoinPrivateKey. For example if I wanted to create a bech32 brainwallet from the value "TheEarthIsFlatl" how would you do that? The private key would be Kx9M5DcnWdcsfhtQdZyQVLa265EtTt2T2HTSwhHhhf5e16JhZWui and the public address would be bc1q856phefcga58hcmd0et2mx78v6q54egj2ulhfl – vvvv4d – 2020-11-18T00:06:20.987


Thanks everyone for the explanation. So yes, only a private key can be enough to generate a bech32 address. The issue in my case seems to be that I was using

var addr = key1.ScriptPubKey.GetWitScriptAddress(net);

Instead of

Key key1 = new Key();

var secret = new BitcoinSecret(key1, net);

var addr = secret.GetAddress(ScriptPubKeyType.Segwit);

Now the output addresses of mycelium and my program match.


Posted 2020-11-01T21:21:33.623

Reputation: 11