keystore

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotTerminal is returned when stdin is not a terminal
	ErrNotTerminal = errors.New("stdin is not a terminal, cannot read password securely")

	// ErrPasswordMismatch is returned when password confirmation does not match
	ErrPasswordMismatch = errors.New("passwords do not match")

	// ErrEmptyPassword is returned when password is empty
	ErrEmptyPassword = errors.New("password cannot be empty")

	// ErrContextCanceled is returned when context is canceled during password reading
	ErrContextCanceled = errors.New("password reading canceled by context")
)

Functions

func ChangePassword

func ChangePassword(keystorePath string, currentPassword, newPassword []byte) error

ChangePassword changes the password of an existing keystore file.

Parameters:

  • keystorePath: Path to the keystore file
  • currentPassword: Current password
  • newPassword: New password

Returns:

  • error: Any error that occurred

func CreateKeystore

func CreateKeystore(dir string, password []byte) (string, string, error)

CreateKeystore creates a new keystore with a randomly generated key.

Parameters:

  • dir: Directory to store the keystore file (will be created if not exists)
  • password: Password to encrypt the keystore

Returns:

  • address: The Ethereum address of the new key (0x prefixed)
  • path: Full path to the created keystore file
  • error: Any error that occurred

func GetKeystoreAddress

func GetKeystoreAddress(keystorePath string) (string, error)

GetKeystoreAddress reads a keystore file and returns the address without decrypting.

Parameters:

  • keystorePath: Path to the keystore file

Returns:

  • address: The Ethereum address (0x prefixed, checksummed)
  • error: Any error that occurred

func ImportPrivateKey

func ImportPrivateKey(dir string, privateKeyHex []byte, password []byte) (string, string, error)

ImportPrivateKey imports a hex-encoded private key into a new keystore.

Parameters:

  • dir: Directory to store the keystore file (will be created if not exists)
  • privateKeyHex: Hex-encoded private key (with or without 0x prefix)
  • password: Password to encrypt the keystore

Returns:

  • address: The Ethereum address derived from the private key (0x prefixed)
  • path: Full path to the created keystore file
  • error: Any error that occurred

func IsTerminal

func IsTerminal() bool

IsTerminal checks if stdin is a terminal.

func ReadPasswordWithConfirm

func ReadPasswordWithConfirm(prompt string) ([]byte, error)

ReadPasswordWithConfirm reads a password twice for confirmation. Returns error if passwords don't match or if stdin is not a terminal.

func ReadSecret

func ReadSecret() ([]byte, error)

ReadSecret reads a secret from stdin without echoing. Returns error if stdin is not a terminal to prevent insecure piping.

func ReadSecretWithContext added in v0.2.1

func ReadSecretWithContext(ctx context.Context) ([]byte, error)

ReadSecretWithContext reads a secret from stdin without echoing, with context support. Returns error if stdin is not a terminal to prevent insecure piping. If context is canceled, returns ErrContextCanceled.

func SecureZeroize

func SecureZeroize(data []byte)

SecureZeroize securely erases sensitive data from memory. This function should be called with defer after reading a password.

func VerifyPassword

func VerifyPassword(keystorePath string, password []byte) error

VerifyPassword verifies that the password can decrypt the keystore.

Parameters:

  • keystorePath: Path to the keystore file
  • password: Password to verify

Returns:

  • error: nil if password is correct, error otherwise

Types

type KeystoreInfo

type KeystoreInfo struct {
	Address string `json:"address"`
	Path    string `json:"path"`
}

KeystoreInfo contains information about a keystore file.

func ListKeystores

func ListKeystores(dir string) ([]KeystoreInfo, error)

ListKeystores lists all keystore files in a directory.

Parameters:

  • dir: Directory to scan for keystore files

Returns:

  • []KeystoreInfo: List of keystores found
  • error: Any error that occurred

Jump to

Keyboard shortcuts

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