utils

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressFromPrivateKey

func AddressFromPrivateKey(privateKeyHex string) string

AddressFromPrivateKey derives the Ethereum address from a hexadecimal private key string.

This function takes a hexadecimal string representing an Ethereum private key (with or without the '0x' prefix) and computes the corresponding Ethereum address.

Parameters:

  • privateKeyHex: Hexadecimal string representing the Ethereum private key.

Returns:

  • string: The Ethereum address derived from the private key.

func CreateHTTPRequestWithBody

func CreateHTTPRequestWithBody(method string, uri string, body interface{}) (*http.Request, error)

CreateHTTPRequestWithBody creates a new HTTP request with a request body.

Parameters:

  • method: HTTP method (GET, POST, PUT, DELETE, etc.).
  • uri: Request URI.
  • body: Request body to be included in the HTTP request. It can be a string, []byte, or any other type that can be marshaled into a valid HTTP request body.

Returns:

  • *http.Request: Created HTTP request instance.
  • error: Any error encountered during request creation.

func GetHTTPClient

func GetHTTPClient(timeout time.Duration) *http.Client

GetHTTPClient initializes a new `http.Client` with custom `http.Transport` and timeout. The `http.Transport` is configured with:

  • 100 `MaxIdleConns`
  • 100 `MaxConnsPerHost`
  • 100 `MaxIdleConnsPerHost`

The timeout for the `http.Client` can be configured via the `time.Duration` parameter.

Parameters:

  • timeout: Timeout duration for the `http.Client`.

Returns:

  • *http.Client: Configured HTTP client instance.

func GetTransactionParams

func GetTransactionParams(
	ctx context.Context,
	ethClient types.IEthClient,
	privateKey *ecdsa.PrivateKey,
	from *common.Address,
	to *common.Address,
	data *[]byte,
) (nonce uint64, gasPrice *big.Int, chainID *big.Int, gasLimit uint64, err error)

GetTransactionParams retrieves transaction parameters required for sending a transaction.

This function queries the Ethereum network using the provided Ethereum client (`ethClient`) to fetch the following transaction parameters: - Nonce: The transaction count of the sender's address. - GasPrice: The current gas price for the transaction. - ChainID: The ID of the Ethereum chain the transaction will be sent on. - GasLimit: The maximum amount of gas that can be used for the transaction.

Parameters:

  • ctx: The context for the Ethereum client operations.
  • ethClient: Interface for interacting with the Ethereum blockchain.
  • privateKey: The sender's private key for signing the transaction.
  • from: The sender's Ethereum address.
  • to: The recipient's Ethereum address (optional for contract creation).
  • data: The data payload for the transaction (optional).

Returns:

  • nonce: The current nonce (transaction count) of the sender's address.
  • gasPrice: The current gas price in Wei.
  • chainID: The ID of the Ethereum chain.
  • gasLimit: The maximum gas limit for the transaction.
  • err: Any error encountered during the retrieval of parameters.

func SendHTTPRequest

func SendHTTPRequest(c types.IHTTPClient, req *http.Request) (*http.Response, error)

SendHTTPRequest sends an HTTP request using a provided `http.Client` and returns the response.

Parameters:

  • c: Custom HTTP client implementing `types.IHTTPClient`.
  • req: HTTP request instance to be sent.

Returns:

  • *http.Response: HTTP response received from the server.
  • error: Any error encountered during the HTTP request or response handling.

func SendRPCRequest

func SendRPCRequest(connection types.IWSConnection, request interface{}) error

SendRPCRequest sends a RPC request via a WebSocket connection and returns any error encountered.

Parameters:

  • connection: WebSocket connection implementing `types.IWSConnection` interface.
  • request: JSON-RPC request payload to be sent over the WebSocket connection.

Returns:

  • error: Returns an error if there was any issue sending the RPC request.

func SignMessage

func SignMessage(domain apitypes.TypedDataDomain, privateKey string, primaryType types.PrimaryType, message interface{}) (string, error)

SignMessage signs a message using EIP-712 and returns the signature.

This function signs a message using the EIP-712 standard, which defines structured data hashing and signing for Ethereum. It takes the domain parameters (like name, version, chainId, verifyingContract), private key of the signer, primary type (the structure of the message), and the message itself.

Parameters:

  • domain: The domain parameters required for EIP-712 signing.
  • privateKey: The private key of the signer in hexadecimal format (without '0x' prefix).
  • primaryType: The primary type describing the structure of the message being signed.
  • message: The message payload to be signed. It should conform to the primaryType structure.

Returns:

  • string: The signature of the message in hexadecimal format (with '0x' prefix).
  • error: An error if the signing process fails, nil otherwise.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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