util

package
v7.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ED25519PubKeyMultiCodec   = 0xed
	SECP256k1PubKeyMultiCodec = 0xe7
)

Variables

View Source
var ErrInvalidCharset = errors.New("invalid charset")
View Source
var ErrInvalidHashAlg = fmt.Errorf("not a valid HashAlg, try [%s]", strings.Join(_HashAlgNames, ", "))
View Source
var ErrInvalidKeyAlg = fmt.Errorf("not a valid KeyAlg, try [%s]", strings.Join(_KeyAlgNames, ", "))
View Source
var ErrInvalidSize = func(size int) error {
	return fmt.Errorf("invalid buffer size %d", size)
}

Functions

func BytesToPubKey

func BytesToPubKey(bz []byte, keytype KeyAlg) (cryptotypes.PubKey, error)

BytesToPubKey converts bytes to a PubKey given a key type. Supported key types: secp256k1, ed25519.

func CreateDIDKeyByPubKey

func CreateDIDKeyByPubKey(pubKey cryptotypes.PubKey) (string, error)

CreateDIDKeyByPubKey creates a did:key ID using the given public key. The multicodec key fingerprint is determined by the key type and complies with the did:key format spec found at: https://w3c-ccg.github.io/did-method-key/#format.

func CreateDIDKeyIDByPubKey

func CreateDIDKeyIDByPubKey(pubKey cryptotypes.PubKey) (string, error)

CreateDIDKeyIDByPubKey creates a DID key ID using the given public key. The function is similar to CreateDIDKeyByPubKey but returns the DID with the key ID as hash fragment. The multicodec key fingerprint is determined by the key type and complies with the did:key format spec found at: https://w3c-ccg.github.io/did-method-key/#format.

func Decode

func Decode(bs []byte, label string) (string, error)

Decode converts a byte slice from a specified encoding to a string. Decode function is the reverse of encode function.

func DerefOrDefault

func DerefOrDefault[T any](ptr *T, defaultValue T) T

DerefOrDefault returns the value of the pointer if it is not nil, otherwise returns the default value.

func Encode

func Encode(str string, label string) ([]byte, error)

Encode converts a string to a slice of bytes in a specified encoding.

func Hash

func Hash(alg HashAlg, bytes []byte) ([]byte, error)

Hash hashes the given data using the given algorithm.

func HashAlgNames

func HashAlgNames() []string

HashAlgNames returns a list of possible string values of HashAlg.

func Indexed

func Indexed[T any, U any](f func(t T) U) func(T, int) U

Indexed returns a function that applies the given function to the given item and returns the result. It's a convenience function to be used with lo.Map which transforms a predicate function into a mapper function by adding an index argument (which is ignored).

func IsNil

func IsNil(t any) bool

IsNil returns true if the given value is nil, false otherwise.

func KeyAlgNames

func KeyAlgNames() []string

KeyAlgNames returns a list of possible string values of KeyAlg.

func Map

func Map[T, M any](s []T, f func(T) M) []M

Map applies the given function to each element of the given slice and returns a new slice with the results.

func NonZeroOrDefault

func NonZeroOrDefault[T any](v, defaultValue T) T

NonZeroOrDefault returns the value of the argument if it is not nil and not zero, otherwise returns the default value.

func ParseURLMust

func ParseURLMust(s string) *url.URL

ParseURLMust parses the given url and panics if it fails. You have been warned.

func QueryInterpreter

func QueryInterpreter(
	ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint,
) (*types.Answer, error)

QueryInterpreter interprets a query and returns the solutions up to the given limit.

func URLMatches

func URLMatches(this *url.URL) func(*url.URL) bool

URLMatches is a function that returns a function that matches the given url against the given other item.

The function matches the components of the given url against the components of the given other url. If the component of the given other url is empty, it is considered to match the component of the given url. For example:

The function is curried, and is a binary relation that is reflexive, associative (but not commutative).

func VerifySignature

func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err error)

VerifySignature verifies the signature of the given message with the given public key using the given algorithm.

func WhitelistBlacklistMatches

func WhitelistBlacklistMatches[T any](whitelist []T, blacklist []T, predicate func(item T) func(b T) bool) func(T) bool

WhitelistBlacklistMatches returns a function that matches the given item according to the given whitelist and blacklist returning true if the item matches the whitelist and does not match the blacklist, and false otherwise. Note that if the whitelist is empty, the item is considered to match the whitelist.

Types

type BoundedBuffer

type BoundedBuffer struct {
	// contains filtered or unexported fields
}

BoundedBuffer is a fixed size buffer that overwrites older data when the buffer is full.

func NewBoundedBuffer

func NewBoundedBuffer(size int) (*BoundedBuffer, error)

NewBoundedBuffer creates a new BoundedBuffer with the given fixed size. If size is 0, the buffer will be disabled.

func NewBoundedBufferMust

func NewBoundedBufferMust(size int) *BoundedBuffer

NewBoundedBufferMust is like NewBoundedBuffer but panics if an error occurs.

func (*BoundedBuffer) String

func (b *BoundedBuffer) String() string

String returns the contents of the buffer as a string.

func (*BoundedBuffer) Write

func (b *BoundedBuffer) Write(p []byte) (n int, err error)

Write implements io.Writer.

type HashAlg

type HashAlg int

HashAlg is the type of hash algorithm supported by the crypto util functions. ENUM(md5,sha256,sha512).

const (
	// HashAlgMd5 is a HashAlg of type Md5.
	HashAlgMd5 HashAlg = iota
	// HashAlgSha256 is a HashAlg of type Sha256.
	HashAlgSha256
	// HashAlgSha512 is a HashAlg of type Sha512.
	HashAlgSha512
)

func ParseHashAlg

func ParseHashAlg(name string) (HashAlg, error)

ParseHashAlg attempts to convert a string to a HashAlg.

func (HashAlg) Hasher

func (a HashAlg) Hasher() (hash.Hash, error)

Hasher returns a new hash.Hash for the given algorithm.

func (HashAlg) IsValid

func (x HashAlg) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (HashAlg) String

func (x HashAlg) String() string

String implements the Stringer interface.

type KeyAlg

type KeyAlg int

KeyAlg is the type of key algorithm supported by the crypto util functions. ENUM(secp256k1,secp256r1,ed25519).

const (
	// KeyAlgSecp256k1 is a KeyAlg of type Secp256k1.
	KeyAlgSecp256k1 KeyAlg = iota
	// KeyAlgSecp256r1 is a KeyAlg of type Secp256r1.
	KeyAlgSecp256r1
	// KeyAlgEd25519 is a KeyAlg of type Ed25519.
	KeyAlgEd25519
)

func ParseKeyAlg

func ParseKeyAlg(name string) (KeyAlg, error)

ParseKeyAlg attempts to convert a string to a KeyAlg.

func (KeyAlg) IsValid

func (x KeyAlg) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (KeyAlg) String

func (x KeyAlg) String() string

String implements the Stringer interface.

type URIComponent

type URIComponent int
const (
	PathComponent URIComponent = iota
	SegmentComponent
	QueryValueComponent
	FragmentComponent
)

func (URIComponent) Escape

func (c URIComponent) Escape(v string) string

Escape return the given input string by adding percent encoding depending on the current component where it's supposed to be put. This is a re-implementation of native url.escape. See shouldEscape() comment's for more details.

func (URIComponent) Unescape

func (c URIComponent) Unescape(v string) (string, error)

Jump to

Keyboard shortcuts

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