-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
292 lines (239 loc) · 7.87 KB
/
factory.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package tx
import (
"errors"
"fmt"
sdk "github.com/gridironzone/service-sdk-go/types"
"github.com/gridironzone/service-sdk-go/types/tx/signing"
)
// Factory defines a client transaction factory that facilitates generating and
// signing an application-specific transaction.
type (
// Factory implements a transaction context created in SDK modules.
Factory struct {
address string
chainID string
memo string
password string
accountNumber uint64
sequence uint64
gas uint64
gasAdjustment float64
simulateAndExecute bool
fees sdk.Coins
gasPrices sdk.DecCoins
mode sdk.BroadcastMode
signMode signing.SignMode
signModeHandler sdk.SignModeHandler
keyManager sdk.KeyManager
txConfig sdk.TxConfig
queryFunc QueryWithData
}
// QueryWithData implements a query method from cschain.
QueryWithData func(string, []byte) ([]byte, int64, error)
)
// NewFactory return a point of the instance of Factory.
func NewFactory() *Factory {
return &Factory{}
}
// ChainID returns the chainID of the current chain.
func (f *Factory) ChainID() string { return f.chainID }
// Gas returns the gas of the transaction.
func (f *Factory) Gas() uint64 { return f.gas }
// GasAdjustment returns the gasAdjustment.
func (f Factory) GasAdjustment() float64 { return f.gasAdjustment }
// Fees returns the fee of the transaction.
func (f *Factory) Fees() sdk.Coins { return f.fees }
// Sequence returns the sequence of the account.
func (f *Factory) Sequence() uint64 { return f.sequence }
// Memo returns memo.
func (f *Factory) Memo() string { return f.memo }
// AccountNumber returns accountNumber.
func (f *Factory) AccountNumber() uint64 { return f.accountNumber }
// KeyManager returns keyManager.
func (f *Factory) KeyManager() sdk.KeyManager { return f.keyManager }
// Mode returns mode.
func (f *Factory) Mode() sdk.BroadcastMode { return f.mode }
// SimulateAndExecute returns the option to simulateAndExecute and then execute the transaction
// using the gas from the simulation results
func (f *Factory) SimulateAndExecute() bool { return f.simulateAndExecute }
// Password returns password.
func (f *Factory) Password() string { return f.password }
// Address returns the address.
func (f *Factory) Address() string { return f.address }
// WithChainID returns a pointer of the context with an updated ChainID.
func (f *Factory) WithChainID(chainID string) *Factory {
f.chainID = chainID
return f
}
// WithGas returns a pointer of the context with an updated Gas.
func (f *Factory) WithGas(gas uint64) *Factory {
f.gas = gas
return f
}
// WithGasAdjustment returns a pointer of the context with an updated gasAdjustment.
func (f *Factory) WithGasAdjustment(gasAdjustment float64) *Factory {
f.gasAdjustment = gasAdjustment
return f
}
// WithFee returns a pointer of the context with an updated Fee.
func (f *Factory) WithFee(fee sdk.Coins) *Factory {
f.fees = fee
return f
}
// WithSequence returns a pointer of the context with an updated sequence number.
func (f *Factory) WithSequence(sequence uint64) *Factory {
f.sequence = sequence
return f
}
// WithMemo returns a pointer of the context with an updated memo.
func (f *Factory) WithMemo(memo string) *Factory {
f.memo = memo
return f
}
// WithAccountNumber returns a pointer of the context with an account number.
func (f *Factory) WithAccountNumber(accnum uint64) *Factory {
f.accountNumber = accnum
return f
}
// WithKeyManager returns a pointer of the context with a KeyManager.
func (f *Factory) WithKeyManager(keyManager sdk.KeyManager) *Factory {
f.keyManager = keyManager
return f
}
// WithMode returns a pointer of the context with a Mode.
func (f *Factory) WithMode(mode sdk.BroadcastMode) *Factory {
f.mode = mode
return f
}
// WithSimulateAndExecute returns a pointer of the context with a simulateAndExecute.
func (f *Factory) WithSimulateAndExecute(simulate bool) *Factory {
f.simulateAndExecute = simulate
return f
}
// WithPassword returns a pointer of the context with a password.
func (f *Factory) WithPassword(password string) *Factory {
f.password = password
return f
}
// WithAddress returns a pointer of the context with a password.
func (f *Factory) WithAddress(address string) *Factory {
f.address = address
return f
}
// WithTxConfig returns a pointer of the context with an TxConfig
func (f *Factory) WithTxConfig(txConfig sdk.TxConfig) *Factory {
f.txConfig = txConfig
return f
}
// WithSignModeHandler returns a pointer of the context with an signModeHandler.
func (f *Factory) WithSignModeHandler(signModeHandler sdk.SignModeHandler) *Factory {
f.signModeHandler = signModeHandler
return f
}
// WithQueryFunc returns a pointer of the context with an queryFunc.
func (f *Factory) WithQueryFunc(queryFunc QueryWithData) *Factory {
f.queryFunc = queryFunc
return f
}
func (f *Factory) BuildAndSign(name string, msgs []sdk.Msg) ([]byte, error) {
tx, err := f.BuildUnsignedTx(msgs)
if err != nil {
return nil, err
}
if err = f.Sign(name, tx); err != nil {
return nil, err
}
txBytes, err := f.txConfig.TxEncoder()(tx.GetTx())
if err != nil {
return nil, err
}
return txBytes, nil
}
func (f *Factory) BuildUnsignedTx(msgs []sdk.Msg) (sdk.TxBuilder, error) {
if f.chainID == "" {
return nil, fmt.Errorf("chain ID required but not specified")
}
fees := f.fees
if !f.gasPrices.IsZero() {
if !fees.IsZero() {
return nil, errors.New("cannot provide both fees and gas prices")
}
glDec := sdk.NewDec(int64(f.gas))
// Derive the fees based on the provided gas prices, where
// fee = ceil(gasPrice * gasLimit).
fees = make(sdk.Coins, len(f.gasPrices))
for i, gp := range f.gasPrices {
fee := gp.Amount.Mul(glDec)
fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
}
}
tx := f.txConfig.NewTxBuilder()
if err := tx.SetMsgs(msgs...); err != nil {
return nil, err
}
tx.SetMemo(f.memo)
tx.SetFeeAmount(fees)
tx.SetGasLimit(f.gas)
//f.txBuilder.SetTimeoutHeight(f.TimeoutHeight())
return tx, nil
}
// Sign signs a transaction given a name, passphrase, and a single message to
// signed. An error is returned if signing fails.
func (f *Factory) Sign(name string, txBuilder sdk.TxBuilder) error {
signMode := f.signMode
if signMode == signing.SignMode_SIGN_MODE_UNSPECIFIED {
// use the SignModeHandler's default mode if unspecified
signMode = f.txConfig.SignModeHandler().DefaultMode()
}
signerData := sdk.SignerData{
ChainID: f.chainID,
AccountNumber: f.accountNumber,
Sequence: f.sequence,
}
pubkey, _, err := f.keyManager.Find(name, f.password)
if err != nil {
return err
}
// For SIGN_MODE_DIRECT, calling SetSignatures calls setSignerInfos on
// Factory under the hood, and SignerInfos is needed to generated the
// sign bytes. This is the reason for setting SetSignatures here, with a
// nil signature.
//
// Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it
// also doesn't affect its generated sign bytes, so for code's simplicity
// sake, we put it here.
sigData := signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
}
sig := signing.SignatureV2{
PubKey: pubkey,
Data: &sigData,
Sequence: f.Sequence(),
}
if err := txBuilder.SetSignatures(sig); err != nil {
return err
}
// Generate the bytes to be signed.
signBytes, err := f.signModeHandler.GetSignBytes(signMode, signerData, txBuilder.GetTx())
if err != nil {
return err
}
// Sign those bytes
sigBytes, _, err := f.keyManager.Sign(name, f.password, signBytes)
if err != nil {
return err
}
// Construct the SignatureV2 struct
sigData = signing.SingleSignatureData{
SignMode: signMode,
Signature: sigBytes,
}
sig = signing.SignatureV2{
PubKey: pubkey,
Data: &sigData,
Sequence: f.Sequence(),
}
// And here the tx is populated with the signature
return txBuilder.SetSignatures(sig)
}