Skip to content

Commit

Permalink
fix: amino-json custom encoders
Browse files Browse the repository at this point in the history
Custom encoders module_account and threshold_string need to output
fields in lexicographical order.
  • Loading branch information
maharifu committed Sep 18, 2024
1 parent 16dc491 commit b16697e
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions x/tx/signing/aminojson/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func keyFieldEncoder(_ *Encoder, msg protoreflect.Message, w io.Writer) error {
}

type moduleAccountPretty struct {
Address string `json:"address"`
PubKey string `json:"public_key"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Address string `json:"address"`
Name string `json:"name"`
Permissions []string `json:"permissions"`
PubKey string `json:"public_key"`
Sequence uint64 `json:"sequence"`
}

// moduleAccountEncoder replicates the behavior in
Expand Down Expand Up @@ -170,25 +170,29 @@ func thresholdStringEncoder(enc *Encoder, msg protoreflect.Message, w io.Writer)
if !ok {
return errors.New("thresholdStringEncoder: msg not a multisig.LegacyAminoPubKey")
}
_, err := fmt.Fprintf(w, `{"threshold":"%d","pubkeys":`, pk.Threshold)

_, err := io.WriteString(w, `{"pubkeys":`)
if err != nil {
return err
}

if len(pk.PublicKeys) == 0 {
_, err = io.WriteString(w, `[]}`)
return err
}

fields := msg.Descriptor().Fields()
pubkeysField := fields.ByName("public_keys")
pubkeys := msg.Get(pubkeysField).List()
_, err = io.WriteString(w, `[]`)
if err != nil {
return err
}
} else {
fields := msg.Descriptor().Fields()
pubkeysField := fields.ByName("public_keys")
pubkeys := msg.Get(pubkeysField).List()

err = enc.marshalList(pubkeys, pubkeysField, w)
if err != nil {
return err
err = enc.marshalList(pubkeys, pubkeysField, w)
if err != nil {
return err
}
}
_, err = io.WriteString(w, `}`)

_, err = fmt.Fprintf(w, `,"threshold":"%d"}`, pk.Threshold)
return err
}

Expand Down

0 comments on commit b16697e

Please sign in to comment.