Bitcoin Forum
June 23, 2021, 06:38:21 AM *
News: Latest Bitcoin Core release: 0.21.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [GUIDE] Importing Electrum Segwit Private Key Into Bitcoin Core (And Vice Versa)  (Read 433 times)
Xynerise
Sr. Member
****
Offline Offline

Activity: 322
Merit: 304

39twH4PSYgDSzU7sLnRoDfthR6gWYrrPoD


View Profile
April 24, 2018, 08:00:01 AM
Merited by LoyceV (8), dbshck (8), OmegaStarScream (3), achow101 (2), pooya87 (2), Lucius (2), hugeblack (2), o_e_l_e_o (2), ranochigo (1), HCP (1), Crypto_Collection (1)
 #1

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
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
Abdussamad
Legendary
*
Offline Offline

Activity: 2828
Merit: 1318



View Profile
April 24, 2018, 12:46:50 PM
Merited by Xynerise (2), xandry (1), HCP (1)
 #2

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!