@ -1,2 +1,5 @@ |
|||||
parsed_state.json |
parsed_state.json |
||||
new_state.json |
new_state.json |
||||
|
cmd/prevBatchContribution.json |
||||
|
cmd/contribution.json |
||||
|
cmd/contribution_receipt.json |
@ -0,0 +1,45 @@ |
|||||
|
package client |
||||
|
|
||||
|
import "fmt" |
||||
|
|
||||
|
type errorMsg struct { |
||||
|
Message string `json:"message"` |
||||
|
} |
||||
|
|
||||
|
type MsgStatus struct { |
||||
|
LobbySize uint64 `json:"lobby_size"` |
||||
|
NumContributions uint64 `json:"num_contributions"` |
||||
|
SequencerAddress string `json:"sequencer_address"` |
||||
|
} |
||||
|
|
||||
|
func (m *MsgStatus) String() string { |
||||
|
return fmt.Sprintf("Sequencer status:\n Lobby size: %d\n NumContributions: %d\n SequencerAddress: %s\n", |
||||
|
m.LobbySize, m.NumContributions, m.SequencerAddress) |
||||
|
} |
||||
|
|
||||
|
type MsgRequestLink struct { |
||||
|
EthAuthURL string `json:"eth_auth_url"` |
||||
|
GithubAuthURL string `json:"github_auth_url"` |
||||
|
} |
||||
|
|
||||
|
type IDToken struct { |
||||
|
Exp uint64 `json:"exp"` |
||||
|
Nickname string `json:"nickname"` |
||||
|
Provider string `json:"provider"` |
||||
|
Sub string `json:"sub"` |
||||
|
} |
||||
|
|
||||
|
type MsgAuthCallback struct { |
||||
|
IDToken IDToken `json:"id_token"` |
||||
|
SessionID string `json:"session_id"` |
||||
|
} |
||||
|
|
||||
|
type MsgContributeReceipt struct { |
||||
|
Receipt string `json:"receipt"` |
||||
|
Signature string `json:"signature"` |
||||
|
} |
||||
|
|
||||
|
func (m MsgContributeReceipt) String() string { |
||||
|
return fmt.Sprintf("Contribute Receipt:\n Receipt: %s\n Signature: %s\n", |
||||
|
m.Receipt, m.Signature) |
||||
|
} |
@ -0,0 +1,225 @@ |
|||||
|
package client |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"io" |
||||
|
"io/ioutil" |
||||
|
"kzgceremony" |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
|
type Client struct { |
||||
|
url string |
||||
|
c *http.Client |
||||
|
} |
||||
|
|
||||
|
func (c *Client) postWithAuth(url, contentType string, body io.Reader, bearer string) (resp *http.Response, err error) { |
||||
|
req, err := http.NewRequest("POST", url, body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
req.Header.Set("Content-Type", contentType) |
||||
|
req.Header.Add("Authorization", bearer) |
||||
|
return c.c.Do(req) |
||||
|
} |
||||
|
|
||||
|
func NewClient(sequencerURL string) *Client { |
||||
|
httpClient := &http.Client{} |
||||
|
return &Client{ |
||||
|
url: sequencerURL, |
||||
|
c: httpClient, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (c *Client) GetCurrentStatus() (*MsgStatus, error) { |
||||
|
resp, err := c.c.Get( |
||||
|
c.url + "/info/status") |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
var msg MsgStatus |
||||
|
err = json.Unmarshal(body, &msg) |
||||
|
return &msg, err |
||||
|
} |
||||
|
|
||||
|
func (c *Client) GetCurrentState() (*kzgceremony.State, error) { |
||||
|
resp, err := c.c.Get( |
||||
|
c.url + "/info/current_state") |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
var state *kzgceremony.State |
||||
|
err = json.Unmarshal(body, &state) |
||||
|
return state, err |
||||
|
} |
||||
|
|
||||
|
func (c *Client) GetRequestLink() (*MsgRequestLink, error) { |
||||
|
resp, err := c.c.Get( |
||||
|
c.url + "/auth/request_link") |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
switch resp.StatusCode { |
||||
|
case http.StatusBadRequest: |
||||
|
return nil, fmt.Errorf("Invalid request. Missing parameters.") |
||||
|
case http.StatusUnauthorized: |
||||
|
return nil, fmt.Errorf("Eth address doesn't match message signer, or account nonce is too low") |
||||
|
case http.StatusForbidden: |
||||
|
return nil, fmt.Errorf("Invalid HTTP method") |
||||
|
default: |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
var msg MsgRequestLink |
||||
|
err = json.Unmarshal(body, &msg) |
||||
|
return &msg, err |
||||
|
} |
||||
|
|
||||
|
func (c *Client) PostAuthCallback() (*MsgRequestLink, error) { |
||||
|
resp, err := c.c.Get( |
||||
|
c.url + "/auth/request_link") |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
switch resp.StatusCode { |
||||
|
case http.StatusBadRequest: |
||||
|
return nil, fmt.Errorf("Invalid request. Missing parameters.") |
||||
|
case http.StatusUnauthorized: |
||||
|
return nil, fmt.Errorf("Eth address doesn't match message signer, or account nonce is too low") |
||||
|
case http.StatusForbidden: |
||||
|
return nil, fmt.Errorf("Invalid HTTP method") |
||||
|
default: |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
var msg MsgRequestLink |
||||
|
err = json.Unmarshal(body, &msg) |
||||
|
return &msg, err |
||||
|
} |
||||
|
|
||||
|
func (c *Client) PostTryContribute(sessionID string) (*kzgceremony.BatchContribution, bool, error) { |
||||
|
bearer := "Bearer " + sessionID |
||||
|
resp, err := c.postWithAuth( |
||||
|
c.url+"/lobby/try_contribute", "application/json", nil, bearer) |
||||
|
if err != nil { |
||||
|
return nil, false, err |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, false, err |
||||
|
} |
||||
|
|
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
fmt.Println(string(body)) |
||||
|
switch resp.StatusCode { |
||||
|
case http.StatusBadRequest: |
||||
|
return nil, true, fmt.Errorf("call came to early. rate limited") |
||||
|
case http.StatusUnauthorized: |
||||
|
return nil, false, fmt.Errorf("unkown session id. unauthorized access") |
||||
|
default: |
||||
|
return nil, false, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
err = ioutil.WriteFile("prevBatchContribution.json", body, 0600) |
||||
|
if err != nil { |
||||
|
return nil, false, err |
||||
|
} |
||||
|
bc := &kzgceremony.BatchContribution{} |
||||
|
err = json.Unmarshal(body, bc) |
||||
|
return bc, false, err |
||||
|
} |
||||
|
|
||||
|
func (c *Client) PostAbortContribution(sessionID string) ([]byte, error) { |
||||
|
bearer := "Bearer " + sessionID |
||||
|
resp, err := c.postWithAuth( |
||||
|
c.url+"/contribution/abort", "application/json", nil, bearer) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
fmt.Println("body", string(body)) |
||||
|
|
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
switch resp.StatusCode { |
||||
|
case http.StatusBadRequest: |
||||
|
return nil, fmt.Errorf("invalid request") |
||||
|
case http.StatusUnauthorized: |
||||
|
return nil, fmt.Errorf("unkown session id. unauthorized access") |
||||
|
default: |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return body, nil |
||||
|
} |
||||
|
|
||||
|
func (c *Client) PostContribute(sessionID string, bc *kzgceremony.BatchContribution) (*MsgContributeReceipt, error) { |
||||
|
bearer := "Bearer " + sessionID |
||||
|
|
||||
|
jsonBC, err := json.Marshal(bc) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
resp, err := c.postWithAuth( |
||||
|
c.url+"/contribute", "application/json", bytes.NewBuffer(jsonBC), bearer) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
switch resp.StatusCode { |
||||
|
case http.StatusBadRequest: |
||||
|
return nil, fmt.Errorf("invalid request") |
||||
|
default: |
||||
|
return nil, fmt.Errorf("unexpected http code: %d", resp.StatusCode) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
var msg MsgContributeReceipt |
||||
|
err = json.Unmarshal(body, &msg) |
||||
|
return &msg, err |
||||
|
} |