0

I'm trying to generate a public address and for that, I have the code:

var key = new Key();
var wif = key.GetWif(Network.Main);

Console.WriteLine($"WIF: {wif}");

var secret = new BitcoinSecret(key, Network.Main);
Console.WriteLine($"Private Key: {secret.ToWif()}");
Console.WriteLine($"Public Address (Legacy): {secret.GetAddress(ScriptPubKeyType.Legacy)}");
Console.WriteLine($"Public Address (Segwit): {secret.GetAddress(ScriptPubKeyType.Segwit)}");
Console.WriteLine($"Public Address (SegwitP2SH): {secret.GetAddress(ScriptPubKeyType.SegwitP2SH)}");

When I pick the WIF and import it to Blockchain.com (for example), I only get the Legacy public address. Can the same private key sign Segwit and SegwitP2SH transactions? How can I import a private key and get the Segwit and SegwitP2SH public addresses?

My knowledge about how this works is limited and I'm trying to learn...

1 Answer 1

1

Question was posted & answered on the NBitcoin repo, so copying my answers here for the benefit of SE members:

This is a Blockchain.com-specific UX issue... They technically could generate a Segwit address, but they default to P2PKH (legacy) at the moment.

WIF is an old format that doesn't carry metadata about the kind of address that should be unwrapped from the private key, so when you import a private key WIF into a wallet, it's up to the wallet to decide what kind of address to give you for that private key.

ps: Some wallets are "smart" and check all possible addresses (Legacy, Segwit, SegwitP2SH, ...) and then pick whichever one has a balance, so you might want to put some satoshis on the Segwit address that you create before importing the private key WIF to a wallet.


Private keys can be associated with public keys that are either compressed (33 bytes) or uncompressed (65 bytes), and the WIF will be different if compressed/uncompressed. For instance, compressed public keys start with L or K on Mainnet and c on Testnet, whilst uncompressed public keys start with 5 on Mainnet and 9 on Testnet.

You can modify your code sample above to use uncompressed public keys (the default is compressed):

var key = new Key(fCompressedIn: false);
// ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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