|
| 1 | +package org.consenlabs.tokencore.wallet.transaction; |
| 2 | + |
| 3 | +import com.google.protobuf.Any; |
| 4 | +import com.google.protobuf.ByteString; |
| 5 | +import org.consenlabs.tokencore.wallet.Wallet; |
| 6 | +import org.tron.common.crypto.ECKey; |
| 7 | +import org.tron.common.crypto.Sha256Sm3Hash; |
| 8 | +import org.tron.common.utils.ByteArray; |
| 9 | +import org.tron.common.utils.TransactionUtils; |
| 10 | +import org.tron.protos.Protocol; |
| 11 | +import org.tron.protos.contract.BalanceContract; |
| 12 | +import org.tron.walletserver.WalletApi; |
| 13 | + |
| 14 | +public class TronTransaction implements TransactionSigner { |
| 15 | + |
| 16 | + private final String from; |
| 17 | + |
| 18 | + private final String to; |
| 19 | + |
| 20 | + private final Long amount; |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + public TronTransaction(String from, String to, Long amount) { |
| 25 | + this.from = from; |
| 26 | + this.to = to; |
| 27 | + this.amount = amount; |
| 28 | + } |
| 29 | + |
| 30 | + public static Protocol.Transaction setReference(Protocol.Transaction transaction,Protocol.Block newestBlock) { |
| 31 | + long blockHeight = newestBlock.getBlockHeader().getRawData().getNumber(); |
| 32 | + |
| 33 | + byte[] blockHash = getBlockHash(newestBlock).getBytes(); |
| 34 | + byte[] refBlockNum = ByteArray.fromLong(blockHeight); |
| 35 | + Protocol.Transaction.raw rawData = transaction.getRawData().toBuilder() |
| 36 | + .setRefBlockHash(ByteString.copyFrom(ByteArray.subArray(blockHash, 8, 16))) |
| 37 | + .setRefBlockBytes(ByteString.copyFrom(ByteArray.subArray(refBlockNum, 6, 8))) |
| 38 | + .build(); |
| 39 | + return transaction.toBuilder().setRawData(rawData).build(); |
| 40 | + } |
| 41 | + |
| 42 | + public static Sha256Sm3Hash getBlockHash(Protocol.Block block) { |
| 43 | + return Sha256Sm3Hash.of(block.getBlockHeader().getRawData().toByteArray()); |
| 44 | + } |
| 45 | + |
| 46 | + public String getTransactionHash(Protocol.Transaction transaction) { |
| 47 | + return ByteArray.toHexString(Sha256Sm3Hash.hash(transaction.getRawData().toByteArray())); |
| 48 | + } |
| 49 | + |
| 50 | + public static Protocol.Transaction createTransaction(byte[] from, byte[] to, long amount) { |
| 51 | + Protocol.Block newestBlock=WalletApi.getBlock(-1); |
| 52 | + Protocol.Transaction.Builder transactionBuilder = Protocol.Transaction.newBuilder(); |
| 53 | + Protocol.Transaction.Contract.Builder contractBuilder = Protocol.Transaction.Contract.newBuilder(); |
| 54 | + BalanceContract.TransferContract.Builder transferContractBuilder = BalanceContract.TransferContract.newBuilder(); |
| 55 | + transferContractBuilder.setAmount(amount); |
| 56 | + ByteString bsTo = ByteString.copyFrom(to); |
| 57 | + ByteString bsOwner = ByteString.copyFrom(from); |
| 58 | + transferContractBuilder.setToAddress(bsTo); |
| 59 | + transferContractBuilder.setOwnerAddress(bsOwner); |
| 60 | + try { |
| 61 | + Any any = Any.pack(transferContractBuilder.build()); |
| 62 | + contractBuilder.setParameter(any); |
| 63 | + } catch (Exception e) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + contractBuilder.setType(Protocol.Transaction.Contract.ContractType.TransferContract); |
| 67 | + transactionBuilder.getRawDataBuilder().addContract(contractBuilder) |
| 68 | + .setTimestamp(System.currentTimeMillis()) |
| 69 | + .setExpiration(newestBlock.getBlockHeader().getRawData().getTimestamp() + 10 * 60 * 60 * 1000); |
| 70 | + Protocol.Transaction transaction = transactionBuilder.build(); |
| 71 | + return setReference(transaction, newestBlock); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @Override |
| 76 | + public TxSignResult signTransaction(String chainId, String password, Wallet wallet) { |
| 77 | + String privateStr = wallet.exportPrivateKey(password); |
| 78 | + byte[] privateBytes = ByteArray.fromHexString(privateStr); |
| 79 | + ECKey ecKey = ECKey.fromPrivate(privateBytes); |
| 80 | + Protocol.Transaction transaction = createTransaction(WalletApi.decodeFromBase58Check(from), WalletApi.decodeFromBase58Check(to), amount); |
| 81 | + assert transaction != null; |
| 82 | + transaction = TransactionUtils.sign(transaction, ecKey); |
| 83 | + String txHash=getTransactionHash(transaction); |
| 84 | + return new TxSignResult(ByteArray.toHexString(transaction.toByteArray()), txHash); |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) { |
| 88 | + |
| 89 | + String from="TGA1KxMbBvYWL2C4iirBPMz3Lt1BFoHB1D"; |
| 90 | + String to="TGA1KxMbBvYWL2C4iirBPMz3Lt1BFoHB1D"; |
| 91 | + long amount=0L; |
| 92 | + Protocol.Transaction transaction = createTransaction(WalletApi.decodeFromBase58Check(from), WalletApi.decodeFromBase58Check(to), amount); |
| 93 | + assert transaction != null; |
| 94 | + System.out.println(ByteArray.toHexString(transaction.toByteArray())); |
| 95 | + |
| 96 | + |
| 97 | + } |
| 98 | +} |
| 99 | + |
0 commit comments