Author

Topic: [GUIDE] Importing Electrum Segwit Private Key Into Bitcoin Core (And Vice Versa) (Read 570 times)

legendary
Activity: 3584
Merit: 1560
If you use Electrum 3.1 or newer to view your private keys it'll display them in "script_type:priv_key_in_standard_wif" so there is nothing to convert. Just remove the colon and whatever comes before it.
sr. member
Activity: 322
Merit: 363
39twH4PSYgDSzU7sLnRoDfthR6gWYrrPoD
Usually, when you import a segwit private key from Bitcoin Core to Electrum, Electrum generates a legacy address (starting with '1') instead of a Segwit address (starting with '3' or 'bc1') because Bitcoin Core uses the WIF for all address types while Electrum has a custom WIF for different types of addresses so each WIF corresponds to 1 bitcoin address.
Also, because of this, you're also unable to import a segwit WIF from Electrum into Bitcoin Core because Core doesn't understand the custom WIF.

However, Electrum internally maintains backwards compatibility so you can convert an Electrum custom WIF to one Core understands.

How To Convert An Electrum segwit WIF to standard WIF

For P2SH-P2WPKH addresses:
Open the console in Electrum and type the following code . (Press enter after each line)
Code:
key = 'L4wAtJ8RYaxtRYZxUwZRH8qGYVv1LUP3RAh9ER9KK2HeTXW6v4Ru'
txin_type, secret, compressed = bitcoin.deserialize_privkey(key)
wif2 = bitcoin.serialize_privkey(secret, compressed, 'p2wpkh-p2sh')
print(wif2)
Replace "L4..." with the private key you got from Electrum and leave the quotation marks as is.
It should print a WIF that you can import into Bitcoin Core.

For P2WPKH addresses (Bech32)
Type the following code, replacing the private key there with yours.
Code:
key = 'LAst2SKVNx36PUY2q49cyLCcpze1YMvdJ66z3uKuiyaLKTVVsH3J'
txin_type, secret, compressed = bitcoin.deserialize_privkey(key)
wif2 = bitcoin.serialize_privkey(secret, compressed, 'p2pkh')
print(wif2)
It should print a private key you can import into Bitcoin Core to get your Bech32 address.


How To Import Segwit WIF from Bitcoin Core to Electrum
This one is simple to do.
NB: For this to work you need to be on Electrum version 3.1.1 and above


For P2SH-P2WPKH addresses (Addresses starting with "3")
Just add p2wpkh-p2sh: directly in front of your  private key.
For example
Code:
p2wpkh-p2sh:5BitcoinPrivateKey
This should generate the corresponding p2wpkh-p2sh bitcoin address.

For P2WPKH addresses (Bech32, starting with 'bc1')
This time we'll add p2wpkh: in front of the private key.
For example
Code:
p2wpkh:5PrivateKey
This should generate the corresponding Bech32 address
Jump to: