github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/docs/Privacy/Constellation/constellation-go.md (about)

     1  ```go
     2  package constellation
     3  
     4  import (
     5  	"bytes"
     6  	"encoding/base64"
     7  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  	"github.com/tv42/httpunix"
    11  	"io"
    12  	"io/ioutil"
    13  	"net/http"
    14  	"os"
    15  	"os/exec"
    16  	"strings"
    17  	"time"
    18  )
    19  
    20  func launchNode(cfgPath string) (*exec.Cmd, error) {
    21  	cmd := exec.Command("constellation-node", cfgPath)
    22  	stderr, err := cmd.StderrPipe()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	go io.Copy(os.Stderr, stderr)
    27  	if err := cmd.Start(); err != nil {
    28  		return nil, err
    29  	}
    30  	time.Sleep(100 * time.Millisecond)
    31  	return cmd, nil
    32  }
    33  
    34  func unixTransport(socketPath string) *httpunix.Transport {
    35  	t := &httpunix.Transport{
    36  		DialTimeout:           1 * time.Second,
    37  		RequestTimeout:        5 * time.Second,
    38  		ResponseHeaderTimeout: 5 * time.Second,
    39  	}
    40  	t.RegisterLocation("c", socketPath)
    41  	return t
    42  }
    43  
    44  func unixClient(socketPath string) *http.Client {
    45  	return &http.Client{
    46  		Transport: unixTransport(socketPath),
    47  	}
    48  }
    49  
    50  func RunNode(socketPath string) error {
    51  	c := unixClient(socketPath)
    52  	res, err := c.Get("http+unix://c/upcheck")
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if res.StatusCode == 200 {
    57  		return nil
    58  	}
    59  	return errors.New("Constellation Node API did not respond to upcheck request")
    60  }
    61  
    62  type Client struct {
    63  	httpClient *http.Client
    64  }
    65  
    66  func (c *Client) doJson(path string, apiReq interface{}) (*http.Response, error) {
    67  	buf := new(bytes.Buffer)
    68  	err := json.NewEncoder(buf).Encode(apiReq)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	req, err := http.NewRequest("POST", "http+unix://c/"+path, buf)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	req.Header.Set("Content-Type", "application/json")
    77  	res, err := c.httpClient.Do(req)
    78  	if err == nil && res.StatusCode != 200 {
    79  		return nil, fmt.Errorf("Non-200 status code: %+v", res)
    80  	}
    81  	return res, err
    82  }
    83  
    84  func (c *Client) SendPayload(pl []byte, b64From string, b64To []string) ([]byte, error) {
    85  	buf := bytes.NewBuffer(pl)
    86  	req, err := http.NewRequest("POST", "http+unix://c/sendraw", buf)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	if b64From != "" {
    91  		req.Header.Set("c11n-from", b64From)
    92  	}
    93  	req.Header.Set("c11n-to", strings.Join(b64To, ","))
    94  	req.Header.Set("Content-Type", "application/octet-stream")
    95  	res, err := c.httpClient.Do(req)
    96  
    97  	if res != nil {
    98  		defer res.Body.Close()
    99  	}
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	if res.StatusCode != 200 {
   104  		return nil, fmt.Errorf("Non-200 status code: %+v", res)
   105  	}
   106  
   107  	return ioutil.ReadAll(base64.NewDecoder(base64.StdEncoding, res.Body))
   108  }
   109  
   110  func (c *Client) SendSignedPayload(signedPayload []byte, b64To []string) ([]byte, error) {
   111  	buf := bytes.NewBuffer(signedPayload)
   112  	req, err := http.NewRequest("POST", "http+unix://c/sendsignedtx", buf)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	req.Header.Set("c11n-to", strings.Join(b64To, ","))
   118  	req.Header.Set("Content-Type", "application/octet-stream")
   119  	res, err := c.httpClient.Do(req)
   120  
   121  	if res != nil {
   122  		defer res.Body.Close()
   123  	}
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	if res.StatusCode != 200 {
   128  		return nil, fmt.Errorf("Non-200 status code: %+v", res)
   129  	}
   130  
   131  	return ioutil.ReadAll(base64.NewDecoder(base64.StdEncoding, res.Body))
   132  }
   133  
   134  func (c *Client) ReceivePayload(key []byte) ([]byte, error) {
   135  	req, err := http.NewRequest("GET", "http+unix://c/receiveraw", nil)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	req.Header.Set("c11n-key", base64.StdEncoding.EncodeToString(key))
   140  	res, err := c.httpClient.Do(req)
   141  
   142  	if res != nil {
   143  		defer res.Body.Close()
   144  	}
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	if res.StatusCode != 200 {
   149  		return nil, fmt.Errorf("Non-200 status code: %+v", res)
   150  	}
   151  
   152  	return ioutil.ReadAll(res.Body)
   153  }
   154  
   155  func NewClient(socketPath string) (*Client, error) {
   156  	return &Client{
   157  		httpClient: unixClient(socketPath),
   158  	}, nil
   159  }
   160  ```