Files
sonr/app/encoding.go

72 lines
2.3 KiB
Go
Raw Permalink Normal View History

2025-10-03 14:45:52 -04:00
// Package app provides encoding utilities for the Sonr blockchain application.
2024-07-05 22:20:13 -04:00
package app
import (
"testing"
dbm "github.com/cosmos/cosmos-db"
2025-10-03 14:45:52 -04:00
"github.com/cosmos/gogoproto/proto"
2024-07-05 22:20:13 -04:00
"cosmossdk.io/log"
2025-10-03 14:45:52 -04:00
"cosmossdk.io/x/tx/signing"
2024-07-05 22:20:13 -04:00
2025-10-03 14:45:52 -04:00
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
2024-07-05 22:20:13 -04:00
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
2025-10-03 14:45:52 -04:00
sdk "github.com/cosmos/cosmos-sdk/types"
2024-07-05 22:20:13 -04:00
2025-10-03 14:45:52 -04:00
"github.com/sonr-io/sonr/app/params"
2024-07-05 22:20:13 -04:00
)
2025-10-03 14:45:52 -04:00
// MakeEncodingConfig creates a new EncodingConfig with all modules registered.
// This function is intended for testing purposes only, as it creates a temporary
// application instance to extract the encoding configuration.
2024-07-05 22:20:13 -04:00
func MakeEncodingConfig(t testing.TB) params.EncodingConfig {
t.Helper()
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
// note, this is not necessary when using app wiring, as depinject can be directly used (see root_v2.go)
tempApp := NewChainApp(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
simtestutil.NewAppOptionsWithFlagHome(t.TempDir()),
2025-10-03 14:45:52 -04:00
EVMAppOptions,
2024-07-05 22:20:13 -04:00
)
return makeEncodingConfig(tempApp)
}
2025-10-03 14:45:52 -04:00
// makeEncodingConfig extracts the encoding configuration from a ChainApp instance.
// It returns an EncodingConfig struct containing the interface registry, codec,
// transaction config, and amino codec.
func makeEncodingConfig(tempApp *ChainApp) params.EncodingConfig {
2024-07-05 22:20:13 -04:00
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
TxConfig: tempApp.TxConfig(),
Amino: tempApp.LegacyAmino(),
}
return encodingConfig
}
2025-10-03 14:45:52 -04:00
// GetInterfaceRegistry creates and returns a new interface registry with proper
// address codecs configured. This registry is used for protobuf Any type
// registration and message routing.
func GetInterfaceRegistry() types.InterfaceRegistry {
interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
},
})
if err != nil {
panic(err)
}
return interfaceRegistry
}