github.com/InjectiveLabs/sdk-go@v1.53.0/client/chain/context.go (about) 1 package chain 2 3 import ( 4 "os" 5 6 "cosmossdk.io/x/tx/signing" 7 "github.com/cosmos/cosmos-sdk/codec/address" 8 "github.com/cosmos/gogoproto/proto" 9 10 "github.com/cosmos/cosmos-sdk/client" 11 "github.com/cosmos/cosmos-sdk/codec" 12 "github.com/cosmos/cosmos-sdk/codec/types" 13 "github.com/cosmos/cosmos-sdk/crypto/keyring" 14 "github.com/cosmos/cosmos-sdk/std" 15 "github.com/cosmos/cosmos-sdk/x/auth/tx" 16 "github.com/pkg/errors" 17 18 keyscodec "github.com/InjectiveLabs/sdk-go/chain/crypto/codec" 19 20 wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" 21 22 auction "github.com/InjectiveLabs/sdk-go/chain/auction/types" 23 exchange "github.com/InjectiveLabs/sdk-go/chain/exchange/types" 24 insurance "github.com/InjectiveLabs/sdk-go/chain/insurance/types" 25 ocr "github.com/InjectiveLabs/sdk-go/chain/ocr/types" 26 oracle "github.com/InjectiveLabs/sdk-go/chain/oracle/types" 27 peggy "github.com/InjectiveLabs/sdk-go/chain/peggy/types" 28 tokenfactory "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types" 29 chaintypes "github.com/InjectiveLabs/sdk-go/chain/types" 30 wasmx "github.com/InjectiveLabs/sdk-go/chain/wasmx/types" 31 32 evidencetypes "cosmossdk.io/x/evidence/types" 33 feegranttypes "cosmossdk.io/x/feegrant" 34 upgradetypes "cosmossdk.io/x/upgrade/types" 35 cosmostypes "github.com/cosmos/cosmos-sdk/types" 36 signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" 37 authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" 38 vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" 39 authztypes "github.com/cosmos/cosmos-sdk/x/authz" 40 banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" 41 crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types" 42 distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" 43 govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1" 44 govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" 45 paramproposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal" 46 slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" 47 stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 48 icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types" 49 ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types" 50 ibcapplicationtypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" 51 ibccoretypes "github.com/cosmos/ibc-go/v8/modules/core/types" 52 ibclightclienttypes "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine" 53 ibctenderminttypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" 54 ) 55 56 // NewInterfaceRegistry returns a new InterfaceRegistry 57 func NewInterfaceRegistry() types.InterfaceRegistry { 58 registry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{ 59 ProtoFiles: proto.HybridResolver, 60 SigningOptions: signing.Options{ 61 AddressCodec: address.Bech32Codec{ 62 Bech32Prefix: cosmostypes.GetConfig().GetBech32AccountAddrPrefix(), 63 }, 64 ValidatorAddressCodec: address.Bech32Codec{ 65 Bech32Prefix: cosmostypes.GetConfig().GetBech32ValidatorAddrPrefix(), 66 }, 67 }, 68 }) 69 if err != nil { 70 panic(err) 71 } 72 return registry 73 } 74 75 // NewTxConfig initializes new Cosmos TxConfig with certain signModes enabled. 76 func NewTxConfig(signModes []signingtypes.SignMode) client.TxConfig { 77 interfaceRegistry := NewInterfaceRegistry() 78 keyscodec.RegisterInterfaces(interfaceRegistry) 79 std.RegisterInterfaces(interfaceRegistry) 80 exchange.RegisterInterfaces(interfaceRegistry) 81 oracle.RegisterInterfaces(interfaceRegistry) 82 insurance.RegisterInterfaces(interfaceRegistry) 83 auction.RegisterInterfaces(interfaceRegistry) 84 peggy.RegisterInterfaces(interfaceRegistry) 85 ocr.RegisterInterfaces(interfaceRegistry) 86 wasmx.RegisterInterfaces(interfaceRegistry) 87 chaintypes.RegisterInterfaces(interfaceRegistry) 88 tokenfactory.RegisterInterfaces(interfaceRegistry) 89 90 // more cosmos types 91 authtypes.RegisterInterfaces(interfaceRegistry) 92 authztypes.RegisterInterfaces(interfaceRegistry) 93 vestingtypes.RegisterInterfaces(interfaceRegistry) 94 banktypes.RegisterInterfaces(interfaceRegistry) 95 crisistypes.RegisterInterfaces(interfaceRegistry) 96 distributiontypes.RegisterInterfaces(interfaceRegistry) 97 evidencetypes.RegisterInterfaces(interfaceRegistry) 98 govtypes.RegisterInterfaces(interfaceRegistry) 99 govv1types.RegisterInterfaces(interfaceRegistry) 100 paramproposaltypes.RegisterInterfaces(interfaceRegistry) 101 ibcapplicationtypes.RegisterInterfaces(interfaceRegistry) 102 ibccoretypes.RegisterInterfaces(interfaceRegistry) 103 ibclightclienttypes.RegisterInterfaces(interfaceRegistry) 104 ibctenderminttypes.RegisterInterfaces(interfaceRegistry) 105 slashingtypes.RegisterInterfaces(interfaceRegistry) 106 stakingtypes.RegisterInterfaces(interfaceRegistry) 107 upgradetypes.RegisterInterfaces(interfaceRegistry) 108 feegranttypes.RegisterInterfaces(interfaceRegistry) 109 wasmtypes.RegisterInterfaces(interfaceRegistry) 110 icatypes.RegisterInterfaces(interfaceRegistry) 111 112 marshaler := codec.NewProtoCodec(interfaceRegistry) 113 return tx.NewTxConfig(marshaler, signModes) 114 } 115 116 // NewClientContext creates a new Cosmos Client context, where chainID 117 // corresponds to Cosmos chain ID, fromSpec is either name of the key, or bech32-address 118 // of the Cosmos account. Keyring is required to contain the specified key. 119 func NewClientContext( 120 chainId, fromSpec string, kb keyring.Keyring, 121 ) (client.Context, error) { 122 clientCtx := client.Context{} 123 124 interfaceRegistry := NewInterfaceRegistry() 125 keyscodec.RegisterInterfaces(interfaceRegistry) 126 std.RegisterInterfaces(interfaceRegistry) 127 exchange.RegisterInterfaces(interfaceRegistry) 128 insurance.RegisterInterfaces(interfaceRegistry) 129 auction.RegisterInterfaces(interfaceRegistry) 130 oracle.RegisterInterfaces(interfaceRegistry) 131 peggy.RegisterInterfaces(interfaceRegistry) 132 ocr.RegisterInterfaces(interfaceRegistry) 133 wasmx.RegisterInterfaces(interfaceRegistry) 134 chaintypes.RegisterInterfaces(interfaceRegistry) 135 tokenfactory.RegisterInterfaces(interfaceRegistry) 136 137 // more cosmos types 138 authtypes.RegisterInterfaces(interfaceRegistry) 139 authztypes.RegisterInterfaces(interfaceRegistry) 140 vestingtypes.RegisterInterfaces(interfaceRegistry) 141 banktypes.RegisterInterfaces(interfaceRegistry) 142 crisistypes.RegisterInterfaces(interfaceRegistry) 143 distributiontypes.RegisterInterfaces(interfaceRegistry) 144 evidencetypes.RegisterInterfaces(interfaceRegistry) 145 govtypes.RegisterInterfaces(interfaceRegistry) 146 govv1types.RegisterInterfaces(interfaceRegistry) 147 paramproposaltypes.RegisterInterfaces(interfaceRegistry) 148 ibcapplicationtypes.RegisterInterfaces(interfaceRegistry) 149 ibccoretypes.RegisterInterfaces(interfaceRegistry) 150 ibclightclienttypes.RegisterInterfaces(interfaceRegistry) 151 ibctenderminttypes.RegisterInterfaces(interfaceRegistry) 152 slashingtypes.RegisterInterfaces(interfaceRegistry) 153 stakingtypes.RegisterInterfaces(interfaceRegistry) 154 upgradetypes.RegisterInterfaces(interfaceRegistry) 155 feegranttypes.RegisterInterfaces(interfaceRegistry) 156 wasmtypes.RegisterInterfaces(interfaceRegistry) 157 icatypes.RegisterInterfaces(interfaceRegistry) 158 ibcfeetypes.RegisterInterfaces(interfaceRegistry) 159 160 marshaler := codec.NewProtoCodec(interfaceRegistry) 161 encodingConfig := EncodingConfig{ 162 InterfaceRegistry: interfaceRegistry, 163 Marshaler: marshaler, 164 TxConfig: NewTxConfig([]signingtypes.SignMode{ 165 signingtypes.SignMode_SIGN_MODE_DIRECT, 166 }), 167 } 168 169 var keyInfo keyring.Record 170 171 if kb != nil { 172 addr, err := cosmostypes.AccAddressFromBech32(fromSpec) 173 if err == nil { 174 record, err := kb.KeyByAddress(addr) 175 if err != nil { 176 err = errors.Wrapf(err, "failed to load key info by address %s", addr.String()) 177 return clientCtx, err 178 } 179 keyInfo = *record 180 } else { 181 // failed to parse Bech32, is it a name? 182 record, err := kb.Key(fromSpec) 183 if err != nil { 184 err = errors.Wrapf(err, "no key in keyring for name: %s", fromSpec) 185 return clientCtx, err 186 } 187 keyInfo = *record 188 } 189 } 190 191 clientCtx = newContext( 192 chainId, 193 encodingConfig, 194 kb, 195 keyInfo, 196 ) 197 198 return clientCtx, nil 199 } 200 201 type EncodingConfig struct { 202 InterfaceRegistry types.InterfaceRegistry 203 Marshaler codec.Codec 204 TxConfig client.TxConfig 205 } 206 207 func newContext( 208 chainId string, 209 encodingConfig EncodingConfig, 210 kb keyring.Keyring, 211 keyInfo keyring.Record, 212 213 ) client.Context { 214 clientCtx := client.Context{ 215 ChainID: chainId, 216 Codec: encodingConfig.Marshaler, 217 InterfaceRegistry: encodingConfig.InterfaceRegistry, 218 Output: os.Stderr, 219 OutputFormat: "json", 220 BroadcastMode: "block", 221 UseLedger: false, 222 Simulate: true, 223 GenerateOnly: false, 224 Offline: false, 225 SkipConfirm: true, 226 TxConfig: encodingConfig.TxConfig, 227 AccountRetriever: authtypes.AccountRetriever{}, 228 } 229 230 if keyInfo.PubKey != nil { 231 keyInfoAddress, err := keyInfo.GetAddress() 232 if err != nil { 233 panic(err) 234 } 235 clientCtx = clientCtx.WithKeyring(kb) 236 clientCtx = clientCtx.WithFromAddress(keyInfoAddress) 237 clientCtx = clientCtx.WithFromName(keyInfo.Name) 238 clientCtx = clientCtx.WithFrom(keyInfo.Name) 239 } 240 241 return clientCtx 242 }