Version: v1.1.1 Latest Latest

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 21 Imported by: 63




View Source
const (
	// Name is the protocol slug.
	Name = "thread"
	// Code is the protocol code.
	Code = 406
	// Version is the current protocol version.
	Version = "0.0.1"
	// Protocol is the threads protocol tag.
	Protocol protocol.ID = "/" + Name + "/" + Version
View Source
const CounterUndef int64 = 0
View Source
const (
	V1 = 0x01



View Source
var (
	// ErrVarintBuffSmall means that a buffer passed to the ID parser was not
	// long enough, or did not contain an invalid ID.
	ErrVarintBuffSmall = fmt.Errorf("reading varint: buffer too small")

	// ErrVarintTooBig means that the varint in the given ID was above the
	// limit of 2^64.
	ErrVarintTooBig = fmt.Errorf("reading varint: varint bigger than 64bits" +
		" and not supported")

	// ErrIDTooShort means that the ID passed to decode was not long
	// enough to be a valid ID.
	ErrIDTooShort = fmt.Errorf("id too short")
View Source
var (
	// ErrInvalidKey indicates an invalid byte slice was given to KeyFromBytes.
	ErrInvalidKey = fmt.Errorf("invalid key")
View Source
var ErrInvalidToken = fmt.Errorf("invalid thread token")

ErrInvalidToken indicates the token is invalid.

View Source
var ErrTokenNotFound = fmt.Errorf("thread token not found")

ErrTokenNotFound indicates the token was not found in the context.

View Source
var HeadUndef = Head{
	ID:      cid.Undef,
	Counter: CounterUndef,
View Source
var Undef = ID("")

Undef can be used to represent a nil or undefined Cid, using Cid{} directly is also acceptable.


func ExtractEncoding

func ExtractEncoding(v string) (mbase.Encoding, error)

ExtractEncoding from an ID. If Decode on the same string did not return an error neither will this function.

func NewTokenContext added in v0.1.13

func NewTokenContext(ctx context.Context, token Token) context.Context

NewTokenContext adds Token to a context.

func ToAddr added in v0.1.12

func ToAddr(id ID) ma.Multiaddr

ToAddr returns ID wrapped as a multiaddress.


type Credentials added in v0.1.13

type Credentials struct {
	Secure bool

Credentials implements PerRPCCredentials, allowing context values to be included in request metadata.

func (Credentials) GetRequestMetadata added in v0.1.13

func (c Credentials) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error)

func (Credentials) RequireTransportSecurity added in v0.1.13

func (c Credentials) RequireTransportSecurity() bool
type Head struct {
	// ID of the head
	ID cid.Cid
	// Counter is the number of logs in the head
	Counter int64

Head represents the log head (including the number of records in the log and the id of the head)

type ID

type ID string

ID represents a self-describing thread identifier. It is formed by a Version, a Variant, and a random number of a given length.

func Cast

func Cast(data []byte) (ID, error)

Cast takes an ID data slice, parses it and returns an ID. For IDV1, the data buffer is in the form:


Please use decode when parsing a regular ID string, as Cast does not expect multibase-encoded data. Cast accepts the output of ID.Bytes().

func Decode

func Decode(v string) (ID, error)

Decode parses an ID-encoded string and returns an ID object. For IDV1, an ID-encoded string is primarily a multibase string:


The base-encoded string represents a:


func FromAddr added in v0.1.12

func FromAddr(addr ma.Multiaddr) (ID, error)

FromAddr returns ID from a multiaddress if present.

func NewIDV1

func NewIDV1(variant Variant, size uint8) ID

NewIDV1 returns a new random ID using the given variant.

func (ID) Bytes

func (i ID) Bytes() []byte

Bytes returns the byte representation of an ID. The output of bytes can be parsed back into an ID with Cast().

func (ID) Defined

func (i ID) Defined() bool

Defined returns true if an ID is defined. Calling any other methods on an undefined ID will result in undefined behavior.

func (ID) Encode

func (i ID) Encode(base mbase.Encoder) string

Encode return the string representation of an ID in a given base when applicable.

func (ID) Equals

func (i ID) Equals(o ID) bool

Equals checks that two IDs are the same.

func (ID) KeyString

func (i ID) KeyString() string

KeyString returns the binary representation of the ID as a string.

func (ID) Loggable

func (i ID) Loggable() map[string]interface{}

Loggable returns a Loggable (as defined by

func (ID) MarshalBinary

func (i ID) MarshalBinary() ([]byte, error)

MarshalBinary is equivalent to Bytes(). It implements the encoding.BinaryMarshaler interface.

func (ID) MarshalText

func (i ID) MarshalText() ([]byte, error)

MarshalText is equivalent to String(). It implements the encoding.TextMarshaler interface.

func (ID) String

func (i ID) String() string

String returns the default string representation of an ID. Currently, Base32 is used as the encoding for the multibase string.

func (ID) StringOfBase

func (i ID) StringOfBase(base mbase.Encoding) (string, error)

StringOfBase returns the string representation of an ID encoded is selected base.

func (*ID) UnmarshalBinary

func (i *ID) UnmarshalBinary(data []byte) error

UnmarshalBinary is equivalent to Cast(). It implements the encoding.BinaryUnmarshaler interface.

func (*ID) UnmarshalText

func (i *ID) UnmarshalText(text []byte) error

UnmarshalText is equivalent to Decode(). It implements the encoding.TextUnmarshaler interface.

func (ID) Validate added in v0.1.18

func (i ID) Validate() error

Validate the ID.

func (ID) Variant

func (i ID) Variant() Variant

Variant returns the variant of an ID.

func (ID) Version

func (i ID) Version() uint64

Version returns the ID version.

type IDSlice

type IDSlice []ID

IDSlice for sorting threads.

func (IDSlice) Len

func (s IDSlice) Len() int

func (IDSlice) Less

func (s IDSlice) Less(i, j int) bool

func (IDSlice) Swap

func (s IDSlice) Swap(i, j int)

type Identity added in v0.1.13

type Identity interface {

	// Sign the given bytes cryptographically.
	Sign(context.Context, []byte) ([]byte, error)
	// GetPublic returns the public key paired with this identity.
	GetPublic() PubKey
	// Decrypt returns decrypted data.
	Decrypt(context.Context, []byte) ([]byte, error)
	// Equals returns true if the identities are equal.
	Equals(Identity) bool

Identity represents an entity capable of signing a message and returning the associated public key for verification. In many cases, this will just be a private key, but callers can use any setup that suits their needs.

func NewLibp2pIdentity added in v0.1.13

func NewLibp2pIdentity(key crypto.PrivKey) Identity

NewLibp2pIdentity returns a new Libp2pIdentity.

type Info

type Info struct {
	ID    ID
	Key   Key
	Logs  []LogInfo
	Addrs []ma.Multiaddr

Info holds thread logs, keys and addresses.

func (Info) GetFirstPrivKeyLog deprecated added in v1.0.0

func (i Info) GetFirstPrivKeyLog() *LogInfo

GetFirstPrivKeyLog returns the first log found with a private key. This is a strict owership check, vs returning all directly 'managed' logs. Deprecated: This is no longer safe to use.

type Key added in v0.1.12

type Key struct {
	// contains filtered or unexported fields

Key is a thread encryption key with two components. Service key is used to encrypt outer log record linkages. Read key is used to encrypt inner record events.

func KeyFromBytes added in v0.1.12

func KeyFromBytes(b []byte) (k Key, err error)

KeyFromBytes returns a key by wrapping k.

func KeyFromString added in v0.1.12

func KeyFromString(s string) (k Key, err error)

KeyFromString returns a key by decoding a base32-encoded string.

func NewKey added in v0.1.12

func NewKey(sk, rk *sym.Key) Key

NewKey wraps service and read keys.

func NewRandomKey added in v0.1.12

func NewRandomKey() Key

NewRandomKey returns a random key, which includes a service and read key.

func NewRandomServiceKey added in v0.1.12

func NewRandomServiceKey() Key

NewRandomServiceKey returns a random service-only key.

func NewServiceKey added in v0.1.12

func NewServiceKey(sk *sym.Key) Key

NewServiceKey wraps a service-only key.

func (Key) Bytes added in v0.1.12

func (k Key) Bytes() []byte

Bytes returns raw key bytes.

func (Key) CanRead added in v0.1.12

func (k Key) CanRead() bool

CanRead returns whether or not read key is available.

func (Key) Defined added in v0.1.12

func (k Key) Defined() bool

Defined returns whether or not key has any defined components. Since it's not possible to have a read key w/o a service key, we just need to check service key.

func (Key) MarshalBinary added in v0.1.12

func (k Key) MarshalBinary() ([]byte, error)

MarshalBinary implements BinaryMarshaler.

func (Key) Read added in v0.1.12

func (k Key) Read() *sym.Key

Read returns the read key.

func (Key) Service added in v0.1.12

func (k Key) Service() *sym.Key

Service returns the service key.

func (Key) String added in v0.1.12

func (k Key) String() string

String returns the base32-encoded string representation of raw key bytes. For example, Full: "brv7t5l2h55uklz5qwpntcat26csaasfchzof3emmdy6povabcd3a2to2qdkqdkto2prfhizerqqudqsdvwherbiy4nazqxjejgdr4oy" Service: "bp2vvqody5zm6yqycsnazb4kpqvycbdosos352zvpsorxce5koh7q"

type Libp2pIdentity added in v0.1.13

type Libp2pIdentity struct {

Libp2pIdentity wraps crypto.PrivKey, overwriting GetPublic with thread.PubKey.

func (*Libp2pIdentity) Decrypt added in v1.0.0

func (p *Libp2pIdentity) Decrypt(_ context.Context, data []byte) ([]byte, error)

func (*Libp2pIdentity) Equals added in v1.0.0

func (p *Libp2pIdentity) Equals(i Identity) bool

func (*Libp2pIdentity) GetPublic added in v0.1.13

func (p *Libp2pIdentity) GetPublic() PubKey

func (*Libp2pIdentity) MarshalBinary added in v1.0.0

func (p *Libp2pIdentity) MarshalBinary() ([]byte, error)

func (*Libp2pIdentity) Sign added in v0.1.13

func (p *Libp2pIdentity) Sign(_ context.Context, msg []byte) ([]byte, error)

func (*Libp2pIdentity) UnmarshalBinary added in v1.0.0

func (p *Libp2pIdentity) UnmarshalBinary(bytes []byte) (err error)

type Libp2pPubKey added in v0.1.13

type Libp2pPubKey struct {

Libp2pPubKey wraps crypto.PubKey.

func (*Libp2pPubKey) Encrypt added in v1.0.0

func (p *Libp2pPubKey) Encrypt(data []byte) ([]byte, error)

func (*Libp2pPubKey) Equals added in v1.0.0

func (p *Libp2pPubKey) Equals(k PubKey) bool

func (*Libp2pPubKey) MarshalBinary added in v0.1.13

func (p *Libp2pPubKey) MarshalBinary() ([]byte, error)

func (*Libp2pPubKey) String added in v0.1.13

func (p *Libp2pPubKey) String() string

func (*Libp2pPubKey) UnmarshalBinary added in v0.1.13

func (p *Libp2pPubKey) UnmarshalBinary(bytes []byte) (err error)

func (*Libp2pPubKey) UnmarshalString added in v0.1.13

func (p *Libp2pPubKey) UnmarshalString(str string) error

type LogInfo

type LogInfo struct {
	// ID is the log's identifier.
	ID peer.ID
	// PubKey is the log's public key.
	PubKey crypto.PubKey
	// PrivKey is the log's private key.
	PrivKey crypto.PrivKey
	// Addrs are the addresses associated with the given log.
	Addrs []ma.Multiaddr
	// Head is the log's current head.
	Head Head
	// Managed logs are any logs directly added/created by the host, and/or logs for which we have the private key
	Managed bool

LogInfo holds log keys, addresses, and heads.

type PubKey added in v0.1.13

type PubKey interface {

	// String encodes the public key into a base32 string.
	// UnmarshalString decodes the public key from a base32 string.
	UnmarshalString(string) error
	// Verify that 'sig' is the signed hash of 'data'
	Verify(data []byte, sig []byte) (bool, error)
	// Encrypt data with the public key.
	Encrypt([]byte) ([]byte, error)
	// Equals returns true if the keys are equal.
	Equals(PubKey) bool

Pubkey can be anything that provides a verify method.

func NewLibp2pPubKey added in v0.1.13

func NewLibp2pPubKey(key crypto.PubKey) PubKey

NewLibp2pPubKey returns a new PubKey.

type Token added in v0.1.13

type Token string

Token is a concrete type for a JWT token string, which provides a claim to an identity.

func NewToken added in v0.1.13

func NewToken(issuer crypto.PrivKey, key PubKey) (tok Token, err error)

NewToken issues a new JWT token from issuer for the given pubic key.

func NewTokenFromMD added in v0.1.13

func NewTokenFromMD(ctx context.Context) (tok Token, err error)

NewTokenFromMD returns Token from the given context, if present.

func TokenFromContext added in v0.1.13

func TokenFromContext(ctx context.Context) (Token, bool)

TokenFromContext returns Token from a context.

func (Token) Defined added in v0.1.15

func (t Token) Defined() bool

Defined returns true if token is not empty.

func (Token) PubKey added in v1.0.0

func (t Token) PubKey() (PubKey, error)

PubKey returns the public key encoded in the token. Note: This does NOT verify the token.

func (Token) Validate added in v0.1.15

func (t Token) Validate(issuer crypto.PrivKey) (PubKey, error)

Validate token against an issuer. If token is present and was issued by issuer (is valid), the embedded public key is returned. If token is not present, both the returned public key and error will be nil.

type Variant added in v0.1.7

type Variant uint64

Variant is a type for thread variants.

const (
	Raw              Variant = 0x55
	AccessControlled Variant = 0x70 // Supports access control lists


func (Variant) String added in v0.1.7

func (v Variant) String() string

Jump to

Keyboard shortcuts

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