Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added header-key and header-value to pass header if required for wasm clients #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions relayer/chains/wasm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"os"
"path"
"sync"
Expand Down Expand Up @@ -53,6 +54,8 @@ type WasmProviderConfig struct {
ChainName string `json:"-" yaml:"-"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
HeaderKey string `json:"header-key" yaml:"header-key"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use standard Authorization header key rather using config.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header key may be different for different API endpoints so letting it be set by the config.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authorization header is standard.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not authorization header, but rather may be API key based where some expect in key api-key or some API in authorization etc. If we are only talking about some predefined API please do mention them so it will be tailored to work only for them.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's specific for now, almost all RPC support authorization from endpoint.

HeaderValue string `json:"header-value" yaml:"header-value"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
GasAdjustment float64 `json:"gas-adjustment" yaml:"gas-adjustment"`
Expand Down Expand Up @@ -345,16 +348,13 @@ func (ap *WasmProvider) Init(ctx context.Context) error {
return err
}

rpcClient, err := NewRPCClient(ap.PCfg.RPCAddr, timeout)
rpcClient, err := NewRPCClient(ap.PCfg.RPCAddr, timeout, ap.PCfg.HeaderKey, ap.PCfg.HeaderValue)
if err != nil {
return err
}
ap.RPCClient = rpcClient

lightprovider, err := prov.New(ap.PCfg.ChainID, ap.PCfg.RPCAddr)
if err != nil {
return err
}
lightprovider := prov.NewWithClient(ap.PCfg.ChainID, rpcClient)
ap.LightProvider = lightprovider

clientCtx := client.Context{}.
Expand Down Expand Up @@ -516,7 +516,40 @@ func keysDir(home, chainID string) string {
return path.Join(home, "keys", chainID)
}

type CustomRoundTripper struct {
rt http.RoundTripper
headers http.Header
}

// RoundTrip executes a single HTTP transaction and adds custom headers
func (c *CustomRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for key, values := range c.headers {
for _, value := range values {
req.Header.Add(key, value)
}
}
return c.rt.RoundTrip(req)
}

// NewRPCClient initializes a new tendermint RPC client connected to the specified address.
func NewRPCClient(addr string, timeout time.Duration) (*rpchttp.HTTP, error) {
return client.NewClientFromNode(addr)
func NewRPCClient(addr string, timeout time.Duration, headerKey, headerValue string) (*rpchttp.HTTP, error) {
var customTransport *CustomRoundTripper
headers := http.Header{}
if headerKey != "" {
headers.Add(headerKey, headerValue)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use key from environment variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key or value ?? since other api which used API key in URL are also saved in plain mode in config, it can be done later if you are concerned about security analyzing the benefits and disadvantages of saving key in file.

}
customTransport = &CustomRoundTripper{
rt: http.DefaultTransport,
headers: headers,
}
// Create a custom HTTP client with a custom transport
customHTTPClient := &http.Client{
Timeout: timeout,
Transport: customTransport,
}
rpcClient, err := rpchttp.NewWithClient(addr, "/websocket", customHTTPClient)
if err != nil {
return nil, err
}
return rpcClient, err
}
Loading