forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_trust_line_flags.go
126 lines (106 loc) · 4 KB
/
set_trust_line_flags.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
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// TrustLineFlag represents the bitmask flags used to set and clear account authorization options.
type TrustLineFlag uint32
// TrustLineAuthorized is a flag that indicates whether the trustline is authorized.
const TrustLineAuthorized = TrustLineFlag(xdr.TrustLineFlagsAuthorizedFlag)
// TrustLineAuthorizedToMaintainLiabilities is a flag that if set, will allow a trustline to maintain liabilities
// without permitting any other operations.
const TrustLineAuthorizedToMaintainLiabilities = TrustLineFlag(xdr.TrustLineFlagsAuthorizedToMaintainLiabilitiesFlag)
// TrustLineClawbackEnabled is a flag that if set allows clawing back assets.
const TrustLineClawbackEnabled = TrustLineFlag(xdr.TrustLineFlagsTrustlineClawbackEnabledFlag)
// SetTrustLineFlags represents the Stellar set trust line flags operation. See
// https://developers.stellar.org/docs/start/list-of-operations/
type SetTrustLineFlags struct {
Trustor string
Asset Asset
SetFlags []TrustLineFlag
ClearFlags []TrustLineFlag
SourceAccount string
}
// BuildXDR for SetTrustLineFlags returns a fully configured XDR Operation.
func (stf *SetTrustLineFlags) BuildXDR() (xdr.Operation, error) {
var xdrOp xdr.SetTrustLineFlagsOp
// Set XDR address associated with the trustline
err := xdrOp.Trustor.SetAddress(stf.Trustor)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set trustor address")
}
// Validate this is an issued asset
if stf.Asset.IsNative() {
return xdr.Operation{}, errors.New("trustline doesn't exist for a native (XLM) asset")
}
xdrOp.Asset, err = stf.Asset.ToXDR()
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "can't convert asset to XDR")
}
xdrOp.ClearFlags = trustLineFlagsToXDR(stf.ClearFlags)
xdrOp.SetFlags = trustLineFlagsToXDR(stf.SetFlags)
opType := xdr.OperationTypeSetTrustLineFlags
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR OperationBody")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, stf.SourceAccount)
return op, nil
}
func trustLineFlagsToXDR(flags []TrustLineFlag) xdr.Uint32 {
var result xdr.Uint32
for _, flag := range flags {
result = result | xdr.Uint32(flag)
}
return result
}
// FromXDR for SetTrustLineFlags initialises the txnbuild struct from the corresponding xdr Operation.
func (stf *SetTrustLineFlags) FromXDR(xdrOp xdr.Operation) error {
op, ok := xdrOp.Body.GetSetTrustLineFlagsOp()
if !ok {
return errors.New("error parsing allow_trust operation from xdr")
}
stf.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
stf.Trustor = op.Trustor.Address()
asset, err := assetFromXDR(op.Asset)
if err != nil {
return errors.Wrap(err, "error parsing asset from xdr")
}
stf.Asset = asset
stf.ClearFlags = fromXDRTrustlineFlag(op.ClearFlags)
stf.SetFlags = fromXDRTrustlineFlag(op.SetFlags)
return nil
}
func fromXDRTrustlineFlag(flags xdr.Uint32) []TrustLineFlag {
flagsValue := xdr.TrustLineFlags(flags)
var result []TrustLineFlag
if flagsValue.IsAuthorized() {
result = append(result, TrustLineAuthorized)
}
if flagsValue.IsAuthorizedToMaintainLiabilitiesFlag() {
result = append(result, TrustLineAuthorizedToMaintainLiabilities)
}
if flagsValue.IsClawbackEnabledFlag() {
result = append(result, TrustLineClawbackEnabled)
}
return result
}
// Validate for SetTrustLineFlags validates the required struct fields. It returns an error if any of the fields are
// invalid. Otherwise, it returns nil.
func (stf *SetTrustLineFlags) Validate() error {
err := validateStellarPublicKey(stf.Trustor)
if err != nil {
return NewValidationError("Trustor", err.Error())
}
err = validateAssetCode(stf.Asset)
if err != nil {
return NewValidationError("Asset", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or nil if not
// set.
func (stf *SetTrustLineFlags) GetSourceAccount() string {
return stf.SourceAccount
}