forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder_allow_trust.go
57 lines (46 loc) · 1.82 KB
/
builder_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
package bridge
import (
shared "github.com/stellar/go/services/internal/bridge-compliance-shared"
"github.com/stellar/go/services/internal/bridge-compliance-shared/http/helpers"
"github.com/stellar/go/txnbuild"
)
// AllowTrustOperationBody represents allow_trust operation
type AllowTrustOperationBody struct {
Source *string
AssetCode string `json:"asset_code"`
Trustor string
Authorize bool
}
// Build returns a txnbuild.Operation
func (op AllowTrustOperationBody) Build() txnbuild.Operation {
txnOp := txnbuild.AllowTrust{
Trustor: op.Trustor,
Authorize: op.Authorize,
}
if op.Source != nil {
txnOp.SourceAccount = &txnbuild.SimpleAccount{AccountID: *op.Source}
txnOp.Type = txnbuild.CreditAsset{Code: op.AssetCode, Issuer: *op.Source}
}
return &txnOp
}
// Validate validates if operation body is valid.
func (op AllowTrustOperationBody) Validate() error {
// Note (Peter 23-05-2019): We need source account to be set here because it is used for
// creating an asset type which is needed by txnbuild.AllowTrust.
// to do: Update documentation for bridge server to indicate that source account is required for
// AllowTrust operation. Alternatively, update txnbuild to ignore issuer when building
// AllowTrust operations.
if op.Source == nil {
return helpers.NewInvalidParameterError("source", "Source must be specified for AllowTrust operation.")
}
if !shared.IsValidAssetCode(op.AssetCode) {
return helpers.NewInvalidParameterError("asset_code", "Asset code is invalid")
}
if !shared.IsValidAccountID(op.Trustor) {
return helpers.NewInvalidParameterError("trustor", "Trustor must be a public key (starting with `G`).")
}
if op.Source != nil && !shared.IsValidAccountID(*op.Source) {
return helpers.NewInvalidParameterError("source", "Source must be a public key (starting with `G`).")
}
return nil
}