-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
stdtx_builder.go
211 lines (178 loc) · 5.24 KB
/
stdtx_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package legacytx
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)
// StdTxBuilder wraps StdTx to implement to the context.TxBuilder interface.
// Note that this type just exists for backwards compatibility with amino StdTx
// and will not work for protobuf transactions.
type StdTxBuilder struct {
StdTx
cdc *codec.LegacyAmino
}
// ensure interface implementation
var _ client.TxBuilder = &StdTxBuilder{}
// GetTx implements TxBuilder.GetTx
func (s *StdTxBuilder) GetTx() authsigning.Tx {
return s.StdTx
}
// SetMsgs implements TxBuilder.SetMsgs
func (s *StdTxBuilder) SetMsgs(msgs ...sdk.Msg) error {
stdTxMsgs := make([]sdk.Msg, len(msgs))
for i, msg := range msgs {
switch msg := msg.(type) {
case sdk.ServiceMsg:
// Since ServiceMsg isn't registered with amino, we unpack msg.Request
// into a Msg so that it's handled gracefully for the legacy
// GET /txs/{hash} and /txs endpoints.
sdkMsg, ok := msg.Request.(sdk.Msg)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting %T at %d to implement sdk.Msg", msg.Request, i)
}
stdTxMsgs[i] = sdkMsg
default:
// legacy sdk.Msg
stdTxMsgs[i] = msg
}
}
s.Msgs = stdTxMsgs
return nil
}
// SetSignatures implements TxBuilder.SetSignatures.
func (s *StdTxBuilder) SetSignatures(signatures ...signing.SignatureV2) error {
sigs := make([]StdSignature, len(signatures))
var err error
for i, sig := range signatures {
sigs[i], err = SignatureV2ToStdSignature(s.cdc, sig)
if err != nil {
return err
}
}
s.Signatures = sigs
return nil
}
func (s *StdTxBuilder) SetFeeAmount(amount sdk.Coins) {
s.StdTx.Fee.Amount = amount
}
func (s *StdTxBuilder) SetGasLimit(limit uint64) {
s.StdTx.Fee.Gas = limit
}
// SetMemo implements TxBuilder.SetMemo
func (s *StdTxBuilder) SetMemo(memo string) {
s.Memo = memo
}
// SetTimeoutHeight sets the transaction's height timeout.
func (s *StdTxBuilder) SetTimeoutHeight(height uint64) {
s.TimeoutHeight = height
}
// StdTxConfig is a context.TxConfig for StdTx
type StdTxConfig struct {
Cdc *codec.LegacyAmino
}
var _ client.TxConfig = StdTxConfig{}
// NewTxBuilder implements TxConfig.NewTxBuilder
func (s StdTxConfig) NewTxBuilder() client.TxBuilder {
return &StdTxBuilder{
StdTx: StdTx{},
cdc: s.Cdc,
}
}
// WrapTxBuilder returns a StdTxBuilder from provided transaction
func (s StdTxConfig) WrapTxBuilder(newTx sdk.Tx) (client.TxBuilder, error) {
stdTx, ok := newTx.(StdTx)
if !ok {
return nil, fmt.Errorf("wrong type, expected %T, got %T", stdTx, newTx)
}
return &StdTxBuilder{StdTx: stdTx, cdc: s.Cdc}, nil
}
// MarshalTx implements TxConfig.MarshalTx
func (s StdTxConfig) TxEncoder() sdk.TxEncoder {
return DefaultTxEncoder(s.Cdc)
}
func (s StdTxConfig) TxDecoder() sdk.TxDecoder {
return mkDecoder(s.Cdc.UnmarshalBinaryBare)
}
func (s StdTxConfig) TxJSONEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
return s.Cdc.MarshalJSON(tx)
}
}
func (s StdTxConfig) TxJSONDecoder() sdk.TxDecoder {
return mkDecoder(s.Cdc.UnmarshalJSON)
}
func (s StdTxConfig) MarshalSignatureJSON(sigs []signing.SignatureV2) ([]byte, error) {
stdSigs := make([]StdSignature, len(sigs))
for i, sig := range sigs {
stdSig, err := SignatureV2ToStdSignature(s.Cdc, sig)
if err != nil {
return nil, err
}
stdSigs[i] = stdSig
}
return s.Cdc.MarshalJSON(stdSigs)
}
func (s StdTxConfig) UnmarshalSignatureJSON(bz []byte) ([]signing.SignatureV2, error) {
var stdSigs []StdSignature
err := s.Cdc.UnmarshalJSON(bz, &stdSigs)
if err != nil {
return nil, err
}
sigs := make([]signing.SignatureV2, len(stdSigs))
for i, stdSig := range stdSigs {
sig, err := StdSignatureToSignatureV2(s.Cdc, stdSig)
if err != nil {
return nil, err
}
sigs[i] = sig
}
return sigs, nil
}
func (s StdTxConfig) SignModeHandler() authsigning.SignModeHandler {
return stdTxSignModeHandler{}
}
// SignatureV2ToStdSignature converts a SignatureV2 to a StdSignature
// [Deprecated]
func SignatureV2ToStdSignature(cdc *codec.LegacyAmino, sig signing.SignatureV2) (StdSignature, error) {
var (
sigBz []byte
err error
)
if sig.Data != nil {
sigBz, err = SignatureDataToAminoSignature(cdc, sig.Data)
if err != nil {
return StdSignature{}, err
}
}
return StdSignature{
PubKey: sig.PubKey,
Signature: sigBz,
}, nil
}
// Unmarshaler is a generic type for Unmarshal functions
type Unmarshaler func(bytes []byte, ptr interface{}) error
func mkDecoder(unmarshaler Unmarshaler) sdk.TxDecoder {
return func(txBytes []byte) (sdk.Tx, error) {
if len(txBytes) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty")
}
var tx = StdTx{}
// StdTx.Msg is an interface. The concrete types
// are registered by MakeTxCodec
err := unmarshaler(txBytes, &tx)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, err.Error())
}
return tx, nil
}
}
// DefaultTxEncoder logic for standard transaction encoding
func DefaultTxEncoder(cdc *codec.LegacyAmino) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
return cdc.MarshalBinaryBare(tx)
}
}