本篇內容介紹了“怎么實現(xiàn)JAVA離線簽名”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
滄縣網站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、響應式網站開發(fā)等網站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選成都創(chuàng)新互聯(lián)。
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 = "你的私鑰"; byte[] privateBytes = ByteArray.fromHexString(privateStr); ECKey ecKey = ECKey.fromPrivate(privateBytes); byte[] from = ecKey.getAddress(); byte[] to = WalletApi.decodeFromBase58Check("地址"); long amount = 100_000_000L; String blockStr = HttpUtil.get("https://apilist.tronscan.org/api/block/latest"); Map<String, Object> 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()); //開始看官方demo改寫的blockHashd,但是錯的,簽名沒的問題,但是廣播就報錯TAPOS_ERROR,說塊不在鏈上。 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)); //廣播的URL:https://apilist.tronscan.org/api/broadcast } }
“怎么實現(xiàn)JAVA離線簽名”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網站,小編將為大家輸出更多高質量的實用文章!
新聞名稱:怎么實現(xiàn)JAVA離線簽名
文章分享:http://aaarwkj.com/article18/jegigp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制網站、電子商務、標簽優(yōu)化、ChatGPT、域名注冊、移動網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)