forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_options.go
328 lines (287 loc) · 10.6 KB
/
set_options.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
320
321
322
323
324
325
326
327
328
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// AccountFlag represents the bitmask flags used to set and clear account authorization options.
type AccountFlag uint32
// AuthRequired is a flag that requires the issuing account to give other accounts
// permission before they can hold the issuing account's credit.
const AuthRequired = AccountFlag(xdr.AccountFlagsAuthRequiredFlag)
// AuthRevocable is a flag that allows the issuing account to revoke its credit
// held by other accounts.
const AuthRevocable = AccountFlag(xdr.AccountFlagsAuthRevocableFlag)
// AuthImmutable is a flag that if set prevents any authorization flags from being
// set, and prevents the account from ever being merged (deleted).
const AuthImmutable = AccountFlag(xdr.AccountFlagsAuthImmutableFlag)
// AuthClawbackEnabled is a flag that if set allows clawing back assets.
const AuthClawbackEnabled = AccountFlag(xdr.AccountFlagsAuthClawbackEnabledFlag)
// Threshold is the datatype for MasterWeight, Signer.Weight, and Thresholds. Each is a number
// between 0-255 inclusive.
type Threshold uint8
// Signer represents the Signer in a SetOptions operation.
// If the signer already exists, it is updated.
// If the weight is 0, the signer is deleted.
type Signer struct {
Address string
Weight Threshold
}
// NewHomeDomain is syntactic sugar that makes instantiating SetOptions more convenient.
func NewHomeDomain(hd string) *string {
return &hd
}
// NewThreshold is syntactic sugar that makes instantiating SetOptions more convenient.
func NewThreshold(t Threshold) *Threshold {
return &t
}
// NewInflationDestination is syntactic sugar that makes instantiating SetOptions more convenient.
func NewInflationDestination(ai string) *string {
return &ai
}
// SetOptions represents the Stellar set options operation. See
// https://developers.stellar.org/docs/start/list-of-operations/
type SetOptions struct {
InflationDestination *string
SetFlags []AccountFlag
ClearFlags []AccountFlag
MasterWeight *Threshold
LowThreshold *Threshold
MediumThreshold *Threshold
HighThreshold *Threshold
HomeDomain *string
Signer *Signer
xdrOp xdr.SetOptionsOp
SourceAccount string
}
// BuildXDR for SetOptions returns a fully configured XDR Operation.
func (so *SetOptions) BuildXDR() (xdr.Operation, error) {
err := so.handleInflation()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set inflation destination address")
}
so.handleClearFlags()
so.handleSetFlags()
so.handleMasterWeight()
so.handleLowThreshold()
so.handleMediumThreshold()
so.handleHighThreshold()
err = so.handleHomeDomain()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set home domain")
}
err = so.handleSigner()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set signer")
}
opType := xdr.OperationTypeSetOptions
body, err := xdr.NewOperationBody(opType, so.xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR OperationBody")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, so.SourceAccount)
return op, nil
}
// handleInflation for SetOptions sets the XDR inflation destination.
// Once set, a new address can be set, but there's no way to ever unset.
func (so *SetOptions) handleInflation() (err error) {
if so.InflationDestination != nil {
var xdrAccountID xdr.AccountId
err = xdrAccountID.SetAddress(*so.InflationDestination)
if err != nil {
return
}
so.xdrOp.InflationDest = &xdrAccountID
}
return
}
// handleInflationXDR for SetOptions sets the inflation destination from a XDR object.
func (so *SetOptions) handleInflationXDR(account *xdr.AccountId) {
if account != nil {
address := account.Address()
so.InflationDestination = &address
}
}
// handleSetFlags for SetOptions sets XDR account flags (represented as a bitmask).
// See https://developers.stellar.org/docs/glossary/accounts/#flags
func (so *SetOptions) handleSetFlags() {
var flags xdr.Uint32
for _, flag := range so.SetFlags {
flags = flags | xdr.Uint32(flag)
}
if len(so.SetFlags) > 0 {
so.xdrOp.SetFlags = &flags
}
}
// handleSetFlagsXDR for SetOptions sets account flags from XDR object (represented as a bitmask).
// See https://developers.stellar.org/docs/glossary/accounts/#flags
func (so *SetOptions) handleSetFlagsXDR(flags *xdr.Uint32) {
if flags != nil {
for _, f := range []AccountFlag{AuthRequired, AuthRevocable, AuthImmutable, AuthClawbackEnabled} {
if f&AccountFlag(*flags) != 0 {
so.SetFlags = append(so.SetFlags, f)
}
}
}
}
// handleClearFlags for SetOptions unsets XDR account flags (represented as a bitmask).
// See https://developers.stellar.org/docs/glossary/accounts/#flags
func (so *SetOptions) handleClearFlags() {
var flags xdr.Uint32
for _, flag := range so.ClearFlags {
flags = flags | xdr.Uint32(flag)
}
if len(so.ClearFlags) > 0 {
so.xdrOp.ClearFlags = &flags
}
}
// handleClearFlagsXDR for SetOptions unsets account flags (represented as a bitmask).
// See https://developers.stellar.org/docs/glossary/accounts/#flags
func (so *SetOptions) handleClearFlagsXDR(flags *xdr.Uint32) {
if flags != nil {
for _, f := range []AccountFlag{AuthRequired, AuthRevocable, AuthImmutable, AuthClawbackEnabled} {
if f&AccountFlag(*flags) != 0 {
so.ClearFlags = append(so.ClearFlags, f)
}
}
}
}
// handleMasterWeight for SetOptions sets the XDR weight of the master signing key.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleMasterWeight() {
if so.MasterWeight != nil {
xdrWeight := xdr.Uint32(*so.MasterWeight)
so.xdrOp.MasterWeight = &xdrWeight
}
}
// handleMasterWeightXDR for SetOptions sets the weight of the master signing key.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleMasterWeightXDR(weight *xdr.Uint32) {
if weight != nil {
mw := Threshold(uint32(*weight))
so.MasterWeight = &mw
}
}
// handleLowThreshold for SetOptions sets the XDR value of the account's "low" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleLowThreshold() {
if so.LowThreshold != nil {
xdrThreshold := xdr.Uint32(*so.LowThreshold)
so.xdrOp.LowThreshold = &xdrThreshold
}
}
// handleLowThresholdXDR for SetOptions sets value of the account's "low" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleLowThresholdXDR(weight *xdr.Uint32) {
if weight != nil {
lt := Threshold(uint32(*weight))
so.LowThreshold = <
}
}
// handleMediumThreshold for SetOptions sets the XDR value of the account's "medium" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleMediumThreshold() {
if so.MediumThreshold != nil {
xdrThreshold := xdr.Uint32(*so.MediumThreshold)
so.xdrOp.MedThreshold = &xdrThreshold
}
}
// handleLowMediumXDR for SetOptions sets value of the account's "medium" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleMediumThresholdXDR(weight *xdr.Uint32) {
if weight != nil {
mt := Threshold(uint32(*weight))
so.MediumThreshold = &mt
}
}
// handleHighThreshold for SetOptions sets the XDR value of the account's "high" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleHighThreshold() {
if so.HighThreshold != nil {
xdrThreshold := xdr.Uint32(*so.HighThreshold)
so.xdrOp.HighThreshold = &xdrThreshold
}
}
// handleHighThresholdXDR for SetOptions sets value of the account's "high" threshold.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleHighThresholdXDR(weight *xdr.Uint32) {
if weight != nil {
ht := Threshold(uint32(*weight))
so.HighThreshold = &ht
}
}
// handleHomeDomain for SetOptions sets the XDR value of the account's home domain.
// https://developers.stellar.org/docs/glossary/federation/
func (so *SetOptions) handleHomeDomain() error {
if so.HomeDomain != nil {
if len(*so.HomeDomain) > 32 {
return errors.New("homeDomain must be 32 characters or less")
}
xdrHomeDomain := xdr.String32(*so.HomeDomain)
so.xdrOp.HomeDomain = &xdrHomeDomain
}
return nil
}
// handleHomeDomainXDR for SetOptions sets the value of the account's home domain.
// https://developers.stellar.org/docs/glossary/federation/
func (so *SetOptions) handleHomeDomainXDR(xDomain *xdr.String32) {
if xDomain != nil {
domain := string(*xDomain)
so.HomeDomain = &domain
}
}
// handleSigner for SetOptions sets the XDR value of a signer for the account.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleSigner() (err error) {
if so.Signer != nil {
var xdrSigner xdr.Signer
xdrWeight := xdr.Uint32(so.Signer.Weight)
xdrSigner.Weight = xdrWeight
err = xdrSigner.Key.SetAddress(so.Signer.Address)
if err != nil {
return
}
so.xdrOp.Signer = &xdrSigner
}
return nil
}
// handleSignerXDR for SetOptions sets the value of a signer for the account.
// See https://developers.stellar.org/docs/glossary/multisig/
func (so *SetOptions) handleSignerXDR(xSigner *xdr.Signer) {
if xSigner != nil {
newSigner := Signer{}
newSigner.Address = xSigner.Key.Address()
newSigner.Weight = Threshold(uint32(xSigner.Weight))
so.Signer = &newSigner
}
}
// FromXDR for SetOptions initialises the txnbuild struct from the corresponding xdr Operation.
func (so *SetOptions) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetSetOptionsOp()
if !ok {
return errors.New("error parsing set_options operation from xdr")
}
so.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
so.handleInflationXDR(result.InflationDest)
so.handleClearFlagsXDR(result.ClearFlags)
so.handleSetFlagsXDR(result.SetFlags)
so.handleMasterWeightXDR(result.MasterWeight)
so.handleLowThresholdXDR(result.LowThreshold)
so.handleMediumThresholdXDR(result.MedThreshold)
so.handleHighThresholdXDR(result.HighThreshold)
so.handleHomeDomainXDR(result.HomeDomain)
so.handleSignerXDR(result.Signer)
return nil
}
// Validate for SetOptions validates the required struct fields. It returns an error if any
// of the fields are invalid. Otherwise, it returns nil.
func (so *SetOptions) Validate() error {
// skipping checks here because the individual methods above already check for required fields.
// Refactoring is out of the scope of this issue(https://github.com/stellar/go/issues/1041) so will leave as is for now.
return nil
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (so *SetOptions) GetSourceAccount() string {
return so.SourceAccount
}