-
Notifications
You must be signed in to change notification settings - Fork 163
/
AllowTrustOperation.java
158 lines (140 loc) · 5.41 KB
/
AllowTrustOperation.java
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
package org.stellar.sdk;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import org.stellar.sdk.xdr.*;
/**
* @deprecated As of release 0.24.0, replaced by {@link SetTrustlineFlagsOperation}
* <p>Represents <a
* href="https://developers.stellar.org/docs/fundamentals-and-concepts/list-of-operations#allow-trust"
* target="_blank">AllowTrust</a> operation.
*/
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@Deprecated
public class AllowTrustOperation extends Operation {
/** The account of the recipient of the trustline. */
@Getter @NonNull private final String trustor;
/**
* The asset of the trustline the source account is authorizing. For example, if a gateway wants
* to allow another account to hold its USD credit, the type is USD.
*/
@Getter @NonNull private final String assetCode;
/** Flag indicating whether the trustline is authorized. */
private final boolean authorize;
/** Flag indicating whether the trustline is authorized to maintain liabilities. */
@Getter private final boolean authorizeToMaintainLiabilities;
public boolean getAuthorize() {
// lombok will generate a getter for authorize, but it will be `isAuthorize` instead of.
// For keep the same with the old version, we need to override the getter.
return authorize;
}
@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
AllowTrustOp op = new AllowTrustOp();
// trustor
op.setTrustor(StrKey.encodeToXDRAccountId(this.trustor));
// asset
AssetCode asset = new AssetCode();
if (assetCode.length() <= 4) {
AssetCode4 assetCode4 = new AssetCode4();
assetCode4.setAssetCode4(Util.paddedByteArray(assetCode, 4));
asset.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM4);
asset.setAssetCode4(assetCode4);
} else {
AssetCode12 assetCode12 = new AssetCode12();
assetCode12.setAssetCode12(Util.paddedByteArray(assetCode, 12));
asset.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM12);
asset.setAssetCode12(assetCode12);
}
op.setAsset(asset);
Uint32 flag = new Uint32();
// authorize
if (authorize) {
flag.setUint32(new XdrUnsignedInteger(TrustLineFlags.AUTHORIZED_FLAG.getValue()));
} else if (authorizeToMaintainLiabilities) {
flag.setUint32(
new XdrUnsignedInteger(
TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue()));
} else {
flag.setUint32(new XdrUnsignedInteger(0));
}
op.setAuthorize(flag);
org.stellar.sdk.xdr.Operation.OperationBody body =
new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.ALLOW_TRUST);
body.setAllowTrustOp(op);
return body;
}
/**
* Builds AllowTrust operation.
*
* @see AllowTrustOperation
*/
public static class Builder {
private final String trustor;
private final String assetCode;
private final boolean authorize;
private boolean authorizeToMaintainLiabilities;
private String sourceAccount;
Builder(AllowTrustOp op) {
trustor = StrKey.encodeEd25519PublicKey(op.getTrustor());
switch (op.getAsset().getDiscriminant()) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
break;
case ASSET_TYPE_CREDIT_ALPHANUM12:
assetCode = new String(op.getAsset().getAssetCode12().getAssetCode12()).trim();
break;
default:
throw new RuntimeException("Unknown asset code");
}
int flag = op.getAuthorize().getUint32().getNumber().intValue();
if (flag == TrustLineFlags.AUTHORIZED_FLAG.getValue()) {
authorize = true;
authorizeToMaintainLiabilities = false;
} else if (flag == TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue()) {
authorize = false;
authorizeToMaintainLiabilities = true;
} else if (flag != 0) {
throw new IllegalArgumentException("invalid authorize flag " + flag);
} else {
authorize = false;
authorizeToMaintainLiabilities = false;
}
}
/**
* Creates a new AllowTrust builder.
*
* @param trustor The account of the recipient of the trustline.
* @param assetCode The asset of the trustline the source account is authorizing. For example,
* if a gateway wants to allow another account to hold its USD credit, the type is USD.
* @param authorize Flag indicating whether the trustline is authorized.
*/
public Builder(String trustor, String assetCode, boolean authorize) {
this.trustor = trustor;
this.assetCode = assetCode;
this.authorize = authorize;
}
/**
* Set source account of this operation
*
* @param sourceAccount Source account
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(String sourceAccount) {
this.sourceAccount = sourceAccount;
return this;
}
/** Builds an operation */
public AllowTrustOperation build() {
AllowTrustOperation operation =
new AllowTrustOperation(trustor, assetCode, authorize, authorizeToMaintainLiabilities);
if (sourceAccount != null) {
operation.setSourceAccount(sourceAccount);
}
return operation;
}
}
}