<p>I've been following <a href="https://github.com/zeltsi/segwit_tutorial/blob/master/addresses/demo.py" rel="nofollow noreferrer">this</a> 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.</p>
<p>I found <a href="https://reference.cash/protocol/blockchain/encoding/base58check" rel="nofollow noreferrer">this site</a> which told me how to convert the key to a compressed WIF.</p>
<p>Here's one example, I ran the script and got this output:</p>
<pre><code>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
</code></pre>
<p>I then added my own additional code to convert to WIF.</p>
<pre><code>WIF: 5KP9tSHuzYd1WTCTwr5ewace9eNM8Kv8xjsWYyELHdEkKcQkiUZ
Compressed WIF: L48nE2detzZwEzdbw5dYTuNJy9wDqLVY92DwS7Br8NwgD6S8NiyT
</code></pre>
<p>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?.</p>
<p>When I view 'details', Electrum gives me the correct compressed public key (03e5ee63efe153ebf1ec537831f2fba227f4b5f80275e750a9d0a8284c9fb4f91f).</p>
<p>Why doesn't Electrum give me a bech32 address or a nested address? Have I done the correct conversion of the private key?</p>
<p>Here's my code just in case there's an obvious mistake somehow.</p>
<pre><code>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]))
</code></pre>

CC BY-SA 4.0
2021-01-25T02:46:26.687Z
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
<electrum><segregated-witness><python>
How do I convert a private key in hex to a Segwit private key expected by Electrum?
Alfie Stoppani
# How do I convert a private key in hex to a Segwit private key expected by Electrum?

(Asked by: username_0 on 2021-01-25)

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]))

**Tags:** electrum, segregated-witness, python

> **Comments:**
> **username_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.  

---

## 0 answers

