Skip to content

Commit

Permalink
refactor codec from keybase
Browse files Browse the repository at this point in the history
  • Loading branch information
denalimarsh committed Jun 12, 2020
1 parent c5f2792 commit 62ee2a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
4 changes: 3 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type KavaClient struct {
Network ChainNetwork
HTTP *rpcclient.HTTP
Keybase keys.KeyManager
Cdc *amino.Codec
}

// NewKavaClient creates a new KavaClient
Expand All @@ -41,6 +42,7 @@ func NewKavaClient(cdc *amino.Codec, mnemonic string, coinID uint32, rpcAddr str
Network: networkType,
HTTP: http,
Keybase: keyManager,
Cdc: cdc,
}
}

Expand Down Expand Up @@ -124,7 +126,7 @@ func (kc *KavaClient) sign(m sdk.Msg) ([]byte, error) {
}
}

signedMsg, err := kc.Keybase.Sign(*signMsg)
signedMsg, err := kc.Keybase.Sign(*signMsg, kc.Cdc)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions client/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// GetSwapByID gets an atomic swap on Kava by ID
func (kc *KavaClient) GetSwapByID(swapID tmbytes.HexBytes) (swap bep3.AtomicSwap, err error) {
params := bep3.NewQueryAtomicSwapByID(swapID)
bz, err := kc.Keybase.GetCodec().MarshalJSON(params)
bz, err := kc.Cdc.MarshalJSON(params)
if err != nil {
return bep3.AtomicSwap{}, err
}
Expand All @@ -26,7 +26,7 @@ func (kc *KavaClient) GetSwapByID(swapID tmbytes.HexBytes) (swap bep3.AtomicSwap
return bep3.AtomicSwap{}, err
}

err = kc.Keybase.GetCodec().UnmarshalJSON(result, &swap)
err = kc.Cdc.UnmarshalJSON(result, &swap)
if err != nil {
return bep3.AtomicSwap{}, err
}
Expand All @@ -36,7 +36,7 @@ func (kc *KavaClient) GetSwapByID(swapID tmbytes.HexBytes) (swap bep3.AtomicSwap
// GetAccount gets the account associated with an address on Kava
func (kc *KavaClient) GetAccount(addr sdk.AccAddress) (acc authtypes.BaseAccount, err error) {
params := authtypes.NewQueryAccountParams(addr)
bz, err := kc.Keybase.GetCodec().MarshalJSON(params)
bz, err := kc.Cdc.MarshalJSON(params)
if err != nil {
return authtypes.BaseAccount{}, err
}
Expand All @@ -48,7 +48,7 @@ func (kc *KavaClient) GetAccount(addr sdk.AccAddress) (acc authtypes.BaseAccount
return authtypes.BaseAccount{}, err
}

err = kc.Keybase.GetCodec().UnmarshalJSON(result, &acc)
err = kc.Cdc.UnmarshalJSON(result, &acc)
if err != nil {
return authtypes.BaseAccount{}, err
}
Expand Down
19 changes: 4 additions & 15 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"strings"

"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/go-bip39"
sdk "github.com/kava-labs/cosmos-sdk/types"
authtypes "github.com/kava-labs/cosmos-sdk/x/auth/types"
"github.com/cosmos/go-bip39"

"github.com/kava-labs/tendermint/crypto"
"github.com/kava-labs/tendermint/crypto/secp256k1"
Expand All @@ -31,9 +31,7 @@ const (
type KeyManager interface {
GetPrivKey() crypto.PrivKey
GetAddr() sdk.AccAddress
GetCodec() *amino.Codec
SetCodec(*amino.Codec)
Sign(authtypes.StdSignMsg) ([]byte, error)
Sign(authtypes.StdSignMsg, *amino.Codec) ([]byte, error)
}

// NewMnemonicKeyManager creates a new KeyManager from a mnenomic
Expand All @@ -54,7 +52,6 @@ func NewPrivateKeyManager(priKey string) (KeyManager, error) {
}

type keyManager struct {
cdc *amino.Codec
privKey crypto.PrivKey
addr sdk.AccAddress
mnemonic string
Expand All @@ -68,24 +65,16 @@ func (m *keyManager) GetAddr() sdk.AccAddress {
return m.addr
}

func (m *keyManager) GetCodec() *amino.Codec {
return m.cdc
}

func (m *keyManager) SetCodec(codec *amino.Codec) {
m.cdc = codec
}

// Sign signs a standard msg and marshals the result to bytes
func (m *keyManager) Sign(stdMsg authtypes.StdSignMsg) ([]byte, error) {
func (m *keyManager) Sign(stdMsg authtypes.StdSignMsg, cdc *amino.Codec) ([]byte, error) {
sig, err := m.makeSignature(stdMsg)
if err != nil {
return nil, err
}

newTx := authtypes.NewStdTx(stdMsg.Msgs, stdMsg.Fee, []authtypes.StdSignature{sig}, stdMsg.Memo)

bz, err := m.cdc.MarshalBinaryLengthPrefixed(&newTx)
bz, err := cdc.MarshalBinaryLengthPrefixed(&newTx)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 62ee2a5

Please sign in to comment.