Skip to content

Commit

Permalink
Merge pull request #12 from rarimo/fix/digest-packing
Browse files Browse the repository at this point in the history
* fix: build message digest with solidity abi encoding

* fix: build message digest with solidity abi encoding, remove abi building stuff, use just buffer with bytes padding
  • Loading branch information
mhrynenko authored Dec 11, 2024
2 parents 74af7c5 + 36392da commit b170bac
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions internal/service/api/handlers/get_signed_state.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package handlers

import (
"bytes"
"encoding/hex"
"math/big"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rarimo/proof-verification-relayer/internal/data"
"github.com/rarimo/proof-verification-relayer/internal/service/api/requests"
Expand Down Expand Up @@ -66,6 +68,20 @@ func GetSignedState(w http.ResponseWriter, r *http.Request) {
}

func signState(state data.State, r *http.Request) ([]byte, error) {
digest, err := buildMessageDigest(state, r)
if err != nil {
return nil, errors.Wrap(err, "failed to build message digest")
}

signature, err := crypto.Sign(digest, Config(r).NetworkConfig().PrivateKey)
if err != nil {
return nil, errors.Wrap(err, "failed to sign state")
}

return signature, nil
}

func buildMessageDigest(state data.State, r *http.Request) ([]byte, error) {
rootBytes, err := hex.DecodeString(state.Root)
if err != nil {
return nil, errors.Wrap(err, "failed to decode signature digest", logan.F{"root": state.Root})
Expand All @@ -78,18 +94,15 @@ func signState(state data.State, r *http.Request) ([]byte, error) {
// newRoot_,
// transitionTimestamp_
//));
digest := crypto.Keccak256(
[]byte(Config(r).Replicator().RootPrefix),
Config(r).Replicator().SourceSMT.Bytes(),
Config(r).Replicator().Address.Bytes(),
rootBytes,
new(big.Int).SetUint64(state.Timestamp).Bytes(),
)

signature, err := crypto.Sign(digest, Config(r).NetworkConfig().PrivateKey)
if err != nil {
return nil, errors.Wrap(err, "failed to sign state")
}
replicator := Config(r).Replicator()
var msgBuf bytes.Buffer

return signature, nil
msgBuf.Write([]byte(replicator.RootPrefix))
msgBuf.Write(replicator.SourceSMT.Bytes())
msgBuf.Write(replicator.Address.Bytes())
msgBuf.Write(rootBytes)
msgBuf.Write(common.LeftPadBytes(new(big.Int).SetUint64(state.Timestamp).Bytes(), 32))

return crypto.Keccak256(msgBuf.Bytes()), nil
}

0 comments on commit b170bac

Please sign in to comment.