Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251"""Encrypt a BIP324 packet.253Returns:254bytes - encrypted packet contents255"""256assert len(contents) <= 2**24 - 1257header = (ignore << IGNORE_BIT_POS).to_bytes(HEADER_LEN, 'little')258plaintext = header + contents259aead_ciphertext = self.peer['send_P'].encrypt(aad, plaintext)260enc_plaintext_len = self.peer['send_L'].crypt(len(contents).to_bytes(LENGTH_FIELD_LEN, 'little'))261return enc_plaintext_len + aead_ciphertext263def v2_receive_packet(self, response, aad=b''):264"""Decrypt a BIP324 packet266Returns:2671. int - number of bytes consumed (or -1 if error)2682. bytes - contents of decrypted non-decoy packet if any (or None otherwise)269"""270if self.contents_len == -1:271if len(response) < LENGTH_FIELD_LEN:272return 0, None273enc_contents_len = response[:LENGTH_FIELD_LEN]274self.contents_len = int.from_bytes(self.peer['recv_L'].crypt(enc_contents_len), 'little')275response = response[LENGTH_FIELD_LEN:]276if len(response) < HEADER_LEN + self.contents_len + CHACHA20POLY1305_EXPANSION:277return 0, None278aead_ciphertext = response[:HEADER_LEN + self.contents_len + CHACHA20POLY1305_EXPANSION]279plaintext = self.peer['recv_P'].decrypt(aad, aead_ciphertext)280if plaintext is None:281return -1, None # disconnect282header = plaintext[:HEADER_LEN]283length = LENGTH_FIELD_LEN + HEADER_LEN + self.contents_len + CHACHA20POLY1305_EXPANSION284self.contents_len = -1285return length, None if (header[0] & (1 << IGNORE_BIT_POS)) else plaintext[HEADER_LEN:]
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.