@@ -3,6 +3,7 @@ package api
3
3
import (
4
4
"bytes"
5
5
"crypto/ecdsa"
6
+ "crypto/sha256"
6
7
"encoding/json"
7
8
"fmt"
8
9
"io"
@@ -32,11 +33,12 @@ func newErrResp(err error) *errResp {
32
33
}
33
34
34
35
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"`
40
42
}
41
43
42
44
type CreateTaskResp struct {
@@ -79,7 +81,7 @@ func (s *httpServer) createTask(c *gin.Context) {
79
81
c .JSON (http .StatusBadRequest , newErrResp (errors .Wrap (err , "failed to decode signature from hex format" )))
80
82
return
81
83
}
82
- pubKey , err := recoverPubkey (* req , sig )
84
+ pubKey , hashAlg , err := recoverPubkey (* req , sig )
83
85
if err != nil {
84
86
slog .Error ("failed to recover public key" , "error" , err )
85
87
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) {
126
128
ProjectVersion : req .ProjectVersion ,
127
129
Payloads : payloadsJ ,
128
130
Signature : sig ,
131
+ HashAlgorithm : hashAlg ,
129
132
},
130
133
); err != nil {
131
134
slog .Error ("failed to create task to persistence layer" , "error" , err )
@@ -164,19 +167,32 @@ func (s *httpServer) createTask(c *gin.Context) {
164
167
})
165
168
}
166
169
167
- func recoverPubkey (req CreateTaskReq , sig []byte ) (* ecdsa.PublicKey , error ) {
170
+ func recoverPubkey (req CreateTaskReq , sig []byte ) (* ecdsa.PublicKey , string , error ) {
168
171
req .Signature = ""
169
172
reqJson , err := json .Marshal (req )
170
173
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" )
172
175
}
173
176
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 )
176
192
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" )
178
194
}
179
- return sigpk , nil
195
+ return sigpk , alg , nil
180
196
}
181
197
182
198
func (s * httpServer ) getProverTaskState (taskID string ) (* StateLog , error ) {
0 commit comments