github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/rpc/apimiddleware/custom_hooks.go (about)

     1  package apimiddleware
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/http"
     8  
     9  	"github.com/ethereum/go-ethereum/common/hexutil"
    10  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    11  	"github.com/prysmaticlabs/prysm/shared/gateway"
    12  )
    13  
    14  // https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolAttestations expects posting a top-level array.
    15  // We make it more proto-friendly by wrapping it in a struct with a 'data' field.
    16  func wrapAttestationsArray(endpoint gateway.Endpoint, _ http.ResponseWriter, req *http.Request) gateway.ErrorJson {
    17  	if _, ok := endpoint.PostRequest.(*submitAttestationRequestJson); ok {
    18  		atts := make([]*attestationJson, 0)
    19  		if err := json.NewDecoder(req.Body).Decode(&atts); err != nil {
    20  			return gateway.InternalServerErrorWithMessage(err, "could not decode attestations array")
    21  		}
    22  		j := &submitAttestationRequestJson{Data: atts}
    23  		b, err := json.Marshal(j)
    24  		if err != nil {
    25  			return gateway.InternalServerErrorWithMessage(err, "could not marshal wrapped attestations array")
    26  		}
    27  		req.Body = ioutil.NopCloser(bytes.NewReader(b))
    28  	}
    29  	return nil
    30  }
    31  
    32  // Posted graffiti needs to have length of 32 bytes, but client is allowed to send data of any length.
    33  func prepareGraffiti(endpoint gateway.Endpoint, _ http.ResponseWriter, _ *http.Request) gateway.ErrorJson {
    34  	if block, ok := endpoint.PostRequest.(*beaconBlockContainerJson); ok {
    35  		b := bytesutil.ToBytes32([]byte(block.Message.Body.Graffiti))
    36  		block.Message.Body.Graffiti = hexutil.Encode(b[:])
    37  	}
    38  	return nil
    39  }