Skip to content

Commit

Permalink
change req []byte to hex string (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Dec 16, 2024
1 parent 69dc91c commit 5498873
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
7 changes: 4 additions & 3 deletions e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -30,7 +31,7 @@ func signMesssage(data []byte, projectID uint64, key *ecdsa.PrivateKey) ([]byte,
req := &api.CreateTaskReq{
Nonce: uint64(time.Now().Unix()),
ProjectID: strconv.Itoa(int(projectID)),
Payload: data,
Payload: hexutil.Encode(data),
}

reqJson, err := json.Marshal(req)
Expand All @@ -39,7 +40,7 @@ func signMesssage(data []byte, projectID uint64, key *ecdsa.PrivateKey) ([]byte,
}

h := sha256.Sum256(reqJson)
value := gjson.GetBytes(req.Payload, "timestamp")
value := gjson.GetBytes(data, "timestamp")
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, value.Uint()); err != nil {
return nil, errors.New("failed to convert uint64 to bytes array")
Expand All @@ -54,7 +55,7 @@ func signMesssage(data []byte, projectID uint64, key *ecdsa.PrivateKey) ([]byte,
}
sig = sig[:len(sig)-1]

req.Signature = sig
req.Signature = hexutil.Encode(sig)

return json.Marshal(req)
}
Expand Down
32 changes: 22 additions & 10 deletions service/apinode/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func newErrResp(err error) *errResp {
}

// TODO move to project file
// mainnet 6
// testnet 923
var pebbleProject = project.Config{
SignedKeys: []project.SignedKey{{Name: "timestamp", Type: "uint64"}},
SignatureAlgorithm: "ecdsa",
Expand All @@ -50,8 +52,8 @@ type CreateTaskReq struct {
Nonce uint64 `json:"nonce" binding:"required"`
ProjectID string `json:"projectID" binding:"required"`
ProjectVersion string `json:"projectVersion,omitempty"`
Payload []byte `json:"payload" binding:"required"`
Signature []byte `json:"signature,omitempty" binding:"required"`
Payload string `json:"payload" binding:"required"`
Signature string `json:"signature,omitempty" binding:"required"`
}

type CreateTaskResp struct {
Expand Down Expand Up @@ -93,21 +95,32 @@ func (s *httpServer) createTask(c *gin.Context) {
c.JSON(http.StatusBadRequest, newErrResp(errors.New("failed to decode project id string")))
return
}
if ok := gjson.ValidBytes(req.Payload); !ok {
payload, err := hexutil.Decode(req.Payload)
if err != nil {
slog.Error("failed to decode payload", "error", err)
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode payload")))
return
}
sig, err := hexutil.Decode(req.Signature)
if err != nil {
slog.Error("failed to decode signature", "error", err)
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode signature")))
return
}
if ok := gjson.ValidBytes(payload); !ok {
slog.Error("failed to validate payload in json format")
c.JSON(http.StatusBadRequest, newErrResp(errors.New("failed to validate payload in json format")))
return
}

recovered, sigAlg, hashAlg, err := recover(*req, &pebbleProject)
recovered, sigAlg, hashAlg, err := recover(*req, &pebbleProject, sig, payload)
if err != nil {
slog.Error("failed to recover public key", "error", err)
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "invalid signature; could not recover public key")))
return
}
var addr common.Address
var approved bool
var sig []byte
for _, r := range recovered {
slog.Info("recovered address", "project_id", pid.String(), "address", r.addr.String())
ok, err := s.db.IsDeviceApproved(pid, r.addr)
Expand Down Expand Up @@ -138,7 +151,7 @@ func (s *httpServer) createTask(c *gin.Context) {
Nonce: req.Nonce,
ProjectID: pid.String(),
ProjectVersion: req.ProjectVersion,
Payload: string(req.Payload),
Payload: string(payload),
Signature: hexutil.Encode(sig),
SignatureAlgorithm: sigAlg,
HashAlgorithm: hashAlg,
Expand All @@ -156,12 +169,11 @@ func (s *httpServer) createTask(c *gin.Context) {
})
}

func recover(req CreateTaskReq, cfg *project.Config) (res []*struct {
func recover(req CreateTaskReq, cfg *project.Config, sig, payload []byte) (res []*struct {
addr common.Address
sig []byte
}, sigAlg, hashAlg string, err error) {
sig := req.Signature
req.Signature = nil
req.Signature = ""
reqJson, err := json.Marshal(req)
if err != nil {
return nil, "", "", errors.Wrap(err, "failed to marshal request into json format")
Expand All @@ -176,7 +188,7 @@ func recover(req CreateTaskReq, cfg *project.Config) (res []*struct {
d = append(d, h1[:]...)

for _, k := range cfg.SignedKeys {
value := gjson.GetBytes(req.Payload, k.Name)
value := gjson.GetBytes(payload, k.Name)
switch k.Type {
case "uint64":
buf := new(bytes.Buffer)
Expand Down

0 comments on commit 5498873

Please sign in to comment.