From 839c6631646d1aa29dccb1eccc848439321ec2a2 Mon Sep 17 00:00:00 2001 From: tarassh Date: Wed, 25 Sep 2024 20:52:44 +0300 Subject: [PATCH] Fix/request decoder (#36) * Fix * default signature type - []byte --- internal/light-client/events/listener.go | 26 ++++++++------------ internal/light-client/publisher/publisher.go | 2 +- internal/light-client/sampler/sampler.go | 4 +-- internal/light-client/utils/identity.go | 9 ++----- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/internal/light-client/events/listener.go b/internal/light-client/events/listener.go index 9f24720..4466298 100644 --- a/internal/light-client/events/listener.go +++ b/internal/light-client/events/listener.go @@ -13,7 +13,6 @@ import ( "github.com/covalenthq/das-ipfs-pinner/internal" "github.com/covalenthq/das-ipfs-pinner/internal/light-client/sampler" "github.com/covalenthq/das-ipfs-pinner/internal/light-client/utils" - "github.com/ethereum/go-ethereum/common" "github.com/filecoin-project/go-jsonrpc" "github.com/gorilla/websocket" logging "github.com/ipfs/go-log/v2" @@ -45,20 +44,17 @@ func (h *EventListener) Id() (string, error) { } // Sample is a placeholder for implementing sampling logic -func (h *EventListener) Sample(clientId, cid string, chainId, blockNum uint64, signature string) error { - request := &internal.SamplingRequest{ - ClientId: clientId, - Cid: cid, - ChainId: chainId, - BlockNum: blockNum, +func (h *EventListener) Sample(requestBytes []byte, signature []byte) error { + var request internal.SamplingRequest + if err := json.Unmarshal(requestBytes, &request); err != nil { + return fmt.Errorf("failed to unmarshal request: %w", err) } - err := h.verifyRequest(request, signature) - if err != nil { + if err := h.verifyRequest(&request, signature); err != nil { return err } - h.sampler.ProcessEvent(*request, signature) + h.sampler.ProcessEvent(request, signature) return nil } @@ -135,9 +131,9 @@ func (l *EventListener) handleSubscription(client *struct{ Subscribe func() erro } // buildHeaders constructs the HTTP headers for the JSON-RPC request -func (l *EventListener) buildHeaders(signature string) http.Header { +func (l *EventListener) buildHeaders(signature []byte) http.Header { requestHeader := http.Header{} - requestHeader.Add("X-LC-Signature", signature) + requestHeader.Add("X-LC-Signature", fmt.Sprintf("%x", signature)) requestHeader.Add("X-LC-Address", l.identity.GetAddress().Hex()) return requestHeader } @@ -149,15 +145,13 @@ func (l *EventListener) waitForShutdown() { <-quit } -func (l *EventListener) verifyRequest(request *internal.SamplingRequest, signature string) error { +func (l *EventListener) verifyRequest(request *internal.SamplingRequest, signature []byte) error { requestBytes, err := json.Marshal(request) if err != nil { return fmt.Errorf("failed to marshal request: %w", err) } - signatureBytes := common.Hex2Bytes(signature) - - ok, recoveredAddress := utils.VerifySignature(requestBytes, signatureBytes) + ok, recoveredAddress := utils.VerifySignature(requestBytes, signature) if !ok { return fmt.Errorf("failed to verify signature") } diff --git a/internal/light-client/publisher/publisher.go b/internal/light-client/publisher/publisher.go index b3e8464..8cf2729 100644 --- a/internal/light-client/publisher/publisher.go +++ b/internal/light-client/publisher/publisher.go @@ -49,7 +49,7 @@ func (p *Publisher) SendStoreRequest(request *internal.StoreRequest) error { } // Set the headers - req.Header.Set("X-LC-Signature", signature) + req.Header.Set("X-LC-Signature", fmt.Sprintf("%x", signature)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} diff --git a/internal/light-client/sampler/sampler.go b/internal/light-client/sampler/sampler.go index 7a76c17..909598b 100644 --- a/internal/light-client/sampler/sampler.go +++ b/internal/light-client/sampler/sampler.go @@ -48,7 +48,7 @@ func NewSampler(ipfsAddr string, samplingDelay uint, pub *publisher.Publisher) ( } // ProcessEvent handles events asynchronously by processing the provided CID. -func (s *Sampler) ProcessEvent(request internal.SamplingRequest, signature string) { +func (s *Sampler) ProcessEvent(request internal.SamplingRequest, signature []byte) { go func(request internal.SamplingRequest) { rawCid, err := cid.Decode(request.Cid) if err != nil { @@ -113,7 +113,7 @@ func (s *Sampler) ProcessEvent(request internal.SamplingRequest, signature strin storeReq := internal.StoreRequest{ SamplingReqest: request, - SamplingSignature: signature, + SamplingSignature: fmt.Sprintf("%x", signature), Status: res, Commitment: base64.StdEncoding.EncodeToString(commitment), Proof: base64.StdEncoding.EncodeToString(proof), diff --git a/internal/light-client/utils/identity.go b/internal/light-client/utils/identity.go index 6c321dd..308d5d1 100644 --- a/internal/light-client/utils/identity.go +++ b/internal/light-client/utils/identity.go @@ -2,7 +2,6 @@ package utils import ( "crypto/ecdsa" - "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -28,10 +27,6 @@ func (i *Identity) GetAddress() common.Address { return crypto.PubkeyToAddress(*i.GetPublicKey()) } -func (i *Identity) SignMessage(message []byte) (string, error) { - signBytes, err := SignMessage(i.privKey, message) - if err != nil { - return "", err - } - return fmt.Sprintf("%x", signBytes), nil +func (i *Identity) SignMessage(message []byte) ([]byte, error) { + return SignMessage(i.privKey, message) }