Skip to content

Commit

Permalink
feat: added cipher text decrypt function (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuguen committed Jun 28, 2022
1 parent 91a8646 commit 3b23488
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
98 changes: 98 additions & 0 deletions client/decrypt/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package decrypt

import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tendermint/tendermint/libs/cli"
"io/ioutil"
)

const (
FlagCipherText = "cipher-text"
FlagCipherTextPath = "path"
)

func Command(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "decrypt [name] (--cipher-text [ciphertext] | --path [ciphertext-file])",
Short: "Decrypt and output the ciphertext file encrypted with ECDSA PublicKey.",
Long: `This command can decrypt ciphertext encrypted with ECDSA PublicKey.
And your key should be stored in the localStore.
(According to the following command (panacead keys add ...)`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

privKeyHex, err := keyring.NewUnsafe(clientCtx.Keyring).UnsafeExportPrivKeyHex(args[0])
if err != nil {
return err
}

privKey, err := hex.DecodeString(privKeyHex)
if err != nil {
return err
}

content, err := getContent(cmd.Flags())

if err != nil {
return fmt.Errorf("failed read cipherText. %w", err)
}

ecdsaPrivKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKey)
if err != nil {
return err
}
plainText, err := btcec.Decrypt(ecdsaPrivKey, content)
if err != nil {
return err
}

cmd.Println(string(plainText))

return nil
},
}

cmd.PersistentFlags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.PersistentFlags().String(flags.FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used")
cmd.PersistentFlags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
cmd.PersistentFlags().String(cli.OutputFlag, "text", "Output format (text|json)")
cmd.Flags().String(FlagCipherText, "", "Cipher text in the form of a byte array")
cmd.Flags().String(FlagCipherTextPath, "", "Path to the file where the cipher text in the form of a byte array is stored")

return cmd
}

func getContent(flag *pflag.FlagSet) ([]byte, error) {
cipherText, err := flag.GetString(FlagCipherText)
if err != nil {
return nil, err
}
cipherTextPath, err := flag.GetString(FlagCipherTextPath)
if err != nil {
return nil, err
}

if len(cipherText) == 0 && len(cipherTextPath) == 0 {
return nil, fmt.Errorf("either --text or --path should be required")
} else if len(cipherText) != 0 && len(cipherTextPath) != 0 {
return nil, fmt.Errorf("only one of --text or --path should be specified")
}

if len(cipherText) != 0 {
return []byte(cipherText), nil
}

content, err := ioutil.ReadFile(cipherTextPath)
if err != nil {
return nil, err
}
return content, nil
}
2 changes: 2 additions & 0 deletions cmd/panacead/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/medibloc/panacea-core/v2/app"
"github.com/medibloc/panacea-core/v2/client/decrypt"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
)
Expand Down Expand Up @@ -106,6 +107,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
queryCommand(),
txCommand(),
keys.Commands(app.DefaultNodeHome),
decrypt.Command(app.DefaultNodeHome),
)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/CosmWasm/wasmd v0.21.0
github.com/btcsuite/btcd v0.22.0-beta
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/cosmos/cosmos-sdk v0.42.11
github.com/cosmos/go-bip39 v1.0.0
Expand Down

0 comments on commit 3b23488

Please sign in to comment.