In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve JAVA offline signature". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Source code import com.alibaba.fastjson.JSON;import com.google.protobuf.Any;import com.google.protobuf.ByteString;import com.google.protobuf.InvalidProtocolBufferException;import java.util.Map;import org.bouncycastle.util.encoders.Hex;import org.tron.common.crypto.ECKey;import org.tron.common.crypto.Sha256Hash;import org.tron.common.utils.ByteArray;import org.tron.core.exception.CancelException;import org.tron.protos.Contract;import org.tron.protos.Protocol.Block;import org.tron.protos.Protocol.Transaction Import org.tron.walletserver.WalletApi;public class TransactionSignDemo {public static Transaction createTransaction (byte [] from, byte [] to, long amount,long blockTimestamp,long blockHeight,byte [] blockHash) {Transaction.Builder transactionBuilder = Transaction.newBuilder (); / / Block newestBlock = WalletApi.getBlock (- 1); Transaction.Contract.Builder contractBuilder = Transaction.Contract.newBuilder () Contract.TransferContract.Builder transferContractBuilder = Contract.TransferContract.newBuilder (); transferContractBuilder.setAmount (amount); ByteString bsTo = ByteString.copyFrom (to); ByteString bsOwner = ByteString.copyFrom (from); transferContractBuilder.setToAddress (bsTo); transferContractBuilder.setOwnerAddress (bsOwner) Try {Any any = Any.pack (transferContractBuilder.build ()); contractBuilder.setParameter (any);} catch (Exception e) {return null;} contractBuilder.setType (Transaction.Contract.ContractType.TransferContract) TransactionBuilder.getRawDataBuilder () .addContract (contractBuilder) .setTimestamp (System.currentTimeMillis ()) .setExpiration (blockTimestamp + 10 * 60 * 60 * 1000); Transaction transaction = transactionBuilder.build (); Transaction refTransaction = setReference (transaction, blockHeight,blockHash); return refTransaction } public static Transaction setReference (Transaction transaction, long blockHeight,byte [] blockHash) {/ / long blockHeight = newestBlock.getBlockHeader (). GetRawData (). GetNumber (); / / byte [] blockHash = getBlockHash (newestBlock). GetBytes (); byte [] refBlockNum = ByteArray.fromLong (blockHeight) Transaction.raw rawData = transaction.getRawData (). ToBuilder () .setRefBlockHash (ByteString.copyFrom (ByteArray.subArray (blockHash, 8,16)) .setRefBlockBytes (ByteString.copyFrom (ByteArray.subArray (refBlockNum, 6,8)). Build (); return transaction.toBuilder (). SetRawData (rawData). Build () } public static Sha256Hash getBlockHash (Block block) {return Sha256Hash.of (block.getBlockHeader (). GetRawData (). ToByteArray ());} public static String getTransactionHash (Transaction transaction) {String txid = ByteArray.toHexString (Sha256Hash.hash (transaction.getRawData (). ToByteArray (); return txid } private static byte [] signTransaction2Byte (byte [] transaction, byte [] privateKey) throws InvalidProtocolBufferException {ECKey ecKey = ECKey.fromPrivate (privateKey); Transaction transaction1 = Transaction.parseFrom (transaction); byte [] rawdata = transaction1.getRawData (). ToByteArray (); byte [] hash = Sha256Hash.hash (rawdata) Byte [] sign = ecKey.sign (hash). ToByteArray (); return transaction1.toBuilder (). AddSignature (ByteString.copyFrom (sign)). Build (). ToByteArray ();} public static void main (String [] args) throws InvalidProtocolBufferException, CancelException {String privateStr = "your private key"; byte [] privateBytes = ByteArray.fromHexString (privateStr) ECKey ecKey = ECKey.fromPrivate (privateBytes); byte [] from = ecKey.getAddress (); byte [] to = WalletApi.decodeFromBase58Check ("address"); long amount = 100000000L; String blockStr = HttpUtil.get ("https://apilist.tronscan.org/api/block/latest");") Map blockInfo=JSON.parseObject (blockStr); System.out.println (blockInfo.toString ()); Long blockTimestamp = Long.parseLong (blockInfo.get ("timestamp"). ToString (); Long blockHeight = Long.parseLong (blockInfo.get ("number"). ToString ()) Byte [] blockHash = Hex.decode (blockInfo.get ("hash"). ToString ()); / / start looking at the blockHashd rewritten by the official demo, but if it is wrong, there is no problem with the signature, but the broadcast reported an error TAPOS_ERROR, saying that the block is not on the chain. Byte [] blockHash3=Sha256Hash.of (blockInfo.get ("hash"). ToString (). GetBytes (). GetBytes (); System.out.println ("= = blockHash:" + ByteArray.toHexString (blockHash)); System.out.println ("= = blockHash3:" + ByteArray.toHexString (blockHash3)); Transaction transaction = createTransaction (from, to, amount, blockTimestamp, blockHeight, blockHash) Byte [] transactionBytes = transaction.toByteArray (); byte [] transaction4 = signTransaction2Byte (transactionBytes, privateBytes); System.out.println ("transaction4:" + ByteArray.toHexString (transaction4)) / / broadcast URL: https://apilist.tronscan.org/api/broadcast}} "how to implement JAVA offline signature" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.