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.

225 lines
5.3 KiB

  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "kzgceremony"
  9. "net/http"
  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. func (c *Client) PostTryContribute(sessionID string) (*kzgceremony.BatchContribution, bool, error) {
  118. bearer := "Bearer " + sessionID
  119. resp, err := c.postWithAuth(
  120. c.url+"/lobby/try_contribute", "application/json", nil, bearer)
  121. if err != nil {
  122. return nil, false, err
  123. }
  124. body, err := ioutil.ReadAll(resp.Body)
  125. if err != nil {
  126. return nil, false, err
  127. }
  128. if resp.StatusCode != http.StatusOK {
  129. fmt.Println(string(body))
  130. switch resp.StatusCode {
  131. case http.StatusBadRequest:
  132. return nil, true, fmt.Errorf("call came to early. rate limited")
  133. case http.StatusUnauthorized:
  134. return nil, false, fmt.Errorf("unkown session id. unauthorized access")
  135. default:
  136. return nil, false, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  137. }
  138. }
  139. err = ioutil.WriteFile("prevBatchContribution.json", body, 0600)
  140. if err != nil {
  141. return nil, false, err
  142. }
  143. bc := &kzgceremony.BatchContribution{}
  144. err = json.Unmarshal(body, bc)
  145. return bc, false, err
  146. }
  147. func (c *Client) PostAbortContribution(sessionID string) ([]byte, error) {
  148. bearer := "Bearer " + sessionID
  149. resp, err := c.postWithAuth(
  150. c.url+"/contribution/abort", "application/json", nil, bearer)
  151. if err != nil {
  152. return nil, err
  153. }
  154. body, err := ioutil.ReadAll(resp.Body)
  155. if err != nil {
  156. return nil, err
  157. }
  158. fmt.Println("body", string(body))
  159. if resp.StatusCode != http.StatusOK {
  160. switch resp.StatusCode {
  161. case http.StatusBadRequest:
  162. return nil, fmt.Errorf("invalid request")
  163. case http.StatusUnauthorized:
  164. return nil, fmt.Errorf("unkown session id. unauthorized access")
  165. default:
  166. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  167. }
  168. }
  169. return body, nil
  170. }
  171. func (c *Client) PostContribute(sessionID string, bc *kzgceremony.BatchContribution) (*MsgContributeReceipt, error) {
  172. bearer := "Bearer " + sessionID
  173. jsonBC, err := json.Marshal(bc)
  174. if err != nil {
  175. return nil, err
  176. }
  177. resp, err := c.postWithAuth(
  178. c.url+"/contribute", "application/json", bytes.NewBuffer(jsonBC), bearer)
  179. if err != nil {
  180. return nil, err
  181. }
  182. if resp.StatusCode != http.StatusOK {
  183. switch resp.StatusCode {
  184. case http.StatusBadRequest:
  185. return nil, fmt.Errorf("invalid request")
  186. default:
  187. return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode)
  188. }
  189. }
  190. body, err := ioutil.ReadAll(resp.Body)
  191. if err != nil {
  192. return nil, err
  193. }
  194. var msg MsgContributeReceipt
  195. err = json.Unmarshal(body, &msg)
  196. return &msg, err
  197. }