crypto

package
v0.0.0-...-ea2125a Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package crypto provides cryptographic operations for Ethereum transactions. It implements ECDSA signing, Keccak-256 hashing, and key management using the secp256k1 elliptic curve. This package supports RFC 6979 deterministic signatures and Ethereum transaction signing.

Package crypto provides cryptographic operations for Ethereum transactions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesFromECDSAPrivateKey

func BytesFromECDSAPrivateKey(privateKey *ecdsa.PrivateKey) []byte

BytesFromECDSAPrivateKey extracts the raw bytes from an ECDSA private key. Returns the private key D value padded to the curve's bit size.

func ECDSAKeysFromPrivateKeyBytes

func ECDSAKeysFromPrivateKeyBytes(pk []byte) (*ecdsa.PrivateKey, *ecdsa.PublicKey)

ECDSAKeysFromPrivateKeyBytes derives ECDSA key pair from raw private key bytes. Uses the secp256k1 curve for key derivation.

func ECDSAKeysFromPrivateKeyHex

func ECDSAKeysFromPrivateKeyHex(pk string) (priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, err error)

ECDSAKeysFromPrivateKeyHex derives ECDSA key pair from a hex-encoded private key.

func ECDSAPublicKeyCompressedToBytes

func ECDSAPublicKeyCompressedToBytes(pub *ecdsa.PublicKey) []byte

ECDSAPublicKeyCompressedToBytes serializes an ECDSA public key to compressed format. Returns 33 bytes: 0x02 or 0x03 prefix + 32-byte X coordinate.

func ECDSAPublicKeyToBytes

func ECDSAPublicKeyToBytes(pub *ecdsa.PublicKey) []byte

ECDSAPublicKeyToBytes serializes an ECDSA public key to uncompressed format. Returns 65 bytes: 0x04 prefix + 32-byte X + 32-byte Y coordinates.

func Keccak256

func Keccak256(data ...[]byte) []byte

Keccak256 calculates and returns the Keccak256 hash of the input data or few pieces of data.

func PubKeyToAddressBytes

func PubKeyToAddressBytes(p ecdsa.PublicKey) []byte

PubKeyToAddressBytes derives an Ethereum address from an ECDSA public key. Returns the last 20 bytes of the Keccak-256 hash of the public key.

func SignEcdsaRfc6979

func SignEcdsaRfc6979(priv *ecdsa.PrivateKey, hash []byte, alg func() hash.Hash) (v byte, r, s *big.Int)

func SignEcdsaRfc6979Bytes

func SignEcdsaRfc6979Bytes(privateKey *ecdsa.PrivateKey, hash []byte, alg func() hash.Hash) (sig []byte)

SignEcdsaRfc6979Bytes signs using RFC 6979 and returns 65-byte signature [R || S || V].

Types

type EthTxSigner

type EthTxSigner struct {
	Nonce    uint64   // nonce of sender account
	GasPrice *big.Int // wei per gas
	Gas      uint64   // gas limit
	To       *[]byte  `rlp:"nil"` // nil means contract creation
	Value    *big.Int // wei amount
	Data     []byte   // contract invocation input data
	V, R, S  *big.Int // signature values
	// contains filtered or unexported fields
}

EthTxSigner represents an Ethereum transaction with signing capabilities. Supports EIP-155 chain ID for replay protection.

func NewEthTxSigner

func NewEthTxSigner(nonce uint64, gasPrice *big.Int, gas uint64, To []byte, value *big.Int, data []byte) *EthTxSigner

NewEthTxSigner creates a new Ethereum transaction signer with the given parameters.

func (*EthTxSigner) EncodeRPL

func (tx *EthTxSigner) EncodeRPL() (data []byte, err error)

EncodeRPL encodes the signed transaction in RLP format for broadcasting.

func (*EthTxSigner) Hash

func (tx *EthTxSigner) Hash() []byte

Hash computes the EIP-155 transaction hash for signing. Includes chain ID in the hash to prevent replay attacks.

func (*EthTxSigner) SetChainId

func (tx *EthTxSigner) SetChainId(id *big.Int)

SetChainId sets the EIP-155 chain ID for replay protection.

func (*EthTxSigner) Sign

func (tx *EthTxSigner) Sign(privateKey *ecdsa.PrivateKey) (sig []byte)

Sign creates an ECDSA signature for the transaction using RFC 6979. Returns 65-byte signature in [R || S || V] format and populates V, R, S fields.

type KeccakState

type KeccakState interface {
	hash.Hash
	Read([]byte) (int, error)
}

KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports Read to get a variable amount of data from the hash state. Read is faster than Sum because it doesn't copy the internal state, but also modifies the internal state. imported from go-ethereum, modified to remove dependency on go-ethereum/common because "A little copying is better than a little dependency." (https://go-proverbs.github.io) ;)

func NewKeccakState

func NewKeccakState() KeccakState

NewKeccakState creates a new Keccak-256 hash state.

Directories

Path Synopsis
Package secp256k1 implements the secp256k1 elliptic curve used by Bitcoin and Ethereum.
Package secp256k1 implements the secp256k1 elliptic curve used by Bitcoin and Ethereum.

Jump to

Keyboard shortcuts

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