Skip to content

Commit

Permalink
file output
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Jan 24, 2025
1 parent dbc70aa commit cf7aaae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
29 changes: 24 additions & 5 deletions cmd/stakercli/pop/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
babyAddressPrefixFlag = "baby-address-prefix"
keyringDirFlag = "keyring-dir"
keyringBackendFlag = "keyring-backend"
outputFileFlag = "output-file"
)

var PopCommands = []cli.Command{
Expand All @@ -43,15 +44,15 @@ var PopCommands = []cli.Command{
Usage: "Commands about proof-of-possession generation and verification",
Category: "PoP commands",
Subcommands: []cli.Command{
generateCreatePopCmd,
GenerateCreatePopCmd,
generateDeletePopCmd,
signCosmosAdr36Cmd,
validatePopCmd,
ValidatePopCmd,
},
},
}

var generateCreatePopCmd = cli.Command{
var GenerateCreatePopCmd = cli.Command{
Name: "generate-create-pop",
ShortName: "gcp",
Usage: "stakercli pop generate-create-pop",
Expand Down Expand Up @@ -111,6 +112,11 @@ var generateCreatePopCmd = cli.Command{
Usage: "Keyring backend",
Value: "test",
},
cli.StringFlag{
Name: outputFileFlag,
Usage: "Path to output JSON file",
Value: "",
},
},
Action: generatePop,
}
Expand Down Expand Up @@ -170,20 +176,33 @@ func generatePop(c *cli.Context) error {
return err
}

if outputPath := c.String(outputFileFlag); outputPath != "" {
// Convert response to JSON
jsonBytes, err := json.MarshalIndent(popResponse, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal response to JSON: %w", err)
}

// Write to file
if err := os.WriteFile(outputPath, jsonBytes, 0644); err != nil {
return fmt.Errorf("failed to write output file: %w", err)
}
}

helpers.PrintRespJSON(popResponse)

return nil
}

var validatePopCmd = cli.Command{
var ValidatePopCmd = cli.Command{
Name: "validate",
ShortName: "vp",
Usage: "stakercli pop validate <path-to-pop.json>",
Flags: []cli.Flag{
cli.StringFlag{
Name: btcNetworkFlag,
Usage: "Bitcoin network (testnet3, mainnet, regtest, simnet, signet)",
Value: "main",
Value: "mainnet",
},
cli.StringFlag{
Name: babyAddressPrefixFlag,
Expand Down
30 changes: 24 additions & 6 deletions cmd/stakercli/pop/pop_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package pop_test

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/btcsuite/btcd/chaincfg"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/btc-staker/cmd/stakercli/pop"
"github.com/babylonlabs-io/btc-staker/itest/testutil"
"github.com/babylonlabs-io/btc-staker/staker"
)

Expand All @@ -21,11 +24,26 @@ var popsToVerify = []staker.Response{
},
}

func TestPoPValidate(t *testing.T) {
func TestValidatePoPCmd(t *testing.T) {
t.Parallel()

for _, p := range popsToVerify {
err := pop.ValidatePop(p, &chaincfg.MainNetParams, "bbn")
require.NoError(t, err)
// Create a temporary JSON file
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "pop.json")

// Marshal the test data to JSON
jsonData, err := json.MarshalIndent(popsToVerify[0], "", " ")
require.NoError(t, err)

// Write JSON to temporary file
err = os.WriteFile(tmpFile, jsonData, 0644)
require.NoError(t, err)

// Test ValidatePopCmd with the JSON file
app := testutil.TestApp()
validatePop := []string{
"stakercli", "pop", "validate", fmt.Sprintf("%s", tmpFile),

Check failure on line 45 in cmd/stakercli/pop/pop_test.go

View workflow job for this annotation

GitHub Actions / lint_test / lint

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)
}
err = app.Run(validatePop)
require.NoError(t, err)
}
2 changes: 2 additions & 0 deletions itest/testutil/appcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/babylonlabs-io/babylon/testutil/datagen"
cmdadmin "github.com/babylonlabs-io/btc-staker/cmd/stakercli/admin"
cmddaemon "github.com/babylonlabs-io/btc-staker/cmd/stakercli/daemon"
"github.com/babylonlabs-io/btc-staker/cmd/stakercli/pop"
"github.com/babylonlabs-io/btc-staker/cmd/stakercli/transaction"
"github.com/babylonlabs-io/networks/parameters/parser"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestApp() *cli.App {
app.Commands = append(app.Commands, cmddaemon.DaemonCommands...)
app.Commands = append(app.Commands, cmdadmin.AdminCommands...)
app.Commands = append(app.Commands, transaction.TransactionCommands...)
app.Commands = append(app.Commands, pop.PopCommands...)
return app
}

Expand Down

0 comments on commit cf7aaae

Please sign in to comment.