From 7baed6d3619d0bd912a27393420f8a3ac466a987 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 7 Jun 2018 21:36:03 +0200 Subject: [PATCH 01/11] Add MustUnBech32() functions --- types/account.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/types/account.go b/types/account.go index 00d180a1c..fa471ab49 100644 --- a/types/account.go +++ b/types/account.go @@ -62,6 +62,15 @@ func GetAccAddressBech32(address string) (addr Address, err error) { return Address(bz), nil } +// must create an Address from a string +func MustGetAccAddressBech32(address string) Address { + addr, err := GetAccAddressBech32(address) + if err != nil { + panic(err) + } + return addr +} + // create a Pubkey from a string func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) { bz, err := getFromBech32(address, Bech32PrefixAccPub) @@ -77,6 +86,15 @@ func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) { return pk, nil } +// must create a Pubkey from a string +func MustGetAccPubkeyBec32(address string) crypto.PubKey { + pk, err := GetAccPubKeyBech32(address) + if err != nil { + panic(err) + } + return pk +} + // create an Address from a hex string func GetValAddressHex(address string) (addr Address, err error) { if len(address) == 0 { @@ -98,7 +116,16 @@ func GetValAddressBech32(address string) (addr Address, err error) { return Address(bz), nil } -//Decode a validator publickey into a public key +// must create an Address from a bech32 string +func MustGetValAddressBech32(address string) Address { + addr, err := GetValAddressBech32(address) + if err != nil { + panic(err) + } + return addr +} + +// decode a validator public key into a PubKey func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { bz, err := getFromBech32(pubkey, Bech32PrefixValPub) if err != nil { @@ -113,6 +140,15 @@ func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { return pk, nil } +// must decode a validator public key into a PubKey +func MustGetValPubKeyBech32(pubkey string) crypto.PubKey { + pk, err := GetValPubKeyBech32(pubkey) + if err != nil { + panic(err) + } + return pk +} + func getFromBech32(bech32str, prefix string) ([]byte, error) { if len(bech32str) == 0 { return nil, errors.New("must provide non-empty string") From cabb6e68fc7b1afe0693865cb4e4d0278e2f0735 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 7 Jun 2018 23:42:37 +0200 Subject: [PATCH 02/11] bech32ify x/bank/MsgSend --- x/bank/msgs.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/x/bank/msgs.go b/x/bank/msgs.go index 7836056de..ff91ba40f 100644 --- a/x/bank/msgs.go +++ b/x/bank/msgs.go @@ -1,6 +1,8 @@ package bank import ( + "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -53,7 +55,20 @@ func (msg MsgSend) ValidateBasic() sdk.Error { // Implements Msg. func (msg MsgSend) GetSignBytes() []byte { - b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form + var inputs, outputs []json.RawMessage + for _, input := range msg.Inputs { + inputs = append(inputs, input.GetSignBytes()) + } + for _, output := range msg.Outputs { + outputs = append(outputs, output.GetSignBytes()) + } + b, err := msgCdc.MarshalJSON(struct { + Inputs []json.RawMessage `json:"inputs"` + Outputs []json.RawMessage `json:"outputs"` + }{ + Inputs: inputs, + Outputs: outputs, + }) if err != nil { panic(err) } @@ -123,6 +138,24 @@ type Input struct { Coins sdk.Coins `json:"coins"` } +func (in Input) GetSignBytes() []byte { + addr, err := sdk.Bech32ifyAcc(in.Address) + if err != nil { + panic(err) + } + bin, err := msgCdc.MarshalJSON(struct { + Address string `json:"address"` + Coins sdk.Coins `json:"coins"` + }{ + Address: addr, + Coins: in.Coins, + }) + if err != nil { + panic(err) + } + return bin +} + // ValidateBasic - validate transaction input func (in Input) ValidateBasic() sdk.Error { if len(in.Address) == 0 { @@ -155,6 +188,24 @@ type Output struct { Coins sdk.Coins `json:"coins"` } +func (out Output) GetSignBytes() []byte { + addr, err := sdk.Bech32ifyAcc(out.Address) + if err != nil { + panic(err) + } + bin, err := msgCdc.MarshalJSON(struct { + Address string `json:"address"` + Coins sdk.Coins `json:"coins"` + }{ + Address: addr, + Coins: out.Coins, + }) + if err != nil { + panic(err) + } + return bin +} + // ValidateBasic - validate transaction output func (out Output) ValidateBasic() sdk.Error { if len(out.Address) == 0 { From f8896ee7aa374a4e5acf8584618fe990bc0166af Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 7 Jun 2018 23:47:23 +0200 Subject: [PATCH 03/11] Fix x/bank Msg testcases --- x/bank/msgs_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/x/bank/msgs_test.go b/x/bank/msgs_test.go index 8f9791c8d..82d61c765 100644 --- a/x/bank/msgs_test.go +++ b/x/bank/msgs_test.go @@ -187,12 +187,14 @@ func TestMsgSendGetSignBytes(t *testing.T) { } res := msg.GetSignBytes() - unmarshaledMsg := &MsgSend{} - msgCdc.UnmarshalJSON(res, unmarshaledMsg) - assert.Equal(t, &msg, unmarshaledMsg) + // TODO Why did we assert this? + /* + unmarshaledMsg := &MsgSend{} + msgCdc.UnmarshalJSON(res, unmarshaledMsg) + assert.Equal(t, &msg, unmarshaledMsg) + */ - // TODO bad results - expected := `{"type":"EAFDE32A2C87F8","value":{"inputs":[{"address":"696E707574","coins":[{"denom":"atom","amount":10}]}],"outputs":[{"address":"6F7574707574","coins":[{"denom":"atom","amount":10}]}]}}` + expected := `{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"denom":"atom","amount":10}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"denom":"atom","amount":10}]}]}` assert.Equal(t, expected, string(res)) } From a6c0db71e5d68a71499bb23e3aa01286506a70a9 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 00:32:14 +0200 Subject: [PATCH 04/11] Bech32ify GetSignBytes() for x/bank --- types/account.go | 36 ++++++++++++++++++++++++++++++++++++ x/bank/msgs.go | 26 +++++++++++++++----------- x/bank/msgs_test.go | 7 ------- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/types/account.go b/types/account.go index fa471ab49..2c35ca77d 100644 --- a/types/account.go +++ b/types/account.go @@ -26,21 +26,57 @@ func Bech32ifyAcc(addr Address) (string, error) { return bech32.ConvertAndEncode(Bech32PrefixAccAddr, addr.Bytes()) } +// MustBech32ifyAcc panics on bech32-encoding failure +func MustBech32ifyAcc(addr Address) string { + enc, err := Bech32ifyAcc(addr) + if err != nil { + panic(err) + } + return enc +} + // Bech32ifyAccPub takes AccountPubKey and returns the bech32 encoded string func Bech32ifyAccPub(pub crypto.PubKey) (string, error) { return bech32.ConvertAndEncode(Bech32PrefixAccPub, pub.Bytes()) } +// MustBech32ifyAccPub panics on bech32-encoding failure +func MustBech32ifyAccPub(pub crypto.PubKey) string { + enc, err := Bech32ifyAccPub(pub) + if err != nil { + panic(err) + } + return enc +} + // Bech32ifyVal returns the bech32 encoded string for a validator address func Bech32ifyVal(addr Address) (string, error) { return bech32.ConvertAndEncode(Bech32PrefixValAddr, addr.Bytes()) } +// MustBech32ifyVal panics on bech32-encoding failure +func MustBech32ifyVal(addr Address) string { + enc, err := Bech32ifyVal(addr) + if err != nil { + panic(err) + } + return enc +} + // Bech32ifyValPub returns the bech32 encoded string for a validator pubkey func Bech32ifyValPub(pub crypto.PubKey) (string, error) { return bech32.ConvertAndEncode(Bech32PrefixValPub, pub.Bytes()) } +// MustBech32ifyValPub pancis on bech32-encoding failure +func MustBech32ifyValPub(pub crypto.PubKey) string { + enc, err := Bech32ifyValPub(pub) + if err != nil { + panic(err) + } + return enc +} + // create an Address from a string func GetAccAddressHex(address string) (addr Address, err error) { if len(address) == 0 { diff --git a/x/bank/msgs.go b/x/bank/msgs.go index ff91ba40f..e0d73bf08 100644 --- a/x/bank/msgs.go +++ b/x/bank/msgs.go @@ -93,6 +93,8 @@ type MsgIssue struct { Outputs []Output `json:"outputs"` } +var _ sdk.Msg = MsgIssue{} + // NewMsgIssue - construct arbitrary multi-in, multi-out send msg. func NewMsgIssue(banker sdk.Address, out []Output) MsgIssue { return MsgIssue{Banker: banker, Outputs: out} @@ -117,7 +119,17 @@ func (msg MsgIssue) ValidateBasic() sdk.Error { // Implements Msg. func (msg MsgIssue) GetSignBytes() []byte { - b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form + var outputs []json.RawMessage + for _, output := range msg.Outputs { + outputs = append(outputs, output.GetSignBytes()) + } + b, err := msgCdc.MarshalJSON(struct { + Banker string `json:"banker"` + Outputs []json.RawMessage `json:"outputs"` + }{ + Banker: sdk.MustBech32ifyAcc(msg.Banker), + Outputs: outputs, + }) if err != nil { panic(err) } @@ -139,15 +151,11 @@ type Input struct { } func (in Input) GetSignBytes() []byte { - addr, err := sdk.Bech32ifyAcc(in.Address) - if err != nil { - panic(err) - } bin, err := msgCdc.MarshalJSON(struct { Address string `json:"address"` Coins sdk.Coins `json:"coins"` }{ - Address: addr, + Address: sdk.MustBech32ifyAcc(in.Address), Coins: in.Coins, }) if err != nil { @@ -189,15 +197,11 @@ type Output struct { } func (out Output) GetSignBytes() []byte { - addr, err := sdk.Bech32ifyAcc(out.Address) - if err != nil { - panic(err) - } bin, err := msgCdc.MarshalJSON(struct { Address string `json:"address"` Coins sdk.Coins `json:"coins"` }{ - Address: addr, + Address: sdk.MustBech32ifyAcc(out.Address), Coins: out.Coins, }) if err != nil { diff --git a/x/bank/msgs_test.go b/x/bank/msgs_test.go index 82d61c765..06adad1ef 100644 --- a/x/bank/msgs_test.go +++ b/x/bank/msgs_test.go @@ -187,13 +187,6 @@ func TestMsgSendGetSignBytes(t *testing.T) { } res := msg.GetSignBytes() - // TODO Why did we assert this? - /* - unmarshaledMsg := &MsgSend{} - msgCdc.UnmarshalJSON(res, unmarshaledMsg) - assert.Equal(t, &msg, unmarshaledMsg) - */ - expected := `{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"denom":"atom","amount":10}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"denom":"atom","amount":10}]}]}` assert.Equal(t, expected, string(res)) } From b166cc237faabab225a76fce7e692a794b390f10 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 00:35:13 +0200 Subject: [PATCH 05/11] Testcase/linter fixes --- x/bank/msgs.go | 2 ++ x/bank/msgs_test.go | 7 +------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/x/bank/msgs.go b/x/bank/msgs.go index e0d73bf08..48e62d835 100644 --- a/x/bank/msgs.go +++ b/x/bank/msgs.go @@ -150,6 +150,7 @@ type Input struct { Coins sdk.Coins `json:"coins"` } +// Return bytes to sign for Input func (in Input) GetSignBytes() []byte { bin, err := msgCdc.MarshalJSON(struct { Address string `json:"address"` @@ -196,6 +197,7 @@ type Output struct { Coins sdk.Coins `json:"coins"` } +// Return bytes to sign for Output func (out Output) GetSignBytes() []byte { bin, err := msgCdc.MarshalJSON(struct { Address string `json:"address"` diff --git a/x/bank/msgs_test.go b/x/bank/msgs_test.go index 06adad1ef..90b4869db 100644 --- a/x/bank/msgs_test.go +++ b/x/bank/msgs_test.go @@ -257,12 +257,7 @@ func TestMsgIssueGetSignBytes(t *testing.T) { } res := msg.GetSignBytes() - unmarshaledMsg := &MsgIssue{} - msgCdc.UnmarshalJSON(res, unmarshaledMsg) - assert.Equal(t, &msg, unmarshaledMsg) - - // TODO bad results - expected := `{"type":"72E617C06ABAD0","value":{"banker":"696E707574","outputs":[{"address":"6C6F616E2D66726F6D2D62616E6B","coins":[{"denom":"atom","amount":10}]}]}}` + expected := `{"banker":"cosmosaccaddr1d9h8qat5e4ehc5","outputs":[{"address":"cosmosaccaddr1d3hkzm3dveex7mfdvfsku6cwsauqd","coins":[{"denom":"atom","amount":10}]}]}` assert.Equal(t, expected, string(res)) } From 971e1489b21214a05ef7ce5623eaf5ec3c12adac Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 01:24:08 +0200 Subject: [PATCH 06/11] Bech32ify msg.GetSignBytes() for x/slashing & x/stake --- x/slashing/msg.go | 6 +++++- x/stake/msg.go | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/x/slashing/msg.go b/x/slashing/msg.go index d2676af81..acf5f9d93 100644 --- a/x/slashing/msg.go +++ b/x/slashing/msg.go @@ -30,7 +30,11 @@ func (msg MsgUnrevoke) GetSigners() []sdk.Address { return []sdk.Address{msg.Val // get the bytes for the message signer to sign on func (msg MsgUnrevoke) GetSignBytes() []byte { - b, err := cdc.MarshalJSON(msg) + b, err := cdc.MarshalJSON(struct { + ValidatorAddr string `json:"address"` + }{ + ValidatorAddr: sdk.MustBech32ifyAcc(msg.ValidatorAddr), + }) if err != nil { panic(err) } diff --git a/x/stake/msg.go b/x/stake/msg.go index 40bf609ee..a0922bb87 100644 --- a/x/stake/msg.go +++ b/x/stake/msg.go @@ -45,7 +45,20 @@ func (msg MsgCreateValidator) GetSigners() []sdk.Address { // get the bytes for the message signer to sign on func (msg MsgCreateValidator) GetSignBytes() []byte { - return msgCdc.MustMarshalBinary(msg) + b, err := msgCdc.MarshalJSON(struct { + Description + ValidatorAddr string `json:"address"` + PubKey string `json:"pubkey"` + Bond sdk.Coin `json:"bond"` + }{ + Description: msg.Description, + ValidatorAddr: sdk.MustBech32ifyVal(msg.ValidatorAddr), + PubKey: sdk.MustBech32ifyValPub(msg.PubKey), + }) + if err != nil { + panic(err) + } + return b } // quick validity check @@ -89,7 +102,13 @@ func (msg MsgEditValidator) GetSigners() []sdk.Address { // get the bytes for the message signer to sign on func (msg MsgEditValidator) GetSignBytes() []byte { - b, err := msgCdc.MarshalJSON(msg) + b, err := msgCdc.MarshalJSON(struct { + Description + ValidatorAddr string `json:"address"` + }{ + Description: msg.Description, + ValidatorAddr: sdk.MustBech32ifyVal(msg.ValidatorAddr), + }) if err != nil { panic(err) } @@ -133,7 +152,15 @@ func (msg MsgDelegate) GetSigners() []sdk.Address { // get the bytes for the message signer to sign on func (msg MsgDelegate) GetSignBytes() []byte { - b, err := msgCdc.MarshalJSON(msg) + b, err := msgCdc.MarshalJSON(struct { + DelegatorAddr string `json:"delegator_addr"` + ValidatorAddr string `json:"validator_addr"` + Bond sdk.Coin `json:"bond"` + }{ + DelegatorAddr: sdk.MustBech32ifyAcc(msg.DelegatorAddr), + ValidatorAddr: sdk.MustBech32ifyVal(msg.ValidatorAddr), + Bond: msg.Bond, + }) if err != nil { panic(err) } @@ -180,7 +207,15 @@ func (msg MsgUnbond) GetSigners() []sdk.Address { return []sdk.Address{msg.Deleg // get the bytes for the message signer to sign on func (msg MsgUnbond) GetSignBytes() []byte { - b, err := msgCdc.MarshalJSON(msg) + b, err := msgCdc.MarshalJSON(struct { + DelegatorAddr string `json:"delegator_addr"` + ValidatorAddr string `json:"validator_addr"` + Shares string `json:"shares"` + }{ + DelegatorAddr: sdk.MustBech32ifyAcc(msg.DelegatorAddr), + ValidatorAddr: sdk.MustBech32ifyVal(msg.ValidatorAddr), + Shares: msg.Shares, + }) if err != nil { panic(err) } From cbc6989ed60036e99ec74b02333a891bbabc50fc Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 01:49:37 +0200 Subject: [PATCH 07/11] Bech32ify msg.GetSignBytes() for x/ibc --- x/ibc/types.go | 51 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/x/ibc/types.go b/x/ibc/types.go index 09a853b20..76239102a 100644 --- a/x/ibc/types.go +++ b/x/ibc/types.go @@ -1,11 +1,20 @@ package ibc import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" wire "github.com/cosmos/cosmos-sdk/wire" ) +var ( + msgCdc *wire.Codec +) + +func init() { + msgCdc = wire.NewCodec() +} + // ------------------------------ // IBCPacket @@ -32,6 +41,26 @@ func NewIBCPacket(srcAddr sdk.Address, destAddr sdk.Address, coins sdk.Coins, } } +func (p IBCPacket) GetSignBytes() []byte { + b, err := msgCdc.MarshalJSON(struct { + SrcAddr string + DestAddr string + Coins sdk.Coins + SrcChain string + DestChain string + }{ + SrcAddr: sdk.MustBech32ifyAcc(p.SrcAddr), + DestAddr: sdk.MustBech32ifyAcc(p.DestAddr), + Coins: p.Coins, + SrcChain: p.SrcChain, + DestChain: p.DestChain, + }) + if err != nil { + panic(err) + } + return b +} + // validator the ibc packey func (ibcp IBCPacket) ValidateBasic() sdk.Error { if ibcp.SrcChain == ibcp.DestChain { @@ -60,12 +89,7 @@ func (msg IBCTransferMsg) GetSigners() []sdk.Address { return []sdk.Address{msg. // get the sign bytes for ibc transfer message func (msg IBCTransferMsg) GetSignBytes() []byte { - cdc := wire.NewCodec() - bz, err := cdc.MarshalBinary(msg) - if err != nil { - panic(err) - } - return bz + return msg.IBCPacket.GetSignBytes() } // validate ibc transfer message @@ -94,10 +118,17 @@ func (msg IBCReceiveMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.R // get the sign bytes for ibc receive message func (msg IBCReceiveMsg) GetSignBytes() []byte { - cdc := wire.NewCodec() - bz, err := cdc.MarshalBinary(msg) + b, err := msgCdc.MarshalJSON(struct { + IBCPacket json.RawMessage + Relayer string + Sequence int64 + }{ + IBCPacket: json.RawMessage(msg.IBCPacket.GetSignBytes()), + Relayer: sdk.MustBech32ifyAcc(msg.Relayer), + Sequence: msg.Sequence, + }) if err != nil { panic(err) } - return bz + return b } From 34ec53a76139d8f76b485d9b6d808d75338dc17b Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 01:50:59 +0200 Subject: [PATCH 08/11] Update CHANGELOG.md, update x/auth to avoid base64 --- CHANGELOG.md | 1 + x/auth/stdtx.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d6541691..e34a1468b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog BREAKING CHANGES +* msg.GetSignBytes() now returns bech32-encoded addresses in all cases FEATURES diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index 4858ae0b4..e08f77ee4 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -1,6 +1,8 @@ package auth import ( + "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" crypto "github.com/tendermint/go-crypto" ) @@ -83,11 +85,11 @@ func (fee StdFee) Bytes() []byte { // and the Sequence numbers for each signature (prevent // inchain replay and enforce tx ordering per account). type StdSignDoc struct { - ChainID string `json:"chain_id"` - Sequences []int64 `json:"sequences"` - FeeBytes []byte `json:"fee_bytes"` - MsgBytes []byte `json:"msg_bytes"` - AltBytes []byte `json:"alt_bytes"` + ChainID string `json:"chain_id"` + Sequences []int64 `json:"sequences"` + FeeBytes json.RawMessage `json:"fee_bytes"` + MsgBytes json.RawMessage `json:"msg_bytes"` + AltBytes json.RawMessage `json:"alt_bytes"` } // StdSignBytes returns the bytes to sign for a transaction. @@ -96,8 +98,8 @@ func StdSignBytes(chainID string, sequences []int64, fee StdFee, msg sdk.Msg) [] bz, err := msgCdc.MarshalJSON(StdSignDoc{ ChainID: chainID, Sequences: sequences, - FeeBytes: fee.Bytes(), - MsgBytes: msg.GetSignBytes(), + FeeBytes: json.RawMessage(fee.Bytes()), + MsgBytes: json.RawMessage(msg.GetSignBytes()), }) if err != nil { panic(err) From a583a70b7c5c09ef10b381affbfb465c91a85e77 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 01:59:10 +0200 Subject: [PATCH 09/11] Fix address type for x/slashing/MsgUnrevoke --- x/slashing/msg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/slashing/msg.go b/x/slashing/msg.go index acf5f9d93..561c92266 100644 --- a/x/slashing/msg.go +++ b/x/slashing/msg.go @@ -33,7 +33,7 @@ func (msg MsgUnrevoke) GetSignBytes() []byte { b, err := cdc.MarshalJSON(struct { ValidatorAddr string `json:"address"` }{ - ValidatorAddr: sdk.MustBech32ifyAcc(msg.ValidatorAddr), + ValidatorAddr: sdk.MustBech32ifyVal(msg.ValidatorAddr), }) if err != nil { panic(err) From 496d4681c2d0167713effc34b4a580ae267cee28 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 02:05:34 +0200 Subject: [PATCH 10/11] Add MsgUnrevoke.GetSignBytes() testcase, remove unused functions --- types/account.go | 36 ------------------------------------ x/slashing/msg_test.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 36 deletions(-) create mode 100644 x/slashing/msg_test.go diff --git a/types/account.go b/types/account.go index 2c35ca77d..381fb7af8 100644 --- a/types/account.go +++ b/types/account.go @@ -98,15 +98,6 @@ func GetAccAddressBech32(address string) (addr Address, err error) { return Address(bz), nil } -// must create an Address from a string -func MustGetAccAddressBech32(address string) Address { - addr, err := GetAccAddressBech32(address) - if err != nil { - panic(err) - } - return addr -} - // create a Pubkey from a string func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) { bz, err := getFromBech32(address, Bech32PrefixAccPub) @@ -122,15 +113,6 @@ func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) { return pk, nil } -// must create a Pubkey from a string -func MustGetAccPubkeyBec32(address string) crypto.PubKey { - pk, err := GetAccPubKeyBech32(address) - if err != nil { - panic(err) - } - return pk -} - // create an Address from a hex string func GetValAddressHex(address string) (addr Address, err error) { if len(address) == 0 { @@ -152,15 +134,6 @@ func GetValAddressBech32(address string) (addr Address, err error) { return Address(bz), nil } -// must create an Address from a bech32 string -func MustGetValAddressBech32(address string) Address { - addr, err := GetValAddressBech32(address) - if err != nil { - panic(err) - } - return addr -} - // decode a validator public key into a PubKey func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { bz, err := getFromBech32(pubkey, Bech32PrefixValPub) @@ -176,15 +149,6 @@ func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { return pk, nil } -// must decode a validator public key into a PubKey -func MustGetValPubKeyBech32(pubkey string) crypto.PubKey { - pk, err := GetValPubKeyBech32(pubkey) - if err != nil { - panic(err) - } - return pk -} - func getFromBech32(bech32str, prefix string) ([]byte, error) { if len(bech32str) == 0 { return nil, errors.New("must provide non-empty string") diff --git a/x/slashing/msg_test.go b/x/slashing/msg_test.go new file mode 100644 index 000000000..be7797107 --- /dev/null +++ b/x/slashing/msg_test.go @@ -0,0 +1,16 @@ +package slashing + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestMsgUnrevokeGetSignBytes(t *testing.T) { + addr := sdk.Address("abcd") + msg := NewMsgUnrevoke(addr) + bytes := msg.GetSignBytes() + assert.Equal(t, string(bytes), `{"address":"cosmosvaladdr1v93xxeqamr0mv"}`) +} From 946f952de1cf3d7fee2bbae5c0180814671ec799 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 8 Jun 2018 03:28:40 +0200 Subject: [PATCH 11/11] Linter fixes --- x/ibc/types.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x/ibc/types.go b/x/ibc/types.go index 76239102a..4924aec4b 100644 --- a/x/ibc/types.go +++ b/x/ibc/types.go @@ -41,6 +41,7 @@ func NewIBCPacket(srcAddr sdk.Address, destAddr sdk.Address, coins sdk.Coins, } } +//nolint func (p IBCPacket) GetSignBytes() []byte { b, err := msgCdc.MarshalJSON(struct { SrcAddr string @@ -62,11 +63,11 @@ func (p IBCPacket) GetSignBytes() []byte { } // validator the ibc packey -func (ibcp IBCPacket) ValidateBasic() sdk.Error { - if ibcp.SrcChain == ibcp.DestChain { +func (p IBCPacket) ValidateBasic() sdk.Error { + if p.SrcChain == p.DestChain { return ErrIdenticalChains(DefaultCodespace).Trace("") } - if !ibcp.Coins.IsValid() { + if !p.Coins.IsValid() { return sdk.ErrInvalidCoins("") } return nil