I've been following this code to generate SegWit addresses with python. However, I'm confused because this script doesn't output the private key in a usable way for Electrum.

I found this site which told me how to convert the key to a compressed WIF.

Here's one example, I ran the script and got this output:

Private key: ce534f3d1f481f8736c30d866a6b2c5defe5edc36afaafc0e923586791a2462b
Verifiction key: e5ee63efe153ebf1ec537831f2fba227f4b5f80275e750a9d0a8284c9fb4f91fb5427e034ffec25bacc346263f0f6281f951926f924f7c53f771f632ca9e3cbb
Compressed public key: 03e5ee63efe153ebf1ec537831f2fba227f4b5f80275e750a9d0a8284c9fb4f91f
keyhash: 17f022e2c24238760a0a1b070eaa456df4e8b915
Native address: bc1qzlcz9ckzggu8vzs2rvrsa2j9dh6w3wg4atezy6
P2WPKH_V0: 001417f022e2c24238760a0a1b070eaa456df4e8b915
Hashed P2WPKH_VO: b9502db522524782980563a09355fa7052ee7ff1
P2SH_P2WPKH_V0: a9b9502db522524782980563a09355fa7052ee7ff187
Checksum: 7fcb7c1d
Binary address: 05b9502db522524782980563a09355fa7052ee7ff17fcb7c1d
Nested address: 3JarywCp1dbFiykdGNWyo5pdZyvSLEGZ96

I then added my own additional code to convert to WIF.

WIF: 5KP9tSHuzYd1WTCTwr5ewace9eNM8Kv8xjsWYyELHdEkKcQkiUZ
Compressed WIF: L48nE2detzZwEzdbw5dYTuNJy9wDqLVY92DwS7Br8NwgD6S8NiyT

I imported the compressed WIF key into Electrum, but I get 13BaDNdBXkfrGkvJT41HNosBBbbLZRWyZJ as a public key. Everything online confirms that only legacy addresses begin with a '1', but surely it is a SegWit address, no?.

When I view 'details', Electrum gives me the correct compressed public key (03e5ee63efe153ebf1ec537831f2fba227f4b5f80275e750a9d0a8284c9fb4f91f).

Why doesn't Electrum give me a bech32 address or a nested address? Have I done the correct conversion of the private key?

Here's my code just in case there's an obvious mistake somehow.

fullkey = '80' + (private_key.hex())
sha256a = hashlib.sha256(binascii.unhexlify(fullkey)).hexdigest()
sha256b = hashlib.sha256(binascii.unhexlify(sha256a)).hexdigest()
WIF = base58.b58encode(binascii.unhexlify(fullkey+sha256b[:8]))

compressedPubKey = private_key.hex()+'01'
compressed_fullkey = '80' + (compressedPubKey)
compressed_sha256a = hashlib.sha256(binascii.unhexlify(compressed_fullkey)).hexdigest()
compressed_sha256b = hashlib.sha256(binascii.unhexlify(compressed_sha256a)).hexdigest()
compressed_WIF = base58.b58encode(binascii.unhexlify(compressed_fullkey+compressed_sha256b[:8]))
  • 1
    I don't know anything about Electrum, but the 1... address is a P2PKH address, not P2WPKH or P2SH-P2WPKH. And there isn't any conversion to perform on the key; keys are keys, but they can be used to construct multiple types of addresses. Presumably you need some other way of informing Electrum that you want a P2WSH address instead of a P2PKH one. – Pieter Wuille Jan 25 at 2:46

Import it by specifying address type as prefix:


In this example it will be p2wpkh:L48nE2detzZwEzdbw5dYTuNJy9wDqLVY92DwS7Br8NwgD6S8NiyT


You will see the address: bc1qzlcz9ckzggu8vzs2rvrsa2j9dh6w3wg4atezy6 added in wallet


  • Thanks so much, you're amazing, this is exactly what was needed. So what type of address would you call the 13BaDNdBXkfrGkvJT41HNosBBbbLZRWyZJ, it can't be Legacy can it? Are these addresses distinct, if I sent coins to bc1qzlcz9ckzggu8vzs2rvrsa2j9dh6w3wg4atezy6, they wouldn't also appear at 13BaDNdBXkfrGkvJT41HNosBBbbLZRWyZJ, would they? They just use the same private key to access both addresses, but they are different because of the script type. Have I understood correctly? – Alfie Stoppani Jan 25 at 13:50
  • 13BaDNdBXkfrGkvJT41HNosBBbbLZRWyZJ is p2pkh and also called legacy address. Yes these addresses are distinct. If BTC sent to one address it won't appear for other. Yes you need the same private key to spend from both addresses. – Prayank Jan 25 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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