// Package wirefmt is the plaintext (v1) peer transport: it is btcd's frame // around btcd's messages. // // A connection that fell back from BIP324 to the plaintext transport is // carried entirely by btcd's wire package here: WriteMessageN puts the frame // on the connection, ReadMessageWithEncodingN takes it off, and the messages // are btcd's types. Nothing in this package encodes or decodes a message. package wirefmt import ( "bytes" "io" "github.com/btcsuite/btcd/wire/v2" ) // v1 is btcd's plaintext encoding. // // It is LatestEncoding and not BaseEncoding: a block on the wire carries its // witnesses when the peer has negotiated a protocol at or above 70012, which // every node does, and a reader that assumes the base encoding misparses the // first witness block it is sent. The messages this side writes are the same in // both encodings, so one constant is enough. var v1 = wire.LatestEncoding // Write puts one btcd message on the connection in the plaintext frame. func Write(w io.Writer, msg wire.Message, pver uint32, btcnet wire.BitcoinNet) (int, error) { return wire.WriteMessageWithEncodingN(w, msg, pver, btcnet, v1) } // Encode returns the frame for one message without writing it, for a caller // that writes under its own lock. func Encode(msg wire.Message, pver uint32, btcnet wire.BitcoinNet) ([]byte, error) { var buf bytes.Buffer if _, err := Write(&buf, msg, pver, btcnet); err != nil { return nil, err } return buf.Bytes(), nil } // Read takes one message off the connection. func Read(r io.Reader, pver uint32, btcnet wire.BitcoinNet) (wire.Message, []byte, error) { _, msg, payload, err := wire.ReadMessageWithEncodingN(r, pver, btcnet, v1) return msg, payload, err }