<blockquote>
  <p>wif -> private key</p>
</blockquote>

<ul>
<li>Use base58-decode to convert the base58 string to bytes</li>
<li>Strip the first byte (network byte) and the last 4 bytes (checksum). The remainder is the key</li>
<li>Verify that the first 4 bytes of the sha256(sha256(network byte || key)) are equal to the checksum</li>
<li>If the key ends with <code>0x01</code> (compressed) remove the last byte. You are left with the private key</li>
</ul>

<blockquote>
  <p>private key -> public key</p>
</blockquote>

<ul>
<li>Cast the private key as an integer</li>
<li><p>Multiply with the secp256k1 generator point to get a Point(x,y) on the curve. This is your public key</p></li>
<li><p>Encode your public key in a compressed format. Byte <code>0x03</code> (if y is odd) or <code>0x02</code> (if y even) followed by x as 256-bit integer</p></li>
</ul>

<blockquote>
  <p>Public key -> P2WPKH address</p>
</blockquote>

<ul>
<li>Create the witness program = ripemd160(sha256(public key))</li>
<li><a href="https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py#L118" rel="nofollow noreferrer">Encode into bech32</a> by providing the witness program, <code>bc</code> as the human readable part and <code>0</code> as witness version </li>
</ul>

<blockquote>
  <p>Public key -> P2SH-P2WPKH address</p>
</blockquote>

<ul>
<li>Create the witness program = ripemd160(sha256(public key))</li>
<li>Create the redeem script = <code>0x0014&lt;witness program&gt;</code> = witness version + push20 + witness program</li>
<li>Calculate the hash160 = ripemd160(sha256(script))</li>
<li>Prepend the hash with the version byte <code>0x05</code></li>
<li>Calculate the checksum = sha256(sha256(version byte || scripthash ))</li>
<li>Base58-encode(version byte || scripthash || first 4 bytes of checksum)</li>
</ul>

<blockquote>
  <p>How do these two formats differ and why are there two? I've seen that
  the P2SH version is compatible with older clients, are native Segwit
  addresses not?</p>
</blockquote>

<p>Old clients cannot send to segwit addresses but they can send to P2SH addresses and by extension to P2SH-P2WPKH</p>

CC BY-SA 4.0
2018-06-05T19:52:16.263Z
Is there a tool I can use to verify my segwit addresses are correct while testing?
arshbot
CC BY-SA 4.0
2018-06-05T20:09:48.427Z
@arshbot I am using this python library (disclaimer: I wrote it) https://github.com/mcdallas/cryptotools and there is also this online tool but it doesn't support segwit https://gobittest.appspot.com/
Mike D
CC BY-SA 4.0
2018-06-04T21:50:25.610Z
2018-06-04T21:50:25.610Z
Mike D
<p>Can I have the steps to generate a native Segwit address from a standard WIF? And the steps to generate a P2SH Segwit address from the same WIF? </p>

<p>How do these two formats differ and why are there two? I've seen that the P2SH version is compatible with older clients, are native Segwit addresses not? </p>

<address><segregated-witness><address-generation><p2sh>
How to generate a native Segwit address and P2SH Segwit address from a standard WIF?
arshbot
# How to generate a native Segwit address and P2SH Segwit address from a standard WIF?

(Asked by: username_0 on 2018-06-04)

Can I have the steps to generate a native Segwit address from a standard WIF? And the steps to generate a P2SH Segwit address from the same WIF?

How do these two formats differ and why are there two? I've seen that the P2SH version is compatible with older clients, are native Segwit addresses not?

**Tags:** address, segregated-witness, address-generation, p2sh


---

## 1 answers

### Answer by username_1 on 2018-06-04. Score: 4 | Accepted answer

> wif -> private key

* Use base58-decode to convert the base58 string to bytes
* Strip the first byte (network byte) and the last 4 bytes (checksum). The remainder is the key
* Verify that the first 4 bytes of the sha256(sha256(network byte || key)) are equal to the checksum
* If the key ends with `0x01` (compressed) remove the last byte. You are left with the private key

> private key -> public key

* Cast the private key as an integer
* Multiply with the secp256k1 generator point to get a Point(x,y) on the curve. This is your public key
* Encode your public key in a compressed format. Byte `0x03` (if y is odd) or `0x02` (if y even) followed by x as 256-bit integer

> Public key -> P2WPKH address

* Create the witness program = ripemd160(sha256(public key))
* [Encode into bech32](https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py#L118) by providing the witness program, `bc` as the human readable part and `0` as witness version

> Public key -> P2SH-P2WPKH address

* Create the witness program = ripemd160(sha256(public key))
* Create the redeem script = `0x0014<witness program>` = witness version + push20 + witness program
* Calculate the hash160 = ripemd160(sha256(script))
* Prepend the hash with the version byte `0x05`
* Calculate the checksum = sha256(sha256(version byte || scripthash ))
* Base58-encode(version byte || scripthash || first 4 bytes of checksum)

> How do these two formats differ and why are there two? I've seen that
> the P2SH version is compatible with older clients, are native Segwit
> addresses not?

Old clients cannot send to segwit addresses but they can send to P2SH addresses and by extension to P2SH-P2WPKH

> **Comments:**
> **username_0**: Is there a tool I can use to verify my segwit addresses are correct while testing?  
> **username_1**: @username_0 I am using this python library (disclaimer: I wrote it) https://github.com/mcdallas/cryptotools and there is also this online tool but it doesn't support segwit https://gobittest.appspot.com/  

---

