Skip to content

Commit

Permalink
Fix/request decoder (#36)
Browse files Browse the repository at this point in the history
* Fix

* default signature type - []byte
  • Loading branch information
tarassh authored Sep 25, 2024
1 parent 1af5547 commit 839c663
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
26 changes: 10 additions & 16 deletions internal/light-client/events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/light-client/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions internal/light-client/sampler/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 2 additions & 7 deletions internal/light-client/utils/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"crypto/ecdsa"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -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)
}

0 comments on commit 839c663

Please sign in to comment.