ethsig

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSignatureLength = errors.New("signature must be 65 bytes (r||s||v)")

ErrInvalidSignatureLength is returned when the decoded signature is not 65 bytes.

Functions

func CreateKeystore

func CreateKeystore(key *ecdsa.PrivateKey, passphrase, outputPath string) error

CreateKeystore creates a new encrypted keystore file from a private key. This is useful for converting a raw private key to a secure keystore format.

func GenerateNewKey

func GenerateNewKey() (*ecdsa.PrivateKey, error)

GenerateNewKey generates a new random Ethereum private key.

func GetAddressFromKeystore

func GetAddressFromKeystore(keystorePath string) (string, error)

GetAddressFromKeystore returns the Ethereum address from a keystore file without decrypting it. This is useful for showing the user which address will be used before asking for the passphrase.

func GetAddressFromPrivateKey

func GetAddressFromPrivateKey(key *ecdsa.PrivateKey) string

GetAddressFromPrivateKey returns the Ethereum address corresponding to a private key.

func HashKeccak256

func HashKeccak256(data []byte) []byte

HashKeccak256 computes keccak256 hash of the input.

func LoadPrivateKeyFromHex

func LoadPrivateKeyFromHex(hexKey string) (*ecdsa.PrivateKey, error)

LoadPrivateKeyFromHex loads a private key from a hex-encoded string (with or without 0x prefix). This is less secure than using a keystore but useful for testing or when the key is stored in an environment variable.

func LoadPrivateKeyFromKeystore

func LoadPrivateKeyFromKeystore(keystorePath, passphrase string) (*ecdsa.PrivateKey, error)

LoadPrivateKeyFromKeystore loads an Ethereum private key from an encrypted keystore file using the provided passphrase. The keystore file should be in the standard Ethereum keystore JSON format (as generated by geth, MetaMask, etc.).

func PersonalSignHash

func PersonalSignHash(message []byte) []byte

PersonalSignHash returns the keccak256 hash of the prefixed message according to the personal_sign / EIP-191 scheme used by many wallets:

keccak256("\x19Ethereum Signed Message:\n{len(message)}" || message)

func PreparePersonalSignPayload

func PreparePersonalSignPayload(canonical []byte) string

PreparePersonalSignPayload returns the 0x-prefixed hex payload suitable to pass to a wallet's personal_sign method. Many wallets accept the message as a hex string representing UTF-8 bytes.

func PrivateKeyToHex

func PrivateKeyToHex(key *ecdsa.PrivateKey) string

PrivateKeyToHex converts a private key to a hex-encoded string (without 0x prefix).

func RecoverAddressFromSignature

func RecoverAddressFromSignature(canonicalBytes []byte, sigHex string, usePersonalSign bool) (string, error)

RecoverAddressFromSignature recovers the Ethereum address that produced sigHex over canonicalBytes. If usePersonalSign is true, the personal_sign prefix is applied before recovery.

func SignCanonicalWithPrivKey

func SignCanonicalWithPrivKey(canonicalBytes []byte, privKeyHex string) (string, error)

SignCanonicalWithPrivKey signs canonicalBytes using a hex-encoded private key (0x... or raw hex). It signs keccak256(canonicalBytes) directly (RAW mode) and returns a 0x-prefixed hex signature in the common Ethereum format r||s||v (v returned as 27 or 28).

func SignPersonalWithPrivKey

func SignPersonalWithPrivKey(canonicalBytes []byte, privKeyHex string) (string, error)

SignPersonalWithPrivKey signs using the personal_sign prefixed hash (EIP-191 style). Returns 0x-prefixed hex signature r||s||v (v == 27/28).

func VerifyEthereumSignature

func VerifyEthereumSignature(canonicalBytes []byte, sigHex string, expectedAddr string, usePersonalSign bool) (bool, error)

VerifyEthereumSignature verifies that sigHex is a valid signature over canonicalBytes by expectedAddr. If usePersonalSign is true, the verification uses the personal_sign prefix (EIP-191) hash. expectedAddr may be a checksummed or non-checksummed address (0x...), comparison is case-insensitive. Returns (true,nil) if signature is valid, (false,nil) if signature is well-formed but signer differs, or (false,error) if an error occurred during verification.

Types

This section is empty.

Jump to

Keyboard shortcuts

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