util

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2020 License: Apache-2.0 Imports: 26 Imported by: 8

Documentation

Overview

Copyright 2020 PhysarumSM Development Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Copyright 2020 PhysarumSM Development Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

View Source
const (
	// libp2p's PSK definition is a slice of 32 bytes
	PSK_NUM_BYTES = 32

	ENV_KEY_PSK = "P2P_PSK"
)
View Source
const ENV_KEY_BOOTSTRAPS = "P2P_BOOTSTRAPS"
View Source
const (
	RSA_MIN_BITS = 2048
)

Variables

This section is empty.

Functions

func AddBootstrapFlags

func AddBootstrapFlags() (*[]multiaddr.Multiaddr, error)

Returns address to a slice of strings that will store the bootstrap multiaddresses once flag.Parse() is called (prior to that, it will be an empty slice).

func AddPSKFlag

func AddPSKFlag() (*pnet.PSK, error)

Sets the "-psk" flag and returns a pointer to a pre-shared key

func CreateOrLoadKey

func CreateOrLoadKey(kf KeyFlags) (crypto.PrivKey, error)

func CreatePSK

func CreatePSK(psk string) (pnet.PSK, error)

Uses SHA256 to hash an input string into a 256-bit value. This matches libp2p's current implementation for PSK, which seems to be hard-coded to be 32 Bytes (256 bits) long.

If the input string is the zero-value (i.e. ""), then this function call is equivalent to calling CreateRandPSK(), which generates a random pre-shared key.

func CreateRandPSK

func CreateRandPSK() (pnet.PSK, error)

Generates a random PSK

func ExpandTilde

func ExpandTilde(path string) (string, error)

Expands tilde to absolute path Currently only works if path begins with tilde, not somewhere in the middle

func FileExists

func FileExists(filePath string) bool

func GeneratePrivKey

func GeneratePrivKey(algo string, bits int) (crypto.PrivKey, error)

func GetBootstrapPointer

func GetBootstrapPointer() *bootstrapAddrs

Don't like this... feels hacky to have a function just for tests in the main package This is needed to return a pointer to type bootstrapAddrs, a hidden type. This enables tests for the Set() and String() functions above.

func GetEnvBootstraps

func GetEnvBootstraps() ([]multiaddr.Multiaddr, error)

If the environment variable does not exist, or if there are errors during parsing, return the 0-value of the return type.

func GetEnvPSK

func GetEnvPSK() (pnet.PSK, error)

If the environment variable does not exist, or if there are errors during parsing, return the 0-value of the return type.

func GetEnvPSKString

func GetEnvPSKString() string

Returns un-hashed PSK passphrase specified in environment variable

func GetFlagPSKString

func GetFlagPSKString() string

Returns the un-hashed PSK passphrase of the PSK from the last time Get() was called.

func GetFreePort

func GetFreePort() (int, error)

Get free port on machine

func GetIPAddress

func GetIPAddress() (string, error)

Get preferred outbound ip on machine

func GetPSKPointer

func GetPSKPointer() *pskValue

For enabling tests, ideally should not be used. This is needed to return a pointer to type pskValue, a hidden type. This enables tests for the Set() and String() functions above.

func IpfsHashBytes

func IpfsHashBytes(data []byte) (hash string, err error)

func IpfsHashFile

func IpfsHashFile(path string) (hash string, err error)

func LoadPrivKeyFromFile

func LoadPrivKeyFromFile(keyFile string) (crypto.PrivKey, error)

Inverse of storePrivKeyToFile()

func StorePrivKeyToFile

func StorePrivKeyToFile(priv crypto.PrivKey, keyFile string) error

Write private key to file in Base 64 format Store the key type ID followed by a space, then the key, then a new-line

func StringsToMultiaddrs

func StringsToMultiaddrs(stringMultiaddrs []string) ([]multiaddr.Multiaddr, error)

Helper function to cast a slice of strings into a slice of Multiaddrs

func Whoami

func Whoami(node host.Host) ([]multiaddr.Multiaddr, error)

Return identifying multiaddrs of the given node

Types

type ExpoBackoff

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

func NewExpoBackoff

func NewExpoBackoff(init, max time.Duration) (*ExpoBackoff, error)

Creates a new ExpoBackoff. Parameters 'init' and 'max' denote initial and max durations to sleep whenever the Sleep() method is called.

func (*ExpoBackoff) Sleep

func (eb *ExpoBackoff) Sleep()

Sleeps for some duration, where each invocation of this method will exponentially increasing the duration.

type ExpoBackoffAttempts

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

func NewExpoBackoffAttempts

func NewExpoBackoffAttempts(init, max time.Duration,
	attempts int) (*ExpoBackoffAttempts, error)

Creates a new ExpoBackoffAttempts Similar to ExpoBackoff, but limited in the number of times it can sleep See example usage in comments for the Attempt() method

func (*ExpoBackoffAttempts) Attempt

func (eba *ExpoBackoffAttempts) Attempt() bool

Wrapper function around ExponentialBackoff's Sleep(). Allows users to write for loops such as:

eba := NewExpoBackoffAttempts(1 * time.Second, 8 * time.Second, 6)
for eba.Attempt() {
    // DO SOMETHING
}

The above loop will execute 6 times, and sleep 5 times (1, 2, 4, 8, and 8 seconds) between loops. Note that the first call will not sleep.

Returns

  • False: If max attempts has been reached
  • True: If max attempts has not been reached, will return true after potentially sleeping. Note that the first call will not sleep.

type KeyFlags

type KeyFlags struct {
	Algo      *string
	Bits      *int
	Keyfile   *string
	Ephemeral *bool
}

func AddKeyFlags

func AddKeyFlags(defaultKeyFile string) (KeyFlags, error)

Adds CLI arguments for key-related flags. Does not call Parse() on its own.

Takes a single parameter, the default key filename. This allows programs to define different default key filenames, so that if two different programs are run on the same host, they won't read from the same key.

Returns a struct containing pointers to the various variables that will hold the parsed flag values once Parse() is called.

Jump to

Keyboard shortcuts

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