-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path" | ||
"sync" | ||
|
@@ -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"` | ||
|
@@ -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{}. | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use key from environment variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authorization header is standard.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.