-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathAccountEntry.java
More file actions
317 lines (295 loc) · 11.2 KB
/
AccountEntry.java
File metadata and controls
317 lines (295 loc) · 11.2 KB
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
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten
package org.stellar.sdk.xdr;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.stellar.sdk.Base64Factory;
/**
* AccountEntry's original definition in the XDR file is:
*
* <pre>
* struct AccountEntry
* {
* AccountID accountID; // master public key for this account
* int64 balance; // in stroops
* SequenceNumber seqNum; // last sequence number used for this account
* uint32 numSubEntries; // number of sub-entries this account has
* // drives the reserve
* AccountID* inflationDest; // Account to vote for during inflation
* uint32 flags; // see AccountFlags
*
* string32 homeDomain; // can be used for reverse federation and memo lookup
*
* // fields used for signatures
* // thresholds stores unsigned bytes: [weight of master|low|medium|high]
* Thresholds thresholds;
*
* Signer signers<MAX_SIGNERS>; // possible signers for this account
*
* // reserved for future use
* union switch (int v)
* {
* case 0:
* void;
* case 1:
* AccountEntryExtensionV1 v1;
* }
* ext;
* };
* </pre>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class AccountEntry implements XdrElement {
private AccountID accountID;
private Int64 balance;
private SequenceNumber seqNum;
private Uint32 numSubEntries;
private AccountID inflationDest;
private Uint32 flags;
private String32 homeDomain;
private Thresholds thresholds;
private Signer[] signers;
private AccountEntryExt ext;
public void encode(XdrDataOutputStream stream) throws IOException {
accountID.encode(stream);
balance.encode(stream);
seqNum.encode(stream);
numSubEntries.encode(stream);
if (inflationDest != null) {
stream.writeInt(1);
inflationDest.encode(stream);
} else {
stream.writeInt(0);
}
flags.encode(stream);
homeDomain.encode(stream);
thresholds.encode(stream);
int signersSize = getSigners().length;
if (signersSize > 20) {
throw new IOException("signers size " + signersSize + " exceeds max size 20");
}
stream.writeInt(signersSize);
for (int i = 0; i < signersSize; i++) {
signers[i].encode(stream);
}
ext.encode(stream);
}
public static AccountEntry decode(XdrDataInputStream stream, int maxDepth) throws IOException {
if (maxDepth <= 0) {
throw new IOException("Maximum decoding depth reached");
}
maxDepth -= 1;
AccountEntry decodedAccountEntry = new AccountEntry();
decodedAccountEntry.accountID = AccountID.decode(stream, maxDepth);
decodedAccountEntry.balance = Int64.decode(stream, maxDepth);
decodedAccountEntry.seqNum = SequenceNumber.decode(stream, maxDepth);
decodedAccountEntry.numSubEntries = Uint32.decode(stream, maxDepth);
boolean inflationDestPresent = stream.readXdrBoolean();
if (inflationDestPresent) {
decodedAccountEntry.inflationDest = AccountID.decode(stream, maxDepth);
}
decodedAccountEntry.flags = Uint32.decode(stream, maxDepth);
decodedAccountEntry.homeDomain = String32.decode(stream, maxDepth);
decodedAccountEntry.thresholds = Thresholds.decode(stream, maxDepth);
int signersSize = stream.readInt();
if (signersSize < 0) {
throw new IOException("signers size " + signersSize + " is negative");
}
if (signersSize > 20) {
throw new IOException("signers size " + signersSize + " exceeds max size 20");
}
int signersRemainingInputLen = stream.getRemainingInputLen();
if (signersRemainingInputLen >= 0 && signersRemainingInputLen < signersSize) {
throw new IOException(
"signers size "
+ signersSize
+ " exceeds remaining input length "
+ signersRemainingInputLen);
}
decodedAccountEntry.signers = new Signer[signersSize];
for (int i = 0; i < signersSize; i++) {
decodedAccountEntry.signers[i] = Signer.decode(stream, maxDepth);
}
decodedAccountEntry.ext = AccountEntryExt.decode(stream, maxDepth);
return decodedAccountEntry;
}
public static AccountEntry decode(XdrDataInputStream stream) throws IOException {
return decode(stream, XdrDataInputStream.DEFAULT_MAX_DEPTH);
}
public static AccountEntry fromXdrBase64(String xdr) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes);
}
public static AccountEntry fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
xdrDataInputStream.setMaxInputLen(xdr.length);
return decode(xdrDataInputStream);
}
@Override
public String toJson() {
return XdrElement.gson.toJson(toJsonObject());
}
public static AccountEntry fromJson(String json) {
return fromJsonObject(XdrElement.gson.fromJson(json, Object.class));
}
Object toJsonObject() {
LinkedHashMap<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("account_id", accountID.toJsonObject());
jsonMap.put("balance", balance.toJsonObject());
jsonMap.put("seq_num", seqNum.toJsonObject());
jsonMap.put("num_sub_entries", numSubEntries.toJsonObject());
jsonMap.put("inflation_dest", inflationDest != null ? inflationDest.toJsonObject() : null);
jsonMap.put("flags", flags.toJsonObject());
jsonMap.put("home_domain", homeDomain.toJsonObject());
jsonMap.put("thresholds", thresholds.toJsonObject());
jsonMap.put("signers", XdrElement.arrayToJsonArray(signers, i -> signers[i].toJsonObject()));
jsonMap.put("ext", ext.toJsonObject());
return jsonMap;
}
@SuppressWarnings("unchecked")
static AccountEntry fromJsonObject(Object json) {
java.util.Map<String, Object> jsonMap = (java.util.Map<String, Object>) json;
AccountEntry instance = new AccountEntry();
instance.accountID = AccountID.fromJsonObject(jsonMap.get("account_id"));
instance.balance = Int64.fromJsonObject(jsonMap.get("balance"));
instance.seqNum = SequenceNumber.fromJsonObject(jsonMap.get("seq_num"));
instance.numSubEntries = Uint32.fromJsonObject(jsonMap.get("num_sub_entries"));
instance.inflationDest =
jsonMap.get("inflation_dest") != null
? AccountID.fromJsonObject(jsonMap.get("inflation_dest"))
: null;
instance.flags = Uint32.fromJsonObject(jsonMap.get("flags"));
instance.homeDomain = String32.fromJsonObject(jsonMap.get("home_domain"));
instance.thresholds = Thresholds.fromJsonObject(jsonMap.get("thresholds"));
instance.signers =
XdrElement.jsonArrayToArray(
(List<Object>) jsonMap.get("signers"),
Signer.class,
item -> Signer.fromJsonObject(item));
instance.ext = AccountEntryExt.fromJsonObject(jsonMap.get("ext"));
return instance;
}
/**
* AccountEntryExt's original definition in the XDR file is:
*
* <pre>
* union switch (int v)
* {
* case 0:
* void;
* case 1:
* AccountEntryExtensionV1 v1;
* }
* </pre>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public static class AccountEntryExt implements XdrElement {
private Integer discriminant;
private AccountEntryExtensionV1 v1;
public void encode(XdrDataOutputStream stream) throws IOException {
stream.writeInt(discriminant);
switch (discriminant) {
case 0:
break;
case 1:
v1.encode(stream);
break;
}
}
public static AccountEntryExt decode(XdrDataInputStream stream, int maxDepth)
throws IOException {
if (maxDepth <= 0) {
throw new IOException("Maximum decoding depth reached");
}
maxDepth -= 1;
AccountEntryExt decodedAccountEntryExt = new AccountEntryExt();
Integer discriminant = stream.readInt();
decodedAccountEntryExt.setDiscriminant(discriminant);
switch (decodedAccountEntryExt.getDiscriminant()) {
case 0:
break;
case 1:
decodedAccountEntryExt.v1 = AccountEntryExtensionV1.decode(stream, maxDepth);
break;
default:
throw new IOException("Unknown discriminant value: " + discriminant);
}
return decodedAccountEntryExt;
}
public static AccountEntryExt decode(XdrDataInputStream stream) throws IOException {
return decode(stream, XdrDataInputStream.DEFAULT_MAX_DEPTH);
}
public static AccountEntryExt fromXdrBase64(String xdr) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes);
}
public static AccountEntryExt fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
xdrDataInputStream.setMaxInputLen(xdr.length);
return decode(xdrDataInputStream);
}
@Override
public String toJson() {
return XdrElement.gson.toJson(toJsonObject());
}
public static AccountEntryExt fromJson(String json) {
return fromJsonObject(XdrElement.gson.fromJson(json, Object.class));
}
Object toJsonObject() {
if (discriminant == 0) {
return "v0";
}
if (discriminant == 1) {
LinkedHashMap<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("v1", v1.toJsonObject());
return jsonMap;
}
throw new IllegalArgumentException("Unknown discriminant: " + discriminant);
}
@SuppressWarnings("unchecked")
static AccountEntryExt fromJsonObject(Object json) {
if (json instanceof String) {
String strVal = (String) json;
if (!(strVal.equals("v0"))) {
throw new IllegalArgumentException(
"Unexpected string '" + strVal + "' for AccountEntryExt");
}
AccountEntryExt instance = new AccountEntryExt();
instance.discriminant = Integer.parseInt(strVal.substring(1));
return instance;
}
java.util.Map<String, Object> jsonMap = (java.util.Map<String, Object>) json;
if (jsonMap.containsKey("$schema")) {
jsonMap = new LinkedHashMap<>(jsonMap);
jsonMap.remove("$schema");
}
if (jsonMap.size() != 1) {
throw new IllegalArgumentException(
"Expected a single-key object for AccountEntryExt, got: " + json);
}
String key = jsonMap.keySet().iterator().next();
Integer discriminant = Integer.parseInt(key.substring(1));
if (key.equals("v1")) {
AccountEntryExt instance = new AccountEntryExt();
instance.discriminant = discriminant;
instance.v1 = AccountEntryExtensionV1.fromJsonObject(jsonMap.get("v1"));
return instance;
}
throw new IllegalArgumentException("Unknown key '" + key + "' for AccountEntryExt");
}
}
}