Skip to content

Commit

Permalink
chore: modified to output pops to file (#303)
Browse files Browse the repository at this point in the history
Backport #302
  • Loading branch information
RafilxTenfen authored Jan 24, 2025
1 parent 55b1d1c commit 1e72750
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
41 changes: 25 additions & 16 deletions eotsmanager/cmd/eotsd/daemon/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
flagKeyNameBaby = "baby-key-name"
flagKeyringBackendBaby = "baby-keyring-backend"
flagMessage = "message"
flagOutputFile = "output-file"
)

func init() {
Expand Down Expand Up @@ -120,6 +121,11 @@ var (
Usage: "BABY backend of the keyring",
Value: defaultKeyringBackend,
},
cli.StringFlag{
Name: flagOutputFile,
Usage: "Path to output JSON file",
Value: "",
},
},
Action: exportPop,
}
Expand Down Expand Up @@ -165,21 +171,20 @@ var (
Name: flagMessage,
Usage: "Message to be signed",
},
cli.StringFlag{
Name: flagOutputFile,
Usage: "Path to output JSON file",
Value: "",
},
},
Action: deletePop,
}
PoPValidateExportCommand = cli.Command{
Name: "validate",
Usage: "Validates the PoP of the pop export command.",
Description: `Receives as an argument the output of eotsd pop export as a raw string '{...}'`,
UsageText: `eotsd pop validate '{
"eotsPublicKey": "3d0bebcbe800236ce8603c5bb1ab6c2af0932e947db4956a338f119797c37f1e",
"babyPublicKey": "A0V6yw74EdvoAWVauFqkH/GVM9YIpZitZf6bVEzG69tT",
"babySignEotsPk": "GO7xlC+BIypdcQdnIDsM+Ts75X9JKTOkDpXt5t4TSOIt/P1puAHVNhaYbweStVs25J9uRK+4XfrjD0M+t0Qy4g==",
"eotsSignBaby": "pR6vxgU0gXq+VqO+y7dHpZgHTz3zr5hdqXXh0WcWNkqUnRjHrizhYAHDMV8gh4vks4PqzKAIgZ779Wqwf5UrXQ==",
"babyAddress": "bbn1f04czxeqprn0s9fe7kdzqyde2e6nqj63dllwsm"
}'`,
Action: validatePop,
Description: `Receives as an argument the file path of the JSON output of the command eotsd pop export`,
UsageText: "stakercli pop validate <path-to-pop.json>",
Action: validatePop,
}
)

Expand Down Expand Up @@ -230,16 +235,21 @@ func exportPop(ctx *cli.Context) error {
BabySignEotsPk: base64.StdEncoding.EncodeToString(babySignature),
}

printRespJSON(out)
return nil
return printRespJSON(ctx, out)
}

func validatePop(ctx *cli.Context) error {
args := ctx.Args()
strExportJSON := args.First()
filePath := args.First()

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([]byte(bzExportJSON), &pop); err != nil {
return fmt.Errorf("failed to marshal %s into PoPExport structure", bzExportJSON)
}

valid, err := ValidPopExport(pop)
Expand Down Expand Up @@ -301,9 +311,8 @@ func deletePop(ctx *cli.Context) error {

BabySignature: base64.StdEncoding.EncodeToString(babySignature),
}
printRespJSON(out)

return nil
return printRespJSON(ctx, out)
}

func loadEotsManager(eotsHomePath, eotsFpPubKeyStr, eotsKeyName, eotsKeyringBackend string) (*eotsmanager.LocalEOTSManager, error) {
Expand Down
12 changes: 10 additions & 2 deletions eotsmanager/cmd/eotsd/daemon/pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package daemon_test

import (
"encoding/json"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -72,12 +75,17 @@ func TestPoPValidate(t *testing.T) {
t.Parallel()
app := testApp()

tmp := t.TempDir()
r := rand.New(rand.NewSource(time.Now().Unix()))
for _, pop := range popsToVerify {
for i, pop := range popsToVerify {
jsonString, err := json.MarshalIndent(pop, "", " ")
require.NoError(t, err)

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

outputKeysAdd := appRunWithOutput(r, t, app, []string{"eotsd", "pop", "validate", fileName})

require.Equal(t, outputKeysAdd, "Proof of Possession is valid!\n")
}
Expand Down
21 changes: 14 additions & 7 deletions eotsmanager/cmd/eotsd/daemon/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,12 @@ func SignSchnorr(ctx *cli.Context) error {
return fmt.Errorf("failed to sign msg: %w", err)
}

printRespJSON(DataSigned{
return printRespJSON(ctx, DataSigned{
KeyName: keyName,
PubKeyHex: pubKey.MarshalHex(),
SignedDataHashHex: hex.EncodeToString(hashOfMsgToSign),
SchnorrSignatureHex: hex.EncodeToString(signature.Serialize()),
})

return nil
}

func hashFromFile(inputFilePath string) ([]byte, error) {
Expand Down Expand Up @@ -219,12 +217,21 @@ func singMsg(
return eotsManager.SignSchnorrSigFromKeyname(keyName, passphrase, hashOfMsgToSign)
}

func printRespJSON(resp interface{}) {
jsonBytes, err := json.MarshalIndent(resp, "", " ")
func printRespJSON(ctx *cli.Context, resp interface{}) error {
jsonBz, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Println("unable to decode response: ", err)
return
return nil
}

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

fmt.Printf("%s\n", jsonBz)

return nil
}

0 comments on commit 1e72750

Please sign in to comment.