Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions x/tx/signing/aminojson/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ func cosmosDecEncoder(_ *Encoder, v protoreflect.Value, w io.Writer) error {
}
}

// cosmosAddressBytesEncoder encodes `bytes` fields that represent addresses in bech32 format.
func cosmosAddressBytesEncoder(enc *Encoder, v protoreflect.Value, w io.Writer) error {
if enc.addressCodec == nil {
return fmt.Errorf("address codec not set in encoder so we can't render address bytes as bech32 strings")
}

addrStr, err := enc.addressCodec.BytesToString(v.Bytes())
if err != nil {
return fmt.Errorf("failed to convert bytes to address string: %w", err)
}

return jsonMarshal(w, addrStr)
}

// nullSliceAsEmptyEncoder replicates the behavior at:
// https://github.com/cosmos/cosmos-sdk/blob/be9bd7a8c1b41b115d58f4e76ee358e18a52c0af/types/coin.go#L199-L205
func nullSliceAsEmptyEncoder(enc *Encoder, v protoreflect.Value, w io.Writer) error {
Expand Down
10 changes: 8 additions & 2 deletions x/tx/signing/aminojson/json_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"io"
"sort"

"cosmossdk.io/core/address"

Check failure on line 10 in x/tx/signing/aminojson/json_marshal.go

View workflow job for this annotation

GitHub Actions / Analyze

File is not properly formatted (gci)

Check failure on line 10 in x/tx/signing/aminojson/json_marshal.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -44,6 +45,8 @@
TypeResolver signing.TypeResolver
// FileResolver is used to resolve protobuf file descriptors TypeURL when TypeResolver fails.
FileResolver signing.ProtoFileResolver
// AddressCodec is used to render address bytes as JSON strings.
AddressCodec address.Codec
}

// Encoder is a JSON encoder that uses the Amino JSON encoding rules for protobuf messages.
Expand All @@ -60,6 +63,7 @@
enumsAsString bool
aminoNameAsTypeURL bool
marshalMappings bool
addressCodec address.Codec
}

// NewEncoder returns a new Encoder capable of serializing protobuf messages to JSON using the Amino JSON encoding
Expand All @@ -73,8 +77,9 @@
}
enc := Encoder{
cosmosProtoScalarEncoders: map[string]FieldEncoder{
cosmosDecType: cosmosDecEncoder,
"cosmos.Int": cosmosIntEncoder,
cosmosDecType: cosmosDecEncoder,
"cosmos.Int": cosmosIntEncoder,
"cosmos.AddressBytes": cosmosAddressBytesEncoder,
},
aminoMessageEncoders: map[string]MessageEncoder{
"key_field": keyFieldEncoder,
Expand All @@ -97,6 +102,7 @@
enumsAsString: options.EnumAsString,
aminoNameAsTypeURL: options.AminoNameAsTypeURL,
marshalMappings: options.MarshalMappings,
addressCodec: options.AddressCodec,
}
return enc
}
Expand Down
Loading