// Package btcwire implements the Bitcoin P2P wire format and the message // payloads needed by a BIP157 compact-filter light client. // // The package is a pure codec: it performs no I/O of its own beyond reading // from and writing to the io.Reader and []byte values supplied by the caller. // It deliberately avoids the net and os packages so that it can be compiled // for constrained targets such as wasm. // // # Framing // // Every message is framed as a 24-byte header followed by the payload: // // magic uint32, big endian // command 12 bytes, ASCII, NUL padded // length uint32, little endian // checksum 4 bytes, first 4 bytes of SHA256d(payload) // payload length bytes // // EncodeMessage produces one such frame; DecodeMessage and Reader consume // them. // // # Byte order // // Unless documented otherwise, integers use Bitcoin's little-endian wire // encoding. Hashes are chainhash.Hash values in internal byte order: the // bytes as they appear on the wire, which is the reverse of the order used // for display. // // The frame magic is big endian, and network ports are big endian as specified // by the version message. Both are exceptions: a magic is a four-byte string // that Bitcoin writes "f9beb4d9" and sends as those bytes, so the uint32 // constants hold that string read big endian and the codec writes it back out // the same way. Writing a magic little endian reverses it on the wire, and a // node reads the reversed bytes as an unknown network and drops the peer. // // # Untrusted input // // Decoders never panic: truncated, oversized, or otherwise invalid input is // reported as an error. Truncation errors wrap io.ErrUnexpectedEOF, so // callers can test for them with errors.Is. Compact size integers must be // minimally encoded. // // Byte slices returned by DecodePayload (and by ReadVarBytes) alias the input // buffer they were decoded from. Callers that keep such a slice after the // payload buffer is reused must copy it. package btcwire