Electrum: Import WIF private key as P2WPKH-in-P2SH address

Created on 29 Dec 2017  Â·  7Comments  Â·  Source: spesmilo/electrum

Some people used "addwitnessaddress" command in bitcoin-qt, so one private key is corresponding to 2 types of address, one is traditional P2PKH (starting with 1), another is P2WPKH-in-P2SH(starting with 3).

enhancement ✨

Most helpful comment

This project would generally like to avoid associating multiple addresses with a single WIF private key. See e.g. https://github.com/bitcoin/bitcoin/pull/11403#issuecomment-353301840. We might have to change this stance if that PR gets merged as is...

For the meantime, here's a python3 script.

import electrum
from electrum import bitcoin

key = 'L4wAtJ8RYaxtRYZxUwZRH8qGYVv1LUP3RAh9ER9KK2HeTXW6v4Ru'
txin_type, secret, compressed = bitcoin.deserialize_privkey(key)
wif2 = bitcoin.serialize_privkey(secret, compressed, 'p2wpkh-p2sh')
print(wif2)

Note: above lines, without the imports, work inside the Electrum console too.

All 7 comments

This project would generally like to avoid associating multiple addresses with a single WIF private key. See e.g. https://github.com/bitcoin/bitcoin/pull/11403#issuecomment-353301840. We might have to change this stance if that PR gets merged as is...

For the meantime, here's a python3 script.

import electrum
from electrum import bitcoin

key = 'L4wAtJ8RYaxtRYZxUwZRH8qGYVv1LUP3RAh9ER9KK2HeTXW6v4Ru'
txin_type, secret, compressed = bitcoin.deserialize_privkey(key)
wif2 = bitcoin.serialize_privkey(secret, compressed, 'p2wpkh-p2sh')
print(wif2)

Note: above lines, without the imports, work inside the Electrum console too.

I think we should try to convince @sipa and Core that an ambiguous import format needs to be avoided

@ecdsa Don't worry, I'm already convinced. But for compatibility reasons we don't have much of a choice now to leave the existing WIF format to mean "any".

I do think we should work on separate specific per-type private key formats.

@sipa

for compatibility reasons

Last time I checked Bitcoin Core did not allow exporting private keys (using dumpprivkey) for an address "created with" addwitnessaddress. It's not too late not to introduce that. :P

Would it not be possible to introduce that but with relevant metadata included?

Anyone who previously exported private keys for such p2sh-segwit addresses had to explicitly figure out the "corresponding" p2pkh address, and invoke dumpprivkey on that. Hence, they did not actually export private keys for the segwit address. (And actually they would still be able to convert those keys using advanced tools, such as the code snippet I wrote above: https://github.com/spesmilo/electrum/issues/3620#issuecomment-354428035)

@SomberNight The reason is that Bitcoin Core does not work in terms of specific addresses marked a ours. Its logic effectively treats everything it can sign for as a payment. This means that if you have a private key and have used addwitnessaddress, it will accept payment to P2PK, P2PKH, P2SH-P2WPKH, P2WPKH, and probably a few others.

Changing things to only work for specific output scripts is a change that I very much want to push for, but it is a major refactoring and rewrite of part of the wallet logic that isn't going to happen fast. Furthermore, it will require a breaking change to the wallet format first.

As a noob user, what I want is probably a checkbox, like "[X] Add corresponding witness address".
As for WIF, I'll be okay if P2WPKH-in-P2SH have a unique type code (different from P2PKH).

I think this can now be closed since https://github.com/spesmilo/electrum/pull/3925 (which was included in 3.1.0)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

karelbilek picture karelbilek  Â·  4Comments

dl3br picture dl3br  Â·  4Comments

Akshansh1903 picture Akshansh1903  Â·  4Comments

juniorjp picture juniorjp  Â·  5Comments

shawnpringle picture shawnpringle  Â·  3Comments