Skip to content

Commit

Permalink
feat: added header-key and header-value to pass header if required fo…
Browse files Browse the repository at this point in the history
…r wasm clients
  • Loading branch information
bcsainju committed Jul 31, 2024
1 parent 246017f commit 64b6862
Showing 1 changed file with 40 additions and 7 deletions.
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"`
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)
}
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
}

0 comments on commit 64b6862

Please sign in to comment.