This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZCashWalletManager.java
executable file
·395 lines (338 loc) · 15.3 KB
/
ZCashWalletManager.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
package com.gravilink.zcash;
import android.util.Log;
import com.google.common.primitives.Bytes;
import com.gravilink.zcash.crypto.Base58;
import com.gravilink.zcash.crypto.BrainKeyDict;
import com.gravilink.zcash.crypto.DumpedPrivateKey;
import com.gravilink.zcash.crypto.ECKey;
import com.gravilink.zcash.crypto.Sha256Hash;
import com.gravilink.zcash.crypto.Utils;
import com.gravilink.zcash.request.AbstractZCashRequest;
import com.gravilink.zcash.request.CreateTransaction_taddr;
import com.gravilink.zcash.request.GetBalance_taddr;
import com.gravilink.zcash.request.GetUTXOSRequest;
import com.gravilink.zcash.request.PushTransaction_taddr;
import com.gravilink.zcash.request.UpdateTransactionCache_taddr;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class ZCashWalletManager {
private static final String explorerAddress = "https://api.zcha.in/v2/mainnet/";
public static final int EXPIRY_HEIGHT_NO_LIMIT = 0;
private Vector<ZCashTransactionDetails_taddr> cachedTansactionDetails_taddr;
public ZCashWalletManager(String nodeAddress, String nodeUser, String nodePassword) throws ZCashException {
String methodName = "ZCashWalletManager counstructor";
checkArgumentNonNull(nodeAddress, "nodeAddress", methodName);
checkArgumentNonNull(nodeUser, "nodeUser", methodName);
checkArgumentNonNull(nodePassword, "nodePassword", methodName);
AbstractZCashRequest.setExplorerAddress(explorerAddress);
AbstractZCashRequest.setAuth(nodeUser, nodePassword);
AbstractZCashRequest.setNodeAddress(nodeAddress);
}
public static String generateNewPrivateKey_taddr() throws ZCashException {
String fullkey = BrainKeyDict.suggestBrainKey(); //Append default sequence number
String privateKey;
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(fullkey.getBytes("UTF-8"));
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] result = sha256.digest(bytes);
privateKey = ECKey.fromPrivate(result, true).getPrivateKeyEncoded().toBase58();
} catch (NoSuchAlgorithmException e) {
throw new ZCashException("NoSuchAlgotithmException in generateNewPrivatKey_taddr.", e);
} catch (UnsupportedEncodingException e) {
throw new ZCashException("UnsupportedEncodingException in generateNewPrivatKey_taddr.", e);
}
return privateKey;
}
public static String publicKeyFromPrivateKey_taddr(String privKey) throws ZCashException {
String methodName = "publicKeyFromPrivateKey_taddr";
checkArgumentNonNull(privKey, "privKey", methodName);
try {
Base58.decodeChecked(privKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid private key.");
}
ECKey key = DumpedPrivateKey.fromBase58(privKey);
byte[] pubKey = key.getPubKeyPoint().getEncoded(key.isCompressed());
pubKey = Sha256Hash.hash(pubKey);
byte[] pubKeyHash = new byte[20];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.update(pubKey, 0, pubKey.length);
ripemd160Digest.doFinal(pubKeyHash, 0);
pubKey = Bytes.concat(new byte[]{(byte) 0x1c, (byte) 0xb8}, pubKeyHash);
// ^~~~~~~~~~~~~~~~~~~~~~~~ mainnet prefix
// pubKey = Bytes.concat(new byte[]{(byte) 0x1d, (byte) 0x25};, pubKeyHash);
// ^~~~~~~~~~~~~~~~~~~~~~~~ testnet prefix
byte[] checksum = Sha256Hash.hashTwice(pubKey);
byte[] summed = Bytes.concat(pubKey, new byte[]{checksum[0], checksum[1], checksum[2], checksum[3]});
return Base58.encode(summed);
}
public void getBalance_taddr(final String pubKey,
final WalletCallback<String, Long> onComplete) throws ZCashException {
String methodName = "getBalance_taddr";
checkArgumentNonNull(pubKey, "pubKey", methodName);
try {
Base58.decodeChecked(pubKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid address.");
}
new Thread(new GetBalance_taddr(pubKey, cachedTansactionDetails_taddr, onComplete)).start();
}
public void createTransaction_taddr(final String fromAddr,
final String toAddr,
final Long amount,
final Long fee,
final String privateKey,
final long minconf,
final WalletCallback<String, ZCashTransaction_taddr> onComplete) throws ZCashException {
createTransaction_taddr(fromAddr, toAddr, amount, fee, privateKey, minconf, EXPIRY_HEIGHT_NO_LIMIT, onComplete);
}
public void createTransaction_taddr(final String fromAddr,
final String toAddr,
final Long amount,
final Long fee,
final String privateKey,
final long minconf,
final int expiryHeight,
final WalletCallback<String, ZCashTransaction_taddr> onComplete) throws ZCashException {
String methodName = "createTransaction_taddr";
checkArgumentNonNull(fromAddr, "fromAddr", methodName);
checkArgumentNonNull(toAddr, "toAddr", methodName);
checkArgumentNonNull(amount, "amount", methodName);
checkArgumentNonNull(fee, "fee", methodName);
checkArgumentNonNull(privateKey, "privateKey", methodName);
checkArgumentNonNull(onComplete, "onComplete", methodName);
try {
Base58.decodeChecked(fromAddr);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid fromAddr.");
}
try {
Base58.decodeChecked(toAddr);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid toAddr.");
}
try {
Base58.decodeChecked(privateKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid private key.");
}
if (!fromAddr.equals(publicKeyFromPrivateKey_taddr(privateKey))) {
throw new ZCashException("fromAddr does not correspond to privateKey in createTransaction_taddr");
}
if (amount < 0) {
throw new ZCashException("Cannot send negative amount of coins.");
}
if (fee < 0) {
throw new ZCashException("Cannot create transaction with negative fee.");
}
if (amount + fee == 0) {
throw new ZCashException("Transaction with amount + fee = 0 would not do anything.");
}
if(expiryHeight < 0 || expiryHeight > 499999999) {
throw new ZCashException("Expiry height must be in [0, 499999999].");
}
new Thread(new GetUTXOSRequest(fromAddr, minconf, new WalletCallback<String, List<ZCashTransactionOutput>>() {
@Override
public void onResponse(String r1, List<ZCashTransactionOutput> r2) {
if (r1.equals("ok")) {
new CreateTransaction_taddr(fromAddr, toAddr, amount, fee, privateKey, expiryHeight, onComplete, r2).run();
} else {
onComplete.onResponse(r1, null);
}
}
})).start();
}
public void pushTransaction_taddr(ZCashTransaction_taddr tx,
WalletCallback<String, Void> onComplete) throws ZCashException {
String methodName = "pushTransaction_taddr";
checkArgumentNonNull(tx, "tx", methodName);
checkArgumentNonNull(onComplete, "onComplete", methodName);
Thread net = new Thread(new PushTransaction_taddr(tx, onComplete));
net.start();
}
public void getTransactionHistory_taddr(String pubKey,
final int limit,
final int offset,
final UpdateRequirement requirement,
final WalletCallback<String, List<ZCashTransactionDetails_taddr>> onComplete) throws ZCashException {
String methodName = "getTransactionHistory_taddr";
checkArgumentNonNull(pubKey, "pubKey", methodName);
checkArgumentNonNull(onComplete, "onComplete", methodName);
try {
Base58.decodeChecked(pubKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid address.");
}
if (limit < 0) {
throw new ZCashException("Negative limit is not allowed in getTransactionHistory_taddr");
}
if (offset < 0) {
throw new ZCashException("Negative offset is not allowed in getTransactionHistory_taddr");
}
if (requirement == UpdateRequirement.NO_UPDATE) {
new Thread(new Runnable() {
@Override
public void run() {
if (cachedTansactionDetails_taddr == null) {
onComplete.onResponse("Wallet is not imported.", null);
return;
}
int start, end;
int size = cachedTansactionDetails_taddr.size();
end = offset > size ? 0 : size - offset;
start = limit > end ? 0 : end - limit;
List<ZCashTransactionDetails_taddr> result = new LinkedList<>(cachedTansactionDetails_taddr.subList(start, end));
Collections.reverse(result);
onComplete.onResponse("ok", result);
}
}).start();
return;
}
Thread net = new Thread(new UpdateTransactionCache_taddr(cachedTansactionDetails_taddr, false, pubKey, new WalletCallback<String, Void>() {
@Override
public void onResponse(String r1, Void r2) {
if (r1.equals("ok") || requirement == UpdateRequirement.TRY_UPDATE) {
int start, end;
synchronized (cachedTansactionDetails_taddr) {
if (cachedTansactionDetails_taddr == null) {
onComplete.onResponse("Wallet is not imported.", null);
return;
}
int size = cachedTansactionDetails_taddr.size();
end = offset > size ? 0 : size - offset;
start = limit > end ? 0 : end - limit;
List<ZCashTransactionDetails_taddr> result = new LinkedList<>(cachedTansactionDetails_taddr.subList(start, end));
Collections.reverse(result);
onComplete.onResponse("ok", result);
}
} else {
onComplete.onResponse(r1, null);
}
}
}));
net.start();
}
public void importWallet_taddr(String privateKey,
final UpdateRequirement requirement,
final WalletCallback<String, Void> onComplete) throws ZCashException {
String methodName = "importWallet_taddr";
checkArgumentNonNull(privateKey, "privateKey", methodName);
checkArgumentNonNull(onComplete, "onComplete", methodName);
try {
Base58.decodeChecked(privateKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid private key.");
}
cachedTansactionDetails_taddr = new Vector<>();
String pubKey;
if (requirement == UpdateRequirement.NO_UPDATE) {
new Thread(new Runnable() {
@Override
public void run() {
onComplete.onResponse("ok", null);
}
}).start();
} else {
pubKey = publicKeyFromPrivateKey_taddr(privateKey);
new Thread(new UpdateTransactionCache_taddr(cachedTansactionDetails_taddr, true, pubKey, new WalletCallback<String, Void>() {
@Override
public void onResponse(String r1, Void r2) {
if (r1.equals("ok") || requirement == UpdateRequirement.TRY_UPDATE) {
onComplete.onResponse("ok", null);
} else {
onComplete.onResponse(r1, null);
}
}
})).start();
}
}
public void importWallet_taddr(String privateKey,
Vector<ZCashTransactionDetails_taddr> cache,
final UpdateRequirement requirement,
final WalletCallback<String, Void> onComplete) throws ZCashException {
String methodName = "importWallet_taddr";
checkArgumentNonNull(privateKey, "privateKey", methodName);
checkArgumentNonNull(cache, "cache", methodName);
checkArgumentNonNull(onComplete, "onComplete", methodName);
try {
Base58.decodeChecked(privateKey);
} catch (IllegalArgumentException e) {
throw new ZCashException("Invalid private key.");
}
if (cache.size() > 1) {
ZCashTransactionDetails_taddr first = cache.get(0);
for (int i = 1; i < cache.size(); i++) {
ZCashTransactionDetails_taddr second = cache.get(i);
if (first.blockHeight > second.blockHeight) {
throw new ZCashException("Transactions in cache must be sorted in ascending order.");
}
first = second;
}
}
cachedTansactionDetails_taddr = cache;
String pubKey;
if (requirement == UpdateRequirement.NO_UPDATE) {
new Thread(new Runnable() {
@Override
public void run() {
onComplete.onResponse("ok", null);
}
}).start();
} else {
pubKey = publicKeyFromPrivateKey_taddr(privateKey);
new Thread(new UpdateTransactionCache_taddr(cachedTansactionDetails_taddr, false, pubKey, new WalletCallback<String, Void>() {
@Override
public void onResponse(String r1, Void r2) {
if (r1.equals("ok") || requirement == UpdateRequirement.TRY_UPDATE) {
onComplete.onResponse("ok", null);
} else {
onComplete.onResponse(r1, null);
}
}
})).start();
}
}
public static String generateNewPrivateKey_zaddr() throws ZCashException {
throw new ZCashException("Unimplemented");
}
public static String publicKeyFromPrivateKey_zaddr(String privKey) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public void getBalance_zaddr(String pubKey, WalletCallback<String, Long> onComplete) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public ZCashTransaction_zaddr createTransaction_zaddr(String fromAddr, String toAddr, BigInteger amount, BigDecimal fee, String memo, String privateKey) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public void pushTransaction_zaddr(ZCashTransaction_zaddr tx, WalletCallback<String, Void> onComplete) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public void getTransactionHistory_zaddr(String pubKey, int limit, int offset, WalletCallback<String, List<ZCashTransactionDetails_zaddr>> onComplete) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public void importWallet_zaddr(String privateKey, WalletCallback<String, Void> onComplete) throws ZCashException {
throw new ZCashException("Unimplemented");
}
public Vector<ZCashTransactionDetails_taddr> getTransactionCache() {
return cachedTansactionDetails_taddr;
}
private static void checkArgumentNonNull(Object o, String name, String methodName) throws ZCashException {
if (o == null) {
throw new ZCashException(String.format("Parameter %s of %s is null.", name, methodName));
}
}
public enum UpdateRequirement {
NO_UPDATE,
TRY_UPDATE,
REQUIRE_UPDATE
}
}