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 command generating chain link file #583

Merged
merged 33 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4aed658
Build generate chain link json command
dadamu Aug 6, 2021
1c40e11
Fix typo
dadamu Aug 6, 2021
02e74d7
Make chain link json file not generate when testing
dadamu Aug 6, 2021
caa3429
Change thecomment of the flag
dadamu Aug 6, 2021
b7e3553
Seperate chain link generae function from cmd
dadamu Aug 6, 2021
e980647
Add comments
dadamu Aug 6, 2021
df91b02
Fix lint
dadamu Aug 9, 2021
6c7d35f
Fix lint
dadamu Aug 9, 2021
e68e436
Update changeset
dadamu Aug 9, 2021
07a1ec1
Allow generate link json using other chain config
dadamu Aug 11, 2021
7530053
Merge branch 'master' of github.com:desmos-labs/desmos into paul/gene…
dadamu Aug 11, 2021
14a9353
Build interactive prompt for creating chain link json file
dadamu Aug 11, 2021
bae26a5
Fix lint
dadamu Aug 11, 2021
1bee089
Remove unused package
dadamu Aug 11, 2021
a83d60f
Remove unused pkgs
dadamu Aug 11, 2021
5ab6370
Reset sum
dadamu Aug 11, 2021
38d0117
Merge branch 'master' of github.com:desmos-labs/desmos into paul/gene…
dadamu Aug 11, 2021
d87d452
Add go mod tidy in workflow
dadamu Aug 11, 2021
bd9c83f
Fix dependency
dadamu Aug 11, 2021
c4f5449
Remove go mod tidy in workflow
dadamu Aug 11, 2021
388d453
Update sum again with go1.16
dadamu Aug 11, 2021
bcf575e
Fix osmosis chain name
dadamu Aug 11, 2021
28178ba
Update app/desmos/cmd/create-chain-link.go
dadamu Aug 12, 2021
65a2f5a
Remove unused unpack interface
dadamu Aug 12, 2021
5fcd7d9
Fix syntax and apply suggestions
dadamu Aug 12, 2021
a0cf451
Merge branch 'paul/generate-chainlink-file-cmd' of github.com:desmos-…
dadamu Aug 12, 2021
4a392e3
Remove unused filename flag
dadamu Aug 12, 2021
53b90d5
Simplify unit test code
dadamu Aug 12, 2021
b598793
Fix mock generator
dadamu Aug 12, 2021
68c6d44
Update changeset
dadamu Aug 12, 2021
45149b6
Add prompt to ask filename
dadamu Aug 14, 2021
3231ea4
Small improvements and code re-arrangement
RiccardoM Aug 14, 2021
04afde8
Removed changeset in favor of CHANGELOG entry
RiccardoM Aug 14, 2021
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
5 changes: 5 additions & 0 deletions .changeset/entries/2021-08-09T04:27:29Z.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: added
module: x/profiles
pull_request: 583
description: Added generate chain link JSON file command
backward_compatible: true
79 changes: 79 additions & 0 deletions app/desmos/cmd/generate-chain-link-json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"encoding/hex"
"io/ioutil"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/desmos-labs/desmos/app"
profilescliutils "github.com/desmos-labs/desmos/x/profiles/client/utils"
"github.com/desmos-labs/desmos/x/profiles/types"
)

// GetGenerateChainlinkJSONCmd returns the command allowing to generate the chain link json file for creating chain link
func GetGenerateChainlinkJSONCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate-chain-link-json",
Short: "generate the chain link json for creating chain link with the key specified using the --from flag",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

chainLinkJSON, err := GenerateChainLinkJSON(
clientCtx,
app.Bech32MainPrefix,
)
if err != nil {
return err
}

cdc, _ := app.MakeCodecs()
bz, err := cdc.MarshalJSON(&chainLinkJSON)
if err != nil {
return err
}

filename, _ := cmd.Flags().GetString("filename")
if strings.TrimSpace(filename) != "" {
if err := ioutil.WriteFile("data.json", bz, 0600); err != nil {
return err
}
}
return clientCtx.PrintBytes(bz)
},
}
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String("filename", "data.json", "The name of output chain link json file. It does not generate the file if it is empty.")
return cmd
}

// GenerateChainLinkJSON returns ChainLinkJSON instance for creating chain link
func GenerateChainLinkJSON(clientCtx client.Context, prefix string) (profilescliutils.ChainLinkJSON, error) {

// generate signature
addr, _ := sdk.Bech32ifyAddressBytes(app.Bech32MainPrefix, clientCtx.GetFromAddress())
sig, pubkey, err := clientCtx.Keyring.Sign(clientCtx.GetFromName(), []byte(addr))
if err != nil {
return profilescliutils.ChainLinkJSON{}, err
}

// create chain link json
cdc, _ := app.MakeCodecs()
chainLinkJSON := profilescliutils.NewChainLinkJSON(
types.NewBech32Address(addr, app.Bech32MainPrefix),
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like prefix is never used, do you meant to use it here maybe?

types.NewProof(pubkey, hex.EncodeToString(sig), addr),
types.NewChainConfig(app.Bech32MainPrefix),
Copy link
Contributor

Choose a reason for hiding this comment

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

as above

)
if err := chainLinkJSON.UnpackInterfaces(cdc); err != nil {
return profilescliutils.ChainLinkJSON{}, err
}
return chainLinkJSON, nil
}
65 changes: 65 additions & 0 deletions app/desmos/cmd/generate-chain-link-json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd_test

import (
"encoding/hex"
"fmt"
"os"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/desmos-labs/desmos/app"
cmd "github.com/desmos-labs/desmos/app/desmos/cmd"
profilescliutils "github.com/desmos-labs/desmos/x/profiles/client/utils"
"github.com/desmos-labs/desmos/x/profiles/types"
)

func TestGetGenerateChainlinkJsonCmd(t *testing.T) {
cfg := sdk.GetConfig()
app.SetupConfig(cfg)

keyBase := keyring.NewInMemory()
algo := hd.Secp256k1
hdPath := sdk.GetConfig().GetFullFundraiserPath()

keyName := "test"
mnemonic := "clip toilet stairs jaguar baby over mosquito capital speed mule adjust eye print voyage verify smart open crack imitate auto gauge museum planet rebel"
_, err := keyBase.NewAccount(keyName, mnemonic, "", hdPath, algo)
require.NoError(t, err)

output := os.Stdout
clientCtx := client.Context{}.
WithKeyring(keyBase).
WithOutput(output)

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd.GetGenerateChainlinkJSONCmd(), []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, keyName),
fmt.Sprintf("--%s=%s", "filename", ""),
})
require.NoError(t, err)

key, err := keyBase.Key(keyName)
addr, _ := sdk.Bech32ifyAddressBytes(app.Bech32MainPrefix, key.GetAddress())
sig, pubkey, err := clientCtx.Keyring.Sign(keyName, []byte(addr))
require.NoError(t, err)

cdc, _ := app.MakeCodecs()
var data profilescliutils.ChainLinkJSON
err = cdc.UnmarshalJSON(out.Bytes(), &data)
require.NoError(t, err)

expected := profilescliutils.NewChainLinkJSON(
types.NewBech32Address(addr, app.Bech32MainPrefix),
types.NewProof(pubkey, hex.EncodeToString(sig), addr),
types.NewChainConfig(app.Bech32MainPrefix),
)

require.Equal(t, expected, data)

}
1 change: 1 addition & 0 deletions app/desmos/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
queryCommand(),
txCommand(),
GetSignCmd(),
GetGenerateChainlinkJSONCmd(),
keys.Commands(app.DefaultNodeHome),
)
}
Expand Down
1 change: 0 additions & 1 deletion app/desmos/cmd/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
func TestGetSignCmd(t *testing.T) {
cfg := sdk.GetConfig()
app.SetupConfig(cfg)
cfg.Seal()

keyBase := keyring.NewInMemory()
algo := hd.Secp256k1
Expand Down