From 1e727508536fb26aa658489374b3b9bb632d2ac3 Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Fri, 24 Jan 2025 11:07:44 -0300 Subject: [PATCH] chore: modified to output pops to file (#303) Backport #302 --- eotsmanager/cmd/eotsd/daemon/pop.go | 41 +++++++++++++++--------- eotsmanager/cmd/eotsd/daemon/pop_test.go | 12 +++++-- eotsmanager/cmd/eotsd/daemon/sign.go | 21 ++++++++---- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/eotsmanager/cmd/eotsd/daemon/pop.go b/eotsmanager/cmd/eotsd/daemon/pop.go index b151a1ae..ad331462 100644 --- a/eotsmanager/cmd/eotsd/daemon/pop.go +++ b/eotsmanager/cmd/eotsd/daemon/pop.go @@ -29,6 +29,7 @@ const ( flagKeyNameBaby = "baby-key-name" flagKeyringBackendBaby = "baby-keyring-backend" flagMessage = "message" + flagOutputFile = "output-file" ) func init() { @@ -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, } @@ -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 ", + Action: validatePop, } ) @@ -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) @@ -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) { diff --git a/eotsmanager/cmd/eotsd/daemon/pop_test.go b/eotsmanager/cmd/eotsd/daemon/pop_test.go index 28e76359..13464e92 100644 --- a/eotsmanager/cmd/eotsd/daemon/pop_test.go +++ b/eotsmanager/cmd/eotsd/daemon/pop_test.go @@ -2,7 +2,10 @@ package daemon_test import ( "encoding/json" + "fmt" "math/rand" + "os" + "path/filepath" "testing" "time" @@ -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") } diff --git a/eotsmanager/cmd/eotsd/daemon/sign.go b/eotsmanager/cmd/eotsd/daemon/sign.go index 04aca0b5..4c2000a7 100644 --- a/eotsmanager/cmd/eotsd/daemon/sign.go +++ b/eotsmanager/cmd/eotsd/daemon/sign.go @@ -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) { @@ -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 }