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
·334 lines (300 loc) · 11.5 KB
/
Wallet.java
File metadata and controls
executable file
·334 lines (300 loc) · 11.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
/*
* 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.List;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.tron.api.GrpcAPI.AccountList;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.NumberMessage;
import org.tron.api.GrpcAPI.NumberMessage.Builder;
import org.tron.api.GrpcAPI.WitnessList;
import org.tron.common.application.Application;
import org.tron.common.crypto.ECKey;
import org.tron.common.overlay.message.Message;
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.TransactionCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.db.AccountStore;
import org.tron.core.db.BlockStore;
import org.tron.core.db.Manager;
import org.tron.core.db.UtxoStore;
import org.tron.core.exception.BadItemException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.ItemNotFoundException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.net.message.TransactionMessage;
import org.tron.core.net.node.Node;
import org.tron.protos.Contract.AccountCreateContract;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.ParticipateAssetIssueContract;
import org.tron.protos.Contract.TransferAssetContract;
import org.tron.protos.Contract.TransferContract;
import org.tron.protos.Contract.VoteWitnessContract;
import org.tron.protos.Contract.WitnessCreateContract;
import org.tron.protos.Contract.WitnessUpdateContract;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.TXOutput;
import org.tron.protos.Protocol.Transaction;
@Slf4j
public class Wallet {
private BlockStore db;
@Getter
private final ECKey ecKey;
@Getter
private UtxoStore utxoStore;
private Application app;
private Node p2pnode;
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());
}
/**
* constructor.
*/
public Wallet(Application app) {
this.app = app;
this.p2pnode = app.getP2pNode();
this.db = app.getBlockStoreS();
utxoStore = app.getDbManager().getUtxoStore();
dbManager = app.getDbManager();
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 (address == null || address.length == 0) {
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 boolean addressValid(String addressStr) {
if (addressStr == null || "".equals(addressStr)) {
logger.warn("Warning: Address is empty !!");
return false;
}
try {
byte[] address = ByteArray.fromHexString(addressStr);
return addressValid(address);
} catch (Exception e) {
logger.error(e.getMessage());
return false;
}
}
/**
* Get balance by address.
*/
public long getBalance(byte[] address) {
long balance = utxoStore.findUtxo(address).stream().mapToLong(TXOutput::getValue).sum();
logger.info("balance = {}", balance);
return balance;
}
public Account getBalance(Account account) {
AccountStore accountStore = dbManager.getAccountStore();
AccountCapsule accountCapsule = accountStore.get(account.getAddress().toByteArray());
return accountCapsule == null ? null : 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.
*/
public Transaction createTransaction(TransferContract contract) {
AccountStore accountStore = dbManager.getAccountStore();
return new TransactionCapsule(contract, accountStore).getInstance();
}
/**
* Broadcast a transaction.
*/
public boolean broadcastTransaction(Transaction signaturedTransaction) {
TransactionCapsule trx = new TransactionCapsule(signaturedTransaction);
try {
if (trx.validateSignature()) {
Message message = new TransactionMessage(signaturedTransaction);
dbManager.pushTransactions(trx);
p2pnode.broadcast(message);
return true;
}
} catch (ValidateSignatureException e) {
logger.debug(e.getMessage(), e);
} catch (ContractValidateException e) {
logger.debug(e.getMessage(), e);
} catch (ContractExeException e) {
logger.debug(e.getMessage(), e);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
return false;
}
public Transaction createAccount(AccountCreateContract contract) {
AccountStore accountStore = dbManager.getAccountStore();
return new TransactionCapsule(contract, accountStore).getInstance();
}
public Transaction createTransaction(VoteWitnessContract voteWitnessContract) {
return new TransactionCapsule(voteWitnessContract).getInstance();
}
public Transaction createTransaction(AssetIssueContract assetIssueContract) {
return new TransactionCapsule(assetIssueContract).getInstance();
}
public Transaction createTransaction(WitnessCreateContract witnessCreateContract) {
return new TransactionCapsule(witnessCreateContract).getInstance();
}
public Transaction createTransaction(WitnessUpdateContract witnessUpdateContract) {
return new TransactionCapsule(witnessUpdateContract).getInstance();
}
public Block getNowBlock() {
Sha256Hash headBlockId = dbManager.getHeadBlockId();
try {
return dbManager.getBlockById(headBlockId).getInstance();
} catch (BadItemException e) {
logger.info(e.getMessage());
return null;
} catch (ItemNotFoundException e) {
logger.info(e.getMessage());
return null;
}
}
public Block getBlockByNum(long blockNum) {
Sha256Hash headBlockId = null;
try {
headBlockId = dbManager.getBlockIdByNum(blockNum);
} catch (BadItemException e) {
logger.info(e.getMessage());
} catch (ItemNotFoundException e) {
logger.info(e.getMessage());
}
try {
return dbManager.getBlockById(headBlockId).getInstance();
} catch (BadItemException e) {
logger.info(e.getMessage());
return null;
} catch (ItemNotFoundException e) {
logger.info(e.getMessage());
return null;
}
}
public AccountList getAllAccounts() {
AccountList.Builder builder = AccountList.newBuilder();
List<AccountCapsule> accountCapsuleList =
dbManager.getAccountStore().getAllAccounts();
accountCapsuleList.forEach(accountCapsule -> builder.addAccounts(accountCapsule.getInstance()));
return builder.build();
}
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 Transaction createTransaction(TransferAssetContract transferAssetContract) {
return new TransactionCapsule(transferAssetContract).getInstance();
}
public Transaction createTransaction(
ParticipateAssetIssueContract participateAssetIssueContract) {
return new TransactionCapsule(participateAssetIssueContract).getInstance();
}
public AssetIssueList getAssetIssueList() {
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
dbManager.getAssetIssueStore().getAllAssetIssues()
.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 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() {
Builder builder = NumberMessage.newBuilder()
.setNum(dbManager.getTransactionStore().getTotalTransactions());
return builder.build();
}
}