@@ -5,20 +5,33 @@ import (
55 "fmt"
66 "os"
77
8+ "github.com/cosmos/cosmos-sdk/client"
9+ "github.com/cosmos/cosmos-sdk/client/flags"
10+ "github.com/cosmos/cosmos-sdk/client/tx"
811 "github.com/cosmos/cosmos-sdk/codec"
912 codectypes "github.com/cosmos/cosmos-sdk/codec/types"
1013 cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1114 "github.com/cosmos/cosmos-sdk/crypto/keyring"
1215 sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
1316 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1417 sdk "github.com/cosmos/cosmos-sdk/types"
15- "github.com/cosmos/cosmos-sdk/x/feegrant "
18+ "github.com/cosmos/cosmos-sdk/version "
1619 "github.com/gitopia/git-remote-gitopia/config"
20+ glib "github.com/gitopia/gitopia-go"
21+ "github.com/gitopia/gitopia-go/logger"
1722 gitopia "github.com/gitopia/gitopia/v2/app"
1823 offchaintypes "github.com/gitopia/gitopia/v2/x/offchain/types"
1924 goGitConfig "github.com/go-git/go-git/v5/config"
20- "github.com/ignite/cli/ignite/pkg/cosmosaccount"
21- "github.com/ignite/cli/ignite/pkg/cosmosclient"
25+ "github.com/sirupsen/logrus"
26+ "github.com/spf13/pflag"
27+
28+ "github.com/cosmos/cosmos-sdk/std"
29+ authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
30+ authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
31+ gtypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
32+ otypes "github.com/gitopia/gitopia/v2/x/offchain/types"
33+ rtypes "github.com/gitopia/gitopia/v2/x/rewards/types"
34+
2235 "github.com/pkg/errors"
2336 "google.golang.org/grpc"
2437)
@@ -39,23 +52,17 @@ var (
3952type keyringBackend struct {
4053 key string
4154 backend string
42- CC cosmosclient .Client
55+ CC glib .Client
4356}
4457
45- func newKeyringBackend (k string , b string , c cosmosclient.Client ) keyringBackend {
46- c .TxFactory = c .TxFactory .WithGasPrices (config .GasPrices )
58+ func newKeyringBackend (k string , b string , c glib.Client ) keyringBackend {
4759 return keyringBackend {
4860 key : k ,
4961 backend : b ,
5062 CC : c ,
5163 }
5264}
5365
54- func (k keyringBackend ) address () (string , error ) {
55- address , err := k .CC .Address (k .key )
56- return address , err
57- }
58-
5966type OSKeyring struct {
6067 kb keyringBackend
6168 address string
@@ -65,7 +72,6 @@ type OSKeyring struct {
6572func InitOSKeyringWallet () (Wallet , error ) {
6673 var key string
6774 var backend string
68- var cc cosmosclient.Client
6975
7076 conf , err := goGitConfig .LoadConfig (goGitConfig .GlobalScope )
7177 if err != nil {
@@ -85,28 +91,78 @@ func InitOSKeyringWallet() (Wallet, error) {
8591 backend = keyring .BackendOS // default to OS. same as cosmos keys subcommand
8692 }
8793
88- cc , err = cosmosclient .New (context .Background (),
89- cosmosclient .WithNodeAddress (config .TmAddr ),
90- // same service name used in both helper and keys management app
91- cosmosclient .WithKeyringServiceName (AppName ), // not suported on macos
92- cosmosclient .WithKeyringBackend (cosmosaccount .KeyringBackend (backend )), // not all backends supported by cosmos are supported by cosmos client
93- cosmosclient .WithAddressPrefix (AccountAddressPrefix ),
94- )
94+ l := logrus .New ()
95+ l .SetOutput (os .Stderr )
96+ ctx := logger .ContextWithValue (context .Background (), l )
97+ glib .WithGitopiaAddr (config .GRPCHost )
98+ glib .WithGasPrices (config .GasPrices )
99+ cc , err := NewContext (key )
100+ if err != nil {
101+ return nil , errors .Wrap (err , "error creating cosmos client context" )
102+ }
103+ txf := tx .NewFactoryCLI (cc , & pflag.FlagSet {}).WithGasAdjustment (GAS_ADJUSTMENT )
104+ gc , err := glib .NewClient (ctx , cc , txf )
95105 if err != nil {
96106 return nil , errors .Wrap (err , "error creating cosmos client" )
97107 }
98108
99109 o := OSKeyring {
100- kb : newKeyringBackend (key , backend , cc ),
110+ kb : newKeyringBackend (key , backend , gc ),
101111 secType : KEYRING_BACKEND ,
102112 }
103113
104- o .address , err = o .kb .address ()
114+ o .address = o .kb .CC .Address ().String ()
115+
116+ return o , nil
117+ }
118+
119+ func NewContext (from string ) (client.Context , error ) {
120+ version .Name = AppName
121+ clientCtx := client.Context {}
122+
123+ interfaceRegistry := codectypes .NewInterfaceRegistry ()
124+ std .RegisterInterfaces (interfaceRegistry )
125+ cryptocodec .RegisterInterfaces (interfaceRegistry )
126+ authtypes .RegisterInterfaces (interfaceRegistry )
127+ gtypes .RegisterInterfaces (interfaceRegistry )
128+ rtypes .RegisterInterfaces (interfaceRegistry )
129+ otypes .RegisterInterfaces (interfaceRegistry )
130+
131+ marshaler := codec .NewProtoCodec (interfaceRegistry )
132+ txCfg := authtx .NewTxConfig (marshaler , authtx .DefaultSignModes )
133+ clientCtx = clientCtx .
134+ WithCodec (marshaler ).
135+ WithInterfaceRegistry (interfaceRegistry ).
136+ WithAccountRetriever (authtypes.AccountRetriever {}).
137+ WithTxConfig (txCfg ).
138+ WithInput (os .Stdin )
139+
140+ clientCtx = clientCtx .WithChainID (config .ChainId )
141+ clientCtx = clientCtx .WithNodeURI (config .TmAddr )
142+ c , err := client .NewClientFromNode (clientCtx .NodeURI )
105143 if err != nil {
106- return nil , err
144+ return clientCtx , errors . Wrap ( err , "error creatig tm client" )
107145 }
146+ clientCtx = clientCtx .WithClient (c )
147+ clientCtx = clientCtx .WithBroadcastMode (flags .BroadcastSync )
148+ clientCtx = clientCtx .WithSkipConfirmation (true )
108149
109- return o , nil
150+ kr , err := client .NewKeyringFromBackend (clientCtx , keyring .BackendOS )
151+ if err != nil {
152+ return clientCtx , errors .Wrap (err , "error creating keyring backend" )
153+ }
154+ clientCtx = clientCtx .WithKeyring (kr )
155+
156+ fromAddr , fromName , _ , err := client .GetFromFields (clientCtx , kr , from )
157+ if err != nil {
158+ return clientCtx , errors .Wrap (err , "error parsing from Addr" )
159+ }
160+
161+ clientCtx = clientCtx .WithFrom (from ).WithFromAddress (fromAddr ).WithFromName (fromName )
162+
163+ feeGranterAddr := sdk .MustAccAddressFromBech32 (config .FeeGranterAddr )
164+ clientCtx = clientCtx .WithFeeGranterAddress (feeGranterAddr )
165+ return clientCtx , nil
110166}
111167
112168func (o OSKeyring ) SignData (data []byte ) (string , error ) {
@@ -160,41 +216,7 @@ func (o OSKeyring) SignData(data []byte) (string, error) {
160216}
161217
162218func (o OSKeyring ) SignAndBroadcast (grpcConn * grpc.ClientConn , msgs []sdk.Msg ) error {
163- account , err := o .kb .CC .Account (o .kb .key )
164- if err != nil {
165- return err
166- }
167-
168- // check fee grant exists
169- fqc := feegrant .NewQueryClient (grpcConn )
170- fr , _ := fqc .Allowance (context .Background (), & feegrant.QueryAllowanceRequest {
171- Granter : config .FeeGranterAddr ,
172- Grantee : o .Address (),
173- })
174-
175- if fr != nil {
176- feeGranterAddr , err := sdk .AccAddressFromBech32 (config .FeeGranterAddr )
177- if err != nil {
178- return err
179- }
180- feePayerAddr , err := sdk .AccAddressFromBech32 (o .Address ())
181- if err != nil {
182- return err
183- }
184-
185- o .kb .CC .TxFactory .WithFeeGranter (feeGranterAddr )
186- o .kb .CC .TxFactory .WithFeePayer (feePayerAddr )
187- }
188-
189- txResp , err := o .kb .CC .BroadcastTx (account , msgs ... )
190- if err != nil {
191- return err
192- }
193- if txResp .TxResponse .Code != 0 {
194- return errors .Wrap (err , "error broadcasting transaction" )
195- }
196-
197- return nil
219+ return o .kb .CC .BroadcastTxAndWait (context .Background (), msgs ... )
198220}
199221
200222func (o OSKeyring ) Address () string {
0 commit comments