dformats

package module
v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dfmt implements message and subdomain formatting utilities as per DDRP [PIP-7](https://ddrp.network/docs/spec/pip-6.html).

Reading Envelopes

To read envelopes from an io.Reader, use the DecodeEnvelope method:

r := NewBlobReader("some-tld")
for {
	envelope, err := DecodeEnvelope(r)
	if envelope == nil {
		break
	}
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Println(string(envelope.Message.Type()))
}

Similarly, to stream envelope to an io.Writer, use the EncodeEnvelope method:

envelope := NewEnvelope(0, NewLike([32]byte{}))
w := NewBlobWriter("some-tld")
if err := EncodeEnvelope(w, envelope); err != nil {
	log.Fatalf("error: %v", err)
}

Index

Constants

View Source
const (
	// UnknownSubtype is used to represent a subtype value that
	// is unknown to its parent.
	UnknownSubtype = "UNKNOWN"

	// BlobLen is the maximum size of a DDRP blob.
	BlobLen = 16 * 1024 * 1024

	ConnectionTypeFollow = "FOLLOW"
	ConnectionTypeBlock  = "BLOCK"
	ModerationTypeLike   = "LIKE"
	ModerationTypePin    = "PIN"
)
View Source
const (
	// The maximum length a given subdomain can be. See specification.
	MaxSubdomainLen = 15

	// The end offset at which subdomain records can be found.
	EndSubdomainRecordsOffset = 32 * 1024

	// Magic bytes denoting a blob that contains subdomains.
	SubdomainMagic = "SUB"

	// The end offset for reserved blob data.
	EndReservedDataOffset = 64 * 1024
)

Variables

View Source
var (
	// DefaultSubtype is used to signify an empty subtype.
	DefaultSubtype = [4]byte{0x00, 0x00, 0x00, 0x00}

	// DefaultVersion is used to signify the default message version.
	DefaultVersion = uint8(1)

	// PostType contains the PST MessageType.
	PostType = [3]byte{0x50, 0x53, 0x54}

	// ConnectionType contains the CNT MessageType.
	ConnectionType = [3]byte{0x43, 0x4E, 0x54}
	// FollowSubtype contains the F\00\00\00 Connection subtype.
	FollowSubtype = [4]byte{0x46, 0x00, 0x00, 0x00}
	// BlockSubtype contains the B\00\00\00 Connection subtype.
	BlockSubtype = [4]byte{0x42, 0x00, 0x00, 0x00}

	// MediaType contains the MDA MessageType.
	MediaType = [3]byte{0x4D, 0x44, 0x41}

	// ModerationType contains the MOD MessageType.
	ModerationType = [3]byte{0x4D, 0x4F, 0x44}
	// LikeSubtype contains the L\00\00\00 Moderation subtype.
	LikeSubtype = [4]byte{0x4C, 0x00, 0x00, 0x00}
	// PinSubtype contains the P\00\00\00 Moderation subtype.
	PinSubtype = [4]byte{0x50, 0x00, 0x00, 0x00}
)

Functions

func EncodeEnvelope

func EncodeEnvelope(w io.Writer, envelope *Envelope) error

EncodeEnvelope writes an Envelope to a Writer. It writes the Envelope to an in-memory buffer first so that the Envelope's length field can be written to the underlying Writer before the Envelope's content.

func HashEnvelope

func HashEnvelope(envelope *Envelope, subdomain string, tld string) ([32]byte, error)

func IsSubdomainBlob added in v1.7.0

func IsSubdomainBlob(r io.Reader) (bool, error)

IsSubdomainBlob reads the first three bytes of the provided reader, and returns true if they equal the subdomain blob magic bytes.

func NewGUID

func NewGUID() [8]byte

NewGUID generates an 8-byte random GUID.

func NewSubdomainDataReader added in v1.9.0

func NewSubdomainDataReader(r io.ReaderAt) *io.SectionReader

NewSubdomainDataReader returns an io.SectionReader that is limited to the portion of the underlying io.readerAt that can contain subdomain data.

func NewSubdomainRecordsReader

func NewSubdomainRecordsReader(r io.ReaderAt) *io.SectionReader

NewSubdomainRecordsReader returns an io.SectionReader that is limited to the portion of the underlying io.ReaderAt that can contain subdomain records.

Types

type Connection

type Connection struct {
	TLD       string
	Subdomain string
	// contains filtered or unexported fields
}

Connection is a container for DDRP Connection messages.

func NewBlock

func NewBlock() *Connection

NewBlock returns a Connection with the Block subtype.

func NewConnection

func NewConnection(version uint8, subtype [4]byte) *Connection

NewConnection returns a Connection with the given subtype.

func NewFollow

func NewFollow() *Connection

NewFollow returns a Connection with the Follow subtype.

func (*Connection) ConnectionType

func (c *Connection) ConnectionType() string

ConnectionType returns the human-readable representation of the Connection's subtype. Can be ConnectionTypeFollow, ConnectionTypeBlock, or UnknownSubtype.

func (*Connection) Decode

func (c *Connection) Decode(r io.Reader) error

func (*Connection) Encode

func (c *Connection) Encode(w io.Writer) error

func (*Connection) Subtype

func (c *Connection) Subtype() [4]byte

func (*Connection) Type

func (c *Connection) Type() [3]byte

func (*Connection) Version added in v1.3.0

func (c *Connection) Version() uint8

type Envelope

type Envelope struct {
	ID             [8]byte
	NameIndex      uint8
	Timestamp      time.Time
	Signature      []byte
	Message        Message
	AdditionalData []byte
}

An Envelope is a container for a Message. See doc.go for examples of how Envelopes and Messages are structured on the wire.

func DecodeEnvelope

func DecodeEnvelope(r io.Reader) (*Envelope, error)

DecodeEnvelope reads an Envelope from a Reader. It reads the Envelope to an in-memory buffer first after reading the Envelope's length field. DecodeEnvelope will return (nil, nil) if \x00 is encountered for the envelope's length, and is a signal to stop reading.

func NewEnvelope

func NewEnvelope(id [8]byte, nameIndex uint8, message Message) *Envelope

NewEnvelope constructs a new Envelope.

func (*Envelope) Decode

func (e *Envelope) Decode(r io.Reader) error

Decode reads an Envelope from the Reader. This method should generally not be used directly because it does not read the Envelope's length field. DecodeEnvelope should be used instead. The Reader passed to this method must be bounded: ioutil.ReadAll is used to populate the AdditionalData field, which will cause unbounded Readers to hang. If an unsupported MessageType is encountered, the type of the Message field will be UnknownMessage and the AdditionalData field will be empty.

func (*Envelope) Encode

func (e *Envelope) Encode(w io.Writer) error

Encode writes an Envelope to the Writer. This method should generally not be used directly because it does not write the Envelope's length field. EncodeEnvelope should be used instead.

type Media

type Media struct {
	Filename string
	MimeType string
	Content  []byte
	// contains filtered or unexported fields
}

Media is a container for DDRP Media messages.

func NewMedia

func NewMedia(version uint8, subtype [4]byte) *Media

NewMedia returns a Media with the given subtype.

func (*Media) Decode

func (m *Media) Decode(r io.Reader) error

func (*Media) Encode

func (m *Media) Encode(w io.Writer) error

func (*Media) Subtype

func (m *Media) Subtype() [4]byte

func (*Media) Type

func (m *Media) Type() [3]byte

func (*Media) Version added in v1.3.0

func (m *Media) Version() uint8

type Message

type Message interface {
	dwire.EncodeDecoder

	// Type returns the message's 3-byte supertype.
	Type() [3]byte

	// Version returns the message's version.
	Version() uint8

	// Subtype returns the message's 5-byte subtype.
	Subtype() [4]byte
}

Message is the generic interface to the various DDRP social message types. See doc.go for more about how Envelopes and Messages are structures on the wire.

type Moderation

type Moderation struct {
	Reference [32]byte
	// contains filtered or unexported fields
}

Moderation is a container for DDRP Moderation messages.

func NewLike

func NewLike() *Moderation

NewLike returns a Moderation with the Like subtype.

func NewModeration

func NewModeration(version uint8, subtype [4]byte) *Moderation

NewModeration returns a Moderation with the given subtype.

func NewPin

func NewPin() *Moderation

NewPin returns a Moderation with the Pin subtype.

func (*Moderation) Decode

func (m *Moderation) Decode(r io.Reader) error

func (*Moderation) Encode

func (m *Moderation) Encode(w io.Writer) error

func (*Moderation) ModerationType

func (m *Moderation) ModerationType() string

ModerationType returns the human-readable representation of the Moderation's subtype. Can be ModerationTypeLike, ModerationTypePin, or UnknownSubtype.

func (*Moderation) Subtype

func (m *Moderation) Subtype() [4]byte

func (*Moderation) Type

func (m *Moderation) Type() [3]byte

func (*Moderation) Version added in v1.3.0

func (m *Moderation) Version() uint8

type Post

type Post struct {
	Body      string
	Title     string
	Reference [32]byte
	Topic     string
	Tags      []string
	// contains filtered or unexported fields
}

Post is a container for DDRP Post messages.

func NewPost

func NewPost(version uint8, subtype [4]byte) *Post

NewPost returns a new Post with the given subtype.

func (*Post) Decode

func (p *Post) Decode(r io.Reader) error

func (*Post) Encode

func (p *Post) Encode(w io.Writer) error

func (*Post) Subtype

func (p *Post) Subtype() [4]byte

func (*Post) Type

func (p *Post) Type() [3]byte

func (*Post) Version added in v1.3.0

func (p *Post) Version() uint8

type SubdomainRecord

type SubdomainRecord struct {
	// The subdomain itself.
	Name string

	// The index identifying this subdomain.
	Index uint8

	// Public key belonging to this subdomain. Currently unused.
	PublicKey [33]byte
}

A SubdomainRecord holds information about DDRP blob subdomain data.

func (*SubdomainRecord) Decode

func (s *SubdomainRecord) Decode(r io.Reader) error

Decode reads a subdomain record from the Reader.

func (*SubdomainRecord) Encode

func (s *SubdomainRecord) Encode(w io.Writer) error

Encode writes a subdomain record to the Writer.

type UnknownMessage

type UnknownMessage struct {
	// MessageType is the type received on-the-wire.
	MessageType [3]byte
	// MessageVersion is the version received on-the-wire.
	MessageVersion uint8
	// MessageSubtype is the subtype received on-the-wire.
	MessageSubtype [4]byte
	// Data is a byte array of the message's contents.
	Data []byte
}

UnknownMessage represents a correctly-formatted but unknown DDRP message.

func (*UnknownMessage) Decode

func (u *UnknownMessage) Decode(r io.Reader) error

func (*UnknownMessage) Encode

func (u *UnknownMessage) Encode(w io.Writer) error

func (*UnknownMessage) Subtype

func (u *UnknownMessage) Subtype() [4]byte

func (*UnknownMessage) Type

func (u *UnknownMessage) Type() [3]byte

func (*UnknownMessage) Version added in v1.3.0

func (u *UnknownMessage) Version() uint8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL