frame.go raw

   1  // Package wirefmt is the plaintext (v1) peer transport: it is btcd's frame
   2  // around btcd's messages.
   3  //
   4  // A connection that fell back from BIP324 to the plaintext transport is
   5  // carried entirely by btcd's wire package here: WriteMessageN puts the frame
   6  // on the connection, ReadMessageWithEncodingN takes it off, and the messages
   7  // are btcd's types. Nothing in this package encodes or decodes a message.
   8  package wirefmt
   9  
  10  import (
  11  	"bytes"
  12  	"io"
  13  
  14  	"github.com/btcsuite/btcd/wire/v2"
  15  )
  16  
  17  // v1 is btcd's plaintext encoding.
  18  //
  19  // It is LatestEncoding and not BaseEncoding: a block on the wire carries its
  20  // witnesses when the peer has negotiated a protocol at or above 70012, which
  21  // every node does, and a reader that assumes the base encoding misparses the
  22  // first witness block it is sent. The messages this side writes are the same in
  23  // both encodings, so one constant is enough.
  24  var v1 = wire.LatestEncoding
  25  
  26  // Write puts one btcd message on the connection in the plaintext frame.
  27  func Write(w io.Writer, msg wire.Message, pver uint32, btcnet wire.BitcoinNet) (int, error) {
  28  	return wire.WriteMessageWithEncodingN(w, msg, pver, btcnet, v1)
  29  }
  30  
  31  // Encode returns the frame for one message without writing it, for a caller
  32  // that writes under its own lock.
  33  func Encode(msg wire.Message, pver uint32, btcnet wire.BitcoinNet) ([]byte, error) {
  34  	var buf bytes.Buffer
  35  	if _, err := Write(&buf, msg, pver, btcnet); err != nil {
  36  		return nil, err
  37  	}
  38  	return buf.Bytes(), nil
  39  }
  40  
  41  // Read takes one message off the connection.
  42  func Read(r io.Reader, pver uint32, btcnet wire.BitcoinNet) (wire.Message, []byte, error) {
  43  	_, msg, payload, err := wire.ReadMessageWithEncodingN(r, pver, btcnet, v1)
  44  	return msg, payload, err
  45  }
  46