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: plugin proof of concept #10452

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
19 changes: 19 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package plugin

import "github.com/gogo/protobuf/proto"

type Host interface {
// Expect tells the host to expect that every registered protobuf type which
// implements the protobuf interface name (cosmos_proto.implements_interface)
// is expected to register a golang interface implementation of type golangInterfaceType.
Expect(protoInterfaceName string, golangInterfaceType interface{})

// Register registers plugin handler constructors which are functions taking a plugin target
// and returning a handler type. Ex: func(secp256k1.PubKey) crypto.PubKey.
Register(handlerConstructors ...interface{})

// Resolve resolves the handler for the provided target. Ex:
// var pubKey crypto.PubKey
// host.Resolve(&secp256k1.PubKey{}, &pubKey)
Resolve(target proto.Message, handler interface{}) error
}
48 changes: 48 additions & 0 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package plugin_test

import (
"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/plugin"
)

type PubKeyModule struct{}

type AuthModule struct{}

type SecpHandler struct{ *secp256k1.PubKey }

func NewSecpHandler(pubKey *secp256k1.PubKey) types.PubKey { return &SecpHandler{PubKey: pubKey} }

type EdHandler struct{ *ed25519.PubKey }

func NewEdHandler(pubKey *ed25519.PubKey) types.PubKey { return &EdHandler{PubKey: pubKey} }

func (m PubKeyModule) Provide(host plugin.Host) {
host.Register(
NewSecpHandler,
NewEdHandler,
)
}

func (m AuthModule) Provide(host plugin.Host) AuthKeeper {
host.Expect("cosmos.crypto.PubKey", (*types.PubKey)(nil))
return AuthKeeper{pluginHost: host}
}

type AuthKeeper struct {
pluginHost plugin.Host
}

func (k AuthKeeper) GetAddress(pubKeyProtoType proto.Message) types.Address {
var pubKey types.PubKey
err := k.pluginHost.Resolve(pubKeyProtoType, &pubKey)
if err != nil {
panic(err)
}

return pubKey.Address()
}