本篇內(nèi)容介紹了“怎么實(shí)現(xiàn)JAVA離線簽名”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
滄縣網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(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,但是錯(cuò)的,簽名沒的問(wèn)題,但是廣播就報(bào)錯(cuò)TAPOS_ERROR,說(shuō)塊不在鏈上。 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 } }
“怎么實(shí)現(xiàn)JAVA離線簽名”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
新聞名稱:怎么實(shí)現(xiàn)JAVA離線簽名
文章分享:http://m.newbst.com/article18/jegigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、電子商務(wù)、標(biāo)簽優(yōu)化、ChatGPT、域名注冊(cè)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)