Documentation
¶
Overview ¶
Package web3signer provides a client for interacting with Web3Signer services.
Web3Signer is a remote signing service that provides a JSON-RPC API for signing Ethereum transactions and messages. This client package provides a Go interface for interacting with Web3Signer instances via JSON-RPC.
The client supports the following operations:
- Signing transactions with specified keys
- Listing available public keys
- Reloading signer keys
- Health checking and status monitoring
Example usage:
// Basic HTTP configuration
cfg := web3signer.DefaultConfig()
cfg.BaseURL = "http://localhost:9000"
cfg.Timeout = 30 * time.Second
client, err := web3signer.NewClient(cfg, logger)
// HTTPS with TLS configuration
tlsConfig := web3signer.NewConfigWithTLS(
"https://web3signer.example.com:9000",
caCert, // PEM-encoded CA certificate
clientCert, // PEM-encoded client certificate
clientKey, // PEM-encoded client private key
)
secureClient, err := web3signer.NewClient(tlsConfig, logger)
// Sign a transaction
signature, err := client.EthSignTransaction(ctx, "0x1234...", txData)
if err != nil {
log.Fatal(err)
}
// List available accounts
accounts, err := client.EthAccounts(ctx)
if err != nil {
log.Fatal(err)
}
Index ¶
- type Client
- func (c *Client) EthAccounts(ctx context.Context) ([]string, error)
- func (c *Client) EthSign(ctx context.Context, account string, data string) (string, error)
- func (c *Client) EthSignTransaction(ctx context.Context, from string, transaction map[string]interface{}) (string, error)
- func (c *Client) EthSignTypedData(ctx context.Context, account string, typedData interface{}) (string, error)
- func (c *Client) ListPublicKeys(ctx context.Context) ([]string, error)
- func (c *Client) SetHttpClient(client *http.Client)
- func (c *Client) Sign(ctx context.Context, account string, data string) (string, error)
- func (c *Client) SignRaw(ctx context.Context, identifier string, data []byte) (string, error)
- type Config
- type EthSignTransactionRequest
- type HealthCheck
- type JSONRPCError
- type JSONRPCRequest
- type JSONRPCResponse
- type PublicKeysResponse
- type SignRequest
- type SignResponse
- type StatusCheck
- type TLSConfig
- type Web3SignerError
- type Web3SignerResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// Logger is used for logging client operations and debugging
Logger *zap.Logger
// contains filtered or unexported fields
}
Client represents a Web3Signer JSON-RPC client that provides methods for interacting with a Web3Signer service instance.
func NewClient ¶
NewClient creates a new Web3Signer client with the given configuration and logger. Both cfg and logger must be non-nil. Use DefaultConfig() if you want default configuration.
func (*Client) EthAccounts ¶
EthAccounts returns a list of accounts available for signing. This corresponds to the eth_accounts JSON-RPC method.
func (*Client) EthSign ¶
EthSign signs data with the specified account. This corresponds to the eth_sign JSON-RPC method.
func (*Client) EthSignTransaction ¶
func (c *Client) EthSignTransaction(ctx context.Context, from string, transaction map[string]interface{}) (string, error)
EthSignTransaction signs a transaction and returns the signature. This corresponds to the eth_signTransaction JSON-RPC method.
func (*Client) EthSignTypedData ¶
func (c *Client) EthSignTypedData(ctx context.Context, account string, typedData interface{}) (string, error)
EthSignTypedData signs typed data with the specified account. This corresponds to the eth_signTypedData JSON-RPC method.
func (*Client) ListPublicKeys ¶
ListPublicKeys retrieves all available public keys from the Web3Signer service. This is a convenience method that calls EthAccounts.
func (*Client) SetHttpClient ¶
SetHttpClient allows setting a custom HTTP client for the Web3Signer client. This is useful for testing or when custom HTTP client configuration is needed.
func (*Client) Sign ¶
Sign signs data with the specified account using eth_sign. This is a convenience method that calls EthSign.
type Config ¶
type Config struct {
// BaseURL is the base URL of the Web3Signer service (e.g., "http://localhost:9000")
BaseURL string
// Timeout is the maximum duration for HTTP requests
Timeout time.Duration
// TLS configuration for HTTPS connections
TLS *TLSConfig
}
Config holds the configuration for the Web3Signer client.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a default configuration for the Web3Signer client. The default configuration uses localhost:9000 as the base URL and a 30-second timeout.
func NewConfigWithTLS ¶
NewConfigWithTLS creates a Web3Signer Config with TLS configuration. This is a convenience function to create a config with TLS settings for secure connections.
Parameters:
- baseURL: The Web3Signer service URL (e.g., "https://web3signer.example.com:9000")
- caCert: PEM-encoded CA certificate to verify the server's certificate (optional)
- clientCert: PEM-encoded client certificate for mutual TLS authentication (optional)
- clientKey: PEM-encoded client private key for mutual TLS authentication (optional)
TLS configuration is only applied for HTTPS URLs. For HTTP URLs, TLS settings are ignored. If any TLS parameter is provided for an HTTPS URL, a TLS config will be created.
type EthSignTransactionRequest ¶
type EthSignTransactionRequest struct {
// From is the account to sign with
From string `json:"from"`
// To is the destination address
To string `json:"to,omitempty"`
// Gas is the gas limit
Gas string `json:"gas,omitempty"`
// GasPrice is the gas price
GasPrice string `json:"gasPrice,omitempty"`
// Value is the value to send
Value string `json:"value,omitempty"`
// Data is the transaction data
Data string `json:"data,omitempty"`
// Nonce is the transaction nonce
Nonce string `json:"nonce,omitempty"`
// ChainID is the chain ID
ChainID string `json:"chainId,omitempty"`
}
EthSignTransactionRequest represents the parameters for eth_signTransaction.
type HealthCheck ¶
type HealthCheck struct {
// Status is the overall status of the service ("UP" or "DOWN")
Status string `json:"status"`
// Checks contains detailed status information for individual components
Checks []StatusCheck `json:"checks"`
// Outcome is the final health determination ("UP" or "DOWN")
Outcome string `json:"outcome"`
}
HealthCheck represents the detailed health status of the Web3Signer service.
type JSONRPCError ¶
type JSONRPCError struct {
// Code is the error code
Code int `json:"code"`
// Message is the error message
Message string `json:"message"`
// Data contains additional error data
Data interface{} `json:"data,omitempty"`
}
JSONRPCError represents a JSON-RPC 2.0 error.
type JSONRPCRequest ¶
type JSONRPCRequest struct {
// Jsonrpc specifies the JSON-RPC version (always "2.0")
Jsonrpc string `json:"jsonrpc"`
// Method is the JSON-RPC method name
Method string `json:"method"`
// Params contains the method parameters
Params interface{} `json:"params,omitempty"`
// ID is a unique identifier for the request
ID int64 `json:"id"`
}
JSONRPCRequest represents a JSON-RPC 2.0 request.
type JSONRPCResponse ¶
type JSONRPCResponse struct {
// Jsonrpc specifies the JSON-RPC version (always "2.0")
Jsonrpc string `json:"jsonrpc"`
// Result contains the method result (present on success)
Result interface{} `json:"result,omitempty"`
// Error contains error information (present on error)
Error *JSONRPCError `json:"error,omitempty"`
// ID is the request identifier
ID int64 `json:"id"`
}
JSONRPCResponse represents a JSON-RPC 2.0 response.
type PublicKeysResponse ¶
type PublicKeysResponse []string
PublicKeysResponse represents a list of public keys returned by the Web3Signer service.
func (PublicKeysResponse) MarshalJSON ¶
func (p PublicKeysResponse) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for PublicKeysResponse.
func (*PublicKeysResponse) UnmarshalJSON ¶
func (p *PublicKeysResponse) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for PublicKeysResponse.
type SignRequest ¶
type SignRequest struct {
// Data is the hex-encoded data to be signed
Data string `json:"data"`
}
SignRequest represents a request to sign data sent to the Web3Signer service.
type SignResponse ¶
type SignResponse struct {
// Signature is the hex-encoded signature returned by the service
Signature string `json:"signature"`
}
SignResponse represents the response from a signing operation.
type StatusCheck ¶
type StatusCheck struct {
// ID is the identifier of the component being checked (e.g., "disk-space", "memory")
ID string `json:"id"`
// Status is the status of this component ("UP" or "DOWN")
Status string `json:"status"`
}
StatusCheck represents the status of an individual component within the health check.
type TLSConfig ¶
type TLSConfig struct {
// CACert is the PEM-encoded CA certificate to verify the server's certificate
CACert string
// ClientCert is the PEM-encoded client certificate for mutual TLS authentication
ClientCert string
// ClientKey is the PEM-encoded client private key for mutual TLS authentication
ClientKey string
// InsecureSkipVerify skips server certificate verification (not recommended for production)
InsecureSkipVerify bool
}
TLSConfig holds TLS configuration for secure connections to Web3Signer. According to the Web3Signer TLS documentation, this supports: - Server certificate verification using custom CA certificates - Mutual TLS authentication using client certificates - Optional certificate verification skipping for testing
type Web3SignerError ¶
type Web3SignerError struct {
// Code is the HTTP status code associated with the error
Code int `json:"code"`
// Message is the error message describing what went wrong
Message string `json:"message"`
}
Web3SignerError represents an error response from the Web3Signer service.
func (*Web3SignerError) Error ¶
func (e *Web3SignerError) Error() string
Error implements the error interface for Web3SignerError.
type Web3SignerResponse ¶
type Web3SignerResponse struct {
// Status indicates the response status
Status string `json:"status,omitempty"`
// Data contains the response payload
Data interface{} `json:"data,omitempty"`
// Error contains error information if the request failed
Error *Web3SignerError `json:"error,omitempty"`
}
Web3SignerResponse represents a generic response structure from the Web3Signer service.
func (*Web3SignerResponse) IsError ¶
func (r *Web3SignerResponse) IsError() bool
IsError returns true if the response contains an error.