Skip to content

Commit d1dd439

Browse files
authored
support multi hash algorithm (#761)
1 parent 1130e61 commit d1dd439

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

service/apinode/api/http.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"bytes"
55
"crypto/ecdsa"
6+
"crypto/sha256"
67
"encoding/json"
78
"fmt"
89
"io"
@@ -32,11 +33,12 @@ func newErrResp(err error) *errResp {
3233
}
3334

3435
type CreateTaskReq struct {
35-
Nonce uint64 `json:"nonce" binding:"required"`
36-
ProjectID uint64 `json:"projectID" binding:"required"`
37-
ProjectVersion string `json:"projectVersion"`
38-
Payloads []string `json:"payloads" binding:"required"`
39-
Signature string `json:"signature,omitempty" binding:"required"`
36+
Nonce uint64 `json:"nonce" binding:"required"`
37+
ProjectID uint64 `json:"projectID" binding:"required"`
38+
ProjectVersion string `json:"projectVersion,omitempty"`
39+
Payloads []string `json:"payloads" binding:"required"`
40+
HashAlgorithm string `json:"hashAlgorithm,omitempty"`
41+
Signature string `json:"signature,omitempty" binding:"required"`
4042
}
4143

4244
type CreateTaskResp struct {
@@ -79,7 +81,7 @@ func (s *httpServer) createTask(c *gin.Context) {
7981
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "failed to decode signature from hex format")))
8082
return
8183
}
82-
pubKey, err := recoverPubkey(*req, sig)
84+
pubKey, hashAlg, err := recoverPubkey(*req, sig)
8385
if err != nil {
8486
slog.Error("failed to recover public key", "error", err)
8587
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "invalid signature; could not recover public key")))
@@ -126,6 +128,7 @@ func (s *httpServer) createTask(c *gin.Context) {
126128
ProjectVersion: req.ProjectVersion,
127129
Payloads: payloadsJ,
128130
Signature: sig,
131+
HashAlgorithm: hashAlg,
129132
},
130133
); err != nil {
131134
slog.Error("failed to create task to persistence layer", "error", err)
@@ -164,19 +167,32 @@ func (s *httpServer) createTask(c *gin.Context) {
164167
})
165168
}
166169

167-
func recoverPubkey(req CreateTaskReq, sig []byte) (*ecdsa.PublicKey, error) {
170+
func recoverPubkey(req CreateTaskReq, sig []byte) (*ecdsa.PublicKey, string, error) {
168171
req.Signature = ""
169172
reqJson, err := json.Marshal(req)
170173
if err != nil {
171-
return nil, errors.Wrap(err, "failed to marshal request into json format")
174+
return nil, "", errors.Wrap(err, "failed to marshal request into json format")
172175
}
173176

174-
h := crypto.Keccak256Hash(reqJson)
175-
sigpk, err := crypto.SigToPub(h.Bytes(), sig)
177+
var alg string
178+
var h []byte
179+
180+
switch req.HashAlgorithm {
181+
case "sha256":
182+
hash := sha256.New()
183+
hash.Write(reqJson)
184+
h = hash.Sum(nil)
185+
alg = "sha256"
186+
default:
187+
h = crypto.Keccak256Hash(reqJson).Bytes()
188+
alg = "keccak256"
189+
}
190+
191+
sigpk, err := crypto.SigToPub(h, sig)
176192
if err != nil {
177-
return nil, errors.Wrap(err, "failed to recover public key from signature")
193+
return nil, "", errors.Wrap(err, "failed to recover public key from signature")
178194
}
179-
return sigpk, nil
195+
return sigpk, alg, nil
180196
}
181197

182198
func (s *httpServer) getProverTaskState(taskID string) (*StateLog, error) {

service/apinode/db/clickhouse.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Task struct {
1919
ProjectVersion string `ch:"project_version"`
2020
Payloads []byte `ch:"payloads"`
2121
Signature []byte `ch:"signature"`
22+
HashAlgorithm string `ch:"hash_algorithm"`
2223
CreatedAt time.Time `ch:"create_at"`
2324
}
2425

@@ -44,21 +45,22 @@ func (p *DB) FetchTask(taskID common.Hash) (*Task, error) {
4445

4546
func migrateCH(conn driver.Conn) error {
4647
err := conn.Exec(context.Background(), `
47-
CREATE TABLE IF NOT EXISTS w3bstream_tasks
48-
(
49-
task_id Array(UInt8) NOT NULL,
50-
device_id Array(UInt8) NOT NULL,
51-
nonce UInt64 NOT NULL,
52-
project_id UInt64 NOT NULL,
53-
project_version String NOT NULL,
54-
payloads Array(UInt8) NOT NULL,
55-
signature Array(UInt8) NOT NULL,
56-
create_at DateTime NOT NULL
57-
)
58-
ENGINE = ReplacingMergeTree()
59-
PRIMARY KEY task_id
60-
ORDER BY task_id
61-
`)
48+
CREATE TABLE IF NOT EXISTS w3bstream_tasks
49+
(
50+
task_id Array(UInt8) NOT NULL,
51+
device_id Array(UInt8) NOT NULL,
52+
nonce UInt64 NOT NULL,
53+
project_id UInt64 NOT NULL,
54+
project_version String NOT NULL,
55+
payloads Array(UInt8) NOT NULL,
56+
signature Array(UInt8) NOT NULL,
57+
hash_algorithm String NOT NULL,
58+
create_at DateTime NOT NULL
59+
)
60+
ENGINE = ReplacingMergeTree()
61+
PRIMARY KEY task_id
62+
ORDER BY task_id`,
63+
)
6264
return errors.Wrap(err, "failed to create clickhouse table")
6365
}
6466

0 commit comments

Comments
 (0)