Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use stdout when encrypt and decrypt data #561

Merged
merged 8 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions cmd/panacead/cmd/decrypt_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"

"github.com/btcsuite/btcd/btcec"
Expand All @@ -18,16 +19,16 @@ import (
// DecryptDataCmd decrypts data encrypted by the shared key which is generated by oracle private key and buyer's public key
func DecryptDataCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "decrypt-data [input-file-path] [output-file-path] [key-name] [encrypted-combined-key]",
Use: "decrypt-data [input-file-path] [key-name] [encrypted-secret-key]",
Short: "Decrypt data with encryptedCombinedKey",
Long: `
This command decrypts the encrypted data with the encrypted combinedKey.
This command decrypts the encrypted data with the encrypted secret key.
The encrypted combinedKey can be obtained from Oracle.
The key to be used for encryption should be stored in the localStore.
If not stored, please add the key first via the following command.
panacead keys add ...
`,
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
Expand All @@ -41,8 +42,8 @@ func DecryptDataCmd(defaultNodeHome string) *cobra.Command {
}
oraclePubKey := oracleResp.GetParams().MustDecodeOraclePublicKey()

// TODO I think we need to change it to get CombineKey from Oracle node.(Maybe...)
encryptedCombinedKey, err := base64.StdEncoding.DecodeString(args[3])
// TODO: I think we need to change it to get Secret from Oracle node.(Maybe...)
encryptedSecretKey, err := base64.StdEncoding.DecodeString(args[2])
if err != nil {
return err
}
Expand All @@ -52,12 +53,12 @@ func DecryptDataCmd(defaultNodeHome string) *cobra.Command {
return err
}

decryptedData, err := decrypt(clientCtx, args[2], encryptedCombinedKey, oraclePubKey, encryptedData)
decryptedData, err := decrypt(clientCtx, args[1], encryptedSecretKey, oraclePubKey, encryptedData)
if err != nil {
return err
}

err = os.WriteFile(args[1], decryptedData, 0644)
_, err = fmt.Fprintln(cmd.OutOrStdout(), decryptedData)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/panacead/cmd/encrypt_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"

"github.com/btcsuite/btcd/btcec"
Expand All @@ -17,15 +18,15 @@ import (

func EncryptDataCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "encrypt-data [input-file-path] [output-file-path] [key-name]",
Use: "encrypt-data [input-file-path] [key-name]",
Short: "Encrypt data with shared key which consists of oracle public key and provider's private key",
Long: `
This command can encrypt data with shared key which consists of oracle public key and provider's private key.
The key to be used for encryption should be stored in the localStore.
If not stored, please add the key first via the following command.
panacead keys add ...
`,
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
Expand All @@ -46,17 +47,17 @@ func EncryptDataCmd(defaultNodeHome string) *cobra.Command {

oraclePubKey := params.GetParams().GetOraclePublicKey()

encryptedData, err := encrypt(clientCtx, args[2], origData, oraclePubKey)
encryptedData, err := encrypt(clientCtx, args[1], origData, oraclePubKey)
if err != nil {
return err
}

encryptedDataBase64 := []byte(base64.StdEncoding.EncodeToString(encryptedData))
encryptedDataBase64 := base64.StdEncoding.EncodeToString(encryptedData)

if err := os.WriteFile(args[1], encryptedDataBase64, 0644); err != nil {
_, err = fmt.Fprintln(cmd.OutOrStdout(), encryptedDataBase64)
if err != nil {
return err
}

return nil
},
}
Expand Down