Bech32ify GetSignBytes() for x/bank
This commit is contained in:
parent
f8896ee7aa
commit
a6c0db71e5
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue