forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
319 lines (265 loc) · 9.17 KB
/
transaction.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package build
import (
"encoding/hex"
"fmt"
"github.com/stellar/go/network"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// Transaction groups the creation of a new TransactionBuilder with a call
// to Mutate.
func Transaction(muts ...TransactionMutator) (*TransactionBuilder, error) {
result := &TransactionBuilder{}
err := result.Mutate(muts...)
if err != nil {
return nil, err
}
err = result.Mutate(Defaults{})
if err != nil {
return nil, err
}
return result, nil
}
// TransactionMutator is a interface that wraps the
// MutateTransaction operation. types may implement this interface to
// specify how they modify an xdr.Transaction object
type TransactionMutator interface {
MutateTransaction(*TransactionBuilder) error
}
// TransactionBuilder represents a Transaction that is being constructed.
type TransactionBuilder struct {
TX *xdr.Transaction
NetworkPassphrase string
BaseFee uint64
}
// Mutate applies the provided TransactionMutators to this builder's transaction
func (b *TransactionBuilder) Mutate(muts ...TransactionMutator) error {
if b.TX == nil {
b.TX = &xdr.Transaction{}
}
for i, m := range muts {
err := m.MutateTransaction(b)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("mutator:%d failed", i))
}
}
return nil
}
// Hash returns the hash of this builder's transaction.
func (b *TransactionBuilder) Hash() ([32]byte, error) {
return network.HashTransaction(b.TX, b.NetworkPassphrase)
}
// HashHex returns the hex-encoded hash of this builder's transaction
func (b *TransactionBuilder) HashHex() (string, error) {
hash, err := b.Hash()
if err != nil {
return "", err
}
return hex.EncodeToString(hash[:]), nil
}
// Sign returns an new TransactionEnvelopeBuilder using this builder's
// transaction as the basis and with signatures of that transaction from the
// provided Signers.
func (b *TransactionBuilder) Sign(signers ...string) (TransactionEnvelopeBuilder, error) {
var result TransactionEnvelopeBuilder
err := result.Mutate(b)
if err != nil {
return result, err
}
for _, s := range signers {
err := result.Mutate(Sign{s})
if err != nil {
return result, err
}
}
return result, nil
}
// ------------------------------------------------------------
//
// Mutator implementations
//
// ------------------------------------------------------------
// MutateTransaction for AccountMergeBuilder causes the underylying Destination
// to be added to the operation list for the provided transaction
func (m AccountMergeBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeAccountMerge, m.Destination)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for AllowTrustBuilder causes the underylying AllowTrustOp
// to be added to the operation list for the provided transaction
func (m AllowTrustBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeAllowTrust, m.AT)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for AutoSequence loads the sequence and sets it on the tx.
// NOTE: this mutator assumes that the source account has already been set on
// the transaction and will error if that has not occurred.
func (m AutoSequence) MutateTransaction(o *TransactionBuilder) error {
source := o.TX.SourceAccount
if source == (xdr.AccountId{}) {
return errors.New("auto sequence used prior to setting source account")
}
seq, err := m.SequenceForAccount(source.Address())
if err != nil {
return errors.Wrap(err, "couldn't load account for auto sequence")
}
o.TX.SeqNum = seq + 1
return nil
}
// MutateTransaction for ChangeTrustBuilder causes the underylying
// CreateAccountOp to be added to the operation list for the provided
// transaction
func (m ChangeTrustBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeChangeTrust, m.CT)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for CreateAccountBuilder causes the underylying
// CreateAccountOp to be added to the operation list for the provided
// transaction
func (m CreateAccountBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeCreateAccount, m.CA)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// DefaultBaseFee is used to calculate the transaction fee by default
var DefaultBaseFee uint64 = 100
// MutateTransaction for Defaults sets reasonable defaults on the transaction being built
func (m Defaults) MutateTransaction(o *TransactionBuilder) error {
if o.BaseFee == 0 {
o.BaseFee = DefaultBaseFee
}
if o.TX.Fee == 0 {
o.TX.Fee = xdr.Uint32(int(o.BaseFee) * len(o.TX.Operations))
}
if o.NetworkPassphrase == "" {
o.NetworkPassphrase = DefaultNetwork.Passphrase
}
return nil
}
// MutateTransaction for InflationBuilder causes the underylying
// InflationOp to be added to the operation list for the provided
// transaction
func (m InflationBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeInflation, nil)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for ManageDataBuilder causes the underylying
// ManageData to be added to the operation list for the provided
// transaction
func (m ManageDataBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeManageData, m.MD)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for ManageOfferBuilder causes the underylying
// ManageData to be added to the operation list for the provided
// transaction
func (m ManageOfferBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
if m.PassiveOffer {
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeCreatePassiveOffer, m.PO)
o.TX.Operations = append(o.TX.Operations, m.O)
} else {
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeManageOffer, m.MO)
o.TX.Operations = append(o.TX.Operations, m.O)
}
return m.Err
}
// MutateTransaction for MemoHash sets the memo.
func (m MemoHash) MutateTransaction(o *TransactionBuilder) (err error) {
o.TX.Memo, err = xdr.NewMemo(xdr.MemoTypeMemoHash, m.Value)
return
}
// MutateTransaction for MemoID sets the memo.
func (m MemoID) MutateTransaction(o *TransactionBuilder) (err error) {
o.TX.Memo, err = xdr.NewMemo(xdr.MemoTypeMemoId, xdr.Uint64(m.Value))
return
}
// MutateTransaction for MemoReturn sets the memo.
func (m MemoReturn) MutateTransaction(o *TransactionBuilder) (err error) {
o.TX.Memo, err = xdr.NewMemo(xdr.MemoTypeMemoReturn, m.Value)
return
}
// MutateTransaction for MemoText sets the memo.
func (m MemoText) MutateTransaction(o *TransactionBuilder) (err error) {
if len([]byte(m.Value)) > MemoTextMaxLength {
err = errors.New("Memo too long; over 28 bytes")
return
}
o.TX.Memo, err = xdr.NewMemo(xdr.MemoTypeMemoText, m.Value)
return
}
func (m Timebounds) MutateTransaction(o *TransactionBuilder) error {
o.TX.TimeBounds = &xdr.TimeBounds{MinTime: xdr.Uint64(m.MinTime), MaxTime: xdr.Uint64(m.MaxTime)}
return nil
}
// MutateTransaction for Network sets the Network ID to use when signing this transaction
func (m Network) MutateTransaction(o *TransactionBuilder) error {
o.NetworkPassphrase = m.Passphrase
return nil
}
// MutateTransaction for PaymentBuilder causes the underylying PaymentOp
// or PathPaymentOp to be added to the operation list for the provided transaction
func (m PaymentBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
if m.PathPayment {
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypePathPayment, m.PP)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypePayment, m.P)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for SetOptionsBuilder causes the underylying
// SetOptionsOp to be added to the operation list for the provided
// transaction
func (m SetOptionsBuilder) MutateTransaction(o *TransactionBuilder) error {
if m.Err != nil {
return m.Err
}
m.O.Body, m.Err = xdr.NewOperationBody(xdr.OperationTypeSetOptions, m.SO)
o.TX.Operations = append(o.TX.Operations, m.O)
return m.Err
}
// MutateTransaction for Sequence sets the SeqNum on the transaction.
func (m Sequence) MutateTransaction(o *TransactionBuilder) error {
o.TX.SeqNum = xdr.SequenceNumber(m.Sequence)
return nil
}
// MutateTransaction for SourceAccount sets the transaction's SourceAccount
// to the pubilic key for the address provided
func (m SourceAccount) MutateTransaction(o *TransactionBuilder) error {
return setAccountId(m.AddressOrSeed, &o.TX.SourceAccount)
}
// MutateTransaction for BaseFee sets the base fee
func (m BaseFee) MutateTransaction(o *TransactionBuilder) error {
o.BaseFee = m.Amount
return nil
}