8000 fix(network): fix validator pubkey parser from gentx by Pantani · Pull Request #2456 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(network): fix validator pubkey parser from gentx #2456

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

Merged
merged 4 commits into from
May 10, 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
3 changes: 2 additions & 1 deletion ignite/pkg/cosmosgen/cosmosgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cosmosgen
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ignite-hq/cli/ignite/pkg/cosmosanalysis/module"
"github.com/ignite-hq/cli/ignite/pkg/protoanalysis"
"github.com/stretchr/testify/require"
)

func TestVuexStoreModulePath(t *testing.T) {
Expand Down
26 changes: 13 additions & 13 deletions ignite/pkg/cosmosutil/gentx.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package cosmosutil

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"

"github.com/tendermint/tendermint/crypto/ed25519"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var GentxFilename = "gentx.json"

type (
// PubKey represents the public key in bytes array
PubKey []byte

// GentxInfo represents the basic info about gentx file
GentxInfo struct {
DelegatorAddress string
PubKey PubKey
PubKey ed25519.PubKey
SelfDelegation sdk.Coin
Memo string
}
Expand All @@ -30,7 +30,8 @@ type (
DelegatorAddress string `json:"delegator_address"`
ValidatorAddress string `json:"validator_address"`
PubKey struct {
Key string `json:"key"`
Type string `json:"@type"`
Key string `json:"key"`
} `json:"pubkey"`
Value struct {
Denom string `json:"denom"`
Expand All @@ -42,12 +43,6 @@ type (
}
)

// Equal returns true if the public keys are equal
func (pb PubKey) Equal(key []byte) bool {
res := bytes.Compare(pb, key)
return res == 0
}

// GentxFromPath returns GentxInfo from the json file
func GentxFromPath(path string) (info GentxInfo, gentx []byte, err error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
Expand Down Expand Up @@ -80,7 +75,12 @@ func ParseGentx(gentx []byte) (info GentxInfo, file []byte, err error) {

info.Memo = stargateGentx.Body.Memo
info.DelegatorAddress = stargateGentx.Body.Messages[0].DelegatorAddress
info.PubKey = []byte(stargateGentx.Body.Messages[0].PubKey.Key)

pb := stargateGentx.Body.Messages[0].PubKey.Key
info.PubKey, err = base64.StdEncoding.DecodeString(pb)
if err != nil {
return info, gentx, fmt.Errorf("invalid validator public key %s", err.Error())
}

amount, ok := sdk.NewIntFromString(stargateGentx.Body.Messages[0].Value.Amount)
if !ok {
Expand Down
40 changes: 9 additions & 31 deletions ignite/pkg/cosmosutil/gentx_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package cosmosutil_test

import (
"encoding/base64"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/ignite-hq/cli/ignite/pkg/cosmosutil"
)

func TestParseGentx(t *testing.T) {
pk1, err := base64.StdEncoding.DecodeString("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs=")
require.NoError(t, err)
pk2, err := base64.StdEncoding.DecodeString("OL+EIoo7DwyaBFDbPbgAhwS5rvgIqoUa0x8qWqzfQVQ=")
require.NoError(t, err)

tests := []struct {
name string
gentxPath string
Expand All @@ -21,7 +28,7 @@ func TestParseGentx(t *testing.T) {
gentxPath: "testdata/gentx1.json",
wantInfo: cosmosutil.GentxInfo{
DelegatorAddress: "cosmos1dd246yq6z5vzjz9gh8cff46pll75yyl8ygndsj",
PubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
PubKey: ed25519.PubKey(pk1),
SelfDelegation: sdk.Coin{
Denom: "stake",
Amount: sdk.NewInt(95000000),
Expand All @@ -33,7 +40,7 @@ func TestParseGentx(t *testing.T) {
gentxPath: "testdata/gentx2.json",
wantInfo: cosmosutil.GentxInfo{
DelegatorAddress: "cosmos1mmlqwyqk7neqegffp99q86eckpm4pjah3ytlpa",
PubKey: []byte("OL+EIoo7DwyaBFDbPbgAhwS5rvgIqoUa0x8qWqzfQVQ="),
PubKey: ed25519.PubKey(pk2),
SelfDelegation: sdk.Coin{
Denom: "stake",
Amount: sdk.NewInt(95000000),
Expand Down Expand Up @@ -62,32 +69,3 @@ func TestParseGentx(t *testing.T) {
})
}
}

func TestPubKey_Equal(t *testing.T) {
tests := []struct {
name string
pb []byte
cmpKey []byte
want bool
}{
{
name: "equal public keys",
pb: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
cmpKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
want: true,
},
{
name: "not equal public keys",
pb: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
cmpKey: []byte("EIoo7DwyaBFDbPbgAhwS5rvgIqoUa0x8qWqzfQVQ="),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pb := cosmosutil.PubKey(tt.pb)
got := pb.Equal(tt.cmpKey)
require.Equal(t, tt.want, got)
})
}
}
7 changes: 4 additions & 3 deletions ignite/services/network/networktypes/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

launchtypes "github.com/tendermint/spn/x/launch/types"
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/ignite-hq/cli/ignite/pkg/cosmosutil"
"github.com/ignite-hq/cli/ignite/pkg/xtime"
Expand Down Expand Up @@ -80,11 +81,11 @@ func VerifyAddValidatorRequest(req *launchtypes.RequestContent_GenesisValidator)
}

// Check validator address
if !info.PubKey.Equal(consPubKey) {
if !info.PubKey.Equals(ed25519.PubKey(consPubKey)) {
return fmt.Errorf(
"the consensus pub key %s doesn't match the one inside the gentx %s",
string(consPubKey),
string(info.PubKey),
ed25519.PubKey(consPubKey).String(),
info.PubKey.String(),
)
}

Expand Down
20 changes: 12 additions & 8 deletions ignite/services/network/networktypes/request_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package networktypes_test

import (
"encoding/base64"
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
launchtypes "github.com/tendermint/spn/x/launch/types"
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/ignite-hq/cli/ignite/services/network/networktypes"
)
Expand All @@ -30,6 +32,8 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
]
}
}`)
pk, err := base64.StdEncoding.DecodeString("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs=")
require.NoError(t, err)

tests := []struct {
name string
Expand All @@ -42,7 +46,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: gentx,
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
Expand All @@ -54,7 +58,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: gentx,
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "122.114.800.11"),
},
Expand All @@ -67,7 +71,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: []byte(`{}`),
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
Expand All @@ -80,7 +84,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: gentx,
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("foo", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
Expand All @@ -93,7 +97,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: gentx,
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(3)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
Expand All @@ -106,20 +110,20 @@ func TestVerifyAddValidatorRequest(t *testing.T) {
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1dd246yq6z5vzjz9gh8cff46pll75yyl8c5tt7g",
GenTx: gentx,
ConsPubKey: []byte("cosmos1gkheudhhjsvq0s8fxt7p6pwe0k3k30kepcnz9p="),
ConsPubKey: ed25519.PubKey("cosmos1gkheudhhjsvq0s8fxt7p6pwe0k3k30kepcnz9p="),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
},
want: fmt.Errorf("the consensus pub key cosmos1gkheudhhjsvq0s8fxt7p6pwe0k3k30kepcnz9p= doesn't match the one inside the gentx aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
want: fmt.Errorf("the consensus pub key PubKeyEd25519{636F736D6F7331676B6865756468686A737671307338667874377036707765306B336B33306B6570636E7A39703D} doesn't match the one inside the gentx PubKeyEd25519{69E40B0893A35D4C81EDEBCEA1D23899BAEC848B77BE11C69727090DB52468CB}"),
},
{
name: "invalid validator address",
req: &launchtypes.RequestContent_GenesisValidator{
GenesisValidator: &launchtypes.GenesisValidator{
Address: "spn1gkheudhhjsvq0s8fxt7p6pwe0k3k30keaytytm",
GenTx: gentx,
ConsPubKey: []byte("aeQLCJOjXUyB7evOodI4mbrshIt3vhHGlycJDbUkaMs="),
ConsPubKey: ed25519.PubKey(pk),
SelfDelegation: sdk.NewCoin("stake", sdk.NewInt(95000000)),
Peer: launchtypes.NewPeerConn("nodeid", "127.163.0.1:2446"),
},
Expand Down
0