<blockquote>
  <p>it seems like the process to calculate a checksum for an extended key is to decode the base58 string to data, </p>
</blockquote>

<p>If <code>raw_xpub = version + depth + fingerprint + child + chain + DATA</code>
than to make the checksum you need to do doublehash of xpriv/xpub (sha256), in python:</p>

<pre><code>hashed_xpub = hashlib.sha256(raw_xpub).digest()
hashed_xpub = hashlib.sha256(hashed_xpub).digest()
</code></pre>

<p>and then take the checksum as first 4 characters (0 to 3) and add it at the end of the raw xpub:</p>

<pre><code>raw_xpub += hashed_xpub[:4]
</code></pre>

<p>Finally, run it through Base58 to encode it. I think you've might performed base58 operation too early based on your question. It's the last step in the process.</p>

CC BY-SA 4.0
2020-05-06T05:02:14.017Z
2020-05-06T05:08:50.283Z
2020-05-06T05:08:50.283Z
Tony Sanak
<p>Very difficult to find definitive info on this.</p>

<p>From what I can find on google and looking at BIP32 it seems like the process to calculate a checksum for an extended key is to decode the base58 string to data, then perform a double sha256 on the data and the first 4 bytes from the double hash are the checksum.</p>

<p>I am trying to take in a non xpub (vpubs, upub ypub etc) and convert it to an xpub/tpub programmatically. Currently I have it working but the last 6 digits of the xpub are always incorrect, I am assuming this is the checksum?</p>

<p>In order to correctly calculate the checksum do I remove the prefix first, then remove the existing checksum then do the double sha256 on the stripped down extended key or do I keep the new xpub prefix and then perform the double sha256? Or am I completely wrong in my assumptions here?</p>

<p>Thanks in advanced!</p>

<bitcoind><bip32-hd-wallets><xpub><checksum>
How to calculate the checksum of an extended public key?
Fontaine
# How to calculate the checksum of an extended public key?

(Asked by: username_0 on 2020-05-06)

Very difficult to find definitive info on this.

From what I can find on google and looking at BIP32 it seems like the process to calculate a checksum for an extended key is to decode the base58 string to data, then perform a double sha256 on the data and the first 4 bytes from the double hash are the checksum.

I am trying to take in a non xpub (vpubs, upub ypub etc) and convert it to an xpub/tpub programmatically. Currently I have it working but the last 6 digits of the xpub are always incorrect, I am assuming this is the checksum?

In order to correctly calculate the checksum do I remove the prefix first, then remove the existing checksum then do the double sha256 on the stripped down extended key or do I keep the new xpub prefix and then perform the double sha256? Or am I completely wrong in my assumptions here?

Thanks in advanced!

**Tags:** bitcoind, bip32-hd-wallets, xpub, checksum


---

## 1 answers

### Answer by username_1 on 2020-05-06. Score: 2

> it seems like the process to calculate a checksum for an extended key is to decode the base58 string to data,

If `raw_xpub = version + depth + fingerprint + child + chain + DATA`
than to make the checksum you need to do doublehash of xpriv/xpub (sha256), in python:

```
hashed_xpub = hashlib.sha256(raw_xpub).digest()
hashed_xpub = hashlib.sha256(hashed_xpub).digest()

```

and then take the checksum as first 4 characters (0 to 3) and add it at the end of the raw xpub:

```
raw_xpub += hashed_xpub[:4]

```

Finally, run it through Base58 to encode it. I think you've might performed base58 operation too early based on your question. It's the last step in the process.


---

