Documentation
¶
Overview ¶
Package endpoint provides the HTTP/RPC API server for EthBackNode. It exposes JSON-RPC 2.0 endpoints for address management, balance queries, transaction operations, and subscriber management.
Index ¶
- Constants
- Variables
- func NewServer(options ...ServerOption) (server *endpointServer, err error)
- type BackRpc
- func (r *BackRpc) AddRpcProcessor(method RpcMethod, processor RpcProcessor)
- func (r *BackRpc) Handle(ctx *fasthttp.RequestCtx)
- func (r *BackRpc) InitProcessors()
- func (r *BackRpc) RegisterProcessor(method RpcMethod, processor RpcProcessor)
- func (r *BackRpc) RegisterSecuredProcessor(method RpcMethod, processor RpcProcessor)
- func (r *BackRpc) RouteRpcRequest(ctx *fasthttp.RequestCtx) (err error)
- type BackRpcOption
- type DevForm
- type HttpResponse
- type JsonRpcError
- type JsonRpcRequest
- func (r *JsonRpcRequest) GetMethod() (method RpcMethod)
- func (r *JsonRpcRequest) GetParamBool(key string) (value bool, err error)
- func (r *JsonRpcRequest) GetParamFloat64(key string) (param float64, err error)
- func (r *JsonRpcRequest) GetParamInt(key string) (param int64, err error)
- func (r *JsonRpcRequest) GetParamRaw(key string) (param interface{}, found bool)
- func (r *JsonRpcRequest) GetParamString(key string) (param string, err error)
- func (r *JsonRpcRequest) ParseParams(params interface{}) (err error)
- type JsonRpcResponse
- type Number
- type Processor
- type RequestContext
- type RequestId
- type Router
- type RpcMethod
- type RpcProcessor
- type RpcRequest
- type RpcRequestContext
- func (r *RpcRequestContext) Authorized(v bool)
- func (r *RpcRequestContext) GetApiToken() (token string, err error)
- func (r *RpcRequestContext) GetBool(key string) (value bool, err error)
- func (r *RpcRequestContext) GetInt(key string) (value int64, err error)
- func (r *RpcRequestContext) GetString(key string) (value string, err error)
- func (r *RpcRequestContext) SetBool(key string, value bool)
- func (r *RpcRequestContext) SetInt(key string, value int64)
- func (r *RpcRequestContext) SetString(key, value string)
- type RpcResponse
- type ServerOption
- type TransferInfoResponse
Constants ¶
const ( // MIME_TYPE_JSON is the content type for JSON responses. MIME_TYPE_JSON = "application/json" // ERROR_METHOD_NOT_ALLOWED is the JSON-RPC error for unsupported methods. ERROR_METHOD_NOT_ALLOWED = `{"error": {"code": -32601, "message": "method not found"}}` )
Constants for HTTP content types and error messages.
const ( // JSON_RPC_VERSION is the JSON-RPC protocol version. JSON_RPC_VERSION = "2.0" ERROR_CODE_PARSE_ERROR = -32700 ERROR_MESSAGE_PARSE_ERROR = "Parse error" ERROR_CODE_INVALID_REQUEST = -32600 ERROR_MESSAGE_INVALID_REQUEST = "invalid request" ERROR_CODE_METHOD_NOT_FOUND = -32601 ERROR_MESSAGE_METHOD_NOT_FOUND = "method not found" ERROR_CODE_SERVER_ERROR = -32000 ERROR_MESSAGE_SERVER_ERROR = "server error" ERROR_CODE_UNAUTHORIZED = -32001 ERROR_MESSAGE_UNAUTHORIZED = "unauthorized access" )
JSON-RPC 2.0 version and standard error codes.
const (
MIME_TYPE_TEXT_HTML = "text/html"
)
Variables ¶
var ( // ErrParamNotFound is returned when a required parameter is missing. ErrParamNotFound = errors.New("param not found") // ErrInvalidParamType is returned when a parameter has the wrong type. ErrInvalidParamType = errors.New("invalid param type") // ErrInvalidAmount is returned when an amount value is invalid. ErrInvalidAmount = errors.New("invalid amount") )
Error definitions for endpoint operations.
Functions ¶
func NewServer ¶
func NewServer(options ...ServerOption) (server *endpointServer, err error)
NewServer creates a new HTTP server with the specified options.
Types ¶
type BackRpc ¶
type BackRpc struct {
// contains filtered or unexported fields
}
BackRpc is the main RPC handler that processes JSON-RPC 2.0 requests. It manages address pools, blockchain clients, subscriptions, and security.
func NewBackRpc ¶
func NewBackRpc(addressPool *address.Manager, chainClient types.ChainClient, subscriptions *subscriptions.Manager, watchdog *watchdog.Service, txCache types.TxCache, options ...BackRpcOption) *BackRpc
NewBackRpc creates a new RPC handler with all required dependencies. Initializes processors and loads known tokens from the blockchain client.
func (*BackRpc) AddRpcProcessor ¶
func (r *BackRpc) AddRpcProcessor(method RpcMethod, processor RpcProcessor)
AddRpcProcessor registers an RPC processor, panicking if the method already exists.
func (*BackRpc) Handle ¶
func (r *BackRpc) Handle(ctx *fasthttp.RequestCtx)
Handle is the main HTTP request handler implementing fasthttp.RequestHandler. Routes requests to appropriate RPC processors.
func (*BackRpc) InitProcessors ¶
func (r *BackRpc) InitProcessors()
InitProcessors registers all built-in RPC method processors. Sets up processors for ping, info, address, balance, transfer, and service methods.
func (*BackRpc) RegisterProcessor ¶
func (r *BackRpc) RegisterProcessor(method RpcMethod, processor RpcProcessor)
RegisterProcessor registers an RPC method processor without authentication.
func (*BackRpc) RegisterSecuredProcessor ¶
func (r *BackRpc) RegisterSecuredProcessor(method RpcMethod, processor RpcProcessor)
RegisterSecuredProcessor registers an RPC method processor with authentication. Requires serviceId parameter and validates API token or signature.
func (*BackRpc) RouteRpcRequest ¶
func (r *BackRpc) RouteRpcRequest(ctx *fasthttp.RequestCtx) (err error)
RouteRpcRequest routes incoming HTTP requests to the appropriate handler. Supports /rpc for JSON-RPC, with placeholders for REST, WebSocket, and docs.
type BackRpcOption ¶
type BackRpcOption func(r *BackRpc)
BackRpcOption is a function that configures a BackRpc handler.
func WithDebugMode ¶
func WithDebugMode(debugMode bool) BackRpcOption
WithDebugMode enables debug logging for RPC requests.
func WithFallbackResponse ¶
func WithFallbackResponse(response HttpResponse) BackRpcOption
WithFallbackResponse sets the response for unmatched GET requests.
func WithRpcProcessor ¶
func WithRpcProcessor(method RpcMethod, processor RpcProcessor) BackRpcOption
WithRpcProcessor registers a custom RPC method processor.
func WithSecurityManager ¶
func WithSecurityManager(securityManager *security.Manager) BackRpcOption
WithSecurityManager sets the security manager for request authentication.
type DevForm ¶
type DevForm struct {
FormPath string
}
func (DevForm) ContentType ¶
func (DevForm) StatusCode ¶
type HttpResponse ¶
HttpResponse defines the interface for custom HTTP fallback responses.
type JsonRpcError ¶
type JsonRpcRequest ¶
type JsonRpcRequest struct {
Id RequestId `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method RpcMethod `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
// contains filtered or unexported fields
}
JsonRpcRequest represents a JSON-RPC 2.0 request.
func (*JsonRpcRequest) GetMethod ¶
func (r *JsonRpcRequest) GetMethod() (method RpcMethod)
GetMethod returns the RPC method name.
func (*JsonRpcRequest) GetParamBool ¶
func (r *JsonRpcRequest) GetParamBool(key string) (value bool, err error)
GetParamBool extracts a boolean parameter by key.
func (*JsonRpcRequest) GetParamFloat64 ¶
func (r *JsonRpcRequest) GetParamFloat64(key string) (param float64, err error)
GetParamFloat64 extracts a float64 parameter by key.
func (*JsonRpcRequest) GetParamInt ¶
func (r *JsonRpcRequest) GetParamInt(key string) (param int64, err error)
GetParamInt extracts an integer parameter by key.
func (*JsonRpcRequest) GetParamRaw ¶
func (r *JsonRpcRequest) GetParamRaw(key string) (param interface{}, found bool)
GetParamRaw extracts a raw parameter value by key.
func (*JsonRpcRequest) GetParamString ¶
func (r *JsonRpcRequest) GetParamString(key string) (param string, err error)
GetParamString extracts a string parameter by key.
func (*JsonRpcRequest) ParseParams ¶
func (r *JsonRpcRequest) ParseParams(params interface{}) (err error)
ParseParams unmarshals the request parameters into the provided struct.
type JsonRpcResponse ¶
type JsonRpcResponse struct {
Id RequestId `json:"id"`
JsonRpc string `json:"jsonrpc"`
Error *JsonRpcError `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}
func NewResponse ¶
func NewResponse() *JsonRpcResponse
func (*JsonRpcResponse) SetError ¶
func (r *JsonRpcResponse) SetError(code int, message string)
func (*JsonRpcResponse) SetErrorWithData ¶
func (r *JsonRpcResponse) SetErrorWithData(code int, message, data string)
func (*JsonRpcResponse) SetResult ¶
func (r *JsonRpcResponse) SetResult(result interface{})
func (*JsonRpcResponse) SetResultBytes ¶
func (r *JsonRpcResponse) SetResultBytes(result []byte)
type Processor ¶
type Processor interface {
Process(ctx RequestContext, request RpcRequest, result RpcResponse) (err error)
}
Processor defines the interface for RPC request processing.
type RequestContext ¶
type RequestContext interface {
GetString(key string) (value string, err error)
GetInt(key string) (value int64, err error)
GetBool(key string) (value bool, err error)
GetApiToken() (token string, err error)
SetBool(key string, value bool)
SetString(key string, value string)
SetInt(key string, value int64)
Authorized(bool)
}
RequestContext provides access to request-scoped data and authentication.
type RequestId ¶
type RequestId string
RequestId represents a JSON-RPC request identifier. Handles both string and numeric IDs in JSON.
func (RequestId) MarshalJSON ¶
func (*RequestId) UnmarshalJSON ¶
type RpcProcessor ¶
type RpcProcessor func(ctx RequestContext, request RpcRequest, response RpcResponse)
RpcProcessor is a function that handles an RPC request.
type RpcRequest ¶
type RpcRequest interface {
GetMethod() (method RpcMethod)
ParseParams(params interface{}) (err error)
GetParamString(key string) (value string, err error)
GetParamInt(key string) (value int64, err error)
GetParamBool(key string) (value bool, err error)
}
RpcRequest defines the interface for accessing JSON-RPC request data.
type RpcRequestContext ¶
type RpcRequestContext struct {
// contains filtered or unexported fields
}
func NewRpcRequestContext ¶
func NewRpcRequestContext() *RpcRequestContext
func (*RpcRequestContext) Authorized ¶
func (r *RpcRequestContext) Authorized(v bool)
func (*RpcRequestContext) GetApiToken ¶
func (r *RpcRequestContext) GetApiToken() (token string, err error)
func (*RpcRequestContext) GetBool ¶
func (r *RpcRequestContext) GetBool(key string) (value bool, err error)
func (*RpcRequestContext) GetInt ¶
func (r *RpcRequestContext) GetInt(key string) (value int64, err error)
func (*RpcRequestContext) GetString ¶
func (r *RpcRequestContext) GetString(key string) (value string, err error)
func (*RpcRequestContext) SetBool ¶
func (r *RpcRequestContext) SetBool(key string, value bool)
func (*RpcRequestContext) SetInt ¶
func (r *RpcRequestContext) SetInt(key string, value int64)
func (*RpcRequestContext) SetString ¶
func (r *RpcRequestContext) SetString(key, value string)
type RpcResponse ¶
type RpcResponse interface {
SetResult(result interface{})
SetError(code int, message string)
SetErrorWithData(code int, message, data string)
}
RpcResponse defines the interface for building JSON-RPC responses.
type ServerOption ¶
type ServerOption func(s *endpointServer) error
ServerOption is a function that configures an endpoint server.
func WithHandler ¶
func WithHandler(handler fasthttp.RequestHandler) ServerOption
WithHandler sets the HTTP request handler for the server.
func WithHttpListener ¶
func WithHttpListener(listenAddress string) ServerOption
WithHttpListener configures the server to listen on HTTP at the specified address.
func WithSocketListener ¶
func WithSocketListener(socketFile string) ServerOption
WithSocketListener configures the server to listen on a Unix socket.
func WithSshListener ¶
func WithSshListener(listenPort int, knownKeys []string) ServerOption
WithSshListener configures the server to accept SSH connections with key authentication.
type TransferInfoResponse ¶
type TransferInfoResponse struct {
TxID string `json:"tx_id"`
Timestamp int64 `json:"timestamp"`
BlockNum int `json:"blockNum"`
Success bool `json:"success"`
Transfer bool `json:"transfer"`
NativeCoin bool `json:"nativeCoin,omitempty"`
Symbol string `json:"symbol,omitempty"`
Decimals int `json:"decimals"`
SmartContract bool `json:"smartContract,omitempty"`
From string `json:"from"`
To string `json:"to"`
Amount amount `json:"amount"`
Token string `json:"token,omitempty"`
TokenSymbol string `json:"tokenSymbol,omitempty"`
Fee amount `json:"fee"`
InPool bool `json:"inPool"`
Confirmed bool `json:"confirmed"`
Confirmations int `json:"confirmations,omitempty"`
ChainSpecificData []byte `json:"chainSpecificData,omitempty"`
}