From 62ee2a5eafa2b42b9ec5ece2ebd4b9a5598c0853 Mon Sep 17 00:00:00 2001 From: denalimarsh Date: Fri, 12 Jun 2020 19:23:08 +0200 Subject: [PATCH] refactor codec from keybase --- client/client.go | 4 +++- client/queries.go | 8 ++++---- keys/keys.go | 19 ++++--------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/client/client.go b/client/client.go index 4cee6bb..b39fd13 100644 --- a/client/client.go +++ b/client/client.go @@ -20,6 +20,7 @@ type KavaClient struct { Network ChainNetwork HTTP *rpcclient.HTTP Keybase keys.KeyManager + Cdc *amino.Codec } // NewKavaClient creates a new KavaClient @@ -41,6 +42,7 @@ func NewKavaClient(cdc *amino.Codec, mnemonic string, coinID uint32, rpcAddr str Network: networkType, HTTP: http, Keybase: keyManager, + Cdc: cdc, } } @@ -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 } diff --git a/client/queries.go b/client/queries.go index 4bb1c5e..b8c434f 100644 --- a/client/queries.go +++ b/client/queries.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/keys/keys.go b/keys/keys.go index 7bf5280..a28f36e 100644 --- a/keys/keys.go +++ b/keys/keys.go @@ -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" @@ -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 @@ -54,7 +52,6 @@ func NewPrivateKeyManager(priKey string) (KeyManager, error) { } type keyManager struct { - cdc *amino.Codec privKey crypto.PrivKey addr sdk.AccAddress mnemonic string @@ -68,16 +65,8 @@ 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 @@ -85,7 +74,7 @@ func (m *keyManager) Sign(stdMsg authtypes.StdSignMsg) ([]byte, error) { 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 }