Skip to content

Commit

Permalink
Bech32ify GetSignBytes() for x/bank
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Jun 7, 2018
1 parent f8896ee commit a6c0db7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
36 changes: 36 additions & 0 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions x/bank/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions x/bank/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit a6c0db7

Please sign in to comment.