Skip to content

Commit

Permalink
feat: add task signature
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jun 20, 2024
1 parent 49ffb16 commit 6843b55
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 60 deletions.
File renamed without changes.
32 changes: 32 additions & 0 deletions cmd/sequencer/commands/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package commands

import (
"context"

"github.com/spf13/cobra"
"github.com/xoctopus/x/misc/must"

"github.com/machinefi/sprout-pebble-sequencer/pkg/contexts"
"github.com/machinefi/sprout-pebble-sequencer/pkg/models"
)

func Migrate(ctx context.Context) *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "migrate database",
Run: func(cmd *cobra.Command, args []string) {
db := must.BeTrueV(contexts.DatabaseFromContext(ctx))
must.NoErrorWrap(db.AutoMigrate(
&models.Account{},
&models.App{},
&models.AppV2{},
&models.Bank{},
&models.BankRecord{},
&models.Device{},
&models.DeviceRecord{},
&models.Task{},
&models.Message{},
), "failed to migrate database")
},
}
}
9 changes: 7 additions & 2 deletions cmd/sequencer/config.go → cmd/sequencer/commands/project.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main
package commands

import (
"bytes"
"compress/zlib"
"context"
_ "embed"
"encoding/hex"
"encoding/json"
Expand All @@ -13,7 +14,7 @@ import (
"github.com/xoctopus/confx/confcmd"
)

//go:embed abis/confirm.json
//go:embed confirm.json
var abi string

func NewDefaultSproutConfigGenerator() *SproutConfigGenerator {
Expand Down Expand Up @@ -132,3 +133,7 @@ func (g *SproutConfigGenerator) Exec(cmd *cobra.Command, args ...string) error {
enc.SetIndent("", " ")
return enc.Encode(project)
}

func GenerateSproutConfig(_ context.Context) *cobra.Command {
return confcmd.NewCommand(NewDefaultSproutConfigGenerator())
}
1 change: 1 addition & 0 deletions cmd/sequencer/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ PEBBLE_SEQUENCER__MqttBroker_Retry_Interval: "3000000000"
PEBBLE_SEQUENCER__MqttBroker_Retry_Repeats: "3"
PEBBLE_SEQUENCER__MqttBroker_Server: tcp://127.0.0.1:1883
PEBBLE_SEQUENCER__MqttBroker_Timeout: "10000000000"
PEBBLE_SEQUENCER__PrivateKey_Hex: dbfe03b0406549232b8dccc04be8224fcc0afa300a33d4f335dcfdfead861c85
PEBBLE_SEQUENCER__ProjectID: "0"
PEBBLE_SEQUENCER__ProjectVersion: ""
PEBBLE_SEQUENCER__ServerPort: "6666"
25 changes: 0 additions & 25 deletions cmd/sequencer/debug_server.go

This file was deleted.

47 changes: 23 additions & 24 deletions cmd/sequencer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"

"github.com/spf13/cobra"
"github.com/gin-gonic/gin"
"github.com/xoctopus/confx/confapp"
"github.com/xoctopus/confx/confcmd"
"github.com/xoctopus/confx/confmws/confmqtt"
"github.com/xoctopus/x/contextx"
"github.com/xoctopus/x/misc/must"

"github.com/machinefi/sprout-pebble-sequencer/cmd/sequencer/commands"
"github.com/machinefi/sprout-pebble-sequencer/pkg/contexts"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/blockchain"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/crypto"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/database"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/logger"
"github.com/machinefi/sprout-pebble-sequencer/pkg/models"
"github.com/machinefi/sprout-pebble-sequencer/pkg/modules/event"
)

Expand All @@ -35,6 +36,7 @@ var (
Database *database.Postgres
Blockchain *blockchain.Blockchain
Logger *logger.Logger
PrivateKey *crypto.EcdsaPrivateKey
ServerPort uint16
ProjectID uint64
ProjectVersion string
Expand All @@ -43,6 +45,7 @@ var (
Blockchain: &blockchain.Blockchain{Contracts: contracts},
MqttBroker: &confmqtt.Broker{},
Database: &database.Postgres{},
PrivateKey: &crypto.EcdsaPrivateKey{Hex: "dbfe03b0406549232b8dccc04be8224fcc0afa300a33d4f335dcfdfead861c85"},
ServerPort: 6666,
}
ctx context.Context
Expand All @@ -56,6 +59,7 @@ func init() {
contexts.WithMqttBrokerContext(config.MqttBroker),
contexts.WithProjectIDContext(config.ProjectID),
contexts.WithProjectVersionContext(config.ProjectVersion),
contexts.WithEcdsaPrivateKeyContext(config.PrivateKey),
)(context.Background())

app = confapp.NewAppContext(
Expand All @@ -77,16 +81,11 @@ func init() {
"project id and version is required",
)

app.AddCommand(&cobra.Command{
Use: "migrate",
Short: "migrate database",
Run: func(cmd *cobra.Command, args []string) {
Migrate(ctx)
},
})
app.AddCommand(confcmd.NewCommand(NewDefaultSproutConfigGenerator()))
app.AddCommand(commands.Migrate(ctx))
app.AddCommand(commands.GenerateSproutConfig(ctx))
}

// Main app main entry
func Main() error {
if err := config.Blockchain.RunMonitors(); err != nil {
config.Logger.Error(err, "failed to start tx monitor")
Expand All @@ -103,19 +102,19 @@ func Main() error {
return nil
}

func Migrate(ctx context.Context) {
db := must.BeTrueV(contexts.DatabaseFromContext(ctx))
must.NoErrorWrap(db.AutoMigrate(
&models.Account{},
&models.App{},
&models.AppV2{},
&models.Bank{},
&models.BankRecord{},
&models.Device{},
&models.DeviceRecord{},
&models.Task{},
&models.Message{},
), "failed to migrate database")
// RunDebugServer enable simple http server for debugging
func RunDebugServer(ctx context.Context, addr string) {
// addr := contexts.ServerAddrFromContext(ctx)
eng := gin.Default()
eng.Handle(
http.MethodGet, "/debug/monitor-info",
func(c *gin.Context) {
bc := must.BeTrueV(contexts.BlockchainFromContext(ctx))
monitors := bc.MonitorsInfo()
c.JSON(http.StatusOK, monitors)
},
)
eng.Run(addr)
}

func main() {
Expand Down
31 changes: 24 additions & 7 deletions pkg/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ package contexts

import (
"context"
"crypto/ecdsa"

"github.com/xoctopus/confx/confmws/confmqtt"
"github.com/xoctopus/x/contextx"

"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/blockchain"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/crypto"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/database"
"github.com/machinefi/sprout-pebble-sequencer/pkg/middlewares/logger"
)

type (
ctxLogger struct{}
ctxMqttBroker struct{}
ctxMqttClient struct{}
ctxBlockchain struct{}
ctxDatabase struct{}
ctxProjectID struct{}
ctxProjectVersion struct{}
ctxLogger struct{}
ctxMqttBroker struct{}
ctxMqttClient struct{}
ctxBlockchain struct{}
ctxDatabase struct{}
ctxProjectID struct{}
ctxProjectVersion struct{}
ctxEcdsaPrivateKey struct{}
)

func LoggerFromContext(ctx context.Context) (*logger.Logger, bool) {
Expand Down Expand Up @@ -106,3 +109,17 @@ func WithProjectVersionContext(v string) contextx.WithContext {
return context.WithValue(ctx, ctxProjectVersion{}, v)
}
}

func EcdsaPrivateKeyFromContext(ctx context.Context) (*ecdsa.PrivateKey, bool) {
v, ok := ctx.Value(ctxEcdsaPrivateKey{}).(*crypto.EcdsaPrivateKey)
if !ok {
return nil, false
}
return v.PrivateKey, ok
}

func WithEcdsaPrivateKeyContext(v *crypto.EcdsaPrivateKey) contextx.WithContext {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxEcdsaPrivateKey{}, v)
}
}
49 changes: 49 additions & 0 deletions pkg/middlewares/crypto/ecdsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package crypto

import (
"crypto/ecdsa"
"encoding/hex"

"github.com/ethereum/go-ethereum/crypto"
)

const MaskedPrivateKey = "--------"

type EcdsaPrivateKey struct {
Hex string

*ecdsa.PrivateKey `env:"-"`
}

func (k *EcdsaPrivateKey) Init() error {
sk, err := crypto.HexToECDSA(k.Hex)
if err != nil {
return err
}
k.PrivateKey = sk
return nil
}

func (k *EcdsaPrivateKey) SecurityString() string {
return MaskedPrivateKey
}

type EcdsaPublicKey struct {
Hex string

*ecdsa.PublicKey `env:"-"`
}

func (k *EcdsaPublicKey) Init() error {
raw, err := hex.DecodeString(k.Hex)
if err != nil {
return err
}

pk, err := crypto.UnmarshalPubkey(raw)
if err != nil {
return err
}
k.PublicKey = pk
return nil
}
31 changes: 30 additions & 1 deletion pkg/models/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package models

import "gorm.io/gorm"
import (
"bytes"
"crypto/ecdsa"
"encoding/binary"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"gorm.io/gorm"
)

type Task struct {
gorm.Model
Expand All @@ -9,3 +18,23 @@ type Task struct {
MessageIDs []byte `gorm:"not null"`
Signature string `gorm:"not null,default:''"`
}

func (t *Task) Sign(sk *ecdsa.PrivateKey, msg *Message) error {
if msg.ProjectID != t.ProjectID {
return errors.New("unmatched project id")
}

buf := bytes.NewBuffer(nil)
_ = binary.Write(buf, binary.BigEndian, uint64(t.ID))
_ = binary.Write(buf, binary.BigEndian, t.ProjectID)
_, _ = buf.WriteString(msg.ClientID)
_, _ = buf.Write(crypto.Keccak256Hash(msg.Data).Bytes())

h := crypto.Keccak256Hash(buf.Bytes())
sig, err := crypto.Sign(h.Bytes(), sk)
if err != nil {
return err
}
t.Signature = hexutil.Encode(sig)
return nil
}
7 changes: 6 additions & 1 deletion pkg/modules/event/event_device_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
Signature: "",
}

sk := must.BeTrueV(contexts.EcdsaPrivateKeyFromContext(ctx))
db, _ := contexts.DatabaseFromContext(ctx)
return db.Transaction(func(tx *gorm.DB) error {
if err = tx.Create(msg).Error; err != nil {
Expand All @@ -131,6 +132,10 @@ func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
if err = tx.Create(task).Error; err != nil {
return err
}
return nil
if err = task.Sign(sk, msg); err != nil {
return err
}
return tx.Model(task).
Update("signature", task.Signature).Where("id=?", task.ID).Error
})
}
1 change: 1 addition & 0 deletions pkg/modules/event/event_device_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestDeviceConfirmUnmarshal(t *testing.T) {
t.Skip("need postgres dependency")
r := require.New(t)

t.Run("UnmarshalTopic", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/modules/event/event_device_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestDeviceDataUnmarshal(t *testing.T) {
t.Skip("need postgres dependency")
r := require.New(t)

v, ok := event.NewEvent("device/+/data").(*event.DeviceData)
Expand Down
1 change: 1 addition & 0 deletions pkg/modules/event/event_device_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestDeviceQuery_Handle(t *testing.T) {
t.Skip("need postgres dependency")
r := require.New(t)

v, ok := event.NewEvent("device/+/query").(*event.DeviceQuery)
Expand Down
Loading

0 comments on commit 6843b55

Please sign in to comment.