keystore

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package keystore provides key creation and management functionalities.

Index

Constants

View Source
const (
	DefaultPassphrase = "passphrase"
)

Variables

View Source
var (
	Version = "0.0.1"
)

Functions

This section is empty.

Types

type AskPassOnceFunc

type AskPassOnceFunc func(prompt ...string) (string, error)

type Keystore

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

Keystore implements Keystore. It provides the ability to create, update, fetch and import keys and accounts.

func New

func New(dir string) *Keystore

New creates an instance of Keystore. dir is where encrypted key files are stored. EXPECTS: - dir to have been created

func (*Keystore) AskForPassword

func (ks *Keystore) AskForPassword(prompt ...string) (string, error)

AskForPassword starts an interactive prompt to collect passphrase. Returns error if passphrase and repeated passphrases do not match

func (*Keystore) AskForPasswordOnce

func (ks *Keystore) AskForPasswordOnce(prompt ...string) (string, error)

AskForPasswordOnce is like askForPassword but it does not ask to confirm passphrase.

func (*Keystore) CreateCmd

func (ks *Keystore) CreateCmd(
	keyType types.KeyType,
	seed int64,
	passphrase string,
	nopass bool) (*ed25519.Key, error)

CreateCmd creates a new key in the keystore. It will prompt the user to obtain encryption passphrase if one is not provided. If seed is non-zero, it is used as the seed for key generation, otherwise, one will be randomly generated. If passphrase is a file path, the file is read and its content is used as the passphrase. If nopass is true, the default encryption passphrase is used and the key will be marked 'unprotected'

func (*Keystore) CreateKey

func (ks *Keystore) CreateKey(key *ed25519.Key, keyType types.KeyType, passphrase string) error

CreateKey creates a new key

func (*Keystore) Exist

func (ks *Keystore) Exist(address string) (bool, error)

Exist checks if a key that matches the given address exists

func (*Keystore) GetByAddress

func (ks *Keystore) GetByAddress(addr string) (types2.StoredKey, error)

GetByAddress finds a key where its user address or push key address matches addr

func (*Keystore) GetByIndex

func (ks *Keystore) GetByIndex(i int) (types2.StoredKey, error)

GetByIndex returns a key by its current position in the list of accounts which is ordered by the time of creation.

func (*Keystore) GetByIndexOrAddress

func (ks *Keystore) GetByIndexOrAddress(idxOrAddr string) (types2.StoredKey, error)

GetByIndexOrAddress gets a key by either its address or index

func (*Keystore) GetCmd

func (ks *Keystore) GetCmd(addrOrIdx, pass string, showPrivKey bool) error

GetCmd decrypts a key and prints all information about the key. If pass is provide and it is not a file path, it is used as the passphrase. Otherwise, the file content is used as the passphrase. When pass is not set, the user is prompted to provide their passphrase. If showPrivKey is true, the private key will be printed out.

func (*Keystore) ImportCmd

func (ks *Keystore) ImportCmd(keyfile string, keyType types.KeyType, pass string) error

ImportCmd creates a new key from a private key stored in a file. If pass is provide and it is not a file path, it is used as the passphrase. Otherwise, the file is read and used as the passphrase. When pass is not set, the user is prompted to provide the passphrase.

func (*Keystore) List

func (ks *Keystore) List() (accounts []types.StoredKey, err error)

List returns the accounts stored on disk.

func (*Keystore) ListCmd

func (ks *Keystore) ListCmd(out io.Writer) error

ListCmd fetches and lists all accounts

func (*Keystore) SetOutput

func (ks *Keystore) SetOutput(out io.Writer)

SetOutput sets the output writer

func (*Keystore) UnlockKeyUI

func (ks *Keystore) UnlockKeyUI(addressOrIndex, passphrase, promptMsg string) (types.StoredKey, string, error)

UnlockKeyUI unlocks a target key by rendering prompt to collect a passphrase. addressOrIndex: The address or index of the key. passphrase: The user supplied passphrase. If not provided, an interactive session will be started to collect the passphrase Onsuccess, it returns the unlocked key and the passphrase used to unlock it.

func (*Keystore) UpdateCmd

func (ks *Keystore) UpdateCmd(addressOrIndex, passphrase string) error

UpdateCmd fetches and lists all accounts

type StoredKey

type StoredKey struct {

	// Type indicates the key type
	Type types2.KeyType

	// Address is the user address encoding of the key
	UserAddress string

	// PushAddress is the push address encoding of the key
	PushAddress string

	// Cipher includes the encryption data
	Cipher []byte

	// Data contains the decrypted data.
	// Only available after account is unlocked.
	Data []byte

	// CreatedAt represents the time the key was created and stored on disk
	CreatedAt time.Time

	// Unprotected indicates that the key is encrypted with a default passphrase.
	// An unprotected key is equivalent to a key that has no passphrase.
	Unprotected bool

	// The filename of the key file
	Filename string
	// contains filtered or unexported fields
}

StoredKey represents an encrypted key stored on disk

func (*StoredKey) GetCreatedAt

func (sk *StoredKey) GetCreatedAt() time.Time

GetCreatedAt returns the locked data. Only available when key is unlocked.

func (*StoredKey) GetFilename

func (sk *StoredKey) GetFilename() string

GetFilename returns the filename of the key file

func (*StoredKey) GetKey

func (sk *StoredKey) GetKey() *ed25519.Key

GetKey returns the underlying Ed25519 key. Unlock() must be called first.

func (*StoredKey) GetMeta

func (sk *StoredKey) GetMeta() types2.StoredKeyMeta

GetMeta returns the meta information of the key

func (*StoredKey) GetPayload

func (sk *StoredKey) GetPayload() *types2.KeyPayload

GetKey returns the key object that is serialized and persisted.

func (*StoredKey) GetPushKeyAddress

func (sk *StoredKey) GetPushKeyAddress() string

GetPushKeyAddress returns the push address of the key

func (*StoredKey) GetType

func (sk *StoredKey) GetType() types2.KeyType

GetType returns the key type

func (*StoredKey) GetUnlockedData

func (sk *StoredKey) GetUnlockedData() []byte

GetUnlockedData returns the locked data. Only available when key is unlocked.

func (*StoredKey) GetUserAddress

func (sk *StoredKey) GetUserAddress() string

GetUserAddress returns the address of the key

func (*StoredKey) IsUnprotected

func (sk *StoredKey) IsUnprotected() bool

IsUnprotected checks whether the key is encrypted using the default passphrase

func (*StoredKey) Unlock

func (sk *StoredKey) Unlock(passphrase string) error

Unlock decrypts the key using the given passphrase.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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