forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallet.java
More file actions
executable file
·469 lines (421 loc) · 17.5 KB
/
Wallet.java
File metadata and controls
executable file
·469 lines (421 loc) · 17.5 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.core;
import com.google.protobuf.ByteString;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.tron.api.GrpcAPI;
import org.tron.api.GrpcAPI.AccountNetMessage;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.BlockList;
import org.tron.api.GrpcAPI.NumberMessage;
import org.tron.api.GrpcAPI.Return.response_code;
import org.tron.api.GrpcAPI.WitnessList;
import org.tron.common.crypto.ECKey;
import org.tron.common.overlay.message.Message;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Sha256Hash;
import org.tron.common.utils.Utils;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.db.AccountStore;
import org.tron.core.db.BandwidthProcessor;
import org.tron.core.db.Manager;
import org.tron.core.db.PendingManager;
import org.tron.core.exception.AccountResourceInsufficientException;
import org.tron.core.exception.BadItemException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.DupTransactionException;
import org.tron.core.exception.StoreException;
import org.tron.core.exception.TaposException;
import org.tron.core.exception.TooBigTransactionException;
import org.tron.core.exception.TransactionExpirationException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.net.message.TransactionMessage;
import org.tron.core.net.node.NodeImpl;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.TransferContract;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.TransactionSign;
@Slf4j
@Component
public class Wallet {
@Getter
private final ECKey ecKey;
@Autowired
private NodeImpl p2pNode;
@Autowired
private Manager dbManager;
private static String addressPreFixString = Constant.ADD_PRE_FIX_STRING_TESTNET; //default testnet
private static byte addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_TESTNET;
/**
* Creates a new Wallet with a random ECKey.
*/
public Wallet() {
this.ecKey = new ECKey(Utils.getRandom());
}
/**
* Creates a Wallet with an existing ECKey.
*/
public Wallet(final ECKey ecKey) {
this.ecKey = ecKey;
logger.info("wallet address: {}", ByteArray.toHexString(this.ecKey.getAddress()));
}
public byte[] getAddress() {
return ecKey.getAddress();
}
public static String getAddressPreFixString() {
return addressPreFixString;
}
public static void setAddressPreFixString(String addressPreFixString) {
Wallet.addressPreFixString = addressPreFixString;
}
public static byte getAddressPreFixByte() {
return addressPreFixByte;
}
public static void setAddressPreFixByte(byte addressPreFixByte) {
Wallet.addressPreFixByte = addressPreFixByte;
}
public static boolean addressValid(byte[] address) {
if (ArrayUtils.isEmpty(address)) {
logger.warn("Warning: Address is empty !!");
return false;
}
if (address.length != Constant.ADDRESS_SIZE / 2) {
logger.warn(
"Warning: Address length need " + Constant.ADDRESS_SIZE + " but " + address.length
+ " !!");
return false;
}
if (address[0] != addressPreFixByte) {
logger.warn("Warning: Address need prefix with " + addressPreFixByte + " but "
+ address[0] + " !!");
return false;
}
//Other rule;
return true;
}
public static String encode58Check(byte[] input) {
byte[] hash0 = Sha256Hash.hash(input);
byte[] hash1 = Sha256Hash.hash(hash0);
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
}
private static byte[] decode58Check(String input) {
byte[] decodeCheck = Base58.decode(input);
if (decodeCheck.length <= 4) {
return null;
}
byte[] decodeData = new byte[decodeCheck.length - 4];
System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
byte[] hash0 = Sha256Hash.hash(decodeData);
byte[] hash1 = Sha256Hash.hash(hash0);
if (hash1[0] == decodeCheck[decodeData.length] &&
hash1[1] == decodeCheck[decodeData.length + 1] &&
hash1[2] == decodeCheck[decodeData.length + 2] &&
hash1[3] == decodeCheck[decodeData.length + 3]) {
return decodeData;
}
return null;
}
public static byte[] decodeFromBase58Check(String addressBase58) {
if (StringUtils.isEmpty(addressBase58)) {
logger.warn("Warning: Address is empty !!");
return null;
}
byte[] address = decode58Check(addressBase58);
if (address == null) {
return null;
}
if (!addressValid(address)) {
return null;
}
return address;
}
public Account getAccount(Account account) {
AccountStore accountStore = dbManager.getAccountStore();
AccountCapsule accountCapsule = accountStore.get(account.getAddress().toByteArray());
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
return accountCapsule.getInstance();
}
/**
* Create a transaction.
*/
/*public Transaction createTransaction(byte[] address, String to, long amount) {
long balance = getBalance(address);
return new TransactionCapsule(address, to, amount, balance, utxoStore).getInstance();
} */
/**
* Create a transaction by contract.
*/
@Deprecated
public Transaction createTransaction(TransferContract contract) {
AccountStore accountStore = dbManager.getAccountStore();
return new TransactionCapsule(contract, accountStore).getInstance();
}
/**
* Broadcast a transaction.
*/
public GrpcAPI.Return broadcastTransaction(Transaction signaturedTransaction) {
GrpcAPI.Return.Builder builder = GrpcAPI.Return.newBuilder();
try {
TransactionCapsule trx = new TransactionCapsule(signaturedTransaction);
Message message = new TransactionMessage(signaturedTransaction);
if (dbManager.isTooManyPending()) {
logger.debug(
"Manager is busy, pending transaction count:{}, discard the new coming transaction",
(dbManager.getPendingTransactions().size() + PendingManager.getTmpTransactions()
.size()));
return builder.setResult(false).setCode(response_code.SERVER_BUSY).build();
}
if (dbManager.isGeneratingBlock()) {
logger.debug("Manager is generating block, discard the new coming transaction");
return builder.setResult(false).setCode(response_code.SERVER_BUSY).build();
}
if (dbManager.getTransactionIdCache().getIfPresent(trx.getTransactionId()) != null) {
logger.debug("This transaction has been processed, discard the transaction");
return builder.setResult(false).setCode(response_code.DUP_TRANSACTION_ERROR).build();
} else {
dbManager.getTransactionIdCache().put(trx.getTransactionId(), true);
}
dbManager.pushTransactions(trx);
p2pNode.broadcast(message);
return builder.setResult(true).setCode(response_code.SUCCESS).build();
} catch (ValidateSignatureException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.SIGERROR)
.setMessage(ByteString.copyFromUtf8("validate signature error"))
.build();
} catch (ContractValidateException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.CONTRACT_VALIDATE_ERROR)
.setMessage(ByteString.copyFromUtf8("contract validate error"))
.build();
} catch (ContractExeException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.CONTRACT_EXE_ERROR)
.setMessage(ByteString.copyFromUtf8("contract execute error"))
.build();
} catch (AccountResourceInsufficientException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.BANDWITH_ERROR)
.setMessage(ByteString.copyFromUtf8("AccountResourceInsufficient error"))
.build();
} catch (DupTransactionException e) {
logger.info("dup trans" + e.getMessage());
return builder.setResult(false).setCode(response_code.DUP_TRANSACTION_ERROR)
.setMessage(ByteString.copyFromUtf8("dup transaction"))
.build();
} catch (TaposException e) {
logger.info("tapos error" + e.getMessage());
return builder.setResult(false).setCode(response_code.TAPOS_ERROR)
.setMessage(ByteString.copyFromUtf8("Tapos check error"))
.build();
} catch (TooBigTransactionException e) {
logger.info("transaction error" + e.getMessage());
return builder.setResult(false).setCode(response_code.TOO_BIG_TRANSACTION_ERROR)
.setMessage(ByteString.copyFromUtf8("transaction size is too big"))
.build();
} catch (TransactionExpirationException e) {
logger.info("transaction expired" + e.getMessage());
return builder.setResult(false).setCode(response_code.TRANSACTION_EXPIRATION_ERROR)
.setMessage(ByteString.copyFromUtf8("transaction expired"))
.build();
} catch (Exception e) {
logger.info("exception caught" + e.getMessage());
return builder.setResult(false).setCode(response_code.OTHER_ERROR)
.setMessage(ByteString.copyFromUtf8("other error"))
.build();
}
}
public TransactionCapsule getTransactionSign(TransactionSign transactionSign) {
byte[] privateKey = transactionSign.getPrivateKey().toByteArray();
TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction());
trx.sign(privateKey);
return trx;
}
public Block getNowBlock() {
List<BlockCapsule> blockList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockList)) {
return null;
} else {
return blockList.get(0).getInstance();
}
}
public Block getBlockByNum(long blockNum) {
try {
return dbManager.getBlockByNum(blockNum).getInstance();
} catch (StoreException e) {
logger.info(e.getMessage());
return null;
}
}
public WitnessList getWitnessList() {
WitnessList.Builder builder = WitnessList.newBuilder();
List<WitnessCapsule> witnessCapsuleList = dbManager.getWitnessStore().getAllWitnesses();
witnessCapsuleList
.forEach(witnessCapsule -> builder.addWitnesses(witnessCapsule.getInstance()));
return builder.build();
}
public AssetIssueList getAssetIssueList() {
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
dbManager.getAssetIssueStore().getAllAssetIssues()
.forEach(issueCapsule -> builder.addAssetIssue(issueCapsule.getInstance()));
return builder.build();
}
public AssetIssueList getAssetIssueList(long offset, long limit) {
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
List<AssetIssueCapsule> assetIssueList = dbManager.getAssetIssueStore()
.getAssetIssuesPaginated(offset, limit);
if (null == assetIssueList || assetIssueList.size() == 0) {
return null;
}
assetIssueList.forEach(issueCapsule -> builder.addAssetIssue(issueCapsule.getInstance()));
return builder.build();
}
public AssetIssueList getAssetIssueByAccount(ByteString accountAddress) {
if (accountAddress == null || accountAddress.size() == 0) {
return null;
}
List<AssetIssueCapsule> assetIssueCapsuleList = dbManager.getAssetIssueStore()
.getAllAssetIssues();
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
assetIssueCapsuleList.stream()
.filter(assetIssueCapsule -> assetIssueCapsule.getOwnerAddress().equals(accountAddress))
.forEach(issueCapsule -> {
builder.addAssetIssue(issueCapsule.getInstance());
});
return builder.build();
}
public AccountNetMessage getAccountNet(ByteString accountAddress) {
if (accountAddress == null || accountAddress.size() == 0) {
return null;
}
AccountNetMessage.Builder builder = AccountNetMessage.newBuilder();
AccountCapsule accountCapsule = dbManager.getAccountStore().get(accountAddress.toByteArray());
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
Map<String, Long> assetNetLimitMap = new HashMap<>();
accountCapsule.getAllFreeAssetNetUsage().keySet().forEach(asset -> {
byte[] key = ByteArray.fromString(asset);
assetNetLimitMap.put(asset, dbManager.getAssetIssueStore().get(key).getFreeAssetNetLimit());
});
builder.setFreeNetUsed(accountCapsule.getFreeNetUsage())
.setFreeNetLimit(freeNetLimit)
.setNetUsed(accountCapsule.getNetUsage())
.setNetLimit(netLimit)
.setTotalNetLimit(totalNetLimit)
.setTotalNetWeight(totalNetWeight)
.putAllAssetNetUsed(accountCapsule.getAllFreeAssetNetUsage())
.putAllAssetNetLimit(assetNetLimitMap);
return builder.build();
}
public AssetIssueContract getAssetIssueByName(ByteString assetName) {
if (assetName == null || assetName.size() == 0) {
return null;
}
List<AssetIssueCapsule> assetIssueCapsuleList = dbManager.getAssetIssueStore()
.getAllAssetIssues();
for (AssetIssueCapsule assetIssueCapsule : assetIssueCapsuleList) {
if (assetName.equals(assetIssueCapsule.getName())) {
return assetIssueCapsule.getInstance();
}
}
return null;
}
public NumberMessage totalTransaction() {
NumberMessage.Builder builder = NumberMessage.newBuilder()
.setNum(dbManager.getTransactionStore().getTotalTransactions());
return builder.build();
}
public NumberMessage getNextMaintenanceTime() {
NumberMessage.Builder builder = NumberMessage.newBuilder()
.setNum(dbManager.getDynamicPropertiesStore().getNextMaintenanceTime());
return builder.build();
}
public Block getBlockById(ByteString BlockId) {
if (Objects.isNull(BlockId)) {
return null;
}
Block block = null;
try {
block = dbManager.getBlockStore().get(BlockId.toByteArray()).getInstance();
} catch (StoreException e) {
}
return block;
}
public BlockList getBlocksByLimitNext(long number, long limit) {
if (limit <= 0) {
return null;
}
BlockList.Builder blockListBuilder = BlockList.newBuilder();
dbManager.getBlockStore().getLimitNumber(number, limit).forEach(
blockCapsule -> blockListBuilder.addBlock(blockCapsule.getInstance()));
return blockListBuilder.build();
}
public BlockList getBlockByLatestNum(long getNum) {
BlockList.Builder blockListBuilder = BlockList.newBuilder();
dbManager.getBlockStore().getBlockByLatestNum(getNum).forEach(
blockCapsule -> blockListBuilder.addBlock(blockCapsule.getInstance()));
return blockListBuilder.build();
}
public Transaction getTransactionById(ByteString transactionId) {
if (Objects.isNull(transactionId)) {
return null;
}
TransactionCapsule transactionCapsule = null;
try {
transactionCapsule = dbManager.getTransactionStore()
.get(transactionId.toByteArray());
} catch (BadItemException e) {
}
if (transactionCapsule != null) {
return transactionCapsule.getInstance();
}
return null;
}
}