2020-08-03 20:01:38 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-08-06 22:48:28 +00:00
|
|
|
"encoding/json"
|
2020-08-03 20:01:38 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// client implements the `Client` interface.
|
2020-08-06 22:48:28 +00:00
|
|
|
type LotusClient struct {
|
2020-08-03 20:01:38 +00:00
|
|
|
host string
|
|
|
|
jwt jwt.Token
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:48:28 +00:00
|
|
|
type RequestBody struct {
|
|
|
|
Jsonrpc string
|
|
|
|
Method string
|
|
|
|
Params []string
|
|
|
|
ID int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequestBody(method string) *RequestBody {
|
|
|
|
return &RequestBody{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Method: method,
|
|
|
|
Params: []string{},
|
|
|
|
ID: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:01:38 +00:00
|
|
|
// NewClient returns a new client.
|
2020-08-06 22:51:52 +00:00
|
|
|
func NewLotusClient(host string, token jwt.Token) *LotusClient {
|
2020-08-06 22:48:28 +00:00
|
|
|
return &LotusClient{
|
2020-08-03 20:01:38 +00:00
|
|
|
host: host,
|
|
|
|
jwt: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:48:28 +00:00
|
|
|
func (c *LotusClient) GetMessage(r *http.Request, txHash string) (*http.Response, error) {
|
|
|
|
requestBody := NewRequestBody("Filecoin.GetMessage")
|
|
|
|
requestBody.Params = append(requestBody.Params, txHash)
|
|
|
|
body, err := json.Marshal(requestBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to marshal request body %v", err)
|
|
|
|
}
|
|
|
|
client := http.Client{}
|
|
|
|
req, err := http.NewRequest("POST", c.host, bytes.NewBuffer(body))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authentication", "Bearer "+c.jwt.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to construct lotus node rpc request %v", err)
|
|
|
|
}
|
|
|
|
return client.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:01:38 +00:00
|
|
|
// HandleRequest implements the `Client` interface.
|
2020-08-06 22:48:28 +00:00
|
|
|
func (c *LotusClient) HandleRequest(r *http.Request, data []byte) (*http.Response, error) {
|
2020-08-03 20:01:38 +00:00
|
|
|
client := http.Client{}
|
|
|
|
req, err := http.NewRequest("POST", c.host, bytes.NewBuffer(data))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authentication", "Bearer "+c.jwt.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to construct lotus node rpc request %v", err)
|
|
|
|
}
|
|
|
|
return client.Do(req)
|
|
|
|
}
|