Documentation
¶
Index ¶
- Variables
- func CreateKeystore(key *ecdsa.PrivateKey, passphrase, outputPath string) error
- func GenerateNewKey() (*ecdsa.PrivateKey, error)
- func GetAddressFromKeystore(keystorePath string) (string, error)
- func GetAddressFromPrivateKey(key *ecdsa.PrivateKey) string
- func HashKeccak256(data []byte) []byte
- func LoadPrivateKeyFromHex(hexKey string) (*ecdsa.PrivateKey, error)
- func LoadPrivateKeyFromKeystore(keystorePath, passphrase string) (*ecdsa.PrivateKey, error)
- func PersonalSignHash(message []byte) []byte
- func PreparePersonalSignPayload(canonical []byte) string
- func PrivateKeyToHex(key *ecdsa.PrivateKey) string
- func RecoverAddressFromSignature(canonicalBytes []byte, sigHex string, usePersonalSign bool) (string, error)
- func SignCanonicalWithPrivKey(canonicalBytes []byte, privKeyHex string) (string, error)
- func SignPersonalWithPrivKey(canonicalBytes []byte, privKeyHex string) (string, error)
- func VerifyEthereumSignature(canonicalBytes []byte, sigHex string, expectedAddr string, ...) (bool, error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.