encode

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: BlueOak-1.0.0 Imports: 13 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// IntCoder is the DEX-wide integer byte-encoding order. IntCoder must be
	// BigEndian so that variable length data encodings work as intended.
	IntCoder = binary.BigEndian
	// ByteFalse is a byte-slice representation of boolean false.
	ByteFalse = []byte{0}
	// ByteTrue is a byte-slice representation of boolean true.
	ByteTrue = []byte{1}
	// MaxDataLen is the largest byte slice that can be stored when using
	// (BuildyBytes).AddData.
	MaxDataLen = 0x00fe_ffff // top two bytes in big endian stop at 254, signalling 32-bit len
)

Functions

func BytesToUint32

func BytesToUint32(i []byte) uint32

BytesToUint32 converts the length-4, big-endian encoded byte slice to a uint32.

func ClearBytes

func ClearBytes(b []byte)

ClearBytes zeroes the byte slice.

func CopySlice

func CopySlice(b []byte) []byte

CopySlice makes a copy of the slice.

func DecodeBlob

func DecodeBlob(b []byte, preAlloc ...int) (byte, [][]byte, error)

DecodeBlob decodes a versioned blob into its version and the pushes extracted from its data. Empty pushes will be nil.

func DecodeUTime

func DecodeUTime(b []byte) time.Time

DecodeUTime interprets bytes as a uint64 millisecond Unix timestamp and creates a time.Time.

func DropMilliseconds

func DropMilliseconds(t time.Time) time.Time

DropMilliseconds returns the time truncated to the previous second.

func ExtractPushes

func ExtractPushes(b []byte, preAlloc ...int) ([][]byte, error)

ExtractPushes parses the linearly-encoded 2D byte slice into a slice of slices. Empty pushes are nil slices.

func FileHash

func FileHash(name string) ([]byte, error)

FileHash generates the SHA256 hash of the specified file.

func RandomBytes

func RandomBytes(len int) []byte

RandomBytes returns a byte slice with the specified length of random bytes.

func Uint16Bytes

func Uint16Bytes(i uint16) []byte

Uint64Bytes converts the uint16 to a length-2, big-endian encoded byte slice.

func Uint32Bytes

func Uint32Bytes(i uint32) []byte

Uint64Bytes converts the uint32 to a length-4, big-endian encoded byte slice.

func Uint64Bytes

func Uint64Bytes(i uint64) []byte

Uint64Bytes converts the uint64 to a length-8, big-endian encoded byte slice.

Types

type BuildyBytes

type BuildyBytes []byte

BuildyBytes is a byte-slice with an AddData method for building linearly encoded 2D byte slices. The AddData method supports chaining. The canonical use case is to create "versioned blobs", where the BuildyBytes is instantiated with a single version byte, and then data pushes are added using the AddData method. Example use:

version := 0
b := BuildyBytes{version}.AddData(data1).AddData(data2)

The versioned blob can be decoded with DecodeBlob to separate the version byte and the "payload". BuildyBytes has some similarities to dcrd's txscript.ScriptBuilder, though simpler and less efficient.

func (BuildyBytes) AddData

func (b BuildyBytes) AddData(d []byte) BuildyBytes

AddData adds the data to the BuildyBytes, and returns the new BuildyBytes. The data has hard-coded length limit of MaxDataLen = 16711679 bytes. The caller should ensure the data is not larger since AddData panics if it is.

type PassBytes

type PassBytes []byte

PassBytes represents a UTF8-encoded byte slice.

func (PassBytes) Clear

func (pb PassBytes) Clear()

Clear zeroes the slice.

func (PassBytes) MarshalJSON

func (pb PassBytes) MarshalJSON() ([]byte, error)

MarshalJSON satisfies the json.Unmarshaler interface, returns a quoted copy of this byte slice. Returns an error if this byte slice is not a valid UTF8-encoded byte slice.

func (*PassBytes) UnmarshalJSON

func (pb *PassBytes) UnmarshalJSON(rawBytes []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface, parses JSON-encoded data into UTF8-encoded bytes and stores the result in the `PassBytes` pointer.

Jump to

Keyboard shortcuts

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