-
Notifications
You must be signed in to change notification settings - Fork 46
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
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 1c40e11
Fix typo
dadamu 02e74d7
Make chain link json file not generate when testing
dadamu caa3429
Change thecomment of the flag
dadamu b7e3553
Seperate chain link generae function from cmd
dadamu e980647
Add comments
dadamu df91b02
Fix lint
dadamu 6c7d35f
Fix lint
dadamu e68e436
Update changeset
dadamu 07a1ec1
Allow generate link json using other chain config
dadamu 7530053
Merge branch 'master' of github.com:desmos-labs/desmos into paul/gene…
dadamu 14a9353
Build interactive prompt for creating chain link json file
dadamu bae26a5
Fix lint
dadamu 1bee089
Remove unused package
dadamu a83d60f
Remove unused pkgs
dadamu 5ab6370
Reset sum
dadamu 38d0117
Merge branch 'master' of github.com:desmos-labs/desmos into paul/gene…
dadamu d87d452
Add go mod tidy in workflow
dadamu bd9c83f
Fix dependency
dadamu c4f5449
Remove go mod tidy in workflow
dadamu 388d453
Update sum again with go1.16
dadamu bcf575e
Fix osmosis chain name
dadamu 28178ba
Update app/desmos/cmd/create-chain-link.go
dadamu 65a2f5a
Remove unused unpack interface
dadamu 5fcd7d9
Fix syntax and apply suggestions
dadamu a0cf451
Merge branch 'paul/generate-chainlink-file-cmd' of github.com:desmos-…
dadamu 4a392e3
Remove unused filename flag
dadamu 53b90d5
Simplify unit test code
dadamu b598793
Fix mock generator
dadamu 68c6d44
Update changeset
dadamu 45149b6
Add prompt to ask filename
dadamu 3231ea4
Small improvements and code re-arrangement
RiccardoM 04afde8
Removed changeset in favor of CHANGELOG entry
RiccardoM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,5 @@ | ||
type: added | ||
module: x/profiles | ||
pull_request: 583 | ||
description: Added generate chain link JSON file command | ||
backward_compatible: true |
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,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), | ||
types.NewProof(pubkey, hex.EncodeToString(sig), addr), | ||
types.NewChainConfig(app.Bech32MainPrefix), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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,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) | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?