user127077: From 'Mastering bitcoin' -
checksum = SHA256(SHA256(prefix+data))

The 'data' is, for example, an address, which was calculated using SHA256 and then RIPEMD160.
Why do we need double hashing when calculating the checksum?

user127388: Prevents some types of attacks.
Satoshi wanted to build something for the ages and was trying to prevent any and all attacks.
See here for details
Why are hashes in the bitcoin protocol typically computed twice (double computed)?

user208: Satoshi standardized on using double-SHA256 for 32-byte hashes, and SHA256+RIPEMD160 (each once) for 20-byte hashes, presumably because of (likely misguided) concern about certain attacks (like length extension attacks, which only apply when hashing secret data), and then used those everywhere:

P2PKH scriptPubKeys use SHA256+RIPEMD160 of the public key
P2PKH address checksums use truncated double-SHA256 of the payload
Transaction hashes (txids) use double-SHA256
Block hashes (and equivalently, proof of work) use double-SHA256
The transaction Merkle tree uses double-SHA256 to compute the next layer.
P2P network packets use truncated double-SHA256 of the payload as checksum.
The message being ECDSA signed to authorize transactions (sighash) is the double-SHA256 hash of a variant of the serialization of a transaction.

To the best of my knowledge, none of these use cases actually benefit at all from double hashing.
Later additions at first copied this practice, presumably because the rationale wasn't that well understood, and continuing with the same approach was easiest to convince people was safe:

P2SH addresses (BIP13) use truncated double-SHA256 as a checksum.
P2SH scriptPubKeys (BIP16) use SHA256+RIPEMD160 of the script.
WIF ("wallet import format") private keys also use truncated double-SHA256 as a checksum.
The same is true for the extended key xpub and xprv formats (BIP32). The derivation algorithm for keys there uses HMAC-SHA512 (which for that purpose is similar to double-SHA512).
P2WPKH scriptPubKeys use SHA256+RIPEMD160 hashes of the public key (BIP141)
Witness transaction identifiers (wtxids) use double-SHA256 like normal txids do (BIP141).
The witness Merkle tree uses the same double-SHA256 based hashing as the transaction tree (BIP141).
The message being ECDSA signed for authorizing witness v0 scripts is the double-SHA256 hash of a different message scheme (BIP143).

In more modern additions, it is no longer used:

P2WSH scriptPubKeys use a single SHA256 hash of the public key (BIP141).
Bech32 and Bech32m address encoding (used for native P2WPKH, P2WSH, and soon, P2TR) addresses use a checksum based on error correction codes, not hashing (BIP173 and BIP350).
The upcoming taproot softfork will introduce P2TR scriptPubKeys, which combine public keys and scripts, in a scheme which involves multiple hashing steps, but they are all single-SHA256 (with a tag prefixed to them to guarantee uniquenesss). The same is true for the message being (Schnorr) signed, and the hash inside the signature algorithm. (BIP340, BIP341, BIP342).

In addition, various places where no cryptographically-sized hash is needed have been using other hash functions too:

In the (now deprecated) server-side Bloom filtering protocol, MurmurHash3 was used to determine filter positions (BIP37).
In client-side compact block filtering, SipHash-2-4 is used to determine filter positions (BIP158).
In the compact block P2P protocol extension, SipHash-2-4 is used to generate short (48-bit) transaction identifiers (BIP152).

Disclaimer: I'm a co-author of several proposals mentioned here, and may have forgotten other extensions.

127077: <p>From '<a href="https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch04.asciidoc#base58alphabet" rel="noreferrer">Mastering bitcoin</a>' -</p>
<pre><code>checksum = SHA256(SHA256(prefix+data))
</code></pre>
<p>The 'data' is, for example, an address, which was calculated using SHA256 and then RIPEMD160.
Why do we need double hashing when calculating the checksum?</p>
 127388: <p>Prevents some types of attacks.</p>
<p>Satoshi wanted to build something for the ages and was trying to prevent any and all attacks.</p>
<p>See here for details
<a href="https://bitcoin.stackexchange.com/questions/6037/why-are-hashes-in-the-bitcoin-protocol-typically-computed-twice-double-computed">Why are hashes in the bitcoin protocol typically computed twice (double computed)?</a></p>
 208: <p>Satoshi standardized on using double-SHA256 for 32-byte hashes, and SHA256+RIPEMD160 (each once) for 20-byte hashes, presumably because of (likely misguided) concern about certain attacks (like length extension attacks, which only apply when hashing secret data), and then used those everywhere:</p>
<ul>
<li>P2PKH scriptPubKeys use SHA256+RIPEMD160 of the public key</li>
<li>P2PKH address checksums use truncated double-SHA256 of the payload</li>
<li>Transaction hashes (txids) use double-SHA256</li>
<li>Block hashes (and equivalently, proof of work) use double-SHA256</li>
<li>The transaction Merkle tree uses double-SHA256 to compute the next layer.</li>
<li>P2P network packets use truncated double-SHA256 of the payload as checksum.</li>
<li>The message being ECDSA signed to authorize transactions (sighash) is the double-SHA256 hash of a variant of the serialization of a transaction.</li>
</ul>
<p>To the best of my knowledge, none of these use cases actually benefit at all from double hashing.</p>
<p>Later additions at first copied this practice, presumably because the rationale wasn't that well understood, and continuing with the same approach was easiest to convince people was safe:</p>
<ul>
<li>P2SH addresses (<a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="noreferrer">BIP13</a>) use truncated double-SHA256 as a checksum.</li>
<li>P2SH scriptPubKeys (<a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="noreferrer">BIP16</a>) use SHA256+RIPEMD160 of the script.</li>
<li>WIF (&quot;wallet import format&quot;) private keys also use truncated double-SHA256 as a checksum.</li>
<li>The same is true for the extended key xpub and xprv formats (<a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki" rel="noreferrer">BIP32</a>). The derivation algorithm for keys there uses <a href="https://en.wikipedia.org/wiki/HMAC" rel="noreferrer">HMAC</a>-SHA512 (which for that purpose is similar to double-SHA512).</li>
<li>P2WPKH scriptPubKeys use SHA256+RIPEMD160 hashes of the public key (<a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">BIP141</a>)</li>
<li>Witness transaction identifiers (wtxids) use double-SHA256 like normal txids do (<a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">BIP141</a>).</li>
<li>The witness Merkle tree uses the same double-SHA256 based hashing as the transaction tree (<a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">BIP141</a>).</li>
<li>The message being ECDSA signed for authorizing witness v0 scripts is the double-SHA256 hash of a different message scheme (<a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="noreferrer">BIP143</a>).</li>
</ul>
<p>In more modern additions, it is no longer used:</p>
<ul>
<li>P2WSH scriptPubKeys use a <strong>single</strong> SHA256 hash of the public key (<a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">BIP141</a>).</li>
<li>Bech32 and Bech32m address encoding (used for native P2WPKH, P2WSH, and soon, P2TR) addresses use a checksum based on error correction codes, not hashing (<a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="noreferrer">BIP173</a> and <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="noreferrer">BIP350</a>).</li>
<li>The upcoming taproot softfork will introduce P2TR scriptPubKeys, which combine public keys and scripts, in a scheme which involves multiple hashing steps, but they are all single-SHA256 (with a tag prefixed to them to guarantee uniquenesss). The same is true for the message being (Schnorr) signed, and the hash inside the signature algorithm. (<a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki" rel="noreferrer">BIP340</a>, <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="noreferrer">BIP341</a>, <a href="https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki" rel="noreferrer">BIP342</a>).</li>
</ul>
<p>In addition, various places where no cryptographically-sized hash is needed have been using other hash functions too:</p>
<ul>
<li>In the (now deprecated) server-side Bloom filtering protocol, <a href="https://en.wikipedia.org/wiki/MurmurHash" rel="noreferrer">MurmurHash3</a> was used to determine filter positions (<a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" rel="noreferrer">BIP37</a>).</li>
<li>In client-side compact block filtering, <a href="https://en.wikipedia.org/wiki/SipHash" rel="noreferrer">SipHash-2-4</a> is used to determine filter positions (<a href="https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki" rel="noreferrer">BIP158</a>).</li>
<li>In the compact block P2P protocol extension, <a href="https://en.wikipedia.org/wiki/SipHash" rel="noreferrer">SipHash-2-4</a> is used to generate short (48-bit) transaction identifiers (<a href="https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki" rel="noreferrer">BIP152</a>).</li>
</ul>
<p>Disclaimer: I'm a co-author of several proposals mentioned here, and may have forgotten other extensions.</p>
