forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allow_trust.go
117 lines (99 loc) · 3.56 KB
/
allow_trust.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
package txnbuild
import (
"bytes"
"fmt"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// Deprecated: use SetTrustLineFlags instead.
// AllowTrust represents the Stellar allow trust operation. See
// https://developers.stellar.org/docs/start/list-of-operations/
type AllowTrust struct {
Trustor string
Type Asset
Authorize bool
AuthorizeToMaintainLiabilities bool
ClawbackEnabled bool
SourceAccount string
}
// BuildXDR for AllowTrust returns a fully configured XDR Operation.
func (at *AllowTrust) BuildXDR() (xdr.Operation, error) {
var xdrOp xdr.AllowTrustOp
// Set XDR address associated with the trustline
err := xdrOp.Trustor.SetAddress(at.Trustor)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set trustor address")
}
// Validate this is an issued asset
if at.Type.IsNative() {
return xdr.Operation{}, errors.New("trustline doesn't exist for a native (XLM) asset")
}
// AllowTrust has a special asset type - map to it
xdrAsset := xdr.Asset{}
xdrOp.Asset, err = xdrAsset.ToAssetCode(at.Type.GetCode())
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "can't convert asset for trustline to allow trust asset type")
}
// Set XDR auth flag
if at.Authorize {
xdrOp.Authorize = xdr.Uint32(xdr.TrustLineFlagsAuthorizedFlag)
} else if at.AuthorizeToMaintainLiabilities {
xdrOp.Authorize = xdr.Uint32(xdr.TrustLineFlagsAuthorizedToMaintainLiabilitiesFlag)
}
opType := xdr.OperationTypeAllowTrust
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, at.SourceAccount)
return op, nil
}
func assetCodeToCreditAsset(assetCode xdr.AssetCode) (CreditAsset, error) {
switch assetCode.Type {
case xdr.AssetTypeAssetTypeCreditAlphanum4:
code := bytes.Trim(assetCode.AssetCode4[:], "\x00")
return CreditAsset{Code: string(code[:])}, nil
case xdr.AssetTypeAssetTypeCreditAlphanum12:
code := bytes.Trim(assetCode.AssetCode12[:], "\x00")
return CreditAsset{Code: string(code[:])}, nil
default:
return CreditAsset{}, fmt.Errorf("unknown asset type: %d", assetCode.Type)
}
}
// FromXDR for AllowTrust initialises the txnbuild struct from the corresponding xdr Operation.
func (at *AllowTrust) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetAllowTrustOp()
if !ok {
return errors.New("error parsing allow_trust operation from xdr")
}
at.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
at.Trustor = result.Trustor.Address()
flag := xdr.TrustLineFlags(result.Authorize)
at.Authorize = flag.IsAuthorized()
at.AuthorizeToMaintainLiabilities = flag.IsAuthorizedToMaintainLiabilitiesFlag()
t, err := assetCodeToCreditAsset(result.Asset)
if err != nil {
return errors.Wrap(err, "error parsing allow_trust operation from xdr")
}
at.Type = t
return nil
}
// Validate for AllowTrust validates the required struct fields. It returns an error if any of the fields are
// invalid. Otherwise, it returns nil.
func (at *AllowTrust) Validate() error {
err := validateStellarPublicKey(at.Trustor)
if err != nil {
return NewValidationError("Trustor", err.Error())
}
err = validateAssetCode(at.Type)
if err != nil {
return NewValidationError("Type", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (at *AllowTrust) GetSourceAccount() string {
return at.SourceAccount
}