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

refactor!: use x/tx/signing in client.TxConfig #15822

Merged
merged 66 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
db53c1d
Hard break client.TxConfig
kocubinski Apr 12, 2023
0019cb9
incorporate changes from #15713
kocubinski Apr 12, 2023
f44fee6
Hard fixing
kocubinski Apr 12, 2023
49c18df
Merge branch 'main' into kocubinski/tx-config-break
kocubinski Apr 12, 2023
14539c4
go mod tidy
kocubinski Apr 12, 2023
5f2b4de
fix breakage in codec from latest main
kocubinski Apr 12, 2023
02c2340
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 13, 2023
147cbdf
remove replace for x/tx
kocubinski Apr 13, 2023
55e1d36
revert codec/ compat for x/tx 0.6.0
kocubinski Apr 13, 2023
55acfe3
x/tx v0.5.1
kocubinski Apr 13, 2023
bedd4b9
add err handling
kocubinski Apr 13, 2023
21927ef
checkpoint, proto gen
kocubinski Apr 13, 2023
623bacf
adjust lots of tests
kocubinski Apr 13, 2023
43ea1b4
fixed ante tests
kocubinski Apr 13, 2023
e32f007
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 13, 2023
bff8090
merged fix
kocubinski Apr 13, 2023
0a25910
revert (temporarily) x/tx to 0.5.1
kocubinski Apr 13, 2023
3f0a4f2
rm files
kocubinski Apr 13, 2023
d24dbc2
fix tx_test
kocubinski Apr 13, 2023
2e62758
revert x/tx
kocubinski Apr 13, 2023
2c9bd78
comment
kocubinski Apr 14, 2023
863c869
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 14, 2023
e0cecb1
maybe some fixes
kocubinski Apr 14, 2023
d0317b3
fix more test/build failures
kocubinski Apr 14, 2023
8365099
use x/tx 0.5.3
kocubinski Apr 14, 2023
ec82685
Merge branch 'main' into kocubinski/tx-config-break
kocubinski Apr 14, 2023
dc0d717
update rosetta go.mod
kocubinski Apr 14, 2023
c4c4cdd
Merge branch 'kocubinski/tx-config-break' of github.com:cosmos/cosmos…
kocubinski Apr 14, 2023
6e691fc
liniting
kocubinski Apr 14, 2023
0010ceb
linting fixes
kocubinski Apr 14, 2023
0b9eb03
lint fix
kocubinski Apr 14, 2023
4843d63
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 17, 2023
caf6987
clean up config
kocubinski Apr 17, 2023
a246afc
go mod tidy
kocubinski Apr 17, 2023
c6f2033
tidy rosetta
kocubinski Apr 17, 2023
978f4f4
rwmutex
kocubinski Apr 17, 2023
a8069dc
lint fix
kocubinski Apr 17, 2023
3851398
proper lazy init with sync.once
kocubinski Apr 17, 2023
6c363f5
clean up todos
kocubinski Apr 18, 2023
c66aefd
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 18, 2023
8fc3108
clean up, comments
kocubinski Apr 18, 2023
7364f41
attempt remove redundant arg
kocubinski Apr 18, 2023
a0acb58
move lazy registry
kocubinski Apr 18, 2023
f04a10c
store init error
kocubinski Apr 18, 2023
59ab3ce
test fixes
kocubinski Apr 18, 2023
ae3fe65
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 18, 2023
79c0f89
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 18, 2023
d4bb593
handle default sign mode todos
kocubinski Apr 18, 2023
96a693d
x/tx 0.5.5
kocubinski Apr 18, 2023
0796a6c
go mod tidy
kocubinski Apr 18, 2023
ad658b5
rm more todos
kocubinski Apr 18, 2023
c270393
lint fix
kocubinski Apr 18, 2023
4ad00c5
clean up handler creation API
kocubinski Apr 18, 2023
8e516b2
fix more test bugs
kocubinski Apr 18, 2023
f138251
finish simd/root command
kocubinski Apr 18, 2023
8973802
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 18, 2023
76c6363
a little clean up
kocubinski Apr 19, 2023
dd3cd93
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 19, 2023
05d52af
Add CHANGELOG entries for API breaks
kocubinski Apr 19, 2023
f3211b8
Add UPGRADING entries
kocubinski Apr 19, 2023
147dc83
Merge branch 'main' into kocubinski/tx-config-break
kocubinski Apr 19, 2023
d1af44c
PR fixes
kocubinski Apr 19, 2023
e8398d2
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/t…
kocubinski Apr 19, 2023
cd4f11f
lint fix
kocubinski Apr 19, 2023
2bb6750
Refactor NewTxConfig
kocubinski Apr 20, 2023
3bcaa98
lazy loading won't work in InterfaceRegistry ctr won't work yet.
kocubinski Apr 20, 2023
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
Prev Previous commit
Next Next commit
clean up config
  • Loading branch information
kocubinski committed Apr 17, 2023
commit caf698719d1ace716d34de0c827fb2f6eb4cfaa8
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
cosmossdk.io/log v1.0.0
cosmossdk.io/math v1.0.0
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc
cosmossdk.io/x/tx v0.5.3
cosmossdk.io/x/tx v0.5.4
github.com/99designs/keyring v1.2.1
github.com/armon/go-metrics v0.4.1
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ cosmossdk.io/math v1.0.0 h1:ro9w7eKx23om2tZz/VM2Pf+z2WAbGX1yDQQOJ6iGeJw=
cosmossdk.io/math v1.0.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc h1:9piuA+NYmhe+SyMPtMoboLw/djgDbrI3dD5TG020Tnk=
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc/go.mod h1:UFF5rmjN7WYVfxo6ArdY/l1+yyWMURBWOmSJypGqFHQ=
cosmossdk.io/x/tx v0.5.3 h1:8h/Z18uHvFnootFWaMluWyotyp+2uLy+V8gAUJ4Cfi0=
cosmossdk.io/x/tx v0.5.3/go.mod h1:Oh3Kh+IPOfMEILNxVd2e8SLqRrIjYHpdGBfDg4ghU/k=
cosmossdk.io/x/tx v0.5.4 h1:tOLDbNDT9F9OvAjGfw3K3rjNhSickQDI9S1iSeovB/A=
cosmossdk.io/x/tx v0.5.4/go.mod h1:Oh3Kh+IPOfMEILNxVd2e8SLqRrIjYHpdGBfDg4ghU/k=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
cloud.google.com/go/storage v1.30.0 // indirect
cosmossdk.io/collections v0.1.0 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/x/tx v0.5.3 // indirect
cosmossdk.io/x/tx v0.5.4 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
6 changes: 3 additions & 3 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ func NewRootCmd() *cobra.Command {
// TODO Currently, the TxConfig below doesn't include Textual, so
// an error will arise when using the --textual flag.
// ref: https://github.com/cosmos/cosmos-sdk/issues/11970
txt, err := txmodule.NewTextualWithGRPCConn(initClientCtx)
opts, err := txmodule.NewSignModeOptionsWithMetadataQueryFn(txmodule.NewGRPCCoinMetadataQueryFn(initClientCtx))
if err != nil {
return err
}
signModes, err := signing.APISignModesToInternal(encodingConfig.TxConfig.SignModeHandler().SupportedModes())
if err != nil {
return err
}
txConfigWithTextual := tx.NewTxConfigWithTextual(
txConfigWithTextual := tx.NewTxConfigWithOptions(
codec.NewProtoCodec(encodingConfig.InterfaceRegistry),
signModes,
txt,
opts,
)
initClientCtx = initClientCtx.WithTxConfig(txConfigWithTextual)

Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.0.0-20230117113717-50e7c4a4ceff
cosmossdk.io/x/nft v0.0.0-20230113085233-fae3332d62fc
cosmossdk.io/x/tx v0.5.3
cosmossdk.io/x/tx v0.5.4
cosmossdk.io/x/upgrade v0.0.0-20230127052425-54c8e1568335
github.com/cometbft/cometbft v0.37.1-0.20230411132551-3a91d155e664
github.com/cosmos/cosmos-db v1.0.0-rc.1
Expand Down
47 changes: 42 additions & 5 deletions types/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"sync"

"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

txsigning "cosmossdk.io/x/tx/signing"
)

// CustomProtobufType defines the interface custom gogo proto types must implement
Expand All @@ -24,21 +27,55 @@ type CustomProtobufType interface {
var (
mu sync.Mutex
mergedRegistry *protoregistry.Files
_ txsigning.ProtoFileResolver = lazyProtoRegistry{}
)

func MergedProtoRegistry() *protoregistry.Files {
mu.Lock()
type lazyProtoRegistry struct{}

func (l lazyProtoRegistry) init() error {
mu.Lock()
defer mu.Unlock()

if mergedRegistry != nil {
return mergedRegistry
return nil
}

var err error
mergedRegistry, err = proto.MergedRegistry()
if err != nil {
panic(err)
return err
}

return nil
}

func (l lazyProtoRegistry) FindFileByPath(s string) (protoreflect.FileDescriptor, error) {
if mergedRegistry == nil {
if err := l.init(); err != nil {
return nil, err
}
}
return mergedRegistry.FindFileByPath(s)
}

func (l lazyProtoRegistry) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
if mergedRegistry == nil {
if err := l.init(); err != nil {
return nil, err
}
}
return mergedRegistry.FindDescriptorByName(name)
}

func (l lazyProtoRegistry) RangeFiles(f func(protoreflect.FileDescriptor) bool) {
if mergedRegistry == nil {
if err := l.init(); err != nil {
panic(err)
}
}
mergedRegistry.RangeFiles(f)
}

return mergedRegistry
func MergedProtoRegistry() txsigning.ProtoFileResolver {
return lazyProtoRegistry{}
}
1 change: 1 addition & 0 deletions x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
Value: anyPk.Value,
},
}
// TODO supply protoRegistry.Files or decoder further up
decodeCtx, err := decode.NewDecoder(decode.Options{ProtoFiles: sdk.MergedProtoRegistry()})
if err != nil {
return ctx, err
Expand Down
13 changes: 6 additions & 7 deletions x/auth/ante/sigverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ func TestSigVerification(t *testing.T) {
enabledSignModes := []signing.SignMode{signing.SignMode_SIGN_MODE_DIRECT, signing.SignMode_SIGN_MODE_TEXTUAL, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}
// Since TEXTUAL is not enabled by default, we create a custom TxConfig
// here which includes it.
txt, err := txmodule.NewTextualWithGRPCConn(suite.clientCtx)
require.NoError(t, err)
suite.clientCtx.TxConfig = authtx.NewTxConfigWithTextual(
opts, err := txmodule.NewSignModeOptionsWithMetadataQueryFn(txmodule.NewGRPCCoinMetadataQueryFn(suite.clientCtx))
suite.clientCtx.TxConfig = authtx.NewTxConfigWithOptions(
codec.NewProtoCodec(suite.encCfg.InterfaceRegistry),
enabledSignModes,
txt,
opts,
)
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()

Expand All @@ -163,12 +162,12 @@ func TestSigVerification(t *testing.T) {
gasLimit := testdata.NewTestGasLimit()

spkd := ante.NewSetPubKeyDecorator(suite.accountKeeper)
txt, err = txmodule.NewTextualWithBankKeeper(suite.txBankKeeper)
opts, err = txmodule.NewSignModeOptionsWithMetadataQueryFn(txmodule.NewBankKeeperCoinMetadataQueryFn(suite.txBankKeeper))
require.NoError(t, err)
anteTxConfig := authtx.NewTxConfigWithTextual(
anteTxConfig := authtx.NewTxConfigWithOptions(
codec.NewProtoCodec(suite.encCfg.InterfaceRegistry),
enabledSignModes,
txt,
opts,
)
svd := ante.NewSigVerificationDecorator(suite.accountKeeper, anteTxConfig.SignModeHandler())
antehandler := sdk.ChainAnteDecorators(spkd, svd)
Expand Down
17 changes: 15 additions & 2 deletions x/auth/tx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type config struct {
// NOTE: Use NewTxConfigWithHandler to provide a custom signing handler in case the sign mode
// is not supported by default (eg: SignMode_SIGN_MODE_EIP_191). Use NewTxConfigWithTextual
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

// to enable SIGN_MODE_TEXTUAL (for testing purposes for now).
//
// We prefer to use depinject to provide client.TxConfig, but we permit this constructor usage. Within the SDK,
// this constructor is primarily used in tests, but also sees usage in app chains like:
// https://github.com/evmos/evmos/blob/719363fbb92ff3ea9649694bd088e4c6fe9c195f/encoding/config.go#L37
// TODO: collapse enabledSignModes and customSignModes
func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode,
customSignModes ...txsigning.SignModeHandler,
Expand All @@ -39,7 +43,8 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin
}
}

// prefer depinject usage but permit this; it is primary used in tests.
// protoFiles should perhaps be a parameter to this function, but the choice was made here to not break the
// NewTxConfig API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything else to elaborate on this choice? I'm still wary of the internalized reference to a global value here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protoFiles := sdk.MergedProtoRegistry()
typeResolver := protoregistry.GlobalTypes
signersContext, err := txsigning.NewGetSignersContext(txsigning.GetSignersOptions{ProtoFiles: protoFiles})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary because of #15873. Just use codec.InterfaceRegistry().SigningContext()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a compile error right now. I think it is OK to land this in main first then work on #15873 (which is still failing tests) then refactor?

Expand All @@ -64,13 +69,21 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin
return NewTxConfigWithHandler(protoCodec, makeSignModeHandler(enabledSignModes, signModeOptions, customSignModes...))
}

func NewTxConfigWithOptions(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode,
signModeOptions SignModeOptions, customSignModes ...txsigning.SignModeHandler,
) client.TxConfig {
return NewTxConfigWithHandler(protoCodec, makeSignModeHandler(enabledSignModes, signModeOptions, customSignModes...))
}

// NewTxConfigWithTextual is like NewTxConfig with the ability to add
// a SIGN_MODE_TEXTUAL renderer. It is currently still EXPERIMENTAL, for should
// be used for TESTING purposes only, until Textual is fully released.
//
// Deprecated: use NewTxConfigWithOptions instead.
func NewTxConfigWithTextual(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode,
signModeOptions SignModeOptions, customSignModes ...txsigning.SignModeHandler,
) client.TxConfig {
return NewTxConfigWithHandler(protoCodec, makeSignModeHandler(enabledSignModes, signModeOptions, customSignModes...))
return NewTxConfigWithOptions(protoCodec, enabledSignModes, signModeOptions, customSignModes...)
}

// NewTxConfigWithHandler returns a new protobuf TxConfig using the provided ProtoCodec and signing handler.
Expand Down
124 changes: 121 additions & 3 deletions x/auth/tx/config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package tx

import (
"context"
"fmt"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoregistry"

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
txsigning "cosmossdk.io/x/tx/signing"
"cosmossdk.io/x/tx/signing/aminojson"
"cosmossdk.io/x/tx/signing/directaux"
"cosmossdk.io/x/tx/signing/textual"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -16,6 +26,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

func init() {
Expand Down Expand Up @@ -52,7 +63,7 @@ type ModuleOutputs struct {
// TODO
// probably move to x/tx. I would do this now but I'm blocked; latest x/tx does not compile or interop with latest sdk
func ProvideSignModeOptions(bk BankKeeper) tx.SignModeOptions {
opts, err := NewTextualWithBankKeeper(bk)
opts, err := NewSignModeOptionsWithMetadataQueryFn(NewBankKeeperCoinMetadataQueryFn(bk))
if err != nil {
panic(err)
}
Expand All @@ -62,9 +73,9 @@ func ProvideSignModeOptions(bk BankKeeper) tx.SignModeOptions {
func ProvideModule(in ModuleInputs) ModuleOutputs {
var txConfig client.TxConfig
if in.CustomSignModeHandlers == nil {
txConfig = tx.NewTxConfigWithTextual(in.ProtoCodecMarshaler, tx.DefaultSignModes, in.SignModeOptions)
txConfig = tx.NewTxConfigWithOptions(in.ProtoCodecMarshaler, tx.DefaultSignModes, in.SignModeOptions)
} else {
txConfig = tx.NewTxConfigWithTextual(in.ProtoCodecMarshaler, tx.DefaultSignModes, in.SignModeOptions,
txConfig = tx.NewTxConfigWithOptions(in.ProtoCodecMarshaler, tx.DefaultSignModes, in.SignModeOptions,
in.CustomSignModeHandlers()...)
}

Expand Down Expand Up @@ -130,3 +141,110 @@ func newAnteHandler(txConfig client.TxConfig, in ModuleInputs) (sdk.AnteHandler,

return anteHandler, nil
}

// NewBankKeeperCoinMetadataQueryFn creates a new Textual struct using the given
// BankKeeper to retrieve coin metadata.
//
// Note: Once we switch to ADR-033, and keepers become ADR-033 clients to each
// other, this function could probably be deprecated in favor of
// `NewTextualWithGRPCConn`.
func NewBankKeeperCoinMetadataQueryFn(bk BankKeeper) textual.CoinMetadataQueryFn {
return func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
res, err := bk.DenomMetadata(ctx, &types.QueryDenomMetadataRequest{Denom: denom})
if err != nil {
return nil, metadataExists(err)
}

m := &bankv1beta1.Metadata{
Base: res.Metadata.Base,
Display: res.Metadata.Display,
// fields below are not strictly needed by Textual
// but added here for completeness.
Description: res.Metadata.Description,
Name: res.Metadata.Name,
Symbol: res.Metadata.Symbol,
Uri: res.Metadata.URI,
UriHash: res.Metadata.URIHash,
}
m.DenomUnits = make([]*bankv1beta1.DenomUnit, len(res.Metadata.DenomUnits))
for i, d := range res.Metadata.DenomUnits {
m.DenomUnits[i] = &bankv1beta1.DenomUnit{
Denom: d.Denom,
Exponent: d.Exponent,
Aliases: d.Aliases,
}
}

return m, nil
}
}

// NewGRPCCoinMetadataQueryFn returns a new Textual instance where the metadata
// queries are done via gRPC using the provided GRPC client connection. In the
// SDK, you can pass a client.Context as the GRPC connection.
//
// Example:
//
// clientCtx := client.GetClientContextFromCmd(cmd)
// txt := tx.NewTextualWithGRPCConn(clientCtxx)
func NewGRPCCoinMetadataQueryFn(grpcConn grpc.ClientConnInterface) textual.CoinMetadataQueryFn {
return func(ctx context.Context, denom string) (*bankv1beta1.Metadata, error) {
bankQueryClient := bankv1beta1.NewQueryClient(grpcConn)
res, err := bankQueryClient.DenomMetadata(ctx, &bankv1beta1.QueryDenomMetadataRequest{
Denom: denom,
})
if err != nil {
return nil, metadataExists(err)
}

return res.Metadata, nil
}
}

// NewSignModeOptionsWithMetadataQueryFn creates a new SignModeOptions instance
func NewSignModeOptionsWithMetadataQueryFn(fn textual.CoinMetadataQueryFn) (tx.SignModeOptions, error) {
protoFiles := sdk.MergedProtoRegistry()
typeResolver := protoregistry.GlobalTypes
signersContext, err := txsigning.NewGetSignersContext(txsigning.GetSignersOptions{ProtoFiles: protoFiles})
if err != nil {
return tx.SignModeOptions{}, err
}

aminoJSONEncoder := aminojson.NewAminoJSON()
signModeOptions := tx.SignModeOptions{
DirectAux: &directaux.SignModeHandlerOptions{
FileResolver: protoFiles,
TypeResolver: typeResolver,
SignersContext: signersContext,
},
AminoJSON: &aminojson.SignModeHandlerOptions{
FileResolver: protoFiles,
TypeResolver: typeResolver,
Encoder: &aminoJSONEncoder,
},
Textual: &textual.SignModeOptions{
CoinMetadataQuerier: fn,
FileResolver: protoFiles,
TypeResolver: typeResolver,
},
}

return signModeOptions, nil
}

// metadataExists parses the error, and only propagates the error if it's
// different than a "not found" error.
func metadataExists(err error) error {
status, ok := grpcstatus.FromError(err)
if !ok {
return err
}

// This means we didn't find any metadata for this denom. Returning
// empty metadata.
if status.Code() == codes.NotFound {
return nil
}

return err
}
Loading