util

package
v0.1.8-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2019 License: GPL-3.0 Imports: 29 Imported by: 24

Documentation

Index

Constants

View Source
const (
	// HashLength is the standard size of hash values
	HashLength = 32
)

Variables

View Source
var Big0 = new(big.Int).SetInt64(0)

Big0 represents a zero value big.Int

View Source
var EmptyBlockNonce = BlockNonce([8]byte{})

EmptyBlockNonce is a BlockNonce with no values

View Source
var EmptyHash = Hash([HashLength]byte{})

EmptyHash is an empty Hash

Functions

func Blake2b256

func Blake2b256(v []byte) []byte

Blake2b256 returns blake2b 256bit hash of v

func BytesToObject

func BytesToObject(bs []byte, dest interface{}) error

BytesToObject decodes bytes produced by BytesToObject to the given dest object

func DecodeNumber

func DecodeNumber(encNum []byte) uint64

DecodeNumber deserialize a number from BigEndian

func Decrypt

func Decrypt(ciphertext []byte, key []byte) ([]byte, error)

Decrypt decrypts a ciphertext

func EncodeForJS

func EncodeForJS(obj interface{}, fieldToIgnore ...string) interface{}

EncodeForJS takes a struct and converts selected types to values that are compatible in the JS environment. It returns a map and will panic if obj is not a map/struct. Set fieldToIgnore to ignore matching fields

func EncodeNumber

func EncodeNumber(n uint64) []byte

EncodeNumber serializes a number to BigEndian

func Encrypt

func Encrypt(plaintext []byte, key []byte) ([]byte, error)

Encrypt encrypts a plaintext

func FromHex

func FromHex(hexValue string) ([]byte, error)

FromHex decodes hex value to bytes. If hex value is prefixed with '0x' it is trimmed before the decode operation.

func GenerateKeyPair

func GenerateKeyPair(r io.Reader) (crypto.PrivKey, crypto.PubKey, error)

GenerateKeyPair generates private and public keys

func GetIPFromAddr

func GetIPFromAddr(addr string) net.IP

GetIPFromAddr get the IP4/6 ip of the address. Expects the caller to have validate the addr

func GetPtrAddr

func GetPtrAddr(ptrAddr interface{}) *big.Int

GetPtrAddr takes a pointer and returns the address

func HexToInt64

func HexToInt64(hexVal string) (int64, error)

HexToInt64 attempts to convert an hex string to Int64. Expects the hex string to begin with '0x'.

func HexToStr

func HexToStr(hexStr string) (string, error)

HexToStr decodes an hex string to string. Expects hexStr to begin with '0x'

func IDFromAddr

func IDFromAddr(addr ma.Multiaddr) peer.ID

IDFromAddr extracts and returns the peer ID

func IDFromAddrString

func IDFromAddrString(addr string) peer.ID

IDFromAddrString is like IDFromAddr but accepts a string. Returns empty string if addr is not a valid multiaddr. Expects the caller to have validated addr before calling the function.

func Int64ToHex

func Int64ToHex(intVal int64) string

Int64ToHex converts an Int64 value to hex string. The resulting hex is prefixed by '0x'

func IsDevAddr

func IsDevAddr(ip net.IP) bool

IsDevAddr returns whether or not the passed address is a local or private address.

func IsFileOk

func IsFileOk(path string) bool

IsFileOk checks if a path exists and it is a file

func IsPathOk

func IsPathOk(path string) bool

IsPathOk checks if a path exist and whether there are no permission errors

func IsRoutable

func IsRoutable(ip net.IP) bool

IsRoutable returns whether or not the passed address is routable over the public internet. This is true as long as the address is valid and is not in any reserved ranges.

func IsRoutableAddr

func IsRoutableAddr(addr string) bool

IsRoutableAddr checks if an addr is valid and routable

func IsValidAddr

func IsValidAddr(addr string) bool

IsValidAddr checks if an address is a valid multi address with ip4/ip6, tcp, and ipfs protocols

func IsValidConnectionString

func IsValidConnectionString(str string) bool

IsValidConnectionString checks whether a connection string is valid

func IsValidHostPortAddress

func IsValidHostPortAddress(address string) bool

IsValidHostPortAddress checks if an address is a valid address matching the format `host:port`

func LogAlert

func LogAlert(format string, args ...interface{})

LogAlert wraps pp.Println with a read [Alert] prefix.

func MapDecode

func MapDecode(m interface{}, rawVal interface{}) error

MapDecode decodes a map to a struct. It uses mapstructure.Decode internally but with 'json' TagName.

func MayDecodeNumber

func MayDecodeNumber(encNum []byte) (r uint64, err error)

MayDecodeNumber is like DecodeNumber but returns an error instead of panicking

func MustFromHex

func MustFromHex(hexValue string) []byte

MustFromHex is like FromHex except it panics if an error occurs

func NonZeroOrDefIn64

func NonZeroOrDefIn64(v int64, def int64) int64

NonZeroOrDefIn64 checks if v is 0 so it returns def, otherwise returns v

func ObjectToBytes

func ObjectToBytes(s interface{}) []byte

ObjectToBytes returns msgpack encoded representation of an object

func ParseAddr

func ParseAddr(addr string) map[string]string

ParseAddr returns the protocol and value present in a multiaddr. Expects the caller to have validate the address before calling the function.

func RandBytes

func RandBytes(n int) []byte

RandBytes gets random string of fixed length

func RandString

func RandString(n int) string

RandString is like RandBytes but returns string

func SerializeMsg

func SerializeMsg(o interface{}) []byte

SerializeMsg serializes an object using msgpack. Panics if an error is encountered

func ShortID

func ShortID(id peer.ID) string

ShortID returns the short version an ID

func StrToDec

func StrToDec(val string) decimal.Decimal

StrToDec converts a numeric string to decimal. Panics if val could not be converted to decimal.

func StrToHex

func StrToHex(str string) string

StrToHex converts a string to hex. T The resulting hex is prefixed by '0x'

func StructToMap

func StructToMap(s interface{}) map[string]interface{}

StructToMap returns a map containing fields from the s. Map fields are named after their json tags on the struct

func ToHex

func ToHex(value []byte) string

ToHex encodes value to hex with a '0x' prefix

func ValidateAndResolveConnString

func ValidateAndResolveConnString(connStr string) (string, error)

ValidateAndResolveConnString validates a connection string and attempts to resolve the address of a connection string to an IP if it is a domain name.

Types

type BlockNonce

type BlockNonce [8]byte

BlockNonce is a 64-bit hash which proves (combined with the mix-hash) that a sufficient amount of computation has been carried out on a block.

func EncodeNonce

func EncodeNonce(i uint64) BlockNonce

EncodeNonce converts the given integer to a block nonce.

func (BlockNonce) MarshalText

func (n BlockNonce) MarshalText() string

MarshalText encodes n as a hex string with 0x prefix.

func (BlockNonce) Uint64

func (n BlockNonce) Uint64() uint64

Uint64 returns the integer value of a block nonce.

type ConnStringData

type ConnStringData struct {
	ID      string
	Address string
	Port    string
}

ConnStringData represents

func ParseConnString

func ParseConnString(str string) *ConnStringData

ParseConnString breaksdown a connection string

func (*ConnStringData) ConnString

func (cs *ConnStringData) ConnString() string

ConnString returns a valid connection string

type Hash

type Hash [HashLength]byte

Hash represents a hash value

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash copies b to a Hash

func HexToHash

func HexToHash(hex string) (Hash, error)

HexToHash creates an Hash from hex string

func StrToHash

func StrToHash(s string) Hash

StrToHash converts a string to a Hash

func (Hash) Big

func (h Hash) Big() *big.Int

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes gets the byte representation of the underlying hash.

func (Hash) Equal

func (h Hash) Equal(o Hash) bool

Equal checks equality between h and o

func (Hash) Hex

func (h Hash) Hex() []byte

Hex is like HexStr but returns bytes

func (Hash) HexStr

func (h Hash) HexStr() string

HexStr returns the hex string version of the hash beginning with 0x

func (Hash) IsEmpty

func (h Hash) IsEmpty() bool

IsEmpty checks whether the hash is empty (having zero values)

func (Hash) SS

func (h Hash) SS() string

SS returns a short version of HexStr with the middle characters truncated when length is at least 32

type NodeAddr

type NodeAddr string

NodeAddr represents address that points to a node on a network. The address are represented as Multiaddr.

func AddressFromConnString

func AddressFromConnString(str string) NodeAddr

AddressFromConnString creates a NodeAddress from a given connection string

func AddressFromHost

func AddressFromHost(host host.Host) NodeAddr

AddressFromHost gets address of an host

func RemoteAddrFromConn

func RemoteAddrFromConn(c inet.Conn) NodeAddr

RemoteAddrFromConn gets the remote address from the given connection

func RemoteAddrFromStream

func RemoteAddrFromStream(s inet.Stream) NodeAddr

RemoteAddrFromStream gets the remote address from the given stream

func (NodeAddr) ConnectionString

func (a NodeAddr) ConnectionString() string

ConnectionString returns an address similar to database connection string with a branded schema

func (NodeAddr) DecapIPFS

func (a NodeAddr) DecapIPFS() ma.Multiaddr

DecapIPFS gets the address without the IPFS part

func (NodeAddr) Equal

func (a NodeAddr) Equal(b NodeAddr) bool

Equal checks whether a and b match

func (NodeAddr) GetMultiaddr

func (a NodeAddr) GetMultiaddr() ma.Multiaddr

GetMultiaddr gets the address. It will panic if the address is not a valid Multiaddr

func (NodeAddr) ID

func (a NodeAddr) ID() peer.ID

ID gets the address unique ID

func (NodeAddr) IP

func (a NodeAddr) IP() net.IP

IP gets the IP from the address

func (NodeAddr) IsRoutable

func (a NodeAddr) IsRoutable() bool

IsRoutable checks whether the address is routable

func (NodeAddr) IsValid

func (a NodeAddr) IsValid() bool

IsValid checks whether the address is valid Multiaddr

func (NodeAddr) String

func (a NodeAddr) String() string

func (NodeAddr) StringID

func (a NodeAddr) StringID() string

StringID the address unique ID as a string

type String

type String string

String represents a custom string

func (String) Bytes

func (s String) Bytes() []byte

Bytes returns the bytes equivalent of the string

func (String) Decimal

func (s String) Decimal() decimal.Decimal

Decimal returns the decimal representation of the string. Panics if string failed to be converted to decimal.

func (String) Equal

func (s String) Equal(o String) bool

Equal check whether s and o are the same

func (String) IsDecimal

func (s String) IsDecimal() bool

IsDecimal checks whether the string can be converted to decimal

func (String) SS

func (s String) SS() string

SS returns a short version of String() with the middle characters truncated when length is at least 32

func (String) String

func (s String) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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