// Package rpc is the bitcoin-cli vocabulary the gate tests use. // // It is a test oracle, not the product: the service pulls from peers and never // talks to a node. The tests compare what the service computes against what a // node with a block filter index says, which is the only independent // implementation of the filter, the filter header chain and the merkle root // this machine has. package rpc import ( "bytes" "encoding/base64" "encoding/hex" "encoding/json" "fmt" "io" "net/http" "os" "strings" "time" ) // Client is one node reached over its JSON-RPC port. type Client struct { url string auth string http *http.Client } // DialCookie opens a session with credentials from a node's .cookie file. func DialCookie(port int, cookiePath string) (*Client, error) { raw, err := os.ReadFile(cookiePath) if err != nil { return nil, fmt.Errorf("rpc: read cookie: %w", err) } return Dial(port, strings.TrimSpace(string(raw))) } // Dial opens a session with user:pass credentials. func Dial(port int, userPass string) (*Client, error) { if !strings.Contains(userPass, ":") { return nil, fmt.Errorf("rpc: credentials are not user:pass") } return &Client{ url: fmt.Sprintf("http://127.0.0.1:%d", port), auth: base64.StdEncoding.EncodeToString([]byte(userPass)), http: &http.Client{Timeout: 60 * time.Second}, }, nil } // Call runs one method and unmarshals its result into out. func (c *Client) Call(method string, params []any, out any) error { if params == nil { params = []any{} } body, err := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "method": method, "params": params}) if err != nil { return err } req, err := http.NewRequest(http.MethodPost, c.url, bytes.NewReader(body)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Basic "+c.auth) resp, err := c.http.Do(req) if err != nil { return fmt.Errorf("rpc %s: %w", method, err) } defer resp.Body.Close() raw, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("rpc %s: %w", method, err) } var envelope struct { Result json.RawMessage `json:"result"` Error *struct { Code int `json:"code"` Message string `json:"message"` } `json:"error"` } if err := json.Unmarshal(raw, &envelope); err != nil { return fmt.Errorf("rpc %s: %d bytes that are not JSON: %w", method, len(raw), err) } if envelope.Error != nil { return fmt.Errorf("rpc %s: %d %s", method, envelope.Error.Code, envelope.Error.Message) } if out == nil { return nil } if err := json.Unmarshal(envelope.Result, out); err != nil { return fmt.Errorf("rpc %s: result: %w", method, err) } return nil } // BlockCount is the node's chain height. func (c *Client) BlockCount() (uint32, error) { var n uint32 err := c.Call("getblockcount", nil, &n) return n, err } // BlockHash is the hash at a height. func (c *Client) BlockHash(height uint32) (string, error) { var h string err := c.Call("getblockhash", []any{height}, &h) return strings.ToLower(h), err } // BlockRaw is the block at a hash in wire form. func (c *Client) BlockRaw(hash string) ([]byte, error) { var raw string if err := c.Call("getblock", []any{hash, 0}, &raw); err != nil { return nil, err } return hex.DecodeString(raw) } // Filter is the BIP157 basic filter at a block and the header it folds to. func (c *Client) Filter(hash string) (filter, header []byte, err error) { var out struct { Filter string `json:"filter"` Header string `json:"header"` } if err := c.Call("getblockfilter", []any{hash}, &out); err != nil { return nil, nil, err } filter, err = hex.DecodeString(out.Filter) if err != nil { return nil, nil, err } header, err = hex.DecodeString(out.Header) return filter, header, err } // RawTx is the raw hex of a wallet transaction. func (c *Client) RawTx(txid string) ([]byte, error) { var raw string if err := c.Call("getrawtransaction", []any{txid, false}, &raw); err != nil { return nil, err } return hex.DecodeString(raw) } // Stop asks the node to shut down. func (c *Client) Stop() error { return c.Call("stop", nil, nil) }