Skip to content

Commit

Permalink
fix KeyringEntry getters
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Apr 30, 2021
1 parent c9ae124 commit 2421d02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
65 changes: 41 additions & 24 deletions crypto/keyring/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package keyring
import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/ptypes"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"

)

var (
Expand All @@ -21,9 +20,9 @@ type Info interface {
// Human-readable type for key listing
GetName() string
// Public key
GetPubKey() *codectypes.Any
GetPubKey() (cryptotypes.PubKey, error)
// Address
GetAddress() types.AccAddress
GetAddress() (types.AccAddress, error)
// Bip44 Path
GetPath() (*BIP44Params, error)
}
Expand All @@ -32,15 +31,17 @@ func (ke KeyringEntry) GetName() string {
return ke.Name
}

func (ke KeyringEntry) GetPubKey() *codectypes.Any {
return ke.PubKey
func (ke KeyringEntry) GetPubKey() (pk cryptotypes.PubKey, err error) {
return ke.unmarshalAnytoPubKey()
}

// GetType implements Info interface
func (ke KeyringEntry) GetAddress() types.AccAddress {
var pk cryptotypes.PubKey
ptypes.UnmarshalAny(ke.PubKey, &pk) // handle error or not?
return pk.Address().Bytes()
func (ke KeyringEntry) GetAddress() (types.AccAddress, error) {
pk, err := ke.unmarshalAnytoPubKey()
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to unmarshal any to Pubkey")
}
return pk.Address().Bytes(),nil
}

func (ke *KeyringEntry) GetPath() (*BIP44Params, error) {
Expand All @@ -49,29 +50,45 @@ func (ke *KeyringEntry) GetPath() (*BIP44Params, error) {
l := ke.GetLedger()
tmp := l.Path
return tmp, nil
case ke.GetOffline() != nil, ke.GetLocal() != nil, ke.GetMulti() != nil: :
case ke.GetOffline() != nil, ke.GetLocal() != nil, ke.GetMulti() != nil:
return nil, fmt.Errorf("BIP44 Paths are not available for this type")
}
/*
should I handle this case? or just the better approach is:
if ke.GetLedger() != nil {
l := ke.GetLedger()
tmp := l.Path
return tmp, nil
}
return nil, fmt.Errorf("BIP44 Paths are not available for this type")
*/

return nil, fmt.Errorf("some error")
}


func (ke KeyringEntry) unmarshalAnytoPubKey() (pk cryptotypes.PubKey, err error) {
if err := ptypes.UnmarshalAny(ke.PubKey, &pk); err != nil {
return nil, err
}
return
}

// encoding info
func protoMarshalInfo(i Info) []byte {
ke, _ := i.(*KeyringEntry)
bz, _ := proto.Marshal(ke)
func protoMarshalInfo(i Info) ([]byte, error) {
ke, ok := i.(*KeyringEntry) // address error
bz, _ := proto.Marshal(ke) // address error
return bz

/*
or
bz, _ := proto.Marshal(i.(proto.Message))
return bz
*/
}

// decoding info
func protoUnmarshalInfo(bz []byte) (Info, error) {
var ke KeyringEntry
if err := proto.Unmarshal(bz, &ke); err != nil {
func protoUnmarshalInfo(bz []byte, cdc codec.Codec) (Info, error) {
// first merge master to my branch
var ke KeyringEntry // will not work cause we use any, use InterfaceRegistry
// cdcc.Marshaler.UnmarshalBinaryBare() // like proto.UnMarshal but works with Any
if err := cdc.Unmarshal(bz, &ke); err != nil {
return nil, sdkerrors.Wrap(err, "failed to unmarshal bytes to Info")
}

Expand Down
4 changes: 4 additions & 0 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,10 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
func (ks keystore) writeLocalKey(name string, priv types.PrivKey, algo hd.PubKeyType) (Info, error) {
// encrypt private key using keyring
pub := priv.PubKey()
//TODO
// pass codec to every function in keyring.go
// info := newLocalInfo(name, pub, string(legacy.Cdc.MustMarshalBinaryBare(priv)), algo)
// put in keystore
// TODO - store private key using proto, rather than legacy.Cdc.... - we already have proto for all keys
info := newLocalInfo(name, pub, string(legacy.Cdc.MustMarshalBinaryBare(priv)), algo)
if err := ks.writeInfo(info); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/crypto/keyring/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ message OfflineInfo {
message MultiInfo {
option (gogoproto.goproto_stringer) = false;

uint32 threshold = 1;
uint32 threshold = 1; //delete
}

0 comments on commit 2421d02

Please sign in to comment.