Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom get signers #16184

Closed
wants to merge 3 commits into from
Closed
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
53 changes: 37 additions & 16 deletions api/cosmos/msg/v1/msg.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proto/cosmos/msg/v1/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ extend google.protobuf.MessageOptions {
// kind in case the signer information is contained within
// a message inside the cosmos message.
repeated string signer = 11110000;

// custom_signer indicates that a message does not define signers in the
// standard way using the signer option but instead defines its own custom
// way of defining signers. Frameworks and clients will need to provide
// custom logic to handle these messages.
bool custom_signer = 57938276;
}
25 changes: 18 additions & 7 deletions types/msgservice/msg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions x/tx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ require (
google.golang.org/grpc v1.55.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// temporary replace
replace cosmossdk.io/api => ../../api
29 changes: 26 additions & 3 deletions x/tx/signing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

msgv1 "cosmossdk.io/api/cosmos/msg/v1"
"cosmossdk.io/core/address"

msgv1 "cosmossdk.io/api/cosmos/msg/v1"
)

// Context is a context for retrieving the list of signers from a
Expand Down Expand Up @@ -110,7 +111,7 @@ func (c *Context) Validate() error {
for j := 0; j < sd.Methods().Len(); j++ {
md := sd.Methods().Get(j).Input()
_, err := c.getGetSignersFn(md)
if err != nil {
if err != nil && !errors.Is(err, NeedCustomSignersError) { // don't fail on custom signers
errs = append(errs, err)
}
}
Expand All @@ -123,6 +124,11 @@ func (c *Context) Validate() error {
}

func (c *Context) makeGetSignersFunc(descriptor protoreflect.MessageDescriptor) (getSignersFunc, error) {
isCustom := proto.GetExtension(descriptor.Options(), msgv1.E_CustomSigner).(bool)
if isCustom {
return nil, fmt.Errorf("%w: %s", NeedCustomSignersError, descriptor.FullName())
}

signersFields, err := getSignersFieldNames(descriptor)
if err != nil {
return nil, err
Expand Down Expand Up @@ -316,11 +322,28 @@ func (c *Context) ValidatorAddressCodec() address.Codec {
return c.validatorAddressCodec
}

// FileResolver returns the proto file resolver used by the context.
// FileResolver returns the protobuf file resolver used by the context.
func (c *Context) FileResolver() ProtoFileResolver {
return c.fileResolver
}

// TypeResolver returns the protobuf type resolver used by the context.
func (c *Context) TypeResolver() protoregistry.MessageTypeResolver {
return c.typeResolver
}

// DefineCustomGetSigners defines a custom GetSigners function for a given
// message type. It is defined as a function rather than a method on Context
// because of how go generics work.
//
// NOTE: if a custom signers function is defined, the message type used to
// define this function MUST be the concrete type passed to GetSigners,
// otherwise a runtime type error will occur.
func DefineCustomGetSigners[T proto.Message](ctx *Context, getSigners func(T) ([][]byte, error)) {
t := *new(T)
ctx.getSignersFuncs[t.ProtoReflect().Descriptor().FullName()] = func(msg proto.Message) ([][]byte, error) {
return getSigners(msg.(T))
}
}

var NeedCustomSignersError = errors.New("need custom signers function")