util

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: ISC Imports: 41 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TokenTypeGit represents a token from the politeiad git backend.
	TokenTypeGit = "git"

	// TokenTypeTstore represents a token from the politeiad tstore
	// backend.
	TokenTypeTstore = "tstore"
)
View Source
var ErrorStatuses = map[ErrorStatusT]string{
	ErrorStatusInvalid:          "signature error invalid",
	ErrorStatusPublicKeyInvalid: "public key invalid",
	ErrorStatusSignatureInvalid: "signature invalid",
}

ErrorStatuses contains the human readable signature error messages.

Functions

func Base64File

func Base64File(filename string) (string, error)

Base64File returns the base64 content of a file.

func CleanAndExpandPath

func CleanAndExpandPath(path string) string

CleanAndExpandPath expands environment variables and leading ~ in the passed path, cleans the result, and returns it.

func ConvertBodyToByteArray

func ConvertBodyToByteArray(r io.Reader, print bool) []byte

ConvertBodyToByteArray converts a response body into a byte array and optionally prints it to stdout.

func ConvertDigest

func ConvertDigest(d string) ([sha256.Size]byte, bool)

ConvertDigest converts a string into a digest.

func ConvertSignature

func ConvertSignature(s string) ([identity.SignatureSize]byte, error)

ConvertSignature converts a hex encoded signature to a proper sized byte slice.

func ConvertStringToken

func ConvertStringToken(token string) ([]byte, error)

ConvertStringToken verifies and converts a string token to a proper sized byte slice. This function accepts both the full length token and token prefixes.

func DcrStringToAtoms added in v1.3.0

func DcrStringToAtoms(dcrstr string) (uint64, error)

DcrStringToAtoms converts a DCR amount as a string into a uint64 representing atoms. Supported input variations: "1", ".1", "0.1".

func DeriveChildAddress added in v1.3.0

func DeriveChildAddress(params *chaincfg.Params, xpub string, index uint32) (string, error)

DeriveChildAddress derives a child address using the provided xpub and index.

func Digest

func Digest(b []byte) []byte

Digest returns the SHA256 of a byte slice.

func DigestFile

func DigestFile(filename string) (string, error)

DigestFile returns the SHA256 of a file.

func DigestFileBytes

func DigestFileBytes(filename string) ([]byte, error)

DigestFileBytes returns the SHA256 of a file.

func FQDN

func FQDN() string

FQDN returns the fully qualified domain name of the machine it is running on.

func FileExists

func FileExists(name string) bool

FilesExists reports whether the named file or directory exists.

func FormatJSON added in v1.0.0

func FormatJSON(v interface{}) string

FormatJSON returns a pretty printed JSON string for the provided structure.

func GenCertPair

func GenCertPair(curve elliptic.Curve, org, certFile, keyFile string) error

GenCertPair generates a key/cert pair to the paths provided.

func GetErrorFromJSON

func GetErrorFromJSON(r io.Reader) (interface{}, error)

GetErrorFromJSON returns the error that is embedded in a JSON reply.

func IsDigest

func IsDigest(digest string) bool

IsDigest determines if a string is a valid SHA256 digest.

func LoadEncryptionKey added in v1.0.0

func LoadEncryptionKey(log slog.Logger, keyFile string) (*[32]byte, error)

LoadEncryptionKey loads the encryption key at the provided file path. If a key does not exists at the file path then a new secretbox key is created and saved to the file path before returning the key.

func LoadFile

func LoadFile(filename string) (mimeType string, digest string, payload string, err error)

LoadFile loads a file of disk and returns the MIME type, the sha256 digest and the payload encoded as base64. If any of the intermediary operations fail the function will return an error instead.

func LoadFile2

func LoadFile2(filename string) (string, []byte, error)

LoadFile2 returns a file and its mime type.

func MerkleRoot added in v1.0.0

func MerkleRoot(digests []string) (*[sha256.Size]byte, error)

MerkleRoot computes and returns the merkle root of the provided digests. The digests should be hex encoded SHA256 digests.

func MimeFile

func MimeFile(filename string) (string, error)

MimeFile returns the MIME type of a file.

func NewHTTPClient added in v1.0.0

func NewHTTPClient(skipVerify bool, certPath string) (*http.Client, error)

NewHTTPClient returns a new http Client.

func NormalizeAddress

func NormalizeAddress(addr, defaultPort string) string

NormalizeAddress returns addr with the passed default port appended if there is not already a port specified.

func ParseGetParams

func ParseGetParams(r *http.Request, dst interface{}) error

ParseGetParams parses the query params from the GET request into a struct. This method requires the struct type to be defined with `schema` tags.

func Random

func Random(n int) ([]byte, error)

Random returns a variable number of bytes of random data.

func RandomUint64

func RandomUint64() (uint64, error)

RandomInt returns a random unsigned integer.

func Regexp added in v1.0.0

func Regexp(supportedChars []string, lengthMin, lengthMax uint64) (*regexp.Regexp, error)

Regexp returns a compiled Regexp for the provided parameters.

func RemoteAddr added in v1.0.0

func RemoteAddr(r *http.Request) string

RemoteAddr returns a string of the remote address, i.e. the address that sent the request.

func RespBody added in v1.0.0

func RespBody(r *http.Response) []byte

RespBody returns the response body as a byte slice.

func RespondRaw added in v0.2.0

func RespondRaw(w http.ResponseWriter, code int, payload []byte)

func RespondWithError

func RespondWithError(w http.ResponseWriter, code int, message string)

func RespondWithJSON

func RespondWithJSON(w http.ResponseWriter, code int, payload interface{})

func ShortToken added in v1.0.0

func ShortToken(token []byte) ([]byte, error)

ShortToken returns the short version of a token.

func ShortTokenEncode added in v1.0.0

func ShortTokenEncode(token []byte) (string, error)

ShortTokenEncode returns the hex encoded shortened token.

func ShortTokenSize added in v1.0.0

func ShortTokenSize() int

ShortTokenSize returns the size, in bytes, of a politeiad short token.

func ShortTokenString added in v1.0.0

func ShortTokenString(token string) (string, error)

ShortTokenString takes a hex encoded token and returns the shortened token for it.

func StackTrace added in v1.1.0

func StackTrace(err error) (string, bool)

StackTrace returns the stack trace for a pkg/errors error. The returned bool indicates whether the provided error is a pkg/errors error. Stack traces are not available for stdlib errors.

func TokenDecode added in v1.0.0

func TokenDecode(tokenType, token string) ([]byte, error)

TokenDecode decodes a full length token. An error is returned if the token is not a full length, hex token.

func TokenDecodeAnyLength added in v1.0.0

func TokenDecodeAnyLength(tokenType, token string) ([]byte, error)

TokenDecodeAnyLength decodes both short tokens and full length tokens.

func TokenEncode added in v1.0.0

func TokenEncode(token []byte) string

TokenEncode returns the hex encoded token. Its possible that padding has been added to the token when it was originally decode in order to make it valid hex. This function checks for padding and removes it before encoding the token.

func TokenIsFullLength added in v1.0.0

func TokenIsFullLength(tokenType string, token []byte) bool

TokenIsFullLength returns whether a token is a valid, full length politeiad censorship token.

func TokenRegexp added in v1.0.0

func TokenRegexp() string

TokenRegexp returns the string regexp that is used to match tokens.

func TokenToPrefix added in v0.2.0

func TokenToPrefix(token string) string

TokenToPrefix returns a substring a token of length pd.TokenPrefixLength, or the token itself, whichever is shorter.

func TokensToPrefixes added in v0.2.0

func TokensToPrefixes(tokens []string) []string

TokensToPrefixes calls TokenToPrefix on a slice of tokens.

func VerifyChallenge

func VerifyChallenge(id *identity.PublicIdentity, challenge []byte, signature string) error

VerifyChallenge checks that the signature returned from politeiad is the challenge signed with the given identity.

func VerifyMessage added in v1.0.0

func VerifyMessage(address, message, signature string, net *chaincfg.Params) (bool, error)

VerifyMessage verifies a message that was signed using a decred P2PKH address.

Copied from: github.com/decred/dcrd/blob/0fc55252f912756c23e641839b1001c21442c38a/rpcserver.go#L5605

func VerifySignature added in v1.0.0

func VerifySignature(signature, pubKey, msg string) error

VerifySignature verifies a hex encoded Ed25519 signature.

func Zero

func Zero(in []byte)

Zero zeros out a byte slice.

Types

type Argon2Params added in v1.0.0

type Argon2Params struct {
	Time    uint32 `json:"time"`
	Memory  uint32 `json:"memory"`
	Threads uint8  `json:"threads"`
	KeyLen  uint32 `json:"keylen"`
	Salt    []byte `json:"salt"`
}

Argon2Params represent the argon2 key derivation parameters that are used to derive various keys in politeia.

func NewArgon2Params added in v1.0.0

func NewArgon2Params() Argon2Params

NewArgon2Params returns a new Argon2Params with default values.

type ErrorStatusT added in v1.0.0

type ErrorStatusT int

ErrorStatusT represents an error that occurred during signature validation.

const (
	// ErrorStatusInvalid is an invalid error status.
	ErrorStatusInvalid ErrorStatusT = 0

	// ErrorStatusPublicKeyInvalid is returned when a public key is not
	// a hex encoded ed25519 public key.
	ErrorStatusPublicKeyInvalid ErrorStatusT = 1

	// ErrorStatusSignatureInvalid is returned when a signature is
	// either not a valid hex encoded ed25519 signature or the
	// signature is wrong for the provided public key and message.
	ErrorStatusSignatureInvalid ErrorStatusT = 2
)

type SignatureError added in v1.0.0

type SignatureError struct {
	ErrorCode    ErrorStatusT
	ErrorContext string
}

SignatureError represents an error that was caused while verifying a signature.

func (SignatureError) Error added in v1.0.0

func (e SignatureError) Error() string

Error satisfies the error interface.

Directories

Path Synopsis
Package version provides a single location to house the version information for dcrd and other utilities provided in the same repository.
Package version provides a single location to house the version information for dcrd and other utilities provided in the same repository.

Jump to

Keyboard shortcuts

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