Skip to content

Commit

Permalink
feat: device white list
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jun 26, 2024
1 parent 4912f4d commit ff5680d
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 9 deletions.
1 change: 0 additions & 1 deletion cmd/sequencer/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,3 @@ PEBBLE_SEQUENCER__MqttBroker_Timeout: "10000000000"
PEBBLE_SEQUENCER__PrivateKey_Hex: dbfe03b0406549232b8dccc04be8224fcc0afa300a33d4f335dcfdfead861c85
PEBBLE_SEQUENCER__ProjectID: "0"
PEBBLE_SEQUENCER__ProjectVersion: ""
PEBBLE_SEQUENCER__ServerPort: "6666"
3 changes: 3 additions & 0 deletions cmd/sequencer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
PrivateKey *crypto.EcdsaPrivateKey
ProjectID uint64
ProjectVersion string
WhiteList contexts.WhiteList
}{
Logger: &logger.Logger{Level: slog.LevelDebug},
Blockchain: &blockchain.Blockchain{Contracts: contracts},
Expand All @@ -47,6 +48,7 @@ var (
PrivateKey: &crypto.EcdsaPrivateKey{
Hex: "dbfe03b0406549232b8dccc04be8224fcc0afa300a33d4f335dcfdfead861c85",
},
// WhiteList: contexts.WhiteList{"103381234567407"},
}
ctx context.Context
)
Expand Down Expand Up @@ -79,6 +81,7 @@ func init() {
contexts.WithProjectIDContext(config.ProjectID),
contexts.WithProjectVersionContext(config.ProjectVersion),
contexts.WithEcdsaPrivateKeyContext(config.PrivateKey),
contexts.WithWhiteListKeyContext(config.WhiteList),
)(context.Background())

app.AddCommand(commands.Migrate(ctx))
Expand Down
15 changes: 15 additions & 0 deletions pkg/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
ctxProjectID struct{}
ctxProjectVersion struct{}
ctxEcdsaPrivateKey struct{}
ctxWhiteList struct{}
)

func LoggerFromContext(ctx context.Context) (*logger.Logger, bool) {
Expand Down Expand Up @@ -123,3 +124,17 @@ func WithEcdsaPrivateKeyContext(v *crypto.EcdsaPrivateKey) contextx.WithContext
return context.WithValue(ctx, ctxEcdsaPrivateKey{}, v)
}
}

func CheckDeviceWhiteListFromContext(ctx context.Context, imei string) bool {
v, ok := ctx.Value(ctxWhiteList{}).(WhiteList)
if !ok {
return true
}
return v.NeedHandle(imei)
}

func WithWhiteListKeyContext(v WhiteList) contextx.WithContext {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxWhiteList{}, v)
}
}
9 changes: 9 additions & 0 deletions pkg/contexts/ctx_white_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package contexts

import "slices"

type WhiteList []string

func (wl WhiteList) NeedHandle(imei string) bool {
return len(wl) == 0 || len(wl) > 0 && slices.Contains(wl, imei)
}
2 changes: 1 addition & 1 deletion pkg/modules/event/event_device_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
IMEI: e.imei,
Owner: common.BytesToAddress(e.pkg.GetOwner()).String(),
Timestamp: e.pkg.GetTimestamp(),
Signature: hex.EncodeToString(append(e.pkg.GetSignature(), byte(0))),
Signature: hex.EncodeToString(e.sig),
GasLimit: big.NewInt(200000).String(),
DataChannel: uint32(dev.DataChannel),
})),
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/event/event_device_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

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

t.Run("UnmarshalTopic", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/event/event_device_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (e *DeviceQuery) Handle(ctx context.Context) (err error) {
}

err = PublicMqttMessage(ctx,
"device_query", "backend/"+e.imei+"/status",
"device_query", "backend/"+e.imei+"/status", e.imei,
&struct {
Status int32 `json:"status"`
Proposer string `json:"proposer,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/event/event_firmware_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e *FirmwareUpdated) Handle(ctx context.Context) (err error) {
}

err = PublicMqttMessage(ctx,
"firmware_updated", "device/app_update/"+app.ID,
"firmware_updated", "device/app_update/"+app.ID, "",
&struct {
Name string `json:"name"`
Version string `json:"version"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/event/event_pebble_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (e *PebbleConfig) Handle(ctx context.Context) (err error) {

err = PublicMqttMessage(ctx,
"pebble_config",
"backend/"+e.Imei+"/config",
"backend/"+e.Imei+"/config", e.Imei,
app.Data,
)
return errors.Wrap(err, "failed to publish pebble_config response")
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/event/event_pebble_firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (e *PebbleFirmware) Handle(ctx context.Context) (err error) {
}

err = PublicMqttMessage(ctx,
"pebble_firmware", "backend/"+e.Imei+"/firmware",
"pebble_firmware", "backend/"+e.Imei+"/firmware", e.Imei,
&struct {
Firmware string `json:"firmware"`
Uri string `json:"uri"`
Expand Down
8 changes: 6 additions & 2 deletions pkg/modules/event/util_mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/machinefi/sprout-pebble-sequencer/pkg/contexts"
)

func PublicMqttMessage(ctx context.Context, id, topic string, v any) error {
mq := must.BeTrueV(contexts.MqttBrokerFromContext(ctx))
func PublicMqttMessage(ctx context.Context, id, topic, imei string, v any) error {
needhandled := contexts.CheckDeviceWhiteListFromContext(ctx, imei)
if !needhandled {
return nil
}

mq := must.BeTrueV(contexts.MqttBrokerFromContext(ctx))
cli, err := mq.NewClient(id, topic)
if err != nil {
return err
Expand Down

0 comments on commit ff5680d

Please sign in to comment.