Skip to content

Commit 6c767f7

Browse files
authored
remove deviceID, change payloads to payload (#767)
1 parent be99dc4 commit 6c767f7

File tree

7 files changed

+53
-69
lines changed

7 files changed

+53
-69
lines changed

cmd/sign/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
req := &api.CreateTaskReq{
2121
ProjectID: "912",
2222
ProjectVersion: "v1.0.0",
23-
Payloads: []string{"{\"private_input\":\"14\", \"public_input\":\"3,34\", \"receipt_type\":\"Snark\"}"},
23+
Payload: "{\"private_input\":\"14\", \"public_input\":\"3,34\", \"receipt_type\":\"Snark\"}",
2424
}
2525
reqJson, _ := json.Marshal(req)
2626
fmt.Println(string(reqJson))

datasource/clickhouse.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package datasource
22

33
import (
44
"context"
5-
"encoding/json"
65
"math/big"
76

87
"github.com/ClickHouse/clickhouse-go/v2"
@@ -33,19 +32,15 @@ func (p *Clickhouse) Retrieve(taskIDs []common.Hash) ([]*task.Task, error) {
3332

3433
res := []*task.Task{}
3534
for i := range ts {
36-
ps := [][]byte{}
37-
if err := json.Unmarshal(ts[i].Payloads, &ps); err != nil {
38-
return nil, errors.Wrapf(err, "failed to unmarshal task payloads, task_id %v", ts[i].TaskID)
39-
}
40-
pid := new(big.Int)
41-
if _, ok := pid.SetString(ts[i].ProjectID, 10); !ok {
35+
pid, ok := new(big.Int).SetString(ts[i].ProjectID, 10)
36+
if !ok {
4237
return nil, errors.New("failed to decode project id string")
4338
}
4439
res = append(res, &task.Task{
4540
ID: common.BytesToHash(ts[i].TaskID),
4641
ProjectID: pid,
4742
ProjectVersion: ts[i].ProjectVersion,
48-
Payloads: ps,
43+
Payload: ts[i].Payload,
4944
DeviceID: common.BytesToAddress(ts[i].DeviceID),
5045
Signature: ts[i].Signature,
5146
})

e2e/util.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import (
2727

2828
func signMesssage(data []byte, projectID uint64, key *ecdsa.PrivateKey) ([]byte, error) {
2929
req := &api.CreateTaskReq{
30-
DeviceID: "did:io:" + crypto.PubkeyToAddress(key.PublicKey).String(),
3130
Nonce: uint64(time.Now().Unix()),
3231
ProjectID: strconv.Itoa(int(projectID)),
33-
Payloads: []string{hexutil.Encode(data)},
32+
Payload: hexutil.Encode(data),
3433
}
3534

3635
reqJson, err := json.Marshal(req)

service/apinode/api/http.go

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"bytes"
5-
"crypto/ecdsa"
65
"crypto/sha256"
76
"encoding/json"
87
"fmt"
@@ -34,13 +33,12 @@ func newErrResp(err error) *errResp {
3433
}
3534

3635
type CreateTaskReq struct {
37-
Nonce uint64 `json:"nonce" binding:"required"`
38-
DeviceID string `json:"deviceID" binding:"required"`
39-
ProjectID string `json:"projectID" binding:"required"`
40-
ProjectVersion string `json:"projectVersion,omitempty"`
41-
Payloads []string `json:"payloads" binding:"required"`
42-
Algorithm string `json:"algorithm,omitempty"` // Refer to the constants defined in JWT (JSON Web Token) https://jwt.io/
43-
Signature string `json:"signature,omitempty" binding:"required"`
36+
Nonce uint64 `json:"nonce" binding:"required"`
37+
ProjectID string `json:"projectID" binding:"required"`
38+
ProjectVersion string `json:"projectVersion,omitempty"`
39+
Payload string `json:"payload" binding:"required"`
40+
Algorithm string `json:"algorithm,omitempty"` // Refer to the constants defined in JWT (JSON Web Token) https://jwt.io/
41+
Signature string `json:"signature,omitempty" binding:"required"`
4442
}
4543

4644
type CreateTaskResp struct {
@@ -69,6 +67,11 @@ type httpServer struct {
6967
proverAddr string
7068
}
7169

70+
type recoverRes struct {
71+
addr common.Address
72+
sig []byte
73+
}
74+
7275
func (s *httpServer) createTask(c *gin.Context) {
7376
req := &CreateTaskReq{}
7477
if err := c.ShouldBindJSON(req); err != nil {
@@ -77,8 +80,8 @@ func (s *httpServer) createTask(c *gin.Context) {
7780
return
7881
}
7982

80-
pid := new(big.Int)
81-
if _, ok := pid.SetString(req.ProjectID, 10); !ok {
83+
pid, ok := new(big.Int).SetString(req.ProjectID, 10)
84+
if !ok {
8285
slog.Error("failed to decode project id string", "project_id", req.ProjectID)
8386
c.JSON(http.StatusBadRequest, newErrResp(errors.New("failed to decode project id string")))
8487
return
@@ -89,42 +92,42 @@ func (s *httpServer) createTask(c *gin.Context) {
8992
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode signature from hex format")))
9093
return
9194
}
92-
deviceAddr := common.HexToAddress(strings.TrimPrefix(req.DeviceID, "did:io:"))
93-
addr, sig, alg, err := recoverAddr(*req, sig, deviceAddr)
95+
96+
recovered, alg, err := recover(*req, sig)
9497
if err != nil {
9598
slog.Error("failed to recover public key", "error", err)
9699
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "invalid signature; could not recover public key")))
97100
return
98101
}
99-
100-
ok, err := s.db.IsDeviceApproved(pid, addr)
101-
if err != nil {
102-
slog.Error("failed to check device permission", "error", err)
103-
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to check device permission")))
104-
return
102+
var addr common.Address
103+
var approved bool
104+
for _, r := range recovered {
105+
ok, err := s.db.IsDeviceApproved(pid, r.addr)
106+
if err != nil {
107+
slog.Error("failed to check device permission", "error", err)
108+
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to check device permission")))
109+
return
110+
}
111+
if ok {
112+
approved = true
113+
addr = r.addr
114+
sig = r.sig
115+
break
116+
}
105117
}
106-
if !ok {
107-
slog.Error("device does not have permission", "project_id", pid.String(), "device_address", addr.String())
118+
if !approved {
119+
slog.Error("device does not have permission", "project_id", pid.String())
108120
c.JSON(http.StatusForbidden, newErrResp(errors.New("device does not have permission")))
109121
return
110122
}
111123

112-
payloadsB := make([][]byte, 0, len(req.Payloads))
113-
for _, p := range req.Payloads {
114-
d, err := hexutil.Decode(p)
115-
if err != nil {
116-
slog.Error("failed to decode payload from hex format", "error", err)
117-
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode payload from hex format")))
118-
return
119-
}
120-
payloadsB = append(payloadsB, d)
121-
}
122-
payloadsJ, err := json.Marshal(payloadsB)
124+
payload, err := hexutil.Decode(req.Payload)
123125
if err != nil {
124-
slog.Error("failed to marshal payloads", "error", err)
125-
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to marshal payloads")))
126+
slog.Error("failed to decode payload from hex format", "error", err)
127+
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode payload from hex format")))
126128
return
127129
}
130+
128131
taskID := crypto.Keccak256Hash(sig)
129132

130133
if err := s.db.CreateTask(
@@ -134,7 +137,7 @@ func (s *httpServer) createTask(c *gin.Context) {
134137
Nonce: req.Nonce,
135138
ProjectID: pid.String(),
136139
ProjectVersion: req.ProjectVersion,
137-
Payloads: payloadsJ,
140+
Payload: payload,
138141
Signature: sig,
139142
Algorithm: alg,
140143
},
@@ -175,40 +178,27 @@ func (s *httpServer) createTask(c *gin.Context) {
175178
})
176179
}
177180

178-
func recoverAddr(req CreateTaskReq, sig []byte, deviceAddr common.Address) (common.Address, []byte, string, error) {
181+
func recover(req CreateTaskReq, sig []byte) ([]*recoverRes, string, error) {
179182
req.Signature = ""
180183
reqJson, err := json.Marshal(req)
181184
if err != nil {
182-
return common.Address{}, nil, "", errors.Wrap(err, "failed to marshal request into json format")
185+
return nil, "", errors.Wrap(err, "failed to marshal request into json format")
183186
}
184187

185188
switch req.Algorithm {
186189
default:
187190
h := sha256.Sum256(reqJson)
188-
res := []struct {
189-
pk *ecdsa.PublicKey
190-
sig []byte
191-
}{}
191+
res := []*recoverRes{}
192192
rID := []uint8{0, 1}
193193
for _, id := range rID {
194194
ns := append(sig, byte(id))
195195
if pk, err := crypto.SigToPub(h[:], ns); err != nil {
196-
slog.Debug("failed to recover public key from signature", "error", err, "recover_id", id, "signature", hexutil.Encode(sig))
196+
return nil, "", errors.Wrapf(err, "failed to recover public key from signature, recover_id %d", id)
197197
} else {
198-
res = append(res, struct {
199-
pk *ecdsa.PublicKey
200-
sig []byte
201-
}{pk: pk, sig: ns})
202-
}
203-
}
204-
205-
for _, r := range res {
206-
addr := crypto.PubkeyToAddress(*r.pk)
207-
if bytes.Equal(addr.Bytes(), deviceAddr.Bytes()) {
208-
return addr, r.sig, "ES256", nil
198+
res = append(res, &recoverRes{addr: crypto.PubkeyToAddress(*pk), sig: ns})
209199
}
210200
}
211-
return common.Address{}, nil, "", errors.New("failed to recover public key from signature")
201+
return res, "ES256", nil
212202
}
213203
}
214204

service/apinode/db/clickhouse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Task struct {
1717
Nonce uint64 `ch:"nonce"`
1818
ProjectID string `ch:"project_id"`
1919
ProjectVersion string `ch:"project_version"`
20-
Payloads []byte `ch:"payloads"`
20+
Payload []byte `ch:"payload"`
2121
Signature []byte `ch:"signature"`
2222
Algorithm string `ch:"algorithm"`
2323
CreatedAt time.Time `ch:"create_at"`
@@ -52,7 +52,7 @@ func migrateCH(conn driver.Conn) error {
5252
nonce UInt64 NOT NULL,
5353
project_id String NOT NULL,
5454
project_version String NOT NULL,
55-
payloads Array(UInt8) NOT NULL,
55+
payload Array(UInt8) NOT NULL,
5656
signature Array(UInt8) NOT NULL,
5757
algorithm String NOT NULL,
5858
create_at DateTime NOT NULL

task/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Task struct {
1616
ProjectID *big.Int `json:"projectID"`
1717
ProjectVersion string `json:"projectVersion,omitempty"`
1818
DeviceID common.Address `json:"deviceID"`
19-
Payloads [][]byte `json:"payloads"`
19+
Payload []byte `json:"payload"`
2020
Signature []byte `json:"signature,omitempty"`
2121
}
2222

vm/vm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func (r *Handler) Handle(task *task.Task, vmTypeID uint64, code string, expParam
4545
resp, err := cli.ExecuteTask(context.Background(), &proto.ExecuteTaskRequest{
4646
ProjectID: task.ProjectID.Uint64(),
4747
TaskID: task.ID[:],
48-
Payloads: task.Payloads,
48+
Payloads: [][]byte{task.Payload},
4949
})
5050
if err != nil {
5151
slog.Error("failed to execute task", "project_id", task.ProjectID, "vm_type", vmTypeID,
52-
"task_id", task.ID, "binary", code, "payloads", task.Payloads, "err", err)
52+
"task_id", task.ID, "binary", code, "payloads", task.Payload, "err", err)
5353
return nil, errors.Wrap(err, "failed to execute vm instance")
5454
}
5555

0 commit comments

Comments
 (0)