ethereum

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 15 Imported by: 3

README

ethereum-sdk

Ethereum SDK is used to interact with the Ethereum blockchain or Evm blockchains, it contains various functions can be used to web3 wallet.

Installation

go get

To obtain the latest version, simply require the project using :

go get -u github.com/xiaohuasheng0x1/blockchains/coins/ethereum

Usage

New Address
	p, _ := hex.DecodeString("559376194bb4c9a9dfb33fde4a2ab15daa8a899a3f43dee787046f57d5f7b10a")
	prvKey, _ := btcec.PrivKeyFromBytes(p)
	address := GetNewAddress(prvKey.PubKey())
	addr, err := PubKeyToAddr(prvKey.PubKey().SerializeUncompressed())
	if err != nil {
		// todo
	}
Transfer
	transaction := NewEthTransaction(
		big.NewInt(int64(00)),
		big.NewInt(int64(420000)),
		big.NewInt(int64(200000000000)),
		big.NewInt(int64(100000000000000000)),
		"0x1ca96f8cfe7276bb053b25e57188f1b5ec6a4728", "0x",
	)
	hash, raw, _ := transaction.GetSigningHash(big.NewInt(int64(10)))
	tx, err := transaction.SignTransaction(big.NewInt(int64(10)), prvKey)
	if err != nil {
		// todo
	}
Transfer Token
	transfer, _ := token.Transfer("0x1ca96f8cfe7276bb053b25e57188f1b5ec6a4728", big.NewInt(int64(100000000000000000)))
	transaction := NewEthTransaction(
		big.NewInt(int64(00)),
		big.NewInt(int64(420000)),
		big.NewInt(int64(200000000000)),
		big.NewInt(int64(0)),
		"0x1ca96f8cfe7276bb053b25e57188f1b5ec6a4728", hex.EncodeToString(transfer),
	)
	tx, err := transaction.SignTransaction(big.NewInt(int64(10)), prvKey)
	if err != nil {
		// todo
	}

License

Most packages or folder are MIT licensed, see package or folder for the respective license.

Documentation

Index

Constants

View Source
const AddressLength = 20
View Source
const MessagePrefixTmp = "\u0019Ethereum Signed Message:\n%d"

Variables

View Source
var (
	ErrInvalidParam = errors.New("invalid param")
)

Functions

func CalTxHash

func CalTxHash(rawTx string) string

func DecodeTx

func DecodeTx(rawTx string) (string, error)

func EcRecover

func EcRecover(signature, message string, addPrefix bool) string

func GenerateRawTransactionWithSignature

func GenerateRawTransactionWithSignature(txType int, chainId, unsignedRawTx, r, s, v string) (string, error)

GenerateRawTransactionWithSignature Generate the transaction to be broadcast based on the unsigned transaction and the signature result

func GetAddress

func GetAddress(pubkeyHex string) string

func GetEthereumMessagePrefix

func GetEthereumMessagePrefix(message string) string

func GetNewAddress

func GetNewAddress(pubKey *btcec.PublicKey) string

func MessageHash

func MessageHash(data string) string

func NewEip1559Transaction

func NewEip1559Transaction(
	chainId *big.Int,
	nonce uint64,
	maxPriorityFeePerGas *big.Int,
	maxFeePerGas *big.Int,
	gasLimit uint64,
	to *common.Address,
	value *big.Int,
	data []byte) *types.Transaction

func PubKeyToAddr

func PubKeyToAddr(publicKey []byte) (string, error)

func ValidateAddress

func ValidateAddress(address string) bool

Types

type EIP1559Transaction

type EIP1559Transaction struct {
	Transaction
	TxType               int    `json:"txType"`
	MaxFeePerGas         string `json:"maxFeePerGas"`
	MaxPriorityFeePerGas string `json:"maxPriorityFeePerGas"`
}

type Eip1559Token

type Eip1559Token struct {
	EIP1559Transaction
	ContractAddress string `json:"contract_address"`
	Amount          string `json:"amount"`
}

type Eip1559Transaction

type Eip1559Transaction struct {
	ChainId    *big.Int         `json:"chainId"`
	Nonce      uint64           `json:"nonce"`
	GasTipCap  *big.Int         `json:"gasTipCap"`
	GasFeeCap  *big.Int         `json:"gasFeeCap"`
	Gas        uint64           `json:"gas"`
	To         *common.Address  `json:"to"`
	Value      *big.Int         `json:"value"`
	Data       []byte           `json:"data"`
	AccessList types.AccessList `json:"accessList"`
}

type EthTransaction

type EthTransaction struct {
	Nonce    *big.Int `json:"nonce"`
	GasPrice *big.Int `json:"gasPrice"`
	GasLimit *big.Int `json:"gas"`
	To       []byte   `json:"to"`
	Value    *big.Int `json:"value"`
	Data     []byte   `json:"data"`
	// Signature values
	V *big.Int `json:"v"`
	R *big.Int `json:"r"`
	S *big.Int `json:"s"`
}

func NewEthTransaction

func NewEthTransaction(nonce, gasLimit, gasPrice, value *big.Int, to, data string) *EthTransaction

func NewTransactionFromRaw

func NewTransactionFromRaw(raw string) (*EthTransaction, error)

func (*EthTransaction) GetSigningHash

func (tx *EthTransaction) GetSigningHash(chainId *big.Int) (string, string, error)

func (*EthTransaction) SignTransaction

func (tx *EthTransaction) SignTransaction(chainId *big.Int, prvKey *btcec.PrivateKey) (string, error)

func (*EthTransaction) SignedTx

func (tx *EthTransaction) SignedTx(chainId *big.Int, sig *SignatureData) (string, error)

func (*EthTransaction) UnSignedTx

func (tx *EthTransaction) UnSignedTx(chainId *big.Int) (string, error)

type SignatureData

type SignatureData struct {
	V *big.Int
	R *big.Int
	S *big.Int

	ByteV byte
	ByteR []byte
	ByteS []byte
}

func NewSignatureData

func NewSignatureData(msgHash []byte, publicKey string, r, s *big.Int) (*SignatureData, error)

func SignAsRecoverable

func SignAsRecoverable(value []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)

func SignMessage

func SignMessage(message []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)

func (SignatureData) ToBytes

func (sd SignatureData) ToBytes() []byte

func (*SignatureData) ToHex

func (sd *SignatureData) ToHex() string

type Transaction

type Transaction struct {
	Nonce    string `json:"nonce"`
	GasLimit string `json:"gasLimit"`
	GasPrice string `json:"gasPrice"`
	From     string `json:"from"`
	To       string `json:"to"`
	Value    string `json:"value"`
	Data     string `json:"data"`
	Fee      string `json:"fee"`
	ChainId  string `json:"chainId"`
}

type UnsignedTx

type UnsignedTx struct {
	Tx   string `json:"tx"`
	Hash string `json:"hash"`
}

func GenerateTxWithJSON

func GenerateTxWithJSON(message string, chainId *big.Int, isToken bool) (*UnsignedTx, error)

generate tx with json param

func NewUnsignedTx

func NewUnsignedTx(nonce, gasLimit, gasPrice, value, chainId *big.Int, to, data string) (*UnsignedTx, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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