-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ func E2EECommand() *cobra.Command { | |
|
||
cmd.AddCommand( | ||
KeygenCommand(), | ||
EncryptCommand(), | ||
) | ||
|
||
return cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
|
||
"filippo.io/age" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/crypto-org-chain/cronos/v2/x/e2ee/types" | ||
) | ||
|
||
const ( | ||
FlagRecipient = "recipient" | ||
flagO = "o" | ||
) | ||
|
||
func EncryptCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "encrypt [input-file]", | ||
Short: "Encrypt input file to one or multiple recipients", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outputFile, err := cmd.Flags().GetString(flags.FlagOutput) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
recs, err := cmd.Flags().GetStringArray(FlagRecipient) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// query encryption key from chain state | ||
client := types.NewQueryClient(clientCtx.GRPCClient) | ||
recipients := make([]age.Recipient, len(recs)) | ||
for i, rec := range recs { | ||
rsp, err := client.Key(clientCtx.CmdContext, &types.KeyRequest{ | ||
Address: rec, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
recipient, err := age.ParseX25519Recipient(string(rsp.Key)) | ||
if err != nil { | ||
return err | ||
} | ||
recipients[i] = recipient | ||
} | ||
|
||
inputFile := args[0] | ||
var input io.Reader | ||
if inputFile == "-" { | ||
input = os.Stdin | ||
} else { | ||
f, err := os.Open(inputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
input = f | ||
} | ||
|
||
var output io.Writer | ||
if outputFile == "-" { | ||
output = os.Stdout | ||
} else { | ||
fp, err := os.Create(outputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer fp.Close() | ||
output = fp | ||
} | ||
return encrypt(recipients, input, output) | ||
}, | ||
} | ||
f := cmd.Flags() | ||
f.StringArrayP(FlagRecipient, "r", []string{}, "recipients") | ||
f.StringP(flags.FlagOutput, "o", "-", "output file (default stdout)") | ||
return cmd | ||
} | ||
|
||
func encrypt(recipients []age.Recipient, in io.Reader, out io.Writer) (err error) { | ||
var w io.WriteCloser | ||
w, err = age.Encrypt(out, recipients...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
err = errors.Join(err, w.Close()) | ||
}() | ||
|
||
_, err = io.Copy(w, in) | ||
return | ||
} |