1
1
package handlers
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/hex"
5
6
"math/big"
6
7
"net/http"
7
8
9
+ "github.com/ethereum/go-ethereum/common"
8
10
"github.com/ethereum/go-ethereum/crypto"
9
11
"github.com/rarimo/proof-verification-relayer/internal/data"
10
12
"github.com/rarimo/proof-verification-relayer/internal/service/api/requests"
@@ -66,6 +68,20 @@ func GetSignedState(w http.ResponseWriter, r *http.Request) {
66
68
}
67
69
68
70
func signState (state data.State , r * http.Request ) ([]byte , error ) {
71
+ digest , err := buildMessageDigest (state , r )
72
+ if err != nil {
73
+ return nil , errors .Wrap (err , "failed to build message digest" )
74
+ }
75
+
76
+ signature , err := crypto .Sign (digest , Config (r ).NetworkConfig ().PrivateKey )
77
+ if err != nil {
78
+ return nil , errors .Wrap (err , "failed to sign state" )
79
+ }
80
+
81
+ return signature , nil
82
+ }
83
+
84
+ func buildMessageDigest (state data.State , r * http.Request ) ([]byte , error ) {
69
85
rootBytes , err := hex .DecodeString (state .Root )
70
86
if err != nil {
71
87
return nil , errors .Wrap (err , "failed to decode signature digest" , logan.F {"root" : state .Root })
@@ -78,18 +94,15 @@ func signState(state data.State, r *http.Request) ([]byte, error) {
78
94
// newRoot_,
79
95
// transitionTimestamp_
80
96
//));
81
- digest := crypto .Keccak256 (
82
- []byte (Config (r ).Replicator ().RootPrefix ),
83
- Config (r ).Replicator ().SourceSMT .Bytes (),
84
- Config (r ).Replicator ().Address .Bytes (),
85
- rootBytes ,
86
- new (big.Int ).SetUint64 (state .Timestamp ).Bytes (),
87
- )
88
97
89
- signature , err := crypto .Sign (digest , Config (r ).NetworkConfig ().PrivateKey )
90
- if err != nil {
91
- return nil , errors .Wrap (err , "failed to sign state" )
92
- }
98
+ replicator := Config (r ).Replicator ()
99
+ var msgBuf bytes.Buffer
93
100
94
- return signature , nil
101
+ msgBuf .Write ([]byte (replicator .RootPrefix ))
102
+ msgBuf .Write (replicator .SourceSMT .Bytes ())
103
+ msgBuf .Write (replicator .Address .Bytes ())
104
+ msgBuf .Write (rootBytes )
105
+ msgBuf .Write (common .LeftPadBytes (new (big.Int ).SetUint64 (state .Timestamp ).Bytes (), 32 ))
106
+
107
+ return crypto .Keccak256 (msgBuf .Bytes ()), nil
95
108
}
0 commit comments