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

Migrate BaseAccount PubKey to protobuf PublicKey #6928

Closed
wants to merge 14 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
16 changes: 4 additions & 12 deletions client/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"encoding/json"
"io"
"os"

Expand All @@ -10,7 +9,6 @@ import (
"github.com/pkg/errors"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
yaml "gopkg.in/yaml.v2"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -231,16 +229,10 @@ func (ctx Context) PrintOutputLegacy(toPrint interface{}) error {
}

func (ctx Context) printOutput(out []byte) error {
if ctx.OutputFormat == "text" {
// handle text format by decoding and re-encoding JSON as YAML
var j interface{}

err := json.Unmarshal(out, &j)
if err != nil {
return err
}
var err error

out, err = yaml.Marshal(j)
if ctx.OutputFormat == "text" {
out, err = codec.MarshalYAML(out)
if err != nil {
return err
}
Expand All @@ -251,7 +243,7 @@ func (ctx Context) printOutput(out []byte) error {
writer = os.Stdout
}

_, err := writer.Write(out)
_, err = writer.Write(out)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions codec/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codec

import (
"encoding/json"

"gopkg.in/yaml.v2"
)

// MarshalYAML marshals the provided bytes content by decoding the bytes to
// JSON, and then encoding YAML.
func MarshalYAML(bz []byte) ([]byte, error) {
// generate YAML by decoding and re-encoding JSON as YAML
var j interface{}

err := json.Unmarshal(bz, &j)
if err != nil {
return nil, err
}

bz, err = yaml.Marshal(j)
if err != nil {
return nil, err
}

return bz, nil
}
49 changes: 49 additions & 0 deletions codec/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package codec_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)

func TestMarshalYAML(t *testing.T) {
dog := &testdata.Dog{
Size_: "small",
Name: "Spot",
}
any, err := types.NewAnyWithValue(dog)
require.NoError(t, err)
hasAnimal := &testdata.HasAnimal{
Animal: any,
X: 0,
}

// proto
protoCdc := codec.NewProtoCodec(NewTestInterfaceRegistry())
bz, err := codec.MarshalYAML(protoCdc.MustMarshalJSON(hasAnimal))
require.NoError(t, err)
require.Equal(t, `animal:
'@type': /testdata.Dog
name: Spot
size: small
x: "0"
`, string(bz))

// amino
aminoCdc := &codec.LegacyAmino{testdata.NewTestAmino()}
bz, err = codec.MarshalYAML(aminoCdc.MustMarshalJSON(hasAnimal))
require.NoError(t, err)
require.Equal(t, `type: testdata/HasAnimal
value:
animal:
type: testdata/Dog
value:
name: Spot
size: small
`, string(bz))
}
3 changes: 2 additions & 1 deletion proto/cosmos/auth/v1beta1/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosmos.auth.v1beta1;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types";

Expand All @@ -15,7 +16,7 @@ message BaseAccount {
option (cosmos_proto.implements_interface) = "AccountI";

bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
bytes pub_key = 2 [(gogoproto.jsontag) = "public_key,omitempty", (gogoproto.moretags) = "yaml:\"public_key\""];
google.protobuf.Any pub_key = 2 [(gogoproto.jsontag) = "public_key,omitempty", (gogoproto.moretags) = "yaml:\"public_key\""];
uint64 account_number = 3 [(gogoproto.moretags) = "yaml:\"account_number\""];
uint64 sequence = 4;
}
Expand Down
59 changes: 25 additions & 34 deletions x/auth/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (
"fmt"
"strings"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"

"github.com/gogo/protobuf/proto"

"github.com/tendermint/tendermint/crypto"
yaml "gopkg.in/yaml.v2"

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

Expand All @@ -30,7 +35,10 @@ func NewBaseAccount(address sdk.AccAddress, pubKey crypto.PubKey, accountNumber,
Sequence: sequence,
}

acc.SetPubKey(pubKey)
err := acc.SetPubKey(pubKey)
if err != nil {
panic(err)
}

return acc
}
Expand Down Expand Up @@ -64,22 +72,27 @@ func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {

// GetPubKey - Implements sdk.AccountI.
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
if len(acc.PubKey) == 0 {
content, ok := acc.PubKey.GetCachedValue().(crypto.PubKey)
if !ok {
return nil
}

amino.MustUnmarshalBinaryBare(acc.PubKey, &pk)
return pk
return content
}

// SetPubKey - Implements sdk.AccountI.
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
if pubKey == nil {
acc.PubKey = nil
} else {
acc.PubKey = amino.MustMarshalBinaryBare(pubKey)
protoMsg, ok := pubKey.(proto.Message)
if !ok {
return sdkerrors.ErrInvalidPubKey
}

any, err := codectypes.NewAnyWithValue(protoMsg)
if err != nil {
return nil
}

acc.PubKey = any

return nil
}

Expand Down Expand Up @@ -107,7 +120,7 @@ func (acc *BaseAccount) SetSequence(seq uint64) error {

// Validate checks for errors on the account fields
func (acc BaseAccount) Validate() error {
if len(acc.PubKey) != 0 && acc.Address != nil &&
if acc.PubKey != nil && acc.Address != nil &&
!bytes.Equal(acc.GetPubKey().Address().Bytes(), acc.Address.Bytes()) {
return errors.New("account address and pubkey address do not match")
}
Expand All @@ -120,35 +133,13 @@ func (acc BaseAccount) String() string {
return out.(string)
}

type baseAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
}

// MarshalYAML returns the YAML representation of an account.
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
alias := baseAccountPretty{
Address: acc.Address,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}

if acc.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
if err != nil {
return nil, err
}

alias.PubKey = pks
}

bz, err := yaml.Marshal(alias)
protoCdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
bz, err := codec.MarshalYAML(protoCdc.MustMarshalJSON(&acc))
if err != nil {
return nil, err
}

return string(bz), err
}

Expand Down
Loading