cosmos-sdk/client/v2/offchain/marshal.go
Julián Toledano 61c367d9d1
feat(client/v2/offchain): sign and verify file (#18626)
Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
2024-01-24 10:48:51 +00:00

44 lines
1.1 KiB
Go

package offchain
import (
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
apitx "cosmossdk.io/api/cosmos/tx/v1beta1"
v2flags "cosmossdk.io/client/v2/internal/flags"
)
// marshaller marshals Messages.
type marshaller interface {
Marshal(message proto.Message) ([]byte, error)
}
// getMarshaller returns the marshaller for the given marshaller id.
func getMarshaller(marshallerId, indent string, emitUnpopulated bool) (marshaller, error) {
switch marshallerId {
case v2flags.OutputFormatJSON:
return protojson.MarshalOptions{
Indent: indent,
EmitUnpopulated: emitUnpopulated,
}, nil
case v2flags.OutputFormatText:
return prototext.MarshalOptions{
Indent: indent,
EmitUnknown: emitUnpopulated,
}, nil
}
return nil, fmt.Errorf("marshaller with id '%s' not identified", marshallerId)
}
// marshalOffChainTx marshals a Tx using given marshaller.
func marshalOffChainTx(tx *apitx.Tx, marshaller marshaller) (string, error) {
bytesTx, err := marshaller.Marshal(tx)
if err != nil {
return "", err
}
return string(bytesTx), nil
}