Skip to content

Commit

Permalink
chore: modified to output pops to file (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen authored Jan 24, 2025
1 parent 548f278 commit e1e768a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#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
* [#302](https://github.com/babylonlabs-io/finality-provider/pull/302) Update pop commands to write to a file

## v0.14.3

Expand Down
57 changes: 37 additions & 20 deletions eotsmanager/cmd/eotsd/daemon/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"

"github.com/btcsuite/btcd/btcec/v2/schnorr"
Expand All @@ -29,6 +30,7 @@ const (
flagKeyNameBaby = "baby-key-name"
flagKeyringBackendBaby = "baby-keyring-backend"
flagMessage = "message"
flagOutputFile = "output-file"
)

func init() {
Expand Down Expand Up @@ -104,23 +106,19 @@ func NewPopExportCmd() *cobra.Command {
f.String(flagKeyNameBaby, "", "BABY key name")
f.String(flagKeyringBackendBaby, keyring.BackendTest, "BABY backend of the keyring")

f.String(flagOutputFile, "", "Path to output JSON file")

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),
Use: "validate",
Short: "Validates the PoP of the pop export command.",
Long: `Receives as an argument the file path of the JSON output of the command eotsd pop export`,
Example: `eotsd pop validate /path/to/pop.json`,
RunE: validatePop,
Args: cobra.ExactArgs(1),
}

return cmd
Expand Down Expand Up @@ -149,14 +147,22 @@ func NewPopDeleteCmd() *cobra.Command {

f.String(flagMessage, "", "Message to be signed")

f.String(flagOutputFile, "", "Path to output JSON file")

return cmd
}

func validatePop(cmd *cobra.Command, args []string) error {
strExportJSON := args[0]
filePath := args[0]

bzExportJSON, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed to read pop file: %w", err)
}

var pop PoPExport
if err := json.Unmarshal([]byte(strExportJSON), &pop); err != nil {
return fmt.Errorf("failed to marshal %s into PoPExport structure", strExportJSON)
if err := json.Unmarshal(bzExportJSON, &pop); err != nil {
return fmt.Errorf("failed to marshal %s into PoPExport structure", string(bzExportJSON))
}

valid, err := ValidPopExport(pop)
Expand Down Expand Up @@ -222,7 +228,7 @@ func exportPop(cmd *cobra.Command, _ []string) error {
BabySignEotsPk: base64.StdEncoding.EncodeToString(babySignature),
}

return printJSON(cmd, out)
return handleOutputJSON(cmd, out)
}

func deletePop(cmd *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -272,7 +278,7 @@ func deletePop(cmd *cobra.Command, _ []string) error {
BabySignature: base64.StdEncoding.EncodeToString(babySignature),
}

return printJSON(cmd, out)
return handleOutputJSON(cmd, out)
}

// babyFlags returns the values of flagHomeBaby, flagKeyNameBaby and
Expand Down Expand Up @@ -345,13 +351,24 @@ func getInterpretedMessage(cmd *cobra.Command) (string, error) {
return interpretedMsg, nil
}

func printJSON(cmd *cobra.Command, out any) error {
jsonString, err := json.MarshalIndent(out, "", " ")
func handleOutputJSON(cmd *cobra.Command, out any) error {
jsonBz, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err
}

cmd.Println(string(jsonString))
outputFilePath, err := cmd.Flags().GetString(flagOutputFile)
if err != nil {
return err
}

if len(outputFilePath) > 0 {
if err := os.WriteFile(outputFilePath, jsonBz, 0644); err != nil {
return fmt.Errorf("failed to write output file: %w", err)
}
}

cmd.Println(string(jsonBz))

return nil
}
Expand Down
13 changes: 11 additions & 2 deletions eotsmanager/cmd/eotsd/daemon/pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/babylonlabs-io/finality-provider/eotsmanager/cmd/eotsd/daemon"
Expand Down Expand Up @@ -72,14 +75,20 @@ func TestPoPValidate(t *testing.T) {
t.Parallel()
validateCmd := daemon.NewPopValidateExportCmd()

for _, pop := range popsToVerify {
tmp := t.TempDir()

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

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

validateCmd.SetArgs([]string{string(jsonString)})
fileName := filepath.Join(tmp, fmt.Sprintf("%d-pop-out.json", i))
err = os.WriteFile(fileName, jsonString, 0644)
require.NoError(t, err)

validateCmd.SetArgs([]string{fileName})

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

0 comments on commit e1e768a

Please sign in to comment.