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

add: signature command in iovnscli #263

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ RUN mv /build/iovnscli iovnscli
RUN mv /build/iovnsd iovnsd
COPY ./scripts .
RUN chmod +x init.sh
RUN bash ./init.sh
RUN sh ./init.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be RUN sh -c 'yes | ./init.sh' to address my comment on the deletion from init.sh.

CMD iovnsd start
88 changes: 88 additions & 0 deletions cmd/iovnscli/extension/signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package extension

import (
"bytes"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"io"
"os"
)

type SignatureSchema struct {
ChanID string `json:"@chain_id"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the chain_id as we discussed. It will only add confusion since it's irrelevant for the signing of a message.

Type string `json:"@type"`
Message []byte `json:"text"`
Sig string `json:"sig"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the tag to signature.

PubKey sdk.AccAddress `json:"address"`
}

type sigCommand struct {
file string
}

func (s *sigCommand) applyFlags(flag *pflag.FlagSet) {
flag.StringP("file", "f", "", "")
}

func (s *sigCommand) extractFlags(flag *pflag.FlagSet) (err error) {
s.file, err = flag.GetString("file")
if err != nil {
return err
}
return
}

func SignatureCommand() *cobra.Command {
req := new(sigCommand)
cmd := &cobra.Command{
Use: "sign",
RunE: func(cmd *cobra.Command, args []string) (err error) {
// extract flags
err = req.extractFlags(cmd.Flags())
if err != nil {
return
}
// retrieve file
f, err := os.Open(req.file)
if err != nil {
return
}
defer f.Close()
buf := &bytes.Buffer{}
_, err = io.Copy(buf, f)
if err != nil {
return
}
cliCtx := context.NewCLIContext()
kb, err := keys.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), cmd.InOrStdin())
if err != nil {
return err
}
sig, _, err := kb.Sign(cliCtx.GetFromName(), keys.DefaultBIP39Passphrase, buf.Bytes())
if err != nil {
return
}
messageJSON, err := json.Marshal(&SignatureSchema{
ChanID: cliCtx.ChainID,
Type: "message",
Sig: string(sig),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Sig should be base64 encoded so that it looks familiar to users.

Message: buf.Bytes(),
PubKey: cliCtx.GetFromAddress(),
Copy link
Contributor

@davepuchyr davepuchyr Jul 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be pubkey, not address, no? We need the curve type, too.

})
if err != nil {
return
}
cmd.Println(fmt.Sprintf("%s", messageJSON))
return nil
},
}
req.applyFlags(cmd.Flags())
return cmd
}
3 changes: 3 additions & 0 deletions cmd/iovnscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/iov-one/iovns/cmd/iovnscli/extension"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -65,6 +66,7 @@ func main() {
}

// Construct Root Command
sign := flags.PostCommands(extension.SignatureCommand())[0]
rootCmd.AddCommand(
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
Expand All @@ -77,6 +79,7 @@ func main() {
flags.LineBreak,
version.Cmd,
flags.NewCompletionCmd(rootCmd, true),
sign,
)

// Add flags and prefix all env exposed with AA
Expand Down
4 changes: 0 additions & 4 deletions scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

set -o nounset

echo -n "This script will remove all existing iovns configurations, wallets etc. Do you want to proceed? [Y/n] "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's nice to destroy existing wallets, etc without warning. If this is blocking some CI or script then just pipe yes into it, eg yes | ./init.sh.

read YN
if ! echo $YN | grep -v -q -i "n"; then exit; fi

rm -rf "$HOME/.iovnscli"
rm -rf "$HOME/.iovnsd"
# init config files
Expand Down