-
Notifications
You must be signed in to change notification settings - Fork 2
/
IridiumClient.java
410 lines (312 loc) · 16.2 KB
/
IridiumClient.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package cash.ird.walletd;
import cash.ird.walletd.model.body.*;
import cash.ird.walletd.model.request.BlockIndexRange;
import cash.ird.walletd.model.request.BlockRange;
import cash.ird.walletd.model.request.Key;
import cash.ird.walletd.model.response.*;
import cash.ird.walletd.rpc.HttpClient;
import cash.ird.walletd.rpc.WalletdClient;
import cash.ird.walletd.rpc.exception.IridiumWalletdException;
import cash.ird.walletd.rpc.method.RequestMethod;
import lombok.NonNull;
import java.util.*;
public class IridiumClient implements IridiumAPI {
private static final String DEFAULT_PROTOCOL = "http";
private static final String DEFAULT_PATH = "json_rpc";
private final WalletdClient walletdClient;
public IridiumClient() {
this("localhost", 14007);
}
public IridiumClient(@NonNull String host, @NonNull int port) {
this(String.format("%s://%s:%s/%s", DEFAULT_PROTOCOL, host, port, DEFAULT_PATH));
}
public IridiumClient(String url) {
this.walletdClient = new WalletdClient(url);
}
public IridiumClient(String url, HttpClient httpClient) {
this.walletdClient = new WalletdClient(url, httpClient);
}
@Override
public boolean reset() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.RESET, NoopResponse.class) != null;
}
@Override
public boolean reset(String viewSecretKey) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (viewSecretKey != null) {
params.put("viewSecretKey", viewSecretKey);
}
return this.walletdClient.doRequest(RequestMethod.RESET, Collections.unmodifiableMap(params), NoopResponse.class) != null;
}
@Override
public boolean save() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.SAVE, NoopResponse.class) != null;
}
@Override
public String getViewKey() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_VIEW_KEY, ViewKeyResponse.class);
}
@Override
public SpendKeyPair getSpendKeys(String address) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
params.put("address", address);
return this.walletdClient.doRequest(RequestMethod.GET_SPEND_KEYS, Collections.unmodifiableMap(params), SpendKeyResponse.class);
}
@Override
public Status getStatus() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_STATUS, StatusResponse.class);
}
@Override
public List<String> getAddresses() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_ADDRESSES, AddressListResponse.class);
}
@Override
public String createAddress() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.CREATE_ADDRESS, AddressResponse.class);
}
@Override
public String createAddress(Key key) throws IridiumWalletdException {
if (key != null) {
Map<String, Object> params = buildParams();
if (key.isPrivate()) {
params.put("spendSecretKey", key.getValue());
} else {
params.put("spendPublicKey", key.getValue());
}
return this.walletdClient.doRequest(RequestMethod.CREATE_ADDRESS, Collections.unmodifiableMap(params), AddressResponse.class);
} else {
return this.createAddress();
}
}
@Override
public boolean deleteAddress(String address) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (address != null) {
params.put("address", address);
} else {
throw new IllegalArgumentException("address must not be null!");
}
return this.walletdClient.doRequest(RequestMethod.DELETE_ADDRESS, Collections.unmodifiableMap(params), NoopResponse.class) != null;
}
@Override
public Balance getBalance() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_BALANCE, BalanceResponse.class);
}
@Override
public Balance getBalance(String address) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (address != null) {
params.put("address", address);
}
return this.walletdClient.doRequest(RequestMethod.GET_BALANCE, Collections.unmodifiableMap(params), BalanceResponse.class);
}
@Override
public List<String> getBlockHashes(BlockIndexRange blockIndexRange) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
params.put("firstBlockIndex", blockIndexRange.getStart());
params.put("blockCount", blockIndexRange.getCount());
return this.walletdClient.doRequest(RequestMethod.GET_BLOCK_HASHES, Collections.unmodifiableMap(params), BlockHashListResponse.class);
}
@Override
public <T extends BlockRange> List<TransactionHashBag> getTransactionHashes(T blockRange) throws IridiumWalletdException {
return this.getTransactionHashes(blockRange, null, null);
}
@Override
public <T extends BlockRange> List<TransactionHashBag> getTransactionHashes(T blockRange, List<String> addresses) throws IridiumWalletdException {
return this.getTransactionHashes(blockRange, addresses, null);
}
@Override
public <T extends BlockRange> List<TransactionHashBag> getTransactionHashes(T blockRange, List<String> addresses, String paymentId) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (blockRange != null) {
params.put(blockRange.getType().getParamName(), blockRange.getStart());
params.put("blockCount", blockRange.getCount());
} else {
throw new IllegalArgumentException("blockRange must not be null!");
}
if (addresses != null && !addresses.isEmpty()) {
params.put("addresses", addresses);
}
if (paymentId != null) {
params.put("paymentId", paymentId);
}
return this.walletdClient.doRequest(RequestMethod.GET_TRANSACTION_HASHES, Collections.unmodifiableMap(params), TransactionHashBagListResponse.class);
}
@Override
public <T extends BlockRange> List<TransactionItemBag> getTransactions(T blockRange) throws IridiumWalletdException {
return this.getTransactions(blockRange, null, null);
}
@Override
public <T extends BlockRange> List<TransactionItemBag> getTransactions(T blockRange, List<String> addresses) throws IridiumWalletdException {
return this.getTransactions(blockRange, addresses, null);
}
@Override
public <T extends BlockRange> List<TransactionItemBag> getTransactions(T blockRange, List<String> addresses, String paymentId) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (blockRange != null) {
params.put(blockRange.getType().getParamName(), blockRange.getStart());
params.put("blockCount", blockRange.getCount());
} else {
throw new IllegalArgumentException("blockRange must not be null!");
}
if (addresses != null && !addresses.isEmpty()) {
params.put("addresses", addresses);
}
if (paymentId != null) {
params.put("paymentId", paymentId);
}
return this.walletdClient.doRequest(RequestMethod.GET_TRANSACTIONS, Collections.unmodifiableMap(params), TransactionItemBagListResponse.class);
}
@Override
public List<String> getUnconfirmedTransactionHashes() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_UNCONFIRMED_TRANSACTION_HASHES, TransactionHashListResponse.class);
}
@Override
public List<String> getUnconfirmedTransactionHashes(List<String> addresses) throws IridiumWalletdException {
if (addresses != null && !addresses.isEmpty()) {
Map<String, Object> params = buildParams();
params.put("addresses", addresses);
return this.walletdClient.doRequest(RequestMethod.GET_UNCONFIRMED_TRANSACTION_HASHES, Collections.unmodifiableMap(params), TransactionHashListResponse.class);
} else {
return this.getUnconfirmedTransactionHashes();
}
}
@Override
public Transaction getTransaction(String transactionHash) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (transactionHash != null) {
params.put("transactionHash", transactionHash);
} else {
throw new IllegalArgumentException("transactionHash must not be null!");
}
return this.walletdClient.doRequest(RequestMethod.GET_TRANSACTION, Collections.unmodifiableMap(params), TransactionResponse.class);
}
@Override
public String sendTransaction(List<Transfer> transfers, long fee, int anonymity, String changeAddress, List<String> addresses, String extra, Long unlockTime, String paymentId) throws IridiumWalletdException {
Map<String, Object> params = buildTxParams(transfers, fee, anonymity, changeAddress, addresses, extra, unlockTime, paymentId);
return this.walletdClient.doRequest(RequestMethod.SEND_TRANSACTION, Collections.unmodifiableMap(params), TransactionHashResponse.class);
}
@Override
public String sendTransaction(List<Transfer> transfers, long fee, int anonymity, String address, String extra, Long unlockTime, String paymentId) throws IridiumWalletdException {
return this.sendTransaction(transfers, fee, anonymity, null, Collections.singletonList(address), extra, unlockTime, paymentId);
}
@Override
public String createDelayedTransaction(List<Transfer> transfers, long fee, int anonymity, String changeAddress, List<String> addresses, String extra, Long unlockTime, String paymentId) throws IridiumWalletdException {
Map<String, Object> params = buildTxParams(transfers, fee, anonymity, changeAddress, addresses, extra, unlockTime, paymentId);
return this.walletdClient.doRequest(RequestMethod.CREATE_DELAYED_TRANSACTION, Collections.unmodifiableMap(params), TransactionHashResponse.class);
}
@Override
public String createDelayedTransaction(List<Transfer> transfers, long fee, int anonymity, String address, String extra, Long unlockTime, String paymentId) throws IridiumWalletdException {
return this.createDelayedTransaction(transfers, fee, anonymity, null, Collections.singletonList(address), extra, unlockTime, paymentId);
}
@Override
public List<String> getDelayedTransactionHashes() throws IridiumWalletdException {
return this.walletdClient.doRequest(RequestMethod.GET_DELAYED_TRANSACTION_HASHES, TransactionHashListResponse.class);
}
@Override
public boolean deleteDelayedTransaction(String transactionHash) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (transactionHash != null) {
params.put("transactionHash", transactionHash);
} else {
throw new IllegalArgumentException("transactionHash must not be null!");
}
return this.walletdClient.doRequest(RequestMethod.DELETE_DELAYED_TRANSACTION, Collections.unmodifiableMap(params), NoopResponse.class) != null;
}
@Override
public boolean sendDelayedTransaction(String transactionHash) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (transactionHash != null) {
params.put("transactionHash", transactionHash);
} else {
throw new IllegalArgumentException("transactionHash must not be null!");
}
return this.walletdClient.doRequest(RequestMethod.SEND_DELAYED_TRANSACTION, Collections.unmodifiableMap(params), NoopResponse.class) != null;
}
@Override
public String sendFusionTransaction(long threshold, int anonymity, List<String> addresses, String destinationAddress) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
params.put("threshold", threshold);
params.put("anonymity", anonymity);
List<String> containerAddresses = this.getAddresses();
if (destinationAddress == null) {
if (addresses == null || addresses.isEmpty()) {
if (containerAddresses.size() == 1) {
params.put("destinationAddress", containerAddresses.get(0));
} else {
throw new IllegalArgumentException("destinationAddress must not be null if no addresses specified and container has multiple addresses!");
}
} else {
if (addresses.size() == 1) {
params.put("destinationAddress", addresses.get(0));
} else {
throw new IllegalArgumentException("destinationAddress must not be null if multiple addresses specified!");
}
}
} else {
params.put("destinationAddress", destinationAddress);
}
return this.walletdClient.doRequest(RequestMethod.SEND_FUSION_TRANSACTION, Collections.unmodifiableMap(params), TransactionHashResponse.class);
}
@Override
public String sendFusionTransaction(long threshold, int anonymity, String address) throws IridiumWalletdException {
return this.sendFusionTransaction(threshold, anonymity, Collections.singletonList(address), null);
}
@Override
public EstimatedFusion estimateFusion(long threshold, List<String> addresses) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
params.put("threshold", threshold);
if (addresses != null && !addresses.isEmpty()) {
params.put("addresses", addresses);
}
return this.walletdClient.doRequest(RequestMethod.ESTIMATE_FUSION, Collections.unmodifiableMap(params), EstimatedFusionResponse.class);
}
@Override
public EstimatedFusion estimateFusion(long threshold) throws IridiumWalletdException {
return this.estimateFusion(threshold, null);
}
private Map<String, Object> buildParams() {
return new HashMap<>();
}
private Map<String, Object> buildTxParams(List<Transfer> transfers, long fee, int anonymity, String changeAddress, List<String> addresses, String extra, Long unlockTime, String paymentId) throws IridiumWalletdException {
Map<String, Object> params = buildParams();
if (transfers != null && !transfers.isEmpty()) {
params.put("transfers", transfers);
} else {
throw new IllegalArgumentException("transfers must not be null or empty!");
}
params.put("fee", fee);
params.put("anonymity", anonymity);
if (changeAddress != null) {
params.put("changeAddress", changeAddress);
} else {
List<String> containerAddresses = this.getAddresses();
if (addresses == null || addresses.size() == 0) {
if (containerAddresses.size() > 1) {
throw new IllegalArgumentException("changeAddress must not be null if no addresses specified and current container has more than 1 address!");
} else if (containerAddresses.size() == 1) {
params.put("changeAddress", containerAddresses.get(0));
} else {
throw new IllegalArgumentException("changeAddress must not be null, could not determine any container address!");
}
} else if (addresses.size() > 1 && containerAddresses.size() > 1){
throw new IllegalArgumentException("changeAddress must not be null if multiple addresses specified and current container has more than 1 address!");
} else {
params.put("changeAddress", addresses.get(0));
}
}
if (addresses != null && !addresses.isEmpty()) {
params.put("addresses", addresses);
}
if (extra != null) {
params.put("extra", extra);
}
if (unlockTime != null) {
params.put("unlockTime", unlockTime);
}
if (paymentId != null) {
params.put("paymentId", paymentId);
}
return params;
}
}