You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.5 KiB

  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. kzgceremony "github.com/arnaucube/eth-kzg-ceremony-alt"
  10. )
  11. type Client struct {
  12. url string
  13. c *http.Client
  14. }
  15. func (c *Client) postWithAuth(url, contentType string, body io.Reader, bearer string) (resp *http.Response, err error) {
  16. req, err := http.NewRequest("POST", url, body)
  17. if err != nil {
  18. return nil, err
  19. }
  20. req.Header.Set("Content-Type", contentType)
  21. req.Header.Add("Authorization", bearer)
  22. return c.c.Do(req)
  23. }
  24. func NewClient(sequencerURL string) *Client {
  25. httpClient := &http.Client{}
  26. return &Client{
  27. url: sequencerURL,
  28. c: httpClient,
  29. }
  30. }
  31. func (c *Client) GetCurrentStatus() (*MsgStatus, error) {
  32. resp, err := c.c.Get(
  33. c.url + "/info/status")
  34. if err != nil {
  35. return nil, err
  36. }
  37. if resp.StatusCode != http.StatusOK {
  38. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  39. }
  40. body, err := ioutil.ReadAll(resp.Body)
  41. if err != nil {
  42. return nil, err
  43. }
  44. var msg MsgStatus
  45. err = json.Unmarshal(body, &msg)
  46. return &msg, err
  47. }
  48. func (c *Client) GetCurrentState() (*kzgceremony.State, error) {
  49. resp, err := c.c.Get(
  50. c.url + "/info/current_state")
  51. if err != nil {
  52. return nil, err
  53. }
  54. if resp.StatusCode != http.StatusOK {
  55. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  56. }
  57. body, err := ioutil.ReadAll(resp.Body)
  58. if err != nil {
  59. return nil, err
  60. }
  61. var state *kzgceremony.State
  62. err = json.Unmarshal(body, &state)
  63. return state, err
  64. }
  65. func (c *Client) GetRequestLink() (*MsgRequestLink, error) {
  66. resp, err := c.c.Get(
  67. c.url + "/auth/request_link")
  68. if err != nil {
  69. return nil, err
  70. }
  71. if resp.StatusCode != http.StatusOK {
  72. switch resp.StatusCode {
  73. case http.StatusBadRequest:
  74. return nil, fmt.Errorf("Invalid request. Missing parameters.")
  75. case http.StatusUnauthorized:
  76. return nil, fmt.Errorf("Eth address doesn't match message signer, or account nonce is too low")
  77. case http.StatusForbidden:
  78. return nil, fmt.Errorf("Invalid HTTP method")
  79. default:
  80. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  81. }
  82. }
  83. body, err := ioutil.ReadAll(resp.Body)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var msg MsgRequestLink
  88. err = json.Unmarshal(body, &msg)
  89. return &msg, err
  90. }
  91. func (c *Client) PostAuthCallback() (*MsgRequestLink, error) {
  92. resp, err := c.c.Get(
  93. c.url + "/auth/request_link")
  94. if err != nil {
  95. return nil, err
  96. }
  97. if resp.StatusCode != http.StatusOK {
  98. switch resp.StatusCode {
  99. case http.StatusBadRequest:
  100. return nil, fmt.Errorf("Invalid request. Missing parameters.")
  101. case http.StatusUnauthorized:
  102. return nil, fmt.Errorf("Eth address doesn't match message signer, or account nonce is too low")
  103. case http.StatusForbidden:
  104. return nil, fmt.Errorf("Invalid HTTP method")
  105. default:
  106. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  107. }
  108. }
  109. body, err := ioutil.ReadAll(resp.Body)
  110. if err != nil {
  111. return nil, err
  112. }
  113. var msg MsgRequestLink
  114. err = json.Unmarshal(body, &msg)
  115. return &msg, err
  116. }
  117. type Status int
  118. const (
  119. StatusReauth = Status(iota)
  120. StatusError
  121. StatusWait
  122. StatusProceed
  123. )
  124. func (c *Client) PostTryContribute(sessionID string) (*kzgceremony.BatchContribution, Status, error) {
  125. bearer := "Bearer " + sessionID
  126. resp, err := c.postWithAuth(
  127. c.url+"/lobby/try_contribute", "application/json", nil, bearer)
  128. if err != nil {
  129. return nil, StatusError, err
  130. }
  131. body, err := ioutil.ReadAll(resp.Body)
  132. if err != nil {
  133. return nil, StatusError, err
  134. }
  135. if resp.StatusCode != http.StatusOK {
  136. fmt.Println(string(body))
  137. switch resp.StatusCode {
  138. case http.StatusBadRequest:
  139. return nil, StatusWait, fmt.Errorf("call came to early. rate limited")
  140. case http.StatusUnauthorized:
  141. return nil, StatusReauth, fmt.Errorf("unkown session id. unauthorized access")
  142. default:
  143. return nil, StatusWait, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  144. }
  145. }
  146. err = ioutil.WriteFile("prevBatchContribution.json", body, 0600)
  147. if err != nil {
  148. return nil, StatusError, err
  149. }
  150. bc := &kzgceremony.BatchContribution{}
  151. err = json.Unmarshal(body, bc)
  152. return bc, StatusError, err
  153. }
  154. func (c *Client) PostAbortContribution(sessionID string) ([]byte, error) {
  155. bearer := "Bearer " + sessionID
  156. resp, err := c.postWithAuth(
  157. c.url+"/contribution/abort", "application/json", nil, bearer)
  158. if err != nil {
  159. return nil, err
  160. }
  161. body, err := ioutil.ReadAll(resp.Body)
  162. if err != nil {
  163. return nil, err
  164. }
  165. fmt.Println("body", string(body))
  166. if resp.StatusCode != http.StatusOK {
  167. switch resp.StatusCode {
  168. case http.StatusBadRequest:
  169. return nil, fmt.Errorf("invalid request")
  170. case http.StatusUnauthorized:
  171. return nil, fmt.Errorf("unkown session id. unauthorized access")
  172. default:
  173. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  174. }
  175. }
  176. return body, nil
  177. }
  178. func (c *Client) PostContribute(sessionID string, bc *kzgceremony.BatchContribution) (*MsgContributeReceipt, error) {
  179. bearer := "Bearer " + sessionID
  180. jsonBC, err := json.Marshal(bc)
  181. if err != nil {
  182. return nil, err
  183. }
  184. resp, err := c.postWithAuth(
  185. c.url+"/contribute", "application/json", bytes.NewBuffer(jsonBC), bearer)
  186. if err != nil {
  187. return nil, err
  188. }
  189. if resp.StatusCode != http.StatusOK {
  190. switch resp.StatusCode {
  191. case http.StatusBadRequest:
  192. return nil, fmt.Errorf("invalid request")
  193. default:
  194. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  195. }
  196. }
  197. body, err := ioutil.ReadAll(resp.Body)
  198. if err != nil {
  199. return nil, err
  200. }
  201. var msg MsgContributeReceipt
  202. err = json.Unmarshal(body, &msg)
  203. return &msg, err
  204. }