Skip to content

Commit

Permalink
feat: add new command to validate exported pop (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen authored Jan 24, 2025
1 parent 46c75a4 commit 548f278
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#284](https://github.com/babylonlabs-io/finality-provider/pull/284) Add new command to delete pop
* [#277](https://github.com/babylonlabs-io/finality-provider/pull/277) Poll many blocks in poller
* [#291](https://github.com/babylonlabs-io/finality-provider/pull/291) chore: remove skip height
* [#297](https://github.com/babylonlabs-io/finality-provider/pull/297) Add new command to validate pop

## v0.14.3

Expand Down
42 changes: 41 additions & 1 deletion eotsmanager/cmd/eotsd/daemon/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewPopCmd() *cobra.Command {
cmd.AddCommand(
NewPopExportCmd(),
NewPopDeleteCmd(),
NewPopValidateExportCmd(),
)

return cmd
Expand Down Expand Up @@ -106,6 +107,25 @@ func NewPopExportCmd() *cobra.Command {
return cmd
}

func NewPopValidateExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Short: "Validates the PoP of the pop export command.",
Long: `Receives as an argument the output of eotsd pop export as a raw string '{...}'`,
Example: `eotsd pop validate '{
"eotsPublicKey": "3d0bebcbe800236ce8603c5bb1ab6c2af0932e947db4956a338f119797c37f1e",
"babyPublicKey": "A0V6yw74EdvoAWVauFqkH/GVM9YIpZitZf6bVEzG69tT",
"babySignEotsPk": "GO7xlC+BIypdcQdnIDsM+Ts75X9JKTOkDpXt5t4TSOIt/P1puAHVNhaYbweStVs25J9uRK+4XfrjD0M+t0Qy4g==",
"eotsSignBaby": "pR6vxgU0gXq+VqO+y7dHpZgHTz3zr5hdqXXh0WcWNkqUnRjHrizhYAHDMV8gh4vks4PqzKAIgZ779Wqwf5UrXQ==",
"babyAddress": "bbn1f04czxeqprn0s9fe7kdzqyde2e6nqj63dllwsm"
}'`,
RunE: validatePop,
Args: cobra.ExactArgs(1),
}

return cmd
}

func NewPopDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Expand All @@ -132,6 +152,26 @@ func NewPopDeleteCmd() *cobra.Command {
return cmd
}

func validatePop(cmd *cobra.Command, args []string) error {
strExportJSON := args[0]
var pop PoPExport
if err := json.Unmarshal([]byte(strExportJSON), &pop); err != nil {
return fmt.Errorf("failed to marshal %s into PoPExport structure", strExportJSON)
}

valid, err := ValidPopExport(pop)
if err != nil {
return fmt.Errorf("failed to validate pop %+v, reason: %w", pop, err)
}
if !valid {
return fmt.Errorf("invalid pop %+v", pop)
}

cmd.Println("Proof of Possession is valid!")

return nil
}

func exportPop(cmd *cobra.Command, _ []string) error {
eotsPassphrase, err := cmd.Flags().GetString(passphraseFlag)
if err != nil {
Expand Down Expand Up @@ -400,7 +440,7 @@ func SignCosmosAdr36(
return babySignBytes, nil
}

func VerifyPopExport(pop PoPExport) (bool, error) {
func ValidPopExport(pop PoPExport) (bool, error) {
valid, err := ValidEotsSignBaby(pop.EotsPublicKey, pop.BabyAddress, pop.EotsSignBaby)
if err != nil || !valid {
return false, err
Expand Down
25 changes: 24 additions & 1 deletion eotsmanager/cmd/eotsd/daemon/pop_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package daemon_test

import (
"bytes"
"context"
"encoding/json"
"testing"

"github.com/babylonlabs-io/finality-provider/eotsmanager/cmd/eotsd/daemon"
Expand Down Expand Up @@ -59,8 +62,28 @@ func TestPoPValidBabySignEotsPk(t *testing.T) {
func TestPoPVerify(t *testing.T) {
t.Parallel()
for _, pop := range popsToVerify {
valid, err := daemon.VerifyPopExport(pop)
valid, err := daemon.ValidPopExport(pop)
require.NoError(t, err)
require.True(t, valid)
}
}

func TestPoPValidate(t *testing.T) {
t.Parallel()
validateCmd := daemon.NewPopValidateExportCmd()

for _, pop := range popsToVerify {
jsonString, err := json.MarshalIndent(pop, "", " ")
require.NoError(t, err)

writer := bytes.NewBuffer([]byte{})
validateCmd.SetOutput(writer)

validateCmd.SetArgs([]string{string(jsonString)})

err = validateCmd.ExecuteContext(context.Background())
require.NoError(t, err)

require.Equal(t, writer.String(), "Proof of Possession is valid!\n")
}
}

0 comments on commit 548f278

Please sign in to comment.