github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/ibc-tx/encoder.go (about) 1 package ibc_tx 2 3 import ( 4 "fmt" 5 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 7 codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types" 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 10 ibctx "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/ibc-adapter" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 12 "github.com/gogo/protobuf/proto" 13 ) 14 15 const MaxGasWanted = uint64((1 << 63) - 1) 16 17 func DefaultTxEncoder() ibctx.TxEncoder { 18 return func(tx ibctx.Tx) ([]byte, error) { 19 txWrapper, ok := tx.(*wrapper) 20 if !ok { 21 return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx) 22 } 23 24 raw := &types.TxRaw{ 25 BodyBytes: txWrapper.getBodyBytes(), 26 AuthInfoBytes: txWrapper.getAuthInfoBytes(), 27 Signatures: txWrapper.tx.Signatures, 28 } 29 30 return proto.Marshal(raw) 31 } 32 } 33 34 // DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler. 35 func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) ibctx.IBCTxEncoder { 36 return func(tx ibctx.Tx) ([]byte, error) { 37 txWrapper, ok := tx.(*wrapper) 38 if ok { 39 return cdc.MarshalJSON(txWrapper.tx) 40 } 41 42 protoTx, ok := tx.(*TxAdapter) 43 if ok { 44 return cdc.MarshalJSON(protoTx) 45 } 46 47 return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx) 48 49 } 50 } 51 52 type TxAdapter struct { 53 *types.Tx 54 } 55 56 func (t *TxAdapter) GetMsgs() []ibctx.Msg { 57 if t == nil || t.Body == nil { 58 return nil 59 } 60 61 anys := t.Body.Messages 62 res := make([]ibctx.Msg, len(anys)) 63 for i, any := range anys { 64 cached := any.GetCachedValue() 65 if cached == nil { 66 panic("Any cached value is nil. Transaction messages must be correctly packed Any values.") 67 } 68 res[i] = cached.(ibctx.Msg) 69 } 70 return res 71 } 72 73 // ValidateBasic implements the ValidateBasic method on sdk.Tx. 74 func (t *TxAdapter) ValidateBasic() error { 75 if t == nil { 76 return fmt.Errorf("bad Tx") 77 } 78 79 body := t.Body 80 if body == nil { 81 return fmt.Errorf("missing TxBody") 82 } 83 84 authInfo := t.AuthInfo 85 if authInfo == nil { 86 return fmt.Errorf("missing AuthInfo") 87 } 88 89 fee := authInfo.Fee 90 if fee == nil { 91 return fmt.Errorf("missing fee") 92 } 93 94 if fee.GasLimit > MaxGasWanted { 95 return sdkerrors.Wrapf( 96 sdkerrors.ErrInvalidRequest, 97 "invalid gas supplied; %d > %d", fee.GasLimit, MaxGasWanted, 98 ) 99 } 100 101 if fee.Amount.IsAnyNil() { 102 return sdkerrors.Wrapf( 103 sdkerrors.ErrInsufficientFee, 104 "invalid fee provided: null", 105 ) 106 } 107 108 if fee.Amount.IsAnyNegative() { 109 return sdkerrors.Wrapf( 110 sdkerrors.ErrInsufficientFee, 111 "invalid fee provided: %s", fee.Amount, 112 ) 113 } 114 115 if fee.Payer != "" { 116 _, err := sdk.AccAddressFromBech32(fee.Payer) 117 if err != nil { 118 return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid fee payer address (%s)", err) 119 } 120 } 121 122 sigs := t.Signatures 123 124 if len(sigs) == 0 { 125 return sdkerrors.ErrNoSignatures 126 } 127 128 if len(sigs) != len(t.GetSigners()) { 129 return sdkerrors.Wrapf( 130 sdkerrors.ErrUnauthorized, 131 "wrong number of signers; expected %d, got %d", len(t.GetSigners()), len(sigs), 132 ) 133 } 134 135 return nil 136 } 137 138 // GetSigners retrieves all the signers of a tx. 139 // This includes all unique signers of the messages (in order), 140 // as well as the FeePayer (if specified and not already included). 141 func (t *TxAdapter) GetSigners() []sdk.AccAddress { 142 var signers []sdk.AccAddress 143 seen := map[string]bool{} 144 145 for _, msg := range t.GetMsgs() { 146 for _, addr := range msg.GetSigners() { 147 if !seen[addr.String()] { 148 signers = append(signers, addr) 149 seen[addr.String()] = true 150 } 151 } 152 } 153 154 // ensure any specified fee payer is included in the required signers (at the end) 155 feePayer := t.AuthInfo.Fee.Payer 156 if feePayer != "" && !seen[feePayer] { 157 payerAddr, err := sdk.AccAddressFromBech32(feePayer) 158 if err != nil { 159 panic(err) 160 } 161 signers = append(signers, payerAddr) 162 seen[feePayer] = true 163 } 164 165 return signers 166 } 167 168 // 169 //func (t *TxRawAdapter) GetGas() uint64 { 170 // return t.AuthInfo.Fee.GasLimit 171 //} 172 //func (t *TxRawAdapter) GetFee() sdk.CoinAdapters { 173 // return t.AuthInfo.Fee.Amount 174 //} 175 //func (t *TxRawAdapter) FeePayer() sdk.AccAddress { 176 // feePayer := t.AuthInfo.Fee.Payer 177 // if feePayer != "" { 178 // payerAddr, err := sdk.AccAddressFromBech32(feePayer) 179 // if err != nil { 180 // panic(err) 181 // } 182 // return payerAddr 183 // } 184 // // use first signer as default if no payer specified 185 // return t.GetSigners()[0] 186 //} 187 // 188 //func (t *TxRawAdapter) FeeGranter() sdk.AccAddress { 189 // feePayer := t.AuthInfo.Fee.Granter 190 // if feePayer != "" { 191 // granterAddr, err := sdk.AccAddressFromBech32(feePayer) 192 // if err != nil { 193 // panic(err) 194 // } 195 // return granterAddr 196 // } 197 // return nil 198 //} 199 // 200 //// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method 201 //func (t *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 202 // if t.Body != nil { 203 // if err := t.Body.UnpackInterfaces(unpacker); err != nil { 204 // return err 205 // } 206 // } 207 // 208 // if t.AuthInfo != nil { 209 // return t.AuthInfo.UnpackInterfaces(unpacker) 210 // } 211 // 212 // return nil 213 //} 214 // 215 //// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method 216 //func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 217 // for _, any := range m.Messages { 218 // var msg sdk.Msg 219 // err := unpacker.UnpackAny(any, &msg) 220 // if err != nil { 221 // return err 222 // } 223 // } 224 // 225 // return nil 226 //} 227 // 228 //// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method 229 //func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 230 // for _, signerInfo := range m.SignerInfos { 231 // err := signerInfo.UnpackInterfaces(unpacker) 232 // if err != nil { 233 // return err 234 // } 235 // } 236 // return nil 237 //} 238 // 239 //// UnpackInterfaces implements the UnpackInterfaceMessages.UnpackInterfaces method 240 //func (m *TxRawAdapter) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 241 // return unpacker.UnpackAny(m.PublicKey, new(cryptotypes.PubKey)) 242 //} 243 244 // TODO add call RegisterInterfaces 245 // RegisterInterfaces registers the sdk.Tx interface. 246 func RegisterInterfaces(registry codectypes.InterfaceRegistry) { 247 registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil)) 248 registry.RegisterImplementations((*sdk.Tx)(nil), &types.Tx{}) 249 }