| giolekva | b64297c | 2021-12-13 14:36:32 +0400 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | ) |
| 9 | |
| 10 | type VPNApiClient struct { |
| 11 | addr string |
| 12 | } |
| 13 | |
| 14 | type signReq struct { |
| 15 | Message []byte `json:"message"` |
| 16 | } |
| 17 | |
| 18 | type signResp struct { |
| 19 | Signature []byte `json:"signature"` |
| 20 | } |
| 21 | |
| 22 | func (c *VPNApiClient) Sign(message []byte) ([]byte, error) { |
| 23 | var data bytes.Buffer |
| 24 | if err := json.NewEncoder(&data).Encode(signReq{message}); err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | client := &http.Client{} |
| 28 | r, err := client.Post(c.addr+"/api/sign", "application/json", &data) |
| 29 | if err != nil { |
| 30 | fmt.Println(111111) |
| 31 | return nil, err |
| 32 | } |
| 33 | resp := &signResp{} |
| 34 | if err := json.NewDecoder(r.Body).Decode(resp); err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | return resp.Signature, nil |
| 38 | } |